diff --git a/src/addoncreator/Addon/AddonFile.cs b/src/addoncreator/Addon/AddonFile.cs index 25474d0..516687c 100644 --- a/src/addoncreator/Addon/AddonFile.cs +++ b/src/addoncreator/Addon/AddonFile.cs @@ -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(f.Key, new LuaAddonFileInfo(f.Value)) + : f) + .ToDictionary(i => i.Key, i => i.Value); // Check for errors and ignores in addon.json var addonJson = diff --git a/src/addoncreator/Addon/LuaAddonFileInfo.cs b/src/addoncreator/Addon/LuaAddonFileInfo.cs new file mode 100644 index 0000000..b39cc06 --- /dev/null +++ b/src/addoncreator/Addon/LuaAddonFileInfo.cs @@ -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 +{ + /// + /// Wrapper around another file info for optimizing Lua files. + /// + public class LuaAddonFileInfo : AddonFileInfo + { + private readonly AddonFileInfo _fi; + + private byte[] _content; + + private readonly string _stripCommentsRegex = string.Join("|", new[] + { + // block comments + @"/\*(.*?)\*/", + @"\-\-\[\[(.*?)\]\]", + + // line comments + @"//(.*?)(?\r?\n)", + @"\-\-(.*?)(?\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; + } + + } +} \ No newline at end of file diff --git a/src/addoncreator/GarrysMod.AddonCreator.csproj b/src/addoncreator/GarrysMod.AddonCreator.csproj index 3895f1d..6347bac 100644 --- a/src/addoncreator/GarrysMod.AddonCreator.csproj +++ b/src/addoncreator/GarrysMod.AddonCreator.csproj @@ -40,6 +40,9 @@ $(SolutionDir)\obj\$(TargetName)\$(Configuration)\$(Platform)\ $(SolutionDir)\bin\$(Configuration)\$(Platform)\ + + GarrysMod.AddonCreator.Program + False @@ -53,6 +56,7 @@ + @@ -64,6 +68,9 @@ + + +