Skip to content

Commit f37f8c7

Browse files
authored
Merge pull request #60 from AkiKurisu/dev/v0.6.8
merge dev v0.6.8
2 parents 99f6994 + 12aedf9 commit f37f8c7

15 files changed

Lines changed: 516 additions & 322 deletions

Runtime/Core/Models/Graph/CeresGraphModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Ceres.Graph
77
{
88
internal class CeresGraphModule: RuntimeModule
99
{
10-
public override void Initialize(ModuleConfig config)
10+
public override void Initialize()
1111
{
1212
// Set global log level
1313
CeresLogger.LogLevel = CeresConfig.Get().logLevel;

Runtime/Core/Models/Graph/Variables/SharedVariableExtension.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using UnityEngine;
2+
23
namespace Ceres.Graph
34
{
45
public static class SharedVariableExtension
@@ -34,7 +35,7 @@ public static SharedVariable GetSharedVariable(this IVariableSource variableScop
3435
/// <returns></returns>
3536
public static SharedVariable<T> GetSharedVariable<T>(this IVariableSource variableScope, string variableName)
3637
{
37-
return GetSharedVariable(variableScope, variableName) as SharedVariable<T>;
38+
return variableScope.GetSharedVariable(variableName) as SharedVariable<T>;
3839
}
3940

4041
/// <summary>

Runtime/Core/Models/UObjectLink.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Linq;
33
using Newtonsoft.Json.Linq;
44
using UObject = UnityEngine.Object;
5+
56
namespace Ceres
67
{
78
[Serializable]
@@ -60,7 +61,11 @@ public static void Parse(ref UObjectLink[] uobjectLinks, string serializedData)
6061
{
6162
if (prop.Name != "instanceID") continue;
6263
var instanceId = (int)prop.Value;
64+
#if UNITY_6000_3_OR_NEWER
65+
var uObject = UnityEditor.EditorUtility.EntityIdToObject(instanceId);
66+
#else
6367
var uObject = UnityEditor.EditorUtility.InstanceIDToObject(instanceId);
68+
#endif
6469
if (uObject)
6570
{
6671
Chris.Collections.ArrayUtils.Add(ref uobjectLinks, new UObjectLink(uObject));

Runtime/Flow/Annotations/ExecutableEventAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Ceres.Graph.Flow.Annotations
66
/// Allow this event can be implemented in flow graph
77
/// </summary>
88
/// <remarks>Annotate event custom create method to let source generator generate wrapper node for creating event</remarks>
9-
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
9+
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
1010
public class ExecutableEventAttribute: Attribute
1111
{
1212

Runtime/SourceGenerators/Ceres.SourceGenerator.deps.json

Lines changed: 0 additions & 148 deletions
Original file line numberDiff line numberDiff line change
@@ -8,66 +8,9 @@
88
".NETStandard,Version=v2.0": {},
99
".NETStandard,Version=v2.0/": {
1010
"Ceres.SourceGenerator/1.0.0": {
11-
"dependencies": {
12-
"Microsoft.CodeAnalysis.CSharp": "4.1.0",
13-
"NETStandard.Library": "2.0.3"
14-
},
1511
"runtime": {
1612
"Ceres.SourceGenerator.dll": {}
1713
}
18-
},
19-
"Microsoft.CodeAnalysis.Analyzers/3.3.3": {},
20-
"Microsoft.CodeAnalysis.Common/4.1.0": {
21-
"dependencies": {
22-
"Microsoft.CodeAnalysis.Analyzers": "3.3.3",
23-
"System.Collections.Immutable": "5.0.0",
24-
"System.Memory": "4.5.4",
25-
"System.Reflection.Metadata": "5.0.0",
26-
"System.Runtime.CompilerServices.Unsafe": "5.0.0",
27-
"System.Text.Encoding.CodePages": "4.5.1",
28-
"System.Threading.Tasks.Extensions": "4.5.4"
29-
}
30-
},
31-
"Microsoft.CodeAnalysis.CSharp/4.1.0": {
32-
"dependencies": {
33-
"Microsoft.CodeAnalysis.Common": "4.1.0"
34-
}
35-
},
36-
"Microsoft.NETCore.Platforms/1.1.0": {},
37-
"NETStandard.Library/2.0.3": {
38-
"dependencies": {
39-
"Microsoft.NETCore.Platforms": "1.1.0"
40-
}
41-
},
42-
"System.Buffers/4.5.1": {},
43-
"System.Collections.Immutable/5.0.0": {
44-
"dependencies": {
45-
"System.Memory": "4.5.4"
46-
}
47-
},
48-
"System.Memory/4.5.4": {
49-
"dependencies": {
50-
"System.Buffers": "4.5.1",
51-
"System.Numerics.Vectors": "4.4.0",
52-
"System.Runtime.CompilerServices.Unsafe": "5.0.0"
53-
}
54-
},
55-
"System.Numerics.Vectors/4.4.0": {},
56-
"System.Reflection.Metadata/5.0.0": {
57-
"dependencies": {
58-
"System.Collections.Immutable": "5.0.0"
59-
}
60-
},
61-
"System.Runtime.CompilerServices.Unsafe/5.0.0": {},
62-
"System.Text.Encoding.CodePages/4.5.1": {
63-
"dependencies": {
64-
"System.Runtime.CompilerServices.Unsafe": "5.0.0"
65-
}
66-
},
67-
"System.Threading.Tasks.Extensions/4.5.4": {
68-
"dependencies": {
69-
"System.Runtime.CompilerServices.Unsafe": "5.0.0"
70-
}
7114
}
7215
}
7316
},
@@ -76,97 +19,6 @@
7619
"type": "project",
7720
"serviceable": false,
7821
"sha512": ""
79-
},
80-
"Microsoft.CodeAnalysis.Analyzers/3.3.3": {
81-
"type": "package",
82-
"serviceable": true,
83-
"sha512": "sha512-j/rOZtLMVJjrfLRlAMckJLPW/1rze9MT1yfWqSIbUPGRu1m1P0fuo9PmqapwsmePfGB5PJrudQLvmUOAMF0DqQ==",
84-
"path": "microsoft.codeanalysis.analyzers/3.3.3",
85-
"hashPath": "microsoft.codeanalysis.analyzers.3.3.3.nupkg.sha512"
86-
},
87-
"Microsoft.CodeAnalysis.Common/4.1.0": {
88-
"type": "package",
89-
"serviceable": true,
90-
"sha512": "sha512-bNzTyxP3iD5FPFHfVDl15Y6/wSoI7e3MeV0lOaj9igbIKTjgrmuw6LoVJ06jUNFA7+KaDC/OIsStWl/FQJz6sQ==",
91-
"path": "microsoft.codeanalysis.common/4.1.0",
92-
"hashPath": "microsoft.codeanalysis.common.4.1.0.nupkg.sha512"
93-
},
94-
"Microsoft.CodeAnalysis.CSharp/4.1.0": {
95-
"type": "package",
96-
"serviceable": true,
97-
"sha512": "sha512-sbu6kDGzo9bfQxuqWpeEE7I9P30bSuZEnpDz9/qz20OU6pm79Z63+/BsAzO2e/R/Q97kBrpj647wokZnEVr97w==",
98-
"path": "microsoft.codeanalysis.csharp/4.1.0",
99-
"hashPath": "microsoft.codeanalysis.csharp.4.1.0.nupkg.sha512"
100-
},
101-
"Microsoft.NETCore.Platforms/1.1.0": {
102-
"type": "package",
103-
"serviceable": true,
104-
"sha512": "sha512-kz0PEW2lhqygehI/d6XsPCQzD7ff7gUJaVGPVETX611eadGsA3A877GdSlU0LRVMCTH/+P3o2iDTak+S08V2+A==",
105-
"path": "microsoft.netcore.platforms/1.1.0",
106-
"hashPath": "microsoft.netcore.platforms.1.1.0.nupkg.sha512"
107-
},
108-
"NETStandard.Library/2.0.3": {
109-
"type": "package",
110-
"serviceable": true,
111-
"sha512": "sha512-st47PosZSHrjECdjeIzZQbzivYBJFv6P2nv4cj2ypdI204DO+vZ7l5raGMiX4eXMJ53RfOIg+/s4DHVZ54Nu2A==",
112-
"path": "netstandard.library/2.0.3",
113-
"hashPath": "netstandard.library.2.0.3.nupkg.sha512"
114-
},
115-
"System.Buffers/4.5.1": {
116-
"type": "package",
117-
"serviceable": true,
118-
"sha512": "sha512-Rw7ijyl1qqRS0YQD/WycNst8hUUMgrMH4FCn1nNm27M4VxchZ1js3fVjQaANHO5f3sN4isvP4a+Met9Y4YomAg==",
119-
"path": "system.buffers/4.5.1",
120-
"hashPath": "system.buffers.4.5.1.nupkg.sha512"
121-
},
122-
"System.Collections.Immutable/5.0.0": {
123-
"type": "package",
124-
"serviceable": true,
125-
"sha512": "sha512-FXkLXiK0sVVewcso0imKQoOxjoPAj42R8HtjjbSjVPAzwDfzoyoznWxgA3c38LDbN9SJux1xXoXYAhz98j7r2g==",
126-
"path": "system.collections.immutable/5.0.0",
127-
"hashPath": "system.collections.immutable.5.0.0.nupkg.sha512"
128-
},
129-
"System.Memory/4.5.4": {
130-
"type": "package",
131-
"serviceable": true,
132-
"sha512": "sha512-1MbJTHS1lZ4bS4FmsJjnuGJOu88ZzTT2rLvrhW7Ygic+pC0NWA+3hgAen0HRdsocuQXCkUTdFn9yHJJhsijDXw==",
133-
"path": "system.memory/4.5.4",
134-
"hashPath": "system.memory.4.5.4.nupkg.sha512"
135-
},
136-
"System.Numerics.Vectors/4.4.0": {
137-
"type": "package",
138-
"serviceable": true,
139-
"sha512": "sha512-UiLzLW+Lw6HLed1Hcg+8jSRttrbuXv7DANVj0DkL9g6EnnzbL75EB7EWsw5uRbhxd/4YdG8li5XizGWepmG3PQ==",
140-
"path": "system.numerics.vectors/4.4.0",
141-
"hashPath": "system.numerics.vectors.4.4.0.nupkg.sha512"
142-
},
143-
"System.Reflection.Metadata/5.0.0": {
144-
"type": "package",
145-
"serviceable": true,
146-
"sha512": "sha512-5NecZgXktdGg34rh1OenY1rFNDCI8xSjFr+Z4OU4cU06AQHUdRnIIEeWENu3Wl4YowbzkymAIMvi3WyK9U53pQ==",
147-
"path": "system.reflection.metadata/5.0.0",
148-
"hashPath": "system.reflection.metadata.5.0.0.nupkg.sha512"
149-
},
150-
"System.Runtime.CompilerServices.Unsafe/5.0.0": {
151-
"type": "package",
152-
"serviceable": true,
153-
"sha512": "sha512-ZD9TMpsmYJLrxbbmdvhwt9YEgG5WntEnZ/d1eH8JBX9LBp+Ju8BSBhUGbZMNVHHomWo2KVImJhTDl2hIgw/6MA==",
154-
"path": "system.runtime.compilerservices.unsafe/5.0.0",
155-
"hashPath": "system.runtime.compilerservices.unsafe.5.0.0.nupkg.sha512"
156-
},
157-
"System.Text.Encoding.CodePages/4.5.1": {
158-
"type": "package",
159-
"serviceable": true,
160-
"sha512": "sha512-4J2JQXbftjPMppIHJ7IC+VXQ9XfEagN92vZZNoG12i+zReYlim5dMoXFC1Zzg7tsnKDM7JPo5bYfFK4Jheq44w==",
161-
"path": "system.text.encoding.codepages/4.5.1",
162-
"hashPath": "system.text.encoding.codepages.4.5.1.nupkg.sha512"
163-
},
164-
"System.Threading.Tasks.Extensions/4.5.4": {
165-
"type": "package",
166-
"serviceable": true,
167-
"sha512": "sha512-zteT+G8xuGu6mS+mzDzYXbzS7rd3K6Fjb9RiZlYlJPam2/hU7JCBZBVEcywNuR+oZ1ncTvc/cq0faRr3P01OVg==",
168-
"path": "system.threading.tasks.extensions/4.5.4",
169-
"hashPath": "system.threading.tasks.extensions.4.5.4.nupkg.sha512"
17022
}
17123
}
17224
}
9.5 KB
Binary file not shown.
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
using Microsoft.CodeAnalysis;
2+
using Microsoft.CodeAnalysis.CSharp;
3+
using Microsoft.CodeAnalysis.CSharp.Syntax;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
7+
namespace Ceres.SourceGenerator;
8+
9+
[Generator]
10+
public class BreakStructGenerator : CeresSourceGenerator, ISourceGenerator
11+
{
12+
public void Initialize(GeneratorInitializationContext context)
13+
{
14+
context.RegisterForSyntaxNotifications(() => new BreakStructSyntaxReceiver());
15+
}
16+
17+
public void Execute(GeneratorExecutionContext context)
18+
{
19+
if (context.SyntaxReceiver is not BreakStructSyntaxReceiver receiver) return;
20+
21+
if (!ShouldRunGenerator(context))
22+
return;
23+
24+
Helpers.SetupContext(context);
25+
Debug.LogInfo($"[BreakStructGenerator] Execute assembly {context.Compilation.Assembly.Name}");
26+
27+
// If the attach_debugger key is present (but without value) the returned string is the empty string (not null)
28+
var debugAssembly = context.GetOptionsString(GlobalOptions.AttachDebugger);
29+
if (debugAssembly != null)
30+
{
31+
Debug.LaunchDebugger(context, debugAssembly);
32+
}
33+
34+
List<GeneratedFile> generatedFiles = [];
35+
36+
foreach (var structDeclaration in receiver.Candidates)
37+
{
38+
var structName = structDeclaration.Identifier.Text;
39+
Debug.LogInfo($"[BreakStructGenerator] Analyze {structName}");
40+
41+
var semanticModel = context.Compilation.GetSemanticModel(structDeclaration.SyntaxTree);
42+
var structSymbol = semanticModel.GetDeclaredSymbol(structDeclaration);
43+
if (structSymbol == null)
44+
{
45+
Debug.LogInfo($"[BreakStructGenerator] Can not get declared struct from {structName}");
46+
continue;
47+
}
48+
49+
var generateAttribute = structSymbol
50+
.GetAttributes()
51+
.FirstOrDefault(x =>
52+
x.AttributeClass?.ToDisplayString() == "Ceres.Graph.Flow.Annotations.ExecutableEventAttribute");
53+
if (generateAttribute == null)
54+
{
55+
Debug.LogInfo($"[BreakStructGenerator] Can not find ExecutableEventAttribute from {structName}");
56+
continue;
57+
}
58+
59+
if (structDeclaration.Parent is not NamespaceDeclarationSyntax namespaceNode)
60+
{
61+
Debug.LogInfo($"[BreakStructGenerator] Namespace was not declared in {structName}");
62+
continue;
63+
}
64+
var namespaceName = namespaceNode.Name.ToString();
65+
66+
// Generate Break node for struct
67+
if (receiver.PublicFields.TryGetValue(structDeclaration, out var publicFields))
68+
{
69+
BreakStructGeneratorTemplate generatorTemplate = new()
70+
{
71+
Namespace = namespaceName,
72+
StructName = structName,
73+
PublicFields = publicFields,
74+
Namespaces = receiver.Namespaces[structDeclaration]
75+
};
76+
var generatedCode = generatorTemplate.GenerateCode();
77+
generatedFiles.Add(new GeneratedFile
78+
{
79+
ClassName = $"FlowNode_Break_{structName}",
80+
Namespace = namespaceName,
81+
Code = generatedCode,
82+
GeneratedFileName = $"FlowNode_Break_{structName}.gen.cs"
83+
});
84+
}
85+
}
86+
87+
GenerateFiles(context, generatedFiles);
88+
}
89+
}
90+
91+
public class BreakStructSyntaxReceiver : ISyntaxReceiver
92+
{
93+
public readonly List<StructDeclarationSyntax> Candidates = [];
94+
95+
public readonly Dictionary<StructDeclarationSyntax, List<GeneratorParameterInfo>> PublicFields = new();
96+
97+
public readonly Dictionary<StructDeclarationSyntax, HashSet<string>> Namespaces = new();
98+
99+
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
100+
{
101+
if (syntaxNode is not StructDeclarationSyntax structNode)
102+
return;
103+
104+
var hasEventAttribute = structNode
105+
.AttributeLists
106+
.SelectMany(a => a.Attributes)
107+
.Any(attr => attr.Name.ToString().Contains("ExecutableEvent"));
108+
109+
if (!hasEventAttribute)
110+
{
111+
return;
112+
}
113+
114+
/* Skip generic types not specify argument */
115+
if (structNode.TypeParameterList?.Parameters.Count > 0)
116+
{
117+
return;
118+
}
119+
120+
Candidates.Add(structNode);
121+
122+
var namespaces = new HashSet<string>();
123+
var root = structNode.SyntaxTree.GetRoot();
124+
var usings = root.DescendantNodes().OfType<UsingDirectiveSyntax>();
125+
foreach (var usingDirective in usings)
126+
{
127+
var namespaceName = usingDirective.ToString();
128+
namespaces.Add(namespaceName);
129+
}
130+
Namespaces[structNode] = namespaces;
131+
132+
var fields = new List<GeneratorParameterInfo>();
133+
foreach (var member in structNode.Members)
134+
{
135+
if (member is FieldDeclarationSyntax fieldDeclaration)
136+
{
137+
if (!HasPublicField(fieldDeclaration)) continue;
138+
139+
// Handle multiple variable declarations (e.g., public int x, y;)
140+
foreach (var variable in fieldDeclaration.Declaration.Variables)
141+
{
142+
var fieldInfo = new GeneratorParameterInfo
143+
{
144+
ParameterType = fieldDeclaration.Declaration.Type!.ToString(),
145+
ParameterName = variable.Identifier.Text
146+
};
147+
fields.Add(fieldInfo);
148+
}
149+
}
150+
}
151+
PublicFields[structNode] = fields;
152+
}
153+
154+
private static bool HasPublicField(FieldDeclarationSyntax fieldDeclaration)
155+
{
156+
// Check if field has public modifier and is not static
157+
if (!fieldDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.PublicKeyword)))
158+
{
159+
return false;
160+
}
161+
162+
if (fieldDeclaration.Modifiers.Any(m => m.IsKind(SyntaxKind.StaticKeyword)))
163+
{
164+
return false;
165+
}
166+
167+
return true;
168+
}
169+
}
170+

0 commit comments

Comments
 (0)