Skip to content

Commit 1e7fb90

Browse files
authored
1.2.0 (#160)
* delegate registry * tests of delegate registry. * hide tests out from essential package. * add packages lock file * able to ignore certain behavior node for testing purpose. * use `DelegateRegistry` replace `VariantRegistry` * declare variant interfaces to be covariant. * preserve delegate classes and methods. * remove preserve from registered delegate method. * fix `SaveToLocalVariant` * ignore tests * ignore tests * integrate expression-evaluation by using DynamicExpresso * fix building * SerializedVariant and its drawer * code generator of variant classes * fix codegen of math types. * add missing dll files of DynamicExpresso * simplify component builder. * fix samples * fix sample of variant * fix sample of character control * generate Assembly-CSharp.dll for samples. * move samples and tests into sample package. * upgrade version of packages * update packages-locks * make `Reset` optional. * fix samples * get object value from array of serialized property. * add search bar for drawer of SerializeReference * use behavior tree id as part of expression lambda id * update serialized behavior tree of sample. * fix unit test.
1 parent 94ab4e5 commit 1e7fb90

File tree

219 files changed

+4419
-1897
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

219 files changed

+4419
-1897
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ Assets/Plugins.meta
7373

7474

7575
.DS_Store
76+
Packages/essential/Tests
77+
Packages/essential/Tests.meta
78+
Packages/variant.expression/Tests/
79+
Packages/codegen/Tests
80+
Packages/codegen/Tests.meta
81+
Packages/variant.expression/Tests.meta

Assets/_

Whitespace-only changes.

Assets/___.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// for generating Assembly-CSharp.dll
2+
public class ___ { }
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/builder.component/Editor/ComponentFieldCodeGenerator.cs

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,7 @@ public string GenerateBuild(FieldInfo fi)
5050

5151
public class BlobVariantFieldCodeGenerator : INodeDataFieldCodeGenerator
5252
{
53-
protected const string HEAD_LINE = "// Automatically generated by `BlobVariantFieldCodeGenerator`";
54-
55-
public bool ShouldGenerateVariantInterface = true;
56-
public string VariantInterfaceDirectory = "Variant";
5753
public string VariantInterfaceNamespace = "EntitiesBT.Variant";
58-
public string VariantPropertyNameSuffix = "Variant";
59-
public NodeCodeGenerator Generator;
60-
public AssemblyDefinitionAsset Assembly;
61-
62-
private ISet<Assembly> _referenceAssemblies;
63-
64-
private bool IsReferenceType(Type type)
65-
{
66-
_referenceAssemblies ??= Assembly.ToAssembly().FindReferenceAssemblies();
67-
return _referenceAssemblies.Contains(type.Assembly);
68-
}
6954

7055
public bool ShouldGenerate(FieldInfo fi)
7156
{
@@ -81,63 +66,16 @@ public string GenerateField(FieldInfo fi)
8166
{
8267
var valueType = fi.FieldType.GetGenericArguments()[0];
8368
var variantType = fi.FieldType.GetGenericTypeDefinition();
84-
var readerSuffix = $"{VariantPropertyNameSuffix}Reader";
85-
var writerSuffix = $"{VariantPropertyNameSuffix}Writer";
86-
var readerAndWriterSuffix = $"{VariantPropertyNameSuffix}ReaderAndWriter";
87-
if (ShouldGenerateVariantInterface)
88-
{
89-
if (variantType == typeof(BlobVariantReaderAndWriter<>))
90-
{
91-
GenerateVariantInterface(valueType, readerSuffix, VariantGenerator.CreateReaderVariants);
92-
GenerateVariantInterface(valueType, writerSuffix, VariantGenerator.CreateWriterVariants);
93-
GenerateVariantInterface(valueType, readerAndWriterSuffix, (writer, valueType, isReferenceType, suffix) =>
94-
{
95-
writer.CreateReaderAndWriterVariants(valueType, isReferenceType, suffix);
96-
writer.CreateSerializedReaderAndWriterVariant(valueType, suffix, $"{VariantPropertyNameSuffix}Reader", $"{VariantPropertyNameSuffix}Writer");
97-
});
98-
}
99-
else if (variantType == typeof(BlobVariantReader<>))
100-
{
101-
GenerateVariantInterface(valueType, readerSuffix, VariantGenerator.CreateReaderVariants);
102-
}
103-
else if (variantType == typeof(BlobVariantWriter<>))
104-
{
105-
GenerateVariantInterface(valueType, writerSuffix, VariantGenerator.CreateWriterVariants);
106-
}
107-
}
108-
10969
if (variantType == typeof(BlobVariantReaderAndWriter<>))
110-
return $"public {VariantInterfaceNamespace}.{valueType.Name}SerializedReaderAndWriterVariant {fi.Name};";
111-
112-
var suffix = variantType == typeof(BlobVariantReader<>) ? readerSuffix : writerSuffix;
113-
var stringBuilder = new StringBuilder();
114-
stringBuilder.Append("[UnityEngine.SerializeReference, SerializeReferenceButton]");
115-
stringBuilder.Append(" ");
116-
stringBuilder.AppendLine($"public {VariantInterfaceNamespace}.{valueType.Name}{suffix} {fi.Name};");
117-
return stringBuilder.ToString();
70+
return $"public {VariantInterfaceNamespace}.SerializedVariantRW<{valueType.FullName}> {fi.Name};";
71+
if (variantType == typeof(BlobVariantReader<>))
72+
return $"public {VariantInterfaceNamespace}.SerializedVariantRO<{valueType.FullName}> {fi.Name};";
73+
return $"public {VariantInterfaceNamespace}.SerializedVariantWO<{valueType.FullName}> {fi.Name};";
11874
}
11975

12076
public string GenerateBuild(FieldInfo fi)
12177
{
12278
return $"{fi.Name}.Allocate(ref builder, ref data.{fi.Name}, Self, tree);";
12379
}
124-
125-
private void GenerateVariantInterface(Type valueType, string suffix, CreateVariants createVariants)
126-
{
127-
var directory = Path.GetDirectoryName(AssetDatabase.GetAssetPath(Generator)) + "/" + VariantInterfaceDirectory;
128-
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
129-
130-
var filepath = $"{directory}/{valueType.Name}{suffix}.cs";
131-
if (!File.Exists(filepath) || File.ReadLines(filepath).FirstOrDefault() == HEAD_LINE)
132-
{
133-
using var writer = new StreamWriter(filepath);
134-
writer.WriteLine(HEAD_LINE);
135-
writer.WriteLine(VariantGenerator.NamespaceBegin(VariantInterfaceNamespace));
136-
createVariants(writer, valueType, IsReferenceType, suffix);
137-
writer.WriteLine(VariantGenerator.NamespaceEnd());
138-
}
139-
}
140-
141-
private delegate void CreateVariants(StreamWriter writer, Type valueType, Predicate<Type> isReferenceType, string suffix);
14280
}
14381
}

Packages/builder.component/Runtime/Components/BTDelayTimer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace EntitiesBT.Components
88
{
99
public class BTDelayTimer : BTNode<EntitiesBT.Nodes.DelayTimerNode>
1010
{
11-
public EntitiesBT.Variant.SingleSerializedReaderAndWriterVariant TimerSeconds;
11+
public EntitiesBT.Variant.SerializedVariantRW<System.Single> TimerSeconds;
1212
protected override void Build(ref EntitiesBT.Nodes.DelayTimerNode data, BlobBuilder builder, ITreeNode<INodeDataBuilder>[] tree)
1313
{
1414
TimerSeconds.Allocate(ref builder, ref data.TimerSeconds, Self, tree);

Packages/builder.component/Runtime/Components/BTTimer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace EntitiesBT.Components
88
{
99
public class BTTimer : BTNode<EntitiesBT.Nodes.TimerNode>
1010
{
11-
public EntitiesBT.Variant.SingleSerializedReaderAndWriterVariant CountdownSeconds;
11+
public EntitiesBT.Variant.SerializedVariantRW<System.Single> CountdownSeconds;
1212
public EntitiesBT.Core.NodeState BreakReturnState;
1313
protected override void Build(ref EntitiesBT.Nodes.TimerNode data, BlobBuilder builder, ITreeNode<INodeDataBuilder>[] tree)
1414
{

Packages/builder.component/Runtime/NodeCodeGenerator.asset

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ MonoBehaviour:
4646
00000005:
4747
type: {class: BlobVariantFieldCodeGenerator, ns: EntitiesBT.Editor, asm: EntitiesBT.Components.Editor}
4848
data:
49-
ShouldGenerateVariantInterface: 1
50-
VariantInterfaceDirectory: Variant
5149
VariantInterfaceNamespace: EntitiesBT.Variant
52-
VariantPropertyNameSuffix: Variant
53-
Generator: {fileID: 11400000}
54-
Assembly: {fileID: 5897886265953266890, guid: c7babe7561eb244059259745ef27b866,
55-
type: 3}
5650
00000006:
5751
type: {class: DefaultNodeDataFieldCodeGenerator, ns: EntitiesBT.Editor, asm: EntitiesBT.Editor}

Packages/builder.component/Runtime/Variant/NodeVariant.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Unity.Collections.LowLevel.Unsafe;
77
using Unity.Entities;
88
using UnityEngine;
9-
using UnityEngine.Scripting;
109
using static EntitiesBT.Core.Utilities;
1110

1211
namespace EntitiesBT.Variant
@@ -21,8 +20,12 @@ public VariantNodeObjectAttribute(string nodeObjectFieldName)
2120
}
2221
}
2322

23+
[VariantClass(ID_RUNTIME_NODE_VARIANT)]
2424
public class NodeVariant
2525
{
26+
public const string ID_RUNTIME_NODE = "220681AA-D884-4E87-90A8-5A8657A734BD";
27+
public const string ID_RUNTIME_NODE_VARIANT = "7DE6DCA5-71DF-4145-91A6-17EB813B9DEB";
28+
2629
// TODO: check loop ref?
2730
[Serializable]
2831
public class Any<T> : IVariant where T : unmanaged
@@ -40,23 +43,20 @@ public unsafe IntPtr Allocate(ref BlobBuilder builder, ref BlobVariant blobVaria
4043
[Serializable] public class Writer<T> : Any<T>, IVariantWriter<T> where T : unmanaged {}
4144
[Serializable] public class ReaderAndWriter<T> : Any<T>, IVariantReaderAndWriter<T> where T : unmanaged {}
4245

43-
public const string ID_RUNTIME_NODE = "220681AA-D884-4E87-90A8-5A8657A734BD";
44-
public const string ID_RUNTIME_NODE_VARIANT = "7DE6DCA5-71DF-4145-91A6-17EB813B9DEB";
45-
46-
[Preserve, WriterMethod(ID_RUNTIME_NODE_VARIANT)]
46+
[WriterMethod]
4747
private static unsafe void WriteVariableFunc<T, TNodeBlob, TBlackboard>(ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb, T value)
4848
where T : unmanaged
4949
where TNodeBlob : struct, INodeBlob
5050
where TBlackboard : struct, IBlackboard
5151
{
52-
ref var data = ref blobVariant.Value<DynamicNodeRefData>();
52+
ref var data = ref blobVariant.As<DynamicNodeRefData>();
5353
var ptr = blob.GetRuntimeDataPtr(data.Index);
5454
ref var variable = ref UnsafeUtility.AsRef<BlobVariantReader<T>>(IntPtr.Add(ptr, data.Offset).ToPointer());
5555
// TODO: check writable on editor?
5656
variable.Value.WriteWithRefFallback(index, ref blob, ref bb, value);
5757
}
5858

59-
[Preserve, RefReaderMethod(ID_RUNTIME_NODE)]
59+
[RefReaderMethod(OverrideGuid = ID_RUNTIME_NODE)]
6060
private static ref T GetRuntimeNodeData<T, TNodeBlob, TBlackboard>(ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
6161
where T : unmanaged
6262
where TNodeBlob : struct, INodeBlob
@@ -65,21 +65,21 @@ private static ref T GetRuntimeNodeData<T, TNodeBlob, TBlackboard>(ref BlobVaria
6565
return ref GetValue<T>(ref blobVariant, blob.GetRuntimeDataPtr);
6666
}
6767

68-
[Preserve, ReaderMethod(ID_RUNTIME_NODE_VARIANT)]
68+
[ReaderMethod]
6969
private static unsafe T GetRuntimeNodeVariable<T, TNodeBlob, TBlackboard>(ref BlobVariant blobVariant, int index, ref TNodeBlob blob, ref TBlackboard bb)
7070
where T : unmanaged
7171
where TNodeBlob : struct, INodeBlob
7272
where TBlackboard : struct, IBlackboard
7373
{
74-
ref var data = ref blobVariant.Value<DynamicNodeRefData>();
74+
ref var data = ref blobVariant.As<DynamicNodeRefData>();
7575
var ptr = blob.GetRuntimeDataPtr(data.Index);
7676
ref var variable = ref UnsafeUtility.AsRef<BlobVariantReader<T>>(IntPtr.Add(ptr, data.Offset).ToPointer());
7777
return variable.Read(index, ref blob, ref bb);
7878
}
7979

8080
private static unsafe ref T GetValue<T>(ref BlobVariant blobVariant, Func<int, IntPtr> ptrFunc) where T : unmanaged
8181
{
82-
ref var data = ref blobVariant.Value<DynamicNodeRefData>();
82+
ref var data = ref blobVariant.As<DynamicNodeRefData>();
8383
var ptr = ptrFunc(data.Index);
8484
return ref UnsafeUtility.AsRef<T>(IntPtr.Add(ptr, data.Offset).ToPointer());
8585
}

Packages/builder.component/Runtime/Variant/SingleVariantReader.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)