Skip to content

Commit b481e06

Browse files
committed
Add a way to lint and generate #ifdefs around UEventAPI calls.
1 parent 7722e8f commit b481e06

17 files changed

+1444
-1266
lines changed

.editorconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
tab_width = 4
7+
trim_trailing_whitespace = true
8+
insert_final_newline = true
9+
charset=utf-8
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ServerCodeExciser
5+
{
6+
public class InjectionTable
7+
{
8+
private readonly Dictionary<int, List<string>> m_table = new Dictionary<int, List<string>>();
9+
10+
public void Add(int line, string value)
11+
{
12+
if (m_table.TryGetValue(line, out var list))
13+
{
14+
list.Add(value);
15+
}
16+
else
17+
{
18+
m_table.Add(line, new List<string> { value });
19+
}
20+
}
21+
22+
public IEnumerable<string> Get(int line)
23+
{
24+
if (m_table.TryGetValue(line, out var list))
25+
{
26+
return list;
27+
}
28+
return Array.Empty<string>();
29+
}
30+
}
31+
}

ServerCodeExciser/ScopeStack.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace ServerCodeExciser
5+
{
6+
public class ScopeStack
7+
{
8+
private Dictionary<string, int> m_scopes = new Dictionary<string, int>();
9+
private Stack<string> m_scope = new Stack<string>();
10+
11+
public bool Push(string name)
12+
{
13+
if (name.StartsWith("#ifdef "))
14+
{
15+
name = TrimAndStripComments(name.Substring(7));
16+
}
17+
else if (name.StartsWith("#if "))
18+
{
19+
name = TrimAndStripComments(name.Substring(4));
20+
}
21+
else if (name.StartsWith("#ifndef "))
22+
{
23+
name = "!" + TrimAndStripComments(name.Substring(8));
24+
}
25+
26+
m_scope.Push(name);
27+
28+
if (m_scopes.ContainsKey(name) && m_scopes[name] > 0)
29+
{
30+
m_scopes[name] += 1;
31+
return false;
32+
}
33+
else
34+
{
35+
m_scopes[name] = 1;
36+
return true;
37+
}
38+
}
39+
40+
public bool Pop(out string name)
41+
{
42+
if (m_scope.Count <= 0)
43+
{
44+
name = string.Empty;
45+
return false;
46+
}
47+
48+
name = m_scope.Pop();
49+
m_scopes[name] -= 1;
50+
return m_scopes[name] == 0;
51+
}
52+
53+
public bool IsInScope(string name)
54+
{
55+
if (m_scopes.TryGetValue(name, out var count))
56+
{
57+
return count > 0;
58+
}
59+
return false;
60+
}
61+
62+
private string TrimAndStripComments(string text)
63+
{
64+
int idx = text.IndexOf("//");
65+
if (idx >= 0)
66+
{
67+
return text.Substring(0, idx).Trim();
68+
}
69+
return text.Trim();
70+
}
71+
}
72+
}

ServerCodeExciser/ScriptBuilder.cs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Text;
3+
4+
namespace ServerCodeExciser
5+
{
6+
public class ScriptBuilder
7+
{
8+
private ScopeStack m_scope = new ScopeStack();
9+
private StringBuilder m_text = new StringBuilder();
10+
11+
public void AddLine(string line)
12+
{
13+
var trimmedLine = line.Trim();
14+
if (trimmedLine.StartsWith("#ifdef "))
15+
{
16+
if (m_scope.Push(trimmedLine))
17+
{
18+
m_text.AppendLine(trimmedLine);
19+
}
20+
}
21+
else if (trimmedLine.StartsWith("#if "))
22+
{
23+
if (m_scope.Push(trimmedLine))
24+
{
25+
m_text.AppendLine(trimmedLine);
26+
}
27+
}
28+
else if (trimmedLine.StartsWith("#ifndef "))
29+
{
30+
if (m_scope.Push(trimmedLine))
31+
{
32+
m_text.AppendLine(trimmedLine);
33+
}
34+
}
35+
else if (trimmedLine.StartsWith("#else"))
36+
{
37+
if (m_scope.Pop(out var name))
38+
{
39+
name = name.Trim('!');
40+
m_scope.Push(name);
41+
m_text.AppendLine($"#else // {name}");
42+
}
43+
else
44+
{
45+
m_text.AppendLine($"#else");
46+
}
47+
}
48+
else if (trimmedLine.StartsWith("#endif"))
49+
{
50+
if (m_scope.Pop(out var name))
51+
{
52+
m_text.AppendLine($"#endif // {name}");
53+
}
54+
}
55+
else
56+
{
57+
m_text.AppendLine(line);
58+
}
59+
}
60+
61+
public override string ToString()
62+
{
63+
return m_text.ToString();
64+
}
65+
66+
public bool IsInScope(string name)
67+
{
68+
return m_scope.IsInScope(name);
69+
}
70+
}
71+
}

ServerCodeExciser/ServerCodeExciser.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using ServerCodeExcisionCommon;
1+
using ServerCodeExcisionCommon;
22
using Spectre.Console;
33
using Spectre.Console.Cli;
44
using System;
@@ -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
{
@@ -75,7 +75,7 @@ public override ValidationResult Validate()
7575
class RootPaths
7676
{
7777
[JsonPropertyName("AngelscriptScriptRoots")]
78-
public string[] AngelscriptScriptRoots { get;set;} = Array.Empty<string>();
78+
public string[] AngelscriptScriptRoots { get; set; } = Array.Empty<string>();
7979
}
8080

8181
public override int Execute([NotNull] CommandContext context, [NotNull] Settings settings)
@@ -99,7 +99,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
9999
var desc = File.ReadAllText(settings.InputPath);
100100
var paths = JsonSerializer.Deserialize<RootPaths>(desc);
101101
if (paths != null)
102-
{
102+
{
103103
parameters.InputPaths.UnionWith(paths.AngelscriptScriptRoots);
104104
}
105105
else
@@ -108,7 +108,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
108108
return (int)EExciserReturnValues.InternalExcisionError;
109109
}
110110
}
111-
else if(Directory.Exists(settings.InputPath))
111+
else if (Directory.Exists(settings.InputPath))
112112
{
113113
parameters.InputPaths.Add(settings.InputPath);
114114
}
@@ -143,7 +143,7 @@ public override int Execute([NotNull] CommandContext context, [NotNull] Settings
143143
}
144144

145145

146-
public class ServerCodeExciser
146+
public class ServerCodeExciserProgram
147147
{
148148
public static int Main(string[] args)
149149
{

0 commit comments

Comments
 (0)