mirror of
https://github.com/barsdeveloper/ueblueprint.git
synced 2026-02-03 23:55:04 +08:00
Inlineable objects
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import GeneralSerializer from "./GeneralSerializer.js"
|
||||
import ISerializer from "./ISerializer.js"
|
||||
|
||||
/**
|
||||
* @typedef {import("../entity/IEntity").AnyValue} AnyValue
|
||||
@@ -7,9 +7,9 @@ import GeneralSerializer from "./GeneralSerializer.js"
|
||||
|
||||
/**
|
||||
* @template {AnyValue} T
|
||||
* @extends {GeneralSerializer<T>}
|
||||
* @extends {ISerializer<T>}
|
||||
*/
|
||||
export default class CustomSerializer extends GeneralSerializer {
|
||||
export default class CustomSerializer extends ISerializer {
|
||||
|
||||
#objectWriter
|
||||
|
||||
@@ -18,7 +18,7 @@ export default class CustomSerializer extends GeneralSerializer {
|
||||
* @param {AnyValueConstructor} entityType
|
||||
*/
|
||||
constructor(objectWriter, entityType) {
|
||||
super(undefined, entityType)
|
||||
super(entityType)
|
||||
this.#objectWriter = objectWriter
|
||||
}
|
||||
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
import Grammar from "./Grammar.js"
|
||||
import ISerializer from "./ISerializer.js"
|
||||
|
||||
/**
|
||||
* @typedef {import("../entity/IEntity").default} IEntity
|
||||
* @typedef {import("../entity/IEntity").AnyValue} AnyValue
|
||||
* @typedef {import("../entity/IEntity").AnyValueConstructor<*>} AnyValueConstructor
|
||||
*/
|
||||
|
||||
/**
|
||||
* @template {AnyValue} T
|
||||
* @extends ISerializer<T>
|
||||
*/
|
||||
export default class GeneralSerializer extends ISerializer {
|
||||
|
||||
/**
|
||||
* @param {(value: String, entity: T) => String} wrap
|
||||
* @param {AnyValueConstructor} entityType
|
||||
*/
|
||||
constructor(wrap, entityType, attributePrefix, attributeSeparator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter) {
|
||||
wrap = wrap ?? (v => `(${v})`)
|
||||
super(entityType, attributePrefix, attributeSeparator, trailingSeparator, attributeValueConjunctionSign, attributeKeyPrinter)
|
||||
this.wrap = wrap
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} value
|
||||
* @returns {T}
|
||||
*/
|
||||
doRead(value) {
|
||||
let grammar = Grammar.grammarFor(undefined, this.entityType)
|
||||
const parseResult = grammar.parse(value)
|
||||
if (!parseResult.status) {
|
||||
throw new Error(`Error when trying to parse the entity ${this.entityType.prototype.constructor.name}.`)
|
||||
}
|
||||
return parseResult.value
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {T} entity
|
||||
* @param {Boolean} insideString
|
||||
* @returns {String}
|
||||
*/
|
||||
doWrite(entity, insideString = false) {
|
||||
let result = this.wrap(super.doWrite(entity, insideString), entity)
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
import Grammar from "./Grammar.js"
|
||||
import IndexedArray from "../entity/IndexedArray.js"
|
||||
import SerializerFactory from "./SerializerFactory.js"
|
||||
import Utility from "../Utility.js"
|
||||
@@ -11,9 +12,15 @@ import Utility from "../Utility.js"
|
||||
/** @template {AnyValue} T */
|
||||
export default class ISerializer {
|
||||
|
||||
/** @type {(v: String, entityType: AnyValueConstructor) => String} */
|
||||
static bracketsWrapped = ((v, entityType) => `(${v})`)
|
||||
/** @type {(v: String, entityType: AnyValueConstructor) => String} */
|
||||
static notWrapped = ((v, entityType) => v)
|
||||
|
||||
/** @param {AnyValueConstructor} entityType */
|
||||
constructor(
|
||||
entityType,
|
||||
wrap = ISerializer.bracketsWrapped,
|
||||
attributePrefix = "",
|
||||
attributeSeparator = ",",
|
||||
trailingSeparator = false,
|
||||
@@ -21,6 +28,7 @@ export default class ISerializer {
|
||||
attributeKeyPrinter = k => k
|
||||
) {
|
||||
this.entityType = entityType
|
||||
this.wrap = wrap
|
||||
this.attributePrefix = attributePrefix
|
||||
this.attributeSeparator = attributeSeparator
|
||||
this.trailingSeparator = trailingSeparator
|
||||
@@ -47,7 +55,12 @@ export default class ISerializer {
|
||||
* @returns {T}
|
||||
*/
|
||||
doRead(value) {
|
||||
throw new Error("Not implemented")
|
||||
let grammar = Grammar.grammarFor(undefined, this.entityType)
|
||||
const parseResult = grammar.parse(value)
|
||||
if (!parseResult.status) {
|
||||
throw new Error(`Error when trying to parse the entity ${this.entityType.prototype.constructor.name}.`)
|
||||
}
|
||||
return parseResult.value
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +69,16 @@ export default class ISerializer {
|
||||
* @param {Boolean} insideString
|
||||
* @returns {String}
|
||||
*/
|
||||
doWrite(entity, insideString) {
|
||||
doWrite(
|
||||
entity,
|
||||
insideString,
|
||||
wrap = this.wrap,
|
||||
attributePrefix = this.attributePrefix,
|
||||
attributeSeparator = this.attributeSeparator,
|
||||
trailingSeparator = this.trailingSeparator,
|
||||
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
|
||||
attributeKeyPrinter = this.attributeKeyPrinter
|
||||
) {
|
||||
let result = ""
|
||||
const attributes = /** @type {EntityConstructor} */(entity.constructor).attributes ?? {}
|
||||
const keys = Utility.mergeArrays(
|
||||
@@ -67,23 +89,35 @@ 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.doWriteValue(value, true)}"`
|
||||
: this.doWriteValue(value, insideString)
|
||||
)
|
||||
result += (result.length ? attributeSeparator : "")
|
||||
if (attributes[key]?.inlined) {
|
||||
result += this.doWrite(
|
||||
value,
|
||||
insideString,
|
||||
ISerializer.notWrapped,
|
||||
`${attributePrefix}${key}.`,
|
||||
attributeSeparator,
|
||||
trailingSeparator,
|
||||
attributeValueConjunctionSign,
|
||||
attributeKeyPrinter
|
||||
)
|
||||
continue
|
||||
}
|
||||
result += (result.length ? this.attributeSeparator : "")
|
||||
+ this.attributePrefix
|
||||
if (value instanceof IndexedArray) {
|
||||
result += this.doWrite(
|
||||
value,
|
||||
insideString,
|
||||
wrap,
|
||||
attributePrefix,
|
||||
attributeSeparator,
|
||||
trailingSeparator,
|
||||
attributeValueConjunctionSign,
|
||||
index => `(${index})`
|
||||
)
|
||||
continue
|
||||
}
|
||||
result +=
|
||||
attributePrefix
|
||||
+ Utility.decodeKeyName(this.attributeKeyPrinter(key))
|
||||
+ this.attributeValueConjunctionSign
|
||||
+ (
|
||||
@@ -97,7 +131,7 @@ export default class ISerializer {
|
||||
// append separator at the end if asked and there was printed content
|
||||
result += this.attributeSeparator
|
||||
}
|
||||
return result
|
||||
return wrap(result, entity.constructor)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,7 @@ import SerializerFactory from "./SerializerFactory.js"
|
||||
export default class ObjectSerializer extends ISerializer {
|
||||
|
||||
constructor() {
|
||||
super(ObjectEntity, " ", "\n", false)
|
||||
super(ObjectEntity, undefined, " ", "\n", false)
|
||||
}
|
||||
|
||||
showProperty(entity, key) {
|
||||
@@ -43,10 +43,24 @@ export default class ObjectSerializer extends ISerializer {
|
||||
}
|
||||
|
||||
/**
|
||||
* @protected
|
||||
* @param {ObjectEntity} entity
|
||||
* @param {Boolean} insideString
|
||||
* @returns {String}
|
||||
*/
|
||||
doWrite(entity, insideString) {
|
||||
doWrite(
|
||||
entity,
|
||||
insideString,
|
||||
wrap = this.wrap,
|
||||
attributePrefix = this.attributePrefix,
|
||||
attributeSeparator = this.attributeSeparator,
|
||||
trailingSeparator = this.trailingSeparator,
|
||||
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
|
||||
attributeKeyPrinter = this.attributeKeyPrinter
|
||||
) {
|
||||
if (!(entity instanceof ObjectEntity)) {
|
||||
return super.doWrite(entity, insideString)
|
||||
}
|
||||
let result = `Begin Object Class=${entity.Class.path} Name=${this.doWriteValue(entity.Name, insideString)}\n`
|
||||
+ super.doWrite(entity, insideString)
|
||||
+ entity.CustomProperties.map(pin =>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import ISerializer from "./ISerializer.js"
|
||||
import Utility from "../Utility.js"
|
||||
import GeneralSerializer from "./GeneralSerializer.js"
|
||||
|
||||
/**
|
||||
* @typedef {import("../entity/IEntity").AnyValue} AnyValue
|
||||
@@ -8,13 +8,13 @@ import GeneralSerializer from "./GeneralSerializer.js"
|
||||
|
||||
/**
|
||||
* @template {AnyValue} T
|
||||
* @extends {GeneralSerializer<T>}
|
||||
* @extends {ISerializer<T>}
|
||||
*/
|
||||
export default class ToStringSerializer extends GeneralSerializer {
|
||||
export default class ToStringSerializer extends ISerializer {
|
||||
|
||||
/** @param {AnyValueConstructor} entityType */
|
||||
constructor(entityType) {
|
||||
super(undefined, entityType)
|
||||
super(entityType)
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,12 +2,12 @@ import ByteEntity from "../entity/ByteEntity.js"
|
||||
import CustomSerializer from "./CustomSerializer.js"
|
||||
import EnumEntity from "../entity/EnumEntity.js"
|
||||
import FunctionReferenceEntity from "../entity/FunctionReferenceEntity.js"
|
||||
import GeneralSerializer from "./GeneralSerializer.js"
|
||||
import GuidEntity from "../entity/GuidEntity.js"
|
||||
import IdentifierEntity from "../entity/IdentifierEntity.js"
|
||||
import Integer64Entity from "../entity/Integer64Entity.js"
|
||||
import IntegerEntity from "../entity/IntegerEntity.js"
|
||||
import InvariantTextEntity from "../entity/InvariantTextEntity.js"
|
||||
import ISerializer from "./ISerializer.js"
|
||||
import KeyBindingEntity from "../entity/KeyBindingEntity.js"
|
||||
import LinearColorEntity from "../entity/LinearColorEntity.js"
|
||||
import LocalizedTextEntity from "../entity/LocalizedTextEntity.js"
|
||||
@@ -40,8 +40,6 @@ import VectorEntity from "../entity/VectorEntity.js"
|
||||
|
||||
export default function initializeSerializerFactory() {
|
||||
|
||||
const bracketsWrapped = v => `(${v})`
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
null,
|
||||
new CustomSerializer(
|
||||
@@ -98,7 +96,7 @@ export default function initializeSerializerFactory() {
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
FunctionReferenceEntity,
|
||||
new GeneralSerializer(bracketsWrapped, FunctionReferenceEntity)
|
||||
new ISerializer(FunctionReferenceEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
@@ -123,27 +121,27 @@ export default function initializeSerializerFactory() {
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
InvariantTextEntity,
|
||||
new GeneralSerializer(v => `${InvariantTextEntity.lookbehind}(${v})`, InvariantTextEntity, "", ", ", false, "", _ => "")
|
||||
new ISerializer(InvariantTextEntity, v => `${InvariantTextEntity.lookbehind}(${v})`, "", ", ", false, "", _ => "")
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
KeyBindingEntity,
|
||||
new GeneralSerializer(bracketsWrapped, KeyBindingEntity)
|
||||
new ISerializer(KeyBindingEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
LinearColorEntity,
|
||||
new GeneralSerializer(bracketsWrapped, LinearColorEntity)
|
||||
new ISerializer(LinearColorEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
LocalizedTextEntity,
|
||||
new GeneralSerializer(v => `${LocalizedTextEntity.lookbehind}(${v})`, LocalizedTextEntity, "", ", ", false, "", _ => "")
|
||||
new ISerializer(LocalizedTextEntity, v => `${LocalizedTextEntity.lookbehind}(${v})`, "", ", ", false, "", _ => "")
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
MacroGraphReferenceEntity,
|
||||
new GeneralSerializer(bracketsWrapped, MacroGraphReferenceEntity)
|
||||
new ISerializer(MacroGraphReferenceEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
@@ -175,17 +173,17 @@ export default function initializeSerializerFactory() {
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
PinEntity,
|
||||
new GeneralSerializer(v => `${PinEntity.lookbehind} (${v})`, PinEntity, "", ",", true)
|
||||
new ISerializer(PinEntity, v => `${PinEntity.lookbehind} (${v})`, "", ",", true)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
PinReferenceEntity,
|
||||
new GeneralSerializer(v => v, PinReferenceEntity, "", " ", false, "", _ => "")
|
||||
new ISerializer(PinReferenceEntity, v => v, "", " ", false, "", _ => "")
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
TerminalTypeEntity,
|
||||
new GeneralSerializer(bracketsWrapped, TerminalTypeEntity)
|
||||
new ISerializer(TerminalTypeEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
@@ -195,7 +193,7 @@ export default function initializeSerializerFactory() {
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
RotatorEntity,
|
||||
new GeneralSerializer(bracketsWrapped, RotatorEntity)
|
||||
new ISerializer(RotatorEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
@@ -241,21 +239,21 @@ export default function initializeSerializerFactory() {
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
UnknownKeysEntity,
|
||||
new GeneralSerializer((string, entity) => `${entity.lookbehind ?? ""}(${string})`, UnknownKeysEntity)
|
||||
new ISerializer(UnknownKeysEntity, (string, entity) => `${entity.lookbehind ?? ""}(${string})`)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
VariableReferenceEntity,
|
||||
new GeneralSerializer(bracketsWrapped, VariableReferenceEntity)
|
||||
new ISerializer(VariableReferenceEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
Vector2DEntity,
|
||||
new GeneralSerializer(bracketsWrapped, Vector2DEntity)
|
||||
new ISerializer(Vector2DEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
|
||||
SerializerFactory.registerSerializer(
|
||||
VectorEntity,
|
||||
new GeneralSerializer(bracketsWrapped, VectorEntity)
|
||||
new ISerializer(VectorEntity, ISerializer.bracketsWrapped)
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user