147 lines
4.5 KiB
Lua
147 lines
4.5 KiB
Lua
local DataMgr = setmetatable({},{
|
||
__index = function(t, key)
|
||
return require("Datas."..key)
|
||
end})
|
||
|
||
|
||
function DataMgr.Print_t ( t )
|
||
-- print table
|
||
local print_r_cache={}
|
||
local function sub_print_r(t,indent)
|
||
if (print_r_cache[tostring(t)]) then
|
||
print(indent.."*"..tostring(t))
|
||
else
|
||
print_r_cache[tostring(t)]=true
|
||
if (type(t)=="table") then
|
||
for pos,val in pairs(t) do
|
||
if (type(val)=="table") then
|
||
print(indent.."["..pos.."] => "..tostring(t).." {")
|
||
sub_print_r(val,indent..string.rep(" ",string.len(pos)+8))
|
||
print(indent..string.rep(" ",string.len(pos)+6).."}")
|
||
elseif (type(val)=="string") then
|
||
print(indent.."["..pos..'] => "'..val..'"')
|
||
else
|
||
print(indent.."["..pos.."] => "..tostring(val))
|
||
end
|
||
end
|
||
else
|
||
print(indent..tostring(t))
|
||
end
|
||
end
|
||
end
|
||
if (type(t)=="table") then
|
||
print(tostring(t).." {")
|
||
sub_print_r(t," ")
|
||
print("}")
|
||
else
|
||
sub_print_r(t," ")
|
||
end
|
||
print()
|
||
end
|
||
|
||
function DataMgr.GetData(filename)
|
||
local pro_path = UE4.UKismetSystemLibrary.GetProjectContentDirectory()
|
||
local file = io.open(pro_path..'../Datas/'..filename .. '.json', "r")
|
||
local info = file:read("*a")
|
||
file:close()
|
||
local json= require("rapidjson")
|
||
local res = json.decode(info)
|
||
return res
|
||
end
|
||
|
||
function DataMgr.GetLevelLoaderJsonData(shortname)
|
||
local pro_path = UE4.UKismetSystemLibrary.GetProjectContentDirectory()
|
||
local path=pro_path..'Script/Datas/Houdini_data/'..shortname .. '.json'
|
||
local info = UE4.URuntimeCommonFunctionLibrary.LoadFile(path)
|
||
-- local file = io.open(path, "r")
|
||
-- local info = file:read("*a")
|
||
-- file:close()
|
||
local json= require("rapidjson")
|
||
local res = json.decode(info)
|
||
return res
|
||
end
|
||
|
||
-- 预加载数据表,将所有的数据表全部加载到内存中,减少之后读表index的时间消耗
|
||
function DataMgr.PreLoad()
|
||
local DataNames = require("Datas.DataNames")
|
||
for _, DataName in pairs(DataNames) do
|
||
require("Datas."..DataName)
|
||
end
|
||
end
|
||
|
||
local all_tables = {}
|
||
|
||
function DataMgr.CollectTable(Table)
|
||
for k, tbl in pairs(all_tables) do
|
||
if tbl == Table then
|
||
all_tables[k] = nil
|
||
for k, v in pairs(tbl) do
|
||
if type(v) == "table" then
|
||
DataMgr.CollectTable(v)
|
||
end
|
||
end
|
||
break
|
||
end
|
||
end
|
||
end
|
||
|
||
DataMgr.ReadOnly_NewIndex = function(t, k, v)
|
||
local rawset = rawset
|
||
local getmetatable = getmetatable
|
||
error("没法对导表数据【" .. tostring(t.__name) .."】中字段【" .. tostring(k) .. "】进行写操作")
|
||
end
|
||
|
||
function DataMgr.CleanAllTable()
|
||
all_tables = {}
|
||
local DataNames = require("Datas.DataNames")
|
||
local HotFix = require("HotFix")
|
||
local data_module_table = rawget(HotFix, "data_module_table")
|
||
for _, DataName in pairs(DataNames) do
|
||
if not (data_module_table and data_module_table[DataName]) then
|
||
require("UnLuaHotReload").RemoveLoadedModule("Datas."..DataName)
|
||
end
|
||
end
|
||
end
|
||
function read_only(name, tbl)
|
||
if not all_tables[tbl] then
|
||
local tbl_mt = getmetatable(tbl)
|
||
if not tbl_mt then
|
||
tbl_mt = {}
|
||
setmetatable(tbl, tbl_mt)
|
||
end
|
||
|
||
local proxy = tbl_mt.__read_only_proxy
|
||
if not proxy then
|
||
proxy = {__name = name}
|
||
tbl_mt.__read_only_proxy = proxy
|
||
local proxy_mt = {
|
||
__index = tbl,
|
||
__newindex = DataMgr.ReadOnly_NewIndex,
|
||
__pairs = function (t) return pairs(tbl) end,
|
||
-- __ipairs = function (t) return ipairs(tbl) end, 5.3版本不需要此方法
|
||
__len = function (t) return #tbl end,
|
||
__read_only_proxy = proxy
|
||
}
|
||
setmetatable(proxy, proxy_mt)
|
||
end
|
||
all_tables[tbl] = proxy
|
||
for k, v in pairs(tbl) do
|
||
if type(v) == "table" then
|
||
tbl[k] = read_only(name, v)
|
||
end
|
||
end
|
||
end
|
||
return all_tables[tbl]
|
||
end
|
||
|
||
--if URuntimeCommonFunctionLibrary.IsDebugDataTable() then
|
||
if true then
|
||
DataMgr.ReadOnly = read_only
|
||
else
|
||
DataMgr.ReadOnly = function (name, tbl)
|
||
return tbl
|
||
end
|
||
end
|
||
_G.DataMgr = DataMgr
|
||
return DataMgr
|