-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
ClassGeneratedValueSerializedTable.cs
107 lines (94 loc) · 2.83 KB
/
ClassGeneratedValueSerializedTable.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
// Copyright (c) 2020-2023 Vladimir Popov zor1994@gmail.com https://github.com/ZorPastaman/Simple-Blackboard
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using UnityEngine;
using Zor.SimpleBlackboard.Core;
using Zor.SimpleBlackboard.Helpers;
namespace Zor.SimpleBlackboard.Serialization
{
/// <summary>
/// <para>Serialized table that has set keys and generates class values at runtime.</para>
/// <para>Inherit this class and override <see cref="GetValue"/> to get this functionality.</para>
/// </summary>
/// <typeparam name="T">Class type of the generated value.</typeparam>
public abstract class ClassGeneratedValueSerializedTable<T> : GeneratedValueSerializedTable_Base where T : class
{
#pragma warning disable CS0649
[SerializeField] private string[] m_Keys;
#pragma warning restore CS0649
/// <inheritdoc/>
public sealed override Type valueType
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => typeof(T);
}
/// <summary>
/// How many keys are contained in this <see cref="ClassGeneratedValueSerializedTable{T}"/>.
/// </summary>
public int keysCount
{
[MethodImpl(MethodImplOptions.AggressiveInlining), Pure]
get => m_Keys.Length;
}
/// <summary>
/// Gets a key at the <paramref name="index"/>.
/// </summary>
/// <param name="index"></param>
/// <returns>
/// A property as a pair of <see cref="string"/> and <typeparamref name="T"/>.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining), NotNull, Pure]
public string GetKey(int index)
{
return m_Keys[index];
}
/// <summary>
/// Sets the <paramref name="key"/> at the <paramref name="index"/>.
/// </summary>
/// <param name="key"></param>
/// <param name="index"></param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void SetKey([NotNull] string key, int index)
{
m_Keys[index] = key;
}
/// <summary>
/// Sets <paramref name="keys"/>.
/// </summary>
/// <param name="keys"></param>
public void SetKeys([NotNull] string[] keys)
{
int count = keys.Length;
if (m_Keys.Length != count)
{
m_Keys = new string[count];
}
Array.Copy(keys, 0, m_Keys, 0, count);
}
/// <inheritdoc/>
public sealed override void Apply(Blackboard blackboard)
{
for (int i = 0, count = m_Keys.Length; i < count; ++i)
{
blackboard.SetClassValue(new BlackboardPropertyName(m_Keys[i]), GetValue());
}
}
/// <inheritdoc/>
public sealed override void GetKeys(List<(string, Type)> keys)
{
int count = m_Keys.Length;
ListHelper.EnsureCapacity(keys, count);
for (int i = 0; i < count; ++i)
{
keys.Add((m_Keys[i], valueType));
}
}
/// <summary>
/// Gets a generated value.
/// </summary>
/// <returns>Generated value.</returns>
protected abstract T GetValue();
}
}