190 lines
5.9 KiB
C#
190 lines
5.9 KiB
C#
using System;
|
||
using UnityEngine;
|
||
using System.IO;
|
||
using UnityEditor;
|
||
using System.Collections.Generic;
|
||
using CSObjectWrapEditor;
|
||
using XLua;
|
||
using System.Diagnostics;
|
||
|
||
namespace BFEditor.Build
|
||
{
|
||
public static class CompileScriptsUtils
|
||
{
|
||
public static void CompileAndEncryptLua()
|
||
{
|
||
if (!CompileAndEncryptLua(true))
|
||
{
|
||
throw new Exception();
|
||
}
|
||
}
|
||
|
||
public static bool CompileAndEncryptLua(bool debugFlag)
|
||
{
|
||
var result = false;
|
||
if (CompileDevLuaToRunLua(debugFlag))
|
||
{
|
||
ProjectEncryptUtils.EncryptLua();
|
||
result = true;
|
||
}
|
||
AssetDatabase.SaveAssets();
|
||
AssetDatabase.Refresh();
|
||
return result;
|
||
}
|
||
|
||
public static bool CompileDevLuaToRunLua(bool debugFlag = true)
|
||
{
|
||
UnityEngine.Debug.Log("[bfinfo]编译lua代码...");
|
||
var srcDir = Path.Combine(Application.dataPath, "Developer/lua");
|
||
var destDir = Path.Combine(Application.dataPath, "lua");
|
||
return CompileLua(srcDir, destDir, debugFlag);
|
||
}
|
||
|
||
static bool CompileLua(string srcDir, string destDir, bool debugFlag)
|
||
{
|
||
var startTime = DateTime.Now.Ticks;
|
||
var srcFileMap = new Dictionary<string, bool>();
|
||
|
||
#if UNITY_EDITOR_OSX
|
||
var scriptPath = Path.Combine(Application.dataPath, "../tools/luac/mac/compile.sh");
|
||
var luacPath = Path.Combine(Application.dataPath, "../tools/luac/mac/luac");
|
||
#else
|
||
srcDir = srcDir.Replace('\\', '/');
|
||
destDir = destDir.Replace('\\', '/');
|
||
var scriptPath = Path.Combine(Application.dataPath, "../tools/luac/windows/compile.bat").Replace('\\', '/');
|
||
var luacPath = Path.Combine(Application.dataPath, "../tools/luac/windows/luac.exe").Replace('\\', '/');
|
||
#endif
|
||
if (luacPath.Length == 0 || !File.Exists(luacPath))
|
||
{
|
||
EditorUtility.ClearProgressBar();
|
||
EditorUtility.DisplayDialog("提示", "编译Lua失败 找不到luac", "确定");
|
||
return false;
|
||
}
|
||
|
||
EditorUtility.DisplayProgressBar("提示", "正在编译lua文件...", 0);
|
||
|
||
var srcDirInfo = new DirectoryInfo(srcDir);
|
||
var srcFiles = srcDirInfo.GetFiles("*.lua", SearchOption.AllDirectories);
|
||
var fileList = new List<FileInfo>(srcFiles);
|
||
|
||
for (int i = fileList.Count - 1; i >= 0; i--)
|
||
{
|
||
if (fileList[i].DirectoryName.Contains("/Test/"))
|
||
{
|
||
fileList.RemoveAt(i);
|
||
}
|
||
}
|
||
|
||
var totalCount = fileList.Count;
|
||
var unit = (float)totalCount / 100;
|
||
|
||
for (var i = 0; i < fileList.Count; ++i)
|
||
{
|
||
|
||
var fileInfo = fileList[i];
|
||
var assetName = fileInfo.FullName.Substring(srcDir.Length) + ".bytes";
|
||
srcFileMap[assetName] = true;
|
||
|
||
var subPath = "";
|
||
#if UNITY_EDITOR_WIN
|
||
var subIndex = assetName.Replace('\\', '/').LastIndexOf('/');
|
||
#else
|
||
var subIndex = assetName.LastIndexOf('/');
|
||
#endif
|
||
if (subIndex >= 0)
|
||
{
|
||
subPath = assetName.Substring(0, subIndex + 1);
|
||
}
|
||
|
||
var copyPath = destDir + subPath;
|
||
if (!Directory.Exists(copyPath))
|
||
{
|
||
Directory.CreateDirectory(copyPath);
|
||
}
|
||
}
|
||
#if UNITY_EDITOR_OSX
|
||
var startInfo = new ProcessStartInfo("sh");
|
||
startInfo.Arguments = scriptPath + " " + srcDir + " " + srcDir.Length + " " + destDir + " " + luacPath;
|
||
#else
|
||
var startInfo = new ProcessStartInfo(scriptPath);
|
||
startInfo.Arguments = " " + srcDir + " " + srcDir.Length + " " + destDir + " " + luacPath;
|
||
#endif
|
||
startInfo.WorkingDirectory = srcDir;
|
||
startInfo.UseShellExecute = false;
|
||
startInfo.RedirectStandardInput = true;
|
||
startInfo.RedirectStandardOutput = true;
|
||
startInfo.CreateNoWindow = true;
|
||
var process = new Process();
|
||
process.StartInfo = startInfo;
|
||
process.Start();
|
||
|
||
process.BeginOutputReadLine();
|
||
process.OutputDataReceived += (sender, e) =>
|
||
{
|
||
if (!string.IsNullOrEmpty(e.Data))
|
||
{
|
||
UnityEngine.Debug.Log(e.Data);
|
||
}
|
||
};
|
||
|
||
process.WaitForExit();
|
||
process.Close();
|
||
|
||
EditorUtility.ClearProgressBar();
|
||
|
||
var deleteList = new List<string>();
|
||
var destDirInfo = new DirectoryInfo(destDir);
|
||
var destFiles = destDirInfo.GetFiles("*.bytes", SearchOption.AllDirectories);
|
||
for (var i = 0; i < destFiles.Length; ++i)
|
||
{
|
||
var fileInfo = destFiles[i];
|
||
var assetName = fileInfo.FullName.Substring(destDir.Length);
|
||
if (!srcFileMap.ContainsKey(assetName))
|
||
{
|
||
deleteList.Add(fileInfo.FullName);
|
||
}
|
||
}
|
||
|
||
if (deleteList.Count > 0)
|
||
{
|
||
EditorUtility.DisplayProgressBar("提示", "正在删除失效lua文件", 0);
|
||
var count = deleteList.Count;
|
||
foreach (var path in deleteList)
|
||
{
|
||
UnityEngine.Debug.LogWarning("Delete dest lua compile file " + path);
|
||
File.Delete(path);
|
||
}
|
||
EditorUtility.ClearProgressBar();
|
||
}
|
||
|
||
var endTime = DateTime.Now.Ticks;
|
||
UnityEngine.Debug.Log("Lua文件已完成编译,用时" + (endTime - startTime) / 10000 + "毫秒");
|
||
return true;
|
||
}
|
||
|
||
public static void RegenerateXLuaCode(bool showDialog = false)
|
||
{
|
||
if (showDialog)
|
||
{
|
||
EditorUtility.DisplayProgressBar("提示", "正在重新生成xlua代码...", 1);
|
||
}
|
||
UnityEngine.Debug.Log("[bfinfo]重新生成XLuaCode...");
|
||
|
||
DelegateBridge.Gen_Flag = true;
|
||
|
||
Generator.ClearAll();
|
||
AssetDatabase.SaveAssets();
|
||
AssetDatabase.Refresh();
|
||
|
||
Generator.GenAll();
|
||
AssetDatabase.SaveAssets();
|
||
AssetDatabase.Refresh();
|
||
|
||
if (showDialog)
|
||
{
|
||
EditorUtility.ClearProgressBar();
|
||
}
|
||
}
|
||
}
|
||
}
|