注意事项:
1.格式化打印输出Lua配置表
调用方法为: XML_TO_LUA
2.格式化Lua表写入到文件中
调用方法为:XML_TO_LUA_2
请在下方的xml_table_list表中配置你要批量输出的Lua表的信息,
例如下方批量输出三个Lua表g_items、g_npcList、g_bossConfig
local OUTPUT_PATH_PREFIX = ""
local output_table_list = {
= {name = "items", data = { t = g_items, name = "g_items" }},
= {name = "npc", data = { t = g_npcList, name = "g_npcList" }},
= {name = "boss", data = { t = g_bossConfig, name = "g_bossConfig" }},
}
-------格式化打印---------
local function LUA_TABLE_PRINT( t, indent )
local pre = string.rep("\t", indent)
for k,v in pairs(t) do
if type(v) == "table" then
if type(k) == "number" then
print(pre .. "" .. k .. "]" .. " = {")
LUA_TABLE_PRINT(v, indent + 1)
print(pre .. "},")
elseif type(k) == "string" then
if tonumber(k) then
print(pre .. "\"" .. k .. "\"] = {")
elseif (tonumber(string.sub(k, 1, 1))) then
print(pre .. "\"" .. k .. "\"] = {")
else
print(pre .. k .. " = {")
end
LUA_TABLE_PRINT(v, indent + 1)
print(pre .. "},")
end
elseif type(v) == "number" then
if type(k) == "number" then
print(pre .. "" .. k .. "]" .. " = " .. v .. ",")
elseif type(k) == "string" then
if tonumber(k) then
print(pre .. "\"" .. k .. "\"] = " .. v .. ",")
elseif (tonumber(string.sub(k, 1, 1))) then
print(pre .. "\"" .. k .. "\"] = " .. v .. ",")
else
print(pre .. k .. " = " .. v .. ",")
end
end
elseif type(v) == "string" then
local text = string.gsub(v, "\n]", "")
text = string.gsub(text, "\"", "\\\"")
if type(k) == "number" then
print(pre .. "" .. k .. "]" .. " = \"" .. text .. "\",")
elseif type(k) == "string" then
if tonumber(k) then
print(pre .. "\"" .. k .. "\"] = \"" .. text .. "\",")
elseif (tonumber(string.sub(k, 1, 1))) then
print(pre .. "\"" .. k .. "\"] = \"" .. text .. "\",")
else
print(pre .. k .. " = \"" .. text .. "\",")
end
end
end
end
end
function PRINT_LUA( )
local indent = 1
local start_index = 1
local end_index = #output_table_list
-- start_index = 73
-- end_index = 0
for i=start_index,end_index do
local v = output_table_list*
print(v.data.name .. " = {")
LUA_TABLE_PRINT(_G, indent)
print("}")
end
end
-------格式化写入文件------
--初始化输出文件存放的文件夹
local function INIT_OUTPUT_DIR( )
print("---INIT_OUTPUT_DIR---")
---此处路劲为Ios模拟器上的Library下的Caches下面的XML_TO_LUA_DIR文件夹
---输出文件夹可自行设定
local path = string.format("%s/Caches/XML_TO_LUA_DIR", kt_library_path())
local path1 = "\"" .. path .. "\""
kt_remove_dir(path1)
kt_create_dir(path)
OUTPUT_PATH_PREFIX = path
end
local function write_to_file( msg, file_handle )
msg = msg .. "\n"
file_handle:write(msg)
end
local function LUA_TABLE_WRITE_TO_FILE( t, indent, file_handle )
local pre = string.rep("\t", indent)
for k,v in pairs(t) do
if type(v) == "table" then
if type(k) == "number" then
write_to_file(pre .. "" .. k .. "]" .. " = {", file_handle)
LUA_TABLE_WRITE_TO_FILE(v, indent + 1, file_handle)
write_to_file(pre .. "},", file_handle)
elseif type(k) == "string" then
if tonumber(k) then
write_to_file(pre .. "\"" .. k .. "\"] = {", file_handle)
elseif (tonumber(string.sub(k, 1, 1))) then
write_to_file(pre .. "\"" .. k .. "\"] = {", file_handle)
else
write_to_file(pre .. k .. " = {", file_handle)
end
LUA_TABLE_WRITE_TO_FILE(v, indent + 1, file_handle)
write_to_file(pre .. "},", file_handle)
end
elseif type(v) == "number" then
if type(k) == "number" then
write_to_file(pre .. "" .. k .. "]" .. " = " .. v .. ",", file_handle)
elseif type(k) == "string" then
if tonumber(k) then
write_to_file(pre .. "\"" .. k .. "\"] = " .. v .. ",", file_handle)
elseif (tonumber(string.sub(k, 1, 1))) then
write_to_file(pre .. "\"" .. k .. "\"] = " .. v .. ",", file_handle)
else
write_to_file(pre .. k .. " = " .. v .. ",", file_handle)
end
end
elseif type(v) == "string" then
local text = string.gsub(v, "\n]", "")
text = string.gsub(text, "\"", "\\\"")
if type(k) == "number" then
write_to_file(pre .. "" .. k .. "]" .. " = \"" .. text .. "\",", file_handle)
elseif type(k) == "string" then
if tonumber(k) then
write_to_file(pre .. "\"" .. k .. "\"] = \"" .. text .. "\",", file_handle)
elseif (tonumber(string.sub(k, 1, 1))) then
write_to_file(pre .. "\"" .. k .. "\"] = \"" .. text .. "\",", file_handle)
else
write_to_file(pre .. k .. " = \"" .. text .. "\",", file_handle)
end
end
end
end
end
function WRITE_LUA( )
INIT_OUTPUT_DIR()
local indent = 1
local start_index = 1
local end_index = #output_table_list
-- start_index = 73
-- end_index = 0
for i=start_index,end_index do
local v = output_table_list*
local name = string.format("xml_%s.lua", v.name)
local path = string.format("%s/%s", OUTPUT_PATH_PREFIX, name)
--创建文件:此为C++方法导出的接口
kt_create_file(path)
local file_handle = assert(io.open(path, 'w'))
write_to_file(v.data.name .. " = {", file_handle)
LUA_TABLE_WRITE_TO_FILE(_G, indent, file_handle)
write_to_file("}", file_handle)
file_handle:close()
end
end
```
bool kt_create_file(const char *filePath) {
int ret = creat(filePath, S_IRWXU | S_IRWXG | S_IRWXO);
if (ret != 0 && (errno != EEXIST) )
return false;
return true;
}
```
INIT_XML_OUTPUT_DIR方法生成的目录:
**
