diff --git a/src/games/minecraft/index.ts b/src/games/minecraft/index.ts new file mode 100644 index 0000000..bf84e24 --- /dev/null +++ b/src/games/minecraft/index.ts @@ -0,0 +1,22 @@ +export { + MinecraftVersionType, +} from "./minecraft-version-type"; + +export { + MinecraftVersion, +} from "./minecraft-version"; + +export { + MINECRAFT_VERSION_PROVIDER, +} from "./minecraft-version-provider"; + +export { + MINECRAFT, +} from "./minecraft"; + +export { + MojangApiClient, + MojangApiOptions, + + MOJANG_API_URL, +} from "./mojang-api-client"; diff --git a/src/utils/minecraft/index.ts b/src/utils/minecraft/index.ts deleted file mode 100644 index 68eabad..0000000 --- a/src/utils/minecraft/index.ts +++ /dev/null @@ -1,158 +0,0 @@ -import fetch from "node-fetch"; -import Version from "../versioning/version"; - -export enum MinecraftVersionType { - Release = "release", - Snapshot = "snapshot", - OldBeta = "old_beta", - OldAlpha = "old_alpha" -} - -export class MinecraftVersion { - public readonly id: string; - public readonly name: string; - public readonly version: Version; - public readonly type: MinecraftVersionType; - public readonly url: string; - public readonly time: Date; - public readonly releaseTime: Date; - - public constructor(id: string, name: string, type: MinecraftVersionType, url: string, time: Date, releaseTime: Date) { - this.id = id; - this.name = name; - this.version = new Version(name); - this.type = type; - this.url = url; - this.time = time; - this.releaseTime = releaseTime; - } - - public get isRelease(): boolean { - return this.type === MinecraftVersionType.Release; - } - - public get isSnapshot(): boolean { - return this.type === MinecraftVersionType.Snapshot; - } -} - -interface ParsedMinecraftVersion { - id: string; - type: MinecraftVersionType; - url: string; - time: string; - releaseTime: string; -} - -let cachedVersionsById: Map = null; -async function getVersionMap(): Promise> { - if (!cachedVersionsById) { - cachedVersionsById = await loadVersions(); - } - return cachedVersionsById; -} - -async function loadVersions(): Promise> { - const response = <{ versions: ParsedMinecraftVersion[] }>await (await fetch("https://launchermeta.mojang.com/mc/game/version_manifest.json")).json(); - const versionsById = new Map(); - for (let i = 0; i < response.versions.length; ++i) { - const version = response.versions[i]; - versionsById.set(version.id, new MinecraftVersion(version.id, getNearestReleaseVersionName(response.versions, i), version.type, version.url, new Date(version.time), new Date(version.releaseTime))); - } - return versionsById; -} - -function getNearestReleaseVersionName(versions: ParsedMinecraftVersion[], start: number): string { - for (let i = start; i >= 0; --i) { - if (versions[i].type === MinecraftVersionType.Release) { - return versions[i].id; - } - } - - const versionMatch = versions[start].id.match(/\d+\.\d+(?:\.\d+)?/); - if (versionMatch && versionMatch.length > 0) { - return versionMatch[0]; - } - - for (let i = start + 1; i < versions.length; ++i) { - if (versions[i].type === MinecraftVersionType.Release) { - return extractVersion(versions[i].id).split(".").map((x, i) => i === 1 ? (+x + 1) : x).filter((x, i) => i < 2).join("."); - } - } - - return null; -} - -export async function getVersions(): Promise { - return [...(await getVersionMap()).values()]; -} - -export async function getVersionById(id: string): Promise { - return (await getVersionMap()).get(id.trim()) || null; -} - -export async function findVersionByName(name: string, snapshot?: boolean): Promise { - const versionMap = await getVersionMap(); - snapshot ??= isSnapshot(name); - const foundVersion = versionMap.get(name); - if (foundVersion && foundVersion.isSnapshot === !!snapshot) { - return foundVersion; - } - - name = extractVersion(name); - for (const version of versionMap.values()) { - if (version.name === name && version.isSnapshot) { - return version; - } - } - return null; -} - -function extractVersion(versionName: string): string { - return versionName.match(/(? x.match(/\d+\.\d+(?:\.\d+)?/)).filter(x => x).map(x => x[0]); - return versionCandidates.length > 1 ? versionCandidates.filter(x => x.startsWith("1.")).reverse()[0] : null; - } -} - -export function parseVersionName(version: string): string | null { - const versionCandidates = [...(version.match(/\d+\.\d+(?:\.\d+)?/g) || [])]; - return versionCandidates.filter(x => x.startsWith("1."))[0] || null; -} - -export async function getLatestRelease(): Promise { - return (await getVersions()).find(x => x.isRelease) || null; -} - -export async function getCompatibleBuilds(build: string | Version): Promise { - if (!(build instanceof Version)) { - build = new Version(build); - } - const versions = new Array(); - for (const version of await getVersions()) { - if (version.version.major !== build.major) { - continue; - } - - if (version.version.minor < build.minor) { - break; - } - - if (version.version.minor === build.minor && version.version.build >= build.build) { - versions.push(version); - } - } - return versions; -}