Maintain strict tagging of GameVersion

This commit is contained in:
Cooldude2606 committed 2026-07-02 00:08:09 +01:00
1 parent 8029ad3b2d
commit 2e27c4f499
8 files changed
+37 -45

No files matched your search

+13 -21
View File
@@ -37,16 +37,13 @@ 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+))?/;
/**
* 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.
* mod's version taken from info.json or a mod file name.
*/
export type GameVersion = string;
declare const gameVersionSymbol: unique symbol;
export type GameVersion = string & { [gameVersionSymbol]: void };
export const GameVersionSchema = Type.Unsafe<GameVersion>(
Type.String({ pattern: gameVersionRegExp.source })
);
@@ -63,7 +60,10 @@ export function isGameVersion(input: string): input is GameVersion {
* @returns The normalised full version, or undefined if no major and minor
* could be read.
*/
export function normaliseGameVersion(input: string): FullVersion | undefined {
export function normaliseGameVersion(input: GameVersion): FullVersion;
export function normaliseGameVersion(input: string): FullVersion | undefined;
export function normaliseGameVersion(input: string | GameVersion): FullVersion | undefined {
const match = gameVersionRegExp.exec(input);
if (match === null) {
return undefined;
@@ -71,19 +71,8 @@ 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);
return integerFullVersion(normaliseGameVersion(version));
}
/**
@@ -287,7 +276,10 @@ export class ModVersionRange {
return this.minVersion.testIntegerVersion(other) && this.maxVersion.testIntegerVersion(other);
}
testVersion(version: GameVersion) {
testVersion(version: PartialVersion | GameVersion) {
if (isPartialVersion(version)) {
return this.testIntegerVersion(integerPartialVersion(version));
}
return this.testIntegerVersion(integerGameVersion(version));
}