g_allXMLPostload = {} require("luaScript.Tools.XMLConfigOptimise") function runXMLPostload(Config) for i , v in pairs(g_allXMLPostload) do v(Config); end end -- 读取xml配置 function loadXMLConfig(configDesc) local xmlPath if configDesc.XMLData == nil then xmlPath = "dataconfig/"; -- 如果有lua文件,则用lua方式解析 if cc.FilePackage:getInstance():isUsePackageFileSystem() then return loadLuaXMLConfig(configDesc.XMLFile , configDesc); else -- 读取转换成lua的配置文件 local luaConfigFile = xmlPath .. configDesc.XMLFile .. ".lua"; if cc.FileUtils:getInstance():isFileExist(luaConfigFile) then return loadLuaXMLConfig(luaConfigFile , configDesc); end end end local result = {} local XMLConfig = {} -- 重新载入所有配置 function XMLConfig:reload() local newDatas = loadXMLConfig(configDesc); local oldResult = {} for i , v in pairs(result) do oldResult[i] = v end -- 删除所有 for i , v in pairs(oldResult) do result[i] = nil; end -- 保存新的 for i , v in pairs(newDatas) do result[i] = v end end function XMLConfig:getConfigDesc() return configDesc; end -- 获取值(不使用默认值) function XMLConfig:get(key) return rawget(self , key); end local xmlFile; local languageRoot; if configDesc.XMLData == nil then local xmlFileName = xmlPath .. configDesc.XMLFile; print("开始读取XML配置:" .. xmlFileName); xmlFile = xml.load(xmlFileName); else if configDesc.IsXMLObject then xmlFile = configDesc.XMLData else xmlFile = xml.eval(configDesc.XMLData); end end local root = xmlFile:find(configDesc.RootNode); -- 根据值的名称去计算一个值的类型,并以这个类型返回这个值 local function getValue(name , value) if value == nil then return nil end local convertFunc = configDesc.Mapping[name]; if convertFunc then return convertFunc(value); else return value; end end local keyName = configDesc.KeyName; local secondKeyName = configDesc.SecondKeyName; --local thirdKeyName = configDesc.ThirdKeyName; -- 读取所有配置 for i , v in ipairs(root) do local valueTable = {} if keyName ~= nil then local key = getValue(keyName , v[keyName]); if key == nil then error("找不到第一键值:" .. keyName); end if secondKeyName then local secondKey = getValue(secondKeyName , v[secondKeyName]); if secondKey == nil then error("找不到第二键值:" .. secondKeyName); end local secondTable = result[key] if secondTable == nil then secondTable = {} result[key] = secondTable; local defaultKey = configDesc.DefaultSecondKey local function defaultData(self , k) if k == "get" then return function(self , secondKey) return rawget(self , secondKey); end else if defaultKey then print("找不到配置[" .. configDesc.XMLFile .. "]键为[" .. tostring(key) .. "][" .. tostring(secondKey) .. "]的值,将使用默认键[" .. tostring(key) .. "][" .. tostring(defaultKey) .. "]代替") return self[defaultKey] else return nil; end end end -- 默认值用metatable来返回 setmetatable(secondTable , {__index=defaultData}) end --[[-- 第三个关键字 if thirdKeyName then local thirdKey = getValue(thirdKeyName , v[thirdKeyName]); if thirdKey == nil then error("找不到第三键值:" .. thirdKeyName); end local thirdTable = result[key][secondKey] if thirdTable == nil then thirdTable = {} result[key][secondKey] = thirdTable; local defaultKey = configDesc.DefaultThirdKey local function defaultData(self , k) if k == "get" then return function(self , thirdKey) return rawget(self , thirdKey); end else print("找不到配置[" .. configDesc.XMLFile .. "]键为[" .. tostring(key) .. "][" .. tostring(secondKey) .. "][" .. tostring(k) .. "]的值,将使用默认键[" .. tostring(key) .. "][" .. tostring(secondKey) .. "][" .. tostring(defaultKey) .. "]代替") return self[defaultKey] end end -- 默认值用metatable来返回 setmetatable(thirdTable , {__index=defaultData}) end thirdTable[thirdKey] = valueTable; else--]] secondTable[secondKey] = valueTable; --end else result[key] = valueTable; end else table.insert(result , valueTable); end for ii , vv in pairs(v) do if type(ii) == "string" then valueTable[ii] = getValue(ii , vv); end end -- 生成语言字段 if languageRoot then local row = languageRoot[i]; if row then for ii , vv in pairs(row) do if type(ii) == "string" then valueTable[ii] = getValue(ii , vv); end end end end end local defaultKey = configDesc.DefaultKey local secondDefaultKey = configDesc.DefaultSecondKey if defaultKey ~= nil then -- 检查默认值 if result[defaultKey] == nil then error("找不到配置[" .. configDesc.XMLFile .. "]的默认值[" .. defaultKey .. "]") return; end if secondKeyName then local secondTable = result[defaultKey]; local secondKey = getValue(secondKeyName , secondTable[secondDefaultKey][secondKeyName]); -- 检查默认值 if result[defaultKey][secondKey] == nil then error("找不到配置[" .. configDesc.XMLFile .. "]的第二默认值[" .. defaultKey .. "][" .. secondKey .."]") return; end end --[[if thirdKeyName and secondKeyName then local thirdTable = result[defaultKey][secondDefaultKey]; local thirdKey = getValue(thirdKeyName , thirdTable[configDesc.DefaultThirdKey][thirdKeyName]); -- 检查默认值 if result[defaultKey][secondKey][thirdKey] == nil then error("找不到配置[" .. configDesc.XMLFile .. "]的第三默认值[" .. defaultKey .. "][" .. secondKey .."][" .. thirdKey .."]") return; end end--]] local function defaultData(self , key) if XMLConfig[key] then return XMLConfig[key] else print("找不到配置[" .. configDesc.XMLFile .. "]键为[" .. tostring(key) .. "]的值,将使用默认键[" .. tostring(defaultKey) .. "]代替") return self[defaultKey] end end -- 默认值用metatable来返回 setmetatable(result , {__index=defaultData}) else local function defaultData(self , key) if XMLConfig[key] then return XMLConfig[key] end end -- 默认值用metatable来返回 setmetatable(result , {__index=defaultData}) end if configDesc.postload then table.insert(g_allXMLPostload , configDesc.postload) end --print("读取XML配置成功:" .. configDesc.XMLFile); return result end -- 从XML节点载入一个Node,并返回此Node local function createFromXmlNode(cls , xmlNode) local instance = cls:createNode() if app.editor then if instance.loadDefaults then instance:loadDefaults() end end instance:loadFromXmlNode(xmlNode); return instance; end -- 从XML节点实例载入一颗Node树,并返回根Node function createNodeFromXmlNode(xmlNode) local cls = cc[xmlNode.ClassName]; if not cls then error("找不到类型:" .. xmlNode.ClassName); end local node = createFromXmlNode(cls , xmlNode); local children = xmlNode.Children; if children then for i , v in ipairs(children) do node:addChild(createNodeFromXmlNode(v)); end end return node; end -- 从XML文件载入一颗Node树,并返回根Node function createNodeFromFile(nodeFile) TimeSpan:enterSpan("createNodeFromFile" .. nodeFile); --TimeSpan:enterSpan("table.loadFile"); local xmlFile = table.loadFile(nodeFile); --TimeSpan:leaveSpan(); --TimeSpan:enterSpan("createNodeFromXmlNode"); local node = createNodeFromXmlNode(xmlFile); --TimeSpan:leaveSpan(); TimeSpan:leaveSpan(); return node; end -- 从XML文件载入一颗Node树,并返回根Node function createNodeFromXmlString(xmlString) local data = table.loadString(xmlString); local node = createNodeFromXmlNode(data); return node; end --g_xmlFileCache = {}; -- 载入XML文件,并缓存XML数据 function loadXmlFileWithCache(effectFile) --local xmlData = g_xmlFileCache[effectFile] --if xmlData then -- return xmlData; --end xmlData = xml.load(effectFile); --g_xmlFileCache[effectFile] = xmlData; return xmlData; end