Various improvements (#13)

* Union lookbehind

* Fix quoted inline array
This commit is contained in:
barsdeveloper
2023-09-02 14:08:29 +02:00
committed by GitHub
parent 11f819e6d9
commit fd991b94b3
17 changed files with 313 additions and 144 deletions

View File

@@ -1,14 +1,14 @@
import IEntity from "./IEntity.js"
import InvariantTextEntity from "./InvariantTextEntity.js"
import LocalizedTextEntity from "./LocalizedTextEntity.js"
import UnionType from "./UnionType.js"
import Union from "./Union.js"
export default class FormatTextEntity extends IEntity {
static lookbehind = "LOCGEN_FORMAT_NAMED"
static lookbehind = new Union("LOCGEN_FORMAT_NAMED", "LOCGEN_FORMAT_ORDERED")
static attributes = {
value: {
type: [new UnionType(String, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity)],
type: [new Union(String, LocalizedTextEntity, InvariantTextEntity, FormatTextEntity)],
default: [],
},
}
@@ -28,11 +28,20 @@ export default class FormatTextEntity extends IEntity {
return ""
}
const values = this.value.slice(1).map(v => v.toString())
return pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
const argLocation = values.indexOf(arg) + 1
return argLocation > 0 && argLocation < values.length
? values[argLocation]
: substring
})
return this.lookbehind == "LOCGEN_FORMAT_NAMED"
? pattern.replaceAll(/\{([a-zA-Z]\w*)\}/g, (substring, arg) => {
const argLocation = values.indexOf(arg) + 1
return argLocation > 0 && argLocation < values.length
? values[argLocation]
: substring
})
: this.lookbehind == "LOCGEN_FORMAT_ORDERED"
? pattern.replaceAll(/\{(\d+)\}/g, (substring, arg) => {
const argValue = Number(arg)
return argValue < values.length
? values[argValue]
: substring
})
: ""
}
}

View File

@@ -1,7 +1,7 @@
import ComputedType from "./ComputedType.js"
import MirroredEntity from "./MirroredEntity.js"
import SerializerFactory from "../serialization/SerializerFactory.js"
import UnionType from "./UnionType.js"
import Union from "./Union.js"
import Utility from "../Utility.js"
/**
@@ -14,7 +14,7 @@ import Utility from "../Utility.js"
* @typedef {IEntity | MirroredEntity | String | Number | BigInt | Boolean} AnySimpleValue
* @typedef {AnySimpleValue | AnySimpleValue[]} AnyValue
* @typedef {(entity: IEntity) => AnyValue} ValueSupplier
* @typedef {AnyValueConstructor<AnyValue> | AnyValueConstructor<AnyValue>[] | UnionType | UnionType[] | ComputedType | MirroredEntity} AttributeType
* @typedef {AnyValueConstructor<AnyValue> | AnyValueConstructor<AnyValue>[] | Union | Union[] | ComputedType | MirroredEntity} AttributeType
* @typedef {{
* type?: AttributeType,
* default?: AnyValue | ValueSupplier,
@@ -182,8 +182,8 @@ export default class IEntity {
return ""
} else if (attributeType === Array || attributeType instanceof Array) {
return () => []
} else if (attributeType instanceof UnionType) {
return this.defaultValueProviderFromType(attributeType.getFirstType())
} else if (attributeType instanceof Union) {
return this.defaultValueProviderFromType(attributeType.values[0])
} else if (attributeType instanceof MirroredEntity) {
return () => new MirroredEntity(attributeType.type, attributeType.key, attributeType.getter)
} else if (attributeType instanceof ComputedType) {

View File

@@ -11,7 +11,7 @@ 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 Union from "./Union.js"
import UnknownPinEntity from "./UnknownPinEntity.js"
import Utility from "../Utility.js"
import VariableReferenceEntity from "./VariableReferenceEntity.js"
@@ -33,9 +33,12 @@ export default class ObjectEntity extends IEntity {
ObjectRef: {
type: ObjectReferenceEntity,
},
PinTags: {
type: [null],
inlined: true,
},
PinNames: {
type: [String],
default: undefined, // To keep the order, may be defined in additionalPinInserter()
inlined: true,
},
AxisKey: {
@@ -46,7 +49,6 @@ export default class ObjectEntity extends IEntity {
},
NumAdditionalInputs: {
type: Number,
default: undefined, // To keep the order, may be defined in additionalPinInserter()
},
bIsPureFunc: {
type: Boolean,
@@ -224,7 +226,7 @@ export default class ObjectEntity extends IEntity {
type: String,
},
CustomProperties: {
type: [new UnionType(PinEntity, UnknownPinEntity)],
type: [new Union(PinEntity, UnknownPinEntity)],
},
}
@@ -305,6 +307,7 @@ export default class ObjectEntity extends IEntity {
/** @type {String} */ this.Name
/** @type {ObjectReferenceEntity?} */ this.ExportPath
/** @type {ObjectReferenceEntity?} */ this.ObjectRef
/** @type {null[]} */ this.PinTags
/** @type {String[]} */ this.PinNames
/** @type {SymbolEntity?} */ this.AxisKey
/** @type {SymbolEntity?} */ this.InputAxisKey
@@ -993,13 +996,23 @@ export default class ObjectEntity extends IEntity {
case Configuration.paths.multiGate:
pinEntities ??= () => this.getPinEntities().filter(pinEntity => pinEntity.isOutput())
pinIndexFromEntity ??= pinEntity => Number(pinEntity.PinName.match(/^\s*Out[_\s]+(\d+)\s*$/i)?.[1])
pinNameFromIndex ??= (index, min = -1, max = -1) => `Out ${index >= 0 ? index : min > 0 ? "Out 0" : max + 1}`
pinNameFromIndex ??= (index, min = -1, max = -1) =>
`Out ${index >= 0 ? index : min > 0 ? "Out 0" : max + 1}`
break
case Configuration.paths.switchInteger:
pinEntities ??= () => this.getPinEntities().filter(pinEntity => pinEntity.isOutput())
pinIndexFromEntity ??= pinEntity => Number(pinEntity.PinName.match(/^\s*(\d+)\s*$/)?.[1])
pinNameFromIndex ??= (index, min = -1, max = -1) => (index < 0 ? max + 1 : index).toString()
break
case Configuration.paths.switchGameplayTag:
pinNameFromIndex ??= (index, min = -1, max = -1) => {
const result = `Case_${index >= 0 ? index : min > 0 ? "0" : max + 1}`
this.PinNames ??= []
this.PinNames.push(result)
delete this.PinTags[this.PinTags.length - 1]
this.PinTags[this.PinTags.length] = null
return result
}
case Configuration.paths.switchName:
case Configuration.paths.switchString:
pinEntities ??= () => this.getPinEntities().filter(pinEntity => pinEntity.isOutput())

View File

@@ -17,7 +17,7 @@ import RotatorEntity from "./RotatorEntity.js"
import SimpleSerializationRotatorEntity from "./SimpleSerializationRotatorEntity.js"
import SimpleSerializationVector2DEntity from "./SimpleSerializationVector2DEntity.js"
import SimpleSerializationVectorEntity from "./SimpleSerializationVectorEntity.js"
import UnionType from "./UnionType.js"
import Union from "./Union.js"
import Utility from "../Utility.js"
import Vector2DEntity from "./Vector2DEntity.js"
import VectorEntity from "./VectorEntity.js"
@@ -62,7 +62,7 @@ export default class PinEntity extends IEntity {
default: "",
},
PinFriendlyName: {
type: new UnionType(LocalizedTextEntity, FormatTextEntity, String),
type: new Union(LocalizedTextEntity, FormatTextEntity, String),
},
PinToolTip: {
type: String,

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

@@ -0,0 +1,17 @@
/**
* @template {any[]} U
* @template {[...U]} T
*/
export default class Union {
/** @type {T} */
#values
get values() {
return this.#values
}
/** @param {T} values */
constructor(...values) {
this.#values = values
}
}

View File

@@ -1,18 +0,0 @@
/** @typedef {import("./IEntity.js").AnyValueConstructor<*>} AnyValueConstructor */
export default class UnionType {
#types
get types() {
return this.#types
}
/** @param {...AnyValueConstructor} types */
constructor(...types) {
this.#types = types
}
getFirstType() {
return this.#types[0]
}
}