Adding some initial playing around with lua bytecode from secondary local source code copies.

lua-bytecode
Icedream 2014-12-11 03:45:22 +01:00
parent 6b89b38b96
commit 496d0b744c
8 changed files with 131 additions and 1 deletions

4
.gitmodules vendored Normal file
View File

@ -0,0 +1,4 @@
[submodule "external/sharplua-mod"]
path = external/sharplua-mod
url = git://github.com/icedream/SharpLua.git
branch = master

View File

@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{45F8AB
.nuget\NuGet.targets = .nuget\NuGet.targets .nuget\NuGet.targets = .nuget\NuGet.targets
EndProjectSection EndProjectSection
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharpLua", "external\sharplua-mod\SharpLua\SharpLua.csproj", "{90A9C907-11AD-4754-8B70-51CCB3A6C98A}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -22,6 +24,10 @@ Global
{E33D031D-7866-40F9-9362-2776CB4A7B5B}.Debug|Any CPU.Build.0 = Debug|Any CPU {E33D031D-7866-40F9-9362-2776CB4A7B5B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E33D031D-7866-40F9-9362-2776CB4A7B5B}.Release|Any CPU.ActiveCfg = Release|Any CPU {E33D031D-7866-40F9-9362-2776CB4A7B5B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E33D031D-7866-40F9-9362-2776CB4A7B5B}.Release|Any CPU.Build.0 = Release|Any CPU {E33D031D-7866-40F9-9362-2776CB4A7B5B}.Release|Any CPU.Build.0 = Release|Any CPU
{90A9C907-11AD-4754-8B70-51CCB3A6C98A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90A9C907-11AD-4754-8B70-51CCB3A6C98A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90A9C907-11AD-4754-8B70-51CCB3A6C98A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{90A9C907-11AD-4754-8B70-51CCB3A6C98A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

1
external/sharplua-mod vendored Submodule

@ -0,0 +1 @@
Subproject commit 9438735d98effb12451fec84afae0b2a29d5d5ac

View File

@ -5,6 +5,11 @@ using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
#if LUA_BYTECODE
using GarrysMod.AddonCreator.Compiler;
using SharpLua;
using SharpLua.Visitors;
#endif
namespace GarrysMod.AddonCreator.Addon namespace GarrysMod.AddonCreator.Addon
{ {
@ -42,9 +47,72 @@ namespace GarrysMod.AddonCreator.Addon
_luaCode = Encoding.UTF8.GetString(_fi.GetContents()); _luaCode = Encoding.UTF8.GetString(_fi.GetContents());
_luaCode = Regex.Replace(_luaCode, _stripCommentsRegex, m => m.Groups["linebreak"] != null ? m.Groups["linebreak"].Value : ""); _luaCode = Regex.Replace(_luaCode, _stripCommentsRegex, m => m.Groups["linebreak"] != null ? m.Groups["linebreak"].Value : "");
#if LUA_BYTECODE
GenerateBytecode();
#else
_content = Encoding.UTF8.GetBytes(_luaCode);
#endif
return _content; return _content;
} }
#if LUA_BYTECODE
private void GenerateBytecode()
{
var lua = Lua.lua_open();
if (Lua.lua_cpcall(lua, LuaMain, null) != 0)
throw new Exception();
Lua.lua_close(lua);
Debug.WriteLine("Bytecode generated ({0} bytes)", _content.Length);
if (_content.Length == 0)
{
Debugger.Break();
}
}
private int LuaMain(Lua.LuaState lua)
{
try
{
var res = Lua.luaL_loadstring(lua, _luaCode);
if (res != 0)
{
Console.Error.WriteLine("ERROR while loading Lua code");
return -1;
}
using (var ms = new MemoryStream())
{
Lua.lua_lock(lua);
Lua.luaU_dump(lua, Lua.clvalue(lua.top - 1).l.p, LuaWriter, ms, 1);
Lua.lua_unlock(lua);
_content = ms.ToArray();
}
return 0;
}
catch (LuaSourceException error)
{
Console.Error.WriteLine("PARSE ERROR - Line {1}, col {2}: {0}", error.Message, error.Line, error.Column);
File.WriteAllText("generated.lua", _luaCode);
return -1;
}
catch (LuaScriptException error)
{
Console.Error.WriteLine("LUA ERROR - {1}: {0}", error.Message, error.Source);
File.WriteAllText("generated.lua", _luaCode);
return -1;
}
}
static int LuaWriter(Lua.LuaState lua, Lua.CharPtr ptr, uint size, object targetStream)
{
return ((Lua.fwrite(ptr, (int)size, 1, (Stream)targetStream) != 1) && (size != 0)) ? 1 : 0;
}
#endif
} }
} }

View File

@ -0,0 +1,31 @@
#if LUA_BYTECODE
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using KopiLua;
namespace GarrysMod.AddonCreator.Compiler
{
public static class LuaCompiler
{
public static byte[] Compile(string luaCode)
{
var lua = new NLua.Lua();
var b = new List<byte>();
var meth = new Func<string>(() => luaCode);
lua.LoadString(luaCode, "luaCode");
lua.RegisterFunction("appendBytecode", b, b.GetType().GetMethod("Add"));
lua.RegisterFunction("appendBytecode", b, b.GetType().GetMethod("Add"));
lua.DoString("for b in string.gmatch(string.dump((luaCode),true),\".\") do appendBytecode(string.byte(b)) end");
return b.ToArray();
}
}
}
#endif

View File

@ -1,4 +1,5 @@
using System.IO; using System;
using System.IO;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -41,6 +42,11 @@ namespace GarrysMod.AddonCreator
return sb.ToString(); return sb.ToString();
} }
public static string EscapeShellArgument(this String s)
{
return "\"" + Regex.Replace(s, @"(\\+)$", @"$1$1") + "\"";
}
/// <summary> /// <summary>
/// Writes a string. If nullTerminated is true, the string will not be written using native string writing but without length prefix and with NULL char termination instead. /// Writes a string. If nullTerminated is true, the string will not be written using native string writing but without length prefix and with NULL char termination instead.
/// </summary> /// </summary>

View File

@ -44,10 +44,16 @@
<StartupObject>GarrysMod.AddonCreator.Program</StartupObject> <StartupObject>GarrysMod.AddonCreator.Program</StartupObject>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="KopiLua">
<HintPath>..\..\packages\NLua_Safe.1.3.1.5\lib\net40\KopiLua.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <SpecificVersion>False</SpecificVersion>
<HintPath>$(SolutionDir)\packages\Newtonsoft.Json.6.0.5\lib\net40\Newtonsoft.Json.dll</HintPath> <HintPath>$(SolutionDir)\packages\Newtonsoft.Json.6.0.5\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="NLua">
<HintPath>..\..\packages\NLua_Safe.1.3.1.5\lib\net40\NLua.dll</HintPath>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
</ItemGroup> </ItemGroup>
@ -57,6 +63,7 @@
<Compile Include="Addon\AddonJson.cs" /> <Compile Include="Addon\AddonJson.cs" />
<Compile Include="Addon\AddonWhitelist.cs" /> <Compile Include="Addon\AddonWhitelist.cs" />
<Compile Include="Addon\LuaAddonFileInfo.cs" /> <Compile Include="Addon\LuaAddonFileInfo.cs" />
<Compile Include="Compiler\LuaCompiler.cs" />
<Compile Include="Extensions.cs" /> <Compile Include="Extensions.cs" />
<Compile Include="Addon\JsonAddonFileInfo.cs" /> <Compile Include="Addon\JsonAddonFileInfo.cs" />
<Compile Include="Hashing\Crc32.cs" /> <Compile Include="Hashing\Crc32.cs" />
@ -68,6 +75,12 @@
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\external\sharplua-mod\SharpLua\SharpLua.csproj">
<Project>{90a9c907-11ad-4754-8b70-51ccb3a6c98a}</Project>
<Name>SharpLua</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Tests\" /> <Folder Include="Tests\" />
</ItemGroup> </ItemGroup>

View File

@ -2,4 +2,5 @@
<packages> <packages>
<package id="Newtonsoft.Json" version="6.0.5" targetFramework="net40" /> <package id="Newtonsoft.Json" version="6.0.5" targetFramework="net40" />
<package id="NLua_Safe" version="1.3.1.5" targetFramework="net40" />
</packages> </packages>