axmol/cocos/platform/apple/CCFileUtils-apple.mm

544 lines
15 KiB
Plaintext
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#import <Foundation/Foundation.h>
2014-09-10 07:50:02 +08:00
#include "CCFileUtils-apple.h"
Squashed commit of the following: commit b9181ae92541d11119bedd766773ddb01f0bb486 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:34:40 2014 -0700 Compiles luasocket on Mac commit 91fc79aa2101867fedfbcd7d846e9508009894e6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:12:30 2014 -0700 disables luasockets commit 31f2ee462708ab7b604b9399eaabd275e2da9e02 Merge: 19099fd 0abe4cb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 08:10:48 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into cmakefile_mac_osx Conflicts: cocos/3d/cocos3d.h commit 19099fd021b4ecf1f2eebd73ea14e84572d1ee33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:21:03 2014 -0700 compiles on Linux commit eec7b92817ac9208123468bb882612aad89f94bf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:16:46 2014 -0700 all projecs compiles with CMake commit 40fcc8325b409a580fdbf26d2ee27aa2d1db0109 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:47:27 2014 -0700 CCUserDefualt.mm better includes commit 30709ec8243562858498cb118240bb45bf9b33b0 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:26:36 2014 -0700 cpp-tests compiles and works on Mac using CMake commit 67f0fc8df887d08d3d7a27c5fdadd335d6841676 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 20:12:09 2014 -0700 Adds missing files to CMakeLists.txt commit 36f939ea7f157ca851aee138300c13d9888c0078 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:52:34 2014 -0700 Adds more box2d missing files commit 00f81e3b5ec97150974cc6022e86c394ff488ccb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:39:54 2014 -0700 Adds missing box2d files commit 9673227b4d7d0e6d9eced49bdd69b98cb519968a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 18:44:15 2014 -0700 Initial CMake support for Mac
2014-09-10 03:18:31 +08:00
#include <ftw.h>
#include <string>
#include <stack>
Squashed commit of the following: commit b9181ae92541d11119bedd766773ddb01f0bb486 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:34:40 2014 -0700 Compiles luasocket on Mac commit 91fc79aa2101867fedfbcd7d846e9508009894e6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:12:30 2014 -0700 disables luasockets commit 31f2ee462708ab7b604b9399eaabd275e2da9e02 Merge: 19099fd 0abe4cb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 08:10:48 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into cmakefile_mac_osx Conflicts: cocos/3d/cocos3d.h commit 19099fd021b4ecf1f2eebd73ea14e84572d1ee33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:21:03 2014 -0700 compiles on Linux commit eec7b92817ac9208123468bb882612aad89f94bf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:16:46 2014 -0700 all projecs compiles with CMake commit 40fcc8325b409a580fdbf26d2ee27aa2d1db0109 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:47:27 2014 -0700 CCUserDefualt.mm better includes commit 30709ec8243562858498cb118240bb45bf9b33b0 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:26:36 2014 -0700 cpp-tests compiles and works on Mac using CMake commit 67f0fc8df887d08d3d7a27c5fdadd335d6841676 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 20:12:09 2014 -0700 Adds missing files to CMakeLists.txt commit 36f939ea7f157ca851aee138300c13d9888c0078 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:52:34 2014 -0700 Adds more box2d missing files commit 00f81e3b5ec97150974cc6022e86c394ff488ccb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:39:54 2014 -0700 Adds missing box2d files commit 9673227b4d7d0e6d9eced49bdd69b98cb519968a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 18:44:15 2014 -0700 Initial CMake support for Mac
2014-09-10 03:18:31 +08:00
2014-04-30 08:37:36 +08:00
#include "base/CCDirector.h"
Squashed commit of the following: commit b9181ae92541d11119bedd766773ddb01f0bb486 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:34:40 2014 -0700 Compiles luasocket on Mac commit 91fc79aa2101867fedfbcd7d846e9508009894e6 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 09:12:30 2014 -0700 disables luasockets commit 31f2ee462708ab7b604b9399eaabd275e2da9e02 Merge: 19099fd 0abe4cb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Tue Sep 9 08:10:48 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into cmakefile_mac_osx Conflicts: cocos/3d/cocos3d.h commit 19099fd021b4ecf1f2eebd73ea14e84572d1ee33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:21:03 2014 -0700 compiles on Linux commit eec7b92817ac9208123468bb882612aad89f94bf Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 23:16:46 2014 -0700 all projecs compiles with CMake commit 40fcc8325b409a580fdbf26d2ee27aa2d1db0109 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:47:27 2014 -0700 CCUserDefualt.mm better includes commit 30709ec8243562858498cb118240bb45bf9b33b0 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 21:26:36 2014 -0700 cpp-tests compiles and works on Mac using CMake commit 67f0fc8df887d08d3d7a27c5fdadd335d6841676 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 20:12:09 2014 -0700 Adds missing files to CMakeLists.txt commit 36f939ea7f157ca851aee138300c13d9888c0078 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:52:34 2014 -0700 Adds more box2d missing files commit 00f81e3b5ec97150974cc6022e86c394ff488ccb Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 19:39:54 2014 -0700 Adds missing box2d files commit 9673227b4d7d0e6d9eced49bdd69b98cb519968a Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Mon Sep 8 18:44:15 2014 -0700 Initial CMake support for Mac
2014-09-10 03:18:31 +08:00
#include "deprecated/CCString.h"
#include "deprecated/CCDictionary.h"
#include "platform/CCFileUtils.h"
#include "platform/CCSAXParser.h"
NS_CC_BEGIN
static void addValueToDict(id nsKey, id nsValue, ValueMap& dict);
static void addObjectToNSDict(const std::string& key, const Value& value, NSMutableDictionary *dict);
static void addItemToArray(id item, ValueVector& array)
{
// add string value into array
if ([item isKindOfClass:[NSString class]])
{
array.push_back(Value([item UTF8String]));
return;
}
// add number value into array(such as int, float, bool and so on)
// the value is a number
if ([item isKindOfClass:[NSNumber class]])
{
NSNumber* num = item;
const char* numType = [num objCType];
if(num == (void*)kCFBooleanFalse || num == (void*)kCFBooleanTrue)
{
2016-02-16 17:01:54 +08:00
bool v = [num boolValue];
array.push_back(Value(v));
}
else if(strcmp(numType, @encode(float)) == 0)
{
array.push_back(Value([num floatValue]));
}
else if(strcmp(numType, @encode(double)) == 0)
{
array.push_back(Value([num doubleValue]));
}
else{
array.push_back(Value([num intValue]));
}
return;
}
// add dictionary value into array
if ([item isKindOfClass:[NSDictionary class]])
{
ValueMap dict;
for (id subKey in [item allKeys])
{
id subValue = [item objectForKey:subKey];
addValueToDict(subKey, subValue, dict);
}
array.push_back(Value(dict));
return;
}
// add array value into array
if ([item isKindOfClass:[NSArray class]])
{
ValueVector subArray;
for (id subItem in item)
{
addItemToArray(subItem, subArray);
}
array.push_back(Value(subArray));
return;
}
}
static void addObjectToNSArray(const Value& value, NSMutableArray *array)
{
// add string into array
if (value.getType() == Value::Type::STRING)
{
NSString *element = [NSString stringWithCString:value.asString().c_str() encoding:NSUTF8StringEncoding];
[array addObject:element];
return;
}
2014-03-18 14:59:59 +08:00
//add float into array
if (value.getType() == Value::Type::FLOAT) {
NSNumber *number = [NSNumber numberWithFloat:value.asFloat()];
[array addObject:number];
}
2014-03-18 14:59:59 +08:00
//add double into array
if (value.getType() == Value::Type::DOUBLE) {
NSNumber *number = [NSNumber numberWithDouble:value.asDouble()];
[array addObject:number];
}
2014-03-18 14:59:59 +08:00
//add boolean into array
if (value.getType() == Value::Type::BOOLEAN) {
NSNumber *element = [NSNumber numberWithBool:value.asBool()];
[array addObject:element];
}
2014-03-18 14:59:59 +08:00
if (value.getType() == Value::Type::INTEGER) {
NSNumber *element = [NSNumber numberWithInt:value.asInt()];
[array addObject:element];
}
2014-03-18 14:59:59 +08:00
//todo: add date and data support
// add array into array
if (value.getType() == Value::Type::VECTOR)
{
NSMutableArray *element = [NSMutableArray array];
ValueVector valueArray = value.asValueVector();
for (const auto &e : valueArray)
{
addObjectToNSArray(e, element);
}
[array addObject:element];
return;
}
// add dictionary value into array
if (value.getType() == Value::Type::MAP)
{
NSMutableDictionary *element = [NSMutableDictionary dictionary];
auto valueDict = value.asValueMap();
for (auto iter = valueDict.begin(); iter != valueDict.end(); ++iter)
{
addObjectToNSDict(iter->first, iter->second, element);
}
[array addObject:element];
}
}
static void addValueToDict(id nsKey, id nsValue, ValueMap& dict)
{
// the key must be a string
CCASSERT([nsKey isKindOfClass:[NSString class]], "The key should be a string!");
std::string key = [nsKey UTF8String];
// the value is a string
if ([nsValue isKindOfClass:[NSString class]])
{
dict[key] = Value([nsValue UTF8String]);
return;
}
// the value is a number
if ([nsValue isKindOfClass:[NSNumber class]])
{
NSNumber* num = nsValue;
const char* numType = [num objCType];
if(num == (void*)kCFBooleanFalse || num == (void*)kCFBooleanTrue)
{
bool v = [num boolValue];
dict[key] = Value(v);
}
else if(strcmp(numType, @encode(float)) == 0)
{
dict[key] = Value([num floatValue]);
}
else if(strcmp(numType, @encode(double)) == 0)
{
dict[key] = Value([num doubleValue]);
}
else{
dict[key] = Value([num intValue]);
}
return;
}
2014-03-18 14:59:59 +08:00
// the value is a new dictionary
if ([nsValue isKindOfClass:[NSDictionary class]])
{
ValueMap subDict;
for (id subKey in [nsValue allKeys])
{
id subValue = [nsValue objectForKey:subKey];
addValueToDict(subKey, subValue, subDict);
}
dict[key] = Value(subDict);
return;
}
// the value is a array
if ([nsValue isKindOfClass:[NSArray class]])
{
ValueVector valueArray;
for (id item in nsValue)
{
addItemToArray(item, valueArray);
}
dict[key] = Value(valueArray);
return;
}
}
static void addObjectToNSDict(const std::string& key, const Value& value, NSMutableDictionary *dict)
{
NSString *NSkey = [NSString stringWithCString:key.c_str() encoding:NSUTF8StringEncoding];
// the object is a Dictionary
if (value.getType() == Value::Type::MAP)
{
NSMutableDictionary *dictElement = [NSMutableDictionary dictionary];
ValueMap subDict = value.asValueMap();
for (auto iter = subDict.begin(); iter != subDict.end(); ++iter)
{
addObjectToNSDict(iter->first, iter->second, dictElement);
}
[dict setObject:dictElement forKey:NSkey];
return;
}
2014-03-18 14:59:59 +08:00
//add float into dict
if (value.getType() == Value::Type::FLOAT) {
NSNumber *number = [NSNumber numberWithFloat:value.asFloat()];
[dict setObject:number forKey:NSkey];
}
2014-03-18 14:59:59 +08:00
//add double into dict
if (value.getType() == Value::Type::DOUBLE) {
NSNumber *number = [NSNumber numberWithDouble:value.asDouble()];
[dict setObject:number forKey:NSkey];
}
2014-03-18 14:59:59 +08:00
//add boolean into dict
if (value.getType() == Value::Type::BOOLEAN) {
NSNumber *element = [NSNumber numberWithBool:value.asBool()];
[dict setObject:element forKey:NSkey];
}
2014-03-18 14:59:59 +08:00
//add integer into dict
if (value.getType() == Value::Type::INTEGER) {
NSNumber *element = [NSNumber numberWithInt:value.asInt()];
[dict setObject:element forKey:NSkey];
}
// the object is a String
if (value.getType() == Value::Type::STRING)
{
NSString *strElement = [NSString stringWithCString:value.asString().c_str() encoding:NSUTF8StringEncoding];
[dict setObject:strElement forKey:NSkey];
return;
}
// the object is a Array
if (value.getType() == Value::Type::VECTOR)
{
NSMutableArray *arrElement = [NSMutableArray array];
ValueVector array = value.asValueVector();
for(const auto& v : array)
{
addObjectToNSArray(v, arrElement);
}
[dict setObject:arrElement forKey:NSkey];
return;
}
}
FileUtilsApple::FileUtilsApple() {
_bundle = [NSBundle mainBundle];
}
void FileUtilsApple::setBundle(NSBundle* bundle) {
_bundle = bundle;
}
NSBundle* FileUtilsApple::getBundle() const {
return _bundle;
}
#pragma mark - FileUtils
static NSFileManager* s_fileManager = [NSFileManager defaultManager];
FileUtils* FileUtils::getInstance()
{
if (s_sharedFileUtils == nullptr)
{
s_sharedFileUtils = new (std::nothrow) FileUtilsApple();
2013-07-23 20:44:08 +08:00
if(!s_sharedFileUtils->init())
{
delete s_sharedFileUtils;
s_sharedFileUtils = nullptr;
CCLOG("ERROR: Could not init CCFileUtilsApple");
2013-07-23 20:44:08 +08:00
}
}
return s_sharedFileUtils;
}
std::string FileUtilsApple::getWritablePath() const
{
if (_writablePath.length())
{
return _writablePath;
}
// save to document folder
2013-02-06 18:11:27 +08:00
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
std::string strRet = [documentsDirectory UTF8String];
strRet.append("/");
return strRet;
}
bool FileUtilsApple::isFileExistInternal(const std::string& filePath) const
{
if (filePath.empty())
{
return false;
}
bool ret = false;
if (filePath[0] != '/')
{
std::string path;
std::string file;
size_t pos = filePath.find_last_of("/");
if (pos != std::string::npos)
{
file = filePath.substr(pos+1);
path = filePath.substr(0, pos+1);
}
else
{
file = filePath;
}
NSString* fullpath = [getBundle() pathForResource:[NSString stringWithUTF8String:file.c_str()]
ofType:nil
inDirectory:[NSString stringWithUTF8String:path.c_str()]];
if (fullpath != nil) {
ret = true;
}
}
else
{
// Search path is an absolute path.
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:filePath.c_str()]]) {
ret = true;
}
}
return ret;
}
static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
{
auto ret = remove(fpath);
if (ret)
{
log("Fail to remove: %s ",fpath);
}
return ret;
}
bool FileUtilsApple::removeDirectory(const std::string& path)
{
if (path.size() > 0 && path[path.size() - 1] != '/')
{
CCLOGERROR("Fail to remove directory, path must termniate with '/': %s", path.c_str());
return false;
}
if (nftw(path.c_str(),unlink_cb, 64, FTW_DEPTH | FTW_PHYS))
return false;
else
return true;
}
std::string FileUtilsApple::getFullPathForDirectoryAndFilename(const std::string& directory, const std::string& filename) const
{
if (directory[0] != '/')
{
NSString* fullpath = [getBundle() pathForResource:[NSString stringWithUTF8String:filename.c_str()]
ofType:nil
inDirectory:[NSString stringWithUTF8String:directory.c_str()]];
if (fullpath != nil) {
return [fullpath UTF8String];
}
}
else
{
std::string fullPath = directory+filename;
// Search path is an absolute path.
if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:fullPath.c_str()]]) {
return fullPath;
}
}
return "";
}
ValueMap FileUtilsApple::getValueMapFromFile(const std::string& filename)
{
std::string fullPath = fullPathForFilename(filename);
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:path];
ValueMap ret;
if (dict != nil)
{
for (id key in [dict allKeys])
{
id value = [dict objectForKey:key];
addValueToDict(key, value, ret);
}
}
return ret;
}
ValueMap FileUtilsApple::getValueMapFromData(const char* filedata, int filesize)
{
NSData* file = [NSData dataWithBytes:filedata length:filesize];
NSPropertyListFormat format;
NSError* error;
NSDictionary* dict = [NSPropertyListSerialization propertyListWithData:file options:NSPropertyListImmutable format:&format error:&error];
ValueMap ret;
if (dict != nil)
{
for (id key in [dict allKeys])
{
id value = [dict objectForKey:key];
addValueToDict(key, value, ret);
}
}
return ret;
}
bool FileUtilsApple::writeToFile(const ValueMap& dict, const std::string &fullPath)
{
return writeValueMapToFile(dict, fullPath);
}
bool FileUtils::writeValueMapToFile(const ValueMap& dict, const std::string& fullPath)
{
//CCLOG("iOS||Mac Dictionary %d write to file %s", dict->_ID, fullPath.c_str());
NSMutableDictionary *nsDict = [NSMutableDictionary dictionary];
for (auto iter = dict.begin(); iter != dict.end(); ++iter)
{
addObjectToNSDict(iter->first, iter->second, nsDict);
}
NSString *file = [NSString stringWithUTF8String:fullPath.c_str()];
2013-04-16 12:38:53 +08:00
// do it atomically
[nsDict writeToFile:file atomically:YES];
return true;
}
bool FileUtils::writeValueVectorToFile(const ValueVector& vecData, const std::string& fullPath)
{
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
NSMutableArray* array = [NSMutableArray array];
for (const auto &e : vecData)
{
addObjectToNSArray(e, array);
}
[array writeToFile:path atomically:YES];
return true;
}
ValueVector FileUtilsApple::getValueVectorFromFile(const std::string& filename)
{
// NSString* pPath = [NSString stringWithUTF8String:pFileName];
// NSString* pathExtension= [pPath pathExtension];
// pPath = [pPath stringByDeletingPathExtension];
// pPath = [[NSBundle mainBundle] pathForResource:pPath ofType:pathExtension];
// fixing cannot read data using Array::createWithContentsOfFile
std::string fullPath = fullPathForFilename(filename);
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
NSArray* array = [NSArray arrayWithContentsOfFile:path];
ValueVector ret;
for (id value in array)
{
addItemToArray(value, ret);
}
return ret;
}
NS_CC_END