Indexed array entity, user defined pins

This commit is contained in:
barsdeveloper
2023-04-04 22:52:42 +02:00
parent 3e2a20302f
commit 7d84062ff6
8 changed files with 151 additions and 45 deletions

92
dist/ueblueprint.js vendored
View File

@@ -1379,6 +1379,24 @@ class IdentifierEntity extends IEntity {
}
}
/** @typedef {import("./IEntity").AnyValueConstructor<*>} AnyValueConstructor */
class IndexedArray {
#type
get type() {
return this.#type
}
value = []
/** @param {AnyValueConstructor} type */
constructor(type, value = []) {
this.#type = type;
this.value = value;
}
}
class Integer64Entity extends IEntity {
static attributes = {
@@ -2591,6 +2609,13 @@ class VariableReferenceEntity extends IEntity {
}
}
/** @typedef {import("./IEntity.js").AnyValue} AnyValue */
class UserDefinedPinEntity extends IEntity {
static lookbehind = "UserDefinedPin"
}
class ObjectEntity extends IEntity {
static attributes = {
@@ -2774,7 +2799,7 @@ class ObjectEntity extends IEntity {
showDefault: false,
},
CustomProperties: {
type: [PinEntity]
type: [new UnionType(PinEntity, UserDefinedPinEntity)]
},
}
@@ -3025,7 +3050,7 @@ class ObjectEntity extends IEntity {
case Configuration.nodeType.variableSet:
return "SET"
case Configuration.nodeType.switchEnum:
return `Switch on ${this.Enum?.getName()}`
return `Switch on ${this.Enum?.getName() ?? "Enum"}`
}
const keyNameSymbol = this.getHIDAttribute();
if (keyNameSymbol) {
@@ -3441,6 +3466,9 @@ class Grammar {
case SymbolEntity:
result = this.symbolEntity;
break
case UserDefinedPinEntity:
result = this.userDefinedPinEntity;
break
case VariableReferenceEntity:
result = this.variableReferenceEntity;
break
@@ -3541,10 +3569,16 @@ class Grammar {
static integerEntity = P.lazy(() => this.integer.map(v => new IntegerEntity(v)))
static invariantTextEntity = P.lazy(() =>
P.seq(
P.regex(new RegExp(`${InvariantTextEntity.lookbehind}\\s*`)),
this.grammarFor(InvariantTextEntity.attributes.value)
)
P.alt(
P.seq(
P.regex(new RegExp(`${InvariantTextEntity.lookbehind}\\s*\\(`)),
this.grammarFor(InvariantTextEntity.attributes.value),
P.regex(/\s*\)/)
)
.map(([_0, value, _2]) => value),
P.regex(new RegExp(InvariantTextEntity.lookbehind)) // InvariantTextEntity can not have arguments
.map(() => "")
).map(value => new InvariantTextEntity(value))
)
static keyBindingEntity = P.lazy(() =>
@@ -3681,6 +3715,8 @@ class Grammar {
static symbolEntity = P.lazy(() => this.symbol.map(v => new SymbolEntity(v)))
static userDefinedPinEntity = P.lazy(() => this.createEntityGrammar(UserDefinedPinEntity))
static variableReferenceEntity = P.lazy(() => this.createEntityGrammar(VariableReferenceEntity))
static vector2DEntity = P.lazy(() => this.createEntityGrammar(Vector2DEntity, false))
@@ -3725,6 +3761,7 @@ class Grammar {
this.string,
this.localizedTextEntity,
this.invariantTextEntity,
this.formatTextEntity,
this.pinReferenceEntity,
this.vectorEntity,
this.linearColorEntity,
@@ -3739,7 +3776,7 @@ class Grammar {
static customProperty = P.lazy(() =>
P.seq(
P.regex(/CustomProperties\s+/),
this.pinEntity,
this.grammarFor(undefined, ObjectEntity.attributes.CustomProperties.type[0]),
).map(([_0, pin]) => values => {
if (!values.CustomProperties) {
values.CustomProperties = [];
@@ -3759,12 +3796,9 @@ class Grammar {
.chain(([symbol, _1]) =>
this.grammarFor(ObjectEntity.attributes[symbol])
.map(currentValue =>
values => {
if (!values[symbol]) {
values[symbol] = [];
}
values[symbol].push(currentValue);
})
values => (values[symbol] ??= new IndexedArray(currentValue.constructor))
.value.push(currentValue)
)
)
})
@@ -3947,14 +3981,29 @@ class ISerializer {
const value = entity[key];
if (value !== undefined && this.showProperty(entity, key)) {
const isSerialized = Utility.isSerialized(entity, key);
if (value instanceof IndexedArray) {
value.value.forEach((value, i) =>
result += (result.length ? this.attributeSeparator : "")
+ this.attributePrefix
+ Utility.decodeKeyName(this.attributeKeyPrinter(key))
+ `(${i})`
+ this.attributeValueConjunctionSign
+ (
isSerialized
? `"${this.writeValue(value, true)}"`
: this.writeValue(value, insideString)
)
);
continue
}
result += (result.length ? this.attributeSeparator : "")
+ this.attributePrefix
+ Utility.decodeKeyName(this.attributeKeyPrinter(key))
+ this.attributeValueConjunctionSign
+ (
isSerialized
? `"${this.writeValue(entity, key, true)}"`
: this.writeValue(entity, key, insideString)
? `"${this.writeValue(value, true)}"`
: this.writeValue(value, insideString)
);
}
}
@@ -3967,19 +4016,20 @@ class ISerializer {
/**
* @protected
* @param {String} key
* @param {Boolean} insideString
*/
writeValue(entity, key, insideString) {
const value = entity[key];
writeValue(value, insideString) {
const type = Utility.getType(value);
// @ts-expect-error
const serializer = SerializerFactory.getSerializer(type);
if (!serializer) {
throw new Error(`Unknown value type "${type.name}", a serializer must be registered in the SerializerFactory class, check initializeSerializerFactory.js`)
throw new Error(
`Unknown value type "${type.name}", a serializer must be registered in the SerializerFactory class, `
+ "check initializeSerializerFactory.js"
)
}
return serializer.write(
entity[key],
value,
insideString
)
}
@@ -4041,7 +4091,7 @@ class ObjectSerializer extends ISerializer {
* @param {Boolean} insideString
*/
write(entity, insideString) {
let result = `Begin Object Class=${entity.Class.path} Name=${this.writeValue(entity, "Name", insideString)}\n`
let result = `Begin Object Class=${entity.Class.path} Name=${this.writeValue(entity.Name, insideString)}\n`
+ super.write(entity, insideString)
+ entity.CustomProperties.map(pin =>
this.attributeSeparator

File diff suppressed because one or more lines are too long

17
js/entity/IndexedArray.js Normal file
View File

@@ -0,0 +1,17 @@
/** @typedef {import("./IEntity").AnyValueConstructor<*>} AnyValueConstructor */
export default class IndexedArray {
#type
get type() {
return this.#type
}
value = []
/** @param {AnyValueConstructor} type */
constructor(type, value = []) {
this.#type = type
this.value = value
}
}

View File

@@ -10,6 +10,8 @@ import ObjectReferenceEntity from "./ObjectReferenceEntity.js"
import PinEntity from "./PinEntity.js"
import SVGIcon from "../SVGIcon.js"
import SymbolEntity from "./SymbolEntity.js"
import UnionType from "./UnionType.js"
import UserDefinedPinEntity from "./UserDefinedPinEntity.js"
import Utility from "../Utility.js"
import VariableReferenceEntity from "./VariableReferenceEntity.js"
@@ -196,7 +198,7 @@ export default class ObjectEntity extends IEntity {
showDefault: false,
},
CustomProperties: {
type: [PinEntity]
type: [new UnionType(PinEntity, UserDefinedPinEntity)]
},
}

View File

@@ -0,0 +1,9 @@
import IEntity from "./IEntity.js"
import PinEntity from "./PinEntity.js"
/** @typedef {import("./IEntity.js").AnyValue} AnyValue */
export default class UserDefinedPinEntity extends IEntity {
static lookbehind = "UserDefinedPin"
}

View File

@@ -4,6 +4,7 @@ import FormatTextEntity from "../entity/FormatTextEntity.js"
import FunctionReferenceEntity from "../entity/FunctionReferenceEntity.js"
import GuidEntity from "../entity/GuidEntity.js"
import IdentifierEntity from "../entity/IdentifierEntity.js"
import IndexedArray from "../entity/IndexedArray.js"
import Integer64Entity from "../entity/Integer64Entity.js"
import IntegerEntity from "../entity/IntegerEntity.js"
import InvariantTextEntity from "../entity/InvariantTextEntity.js"
@@ -27,6 +28,7 @@ import SimpleSerializationVectorEntity from "../entity/SimpleSerializationVector
import SymbolEntity from "../entity/SymbolEntity.js"
import UnionType from "../entity/UnionType.js"
import UnknownKeysEntity from "../entity/UnknownKeysEntity.js"
import UserDefinedPinEntity from "../entity/UserDefinedPinEntity.js"
import Utility from "../Utility.js"
import VariableReferenceEntity from "../entity/VariableReferenceEntity.js"
import Vector2DEntity from "../entity/Vector2DEntity.js"
@@ -242,6 +244,9 @@ export default class Grammar {
case SymbolEntity:
result = this.symbolEntity
break
case UserDefinedPinEntity:
result = this.userDefinedPinEntity
break
case VariableReferenceEntity:
result = this.variableReferenceEntity
break
@@ -342,10 +347,16 @@ export default class Grammar {
static integerEntity = P.lazy(() => this.integer.map(v => new IntegerEntity(v)))
static invariantTextEntity = P.lazy(() =>
P.seq(
P.regex(new RegExp(`${InvariantTextEntity.lookbehind}\\s*`)),
this.grammarFor(InvariantTextEntity.attributes.value)
)
P.alt(
P.seq(
P.regex(new RegExp(`${InvariantTextEntity.lookbehind}\\s*\\(`)),
this.grammarFor(InvariantTextEntity.attributes.value),
P.regex(/\s*\)/)
)
.map(([_0, value, _2]) => value),
P.regex(new RegExp(InvariantTextEntity.lookbehind)) // InvariantTextEntity can not have arguments
.map(() => "")
).map(value => new InvariantTextEntity(value))
)
static keyBindingEntity = P.lazy(() =>
@@ -482,6 +493,8 @@ export default class Grammar {
static symbolEntity = P.lazy(() => this.symbol.map(v => new SymbolEntity(v)))
static userDefinedPinEntity = P.lazy(() => this.createEntityGrammar(UserDefinedPinEntity))
static variableReferenceEntity = P.lazy(() => this.createEntityGrammar(VariableReferenceEntity))
static vector2DEntity = P.lazy(() => this.createEntityGrammar(Vector2DEntity, false))
@@ -526,6 +539,7 @@ export default class Grammar {
this.string,
this.localizedTextEntity,
this.invariantTextEntity,
this.formatTextEntity,
this.pinReferenceEntity,
this.vectorEntity,
this.linearColorEntity,
@@ -540,7 +554,7 @@ export default class Grammar {
static customProperty = P.lazy(() =>
P.seq(
P.regex(/CustomProperties\s+/),
this.pinEntity,
this.grammarFor(undefined, ObjectEntity.attributes.CustomProperties.type[0]),
).map(([_0, pin]) => values => {
if (!values.CustomProperties) {
values.CustomProperties = []
@@ -560,12 +574,9 @@ export default class Grammar {
.chain(([symbol, _1]) =>
this.grammarFor(ObjectEntity.attributes[symbol])
.map(currentValue =>
values => {
if (!values[symbol]) {
values[symbol] = []
}
values[symbol].push(currentValue)
})
values => (values[symbol] ??= new IndexedArray(currentValue.constructor))
.value.push(currentValue)
)
)
})

View File

@@ -1,3 +1,4 @@
import IndexedArray from "../entity/IndexedArray.js"
import SerializerFactory from "./SerializerFactory.js"
import Utility from "../Utility.js"
@@ -66,14 +67,29 @@ export default class ISerializer {
const value = entity[key]
if (value !== undefined && this.showProperty(entity, key)) {
const isSerialized = Utility.isSerialized(entity, key)
if (value instanceof IndexedArray) {
value.value.forEach((value, i) =>
result += (result.length ? this.attributeSeparator : "")
+ this.attributePrefix
+ Utility.decodeKeyName(this.attributeKeyPrinter(key))
+ `(${i})`
+ this.attributeValueConjunctionSign
+ (
isSerialized
? `"${this.writeValue(value, true)}"`
: this.writeValue(value, insideString)
)
)
continue
}
result += (result.length ? this.attributeSeparator : "")
+ this.attributePrefix
+ Utility.decodeKeyName(this.attributeKeyPrinter(key))
+ this.attributeValueConjunctionSign
+ (
isSerialized
? `"${this.writeValue(entity, key, true)}"`
: this.writeValue(entity, key, insideString)
? `"${this.writeValue(value, true)}"`
: this.writeValue(value, insideString)
)
}
}
@@ -86,19 +102,20 @@ export default class ISerializer {
/**
* @protected
* @param {String} key
* @param {Boolean} insideString
*/
writeValue(entity, key, insideString) {
const value = entity[key]
writeValue(value, insideString) {
const type = Utility.getType(value)
// @ts-expect-error
const serializer = SerializerFactory.getSerializer(type)
if (!serializer) {
throw new Error(`Unknown value type "${type.name}", a serializer must be registered in the SerializerFactory class, check initializeSerializerFactory.js`)
throw new Error(
`Unknown value type "${type.name}", a serializer must be registered in the SerializerFactory class, `
+ "check initializeSerializerFactory.js"
)
}
return serializer.write(
entity[key],
value,
insideString
)
}

View File

@@ -47,7 +47,7 @@ export default class ObjectSerializer extends ISerializer {
* @param {Boolean} insideString
*/
write(entity, insideString) {
let result = `Begin Object Class=${entity.Class.path} Name=${this.writeValue(entity, "Name", insideString)}\n`
let result = `Begin Object Class=${entity.Class.path} Name=${this.writeValue(entity.Name, insideString)}\n`
+ super.write(entity, insideString)
+ entity.CustomProperties.map(pin =>
this.attributeSeparator