@bikky/replication 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Constants/Errors.d.ts +27 -0
- package/Constants/Errors.js +75 -0
- package/Constants/Logging.d.ts +17 -0
- package/Constants/Logging.js +97 -0
- package/Constants/ReplicableRegistry.d.ts +37 -0
- package/Constants/ReplicableRegistry.js +234 -0
- package/Constants/SerialisationTypes.d.ts +82 -0
- package/Constants/SerialisationTypes.js +160 -0
- package/Constants/SourceMaps.d.ts +10 -0
- package/Constants/SourceMaps.js +12 -0
- package/Constants/TraversalStep.d.ts +5 -0
- package/Constants/TraversalStep.js +2 -0
- package/Constants/Versions.d.ts +15 -0
- package/Constants/Versions.js +63 -0
- package/Expressions/Compiler/BuiltinGrammar.d.ts +234 -0
- package/Expressions/Compiler/BuiltinGrammar.js +446 -0
- package/Expressions/Compiler/ExpressionGrammar.d.ts +89 -0
- package/Expressions/Compiler/ExpressionGrammar.js +70 -0
- package/Expressions/Compiler/Parser.d.ts +56 -0
- package/Expressions/Compiler/Parser.js +314 -0
- package/Expressions/Compiler/Tokenizer.d.ts +52 -0
- package/Expressions/Compiler/Tokenizer.js +222 -0
- package/Expressions/Compiler/__tests__/Replicable.Expressions.Parser.test.d.ts +1 -0
- package/Expressions/Compiler/__tests__/Replicable.Expressions.Parser.test.js +516 -0
- package/Expressions/Compiler/__tests__/Replicable.Expressions.Tokenizer.test.d.ts +1 -0
- package/Expressions/Compiler/__tests__/Replicable.Expressions.Tokenizer.test.js +68 -0
- package/Expressions/CreateEvaluator.d.ts +4 -0
- package/Expressions/CreateEvaluator.js +85 -0
- package/Expressions/EvaluatorChain.d.ts +19 -0
- package/Expressions/EvaluatorChain.js +137 -0
- package/Expressions/EvaluatorSteps.d.ts +19 -0
- package/Expressions/EvaluatorSteps.js +12 -0
- package/Expressions/EvaluatorString.d.ts +21 -0
- package/Expressions/EvaluatorString.js +26 -0
- package/Expressions/Expression.d.ts +36 -0
- package/Expressions/Expression.js +147 -0
- package/Expressions/Traverser.d.ts +28 -0
- package/Expressions/Traverser.js +348 -0
- package/Expressions/TypeRegistry/Accessors.d.ts +26 -0
- package/Expressions/TypeRegistry/Accessors.js +58 -0
- package/Expressions/TypeRegistry/ChainCollections.d.ts +51 -0
- package/Expressions/TypeRegistry/ChainCollections.js +134 -0
- package/Expressions/TypeRegistry/ChainTypes.d.ts +23 -0
- package/Expressions/TypeRegistry/ChainTypes.js +46 -0
- package/Expressions/TypeRegistry/CustomAPI.d.ts +36 -0
- package/Expressions/TypeRegistry/CustomAPI.js +181 -0
- package/Expressions/TypeRegistry/Primitive.d.ts +19 -0
- package/Expressions/TypeRegistry/Primitive.js +47 -0
- package/Expressions/TypeRegistry/Registry.d.ts +27 -0
- package/Expressions/TypeRegistry/Registry.js +270 -0
- package/Expressions/TypeRegistry/ReplAPI.d.ts +41 -0
- package/Expressions/TypeRegistry/ReplAPI.js +220 -0
- package/Expressions/TypeRegistry/Scope.d.ts +24 -0
- package/Expressions/TypeRegistry/Scope.js +44 -0
- package/Expressions/TypeRegistry/Types.d.ts +23 -0
- package/Expressions/TypeRegistry/Types.js +1 -0
- package/Expressions/TypeRegistry/__tests__/Replicable.Expressions.Accessors.test.d.ts +1 -0
- package/Expressions/TypeRegistry/__tests__/Replicable.Expressions.Accessors.test.js +31 -0
- package/Expressions/__tests__/ExpressionExamples.d.ts +28 -0
- package/Expressions/__tests__/ExpressionExamples.js +50 -0
- package/Expressions/__tests__/Replicable.Expressions.Expressions.test.d.ts +1 -0
- package/Expressions/__tests__/Replicable.Expressions.Expressions.test.js +166 -0
- package/IDPool.d.ts +18 -0
- package/IDPool.data.d.ts +17 -0
- package/IDPool.js +139 -0
- package/License.txt +1 -0
- package/Main.d.ts +13 -0
- package/Main.js +13 -0
- package/Networking.d.ts +60 -0
- package/Networking.js +626 -0
- package/Replicatable.d.ts +66 -0
- package/Replicatable.js +123 -0
- package/Tracking/Buffable.d.ts +68 -0
- package/Tracking/Buffable.js +194 -0
- package/Tracking/Class.d.ts +97 -0
- package/Tracking/Class.js +221 -0
- package/Tracking/Functions.d.ts +14 -0
- package/Tracking/Functions.js +27 -0
- package/Tracking/GlobalGroup.d.ts +5 -0
- package/Tracking/GlobalGroup.js +39 -0
- package/Tracking/Property.d.ts +95 -0
- package/Tracking/Property.js +125 -0
- package/Tracking/Types.d.ts +33 -0
- package/Tracking/Types.js +1 -0
- package/Tracking/__tests__/Replicable.Tracking.Decorator.test.d.ts +1 -0
- package/Tracking/__tests__/Replicable.Tracking.Decorator.test.js +151 -0
- package/Tracking/__tests__/Replicable.Tracking.Deserialisation.test.d.ts +1 -0
- package/Tracking/__tests__/Replicable.Tracking.Deserialisation.test.js +253 -0
- package/Tracking/__tests__/Replicable.Tracking.MixinSchemaGeneration.test.d.ts +1 -0
- package/Tracking/__tests__/Replicable.Tracking.MixinSchemaGeneration.test.js +135 -0
- package/Tracking/__tests__/Replicable.Tracking.Struct.test.d.ts +1 -0
- package/Tracking/__tests__/Replicable.Tracking.Struct.test.js +66 -0
- package/Tracking/__tests__/Replicable.Tracking.Type.test.d.ts +1 -0
- package/Tracking/__tests__/Replicable.Tracking.Type.test.js +67 -0
- package/Transformers/Configurer.d.ts +39 -0
- package/Transformers/Configurer.js +415 -0
- package/Transformers/Constructor.d.ts +12 -0
- package/Transformers/Constructor.js +44 -0
- package/Transformers/Definitions.d.ts +102 -0
- package/Transformers/Definitions.js +626 -0
- package/Transformers/Loader.d.ts +45 -0
- package/Transformers/Loader.js +350 -0
- package/Transformers/Progress.d.ts +32 -0
- package/Transformers/Progress.js +429 -0
- package/Transformers/Reference.d.ts +37 -0
- package/Transformers/Reference.js +212 -0
- package/Transformers/SchemaGenerator.d.ts +102 -0
- package/Transformers/SchemaGenerator.js +564 -0
- package/Transformers/Serialiser.d.ts +31 -0
- package/Transformers/Serialiser.js +366 -0
- package/Transformers/Utils.d.ts +33 -0
- package/Transformers/Utils.js +287 -0
- package/Transformers/__tests__/Examples.d.ts +168 -0
- package/Transformers/__tests__/Examples.js +263 -0
- package/Transformers/__tests__/Replicable.Transformers.Definitions.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.Definitions.test.js +457 -0
- package/Transformers/__tests__/Replicable.Transformers.Loader.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.Loader.test.js +339 -0
- package/Transformers/__tests__/Replicable.Transformers.Progress.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.Progress.test.js +256 -0
- package/Transformers/__tests__/Replicable.Transformers.Reference.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.Reference.test.js +167 -0
- package/Transformers/__tests__/Replicable.Transformers.SchemaGenerator.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.SchemaGenerator.test.js +400 -0
- package/Transformers/__tests__/Replicable.Transformers.SchemaGeneratorOutput.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.SchemaGeneratorOutput.test.js +441 -0
- package/Transformers/__tests__/Replicable.Transformers.Serialiser.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.Serialiser.test.js +320 -0
- package/Transformers/__tests__/Replicable.Transformers.Utils.test.d.ts +1 -0
- package/Transformers/__tests__/Replicable.Transformers.Utils.test.js +534 -0
- package/__tests__/Replicable.Expressions.test.d.ts +1 -0
- package/__tests__/Replicable.Expressions.test.js +166 -0
- package/__tests__/Replicable.IDPool.test.d.ts +1 -0
- package/__tests__/Replicable.IDPool.test.js +11 -0
- package/__tests__/Replicable.ReplicableRegistry.test.d.ts +1 -0
- package/__tests__/Replicable.ReplicableRegistry.test.js +154 -0
- package/__tests__/Replicable.Serialisation.test.d.ts +1 -0
- package/__tests__/Replicable.Serialisation.test.js +283 -0
- package/package.json +14 -0
- package/tsconfig.json +19 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import { ClassCategory, DataSymbol, describeObject, isPrimitiveType, isPrimitiveValue, PropertyTypes, TypeSymbol, ValueIsPrimitiveType } from "../Constants/SerialisationTypes.js";
|
|
2
|
+
import { Expression } from "../Expressions/Expression.js";
|
|
3
|
+
import { Pointer } from "./Reference.js";
|
|
4
|
+
import { ReplicableClass } from "../Tracking/Class.js";
|
|
5
|
+
import { ErrorStack } from "../Constants/Errors.js";
|
|
6
|
+
import { __PrivateConstructorSetup, Constructor } from "./Constructor.js";
|
|
7
|
+
import { Definitions } from "./Definitions.js";
|
|
8
|
+
import { ReplicableRegistry } from "../Constants/ReplicableRegistry.js";
|
|
9
|
+
export var Loader;
|
|
10
|
+
(function (Loader) {
|
|
11
|
+
//Used to keep track of if an object has been initialised,
|
|
12
|
+
// so it can be initialised automatically in create if not done manually.
|
|
13
|
+
var isExpressionKind = Expression.isExpressionKind;
|
|
14
|
+
var isExpressionString = Expression.isExpressionString;
|
|
15
|
+
const IsInitialised = Symbol("IsInitialised");
|
|
16
|
+
const ExprProperties = Symbol("ExpressionProperties");
|
|
17
|
+
function LoadSingleValueAsType(propErrors, obj, prop, propInfo, propType, val) {
|
|
18
|
+
//De-expression expressions, because we don't have fancy ways of handling values in arrays or
|
|
19
|
+
// maps yet (also don't know if we want to do that or not yet).
|
|
20
|
+
if (isExpressionKind(val)) {
|
|
21
|
+
if (!(val instanceof Expression)) {
|
|
22
|
+
val = new Expression(val);
|
|
23
|
+
}
|
|
24
|
+
val = val.get();
|
|
25
|
+
}
|
|
26
|
+
if (typeof propInfo.types === "string") {
|
|
27
|
+
return val;
|
|
28
|
+
}
|
|
29
|
+
if (isPrimitiveType(propType)) {
|
|
30
|
+
if (ValueIsPrimitiveType(propType, val)) {
|
|
31
|
+
return val;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
throw new Error(`Invalid Value.`);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
if (PropertyTypes.isType(propInfo.modifiers, PropertyTypes.Definition)) {
|
|
38
|
+
if (val.__proto__ !== Object.prototype || !("Class" in val)) {
|
|
39
|
+
throw new Error(`Invalid Value.`);
|
|
40
|
+
}
|
|
41
|
+
//Duplicate the data just in case it gets modified.
|
|
42
|
+
return JSON.parse(JSON.stringify(val));
|
|
43
|
+
}
|
|
44
|
+
if (PropertyTypes.isType(propInfo.modifiers, PropertyTypes.Serialised)) {
|
|
45
|
+
if ("Class" in val) {
|
|
46
|
+
throw new Error(`Invalid Value.`);
|
|
47
|
+
}
|
|
48
|
+
else if (!Array.isArray(val)) {
|
|
49
|
+
throw new Error(`Invalid Value.`);
|
|
50
|
+
}
|
|
51
|
+
else if (!ReplicableRegistry.__GetReplicableForDataName(val[0])) {
|
|
52
|
+
throw new Error(`Invalid Value.`);
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
return val;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (ClassCategory.isTypeOr(propType.category, ClassCategory.Pointer, ClassCategory.Inlined)) {
|
|
59
|
+
if (val instanceof Pointer) {
|
|
60
|
+
return val;
|
|
61
|
+
}
|
|
62
|
+
else if (val.__proto__ === Object.prototype) {
|
|
63
|
+
return new Pointer(Constructor.Construct(propType, obj.source, obj.root, val));
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return new Pointer(val);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (ClassCategory.isTypeOr(propType.category, ClassCategory.Struct, ClassCategory.Custom)) {
|
|
70
|
+
//Already instantiated!
|
|
71
|
+
if (val[TypeSymbol]) {
|
|
72
|
+
return val;
|
|
73
|
+
}
|
|
74
|
+
// If it declares its own class then use that, because it might be a child class.
|
|
75
|
+
// We don't need to check if it is a child class because that would have been done before.
|
|
76
|
+
else if (typeof val === "object" && "Class" in val && ReplicableRegistry.__GetReplicableForDataName(val.Class)) {
|
|
77
|
+
let repl = ReplicableRegistry.__GetReplicableForDataName(val.Class);
|
|
78
|
+
return Constructor.Construct(repl, obj.source, obj.root, val);
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return Constructor.Construct(propType, obj.source, obj.root, val);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
Loader.LoadSingleValueAsType = LoadSingleValueAsType;
|
|
86
|
+
function PerValueCallback(value, idxName, triggeredMod, propErrors, obj, prop) {
|
|
87
|
+
var _a;
|
|
88
|
+
if (value instanceof Pointer) {
|
|
89
|
+
value = value.val;
|
|
90
|
+
}
|
|
91
|
+
if (PropertyTypes.isType(triggeredMod, PropertyTypes.Map)) {
|
|
92
|
+
let exprConf = prop.conf.expressionForKey;
|
|
93
|
+
if (isExpressionString(idxName) && exprConf) {
|
|
94
|
+
try {
|
|
95
|
+
idxName = new Expression({ text: idxName, sourceFile: obj.source.SourceFile }, exprConf[obj.source.Version]);
|
|
96
|
+
}
|
|
97
|
+
catch (e) {
|
|
98
|
+
propErrors.add("REP_TL001", `Failed to parse map key expression ${idxName}`);
|
|
99
|
+
if (e instanceof Error) {
|
|
100
|
+
propErrors.add("REP_TL002", (_a = e.stack) !== null && _a !== void 0 ? _a : e.message);
|
|
101
|
+
}
|
|
102
|
+
return [undefined, idxName];
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
let entryErrors = new ErrorStack(obj.source);
|
|
107
|
+
for (let types of prop.conf.load.types) {
|
|
108
|
+
if (!Array.isArray(types.types)) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (!PropertyTypes.isType(types.modifiers, triggeredMod))
|
|
112
|
+
continue;
|
|
113
|
+
for (let type of types.types) {
|
|
114
|
+
if (Definitions.IsLoadableDataForType(type, value, obj.source.Version, entryErrors, false)) {
|
|
115
|
+
return [LoadSingleValueAsType(entryErrors, obj, prop, types, type, value), idxName];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
propErrors.appendWithoutIndent(entryErrors);
|
|
120
|
+
if (idxName != null) {
|
|
121
|
+
propErrors.add("REP_TL003", `Failed to load ${obj.rootRepl.opts.dataTypeName}.${prop.dataName}[${idxName}].`);
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
propErrors.add("REP_TL004", `Failed to load ${obj.rootRepl.opts.dataTypeName}.${prop.dataName}.`);
|
|
125
|
+
}
|
|
126
|
+
return [undefined, idxName];
|
|
127
|
+
}
|
|
128
|
+
Loader.PerValueCallback = PerValueCallback;
|
|
129
|
+
/**
|
|
130
|
+
* ASSUMES INPUTS ARE CORRECT - DOES NOT CHECK FOR ERRORS.
|
|
131
|
+
* Get all of the assigned property values, so that they can individually be checked against the type requirements
|
|
132
|
+
* of the definiton. Turns single values, double arrays, maps and dicts all into a single flat array of values.
|
|
133
|
+
* @param def The property definition to get the values for.
|
|
134
|
+
* @param val The value to get the values from.
|
|
135
|
+
*/
|
|
136
|
+
function LoadPropValuesFromData(val, propErrors, obj, prop) {
|
|
137
|
+
let possibleModifiers = prop.conf.load.possibleModifiers;
|
|
138
|
+
if (PropertyTypes.isType(possibleModifiers, PropertyTypes.Array) && Array.isArray(val)) {
|
|
139
|
+
return val.map((e) => {
|
|
140
|
+
return PerValueCallback(e, null, PropertyTypes.Array, propErrors, obj, prop)[0];
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
else if (PropertyTypes.isType(possibleModifiers, PropertyTypes.Array2) && Array.isArray(val)) {
|
|
144
|
+
return val.map((e, i) => e.map((n, j) => {
|
|
145
|
+
return PerValueCallback(n, `${i},${j}`, PropertyTypes.Array2, propErrors, obj, prop)[0];
|
|
146
|
+
}));
|
|
147
|
+
}
|
|
148
|
+
else if (PropertyTypes.isType(possibleModifiers, PropertyTypes.Map) && (val instanceof Map || typeof val === "object" && val.__proto__ === Object.prototype)) {
|
|
149
|
+
let arr;
|
|
150
|
+
if (val instanceof Map) {
|
|
151
|
+
arr = Array.from(val.entries());
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
arr = Array.from(Object.entries(val));
|
|
155
|
+
}
|
|
156
|
+
return new Map(arr.map((e) => {
|
|
157
|
+
let [newVal, newIdx] = PerValueCallback(e[1], e[0], PropertyTypes.Map, propErrors, obj, prop);
|
|
158
|
+
return [newIdx, newVal];
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
else if (PropertyTypes.isType(possibleModifiers, PropertyTypes.Dictionary) && (typeof val === "object" && val.__proto__ === Object.prototype)) {
|
|
162
|
+
let arr = Array.from(Object.entries(val));
|
|
163
|
+
return Object.fromEntries(arr.map((e) => {
|
|
164
|
+
return [e[0], PerValueCallback(e[1], e[0], PropertyTypes.Dictionary, propErrors, obj, prop)[0]];
|
|
165
|
+
}));
|
|
166
|
+
}
|
|
167
|
+
else if (PropertyTypes.isType(possibleModifiers, PropertyTypes.Solo)) {
|
|
168
|
+
return PerValueCallback(val, null, PropertyTypes.Solo, propErrors, obj, prop)[0];
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
Loader.LoadPropValuesFromData = LoadPropValuesFromData;
|
|
172
|
+
/**
|
|
173
|
+
*
|
|
174
|
+
* @param type The type that the property is defined for.
|
|
175
|
+
* @param source The location that the property's definition was defined at.
|
|
176
|
+
* @param name The name of the property that will be instantiated, used for looking up property info.
|
|
177
|
+
* @param val The data value that will be used to instantiate the property
|
|
178
|
+
* @param target The 'this' object that will house the property
|
|
179
|
+
*/
|
|
180
|
+
function CalcPropInitialValue(obj, name, val, target) {
|
|
181
|
+
let definition = obj.rootRepl.getPropDefinition(obj.source.Version, name);
|
|
182
|
+
if (!definition)
|
|
183
|
+
throw new Error("No information for property " + name);
|
|
184
|
+
if ("removed" in definition)
|
|
185
|
+
throw new Error(`Property ${obj.rootRepl.opts.typescriptTypeName}.${name} has been removed and cannot be loaded.`);
|
|
186
|
+
if ("doesntExist" in definition)
|
|
187
|
+
throw new Error(`Property ${obj.rootRepl.opts.typescriptTypeName}.${name} does not exist yet and cannot be loaded.`);
|
|
188
|
+
if (isExpressionKind(val)) {
|
|
189
|
+
//undefined is implicitly true
|
|
190
|
+
if (!definition.expressionForProperty) {
|
|
191
|
+
throw new Error(`${obj.rootRepl.opts.dataTypeName}.${name} does not accept expressions.`);
|
|
192
|
+
}
|
|
193
|
+
if (val instanceof Expression) {
|
|
194
|
+
return val;
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
let exprConf = val.config;
|
|
198
|
+
let errors = new ErrorStack(obj.source);
|
|
199
|
+
if (Definitions.DoesExpressionMatchProperty(`${obj.rootRepl.opts.dataTypeName}.${name}`, exprConf, val, errors)) {
|
|
200
|
+
return new Expression(val);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
throw new Error(errors.toString("REP_TL005"));
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// Initialise optional properties either with what they've previously had assigned (during construction) and
|
|
208
|
+
// validate it, or with their default value, or if nothing else works, undefined.
|
|
209
|
+
// We handle type by pretending the value was actually loaded in the definition, then letting the following
|
|
210
|
+
// loading code check the value.
|
|
211
|
+
if (typeof val === "undefined" && typeof definition.load.default !== "undefined") {
|
|
212
|
+
val = definition.load.default;
|
|
213
|
+
if (Array.isArray(val)) {
|
|
214
|
+
val = [...val];
|
|
215
|
+
}
|
|
216
|
+
else if (typeof val === "object" && val !== null && val.__proto__ === Object.prototype) {
|
|
217
|
+
val = Object.assign({}, val);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// If the data doesn't define the value, but the class does (using assignment instead of @Property's
|
|
221
|
+
// default variable) then we want to adopt the value from the class as a back-up.
|
|
222
|
+
// We need to use typeof because typescript classes default to initialising properties as undefined.
|
|
223
|
+
if (typeof val === "undefined" && name in target && typeof target[name] !== "undefined") {
|
|
224
|
+
val = target[name];
|
|
225
|
+
}
|
|
226
|
+
//Early exit on optional because we don't need to do anything special.
|
|
227
|
+
if (definition.load.present == "optional" && (typeof val === "undefined" || val instanceof Pointer && val.valOrNull === null)) {
|
|
228
|
+
return val;
|
|
229
|
+
}
|
|
230
|
+
//Early exit on null because we don't need to do anything special.
|
|
231
|
+
if (val === null) {
|
|
232
|
+
return val;
|
|
233
|
+
}
|
|
234
|
+
let propErrors = new ErrorStack(obj.source);
|
|
235
|
+
let result = LoadPropValuesFromData(val, propErrors, obj, { name, dataName: definition.load.name, conf: definition });
|
|
236
|
+
if (!propErrors.hasErrors()) {
|
|
237
|
+
return result;
|
|
238
|
+
}
|
|
239
|
+
let errors = new ErrorStack(obj.source);
|
|
240
|
+
errors.appendAndIndent(propErrors);
|
|
241
|
+
errors.add("REP_TL006", `Object ${describeObject(val)} is not a valid value for property `
|
|
242
|
+
+ `${describeObject(obj.rootRepl.classConstructor)}.${definition.load.name}, property only accepts`
|
|
243
|
+
+ ` types ${ReplicableClass.typesToString(definition.load.types.flatMap((e) => {
|
|
244
|
+
return typeof e.types == "string" ? [e.types] : e.types;
|
|
245
|
+
}))}.\n\n`
|
|
246
|
+
+ errors.toString("REP_TL007"));
|
|
247
|
+
throw new Error(errors.toString("REP_TL008"));
|
|
248
|
+
}
|
|
249
|
+
Loader.CalcPropInitialValue = CalcPropInitialValue;
|
|
250
|
+
function SetPropertyToExpression(target, property, expression) {
|
|
251
|
+
var _a;
|
|
252
|
+
target[ExprProperties] = ((_a = target[ExprProperties]) !== null && _a !== void 0 ? _a : {});
|
|
253
|
+
target[ExprProperties][property] = expression;
|
|
254
|
+
Object.defineProperty(target, property, {
|
|
255
|
+
set(v) {
|
|
256
|
+
expression.set(v);
|
|
257
|
+
},
|
|
258
|
+
get() {
|
|
259
|
+
return expression.get();
|
|
260
|
+
},
|
|
261
|
+
enumerable: true,
|
|
262
|
+
configurable: true
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
Loader.SetPropertyToExpression = SetPropertyToExpression;
|
|
266
|
+
function IsPropertyExpression(target, property) {
|
|
267
|
+
return !!(target[ExprProperties] && target[ExprProperties][property]);
|
|
268
|
+
}
|
|
269
|
+
Loader.IsPropertyExpression = IsPropertyExpression;
|
|
270
|
+
function ClearPropertyExpression(target, property) {
|
|
271
|
+
if (target[ExprProperties]) {
|
|
272
|
+
delete target[ExprProperties][property];
|
|
273
|
+
}
|
|
274
|
+
delete target[property];
|
|
275
|
+
}
|
|
276
|
+
Loader.ClearPropertyExpression = ClearPropertyExpression;
|
|
277
|
+
function GetExprText(target, property) {
|
|
278
|
+
var _a;
|
|
279
|
+
let expr = (_a = target[ExprProperties]) === null || _a === void 0 ? void 0 : _a[property];
|
|
280
|
+
if (expr) {
|
|
281
|
+
return expr.rawText;
|
|
282
|
+
}
|
|
283
|
+
return undefined;
|
|
284
|
+
}
|
|
285
|
+
Loader.GetExprText = GetExprText;
|
|
286
|
+
function InitFromData(type, source, target, data) {
|
|
287
|
+
var _a;
|
|
288
|
+
if (isPrimitiveValue(target) && "DataIsType" in type.classConstructor) {
|
|
289
|
+
//If type class is a special override for working with a primitive data type,
|
|
290
|
+
// like UUIDs and strings, then we don't need to initialise it.
|
|
291
|
+
return target;
|
|
292
|
+
}
|
|
293
|
+
if (!target[DataSymbol]) {
|
|
294
|
+
target[DataSymbol] = {};
|
|
295
|
+
}
|
|
296
|
+
MarkInitialised(target);
|
|
297
|
+
let props = (_a = type.finalised_properties.get(type.opts.version)) !== null && _a !== void 0 ? _a : {};
|
|
298
|
+
let objData = { root: target, source, rootRepl: type };
|
|
299
|
+
for (let [name, def] of Object.entries(props)) {
|
|
300
|
+
if ("removed" in def || "doesntExist" in def) {
|
|
301
|
+
//TODO: run migration functions for removed properties.
|
|
302
|
+
continue;
|
|
303
|
+
}
|
|
304
|
+
if (!def.load.present || def.load.skipLoad)
|
|
305
|
+
continue;
|
|
306
|
+
if (def.load.default && typeof data[def.load.name] === "undefined") {
|
|
307
|
+
data[def.load.name] = def.load.default;
|
|
308
|
+
}
|
|
309
|
+
if (def.load.present == "optional" && typeof data[def.load.name] === "undefined") {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
let value = CalcPropInitialValue(objData, name, data[def.load.name], target);
|
|
313
|
+
//If expression set it as getters/setters making it transparent.
|
|
314
|
+
if (value instanceof Expression) {
|
|
315
|
+
SetPropertyToExpression(target, name, value);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
target[name] = value;
|
|
319
|
+
}
|
|
320
|
+
target[DataSymbol][name] = value;
|
|
321
|
+
}
|
|
322
|
+
//We don't have problem with type being enumerable, but
|
|
323
|
+
// having it as false makes comparing values in unit tests
|
|
324
|
+
// slightly easier.
|
|
325
|
+
Reflect.defineProperty(target, DataSymbol, {
|
|
326
|
+
value: data,
|
|
327
|
+
enumerable: false
|
|
328
|
+
});
|
|
329
|
+
return target;
|
|
330
|
+
}
|
|
331
|
+
Loader.InitFromData = InitFromData;
|
|
332
|
+
function MarkInitialised(object) {
|
|
333
|
+
Reflect.defineProperty(object, IsInitialised, {
|
|
334
|
+
value: true,
|
|
335
|
+
enumerable: false
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
Loader.MarkInitialised = MarkInitialised;
|
|
339
|
+
function HasObjectBeenInitialised(obj) {
|
|
340
|
+
var _a;
|
|
341
|
+
return (_a = obj[IsInitialised]) !== null && _a !== void 0 ? _a : false;
|
|
342
|
+
}
|
|
343
|
+
Loader.HasObjectBeenInitialised = HasObjectBeenInitialised;
|
|
344
|
+
function GetExpression(obj, propName) {
|
|
345
|
+
return obj[ExprProperties][propName];
|
|
346
|
+
}
|
|
347
|
+
Loader.GetExpression = GetExpression;
|
|
348
|
+
})(Loader || (Loader = {}));
|
|
349
|
+
__PrivateConstructorSetup.RegisterInitialiser(Loader.InitFromData);
|
|
350
|
+
__PrivateConstructorSetup.RegisterHasBeenInitialised(Loader.HasObjectBeenInitialised);
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ReplicableClass } from "../Tracking/Class.js";
|
|
2
|
+
import { Class, InstantPromise } from "@bikky/smart-collections";
|
|
3
|
+
import { Primitive } from "../Constants/SerialisationTypes.js";
|
|
4
|
+
import type { ReplicableClassConstructor, TypeClass } from "../Tracking/Types.js";
|
|
5
|
+
export declare namespace Progress {
|
|
6
|
+
function RegisterReplInitialisationFunction(callback: (repl: ReplicableClass) => void): void;
|
|
7
|
+
/**
|
|
8
|
+
* This function is designed to be called after every file in the game has finished loading.
|
|
9
|
+
* It completes the setup of each class, it's properties and functions, since often their types
|
|
10
|
+
* reference other classes.
|
|
11
|
+
* It's done in stages so circular references are treated fine.
|
|
12
|
+
*/
|
|
13
|
+
function FinishedAllLoading(): void;
|
|
14
|
+
function ReadyForParameters(key: any): InstantPromise<(ReplicableClass | Primitive)[]>;
|
|
15
|
+
function AwaitReplicableFor(type: Class<any> | ReplicableClassConstructor | TypeClass | string): InstantPromise<(ReplicableClass | Primitive)[]>;
|
|
16
|
+
function FinishReplicableType(type: ReplicableClass): void;
|
|
17
|
+
/**
|
|
18
|
+
* A type collection is an object or string whose value is only used to look up other values.
|
|
19
|
+
*/
|
|
20
|
+
function RegisterAlias(type: any, value: any): void;
|
|
21
|
+
function RegisterPropertyLoad(classConstructor: any, property: Parameters<ReplicableClass["addProperty"]>): void;
|
|
22
|
+
function RegisterPropertyRemove(classConstructor: any, property: Parameters<ReplicableClass["removeProperty"]>): void;
|
|
23
|
+
function RegisterFunctionAdd(classConstructor: any, property: Parameters<ReplicableClass["addFunction"]>): void;
|
|
24
|
+
function OnLoadFinished(callback: () => void): void;
|
|
25
|
+
function OnAnyReplicableReady(callback: (type: ReplicableClass, originalClass: any) => void): void;
|
|
26
|
+
function OnAnyPrimitiveReady(callback: (alias: any, type: Primitive) => void): void;
|
|
27
|
+
function OnAnyAliasReady(callback: (alias: any, types: (ReplicableClass | Primitive)[]) => void): void;
|
|
28
|
+
/**
|
|
29
|
+
* This function is only for testing!
|
|
30
|
+
*/
|
|
31
|
+
function ClearCaches(): void;
|
|
32
|
+
}
|