jxlast/wb_unity_pro/Assets/Scripts/Editor/LuaBuild.cs

133 lines
3.5 KiB
C#
Raw Permalink Normal View History

2025-11-14 23:38:35 +08:00
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Diagnostics;
//using Debugger = LuaInterface.Debugger;
using System.Collections.Generic;
using taurus.client;
public class LuaBuildUtil {
/// <summary>
/// 数据目录
/// </summary>
static string AppDataPath
{
get { return Application.dataPath.ToLower(); }
}
//static void CreateStreamDir(string dir)
//{
// dir = Application.streamingAssetsPath + "/" + dir;
// if (!File.Exists(dir))
// {
// Directory.CreateDirectory(dir);
// }
//}
static void CopyLuaBytesFiles(string sourceDir, string destDir,bool app=false)
{
if (!Directory.Exists(sourceDir))
{
return;
}
string[] files = Directory.GetFiles(sourceDir, "*.lua", SearchOption.AllDirectories);
int len = sourceDir.Length;
if (sourceDir[len - 1] == '/' || sourceDir[len - 1] == '\\')
{
--len;
}
for (int i = 0; i < files.Length; i++)
{
string str = files[i].Remove(0, len);
string dest = destDir + str;
if(app)dest += ".bytes";
string dir = Path.GetDirectoryName(dest);
Directory.CreateDirectory(dir);
EncodeLuaFile(files[i], dest);
}
}
public static void EncodeLuaFile(string srcFile, string outFile)
{
#if UNITY_STANDALONE
File.Copy(srcFile, outFile, true);
#else
string luaexe = string.Empty;
string args = string.Empty;
string exedir = string.Empty;
string currDir = Directory.GetCurrentDirectory();
#if UNITY_ANDROID
if (BuildBaseWindow.build32Lua)
{
luaexe = "luajit32.exe";
args = "-b -g " + srcFile + " " + outFile;
exedir = AppDataPath.Replace("assets", "") + "LuaEncoder/Luajit/";
}
else
{
luaexe = "luajit64.exe";
args = "-b -g " + srcFile + " " + outFile;
exedir = AppDataPath.Replace("assets", "") + "LuaEncoder/Luajit64/";
}
#endif
#if UNITY_IPHONE
luaexe = "luajit64.exe";
args = "-b -g " + srcFile + " " + outFile;
exedir = AppDataPath.Replace("assets", "") + "LuaEncoder/Luajit64/";
#endif
Directory.SetCurrentDirectory(exedir);
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = luaexe;
info.Arguments = args;
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = true;
info.ErrorDialog = true;
//Debugger.Log(info.FileName + " " + info.Arguments);
Process pro = Process.Start(info);
pro.WaitForExit();
Directory.SetCurrentDirectory(currDir);
#endif
}
public static void BuildLuaBundles(string pack_path,List<string> lua_dir,string rootDir = null)
{
string tempDir = pack_path + "/templua";
if (Directory.Exists(tempDir))
{
Directory.Delete(tempDir, true);
}
else
{
Directory.CreateDirectory(tempDir);
}
string filename = pack_path + "/lua_pack";
foreach(string dir in lua_dir)
{
CopyLuaBytesFiles(dir, tempDir);
}
IFilePack zf = null;
zf = new FilePack20(filename, PackMode.Write);
zf.RootDir = rootDir;
zf.PackFile(tempDir, ".meta|.manifest");
//CopyLuaBytesFiles(tempDir, destDir);
Directory.Delete(tempDir, true);
//AssetDatabase.Refresh();
}
}