Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
92 commits
Select commit Hold shift + click to select a range
6ab89ae
Add generic components
txcslm Oct 29, 2025
3722018
Try to fix generic systems inject code gen
txcslm Oct 30, 2025
775d92c
Revert "Try to fix generic systems inject code gen"
txcslm Oct 30, 2025
43438ba
Add new codegen
txcslm Nov 1, 2025
2dd0884
Merge branch 'main' into feature/CodogenGenericComponents
txcslm Nov 1, 2025
d223f37
Upd
txcslm Nov 1, 2025
e4488a6
add cache
txcslm Nov 1, 2025
44badd7
Add generic components
txcslm Oct 29, 2025
7e3a985
Try to fix generic systems inject code gen
txcslm Oct 30, 2025
67d5a8a
Revert "Try to fix generic systems inject code gen"
txcslm Oct 30, 2025
56c5fea
Add new codegen
txcslm Nov 1, 2025
3c9ba73
Upd
txcslm Nov 1, 2025
8dd9fe1
add cache
txcslm Nov 1, 2025
110017b
Merge branch 'feature/CodogenGenericComponents' of https://github.com…
txcslm Nov 1, 2025
67ccde0
fix KeyNotFoundException in graph compile
txcslm Nov 2, 2025
0fb66db
add tests
txcslm Nov 2, 2025
78e8b54
add IsGenericTypeDefinition check
txcslm Nov 2, 2025
611415e
Add generic components
txcslm Oct 29, 2025
f97d620
Try to fix generic systems inject code gen
txcslm Oct 30, 2025
c8c5e48
Revert "Try to fix generic systems inject code gen"
txcslm Oct 30, 2025
52d4fd6
Add new codegen
txcslm Nov 1, 2025
fe0e135
Upd
txcslm Nov 1, 2025
f849289
add cache
txcslm Nov 1, 2025
901664e
Add generic components
txcslm Oct 29, 2025
23614d6
Add new codegen
txcslm Nov 1, 2025
866f406
fix KeyNotFoundException in graph compile
txcslm Nov 2, 2025
876973d
add tests
txcslm Nov 2, 2025
b686424
add IsGenericTypeDefinition check
txcslm Nov 2, 2025
ecd6b32
Merge branch 'feature/CodogenGenericComponents' of https://github.com…
txcslm Nov 2, 2025
19bfc99
Add generic components
txcslm Oct 29, 2025
16fdd19
Try to fix generic systems inject code gen
txcslm Oct 30, 2025
5c93120
Revert "Try to fix generic systems inject code gen"
txcslm Oct 30, 2025
469ace4
Add new codegen
txcslm Nov 1, 2025
980db96
Upd
txcslm Nov 1, 2025
ed8ebc6
add cache
txcslm Nov 1, 2025
f1dfb66
Add generic components
txcslm Oct 29, 2025
6720bba
Add new codegen
txcslm Nov 1, 2025
37cd07a
fix KeyNotFoundException in graph compile
txcslm Nov 2, 2025
6f7f801
add tests
txcslm Nov 2, 2025
15c910d
add IsGenericTypeDefinition check
txcslm Nov 2, 2025
81451e8
Merge branch 'feature/CodogenGenericComponents' of https://github.com…
txcslm Nov 2, 2025
176307d
Merge branch 'main' into feature/CodogenGenericComponents
txcslm Nov 3, 2025
ee06d6d
fix error after merge
txcslm Nov 3, 2025
37b560c
Fix aot generating
txcslm Nov 4, 2025
26c316a
fix validate
txcslm Nov 4, 2025
fbe1c80
Add more cache
txcslm Nov 4, 2025
846fce4
Add SetDefaultValue for generic components
txcslm Nov 6, 2025
fb6beb7
Add generic components
txcslm Oct 29, 2025
4714724
Try to fix generic systems inject code gen
txcslm Oct 30, 2025
3e189ce
Revert "Try to fix generic systems inject code gen"
txcslm Oct 30, 2025
fc565fc
Add new codegen
txcslm Nov 1, 2025
3307522
Upd
txcslm Nov 1, 2025
528acf2
add cache
txcslm Nov 1, 2025
3024e30
Add generic components
txcslm Oct 29, 2025
2e00179
Add new codegen
txcslm Nov 1, 2025
2582c13
fix KeyNotFoundException in graph compile
txcslm Nov 2, 2025
4a92e46
add tests
txcslm Nov 2, 2025
f6ec287
add IsGenericTypeDefinition check
txcslm Nov 2, 2025
3f27c9a
Add generic components
txcslm Oct 29, 2025
52f221a
Add new codegen
txcslm Nov 1, 2025
97db821
Fix aot generating
txcslm Nov 4, 2025
34c22ff
fix validate
txcslm Nov 4, 2025
05da39f
Add more cache
txcslm Nov 4, 2025
67f2a2e
Add SetDefaultValue for generic components
txcslm Nov 6, 2025
9d50a31
Merge branch 'feature/CodogenGenericComponents' of https://github.com…
txcslm Nov 6, 2025
84501a8
Fix after merge
txcslm Nov 6, 2025
462a171
More fixes
txcslm Nov 6, 2025
4a0ffd7
Merge branch 'main' into feature/CodogenGenericComponents
txcslm Nov 7, 2025
c23120c
Merge branch 'main' into feature/CodogenGenericComponents
txcslm Nov 9, 2025
24e737b
Merge branch 'main' into feature/CodogenGenericComponents
txcslm Nov 10, 2025
5e20add
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 14, 2025
01fe4ec
fix after merge
txcslm Nov 14, 2025
100f86a
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 19, 2025
974ca9e
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 22, 2025
257e91a
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 23, 2025
a1ba29e
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 23, 2025
f02cf05
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 23, 2025
68e6de8
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 25, 2025
b0f5f5a
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 27, 2025
9fd9521
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Nov 27, 2025
2889f9f
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Dec 1, 2025
2b54c1d
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Dec 2, 2025
1579055
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Dec 2, 2025
c79f0bb
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Dec 7, 2025
cceb9cb
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Dec 9, 2025
c0a716f
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Dec 13, 2025
1cf1897
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Dec 20, 2025
9823f1a
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Jan 9, 2026
1e7dbfd
Fix null ref
txcslm Jan 16, 2026
0de3cc8
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Feb 20, 2026
b9bfb4d
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Feb 26, 2026
3a726a3
Merge remote-tracking branch 'upstream/main' into feature/CodogenGene…
txcslm Apr 3, 2026
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
65 changes: 53 additions & 12 deletions Addons/Features/Editor/SystemsCodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ namespace ME.BECS.Editor.Systems {

public class SystemsCodeGenerator : CustomCodeGenerator {

private static string[] _cachedGraphGuids;
private static System.DateTime _graphsCacheTime;

private static string[] GetCachedGraphGuids() {
var now = System.DateTime.Now;
if (_cachedGraphGuids == null || (now - _graphsCacheTime).TotalSeconds > 5) {
_cachedGraphGuids = UnityEditor.AssetDatabase.FindAssets("t:SystemsGraph");
_graphsCacheTime = now;
}
return _cachedGraphGuids;
}

private void AddMethod<T>(SystemsGraph graph, string baseName, string methodName, Method method, out scg::List<string> content, out scg::List<string> innerMethods) where T : class {
//var name = System.Text.RegularExpressions.Regex.Replace(graph.name, @"(\s+|@|&|'|\(|\)|<|>|#|-)", "_");
content = new scg::List<string>();
Expand Down Expand Up @@ -48,7 +60,7 @@ public string AddContent(scg::List<FileContent> filesContent) {

var content = new scg::List<string>();
if (this.editorAssembly == false) {
var graphs = UnityEditor.AssetDatabase.FindAssets("t:SystemsGraph");
var graphs = GetCachedGraphGuids();
if (filesContent != null) {
foreach (var guid in graphs) {
var path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
Expand Down Expand Up @@ -164,6 +176,7 @@ public string AddContent(scg::List<FileContent> filesContent) {
content.Add("}");
content.Add("[UnityEngine.Scripting.PreserveAttribute]");
content.Add("public static void SystemsLoad() {");
graphs = GetCachedGraphGuids();
foreach (var guid in graphs) {
var path = UnityEditor.AssetDatabase.GUIDToAssetPath(guid);
var graph = UnityEditor.AssetDatabase.LoadAssetAtPath<SystemsGraph>(path);
Expand Down Expand Up @@ -245,12 +258,18 @@ public string GetArgString() {
}

public string GetWriteOpString(string key) {
var index = this.keyToIndex[key];
if (this.keyToIndex.TryGetValue(key, out var index) == false) {
this.Add(key);
index = this.keyToIndex[key];
}
return $"dependencies[{index}] = {key};";
}

public string GetReadOpString(string key) {
var index = this.keyToIndex[key];
if (this.keyToIndex.TryGetValue(key, out var index) == false) {
this.Add(key);
index = this.keyToIndex[key];
}
return $"dependencies[{index}]";
}

Expand Down Expand Up @@ -539,6 +558,7 @@ n is ME.BECS.Extensions.GraphProcessor.RelayNode ||
// Parallel mode
var srcDep = index;
methodContent.Add($"var depsGeneric{srcDep.ToString()} = new NativeArray<Unity.Jobs.JobHandle>({types.Length}, Constants.ALLOCATOR_TEMP);");
collectedDeps.Add($"dep{srcDep.ToString()}");
var withoutSync = true;
foreach (var cType in types) {
var type = systemType.MakeGenericType(cType);
Expand All @@ -548,7 +568,6 @@ n is ME.BECS.Extensions.GraphProcessor.RelayNode ||
methodContent.Add($"(({EditorUtils.GetTypeName(type)}*)systems[{(index.globalIndex + index.genericIndex)}])->{method}(ref systemContext);");
methodContent.Add($"depsGeneric{srcDep.ToString()}[{index.genericIndex}] = systemContext.dependsOn;");
methodContent.Add("}");
if (index.genericIndex == 0) collectedDeps.Add($"dep{indexStr}");
printedDependencies.Add($"dep{indexStr}");
if (withoutSync == true) {
var syncState = IsSyncNotRequired(type);
Expand All @@ -572,6 +591,7 @@ n is ME.BECS.Extensions.GraphProcessor.RelayNode ||
var type = systemType.MakeGenericType(cType);
methodContent.Add($"systemContext = SystemContext.Create(dt, in world, localDependsOn);");
methodContent.Add($"(({EditorUtils.GetTypeName(type)}*)systems[{(index.globalIndex + index.genericIndex)}])->{method}(ref systemContext);");
collectedDeps.Add($"dep{indexStr}");
var withoutSync = IsSyncNotRequired(type);
AddApply(systemNode, index, ref schemeDependsOn, "systemContext.dependsOn", "localDependsOn", forceWithoutSync: withoutSync);
index.AddGeneric();
Expand Down Expand Up @@ -914,8 +934,17 @@ private static int GetNodeIndex(SystemsGraph graph, ME.BECS.Extensions.GraphProc
return index;
}

public static int InitializeGraph(CustomCodeGenerator generator, scg::Dictionary<System.Type, string> systemTypeToVar, scg::List<string> content, SystemsGraph graph, int rootGraphId, int index) {
for (int idx = 0; idx < graph.nodes.Count; ++idx) {
public static int InitializeGraph(
CustomCodeGenerator generator,
scg::Dictionary<System.Type, string> systemTypeToVar,
scg::List<string> content,
SystemsGraph graph,
int rootGraphId,
int index
)
{
for (int idx = 0; idx < graph.nodes.Count; ++idx)
{
var node = graph.nodes[idx];
if (node is ME.BECS.FeaturesGraph.Nodes.SystemNode systemNode) {
var system = systemNode.system;
Expand All @@ -942,7 +971,9 @@ public static int InitializeGraph(CustomCodeGenerator generator, scg::Dictionary
++index;
}
}
} else {
}
else
{
var systemTypeStr = EditorUtils.GetTypeName(systemType);
content.Add("{");
content.Add($"var item = allocator.Allocate(TSize<{systemTypeStr}>.sizeInt, TAlign<{systemTypeStr}>.alignInt);");
Expand All @@ -955,8 +986,17 @@ public static int InitializeGraph(CustomCodeGenerator generator, scg::Dictionary
++index;
}
}
} else if (node is ME.BECS.FeaturesGraph.Nodes.GraphNode graphNode) {
index = InitializeGraph(generator, systemTypeToVar, content, graphNode.graphValue, rootGraphId, index);
}
else if (node is ME.BECS.FeaturesGraph.Nodes.GraphNode graphNode)
{
index = InitializeGraph(
generator,
systemTypeToVar,
content,
graphNode.graphValue,
rootGraphId,
index
);
}
}

Expand Down Expand Up @@ -1050,7 +1090,7 @@ public static void InjectDependencies(SystemsGraph graph, scg::List<string> cont
containsBool = true;
}
if (typeof(IInject).IsAssignableFrom(field.FieldType) == true) {
var jobTypeStr = EditorUtils.GetTypeName(jobType);
var jobTypeStr = EditorUtils.GetJobTypeName(jobType);
localContent.Add($"JobInject<{jobTypeStr}>.Init();");
break;
}
Expand All @@ -1066,7 +1106,7 @@ public static void InjectDependencies(SystemsGraph graph, scg::List<string> cont
foreach (var jobType in types) {
if (jobType.IsVisible == false) continue;
var fields = jobType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
var jobTypeStr = EditorUtils.GetTypeName(jobType);
var jobTypeStr = EditorUtils.GetJobTypeName(jobType);
var containsBool = false;
localContent.Clear();
foreach (var field in fields) {
Expand Down Expand Up @@ -1110,7 +1150,8 @@ private static void CollectJobsTypes(MethodInfo root, scg::HashSet<System.Type>
var visited = new scg::HashSet<MethodPointerData>();
while (q.Count > 0) {
var body = q.Dequeue();
var instructions = body.GetInstructions();
var instructions = CodeGenerator.GetCachedInstructions(body);
if (instructions == null) continue;
foreach (var inst in instructions) {
if (inst.Operand is MethodInfo methodInfo) {
if ((methodInfo.Name == "Schedule" || methodInfo.Name == "ScheduleSingleWithInject" || methodInfo.Name == "ScheduleSingleWithInjectByRef") && methodInfo.IsGenericMethod == true) {
Expand Down
Loading