forked from MCDxAI/minecraft-dev-mcp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversion-manager.ts
More file actions
177 lines (155 loc) · 5.2 KB
/
version-manager.ts
File metadata and controls
177 lines (155 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { getCacheManager } from '../cache/cache-manager.js';
import { getMojangDownloader } from '../downloaders/mojang-downloader.js';
import { VersionNotFoundError } from '../utils/errors.js';
import { computeFileSha1 } from '../utils/hash.js';
import { logger } from '../utils/logger.js';
/**
* Manages Minecraft versions - downloading, caching, and metadata
*/
export class VersionManager {
private downloader = getMojangDownloader();
private cache = getCacheManager();
// Lock to prevent concurrent downloads of the same version
private downloadLocks = new Map<string, Promise<string>>();
/**
* Get or download a Minecraft client JAR
* Uses locking to prevent concurrent downloads of the same version
*/
async getVersionJar(
version: string,
onProgress?: (downloaded: number, total: number) => void,
): Promise<string> {
// Check cache first
const cachedPath = this.cache.getVersionJarPath(version);
if (cachedPath) {
logger.info(`Using cached JAR for ${version}: ${cachedPath}`);
return cachedPath;
}
// Check if download is already in progress for this version
const existingDownload = this.downloadLocks.get(`client-${version}`);
if (existingDownload) {
logger.info(`Waiting for existing download of ${version} to complete`);
return existingDownload;
}
// Start download with lock
const downloadPromise = this.downloadClientJarInternal(version, onProgress);
this.downloadLocks.set(`client-${version}`, downloadPromise);
try {
const jarPath = await downloadPromise;
return jarPath;
} finally {
this.downloadLocks.delete(`client-${version}`);
}
}
/**
* Internal method to download client JAR
*/
private async downloadClientJarInternal(
version: string,
onProgress?: (downloaded: number, total: number) => void,
): Promise<string> {
logger.info(`Downloading client JAR for ${version}`);
const jarPath = await this.downloader.downloadClientJar(version, onProgress);
// Compute SHA-1 and cache
const sha1 = await computeFileSha1(jarPath);
this.cache.cacheVersionJar(version, jarPath, sha1);
return jarPath;
}
/**
* Get or download a Minecraft server JAR
* Uses locking to prevent concurrent downloads of the same version
*/
async getServerJar(
version: string,
onProgress?: (downloaded: number, total: number) => void,
): Promise<string> {
// Check cache first
const cachedPath = this.cache.getServerJarPath(version);
if (cachedPath) {
logger.info(`Using cached server JAR for ${version}: ${cachedPath}`);
return cachedPath;
}
// Check if download is already in progress for this version
const existingDownload = this.downloadLocks.get(`server-${version}`);
if (existingDownload) {
logger.info(`Waiting for existing server JAR download of ${version} to complete`);
return existingDownload;
}
// Start download with lock
const downloadPromise = this.downloadServerJarInternal(version, onProgress);
this.downloadLocks.set(`server-${version}`, downloadPromise);
try {
const jarPath = await downloadPromise;
return jarPath;
} finally {
this.downloadLocks.delete(`server-${version}`);
}
}
/**
* Internal method to download server JAR
*/
private async downloadServerJarInternal(
version: string,
onProgress?: (downloaded: number, total: number) => void,
): Promise<string> {
logger.info(`Downloading server JAR for ${version}`);
const jarPath = await this.downloader.downloadServerJar(version, onProgress);
return jarPath;
}
/**
* Check if version JAR is cached
*/
hasVersion(version: string): boolean {
return this.cache.hasVersionJar(version);
}
/**
* List all available Minecraft versions
*/
async listAvailableVersions(): Promise<string[]> {
return this.downloader.listVersions();
}
/**
* List cached versions
*/
listCachedVersions(): string[] {
return this.cache.listCachedVersions();
}
/**
* Verify version exists
*/
async verifyVersion(version: string): Promise<void> {
const exists = await this.downloader.versionExists(version);
if (!exists) {
throw new VersionNotFoundError(version);
}
}
/**
* Check if a Minecraft version ships an unobfuscated JAR.
*
* Starting with Minecraft 26.1 snapshots, Mojang stopped obfuscating the
* client JAR. These versions have no `client_mappings` entry in their
* version JSON because there is nothing to reverse-map.
*/
async isVersionUnobfuscated(version: string): Promise<boolean> {
const versionJson = await this.downloader.getVersionJson(version);
return !versionJson.downloads.client_mappings;
}
/**
* Get version JAR path (must be cached)
*/
getCachedJarPath(version: string): string {
const path = this.cache.getVersionJarPath(version);
if (!path) {
throw new Error(`Version ${version} not cached`);
}
return path;
}
}
// Singleton instance
let versionManagerInstance: VersionManager | undefined;
export function getVersionManager(): VersionManager {
if (!versionManagerInstance) {
versionManagerInstance = new VersionManager();
}
return versionManagerInstance;
}