Various fixes

This commit is contained in:
barsdeveloper
2022-09-09 20:39:08 +02:00
parent 9d424809c9
commit 57ef15c943
13 changed files with 349 additions and 100 deletions

View File

@@ -0,0 +1,22 @@
/**
* @typedef {import("./IEntity").default} IEntity
*/
export default class CalculatedType {
#f
/**
* @param {Function} f
*/
constructor(f) {
this.#f = f
}
/**
* @param {IEntity} entity
*/
calculate(entity) {
return this.f(entity)
}
}

View File

@@ -1,6 +1,7 @@
import Observable from "../Observable"
import TypeInitialization from "./TypeInitialization"
import Utility from "../Utility"
import CalculatedType from "./CalculatedType"
export default class IEntity extends Observable {
@@ -20,26 +21,33 @@ export default class IEntity extends Observable {
Object.getOwnPropertyNames(properties),
Object.getOwnPropertyNames(values ?? {})
)) {
let defaultValue = properties[property]
const defaultType = Utility.getType(defaultValue)
if (!(property in properties)) {
console.warn(`Property ${prefix}${property} is not defined in ${this.constructor.name}`)
console.warn(`Property ${prefix}${property} is not defined in ${this.constructor.name}.attributes`)
} else if (
defaultValue != null
&& !(defaultValue instanceof TypeInitialization && !defaultValue.showDefault)
&& !(property in values)
) {
console.warn(`${this.constructor.name} adds property ${prefix}${property} not defined in the serialized data`)
console.warn(
`${this.constructor.name} adds property ${prefix}${property} not defined in the serialized data`
)
}
// Not instanceof because all objects are instenceof Object, exact match needed
if (defaultType === Object) {
target[property] = {}
defineAllAttributes(target[property], properties[property], values[property], property + ".")
continue
}
/*
* The value can either be:
* - Array: can contain multiple values, its property is assigned multiple times like (X=1, X=4, X="Hello World").
* - Array: can contain multiple values, its property is assigned multiple times like (X=1, X="4").
* - CalculatedType: the exact type depends on the previous attributes assigned to this entity.
* - TypeInitialization: contains the maximum amount of information about the attribute.
* - A type: the default value will be default constructed object without arguments.
* - A proper value.
@@ -50,6 +58,10 @@ export default class IEntity extends Observable {
// We have a value, need nothing more
continue
}
if (defaultValue instanceof CalculatedType) {
defaultValue = defaultValue.calculate(this)
defaultType = Utility.getType(defaultValue)
}
if (defaultValue instanceof TypeInitialization) {
if (!defaultValue.showDefault) {
target[property] = undefined // Declare undefined to preserve the order of attributes
@@ -61,9 +73,6 @@ export default class IEntity extends Observable {
target[property] = []
continue
}
if (defaultValue instanceof Function) {
defaultValue = TypeInitialization.sanitize(new defaultValue(), defaultType)
}
target[property] = TypeInitialization.sanitize(defaultValue, defaultType)
}
}

View File

@@ -30,7 +30,12 @@ export default class PinEntity extends IEntity {
bSerializeAsSinglePrecisionFloat: false,
},
LinkedTo: new TypeInitialization([PinReferenceEntity], false),
DefaultValue: new TypeInitialization(new SerializedType(LinearColorEntity, String), false),
DefaultValue: new TypeInitialization(
new SerializedType([
LinearColorEntity,
]),
false
),
AutogeneratedDefaultValue: new TypeInitialization(String, false),
DefaultObject: new TypeInitialization(ObjectReferenceEntity, false, null),
PersistentGuid: GuidEntity,

View File

@@ -8,7 +8,19 @@ export default class SerializedType {
this.#types = v
}
constructor(...acceptedTypes) {
this.#types = acceptedTypes
#stringFallback
get stringFallback() {
return this.#stringFallback
}
set stringFallback(v) {
this.#stringFallback = v
}
constructor([...acceptedTypes], stringFallback = true) {
this.#types = [...new Set([
...acceptedTypes,
...(stringFallback ? [String] : [])
])]
this.#stringFallback = stringFallback
}
}

View File

@@ -35,7 +35,6 @@ export default class TypeInitialization {
if (targetType === undefined) {
targetType = value?.constructor
}
let wrongType = false
if (
targetType
&& targetType !== SerializedType

10
js/entity/VectorEntity.js Normal file
View File

@@ -0,0 +1,10 @@
import IEntity from "./IEntity"
export default class LinearColorEntity extends IEntity {
static attributes = {
X: Number,
Y: Number,
Z: Number,
}
}