-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
SimpleBlackboardContainer.cs
188 lines (168 loc) · 5.95 KB
/
SimpleBlackboardContainer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2020-2023 Vladimir Popov [email protected] https://github.com/ZorPastaman/Simple-Blackboard
using System;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using UnityEngine;
using Zor.SimpleBlackboard.Core;
using Zor.SimpleBlackboard.Helpers;
using Zor.SimpleBlackboard.Serialization;
namespace Zor.SimpleBlackboard.Components
{
/// <summary>
/// Container of <see cref="Zor.SimpleBlackboard.Core.Blackboard"/> for using that as
/// <see cref="UnityEngine.Component"/>.
/// </summary>
[AddComponentMenu(AddComponentConstants.SimpleBlackboardFolder + "Simple Blackboard Container")]
public sealed class SimpleBlackboardContainer : MonoBehaviour
{
#pragma warning disable CS0649
[SerializeField,
Tooltip("Array of serialized properties for Blackboard.\nIt is automatically applied to Blackboard on Awake.")]
private SimpleSerializedContainer[] m_SerializedContainers;
[SerializeField,
Tooltip("Array of serialized references to local components for Blackboard.\nIt is automatically applied to Blackboard on Awake.")]
private ComponentReference[] m_ComponentReferences;
#pragma warning restore CS0649
private Blackboard m_blackboard;
/// <summary>
/// Contained <see cref="Blackboard"/>.
/// </summary>
/// <remarks>
/// It's not recommended to cache this value.
/// </remarks>
[NotNull]
public Blackboard blackboard
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_blackboard;
}
/// <summary>
/// How many serialized containers this <see cref="SimpleBlackboardContainer"/> depends on.
/// </summary>
public int serializedContainersCount
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_SerializedContainers.Length;
}
/// <summary>
/// How many component references this <see cref="SimpleBlackboardContainer"/> depends on.
/// </summary>
public int componentReferencesCount
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_ComponentReferences.Length;
}
/// <summary>
/// Gets a <see cref="SimpleSerializedContainer"/> at the <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns><see cref="SimpleSerializedContainer"/> at the <paramref name="index"/>.</returns>
/// <remarks>
/// If you change a gotten <see cref="SetSerializedContainer"/>,
/// you need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining), NotNull, Pure]
public SimpleSerializedContainer GetSerializedContainer(int index)
{
return m_SerializedContainers[index];
}
/// <summary>
/// Sets the <paramref name="serializedContainer"/> at the <paramref name="index"/>
/// </summary>
/// <param name="serializedContainer"></param>
/// <param name="index"></param>
/// <remarks>
/// You need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetSerializedContainer([NotNull] SimpleSerializedContainer serializedContainer, int index)
{
m_SerializedContainers[index] = serializedContainer;
}
/// <summary>
/// Sets the <paramref name="serializedContainers"/>.
/// </summary>
/// <param name="serializedContainers"></param>
/// <remarks>
/// You need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
public void SetSerializedContainers([NotNull] SimpleSerializedContainer[] serializedContainers)
{
int count = serializedContainers.Length;
if (m_SerializedContainers.Length != count)
{
m_SerializedContainers = new SimpleSerializedContainer[count];
}
Array.Copy(serializedContainers, 0, m_SerializedContainers, 0, count);
}
/// <summary>
/// Gets a <see cref="ComponentReference"/> at the <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns><see cref="ComponentReference"/> at the <paramref name="index"/>.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
public ComponentReference GetComponentReference(int index)
{
return m_ComponentReferences[index];
}
/// <summary>
/// Sets the component reference <paramref name="componentReference"/> at the <paramref name="index"/>.
/// </summary>
/// <param name="componentReference"></param>
/// <param name="index"></param>
/// <remarks>
/// You need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetComponentReference(ComponentReference componentReference, int index)
{
m_ComponentReferences[index] = componentReference;
}
/// <summary>
/// Sets the component references <paramref name="componentReferences"/>.
/// </summary>
/// <param name="componentReferences"></param>
/// <remarks>
/// You need to call <see cref="RecreateBlackboard"/> to apply changes.
/// </remarks>
public void SetComponentReferences(ComponentReference[] componentReferences)
{
int count = componentReferences.Length;
if (m_ComponentReferences.Length != count)
{
m_ComponentReferences = new ComponentReference[count];
}
Array.Copy(componentReferences, 0, m_ComponentReferences, 0, count);
}
/// <summary>
/// Creates a new <see cref="Blackboard"/> and applies current serialized containers to it.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining), ContextMenu("Recreate Blackboard")]
public void RecreateBlackboard()
{
Awake();
}
private void Awake()
{
var newBlackboard = new Blackboard();
#if SIMPLE_BLACKBOARD_MULTITHREADING
lock (newBlackboard)
#endif
{
m_blackboard = newBlackboard;
DeserializationHelper.Deserialize(m_SerializedContainers, m_blackboard);
DeserializationHelper.Deserialize(m_ComponentReferences, m_blackboard);
}
}
[ContextMenu("Log")]
private void Log()
{
#if SIMPLE_BLACKBOARD_MULTITHREADING
lock (m_blackboard)
#endif
{
Debug.Log(m_blackboard.ToString(), this);
}
}
}
}