Skip to content

Commit 83a5cb2

Browse files
committed
Stuff for template pregenerated
1 parent 26733cf commit 83a5cb2

15 files changed

Lines changed: 104 additions & 26 deletions

File tree

Common/Models/FunctionSymbolModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
namespace Amethyst.Common.Models
44
{
5-
public class FunctionSymbolModel
6-
{
5+
public class FunctionSymbolModel : INamedSymbol {
76
[JsonProperty("name")]
87
public string Name { get; set; } = string.Empty;
98

Common/Models/INamedSymbol.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Amethyst.Common.Models {
8+
public interface INamedSymbol {
9+
string Name { get; set; }
10+
}
11+
}

Common/Models/VariableSymbolModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
namespace Amethyst.Common.Models
99
{
10-
public class VariableSymbolModel
11-
{
10+
public class VariableSymbolModel : INamedSymbol {
1211
[JsonProperty("name")]
1312
public string Name { get; set; } = string.Empty;
1413

Common/Models/VirtualFunctionSymbolModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
namespace Amethyst.Common.Models
99
{
10-
public class VirtualFunctionSymbolModel
11-
{
10+
public class VirtualFunctionSymbolModel : INamedSymbol {
1211
[JsonProperty("name")]
1312
public string Name { get; set; } = string.Empty;
1413

Common/Models/VirtualTableSymbolModel.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
namespace Amethyst.Common.Models
99
{
10-
public class VirtualTableSymbolModel
11-
{
10+
public class VirtualTableSymbolModel : INamedSymbol {
1211
[JsonProperty("name")]
1312
public string Name { get; set; } = string.Empty;
1413

Common/Utility/Utils.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,40 @@ private static IEnumerable<T> TakeChunk<T>(this IEnumerator<T> enumerator, int c
9898
count++;
9999
} while (count < chunkSize && enumerator.MoveNext());
100100
}
101+
102+
public static int LevenshteinDistance(string a, string b) {
103+
int[,] dp = new int[a.Length + 1, b.Length + 1];
104+
for (int i = 0; i <= a.Length; i++)
105+
dp[i, 0] = i;
106+
for (int j = 0; j <= b.Length; j++)
107+
dp[0, j] = j;
108+
109+
for (int i = 1; i <= a.Length; i++)
110+
for (int j = 1; j <= b.Length; j++) {
111+
int cost = a[i - 1] == b[j - 1] ? 0 : 1;
112+
dp[i, j] = Math.Min(
113+
Math.Min(dp[i - 1, j] + 1, dp[i, j - 1] + 1),
114+
dp[i - 1, j - 1] + cost
115+
);
116+
}
117+
118+
return dp[a.Length, b.Length];
119+
}
120+
121+
public static bool CompareSymbolsWithThreshold(string symbolA, string symbolB, int charMatch = 3)
122+
{
123+
if (symbolA == symbolB)
124+
return true;
125+
if (symbolA.Length != symbolB.Length)
126+
return false;
127+
int misses = 0;
128+
for (int i = 0; i < symbolA.Length; i++) {
129+
if (symbolA[i] != symbolB[i])
130+
misses++;
131+
if (misses > charMatch)
132+
return false;
133+
}
134+
return true;
135+
}
101136
}
102137
}

LibraryGenerator/Commands/MainCommand.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class MainCommand : ICommand
1919
[CommandOption("output", 'o', Description = "Path to the output directory where the lib will be generated.", IsRequired = true)]
2020
public string OutputPath { get; set; } = null!;
2121

22-
[CommandOption("platform", 'p', Description = "Target platform for symbol generation (e.g., win-client, win-server).", IsRequired = false)]
22+
[CommandOption("platform", 'p', Description = "Target platform for symbol generation (e.g., win-client, win-server).")]
2323
public string Platform { get; set; } = "win-client";
2424

2525
[CommandOption("pregen-sym", Description = "Overrides the default pregenerated.symbols.json file folder.")]
@@ -80,7 +80,7 @@ public ValueTask ExecuteAsync(IConsole console)
8080
// Collect all symbol files and accumulate mangled names
8181
IEnumerable<FileInfo> symbolFiles = PlatformSymbolInput
8282
.EnumerateFiles("*.symbols.json", SearchOption.AllDirectories)
83-
.Where(f => Path.GetFileName(f.FullName) != "pregenerated.symbols.json");
83+
.Where(f => Path.GetFileName(f.FullName) != "pregenerated.symbols.json" && Path.GetFileName(f.FullName) != "template.pregenerated.symbols.json");
8484

8585
string pregeneratedPath = PregeneratedSymbolsPath is null ?
8686
Path.Combine(PlatformSymbolInput.FullName, "pregenerated.symbols.json") :
@@ -117,6 +117,9 @@ public ValueTask ExecuteAsync(IConsole console)
117117
}
118118
}
119119

120+
Directory.EnumerateFiles(PlatformOutput.FullName, "Minecraft.Windows.*.def").ToList().ForEach(File.Delete);
121+
Directory.EnumerateFiles(PlatformOutput.FullName, "Minecraft.Windows.*.lib").ToList().ForEach(File.Delete);
122+
120123
int index = 0;
121124
foreach (var chunk in allMangledNames.ChunkBy(65500))
122125
{
@@ -131,6 +134,7 @@ public ValueTask ExecuteAsync(IConsole console)
131134
var libProc = Lib.GenerateLib(defFilePath, libFilePath);
132135
libProc.WaitForExit();
133136
if (libProc.ExitCode != 0) {
137+
Logger.Fatal($"lib.exe failed on chunk {index}, output: {libProc.StandardError.ReadToEnd()}");
134138
return ValueTask.FromException(new Exception("Library generation aborted due to errors."));
135139
}
136140

LibraryGenerator/Tools/MSVC/Lib.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ public static Process GenerateLib(string defFile, string libPath)
7171
};
7272

7373
process.BeginErrorReadLine();
74-
process.ErrorDataReceived += (sender, data) =>
75-
{
76-
if (!string.IsNullOrEmpty(data.Data))
77-
Console.Error.WriteLine(data.Data);
78-
};
79-
8074
return process;
8175
}
8276
}

ModuleTweaker/Commands/MainCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ ulong ParseAddress(string? address)
9797
// Collect all symbol files and accumulate mangled names
9898
IEnumerable<FileInfo> symbolFiles = PlatformSymbolInput
9999
.EnumerateFiles("*.symbols.json", SearchOption.AllDirectories)
100-
.Where(f => Path.GetFileName(f.FullName) != "pregenerated.symbols.json");
100+
.Where(f => Path.GetFileName(f.FullName) != "pregenerated.symbols.json" && Path.GetFileName(f.FullName) != "template.pregenerated.symbols.json");
101101

102102
string pregeneratedPath = PregeneratedSymbolsPath is null ?
103103
Path.Combine(PlatformSymbolInput.FullName, "pregenerated.symbols.json") :

SymbolGenerator/Commands/MainCommand.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ public ValueTask ExecuteAsync(IConsole console)
6767
.Where(c => c.ChangeType == ChangeType.Deleted)];
6868

6969
List<RawAnnotation> annotations = [];
70-
Dictionary<string, List<object>> annotationsData = [];
70+
Dictionary<string, List<INamedSymbol>> annotationsData = [];
7171

72+
ASTMethod[] methods = [];
73+
ASTVariable[] variables = [];
74+
ASTClass[] classes = [];
75+
AbstractAnnotationTarget[] targets = [];
7276
if (addedOrModified.Length != 0)
7377
{
7478
// Prepare .cpp file for parsing
@@ -78,9 +82,6 @@ public ValueTask ExecuteAsync(IConsole console)
7882
return [.. Utils.CreateIncludeFile(generatedFile, Input.FullName, addedOrModified)];
7983
});
8084

81-
ASTMethod[] methods = [];
82-
ASTVariable[] variables = [];
83-
ASTClass[] classes = [];
8485
Utils.Benchmark("Parse the AST", () =>
8586
{
8687
// Parse the generated .cpp file
@@ -109,6 +110,7 @@ public ValueTask ExecuteAsync(IConsole console)
109110
methods = [.. visitor.Methods];
110111
variables = [.. visitor.Variables];
111112
classes = [.. visitor.Classes];
113+
targets = [.. methods, .. variables, .. classes];
112114
});
113115

114116
Utils.Benchmark("Parse the comments", () =>
@@ -159,7 +161,8 @@ public ValueTask ExecuteAsync(IConsole console)
159161

160162
if (!annotationsData.ContainsKey(location.File))
161163
annotationsData[location.File] = [];
162-
annotationsData[location.File].Add(processed.Data);
164+
// Kinda hacky, will change later
165+
annotationsData[location.File].Add((processed.Data as INamedSymbol)!);
163166

164167
if (processed.Data is VirtualTableSymbolModel vtable && processed.Target is ASTClass cls)
165168
{
@@ -196,6 +199,34 @@ public ValueTask ExecuteAsync(IConsole console)
196199
NullValueHandling = NullValueHandling.Ignore,
197200
};
198201

202+
string templatePredefined = Path.Combine(PlatformOutput.FullName, "symbols", "template.pregenerated.symbols.json");
203+
if (File.Exists(templatePredefined)) {
204+
string json = File.ReadAllText(templatePredefined);
205+
var predefinedSymbols = JsonConvert.DeserializeObject<SymbolJSONModel>(json, jsonSettings);
206+
if (predefinedSymbols is not null)
207+
{
208+
INamedSymbol[] namedSymbols = [
209+
..predefinedSymbols.Functions,
210+
..predefinedSymbols.Variables,
211+
..predefinedSymbols.VirtualTables,
212+
..predefinedSymbols.VirtualFunctions
213+
];
214+
215+
foreach (var symbol in namedSymbols)
216+
{
217+
if (targets.FirstOrDefault(t => Utils.CompareSymbolsWithThreshold(t.IdentificationName, symbol.Name)) is { } target) {
218+
if (!target.IsImported || annotationsData.SelectMany(kv => kv.Value).Any(v => Utils.CompareSymbolsWithThreshold(v.Name, symbol.Name)))
219+
continue;
220+
string file = target.Location?.File ?? "<unknown>";
221+
if (!annotationsData.ContainsKey(file))
222+
annotationsData[file] = [];
223+
symbol.Name = target.IdentificationName;
224+
annotationsData[file].Add(symbol);
225+
}
226+
}
227+
}
228+
}
229+
199230
// Generate output JSON files
200231
foreach (var (file, data) in annotationsData)
201232
{

0 commit comments

Comments
 (0)