using System; using System.Collections.Generic; using System.Runtime.InteropServices; using Thousandto.Code.Center; using UnityEngine; using XLua; public static class CfgBinaryData { private static int _xluaPtrSize = -1; private static int _xluaTvalueSize = -1; private static Dictionary _compressTables = new Dictionary(400); private static Dictionary _columnTables = new Dictionary(400); private static Dictionary _keyCounts = new Dictionary(400); private static Dictionary _compressCols = new Dictionary(400); private static Dictionary> _nameIndexs = new Dictionary>(400); public static bool GetBaseDataValues(string name, out int keyCount, out int indexFactor, out IntPtr compressData, out IntPtr columnShiftAndList, out int compressMaxColumn, out Dictionary nameIndexs) { compressData = IntPtr.Zero; columnShiftAndList = IntPtr.Zero; keyCount = 0; compressMaxColumn = 0; nameIndexs = null; if (_xluaPtrSize < 0) { _xluaPtrSize = XLua.LuaDLL.Lua.xlua_getptrsize(IntPtr.Zero); } if (_xluaTvalueSize < 0) { _xluaTvalueSize = XLua.LuaDLL.Lua.xlua_gettvaluesize(IntPtr.Zero); } indexFactor = _xluaTvalueSize / sizeof(int); try { if (!_keyCounts.TryGetValue(name, out keyCount)) { var tableName = string.Format("DataConfig.Data{0}.Count", name); keyCount = GameCenter.LuaSystem.GetGlobalInPath(tableName); _keyCounts.Add(name, keyCount); } if (!_compressCols.TryGetValue(name, out compressMaxColumn)) { var tableName = string.Format("DataConfig.Data{0}._CompressMaxColumn_", name); compressMaxColumn = GameCenter.LuaSystem.GetGlobalInPath(tableName); _compressCols.Add(name, compressMaxColumn); } if(!_nameIndexs.TryGetValue(name, out nameIndexs)) { var tableName = string.Format("DataConfig.Data{0}._ColumnNameIndexs_", name); nameIndexs = GameCenter.LuaSystem.GetGlobalInPath>(tableName); _nameIndexs.Add(name, nameIndexs); } if ((_xluaPtrSize == 4 || _xluaPtrSize == 8) && _xluaTvalueSize > 0 && _xluaTvalueSize % sizeof(int) == 0) { //获取配置table LuaTable table = null; if (!_compressTables.TryGetValue(name, out table)) { var tableName = string.Format("DataConfig.Data{0}._CompressData_", name); table = GameCenter.LuaSystem.GetGlobalInPath(tableName); _compressTables.Add(name, table); } LuaTable table2 = null; if (!_columnTables.TryGetValue(name, out table2)) { var tableName = string.Format("DataConfig.Data{0}._ColumnShiftAndList_", name); table2 = GameCenter.LuaSystem.GetGlobalInPath(tableName); _columnTables.Add(name, table2); } if (table != null && table2 != null) { unsafe { //读取数据指针 var ptr = (byte*)table.GetPtr().ToPointer(); //第16位为数据地址 var arrayPtr = *(long*)(ptr + _xluaPtrSize + 8); compressData = new IntPtr((void*)arrayPtr); //读取数据指针 ptr = (byte*)table2.GetPtr().ToPointer(); //第16位为数据地址 arrayPtr = *(long*)(ptr + _xluaPtrSize + 8); columnShiftAndList = new IntPtr((void*)arrayPtr); } } else { return false; } return true; } return true; } catch (Exception) { UnityEngine.Debug.LogErrorFormat("配置表 {0} 加载报错", name); return false; } } public static bool GetDataValues(string name, out IntPtr compressData) { compressData = IntPtr.Zero; if (_xluaPtrSize < 0) { _xluaPtrSize = XLua.LuaDLL.Lua.xlua_getptrsize(IntPtr.Zero); } if (_xluaTvalueSize < 0) { _xluaTvalueSize = XLua.LuaDLL.Lua.xlua_gettvaluesize(IntPtr.Zero); } try { if ((_xluaPtrSize == 4 || _xluaPtrSize == 8) && _xluaTvalueSize > 0 && _xluaTvalueSize % sizeof(int) == 0) { //获取配置table LuaTable table = null; if (!_compressTables.TryGetValue(name, out table)) { var tableName = string.Format("DataConfig.Data{0}._CompressData_", name); table = GameCenter.LuaSystem.GetGlobalInPath(tableName); _compressTables.Add(name, table); } if (table != null) { unsafe { //读取数据指针 var ptr = (byte*)table.GetPtr().ToPointer(); //第16位为数据地址 var arrayPtr = *(long*)(ptr + _xluaPtrSize + 8); compressData = new IntPtr((void*)arrayPtr); } } else { return false; } return true; } return true; } catch (Exception) { UnityEngine.Debug.LogErrorFormat("配置表 {0} 加载报错", name); return false; } } //清除单个缓存 public static void Remove(string name) { _compressTables.Remove(name); _columnTables.Remove(name); _keyCounts.Remove(name); _compressCols.Remove(name); _nameIndexs.Remove(name); } //清除所有缓存 public static void RemoveAll() { _compressTables.Clear(); _columnTables.Clear(); _keyCounts.Clear(); _compressCols.Clear(); _nameIndexs.Clear(); } }