void saveLogic() { if (!string.IsNullOrEmpty(scriptLogic)) { string path = LuaEnv.GetFilePath(logicFile); File.WriteAllText(path, scriptLogic); ShowMessage(string.Format("写入{0}成功!", path)); string prefabPath = string.Format("Assets/{0}/UI/{1}.prefab", ZFrame.Asset.AssetBundleLoader.DIR_ASSETS, selected.name); var prefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject)); if (!prefab) { PrefabUtility.CreatePrefab(prefabPath, selected.gameObject, ReplacePrefabOptions.ConnectToPrefab); AssetDatabase.Refresh(); var ai = AssetImporter.GetAtPath(prefabPath); ai.assetBundleName = "ui.unity3d"; } else { //PrefabUtility.ReplacePrefab(selected.gameObject, prefab, ReplacePrefabOptions.ReplaceNameBased); } } else { ShowMessage(logicFile + "脚本为空!"); } }
// 为选定的预设生成Lua脚本 private void generateWithSelected() { GameObject[] goes = Selection.gameObjects; if (goes != null && goes.Length == 1) { selected = goes[0].GetComponent <LuaComponent>(); flagGenCode = true; if (selected != null) { // 清空结构 codeDefined = null; codeInited = null; dictRef.Clear(); dictEntRef.Clear(); dictFunc.Clear(); // 生成文件名 if (string.IsNullOrEmpty(selected.luaScript)) { ShowMessage("脚本名称为空!"); return; } string fileExt = Path.GetExtension(selected.luaScript); string fileName = Path.GetFileNameWithoutExtension(selected.luaScript); if (string.IsNullOrEmpty(fileExt)) { if (Directory.Exists(LuaEnv.GetFilePath("ui/" + fileName))) { // 扩展名为空,名称是模块名 logicFile = "ui/" + fileName + "/lc_" + selected.name.ToLower() + ".lua"; selected.luaScript = logicFile; } else { ShowMessage("不存在的UI模块: " + fileName); return; } } else { // 名称已存在 logicFile = selected.luaScript; } parseLogic(logicFile); GenerateLogic(); } else { ShowMessage("预设体上需要挂载<LuaComponent>组件"); } } else { ShowMessage("只能选择一个预设体来生成脚本"); } }
/// <summary> /// 压缩和打包Lua脚本/配置 /// </summary> public static void EncryptLua() { CLZF2.Decrypt(null, 260769); CLZF2.Decrypt(new byte[1], 3); string CodeRoot = Path.Combine(Application.dataPath, "LuaCodes"); string scriptDir = Path.Combine(CodeRoot, "Script"); string configDir = Path.Combine(CodeRoot, "Config"); if (!Directory.Exists(scriptDir)) { SystemTools.NeedDirectory(scriptDir); AssetDatabase.Refresh(); var ai = AssetImporter.GetAtPath("Assets/LuaCodes/Script"); ai.assetBundleName = AssetsMgr.LUA_SCRIPT; } if (!Directory.Exists(configDir)) { SystemTools.NeedDirectory(configDir); AssetDatabase.Refresh(); var ai = AssetImporter.GetAtPath("Assets/LuaCodes/Config"); ai.assetBundleName = AssetsMgr.LUA_CONFIG; } var scripts = new DirectoryInfo(scriptDir).GetFiles("*.bytes"); var configs = new DirectoryInfo(configDir).GetFiles("*.bytes"); var listExists = new List <string>(); foreach (var f in scripts) { listExists.Add("Script/" + f.Name); } foreach (var f in configs) { listExists.Add("Config/" + f.Name); } DirectoryInfo dirLua = new DirectoryInfo(LuaEnv.GetFilePath("")); FileInfo[] files = dirLua.GetFiles("*.lua", SearchOption.AllDirectories); int startIndex = dirLua.FullName.Length + 1; foreach (FileInfo f in files) { string fullName = f.FullName.Substring(startIndex).Replace('/', '%').Replace('\\', '%'); string fileName = fullName.Remove(fullName.Length - 4) + ".bytes"; string[] lines = File.ReadAllLines(f.FullName); // 以"--"开头的注释以换行符代替 List <string> liLine = new List <string>(); foreach (var l in lines) { string ltim = l.Trim(); if (ltim.StartsWith("--") && !ltim.StartsWith("--[[") && !ltim.StartsWith("--]]")) { liLine.Add("\n"); } else { liLine.Add(l + "\n"); } } string codes = string.Concat(liLine.ToArray()); byte[] nbytes = System.Text.Encoding.UTF8.GetBytes(codes); if (nbytes.Length > 0) { nbytes = CLZF2.DllCompress(nbytes); CLZF2.Encrypt(nbytes, nbytes.Length); } else { Debug.LogWarning("Compress Lua: " + fileName + " is empty!"); } string path; if (fileName.StartsWith("config")) { listExists.Remove("Config/" + fileName); path = Path.Combine(configDir, fileName); } else { listExists.Remove("Script/" + fileName); path = Path.Combine(scriptDir, fileName); } if (File.Exists(path)) { using (var file = File.OpenWrite(path)) { file.Seek(0, SeekOrigin.Begin); file.Write(nbytes, 0, nbytes.Length); file.SetLength(nbytes.Length); } } else { File.WriteAllBytes(path, nbytes); } } foreach (var n in listExists) { var path = Path.Combine(CodeRoot, n); File.Delete(path); Log("Delete: {0}", n); } Log("Compress {0} files success.\n => {1}", files.Length, CodeRoot); }
// 解析已有的UI Logic脚本 void parseLogic(string path) { var filePath = LuaEnv.GetFilePath(path); if (!File.Exists(filePath)) { codeDefined = null; codeInited = null; dictFunc.Clear(); return; } // 解析 string text = File.ReadAllText(filePath); // 注释:文件名 text = text.Substring(text.IndexOf(".lua") + 5); int codeStart = text.IndexOf(AUTO_DEFINE) - 1; if (codeStart > 0) { codeDefined = text.Substring(0, text.IndexOf(AUTO_DEFINE) - 1); } else { codeDefined = null; } codeStart = text.IndexOf(AUTO_DEFINE); if (codeStart >= 0) { text = text.Substring(codeStart); } else { return; } string funcName = null; string funcDefine = null; using (var reader = new StringReader(text)) { for (;;) { var line = reader.ReadLine(); if (line == null) { break; } if (line.Contains(AUTO_REGIST)) { if (funcName == "init_view") { // 开始记录codeInited codeInited = ""; } continue; } else if (line.Contains("local P = {")) { if (funcName != null) { dictFunc.Add(funcName, funcDefine.Substring(0, funcDefine.Length - 1)); } break; } // function inside function will be ignore... string[] segs = line.Split(new char[] { ' ', '\t', '\n' }, System.StringSplitOptions.None); if (segs != null && segs.Length > 2 && segs[0] == "local" && segs[1] == "function") { if (funcName != null) { if (funcName != "init_view") { try { dictFunc.Add(funcName, funcDefine.Substring(0, funcDefine.Length - 1)); } catch (System.Exception e) { Debug.LogError(e.Message + ":" + funcName); } } else { int endIndex = codeInited.LastIndexOf("end"); codeInited = codeInited.Substring(0, endIndex); } } // 取函数名, 记录函数 funcName = segs[2].Substring(0, segs[2].IndexOf('(')).Trim(); funcDefine = '\n' + line + '\n'; } else { if (funcName != null) { if (funcName == "init_view" && codeInited != null) { codeInited += line + '\n'; } funcDefine += line + '\n'; } } } } }