首页 > 代码库 > 基于SG2D制作的项目资源管理器

基于SG2D制作的项目资源管理器

    假期使用SG2D制作了一个开发辅助工具——“项目资源管理器”支持Windows和MacOSX,下面是其3个主要功能:

  1.添加键值对表。支持添加文本,URL和其他类型的键值对。文本键值对兼容Android项目,导入Android项目后便可通过R类常量来获取字符串。

  2.生成项目依赖资源文件的索引,指定资源目录和资源搜索起始目录(资源目录必须是资源搜索起始目录的子孙目录)并勾选生成资源索引复选框,便会将资源索引信息保存在资源目录下的resources.xml文件中。

  3.同步各平台项目之间依赖的资源文件,拷贝源目录中的所有子孙文件复制到下面的各平台目标路径中。

 

 

 

点击“生成索引文件”按钮将会生成两个索引文件,一个是resources.h头文件放在代码目录下,另一个则是resources.xml文件放在资源目录下。

 

resources.h文件内容如下所示:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.** This file was automatically generated by the Resource Manager.* It should not be modified by hand.**-------------- Powered By SG2D & GeequlimExtends --------------*/#ifndef __RESOURCES_RESOURCES_H__#define __RESOURCES_RESOURCES_H__namespace R{	const int string_app_name = 0;	const int string_error = 1;	const int string_warning = 2;	const int path_d_search_dir = 3;	const int path_d_src_dir = 4;	const int path_f_DirectoryListor_bin_fileListrOSX = 5;	const int path_f_DirectoryListor_bin_fileListrLinux = 6;	const int path_f_DirectoryListor_bin_DirectoryListor_exe = 7;	const int path_d_DirectoryListor_bin_ = 8;	const int path_f_DirectoryListor_src_main_cpp = 9;	const int path_f_DirectoryListor_src_FileHelper_cpp = 10;	const int path_f_DirectoryListor_src_FileHelper_h = 11;	const int path_d_DirectoryListor_src_ = 12;	const int path_d_DirectoryListor_ = 13;	const int path_f_AndroidConnector_AndroidConnector_cpp = 14;	const int path_d_AndroidConnector_ = 15;	const int path_text_resources_xml = 16;};#endif

 

 resources.xml文件内容如下所示:

<?xml version="1.0" encoding="utf-8"?><resources>	<string	name="string_app_name"	id="0"	>应用名称</string>	<string	name="string_error"	id="1"	>错误</string>	<string	name="string_warning"	id="2"	>警告</string>	<path	name="path_d_search_dir"	id="3"	>consoleTools</path>	<path	name="path_d_src_dir"	id="4"	>F:\C++</path>	<path	name="path_f_DirectoryListor_bin_fileListrOSX"	id="5"	>DirectoryListor\bin\fileListrOSX</path>	<path	name="path_f_DirectoryListor_bin_fileListrLinux"	id="6"	>DirectoryListor\bin\fileListrLinux</path>	<path	name="path_f_DirectoryListor_bin_DirectoryListor_exe"	id="7"	>DirectoryListor\bin\DirectoryListor.exe</path>	<path	name="path_d_DirectoryListor_bin_"	id="8"	>DirectoryListor\bin</path>	<path	name="path_f_DirectoryListor_src_main_cpp"	id="9"	>DirectoryListor\src\main.cpp</path>	<path	name="path_f_DirectoryListor_src_FileHelper_cpp"	id="10"	>DirectoryListor\src\FileHelper.cpp</path>	<path	name="path_f_DirectoryListor_src_FileHelper_h"	id="11"	>DirectoryListor\src\FileHelper.h</path>	<path	name="path_d_DirectoryListor_src_"	id="12"	>DirectoryListor\src</path>	<path	name="path_d_DirectoryListor_"	id="13"	>DirectoryListor</path>	<path	name="path_f_AndroidConnector_AndroidConnector_cpp"	id="14"	>AndroidConnector\AndroidConnector.cpp</path>	<path	name="path_d_AndroidConnector_"	id="15"	>AndroidConnector</path>	<path	name="path_text_resources_xml"	id="16"	>resources.xml</path></resources>

 

 

工作原理:

  通过resources.h中的索引值在对应的resources.xml文件中查找相应id的文本。

 

SG2D索引解析类:ResHelper

 

ResHelper.h

  

class ResHelper : public Object		{		public:			ResHelper(const String& filePath);			ResHelper(File* file);			~ResHelper();			/**@brief 从xml文件中加载字符串			@param const String& name 字符串名称属性值			@return 获取到的字符串,查找不到返回NullStr			*/			const String getStringByName(const String& name)const;			/**			@brief 通过id获取字符串			@param id为ResourceManager生成的文档resources.h中命名空间R中的常量			@return 获取到的字符串			*/			const String getStringByID(const int id)const;			/**			@brief 通过id获取路径			@param id为ResourceManager生成的文档resources.h中命名空间R中的常量			@return 获取到的路径			*/			const String getPathByID(const int id)const;		protected:						const String getStringByAtrribut(const String& name, const String& value)const;			/**xml资源文件对象*/			SG2DFD::XMLDocument* m_pFileDoc;			/**xml资源文件根节点*/			XMLNode* m_pRootNode;		private:			typedef Object super;		};

 

ResHelper.cpp

#include "stdafx.h"#include "ResHelper.h"            ResHelper::ResHelper(const String& filePath) :super(), m_pRootNode(NULL) 		{						StreamWriter strm;			LocalFile::loadFileData(filePath, strm);			m_pFileDoc = new SG2DFD::XMLDocument();			if (m_pFileDoc->load(strm).length())				traceError(-1, m_pFileDoc->load(strm).ptr());			else				m_pRootNode = m_pFileDoc->documentNode();		}		ResHelper::ResHelper(File* file) :super()		{			StreamWriter strm;			file->loadToStream(strm);			m_pFileDoc = new SG2DFD::XMLDocument();			if (m_pFileDoc->load(strm).length())				traceError(-1, m_pFileDoc->load(strm).ptr());			else				m_pRootNode = m_pFileDoc->documentNode();		}		ResHelper::~ResHelper()		{			if (m_pFileDoc)				m_pFileDoc->release();			m_pFileDoc = NULL;		}		const String ResHelper::getStringByAtrribut(const String& name, const String& value)const		{			XMLNode * curNode = m_pRootNode->firstChild();			while (curNode)			{				if (curNode->getAttributeValue(name).compare(value) == false)					return curNode->text();				curNode = curNode->nextSibling();			}			return NullStr;		}		const String ResHelper::getStringByName(const String& name)const		{			return getStringByAtrribut("name", name);		}		const String ResHelper::getStringByID(const int id)const		{			UTF8String str_id;			str_id = str_id.format("%d", id);			return getStringByAtrribut("id", str_id);		}		const String ResHelper::getPathByID(const int id)const		{			UTF8String strPath = getStringByName("path_d_search_dir");			strPath += "/";			strPath += getStringByID(id);			return strPath;		}    

  

 

SG2D项目使用示例:

1.把resources.h和ResHelper.h添加到项目中,把resources.xml拷贝到项目资源目下(可以使用项目资源管理器来一键同步多平台)

2.在stdfax.h文件中添加如下代码

//引入资源助手所需头文件#include "ResHelper.h"#include "resources.h"//声明资源助手单例extern ResHelper *resHelper;

 

3.使用ResHelper对象

......//定义resHelperResHelper * resHelper = NULL;ApplicationDelegate::ApplicationDelegate(){    //创建ResHelper 对象     resHelper = new ResHelper("resources.xml");}ApplicationDelegate::onStart(Stage *pStage){    //获取字符串    String appName = resHelper->getStringByID( R::string_app_name );    //获取文件路径    String filePath = resHelper->getPathByID( R::path_text_resources_xml );}ApplicationDelegate::~ApplicationDelegate(){  //销毁ResHelper 对象   if(resHelper )  {       resHelper->release();       resHelper = NULL;     }    }

 其他开发框架请自行编写xml解析代码,欢迎补充。

 

下面给出工具的下载地址: http://pan.baidu.com/s/1c09xwlU