mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 00:13:12 +09:00
Resolve GameVersion type as a plain-string supertype
Cooldude's GameVersion brand was disjoint from the clean version types in both directions, forcing safeVersion/safeFactorioVersion companions and breaking widening from FullVersion at file-name call sites. Make GameVersion a plain string alias: the raw, un-normalised version as the game reads it. The stricter template-literal types (FullVersion, MajorMinorVersion, PartialVersion) now widen into it for free, while a GameVersion still can't be used where a normalised version is required without going through normaliseGameVersion/integerGameVersion. Runtime validation stays in GameVersionSchema. - Drop safeVersion/safeFactorioVersion; ModInfo.version stays raw (verbatim so it matches the mod's file name), integerVersion normalises via the new integerGameVersion. factorioVersion reverts to a normalised MajorMinorVersion since it is never part of a file name. - ModRecord.version becomes a GameVersion so mod packs round-trip the exact file-name version. - ModVersionEquality.testVersion / ModVersionRange.testVersion accept a raw GameVersion and normalise it; the equality bound stays strictly validated. - Migration only pads clean partial versions; raw versions are kept verbatim. - Cover the leading-zeros case: version kept verbatim in the file name while comparing equal to its normalised form. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
523293d4d9
commit
8029ad3b2d
8 files changed
+75
-42
No files matched your search
@@ -37,8 +37,16 @@ export const ApiVersions = ["0.13", "0.14", "0.15", "0.16", "0.17", "0.18", "1.0
|
||||
* not anchored at the end.
|
||||
*/
|
||||
const gameVersionRegExp = /^\s*(\d+)\.\s*(\d+)(?:\.\s*(\d+))?/;
|
||||
declare const gameVersionSymbol: unique symbol;
|
||||
export type GameVersion = string & { [gameVersionSymbol]: void };
|
||||
/**
|
||||
* A raw, un-normalised version string exactly as the game reads it, such as a
|
||||
* mod's version taken from info.json or a mod file name. It is deliberately a
|
||||
* plain string alias so that any of the stricter, normalised version types
|
||||
* ({@link FullVersion}, {@link MajorMinorVersion}, {@link PartialVersion}) widen
|
||||
* into it for free, while a GameVersion can not be used where a normalised
|
||||
* version is required without going through {@link normaliseGameVersion} or
|
||||
* {@link integerGameVersion} first.
|
||||
*/
|
||||
export type GameVersion = string;
|
||||
export const GameVersionSchema = Type.Unsafe<GameVersion>(
|
||||
Type.String({ pattern: gameVersionRegExp.source })
|
||||
);
|
||||
@@ -63,6 +71,21 @@ export function normaliseGameVersion(input: string): FullVersion | undefined {
|
||||
return `${Number(match[1])}.${Number(match[2])}.${Number(match[3] ?? "0")}` as FullVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Integer representation of a raw {@link GameVersion}, normalised the same
|
||||
* lenient way the game reads it. Suitable for comparing versions that may carry
|
||||
* leading zeros, whitespace or trailing content.
|
||||
*
|
||||
* @throws if the input can not be read as a version.
|
||||
*/
|
||||
export function integerGameVersion(version: GameVersion) {
|
||||
const normalised = normaliseGameVersion(version);
|
||||
if (normalised === undefined) {
|
||||
throw new Error(`Invalid version "${version}"`);
|
||||
}
|
||||
return integerFullVersion(normalised);
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches valid mod versions, where all parts are specified.
|
||||
*/
|
||||
@@ -201,8 +224,8 @@ export class ModVersionEquality {
|
||||
}
|
||||
}
|
||||
|
||||
testVersion(version: PartialVersion) {
|
||||
return this.testIntegerVersion(integerPartialVersion(version));
|
||||
testVersion(version: GameVersion) {
|
||||
return this.testIntegerVersion(integerGameVersion(version));
|
||||
}
|
||||
|
||||
toString() {
|
||||
@@ -264,8 +287,8 @@ export class ModVersionRange {
|
||||
return this.minVersion.testIntegerVersion(other) && this.maxVersion.testIntegerVersion(other);
|
||||
}
|
||||
|
||||
testVersion(version: PartialVersion) {
|
||||
return this.testIntegerVersion(integerPartialVersion(version));
|
||||
testVersion(version: GameVersion) {
|
||||
return this.testIntegerVersion(integerGameVersion(version));
|
||||
}
|
||||
|
||||
combineVersion(other: ModVersionEquality) {
|
||||
|
||||
Reference in new issue
Block a user