using UnityEngine; using System.Collections; using System.Collections.Generic; using System; using XLua; [LuaCallCSharp] public class TableManagerLua { private static TableManagerLua _Instance; public static TableManagerLua Instance { get { if (_Instance == null) _Instance = new TableManagerLua(); return _Instance; } } private LuaTable scriptEnv; public void LoadTableLua() { scriptEnv = LuaMain.luaEnv.NewTable(); LuaTable meta = LuaMain.luaEnv.NewTable(); meta.Set("__index", LuaMain.luaEnv.Global); scriptEnv.SetMetaTable(meta); meta.Dispose(); LuaMain.luaEnv.DoString("require 'LuaTableManager'", "LuaTableManager", scriptEnv); Action initTables = scriptEnv.Get("InitTables"); if (initTables != null) { initTables(); } //GetTableList("Tables/ItemGetPath.txt"); } public List> GetTableList(string tablePath) { List> tableSplitList = new List>(); string m_Key = ""; string[] m_Value = null; string[] list = tablePath.Split('.'); string relTablePath = list[0].Substring(7); LuaTable tableEnv = LuaMain.luaEnv.NewTable(); Dictionary tableTypes = LuaMain.luaEnv.Global.Get>(relTablePath + "Type"); GuiTextDebug.debug("scriptEnv.Set:" + relTablePath); TextAsset testAsset = LoadAssetBundle.Instance.LoadTableAsset(relTablePath); if (testAsset == null) return tableSplitList; string[] alldataRow; alldataRow = testAsset.text.Split('\n'); //skip fort three int skip = 0; string[] typeList = null; foreach (string line in alldataRow) { int nKey = -1; if (skip == 1) { string sztemp = line; if (sztemp.Length >= 1) { if (sztemp[sztemp.Length - 1] == '\r') { sztemp = sztemp.TrimEnd('\r'); } } typeList = line.Split('\t'); m_Value = new string[typeList.Length]; ++skip; continue; } if (++skip < 4) continue; if (String.IsNullOrEmpty(line)) continue; if (line[0] == '#') continue; string szlinetemp = line; if (szlinetemp.Length >= 1) { if (szlinetemp[szlinetemp.Length - 1] == '\r') { szlinetemp = szlinetemp.TrimEnd('\r'); } } string[] strCol = MySplit(szlinetemp, typeList, "\t"); if (strCol.Length == 0) continue; string skey = strCol[0]; string[] valuesList = new string[strCol.Length]; if (string.IsNullOrEmpty(skey) || skey.Equals("--")) { skey = m_Key; nKey = Int32.Parse(skey); valuesList[0] = skey; for (int i = 1; i < strCol.Length; ++i) { if (String.IsNullOrEmpty(strCol[i]) || strCol[i] == "--") { valuesList[i] = m_Value[i]; } else { valuesList[i] = strCol[i]; m_Value[i] = strCol[i]; } } } else { m_Key = skey; nKey = Int32.Parse(skey); for (int i = 0; i < strCol.Length; ++i) { if (strCol[i] == "--") { valuesList[i] = "0"; m_Value[i] = "0"; } else { valuesList[i] = strCol[i]; m_Value[i] = strCol[i]; } } } LuaTable scriptEnv = SetTableLine(valuesList, tableTypes); tableEnv.Set(m_Key, scriptEnv); tableSplitList.Add(new List(valuesList)); } LuaMain.luaEnv.Global.Set(relTablePath, tableEnv); GuiTextDebug.debug("tableSplitList count:" + tableSplitList.Count); return tableSplitList; } private string[] MySplit(string str, string[] nTypeList, string regix) { if (string.IsNullOrEmpty(str)) { return null; } String[] content = new String[nTypeList.Length]; int nIndex = 0; int nstartPos = 0; while (nstartPos <= str.Length) { int nsPos = str.IndexOf(regix, nstartPos); if (nsPos < 0) { String lastdataString = str.Substring(nstartPos); if (string.IsNullOrEmpty(lastdataString) && nTypeList[nIndex].ToLower() != "string") { content[nIndex++] = "--"; } else { content[nIndex++] = lastdataString; } break; } else { if (nstartPos == nsPos) { if (nTypeList[nIndex].ToLower() != "string") { content[nIndex++] = "--"; } else { content[nIndex++] = ""; } } else { content[nIndex++] = str.Substring(nstartPos, nsPos - nstartPos); } nstartPos = nsPos + 1; } } return content; } private LuaTable SetTableLine(string[] values, Dictionary typeList) { LuaTable scriptEnv = LuaMain.luaEnv.NewTable(); for (int i = 0; i < values.Length; ++i) { string idStr = "id" + i.ToString(); if (!typeList.ContainsKey(idStr)) continue; string typeStr = typeList[idStr]; string[] typeSplit = typeStr.Split(','); if (typeSplit.Length != 2) continue; switch (typeSplit[1]) { case "STRING": scriptEnv.Set(typeSplit[0], values[i]); break; case "INT": int intValue = 0; int.TryParse(values[i], out intValue); scriptEnv.Set(typeSplit[0], intValue); break; case "FLOAT": float floatValue = 0; float.TryParse(values[i], out floatValue); scriptEnv.Set(typeSplit[0], floatValue); break; } } return scriptEnv; } }