Skip to content

Commit 5af7a44

Browse files
authored
Merge pull request #23 from EmbarkStudios/webbju/general-codebase-fixes
Assorted codebase fixes
2 parents acbbe26 + add8734 commit 5af7a44

File tree

3 files changed

+25
-43
lines changed

3 files changed

+25
-43
lines changed

ServerCodeExciser/ServerCodeExciser.cs

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using System.Text.Json.Serialization;
1010
using UnrealAngelscriptServerCodeExcision;
1111

12-
namespace ServerCodeExcision
12+
namespace ServerCodeExciser
1313
{
1414
internal sealed class ServerCodeExciserCommand : Command<ServerCodeExciserCommand.Settings>
1515
{
@@ -28,9 +28,9 @@ public sealed class Settings : CommandSettings
2828

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

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

4040
[CommandOption("-m|--minratio")]
4141
[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.")]
42-
public int? RequiredExcisionRatio { get; init; } = -1;
42+
public int RequiredExcisionRatio { get; init; } = 0;
4343

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

64-
[CommandArgument(0, "[INPUT]")]
64+
[CommandArgument(0, "<INPUT_PATH>")]
6565
[Description("The input folder to excise.")]
66-
public string InputPath { get; init; } = string.Empty;
67-
68-
public override ValidationResult Validate()
69-
{
70-
if (InputPath.Length <= 0)
71-
{
72-
return ValidationResult.Error("Must provide at least one input path!");
73-
}
74-
75-
return base.Validate();
76-
}
66+
public string? InputPath { get; init; }
7767
}
7868

7969
class RootPaths
@@ -94,10 +84,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
9484
parameters.StrictMode = settings.StrictMode;
9585
parameters.UseFunctionStats = settings.UseFunctionStats;
9686
parameters.DontSkip = settings.DontSkip;
97-
if (settings.RequiredExcisionRatio.HasValue)
98-
{
99-
parameters.RequiredExcisionRatio = settings.RequiredExcisionRatio.Value / 100.0f;
100-
}
87+
parameters.RequiredExcisionRatio = settings.RequiredExcisionRatio / 100.0f;
10188

10289
if (File.Exists(settings.InputPath))
10390
{
@@ -113,7 +100,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
113100
return (int)EExciserReturnValues.InternalExcisionError;
114101
}
115102
}
116-
else if(Directory.Exists(settings.InputPath))
103+
else if (Directory.Exists(settings.InputPath))
117104
{
118105
parameters.InputPaths.Add(settings.InputPath);
119106
}
@@ -152,14 +139,7 @@ public class ServerCodeExciser
152139
{
153140
public static int Main(string[] args)
154141
{
155-
if (args.Length < 1)
156-
{
157-
Console.Error.WriteLine("You must provide an input path to read input files from as the first argument.");
158-
return (int)EExciserReturnValues.BadInputPath;
159-
}
160-
161-
var app = new CommandApp<ServerCodeExciserCommand>();
162-
return app.Run(args);
142+
return new CommandApp<ServerCodeExciserCommand>().Run(args);
163143
}
164144
}
165145
}

ServerCodeExciser/ServerCodeExcisionProcessor.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using Antlr4.Runtime;
22
using ServerCodeExcisionCommon;
33
using System;
4-
using System.Collections;
54
using System.Collections.Generic;
5+
using System.Diagnostics;
66
using System.IO;
77
using System.Linq;
88
using System.Text;
99
using System.Text.RegularExpressions;
1010

11-
namespace ServerCodeExcision
11+
namespace ServerCodeExciser
1212
{
1313
public class ServerCodeExcisionParameters
1414
{
@@ -22,7 +22,7 @@ public class ServerCodeExcisionParameters
2222
public bool StrictMode { get; set; } = false;
2323
public bool UseFunctionStats { get; set; } = false;
2424
public bool DontSkip { get; set; } = false;
25-
public float RequiredExcisionRatio { get; set; } = -1.0f;
25+
public float RequiredExcisionRatio { get; set; } = 0.0f;
2626
public EExcisionLanguage ExcisionLanguage { get; set; } = EExcisionLanguage.Unknown;
2727
}
2828

@@ -37,7 +37,7 @@ public ServerCodeExcisionProcessor(ServerCodeExcisionParameters parameters)
3737
_parameters = parameters;
3838

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

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

6262
public EExciserReturnValues ExciseServerCode(string filePattern, IServerCodeExcisionLanguage excisionLanguage)
6363
{
64-
var startTime = DateTime.UtcNow;
64+
var globalStopwatch = Stopwatch.StartNew();
6565
var globalStats = new ExcisionStats();
6666

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

108108
try
109109
{
110+
var stopwatch = Stopwatch.StartNew();
110111
var stats = ProcessCodeFile(fileName, inputPath, excisionMode, excisionLanguage);
112+
stopwatch.Stop();
113+
111114
if (stats.CharactersExcised > 0)
112115
{
113116
System.Diagnostics.Debug.Assert(stats.TotalNrCharacters > 0, "Something is terribly wrong. We have excised characters, but no total characters..?");
114117
var excisionRatio = (float)stats.CharactersExcised / (float)stats.TotalNrCharacters * 100.0f;
115-
Console.WriteLine("Excised {0:0.00}% of server only code in file ({1}/{2}): {3}", excisionRatio, fileIdx + 1, allFiles.Length, fileName);
118+
Console.WriteLine($"[{fileIdx + 1}/{allFiles.Length}] Excised {excisionRatio:0.00}% of server only code in file (took {stopwatch.Elapsed.TotalMilliseconds:0.0}ms): {fileName}");
116119
}
117120
else
118121
{
119-
Console.WriteLine("No server only code found in file ({0}/{1}): {2}", fileIdx + 1, allFiles.Length, fileName);
122+
Console.WriteLine($"[{fileIdx + 1}/{allFiles.Length}] No server only code found in file: {fileName}");
120123
}
121124

122125
globalStats.CharactersExcised += stats.CharactersExcised;
123126
globalStats.TotalNrCharacters += stats.TotalNrCharacters;
124127
}
125128
catch (Exception)
126129
{
127-
Console.WriteLine("Failed to parse ({0}/{1}): {2}", fileIdx + 1, allFiles.Length, fileName);
130+
Console.Error.WriteLine($"[{fileIdx + 1}/{allFiles.Length}] Failed to parse: {fileName}");
128131
throw;
129132
}
130133
}
131134
}
132135

133-
var endTime = DateTime.UtcNow;
136+
globalStopwatch.Stop();
134137

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

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

163165
if (_parameters.RequiredExcisionRatio > 0.0f && totalExcisionRatio < _parameters.RequiredExcisionRatio)
164166
{

ServerCodeExciserTest/ExcisionIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ServerCodeExcision;
1+
using ServerCodeExciser;
22
using ServerCodeExcisionCommon;
33
using Spectre.Console;
44
using System;

0 commit comments

Comments
 (0)