Tolua for Unity3d 编译字节码
想必折腾过Tolua的都知道, 为了最简化对Lua文件的支持。最优方案就是 把Lua文件 以文件的形式载入。而不是用 AssetBundle 的方式。这样会最优化的使用 Lua本身的一些文件 机制。
那么既然是 以lua文件的形式 载入肯定不能以 明文的形式了。 Tolua已经为我们 提供了 字节码加密的机制。
以上就是 Tolua 为我们集成 编译字节码的 工具。 对于 Tolua 加载 文本的.lua 文件和 字节码的.lua 文件是没有区别的。应为Tolua底层已经为我们实现了。
目前来看只是有一点不太友好,就是在 IOS 平台 armv7 和 arm64 的 字节码不能共用。言外之意就是 要么你编译两种字节码。要么你 你直接用明文的 lua 文件。要么 你只支持一种 CPU类型。
显然编译两种字节码是最优的方案; 对于编译细节在 LuaFramework_UGUI 已经详细介绍了
/// <summary>
/// 适用于 IOS arm64
/// </summary>
/// <param name="srcFile"></param>
/// <param name="outFile"></param>
private void EncodeLuaFile_64(string srcFile, string outFile)
{
string luaexe = string.Empty;
string exedir = string.Empty;
bool isWin = true;
string args = string.Format("-b -g {0} {1}",srcFile, outFile);
string currDir = Directory.GetCurrentDirectory();
if (Application.platform == RuntimePlatform.WindowsEditor)
{
isWin = true;
luaexe = "luajit64.exe";
exedir = Path.Combine(BundleConst.LuaEncoder, "luajit64/");
}
else if (Application.platform == RuntimePlatform.OSXEditor)
{
isWin = false;
luaexe = "./luajit";
exedir = Path.Combine(BundleConst.LuaEncoder, "luajit_mac/");
}
Directory.SetCurrentDirectory(exedir);
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.FileName = luaexe;
info.Arguments = args;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = isWin;
info.ErrorDialog = true;
System.Diagnostics.Process pro = System.Diagnostics.Process.Start(info);
pro.WaitForExit();
Directory.SetCurrentDirectory(currDir);
}
/// <summary>
/// 适用于 ios armv7 所有 android
/// </summary>
/// <param name="srcFile"></param>
/// <param name="outFile"></param>
private void EncodeLuaFile_32(string srcFile, string outFile)
{
string luaexe = string.Empty;
string exedir = string.Empty;
bool isWin = true;
string args = string.Format("-b -g {0} {1}", srcFile, outFile);
string currDir = Directory.GetCurrentDirectory();
if (Application.platform == RuntimePlatform.WindowsEditor)
{
isWin = true;
luaexe = "luajit32.exe";
exedir = Path.Combine(BundleConst.LuaEncoder, "Luajit32/");
}
else if (Application.platform == RuntimePlatform.OSXEditor)
{
isWin = false;
luaexe = "./luajit";
exedir = Path.Combine(BundleConst.LuaEncoder, "luajit_mac/");
}
Directory.SetCurrentDirectory(exedir);
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.FileName = luaexe;
info.Arguments = args;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
info.UseShellExecute = isWin;
info.ErrorDialog = true;
System.Diagnostics.Process pro = System.Diagnostics.Process.Start(info);
pro.WaitForExit();
Directory.SetCurrentDirectory(currDir);
}
对于编译的 更多细节 大家可以下载 https://github.com/jarjin/LuaFramework_UGUI
获取跟多的细节
现在我们已经编译出来了 不同的 lua 字节码: 经过反复测试我得到 一下结论
用 luajit64.exe 编译出来的字节码 只适用于 IOS arm64,
用 luajit32.exe 编译出来的字节码 只适用于 其他( IOS armv7, android )
有意思的 Android 目前 不管 armv7 还是 arm64 都只能使用 luajit32.exe 编译出来的字节码
别人总结的:
2018年8月22日 补充:
其实对于字节码的理解应该是这样的: 32位和64位的 字节码是对应的 目标机器,即 32位机器和64位机器。 很显然Tolua 已经为我们实现了 32位和64的 不同字节码的支持!。
所以呢我们只要为 相应的目标平台提供 对应的字节码就好了!
这是我最初的方案! 统计所有的机型 来判断 是32还是64! 现在看来 很漏记!
往下看:
private static List<DeviceType> deviceTypes = new List<DeviceType>()
{
#region Iphone
new DeviceType( "iPhone3,1","iPhone 4", false),
new DeviceType( "iPhone3,2","iPhone 4", false),
new DeviceType( "iPhone3,3","iPhone 4", false),
new DeviceType( "iPhone4,1","iPhone 4S", false),
new DeviceType( "iPhone5,1","iPhone 5", false),
new DeviceType( "iPhone5,2","iPhone 5", false),
new DeviceType( "iPhone5,3","iPhone 5c", false),
new DeviceType( "iPhone5,4","iPhone 5c", false),
new DeviceType( "iPhone6,1","iPhone 5S", true),///////arm64
new DeviceType( "iPhone6,2","iPhone 5S", true),
new DeviceType( "iPhone7,1","iPhone 6 Plus", true),
new DeviceType( "iPhone7,2","iPhone 6", true),
new DeviceType( "iPhone8,1","iPhone 6s", true),
new DeviceType( "iPhone8,2","iPhone 6s Plus", true),
new DeviceType( "iPhone8,4","iPhone SE", true),
new DeviceType( "iPhone9,1","iPhone 7", true),
new DeviceType( "iPhone9,2","iPhone 7 Plus", true),
new DeviceType( "iPhone9,3","iPhone 7", true),
new DeviceType( "iPhone9,4","iPhone 7 Plus", true),
new DeviceType( "iPhone10,1","iPhone 8", true),
new DeviceType( "iPhone10,2","iPhone 8 Plus", true),
new DeviceType( "iPhone10,4","iPhone 8", true),
new DeviceType( "iPhone10,5","iPhone 8 Plus", true),
new DeviceType( "iPhone10,3","iPhone X", true),
new DeviceType( "iPhone10,6","iPhone X", true),
#endregion
#region Ipad
new DeviceType( "iPad1,1", "iPad", false),
new DeviceType( "iPad1,2", "iPad 3G", false),
new DeviceType( "iPad2,1", "iPad 2", false),
new DeviceType( "iPad2,2", "iPad 2", false),
new DeviceType( "iPad2,3", "iPad 2", false),
new DeviceType( "iPad2,4", "iPad 2", false),
new DeviceType( "iPad2,5", "iPad Mini", false),
new DeviceType( "iPad2,6", "iPad Mini", false),
new DeviceType( "iPad2,7", "iPad Mini", false),
new DeviceType( "iPad3,1", "iPad 3", false),
new DeviceType( "iPad3,2", "iPad 3", false),
new DeviceType( "iPad3,3", "iPad 3", false),
new DeviceType( "iPad3,4", "iPad 4", false),
new DeviceType( "iPad3,5", "iPad 4", false),
new DeviceType( "iPad3,6", "iPad 4", false),
new DeviceType( "iPad4,1", "iPad Air", true),///arm 64
new DeviceType( "iPad4,2", "iPad Air", true),
new DeviceType( "iPad4,4", "iPad Mini 2", true),
new DeviceType( "iPad4,5", "iPad Mini 2", true),
new DeviceType( "iPad4,6", "iPad Mini 2", true),
new DeviceType( "iPad4,7", "iPad Mini 3", true),
new DeviceType( "iPad4,8", "iPad Mini 3", true),
new DeviceType( "iPad4,9", "iPad Mini 3", true),
new DeviceType( "iPad5,1", "iPad Mini 4", true),
new DeviceType( "iPad5,2", "iPad Mini 4", true),
new DeviceType( "iPad5,3", "iPad Air 2", true),
new DeviceType( "iPad5,4", "iPad Air 2", true),
new DeviceType( "iPad6,3", "iPad Pro 9.7", true),
new DeviceType( "iPad6,4", "iPad Pro 9.7", true),
new DeviceType( "iPad6,7", "iPad Pro 12.9",true),
new DeviceType( "iPad6,8", "iPad Pro 12.9",true),
new DeviceType( "iPad6,11", "iPad 5", true),
new DeviceType( "iPad6,12", "iPad 5", true),
new DeviceType( "iPad7,1", "iPad Pro 12.9 inch 2nd gen",true),
new DeviceType( "iPad7,2", "iPad Pro 12.9 inch 2nd gen",true),
new DeviceType( "iPad7,3", "iPad Pro 10.5 inch", true),
new DeviceType( "iPad7,4", "iPad Pro 10.5 inch", true),
#endregion
new DeviceType( "i386", "Simulator", false), //Mac指令集 i386是针对intel通用微处理器32位处理器
new DeviceType( "x86_64", "Simulator", true), //Mac指令集 x86_64是针对x86架构的64位处理器
};
public class DeviceType
{
public string modelType;
public string modelName;
public bool isArm64;//armv7 or arm64
public DeviceType(string type, string name, bool arm64)
{
this.modelType = type;
this.modelName = name;
this.isArm64 = arm64;
}
}
这个是是我找到的 最新的方案
/// <summary>
/// 判断当然 运行 品台是 x86 还是 x68_x64
/// </summary>
public static bool isX86_X64
{
get
{
if (System.IntPtr.Size == 4)
{
return false;
}
else
{
return true;
}
}
}
我已经在各个平台上测试了,都能准确分析出当前的 CPU结构!包括在编辑器 模式下
所以呢 我们就理所当然的 为我们的 目标平台提供对应的 字节码了
好吧! 求打赏下