Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
31 changes: 31 additions & 0 deletions ServerCodeExciser/InjectionTable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;

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

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

public IEnumerable<string> Get(int line)
{
if (m_table.TryGetValue(line, out var list))
{
return list;
}
return Array.Empty<string>();
}
}
}
72 changes: 72 additions & 0 deletions ServerCodeExciser/ScopeStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
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>();

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 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))
{
name = name.Trim('!');
m_scope.Push(name);
m_text.AppendLine($"#else // {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