Process Lua scripts by stripping comments. This can save up a few bytes up to kilobytes.

lua-bytecode
Icedream 2014-12-11 03:43:36 +01:00
parent 8c57d7befb
commit 6b89b38b96
3 changed files with 63 additions and 1 deletions

View File

@ -222,7 +222,12 @@ namespace GarrysMod.AddonCreator.Addon
throw new FileNotFoundException("Addon building requires a valid addon.json file.");
}
var files = Files;
var files = Files
// minimize lua code
.Select(f => f.Key.EndsWith(".lua", StringComparison.OrdinalIgnoreCase)
? new KeyValuePair<string, AddonFileInfo>(f.Key, new LuaAddonFileInfo(f.Value))
: f)
.ToDictionary(i => i.Key, i => i.Value);
// Check for errors and ignores in addon.json
var addonJson =

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace GarrysMod.AddonCreator.Addon
{
/// <summary>
/// Wrapper around another file info for optimizing Lua files.
/// </summary>
public class LuaAddonFileInfo : AddonFileInfo
{
private readonly AddonFileInfo _fi;
private byte[] _content;
private readonly string _stripCommentsRegex = string.Join("|", new[]
{
// block comments
@"/\*(.*?)\*/",
@"\-\-\[\[(.*?)\]\]",
// line comments
@"//(.*?)(?<linebreak>\r?\n)",
@"\-\-(.*?)(?<linebreak>\r?\n)"
});
private string _luaCode;
public LuaAddonFileInfo(AddonFileInfo actual)
{
_fi = actual;
}
public override byte[] GetContents()
{
if (_content != null)
return _content;
_luaCode = Encoding.UTF8.GetString(_fi.GetContents());
_luaCode = Regex.Replace(_luaCode, _stripCommentsRegex, m => m.Groups["linebreak"] != null ? m.Groups["linebreak"].Value : "");
return _content;
}
}
}

View File

@ -40,6 +40,9 @@
<BaseIntermediateOutputPath>$(SolutionDir)\obj\$(TargetName)\$(Configuration)\$(Platform)\</BaseIntermediateOutputPath>
<OutputPath>$(SolutionDir)\bin\$(Configuration)\$(Platform)\</OutputPath>
</PropertyGroup>
<PropertyGroup>
<StartupObject>GarrysMod.AddonCreator.Program</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
@ -53,6 +56,7 @@
<Compile Include="Addon\AddonFileInfo.cs" />
<Compile Include="Addon\AddonJson.cs" />
<Compile Include="Addon\AddonWhitelist.cs" />
<Compile Include="Addon\LuaAddonFileInfo.cs" />
<Compile Include="Extensions.cs" />
<Compile Include="Addon\JsonAddonFileInfo.cs" />
<Compile Include="Hashing\Crc32.cs" />
@ -64,6 +68,9 @@
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Tests\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">