Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 10 additions & 30 deletions ServerCodeExciser/ServerCodeExciser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using System.Text.Json.Serialization;
using UnrealAngelscriptServerCodeExcision;

namespace ServerCodeExcision
namespace ServerCodeExciser
{
internal sealed class ServerCodeExciserCommand : Command<ServerCodeExciserCommand.Settings>
{
Expand All @@ -28,9 +28,9 @@ public sealed class Settings : CommandSettings

[CommandOption("-f|--fullyexcise <REGEX>")]
[Description("If this switch is specified, the next argument should be a string containing regex entries." +
"If any of these regexes matches the relative path of any file to be processed, the entire file will be excised." +
"You can specify more than one entry by separating them with three pipes, eg: " +
"Characters /Paul/.*|||Weapons/Rife.as")]
"If any of these regexes matches the relative path of any file to be processed, the entire file will be excised." +
"You can specify more than one entry by separating them with three pipes, eg: " +
"Characters/Paul/.*|||Weapons/Rife.as")]
public string? FullExcisionRegexString { get; init; }

[CommandOption("-s|--dontskip")]
Expand All @@ -39,7 +39,7 @@ public sealed class Settings : CommandSettings

[CommandOption("-m|--minratio")]
[Description("Specify a ratio in percent as the next argument. If the excised % of code is less than this ratio, an error will be thrown. Good to detect catastrophic changes in excision performance.")]
public int? RequiredExcisionRatio { get; init; } = -1;
public int RequiredExcisionRatio { get; init; } = 0;

[CommandOption("-t|--funcstats")]
[Description("Outputs function stats instead of file stats. This is more accurate, but a lot slower, since it has to parse every file.")]
Expand All @@ -61,19 +61,9 @@ public sealed class Settings : CommandSettings
[Description("Ensure that all files can be analyzed without syntactic or lexicographical errors.")]
public bool StrictMode { get; init; }

[CommandArgument(0, "[INPUT]")]
[CommandArgument(0, "<INPUT_PATH>")]
[Description("The input folder to excise.")]
public string InputPath { get; init; } = string.Empty;

public override ValidationResult Validate()
{
if (InputPath.Length <= 0)
{
return ValidationResult.Error("Must provide at least one input path!");
}

return base.Validate();
}
public string? InputPath { get; init; }
}

class RootPaths
Expand All @@ -94,10 +84,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
parameters.StrictMode = settings.StrictMode;
parameters.UseFunctionStats = settings.UseFunctionStats;
parameters.DontSkip = settings.DontSkip;
if (settings.RequiredExcisionRatio.HasValue)
{
parameters.RequiredExcisionRatio = settings.RequiredExcisionRatio.Value / 100.0f;
}
parameters.RequiredExcisionRatio = settings.RequiredExcisionRatio / 100.0f;

if (File.Exists(settings.InputPath))
{
Expand All @@ -113,7 +100,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
return (int)EExciserReturnValues.InternalExcisionError;
}
}
else if(Directory.Exists(settings.InputPath))
else if (Directory.Exists(settings.InputPath))
{
parameters.InputPaths.Add(settings.InputPath);
}
Expand Down Expand Up @@ -152,14 +139,7 @@ public class ServerCodeExciser
{
public static int Main(string[] args)
{
if (args.Length < 1)
{
Console.Error.WriteLine("You must provide an input path to read input files from as the first argument.");
return (int)EExciserReturnValues.BadInputPath;
}

var app = new CommandApp<ServerCodeExciserCommand>();
return app.Run(args);
return new CommandApp<ServerCodeExciserCommand>().Run(args);
}
}
}
26 changes: 14 additions & 12 deletions ServerCodeExciser/ServerCodeExcisionProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Antlr4.Runtime;
using ServerCodeExcisionCommon;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ServerCodeExcision
namespace ServerCodeExciser
{
public class ServerCodeExcisionParameters
{
Expand All @@ -22,7 +22,7 @@ public class ServerCodeExcisionParameters
public bool StrictMode { get; set; } = false;
public bool UseFunctionStats { get; set; } = false;
public bool DontSkip { get; set; } = false;
public float RequiredExcisionRatio { get; set; } = -1.0f;
public float RequiredExcisionRatio { get; set; } = 0.0f;
public EExcisionLanguage ExcisionLanguage { get; set; } = EExcisionLanguage.Unknown;
}

Expand All @@ -37,7 +37,7 @@ public ServerCodeExcisionProcessor(ServerCodeExcisionParameters parameters)
_parameters = parameters;

_functionExciseRegexes = new List<Regex>();
if (_parameters.ExciseAllFunctionsRegexString != "")
if (!string.IsNullOrEmpty(_parameters.ExciseAllFunctionsRegexString))
{
var allFunctionExciseRegexStrings = _parameters.ExciseAllFunctionsRegexString.Split("|||");
foreach (var regexString in allFunctionExciseRegexStrings)
Expand All @@ -48,7 +48,7 @@ public ServerCodeExcisionProcessor(ServerCodeExcisionParameters parameters)
}

_fullyExciseRegexes = new List<Regex>();
if (_parameters.FullExcisionRegexString != "")
if (!string.IsNullOrEmpty(_parameters.FullExcisionRegexString))
{
var fullyExciseRegexStrings = _parameters.FullExcisionRegexString.Split("|||");
foreach (var regexString in fullyExciseRegexStrings)
Expand All @@ -61,7 +61,7 @@ public ServerCodeExcisionProcessor(ServerCodeExcisionParameters parameters)

public EExciserReturnValues ExciseServerCode(string filePattern, IServerCodeExcisionLanguage excisionLanguage)
{
var startTime = DateTime.UtcNow;
var globalStopwatch = Stopwatch.StartNew();
var globalStats = new ExcisionStats();

var options = new EnumerationOptions();
Expand Down Expand Up @@ -107,30 +107,33 @@ public EExciserReturnValues ExciseServerCode(string filePattern, IServerCodeExci

try
{
var stopwatch = Stopwatch.StartNew();
var stats = ProcessCodeFile(fileName, inputPath, excisionMode, excisionLanguage);
stopwatch.Stop();

if (stats.CharactersExcised > 0)
{
System.Diagnostics.Debug.Assert(stats.TotalNrCharacters > 0, "Something is terribly wrong. We have excised characters, but no total characters..?");
var excisionRatio = (float)stats.CharactersExcised / (float)stats.TotalNrCharacters * 100.0f;
Console.WriteLine("Excised {0:0.00}% of server only code in file ({1}/{2}): {3}", excisionRatio, fileIdx + 1, allFiles.Length, fileName);
Console.WriteLine($"[{fileIdx + 1}/{allFiles.Length}] Excised {excisionRatio:0.00}% of server only code in file (took {stopwatch.Elapsed.TotalMilliseconds:0.0}ms): {fileName}");
}
else
{
Console.WriteLine("No server only code found in file ({0}/{1}): {2}", fileIdx + 1, allFiles.Length, fileName);
Console.WriteLine($"[{fileIdx + 1}/{allFiles.Length}] No server only code found in file: {fileName}");
}

globalStats.CharactersExcised += stats.CharactersExcised;
globalStats.TotalNrCharacters += stats.TotalNrCharacters;
}
catch (Exception)
{
Console.WriteLine("Failed to parse ({0}/{1}): {2}", fileIdx + 1, allFiles.Length, fileName);
Console.Error.WriteLine($"[{fileIdx + 1}/{allFiles.Length}] Failed to parse: {fileName}");
throw;
}
}
}

var endTime = DateTime.UtcNow;
globalStopwatch.Stop();

// In verification mode error codes reverse the normal behavior of the exciser.
// Modifications required -> error
Expand All @@ -157,8 +160,7 @@ public EExciserReturnValues ExciseServerCode(string filePattern, IServerCodeExci
Console.WriteLine("Excised {0:0.00}% ({1}/{2} characters) of server only code from the script files.",
totalExcisionRatio, globalStats.CharactersExcised, globalStats.TotalNrCharacters);

var timeTaken = endTime - startTime;
Console.WriteLine("Excision took {0:0} hours, {1:0} minutes and {2:0.0} seconds.\n\n", timeTaken.Hours, timeTaken.Minutes, timeTaken.Seconds);
Console.WriteLine($"Excision took {globalStopwatch.Elapsed.TotalSeconds:0.0}s.");

if (_parameters.RequiredExcisionRatio > 0.0f && totalExcisionRatio < _parameters.RequiredExcisionRatio)
{
Expand Down
2 changes: 1 addition & 1 deletion ServerCodeExciserTest/ExcisionIntegrationTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ServerCodeExcision;
using ServerCodeExciser;
using ServerCodeExcisionCommon;
using Spectre.Console;
using System;
Expand Down
Loading