LuaMgr.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. using LuaInterface;
  2. using System;
  3. using System.IO;
  4. using System.Text;
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. using System.Collections;
  8. public class LuaMgr : SingletonMono<LuaMgr>
  9. {
  10. private Dictionary<string, byte[]> LuaDic = new Dictionary<string, byte[]>();
  11. private Dictionary<string, byte[]> luaPbDic = new Dictionary<string, byte[]>();
  12. private int LoadCount = 0;
  13. private int LuaDirCount = 3;
  14. public LuaState luaState = null;
  15. protected LuaLooper loop = null;
  16. protected LuaTable luaMainTable = null;
  17. LuaLauncher luaLauncher = null;
  18. string LuaMainPath = "Lua/Core";
  19. bool bDisposed = false;
  20. string luaRootPath = string.Empty;
  21. public override void InitMgr()
  22. {
  23. base.InitMgr();
  24. LoadLuaFiles();
  25. }
  26. public void Clear()
  27. {
  28. Dispose();
  29. }
  30. protected override void Dispose()
  31. {
  32. if (bDisposed) return;
  33. if (luaLauncher)
  34. {
  35. luaLauncher.StopAsync();
  36. }
  37. Destroy();
  38. LoadCount = 0;
  39. LuaDic.Clear();
  40. luaPbDic.Clear();
  41. bDisposed = true;
  42. base.Dispose();
  43. }
  44. void LoadLuaFiles()
  45. {
  46. #if UNITY_EDITOR
  47. if (!Constants.AssetbundleMode)
  48. {
  49. LuaDirCount = 1;
  50. ResourceMgr.Instance.LoadLuaAsset(OnLoadPbCallback, Constants.LuaPbDir);
  51. }
  52. else if (Constants.AssetbundleMode)
  53. #endif
  54. {
  55. ResourceMgr.Instance.LoadLuaAsset(OnCallBack, Constants.ABLuaDir);
  56. ResourceMgr.Instance.LoadLuaAsset(OnCallBack, Constants.ABLuaLogicDir);
  57. ResourceMgr.Instance.LoadLuaAsset(OnLoadPbCallback, Constants.ABLuaPbDir);
  58. }
  59. }
  60. private void OnCallBack(List<TextAsset> objs, string dir, string[] assetNames)
  61. {
  62. if (objs == null || objs.Count <= 0)
  63. {
  64. DebugHelper.Log("No LuaMgr AssetBundle");
  65. return;
  66. }
  67. for (int i = 0; i < objs.Count; ++i)
  68. {
  69. TextAsset tx = objs[i];
  70. if (tx != null && LuaDic.ContainsKey(tx.name))
  71. {
  72. //DebugHelper.LogError(string.Format("LuaDic.ContainsKey {0}", tx.name));
  73. LuaDic.Remove(tx.name);
  74. }
  75. LuaDic.Add(tx.name, tx.bytes);
  76. }
  77. LoadCount++;
  78. if (LoadCount == LuaDirCount)
  79. {
  80. OnLoadFinished();
  81. }
  82. }
  83. private void OnLoadPbCallback(List<TextAsset> objs, string dir, string[] assetNames)
  84. {
  85. if (objs == null || objs.Count <= 0)
  86. {
  87. DebugHelper.Log("No LuaMgr AssetBundle");
  88. return;
  89. }
  90. for (int i = 0; i < objs.Count; ++i)
  91. {
  92. TextAsset tx = objs[i];
  93. if (tx != null && tx.name.Contains(".lua"))
  94. {
  95. continue;
  96. }
  97. if (tx != null && luaPbDic.ContainsKey(tx.name))
  98. {
  99. //DebugHelper.LogError(string.Format("LuaDic.ContainsKey {0}", tx.name));
  100. luaPbDic.Remove(tx.name);
  101. }
  102. if (tx != null)
  103. luaPbDic.Add(tx.name, tx.bytes);
  104. }
  105. LoadCount++;
  106. if (LoadCount == LuaDirCount)
  107. {
  108. OnLoadFinished();
  109. }
  110. }
  111. void OnLoadFinished()
  112. {
  113. if (Application.isEditor)
  114. {
  115. luaRootPath = Application.dataPath;
  116. }
  117. else if (Application.platform == RuntimePlatform.WindowsPlayer ||
  118. Application.platform == RuntimePlatform.OSXPlayer)
  119. {
  120. luaRootPath = Application.streamingAssetsPath;
  121. }
  122. luaLauncher = this.GetOrAddComponent<LuaLauncher>();
  123. }
  124. public void CallMain()
  125. {
  126. DebugHelper.LogWarning("==============LuaMgr CallMain===================");
  127. CallLuaFunc("Start");
  128. }
  129. public void StartMain()
  130. {
  131. luaState = LuaClient.Instance.luaState;
  132. AddLuaSearchPath(LuaMainPath);
  133. DebugHelper.LogWarning("==============LuaMgr StartMain===================");
  134. luaMainTable = luaState.DoFile<LuaTable>("LuaMain.lua");
  135. CallLuaFunc("Init");
  136. }
  137. public void StartMainPrecent(float precent)
  138. {
  139. luaLauncher.StartMainPrecent(precent);
  140. //EventMgr.DispatchEvent<bool, float>(new CoreEvent<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, false, precent));
  141. }
  142. public void StartMainComplete()
  143. {
  144. luaLauncher.StartMainComplete();
  145. //EventMgr.DispatchEvent<bool, float>(new CoreEvent<bool, float>(ECoreEventType.EID_LOAD_LUA_OK, true, 1));
  146. }
  147. public void EnterLogin(bool relogin)
  148. {
  149. if (luaMainTable != null)
  150. {
  151. LuaFunction tableFunc = luaMainTable.GetLuaFunction("EnterLogin");
  152. tableFunc.Call(luaMainTable, relogin);
  153. tableFunc.Dispose();
  154. tableFunc = null;
  155. }
  156. }
  157. public void LuaGC()
  158. {
  159. DebugHelper.Log("LuaGC");
  160. luaState.LuaGC(LuaGCOptions.LUA_GCCOLLECT);
  161. }
  162. void CallLuaFunc(string funcName)
  163. {
  164. if (luaMainTable != null)
  165. {
  166. //DebugHelper.LogError("funcName:" + funcName);
  167. //luaMainTable.Call(funcName);
  168. LuaFunction tableFunc = luaMainTable.GetLuaFunction(funcName);
  169. tableFunc.Call(luaMainTable);
  170. tableFunc.Dispose();
  171. tableFunc = null;
  172. }
  173. }
  174. public void Destroy()
  175. {
  176. if (luaMainTable != null)
  177. {
  178. CallLuaFunc("Destroy");
  179. luaMainTable = null;
  180. }
  181. }
  182. public static LuaState GetMainState()
  183. {
  184. return Instance.luaState;
  185. }
  186. public LuaLooper GetLooper()
  187. {
  188. return loop;
  189. }
  190. public List<LuaByteBuffer> GetPbFiles()
  191. {
  192. if (luaPbDic == null) return null;
  193. List<LuaByteBuffer> pbFiles = new List<LuaByteBuffer>();
  194. foreach (var p in luaPbDic)
  195. {
  196. //string content = p.Value;
  197. //byte[] data = Encoding.Default.GetBytes(content);
  198. byte[] data = p.Value;
  199. LuaByteBuffer luaByte = new LuaByteBuffer(data);
  200. pbFiles.Add(luaByte);
  201. }
  202. return pbFiles;
  203. }
  204. public void AddLuaSearchPath(string path)
  205. {
  206. DebugHelper.LogWarning($"==============LuaMgr AddLuaSearchPath===================[{path}]");
  207. if (!Path.IsPathRooted(path))
  208. {
  209. if (!string.IsNullOrEmpty(luaRootPath))
  210. {
  211. luaState.AddSearchPath(luaRootPath + "/" + path);
  212. }
  213. }
  214. else
  215. {
  216. Debug.LogError("2==== " + path);
  217. luaState.AddSearchPath(path);
  218. }
  219. }
  220. public byte[] GetLuaTextAsset(string name)
  221. {
  222. if (LuaDic.ContainsKey(name))
  223. {
  224. return LuaDic[name];
  225. }
  226. else
  227. {
  228. DebugHelper.LogError("Not Found LuaTextAssetName: {0}", name);
  229. return null;
  230. }
  231. }
  232. }