LuaImporter.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #if UNITY_2018_1_OR_NEWER
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System.IO;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10. #if UNITY_2020_2_OR_NEWER
  11. using UnityEditor.AssetImporters;
  12. #else
  13. using UnityEditor.Experimental.AssetImporters;
  14. #endif
  15. [ScriptedImporter(2, new[] {"lua"})]
  16. public class LuaImporter : ScriptedImporter
  17. {
  18. const string Tag = "LuaImporter";
  19. public static bool compile = false; // compile to lua byte code
  20. public static bool strip = false; // strip lua debug info
  21. public static bool encode = true;
  22. public static void Compile(string exe, string prmt)
  23. {
  24. // disable by Jiang XuFeng on 20220325 for warning
  25. //bool finished = false;
  26. var process = new System.Diagnostics.Process();
  27. // disable by Jiang XuFeng on 20220325 for warning
  28. //var processing = 0f;
  29. try
  30. {
  31. var pi = new System.Diagnostics.ProcessStartInfo(exe, prmt);
  32. pi.WorkingDirectory = ".";
  33. pi.RedirectStandardInput = false;
  34. pi.RedirectStandardOutput = true;
  35. pi.RedirectStandardError = true;
  36. pi.UseShellExecute = false;
  37. pi.CreateNoWindow = true;
  38. process.OutputDataReceived += (sender, e) =>
  39. {
  40. if (string.IsNullOrEmpty(e.Data))
  41. return;
  42. UnityEngine.Debug.Log(e.Data);
  43. };
  44. process.ErrorDataReceived += (sender, e) =>
  45. {
  46. if (!string.IsNullOrEmpty(e.Data))
  47. UnityEngine.Debug.LogError(e.GetType() + ": " + e.Data);
  48. };
  49. process.Exited += (sender, e) =>
  50. {
  51. UnityEngine.Debug.Log($"{exe} {prmt} Exit");
  52. };
  53. process.StartInfo = pi;
  54. process.EnableRaisingEvents = true;
  55. process.Start();
  56. process.BeginOutputReadLine();
  57. process.BeginErrorReadLine();
  58. process.WaitForExit();
  59. }
  60. catch (System.Exception e)
  61. {
  62. UnityEngine.Debug.LogError("catch: " + e);
  63. }
  64. // UnityEngine.Debug.Log("finished: " + process.ExitCode);
  65. EditorUtility.ClearProgressBar();
  66. }
  67. public override void OnImportAsset(AssetImportContext ctx)
  68. {
  69. var prefax = Path.GetExtension(ctx.assetPath).Substring(1);
  70. var asset = LuaAsset.CreateInstance<LuaAsset>();
  71. byte[] data;
  72. if (compile)
  73. {
  74. // compile to lua byte code
  75. var outDir = $"obj/{Path.GetDirectoryName(ctx.assetPath)}";
  76. if (!Directory.Exists(outDir))
  77. Directory.CreateDirectory(outDir);
  78. var outPath = $"obj/{ctx.assetPath}c";
  79. #if UNITY_EDITOR_OSX
  80. var luac = "build/luac/build_unix/luac";
  81. #elif UNITY_EDITOR_WIN
  82. var luac = "build/luac/build32/luac.exe";
  83. #elif UNITY_EDITOR_WIN64
  84. var luac = "build/luac/build64/luac.exe";
  85. #endif
  86. var prmt = $"{(strip ? "-s" : "")} -o {outPath} -- {ctx.assetPath}";
  87. Compile(luac, prmt);
  88. data = File.ReadAllBytes(outPath);
  89. }
  90. else
  91. {
  92. data = File.ReadAllBytes(ctx.assetPath);
  93. }
  94. // TODO: your encode function, like xxtea
  95. if(encode)
  96. {
  97. data = Security.XXTEA.Encrypt(data, LuaAsset.LuaDecodeKey);
  98. }
  99. asset.data = data;
  100. asset.encode = encode;
  101. ctx.AddObjectToAsset("main obj", asset, LoadIconTexture(prefax));
  102. ctx.SetMainObject(asset);
  103. }
  104. private Texture2D LoadIconTexture(string prefax)
  105. {
  106. return AssetDatabase.LoadAssetAtPath("Assets/XLua/Editor/lua.png", typeof(Texture2D)) as
  107. Texture2D;
  108. }
  109. }
  110. [CustomEditor(typeof(LuaAsset))]
  111. public class LuaAssetEditor : UnityEditor.Editor
  112. {
  113. private static bool decode = true;
  114. private LuaAsset mTarget;
  115. private void OnEnable()
  116. {
  117. mTarget = target as LuaAsset;
  118. }
  119. public override void OnInspectorGUI()
  120. {
  121. GUI.enabled = true;
  122. EditorGUILayout.LabelField("Import Config(重新导入时生效)");
  123. {
  124. ++EditorGUI.indentLevel;
  125. LuaImporter.compile = EditorGUILayout.Toggle("compile(编译为字节码)", LuaImporter.compile);
  126. if (LuaImporter.compile)
  127. {
  128. ++EditorGUI.indentLevel;
  129. LuaImporter.strip = EditorGUILayout.Toggle("strip", LuaImporter.strip);
  130. --EditorGUI.indentLevel;
  131. }
  132. LuaImporter.encode = EditorGUILayout.Toggle("encode(加密)", LuaImporter.encode);
  133. --EditorGUI.indentLevel;
  134. }
  135. EditorGUILayout.Space();
  136. EditorGUILayout.LabelField("Display Config");
  137. {
  138. ++EditorGUI.indentLevel;
  139. GUI.enabled = false;
  140. EditorGUILayout.Toggle("encoded(加密)", mTarget.encode);
  141. GUI.enabled = true;
  142. if(mTarget.encode)
  143. decode = EditorGUILayout.Toggle("decode", decode);
  144. --EditorGUI.indentLevel;
  145. }
  146. var text = string.Empty;
  147. if (mTarget.encode && decode)
  148. {
  149. // TODO: your decode function
  150. text = Encoding.UTF8.GetString(Security.XXTEA.Decrypt(mTarget.data, LuaAsset.LuaDecodeKey));
  151. }else
  152. {
  153. text = Encoding.UTF8.GetString(mTarget.data);
  154. }
  155. var MaxTextPreviewLength = 4096;
  156. if (text.Length > MaxTextPreviewLength + 3)
  157. {
  158. text = text.Substring(0, MaxTextPreviewLength) + "...";
  159. }
  160. GUIStyle style = "ScriptText";
  161. Rect rect = GUILayoutUtility.GetRect(new GUIContent(text), style);
  162. rect.x = 0f;
  163. rect.y -= 3f;
  164. rect.width = EditorGUIUtility.currentViewWidth + 1f;
  165. GUI.Box(rect, text, style);
  166. }
  167. }
  168. #endif