说明:本文所论述内容均基于cocos2dx 3.0 版本。
1、UserDefault
它是cocos2d-x用来存取基本数据类型用的。保存为XML文件格式。
查看CCUserDefault文件,可以看出,文件名默认为UserDefault.xml。在win32平台,Debug下,该文件在Debug.win32文件夹内。如果该文件不存在,则会创建新文件。
1 // root name of xml 2 #define USERDEFAULT_ROOT_NAME "userDefaultRoot" 3 4 #define XML_FILE_NAME "UserDefault.xml"
用UserDefault
操作方式比较简单:
主要方法:(和java的map很像,键值对,应该很容易懂的)
1 void setBoolForKey(const char* pKey, bool value); 2 void setIntegerForKey(const char* pKey, int value); 3 void setFloatForKey(const char* pKey, float value); 4 void setDoubleForKey(const char* pKey, double value); 5 void setStringForKey(const char* pKey, const std::string & value);
通过键读取数据,如果键不存在,可以设置一个defaultValue返回自己想要的值。
1 bool getBoolForKey(const char* pKey, bool defaultValue = false); 2 int getIntegerForKey(const char* pKey, int defaultValue = 0); 3 float getFloatForKey(const char* pKey, float defaultValue=0.0f); 4 double getDoubleForKey(const char* pKey, double defaultValue=0.0); 5 std::string getStringForKey(const char* pKey, const std::string & defaultValue = "");
UserDefault封装了对XML文件的处理,提供了简单操作文件的方法,但是不足之处在于固定的文件夹,固定的文件名称。有限的数据类型。
2、FileUtils
static FileUtils* getInstance();//获取FileUtils类的实例
在cocos2d-x中,文件读写其实直接使用c/c++中的文件读写操作方法,但是,在实际运用中,由于移动客户端读写文件需要相应的权限,所以读写文件就需要先指定文件的路径,我们不需要获取绝对路径,只需要获取相对路径就行,因为cocos2d-x底层已经做了相应各个平台的处理.
std::string fullPathForFilename(const std::string &filename);//获取文件的完整路径
std::string getStringFromFile(const std::string& filename);//读取文件中的字符串
Data getDataFromFile(const std::string& filename);//获取文件数据
可以看下Data类:
1 class CC_DLL Data 2 { 3 public: 4 static const Data Null; 5 //构造函数 6 Data(); 7 Data(const Data& other); 8 Data(Data&& other); 9 ~Data(); 10 // 重载符号 11 Data& operator= (const Data& other); 12 Data& operator= (Data&& other); 13 14 unsigned char* getBytes() const;//获取数据 15 ssize_t getSize() const;//尺寸 16 void copy(unsigned char* bytes, const ssize_t size);//从bytes复制 17 void fastSet(unsigned char* bytes, const ssize_t size);//从bytes快速set,使用后bytes将不能在外部使用 18 void clear();//清除 19 bool isNull() const;//判空 20 private: 21 void move(Data& other); 22 private: 23 unsigned char* _bytes; 24 ssize_t _size; 25 };
unsigned char* getFileDataFromZip(const std::string& zipFilePath, const std::string& filename, ssize_t *size);//读取压缩文件数据(zip格式)
如果读取成功size中会返回文件的大小,否则返回0。
如果我们通过setSearchPaths()设置搜索路径("/mnt/sdcard/", "internal_dir/"),然后通过setSearchResolutionsOrder()设置子区分路径("resources-ipadhd/", "resources-ipad/", "resources-iphonehd")。如果搜索文件名为'sprite.png' 那么会先在文件查找字典中查找key: sprite.png -> value: sprite.pvr.gz,然后搜索文件'sprite.pvr.gz'如下顺序:
1 /mnt/sdcard/resources-ipadhd/sprite.pvr.gz (if not found, search next) 2 /mnt/sdcard/resources-ipad/sprite.pvr.gz (if not found, search next) 3 /mnt/sdcard/resources-iphonehd/sprite.pvr.gz (if not found, search next) 4 /mnt/sdcard/sprite.pvr.gz (if not found, search next) 5 internal_dir/resources-ipadhd/sprite.pvr.gz (if not found, search next) 6 internal_dir/resources-ipad/sprite.pvr.gz (if not found, search next) 7 internal_dir/resources-iphonehd/sprite.pvr.gz (if not found, search next) 8 internal_dir/sprite.pvr.gz (if not found, return "sprite.png")
If the filename contains relative path like "gamescene/uilayer/sprite.png",
and the mapping in fileLookup dictionary contains `key: gamescene/uilayer/sprite.png -> value: gamescene/uilayer/sprite.pvr.gz`.
The file search order will be:
/mnt/sdcard/gamescene/uilayer/resources-ipadhd/sprite.pvr.gz (if not found, search next)
/mnt/sdcard/gamescene/uilayer/resources-ipad/sprite.pvr.gz (if not found, search next)
/mnt/sdcard/gamescene/uilayer/resources-iphonehd/sprite.pvr.gz (if not found, search next)
/mnt/sdcard/gamescene/uilayer/sprite.pvr.gz (if not found, search next)
internal_dir/gamescene/uilayer/resources-ipadhd/sprite.pvr.gz (if not found, search next)
internal_dir/gamescene/uilayer/resources-ipad/sprite.pvr.gz (if not found, search next)
internal_dir/gamescene/uilayer/resources-iphonehd/sprite.pvr.gz (if not found, search next)
internal_dir/gamescene/uilayer/sprite.pvr.gz (if not found, return "gamescene/uilayer/sprite.png")
If the new file can't be found on the file system, it will return the parameter filename directly.
如果找到返回完整路径,没找到返回'sprite.png'。
void loadFilenameLookupDictionaryFromFile(const std::string &filename);//从文件导入文件名查找字典
文件为plist格式如下:
1 /** 2 * Loads the filenameLookup dictionary from the contents of a filename. 3 * 4 * @note The plist file name should follow the format below: 5 * 6 * @code 7 * <?xml version="1.0" encoding="UTF-8"?> 8 * <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 9 * <plist version="1.0"> 10 * <dict> 11 * <key>filenames</key> 12 * <dict> 13 * <key>sounds/click.wav</key> 14 * <string>sounds/click.caf</string> 15 * <key>sounds/endgame.wav</key> 16 * <string>sounds/endgame.caf</string> 17 * <key>sounds/gem-0.wav</key> 18 * <string>sounds/gem-0.caf</string> 19 * </dict> 20 * <key>metadata</key> 21 * <dict> 22 * <key>version</key> 23 * <integer>1</integer> 24 * </dict> 25 * </dict> 26 * </plist> 27 * @endcode 28 * @param filename The plist file name. 29 * 30 @since v2.1 31 * @js loadFilenameLookup 32 * @lua loadFilenameLookup 33 */
void setFilenameLookupDictionary(const ValueMap& filenameLookupDict);//从ValueMap中设置文件名查找字典
ValueMap的定义:
1 typedef std::unordered_map<std::string, Value> ValueMap;
std::string fullPathFromRelativeFile(const std::string &filename, const std::string &relativeFile);//获取相对应文件的完整路径
e.g. filename: hello.png, pszRelativeFile: /User/path1/path2/hello.plist Return: /User/path1/path2/hello.pvr (If there a a key(hello.png)-value(hello.pvr) in FilenameLookup dictionary. )
void setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder);//设置子搜索区分路径
见fullPathForFilename()。
void addSearchResolutionsOrder(const std::string &order);//增加子搜索路径
const std::vector<std::string>& getSearchResolutionsOrder();//获取子搜索区分路径
void setSearchPaths(const std::vector<std::string>& searchPaths);//设置搜索路径
见fullPathForFilename()。
void addSearchPath(const std::string & path);//增加搜索路径
const std::vector<std::string>& getSearchPaths() const;//获取搜索路径
std::string getWritablePath();//获取一个可写入文件的路径
经过测试在win32平台上,debug版本返回的是程序文件所在的路径,release返回的是“我的文档”路径。
bool isFileExist(const std::string& filePath);//判断文件是否存在
经过测试在win32平台上,如果路径中包含中文字符会找不到文件。所以可以自己写个。
bool isAbsolutePath(const std::string& path);判断是否为绝对路径
void setPopupNotify(bool notify);
bool isPopupNotify();
Sets/Gets 当文件加载失败时弹出messagebox.
ValueMap getValueMapFromFile(const std::string& filename);//从文件获取ValueMap
bool writeToFile(ValueMap& dict, const std::string& fullPath);//写入一个ValueMap数据到plist格式文件
ValueVector getValueVectorFromFile(const std::string& filename);//从文件获取ValueVector
ValueVector定义:
1 typedef std::vector<Value> ValueVector;
函数也就这些了,还没有写demo代码,写好了贴过来。
3.SQLite
4、plist文件读写
在cocos2d-x中,对于plist文件,既可以读取,也可以进行写入的操作;下面先来看一个plist文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"/> 3 4 <plist version="1.0"> 5 <dict> 6 <key>string element key</key> 7 <string>string element value</string> 8 <key>array</key> 9 <array> 10 <dict> 11 <key>string in dictInArray key 0</key> 12 <string>string in dictInArray value 0</string> 13 <key>string in dictInArray key 1</key> 14 <string>string in dictInArray value 1</string> 15 </dict> 16 <string>string in array</string> 17 <array> 18 <string>string 0 in arrayInArray</string> 19 <string>string 1 in arrayInArray</string> 20 </array> 21 </array> 22 <key>dictInDict, Hello World</key> 23 <dict> 24 <key>string in dictInDict key</key> 25 <string>string in dictInDict value</string> 26 <key>bool</key> 27 <true/> 28 <key>integer</key> 29 <integer>1024</integer> 30 <key>float</key> 31 <real>1024.1024170</real> 32 <key>double</key> 33 <real>1024.1230000000000000</real> 34 </dict> 35 </dict> 36 </plist>