Remove serialization bits

This commit is contained in:
barsdeveloper
2024-06-05 00:28:28 +02:00
parent 6ca966e176
commit ad8305ca52
15 changed files with 105 additions and 1473 deletions

View File

@@ -1,29 +0,0 @@
import Serializer from "./Serializer.js"
/**
* @template {AttributeConstructor<Attribute>} T
* @extends {Serializer<T>}
*/
export default class CustomSerializer extends Serializer {
#objectWriter
/**
* @param {(v: ConstructedType<T>, insideString: Boolean) => String} objectWriter
* @param {T} entityType
*/
constructor(objectWriter, entityType) {
super(entityType)
this.#objectWriter = objectWriter
}
/**
* @param {ConstructedType<T>} entity
* @param {Boolean} insideString
* @returns {String}
*/
doWrite(entity, insideString, indentation = "") {
let result = this.#objectWriter(entity, insideString)
return result
}
}

View File

@@ -5,7 +5,6 @@ import AlternativesEntity from "../entity/AlternativesEntity.js"
import AttributeInfo from "../entity/AttributeInfo.js"
import IEntity from "../entity/IEntity.js"
import MirroredEntity from "../entity/MirroredEntity.js"
import Serializable from "./Serializable.js"
export default class Grammar {
@@ -70,75 +69,6 @@ export default class Grammar {
/* --- Factory --- */
/**
* @template T
* @param {AttributeInfo<T>} attribute
* @param {Parsernostrum<any>} defaultGrammar
* @returns {Parsernostrum<T>}
*/
static grammarFor(attribute, type = attribute?.type, defaultGrammar = this.unknownValue) {
let result = defaultGrammar
if (type === Array || type instanceof Array) {
if (attribute?.inlined) {
return this.grammarFor(undefined, type[0])
}
result = Parsernostrum.seq(
Parsernostrum.reg(/\(\s*/),
this.grammarFor(undefined, type[0]).sepBy(this.commaSeparation).opt(),
Parsernostrum.reg(/\s*(?:,\s*)?\)/),
).map(([_0, values, _3]) => values instanceof Array ? values : [])
} else if (type instanceof Union) {
result = type.values
.map(v => this.grammarFor(undefined, v))
.reduce((acc, cur) => !cur || cur === this.unknownValue || acc === this.unknownValue
? this.unknownValue
: Parsernostrum.alt(acc, cur)
)
} else if (type instanceof MirroredEntity) {
// @ts-expect-error
return this.grammarFor(undefined, type.getTargetType())
.map(v => new MirroredEntity(type.type, () => v))
} else if (attribute?.constructor === Object) {
result = this.grammarFor(undefined, type)
} else {
switch (type) {
case Boolean:
result = this.boolean
break
case null:
result = this.null
break
case Number:
result = this.number
break
case BigInt:
result = this.bigInt
break
case String:
result = this.string
break
default:
if (/** @type {AttributeConstructor<any>} */(type)?.prototype instanceof Serializable) {
result = /** @type {typeof Serializable} */(type).grammar
}
}
}
if (attribute) {
if (attribute.serialized && type.constructor !== String) {
if (result == this.unknownValue) {
result = this.string
} else {
result = Parsernostrum.seq(Parsernostrum.str('"'), result, Parsernostrum.str('"')).map(([_0, value, _2]) => value)
}
}
if (attribute.nullable) {
result = Parsernostrum.alt(result, this.null)
}
}
return result
}
/**
* @param {typeof IEntity} entityType
* @param {String[]} key
@@ -225,7 +155,4 @@ export default class Grammar {
: Parsernostrum.success().map(() => new entityType(values))
})
}
/** @type {Parsernostrum<any>} */
static unknownValue // Defined in initializeSerializerFactor to avoid circular include
}

View File

@@ -1,104 +0,0 @@
import Configuration from "../Configuration.js"
import AttributeInfo from "../entity/AttributeInfo.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import PinEntity from "../entity/PinEntity.js"
import Grammar from "./Grammar.js"
import Serializer from "./Serializer.js"
import SerializerFactory from "./SerializerFactory.js"
/** @extends Serializer<ObjectEntityConstructor> */
export default class ObjectSerializer extends Serializer {
constructor(entityType = ObjectEntity) {
super(entityType, undefined, "\n", true, undefined, Serializer.same)
}
showProperty(entity, key) {
switch (key) {
case "Class":
case "Name":
case "Archetype":
case "ExportPath":
case "CustomProperties":
// Serielized separately, check doWrite()
return false
}
return super.showProperty(entity, key)
}
/** @param {ObjectEntity} value */
write(value, insideString = false) {
return this.doWrite(value, insideString) + "\n"
}
/** @param {String} value */
doRead(value) {
return Grammar.grammarFor(undefined, this.entityType).parse(value)
}
/**
* @param {String} value
* @returns {ObjectEntity[]}
*/
readMultiple(value) {
return ObjectEntity.getMultipleObjectsGrammar().parse(value)
}
/**
* @param {ObjectEntity} entity
* @param {Boolean} insideString
* @returns {String}
*/
doWrite(
entity,
insideString,
indentation = "",
wrap = this.wrap,
attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
attributeKeyPrinter = this.attributeKeyPrinter,
) {
const moreIndentation = indentation + Configuration.indentation
if (!(entity instanceof ObjectEntity)) {
return super.doWrite(
entity,
insideString,
indentation,
wrap,
attributeSeparator,
trailingSeparator,
attributeValueConjunctionSign,
// @ts-expect-error
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
)
}
let result = indentation + "Begin Object"
+ (entity.Class?.type || entity.Class?.path ? ` Class=${this.doWriteValue(entity.Class, insideString)}` : "")
+ (entity.Name ? ` Name=${this.doWriteValue(entity.Name, insideString)}` : "")
+ (entity.Archetype ? ` Archetype=${this.doWriteValue(entity.Archetype, insideString)}` : "")
+ (entity.ExportPath?.type || entity.ExportPath?.path ? ` ExportPath=${this.doWriteValue(entity.ExportPath, insideString)}` : "")
+ "\n"
+ super.doWrite(
entity,
insideString,
moreIndentation,
wrap,
attributeSeparator,
true,
attributeValueConjunctionSign,
key => entity[key] instanceof ObjectEntity ? "" : attributeKeyPrinter(key)
)
+ (!AttributeInfo.getAttribute(entity, "CustomProperties", "ignored")
? entity.getCustomproperties().map(pin =>
moreIndentation
+ attributeKeyPrinter("CustomProperties ")
+ SerializerFactory.getSerializer(PinEntity).doWrite(pin, insideString)
+ this.attributeSeparator
).join("")
: ""
)
+ indentation + "End Object"
return result
}
}

View File

@@ -1,7 +0,0 @@
import Parsernostrum from "parsernostrum"
export default class Serializable {
/** @type {Parsernostrum<any>} */
static grammar = Parsernostrum.failure()
}

View File

@@ -1,168 +0,0 @@
import Utility from "../Utility.js"
import AttributeInfo from "../entity/AttributeInfo.js"
import IEntity from "../entity/IEntity.js"
import Grammar from "./Grammar.js"
import SerializerFactory from "./SerializerFactory.js"
/** @template {AttributeConstructor<Attribute>} T */
export default class Serializer {
/** @type {(v: String) => String} */
static same = v => v
/** @type {(entity: Attribute, serialized: String) => String} */
static notWrapped = (entity, serialized) => serialized
/** @type {(entity: Attribute, serialized: String) => String} */
static bracketsWrapped = (entity, serialized) => `(${serialized})`
/** @param {T} entityType */
constructor(
entityType,
/** @type {(entity: ConstructedType<T>, serialized: String) => String} */
wrap = (entity, serialized) => serialized,
attributeSeparator = ",",
trailingSeparator = false,
attributeValueConjunctionSign = "=",
attributeKeyPrinter = Serializer.same
) {
this.entityType = entityType
this.wrap = wrap
this.attributeSeparator = attributeSeparator
this.trailingSeparator = trailingSeparator
this.attributeValueConjunctionSign = attributeValueConjunctionSign
this.attributeKeyPrinter = attributeKeyPrinter
}
/**
* @param {String} value
* @returns {ConstructedType<T>}
*/
read(value) {
return this.doRead(value.trim())
}
/** @param {ConstructedType<T>} value */
write(value, insideString = false) {
return this.doWrite(value, insideString)
}
/**
* @param {String} value
* @returns {ConstructedType<T>}
*/
doRead(value) {
let grammar = Grammar.grammarFor(undefined, this.entityType)
const parseResult = grammar.run(value)
if (!parseResult.status) {
throw new Error(
this.entityType
? `Error when trying to parse the entity ${this.entityType.prototype.constructor.name}`
: "Error when trying to parse null"
)
}
return parseResult.value
}
/**
* @param {ConstructedType<T>} entity
* @param {Boolean} insideString
* @returns {String}
*/
doWrite(
entity,
insideString = false,
indentation = "",
wrap = this.wrap,
attributeSeparator = this.attributeSeparator,
trailingSeparator = this.trailingSeparator,
attributeValueConjunctionSign = this.attributeValueConjunctionSign,
attributeKeyPrinter = this.attributeKeyPrinter
) {
let result = ""
const keys = entity._keys ?? Object.keys(entity)
let first = true
for (const key of keys) {
const value = entity[key]
if (value !== undefined && this.showProperty(entity, key)) {
let keyValue = entity instanceof Array ? `(${key})` : key
if (AttributeInfo.getAttribute(entity, key, "quoted")) {
keyValue = `"${keyValue}"`
}
const isSerialized = AttributeInfo.getAttribute(entity, key, "serialized")
if (first) {
first = false
} else {
result += attributeSeparator
}
if (AttributeInfo.getAttribute(entity, key, "inlined")) {
result += this.doWrite(
value,
insideString,
indentation,
Serializer.notWrapped,
attributeSeparator,
false,
attributeValueConjunctionSign,
AttributeInfo.getAttribute(entity, key, "type") instanceof Array
? k => attributeKeyPrinter(`${keyValue}${k}`)
: k => attributeKeyPrinter(`${keyValue}.${k}`)
)
continue
}
const keyPrinted = attributeKeyPrinter(keyValue)
const indentationPrinted = attributeSeparator.includes("\n") ? indentation : ""
result += (
keyPrinted.length
? (indentationPrinted + keyPrinted + this.attributeValueConjunctionSign)
: ""
)
+ (
isSerialized
? `"${this.doWriteValue(value, true, indentation)}"`
: this.doWriteValue(value, insideString, indentation)
)
}
}
if (trailingSeparator && result.length) {
// append separator at the end if asked and there was printed content
result += attributeSeparator
}
return wrap(entity, result)
}
/** @param {Boolean} insideString */
doWriteValue(value, insideString, indentation = "") {
const type = Utility.getType(value)
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"
)
}
return serializer.doWrite(value, insideString, indentation)
}
/**
* @param {IEntity} entity
* @param {String} key
*/
showProperty(entity, key) {
if (entity instanceof IEntity) {
if (AttributeInfo.getAttribute(entity, key, "ignored")) {
return false
}
if (AttributeInfo.getAttribute(entity, key, "silent")) {
let defaultValue = AttributeInfo.getAttribute(entity, key, "default")
if (defaultValue instanceof Function) {
defaultValue = defaultValue(entity)
}
if (Utility.equals(entity[key], defaultValue)) {
return false
}
}
}
return true
}
}

View File

@@ -1,22 +0,0 @@
export default class SerializerFactory {
static #serializers = new Map()
/**
* @template {AttributeConstructor<Attribute>} T
* @param {T} type
* @param {Serializer<T>} object
*/
static registerSerializer(type, object) {
SerializerFactory.#serializers.set(type, object)
}
/**
* @template {AttributeConstructor<Attribute>} T
* @param {T} type
* @returns {Serializer<T>}
*/
static getSerializer(type) {
return SerializerFactory.#serializers.get(type)
}
}

View File

@@ -1,28 +0,0 @@
import Utility from "../Utility.js"
import Serializer from "./Serializer.js"
/**
* @template {AttributeConstructor<Attribute>} T
* @extends {Serializer<T>}
*/
export default class ToStringSerializer extends Serializer {
/** @param {T} entityType */
constructor(entityType, escape = true) {
super(entityType)
if (escape) {
this.wrap = (entity, serialized) => Utility.escapeString(serialized)
}
}
/**
* @param {ConstructedType<T>} entity
* @param {Boolean} insideString
*/
doWrite(entity, insideString, indentation = "") {
return !insideString && entity.constructor === String
? `"${this.wrap(entity, entity.toString())}"` // String will have quotes if not inside a string already
: this.wrap(entity, entity.toString())
}
}

View File

@@ -1,46 +1,17 @@
import Parsernostrum from "parsernostrum"
import Utility from "../Utility.js"
import BlueprintEntity from "../entity/BlueprintEntity.js"
import ByteEntity from "../entity/ByteEntity.js"
import ColorChannelEntity from "../entity/ColorChannelEntity.js"
import EnumDisplayValueEntity from "../entity/EnumDisplayValueEntity.js"
import EnumEntity from "../entity/EnumEntity.js"
import FormatTextEntity from "../entity/FormatTextEntity.js"
import FunctionReferenceEntity from "../entity/FunctionReferenceEntity.js"
import GuidEntity from "../entity/GuidEntity.js"
import Integer64Entity from "../entity/Integer64Entity.js"
import IntegerEntity from "../entity/IntegerEntity.js"
import InvariantTextEntity from "../entity/InvariantTextEntity.js"
import KeyBindingEntity from "../entity/KeyBindingEntity.js"
import LinearColorEntity from "../entity/LinearColorEntity.js"
import LocalizedTextEntity from "../entity/LocalizedTextEntity.js"
import MacroGraphReferenceEntity from "../entity/MacroGraphReferenceEntity.js"
import MirroredEntity from "../entity/MirroredEntity.js"
import ObjectEntity from "../entity/ObjectEntity.js"
import ObjectReferenceEntity from "../entity/ObjectReferenceEntity.js"
import PinEntity from "../entity/PinEntity.js"
import PinReferenceEntity from "../entity/PinReferenceEntity.js"
import PinTypeEntity from "../entity/PinTypeEntity.js"
import RBSerializationVector2DEntity from "../entity/RBSerializationVector2DEntity.js"
import RotatorEntity from "../entity/RotatorEntity.js"
import ScriptVariableEntity from "../entity/ScriptVariableEntity.js"
import SimpleSerializationRotatorEntity from "../entity/SimpleSerializationRotatorEntity.js"
import SimpleSerializationVector2DEntity from "../entity/SimpleSerializationVector2DEntity.js"
import SimpleSerializationVector4DEntity from "../entity/SimpleSerializationVector4DEntity.js"
import SimpleSerializationVectorEntity from "../entity/SimpleSerializationVectorEntity.js"
import SymbolEntity from "../entity/SymbolEntity.js"
import TerminalTypeEntity from "../entity/TerminalTypeEntity.js"
import UnknownKeysEntity from "../entity/UnknownKeysEntity.js"
import VariableReferenceEntity from "../entity/VariableReferenceEntity.js"
import Vector2DEntity from "../entity/Vector2DEntity.js"
import Vector4DEntity from "../entity/Vector4DEntity.js"
import VectorEntity from "../entity/VectorEntity.js"
import CustomSerializer from "./CustomSerializer.js"
import Grammar from "./Grammar.js"
import ObjectSerializer from "./ObjectSerializer.js"
import Serializer from "./Serializer.js"
import SerializerFactory from "./SerializerFactory.js"
import ToStringSerializer from "./ToStringSerializer.js"
import BooleanEntity from "../entity/BooleanEntity.js"
import NumberEntity from "../entity/NumberEntity.js"
import StringEntity from "../entity/StringEntity.js"
@@ -76,168 +47,4 @@ export default function initializeSerializerFactory() {
ArrayEntity.of(AlternativesEntity.accepting(NumberEntity, StringEntity, SymbolEntity)).grammar,
Parsernostrum.lazy(() => ArrayEntity.createGrammar(IEntity.unknownEntityGrammar)),
)
SerializerFactory.registerSerializer(
null,
new CustomSerializer(
(nullValue, insideString) => "()",
null
)
)
SerializerFactory.registerSerializer(
Array,
new CustomSerializer(
(array, insideString) =>
`(${array
.map(v => SerializerFactory.getSerializer(Utility.getType(v)).write(v, insideString))
.join(",")
})`,
Array
)
)
SerializerFactory.registerSerializer(
BigInt,
new ToStringSerializer(BigInt)
)
SerializerFactory.registerSerializer(
BlueprintEntity,
new ObjectSerializer(BlueprintEntity),
)
SerializerFactory.registerSerializer(
Boolean,
new CustomSerializer(
/** @param {Boolean} boolean */
(boolean, insideString) => boolean
? insideString
? "true"
: "True"
: insideString
? "false"
: "False",
Boolean
)
)
SerializerFactory.registerSerializer(
FormatTextEntity,
new CustomSerializer(
(v, insideString) => {
let result = v.getLookbehind() + "("
+ v.value.map(v =>
SerializerFactory.getSerializer(Utility.getType(v)).write(v, insideString)
).join(", ")
+ ")"
return result
},
FormatTextEntity)
)
SerializerFactory.registerSerializer(
PinEntity,
new Serializer(PinEntity, (entity, v) => `${entity.getLookbehind()} (${v})`, ",", true)
)
SerializerFactory.registerSerializer(
PinTypeEntity,
new Serializer(PinTypeEntity)
)
SerializerFactory.registerSerializer(
TerminalTypeEntity,
new Serializer(TerminalTypeEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
RBSerializationVector2DEntity,
new CustomSerializer(
(value, insideString) => `X=${value.X} Y=${value.Y}`,
RBSerializationVector2DEntity
)
)
SerializerFactory.registerSerializer(
RotatorEntity,
new Serializer(RotatorEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
ScriptVariableEntity,
new Serializer(ScriptVariableEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
String,
new CustomSerializer(
(value, insideString) => insideString
? Utility.escapeString(value)
: `"${Utility.escapeString(value)}"`,
String
)
)
SerializerFactory.registerSerializer(
SimpleSerializationRotatorEntity,
new CustomSerializer(
(value, insideString) => `${value.P}, ${value.Y}, ${value.R}`,
SimpleSerializationRotatorEntity
)
)
SerializerFactory.registerSerializer(
SimpleSerializationVector2DEntity,
new CustomSerializer(
(value, insideString) => `${value.X}, ${value.Y}`,
SimpleSerializationVector2DEntity
)
)
SerializerFactory.registerSerializer(
SimpleSerializationVectorEntity,
new CustomSerializer(
(value, insideString) => `${value.X}, ${value.Y}, ${value.Z}`,
SimpleSerializationVectorEntity
)
)
SerializerFactory.registerSerializer(
SimpleSerializationVector4DEntity,
new CustomSerializer(
(value, insideString) => `${value.X}, ${value.Y}, ${value.Z}, ${value.W}`,
SimpleSerializationVector4DEntity
)
)
SerializerFactory.registerSerializer(
SymbolEntity,
new ToStringSerializer(SymbolEntity)
)
SerializerFactory.registerSerializer(
UnknownKeysEntity,
new Serializer(UnknownKeysEntity, (entity, string) => `${entity.getLookbehind() ?? ""}(${string})`)
)
SerializerFactory.registerSerializer(
VariableReferenceEntity,
new Serializer(VariableReferenceEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
Vector2DEntity,
new Serializer(Vector2DEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
VectorEntity,
new Serializer(VectorEntity, Serializer.bracketsWrapped)
)
SerializerFactory.registerSerializer(
Vector4DEntity,
new Serializer(Vector4DEntity, Serializer.bracketsWrapped)
)
}