mirror of https://github.com/axmolengine/axmol.git
Unify plist implementation, remove apple specific sources.
This commit is contained in:
parent
8cc189fb4d
commit
3e95b8ccf3
|
@ -48,8 +48,6 @@ NS_CC_BEGIN
|
||||||
|
|
||||||
// Implement DictMaker
|
// Implement DictMaker
|
||||||
|
|
||||||
#if (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC)
|
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
SAX_NONE = 0,
|
SAX_NONE = 0,
|
||||||
|
@ -348,7 +346,6 @@ ValueVector FileUtils::getValueVectorFromFile(const std::string& filename) const
|
||||||
return tMaker.arrayWithContentsOfFile(fullPath);
|
return tMaker.arrayWithContentsOfFile(fullPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* forward statement
|
* forward statement
|
||||||
*/
|
*/
|
||||||
|
@ -442,15 +439,8 @@ static void generateElementForArray(const ValueVector& array, pugi::xml_node& pa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#else
|
|
||||||
|
|
||||||
/* The subclass FileUtilsApple should override these two method. */
|
|
||||||
ValueMap FileUtils::getValueMapFromFile(const std::string& /*filename*/) const {return ValueMap();}
|
|
||||||
ValueMap FileUtils::getValueMapFromData(const char* /*filedata*/, int /*filesize*/) const {return ValueMap();}
|
|
||||||
ValueVector FileUtils::getValueVectorFromFile(const std::string& /*filename*/) const {return ValueVector();}
|
|
||||||
bool FileUtils::writeToFile(const ValueMap& /*dict*/, const std::string &/*fullPath*/) const {return false;}
|
|
||||||
|
|
||||||
#endif /* (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) && (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) */
|
|
||||||
|
|
||||||
// Implement FileUtils
|
// Implement FileUtils
|
||||||
FileUtils* FileUtils::s_sharedFileUtils = nullptr;
|
FileUtils* FileUtils::s_sharedFileUtils = nullptr;
|
||||||
|
|
|
@ -52,11 +52,6 @@ public:
|
||||||
virtual std::string getWritablePath() const override;
|
virtual std::string getWritablePath() const override;
|
||||||
virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const override;
|
virtual std::string getFullPathForFilenameWithinDirectory(const std::string& directory, const std::string& filename) const override;
|
||||||
|
|
||||||
virtual ValueMap getValueMapFromFile(const std::string& filename) const override;
|
|
||||||
virtual ValueMap getValueMapFromData(const char* filedata, int filesize) const override;
|
|
||||||
virtual bool writeToFile(const ValueMap& dict, const std::string& fullPath) const override;
|
|
||||||
|
|
||||||
virtual ValueVector getValueVectorFromFile(const std::string& filename) const override;
|
|
||||||
#if CC_FILEUTILS_APPLE_ENABLE_OBJC
|
#if CC_FILEUTILS_APPLE_ENABLE_OBJC
|
||||||
void setBundle(NSBundle* bundle);
|
void setBundle(NSBundle* bundle);
|
||||||
#endif
|
#endif
|
||||||
|
@ -67,8 +62,6 @@ public:
|
||||||
private:
|
private:
|
||||||
virtual bool isFileExistInternal(const std::string& filePath) const override;
|
virtual bool isFileExistInternal(const std::string& filePath) const override;
|
||||||
virtual bool removeDirectory(const std::string& dirPath) const override;
|
virtual bool removeDirectory(const std::string& dirPath) const override;
|
||||||
virtual void valueMapCompact(ValueMap& valueMap) const override;
|
|
||||||
virtual void valueVectorCompact(ValueVector& valueVector) const override;
|
|
||||||
|
|
||||||
struct IMPL;
|
struct IMPL;
|
||||||
std::unique_ptr<IMPL> pimpl_;
|
std::unique_ptr<IMPL> pimpl_;
|
||||||
|
|
|
@ -35,8 +35,6 @@ THE SOFTWARE.
|
||||||
|
|
||||||
#include "base/CCDirector.h"
|
#include "base/CCDirector.h"
|
||||||
#include "platform/CCFileUtils.h"
|
#include "platform/CCFileUtils.h"
|
||||||
#include "platform/CCSAXParser.h"
|
|
||||||
|
|
||||||
|
|
||||||
#define DECLARE_GUARD std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)
|
#define DECLARE_GUARD std::lock_guard<std::recursive_mutex> mutexGuard(_mutex)
|
||||||
|
|
||||||
|
@ -54,153 +52,6 @@ private:
|
||||||
NSBundle* bundle_;
|
NSBundle* bundle_;
|
||||||
};
|
};
|
||||||
|
|
||||||
static id convertCCValueToNSObject(const cocos2d::Value &value);
|
|
||||||
static cocos2d::Value convertNSObjectToCCValue(id object);
|
|
||||||
|
|
||||||
static void addNSObjectToCCMap(id nsKey, id nsValue, ValueMap& dict);
|
|
||||||
static void addCCValueToNSDictionary(const std::string& key, const Value& value, NSMutableDictionary *dict);
|
|
||||||
static void addNSObjectToCCVector(id item, ValueVector& array);
|
|
||||||
static void addCCValueToNSArray(const Value& value, NSMutableArray *array);
|
|
||||||
|
|
||||||
static id convertCCValueToNSObject(const cocos2d::Value &value)
|
|
||||||
{
|
|
||||||
switch (value.getType())
|
|
||||||
{
|
|
||||||
case Value::Type::NONE:
|
|
||||||
return [NSNull null];
|
|
||||||
|
|
||||||
case Value::Type::STRING:
|
|
||||||
return [NSString stringWithCString:value.asString().c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
|
|
||||||
case Value::Type::BYTE:
|
|
||||||
return [NSNumber numberWithInt:value.toByte()];
|
|
||||||
|
|
||||||
case Value::Type::INTEGER:
|
|
||||||
return [NSNumber numberWithInt:value.toInt()];
|
|
||||||
|
|
||||||
case Value::Type::UNSIGNED:
|
|
||||||
return [NSNumber numberWithUnsignedInt:value.toUnsignedInt()];
|
|
||||||
|
|
||||||
case Value::Type::FLOAT:
|
|
||||||
return [NSNumber numberWithFloat:value.toFloat()];
|
|
||||||
|
|
||||||
case Value::Type::DOUBLE:
|
|
||||||
return [NSNumber numberWithDouble:value.toDouble()];
|
|
||||||
|
|
||||||
case Value::Type::BOOLEAN:
|
|
||||||
return [NSNumber numberWithBool:value.toBool()];
|
|
||||||
|
|
||||||
case Value::Type::VECTOR: {
|
|
||||||
NSMutableArray *array = [NSMutableArray array];
|
|
||||||
const ValueVector &vector = value.asValueVector();
|
|
||||||
for (const auto &e : vector) {
|
|
||||||
addCCValueToNSArray(e, array);
|
|
||||||
}
|
|
||||||
return array;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Value::Type::MAP: {
|
|
||||||
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
|
|
||||||
const ValueMap &map = value.asValueMap();
|
|
||||||
for (auto& iter : map) {
|
|
||||||
addCCValueToNSDictionary(iter.first, iter.second, dictionary);
|
|
||||||
}
|
|
||||||
return dictionary;
|
|
||||||
}
|
|
||||||
|
|
||||||
case Value::Type::INT_KEY_MAP:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
return [NSNull null];
|
|
||||||
}
|
|
||||||
|
|
||||||
static cocos2d::Value convertNSObjectToCCValue(id item)
|
|
||||||
{
|
|
||||||
// add string value into array
|
|
||||||
if ([item isKindOfClass:[NSString class]])
|
|
||||||
{
|
|
||||||
return Value([item UTF8String]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
bool v = [num boolValue];
|
|
||||||
return Value(v);
|
|
||||||
}
|
|
||||||
else if(strcmp(numType, @encode(float)) == 0)
|
|
||||||
{
|
|
||||||
return Value([num floatValue]);
|
|
||||||
}
|
|
||||||
else if(strcmp(numType, @encode(double)) == 0)
|
|
||||||
{
|
|
||||||
return Value([num doubleValue]);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Value([num intValue]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// add dictionary value into array
|
|
||||||
if ([item isKindOfClass:[NSDictionary class]])
|
|
||||||
{
|
|
||||||
ValueMap dict;
|
|
||||||
for (id subKey in [item allKeys])
|
|
||||||
{
|
|
||||||
id subValue = [item objectForKey:subKey];
|
|
||||||
addNSObjectToCCMap(subKey, subValue, dict);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Value(dict);
|
|
||||||
}
|
|
||||||
|
|
||||||
// add array value into array
|
|
||||||
if ([item isKindOfClass:[NSArray class]])
|
|
||||||
{
|
|
||||||
ValueVector subArray;
|
|
||||||
for (id subItem in item)
|
|
||||||
{
|
|
||||||
addNSObjectToCCVector(subItem, subArray);
|
|
||||||
}
|
|
||||||
return Value(subArray);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Value::Null;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addNSObjectToCCVector(id item, ValueVector& array)
|
|
||||||
{
|
|
||||||
array.push_back(convertNSObjectToCCValue(item));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addCCValueToNSArray(const Value& value, NSMutableArray *array)
|
|
||||||
{
|
|
||||||
[array addObject:convertCCValueToNSObject(value)];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addNSObjectToCCMap(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];
|
|
||||||
dict[key] = convertNSObjectToCCValue(nsValue);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void addCCValueToNSDictionary(const std::string& key, const Value& value, NSMutableDictionary *dict)
|
|
||||||
{
|
|
||||||
NSString *NSkey = [NSString stringWithCString:key.c_str() encoding:NSUTF8StringEncoding];
|
|
||||||
[dict setObject:convertCCValueToNSObject(value) forKey:NSkey];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
FileUtilsApple::FileUtilsApple() : pimpl_(new IMPL([NSBundle mainBundle])) {
|
FileUtilsApple::FileUtilsApple() : pimpl_(new IMPL([NSBundle mainBundle])) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -365,140 +216,6 @@ std::string FileUtilsApple::getFullPathForFilenameWithinDirectory(const std::str
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
ValueMap FileUtilsApple::getValueMapFromFile(const std::string& filename) const
|
|
||||||
{
|
|
||||||
auto d(FileUtils::getInstance()->getDataFromFile(filename));
|
|
||||||
return getValueMapFromData(reinterpret_cast<char*>(d.getBytes()), static_cast<int>(d.getSize()));
|
|
||||||
}
|
|
||||||
|
|
||||||
ValueMap FileUtilsApple::getValueMapFromData(const char* filedata, int filesize) const
|
|
||||||
{
|
|
||||||
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];
|
|
||||||
addNSObjectToCCMap(key, value, ret);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FileUtilsApple::writeToFile(const ValueMap& dict, const std::string &fullPath) const
|
|
||||||
{
|
|
||||||
return writeValueMapToFile(dict, fullPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FileUtils::writeValueMapToFile(const ValueMap& dict, const std::string& fullPath) const
|
|
||||||
{
|
|
||||||
valueMapCompact(const_cast<ValueMap&>(dict));
|
|
||||||
//CCLOG("iOS||Mac Dictionary %d write to file %s", dict->_ID, fullPath.c_str());
|
|
||||||
NSMutableDictionary *nsDict = [NSMutableDictionary dictionary];
|
|
||||||
|
|
||||||
for (auto& iter : dict)
|
|
||||||
{
|
|
||||||
addCCValueToNSDictionary(iter.first, iter.second, nsDict);
|
|
||||||
}
|
|
||||||
|
|
||||||
NSString *file = [NSString stringWithUTF8String:fullPath.c_str()];
|
|
||||||
// do it atomically
|
|
||||||
return [nsDict writeToFile:file atomically:YES];
|
|
||||||
}
|
|
||||||
|
|
||||||
void FileUtilsApple::valueMapCompact(ValueMap& valueMap) const
|
|
||||||
{
|
|
||||||
auto itr = valueMap.begin();
|
|
||||||
while(itr != valueMap.end()){
|
|
||||||
auto vtype = itr->second.getType();
|
|
||||||
switch(vtype){
|
|
||||||
case Value::Type::NONE:{
|
|
||||||
itr = valueMap.erase(itr);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Value::Type::MAP:{
|
|
||||||
valueMapCompact(itr->second.asValueMap());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Value::Type::VECTOR:{
|
|
||||||
valueVectorCompact(itr->second.asValueVector());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
++itr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void FileUtilsApple::valueVectorCompact(ValueVector& valueVector) const
|
|
||||||
{
|
|
||||||
auto itr = valueVector.begin();
|
|
||||||
while(itr != valueVector.end()){
|
|
||||||
auto vtype = (*itr).getType();
|
|
||||||
switch(vtype){
|
|
||||||
case Value::Type::NONE:{
|
|
||||||
itr = valueVector.erase(itr);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Value::Type::MAP:{
|
|
||||||
valueMapCompact((*itr).asValueMap());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case Value::Type::VECTOR:{
|
|
||||||
valueVectorCompact((*itr).asValueVector());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
++itr;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FileUtils::writeValueVectorToFile(const ValueVector& vecData, const std::string& fullPath) const
|
|
||||||
{
|
|
||||||
NSString* path = [NSString stringWithUTF8String:fullPath.c_str()];
|
|
||||||
NSMutableArray* array = [NSMutableArray array];
|
|
||||||
|
|
||||||
for (const auto &e : vecData)
|
|
||||||
{
|
|
||||||
addCCValueToNSArray(e, array);
|
|
||||||
}
|
|
||||||
|
|
||||||
[array writeToFile:path atomically:YES];
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
ValueVector FileUtilsApple::getValueVectorFromFile(const std::string& filename) const
|
|
||||||
{
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
addNSObjectToCCVector(value, ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool FileUtilsApple::createDirectory(const std::string& path) const
|
bool FileUtilsApple::createDirectory(const std::string& path) const
|
||||||
{
|
{
|
||||||
CCASSERT(!path.empty(), "Invalid path");
|
CCASSERT(!path.empty(), "Invalid path");
|
||||||
|
|
Loading…
Reference in New Issue