Skip to content
Draft
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
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 4
tab_width = 4
trim_trailing_whitespace = true
insert_final_newline = true
charset=utf-8
66 changes: 66 additions & 0 deletions ServerCodeExciser/InjectionTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using SharpCompress.Common;
using System;
using System.Collections.Generic;

namespace ServerCodeExciser
{
public class Marker
{
public bool Start { get; }
public string OptElse { get; }
public string Context { get; }

public Marker(string context, bool start, string optElse = "")
{
Start = start;
OptElse = optElse;
Context = context;
}

public void Write(ScriptBuilder builder)
{
if (Start)
{
builder.AddLine($"#ifdef WITH_SERVER // {Context}");
}
else
{
if (builder.IsInScope("WITH_SERVER"))
{
var values = OptElse.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries);
foreach (var v in values)
{
builder.AddLine(v);
}
}
builder.AddLine($"#endif // WITH_SERVER {Context}");
}
}
}

public class InjectionTable
{
private readonly Dictionary<int, List<Marker>> m_table = new Dictionary<int, List<Marker>>();

public void Add(int line, Marker value)
{
if (m_table.TryGetValue(line, out var list))
{
list.Add(value);
}
else
{
m_table.Add(line, new List<Marker> { value });
}
}

public IEnumerable<Marker> Get(int line)
{
if (m_table.TryGetValue(line, out var list))
{
return list;
}
return Array.Empty<Marker>();
}
}
}
87 changes: 87 additions & 0 deletions ServerCodeExciser/PreprocessorTokenStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Microsoft.Build.Framework;
using System;
using static System.Formats.Asn1.AsnWriter;

namespace ServerCodeExciser
{
public class Preprocessor : ITokenSource
{
private readonly ITokenSource m_tokenSource;
private readonly ScopeStack m_scope = new ScopeStack();

public Preprocessor(ITokenSource tokenSource)
{
m_tokenSource = tokenSource;
}

public int Line
{
get { return m_tokenSource.Line; }
}

public int Column
{
get { return m_tokenSource.Column; }
}

public ICharStream InputStream
{
get { return m_tokenSource.InputStream; }
}

public string SourceName
{
get { return m_tokenSource.SourceName; }
}

public ITokenFactory TokenFactory
{
get { return m_tokenSource.TokenFactory; }
set { m_tokenSource.TokenFactory = value; }
}

public IToken NextToken()
{
var token = m_tokenSource.NextToken();
while (token.Type == UnrealAngelscriptLexer.Preprocessor || m_scope.IsInScope("WITH_SERVER"))
{
if (token.Type == UnrealAngelscriptLexer.Preprocessor)
{
Process(token.Text);
}
else
{
//Console.WriteLine($"skipping: " + token.Text);
}
token = m_tokenSource.NextToken();
}
return token;
}

private void Process(string line)
{
if (line.StartsWith("#ifdef "))
{
m_scope.Push(line);
}
else if (line.StartsWith("#if "))
{
m_scope.Push(line);
}
else if (line.StartsWith("#ifndef "))
{
m_scope.Push(line);
}
else if (line.StartsWith("#else"))
{
m_scope.Else(out var name);
}
else if (line.StartsWith("#endif"))
{
m_scope.Pop(out var name);
}
}
}
}
8 changes: 8 additions & 0 deletions ServerCodeExciser/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"ServerCodeExciser": {
"commandName": "Project",
"commandLineArgs": "D:\\p4\\task\\Games\\Shared\\Plugins\\AIScript\\Script -o D:\\out"
}
}
}
80 changes: 80 additions & 0 deletions ServerCodeExciser/ScopeStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;

namespace ServerCodeExciser
{
public class ScopeStack
{
private Dictionary<string, int> m_scopes = new Dictionary<string, int>();
private Stack<string> m_scope = new Stack<string>();
private int m_else = 0;

Check warning on line 10 in ServerCodeExciser/ScopeStack.cs

View workflow job for this annotation

GitHub Actions / build

The field 'ScopeStack.m_else' is assigned but its value is never used

public bool Push(string name)
{
if (name.StartsWith("#ifdef "))
{
name = TrimAndStripComments(name.Substring(7));
}
else if (name.StartsWith("#if "))
{
name = TrimAndStripComments(name.Substring(4));
}
else if (name.StartsWith("#ifndef "))
{
name = "!" + TrimAndStripComments(name.Substring(8));
}

m_scope.Push(name);

if (m_scopes.ContainsKey(name) && m_scopes[name] > 0)
{
m_scopes[name] += 1;
return false;
}
else
{
m_scopes[name] = 1;
return true;
}
}

public void Else(out string name)
{
name = m_scope.Pop();
m_scopes[name] -= 1;
var o = (name[0] == '!') ? name.Substring(1) : ("!" + name);
Push(o);
}

public bool Pop(out string name)
{
if (m_scope.Count <= 0)
{
name = string.Empty;
return false;
}
name = m_scope.Pop();
m_scopes[name] -= 1;
return m_scopes[name] == 0;
}

public bool IsInScope(string name)
{
if (m_scopes.TryGetValue(name, out var count))
{
return count > 0;
}
return false;
}

private string TrimAndStripComments(string text)
{
int idx = text.IndexOf("//");
if (idx >= 0)
{
return text.Substring(0, idx).Trim();
}
return text.Trim();
}
}
}
71 changes: 71 additions & 0 deletions ServerCodeExciser/ScriptBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Text;

namespace ServerCodeExciser
{
public class ScriptBuilder
{
private ScopeStack m_scope = new ScopeStack();
private StringBuilder m_text = new StringBuilder();

public void AddLine(string line)
{
var trimmedLine = line.Trim();
if (trimmedLine.StartsWith("#ifdef "))
{
if (m_scope.Push(trimmedLine))
{
m_text.AppendLine(trimmedLine);
}
}
else if (trimmedLine.StartsWith("#if "))
{
if (m_scope.Push(trimmedLine))
{
m_text.AppendLine(trimmedLine);
}
}
else if (trimmedLine.StartsWith("#ifndef "))
{
if (m_scope.Push(trimmedLine))
{
m_text.AppendLine(trimmedLine);
}
}
else if (trimmedLine.StartsWith("#else"))
{
if (m_scope.Pop(out var name))
{
m_text.AppendLine($"#else // {name}");
name = (name[0] == '!') ? name.Substring(1) : ("!" + name);
m_scope.Push(name);
}
else
{
m_text.AppendLine($"#else");
}
}
else if (trimmedLine.StartsWith("#endif"))
{
if (m_scope.Pop(out var name))
{
m_text.AppendLine($"#endif // {name}");
}
}
else
{
m_text.AppendLine(line);
}
}

public override string ToString()
{
return m_text.ToString();
}

public bool IsInScope(string name)
{
return m_scope.IsInScope(name);
}
}
}
12 changes: 6 additions & 6 deletions ServerCodeExciser/ServerCodeExciser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ServerCodeExcisionCommon;
using ServerCodeExcisionCommon;
using Spectre.Console;
using Spectre.Console.Cli;
using System;
Expand All @@ -9,7 +9,7 @@
using System.Text.Json.Serialization;
using UnrealAngelscriptServerCodeExcision;

namespace ServerCodeExcision
namespace ServerCodeExciser
{
internal sealed class ServerCodeExciserCommand : Command<ServerCodeExciserCommand.Settings>
{
Expand Down Expand Up @@ -75,7 +75,7 @@ public override ValidationResult Validate()
class RootPaths
{
[JsonPropertyName("AngelscriptScriptRoots")]
public string[] AngelscriptScriptRoots { get;set;} = Array.Empty<string>();
public string[] AngelscriptScriptRoots { get; set; } = Array.Empty<string>();
}

public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
Expand All @@ -99,7 +99,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
var desc = File.ReadAllText(settings.InputPath);
var paths = JsonSerializer.Deserialize<RootPaths>(desc);
if (paths != null)
{
{
parameters.InputPaths.UnionWith(paths.AngelscriptScriptRoots);
}
else
Expand All @@ -108,7 +108,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 @@ -143,7 +143,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
}


public class ServerCodeExciser
public class ServerCodeExciserProgram
{
public static int Main(string[] args)
{
Expand Down
Loading
Loading