diff --git a/src/addoncreator/Addon/AddonFile.cs b/src/addoncreator/Addon/AddonFile.cs index 516687c..ab1a5b7 100644 --- a/src/addoncreator/Addon/AddonFile.cs +++ b/src/addoncreator/Addon/AddonFile.cs @@ -222,12 +222,15 @@ namespace GarrysMod.AddonCreator.Addon throw new FileNotFoundException("Addon building requires a valid addon.json file."); } - 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); + var files = Files.ToDictionary(i => i.Key, i => i.Value); + + if (MinimizeLua) + files = files + // minimize lua code + .Select(f => f.Key.EndsWith(".lua", StringComparison.OrdinalIgnoreCase) + ? new KeyValuePair(f.Key, new MinifiedLuaAddonFileInfo(f.Value)) + : f) + .ToDictionary(i => i.Key, i => i.Value); // Check for errors and ignores in addon.json var addonJson = @@ -340,5 +343,10 @@ namespace GarrysMod.AddonCreator.Addon } } } + + /// + /// Indicates whether Lua files will have comments and unnecessary whitespace stripped out on export. + /// + public bool MinimizeLua { get; set; } } } \ No newline at end of file diff --git a/src/addoncreator/Addon/LuaAddonFileInfo.cs b/src/addoncreator/Addon/MinifiedLuaAddonFileInfo.cs similarity index 54% rename from src/addoncreator/Addon/LuaAddonFileInfo.cs rename to src/addoncreator/Addon/MinifiedLuaAddonFileInfo.cs index 23d50dd..44fe344 100644 --- a/src/addoncreator/Addon/LuaAddonFileInfo.cs +++ b/src/addoncreator/Addon/MinifiedLuaAddonFileInfo.cs @@ -1,51 +1,63 @@ -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 : ""); - - _content = Encoding.UTF8.GetBytes(_luaCode); - - return _content; - } - - } +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 MinifiedLuaAddonFileInfo : AddonFileInfo + { + private readonly AddonFileInfo _fi; + + private byte[] _content; + + private readonly string _stripCommentsRegex = string.Join("|", new[] + { + // block comments + @"\/\*.*\*\/", + @"\-\-\[\[.*\]\]", + + // line comments + @"//([^\r\n]*?)(?[\r\n]+)", + @"\-\-([^\r\n]*?)(?[\r\n]+)", + + // Unnecessary whitespace + @"^[\s]*$", + @"[\s]*$", + @"^[\s]*" + }); + private string _luaCode; + + public MinifiedLuaAddonFileInfo(AddonFileInfo actual) + { + _fi = actual; + } + + public override byte[] GetContents() + { + if (_content != null) + return _content; + + _luaCode = Encoding.UTF8.GetString(_fi.GetContents()); + string _oldLuaCode; + do + { + _oldLuaCode = _luaCode; + _luaCode = Regex.Replace(_luaCode, _stripCommentsRegex, + m => m.Groups["linebreak"] != null ? m.Groups["linebreak"].Value : "", RegexOptions.Multiline | RegexOptions.Singleline); + _luaCode = _luaCode.Trim(); + } while (_oldLuaCode != _luaCode); + + _content = Encoding.UTF8.GetBytes(_luaCode); + + return _content; + } + + } } \ No newline at end of file diff --git a/src/addoncreator/GarrysMod.AddonCreator.csproj b/src/addoncreator/GarrysMod.AddonCreator.csproj index 6347bac..47b9196 100644 --- a/src/addoncreator/GarrysMod.AddonCreator.csproj +++ b/src/addoncreator/GarrysMod.AddonCreator.csproj @@ -56,7 +56,7 @@ - + diff --git a/src/addoncreator/Program.cs b/src/addoncreator/Program.cs index b7c6c15..0974459 100644 --- a/src/addoncreator/Program.cs +++ b/src/addoncreator/Program.cs @@ -11,10 +11,20 @@ namespace GarrysMod.AddonCreator { private static void Main(string[] args) { + var minimizeLua = false; + while (args.Any()) { switch (args.Length == 0 ? "" : args[0]) { + case "--minimize-lua": + minimizeLua = true; + args = args.Skip(1).ToArray(); + break; + case "--no-minimize-lua": + minimizeLua = false; + args = args.Skip(1).ToArray(); + break; case "create": { if (args.Length < 3) @@ -24,7 +34,7 @@ namespace GarrysMod.AddonCreator var folder = new DirectoryInfo(args[1]); var output = args[2]; - var addon = new AddonFile(); + var addon = new AddonFile {MinimizeLua = minimizeLua}; if (!folder.Exists) { @@ -133,7 +143,7 @@ namespace GarrysMod.AddonCreator } default: - Console.WriteLine("Usage: {0} ", Process.GetCurrentProcess().ProcessName); + Console.WriteLine("Usage: {0} ", Process.GetCurrentProcess().ProcessName); Console.WriteLine(); Console.WriteLine("Commands:"); Console.WriteLine("\t{0}\t{1}", "extract", "Extracts a GMA file and shows information about it."); @@ -141,6 +151,10 @@ namespace GarrysMod.AddonCreator Console.WriteLine("\t{0}\t{1}", "create", "Creates a GMA file."); Console.WriteLine("\t\tArguments: Input folder path, output GMA file path"); Console.WriteLine(); + Console.WriteLine("Options:"); + Console.WriteLine("\t{0}\t{1}", "--minimize-lua", "Causes exported GMAs to have all Lua comments and unneeded whitespace in Lua stripped out."); + Console.WriteLine("\t{0}\t{1}", "--no-minimize-lua", "(default) Will prevent Lua files getting minimized."); + Console.WriteLine(); return; } }