buildClasspath method
Builds the full Java classpath for the specified Minecraft version.
Collects all required JAR files, including the client JAR and all libraries required by the version. Downloads any missing files as needed.
versionInfo
The version information containing library dependencies.
versionId
The Minecraft version identifier.
Returns a list of paths to be included in the Java classpath. Throws an exception if critical files cannot be downloaded.
Implementation
Future<List<String>> buildClasspath(
VersionInfo versionInfo,
String versionId,
) async {
final clientJarPath = getClientJarPath(versionId);
final classpath = <String>[];
if (!await File(clientJarPath).exists()) {
debugPrint(
'Client JAR file not found. Trying to download: $clientJarPath',
);
try {
await downloadClientJar(versionInfo, versionId);
if (!await File(clientJarPath).exists()) {
throw Exception('Failed to download client JAR file: $clientJarPath');
}
} catch (e) {
debugPrint('Failed to download Minecraft client files skipping it: $e');
}
}
classpath.add(clientJarPath);
debugPrint('Adding client jar to classpath: $clientJarPath');
int missingLibraries = 0;
final libraries = versionInfo.libraries ?? [];
for (final library in libraries) {
final rules = library.rules;
if (rules != null) {
bool allowed = false;
for (final rule in rules) {
final action = rule.action;
final os = rule.os;
bool osMatches = true;
if (os != null) {
final osName = os.name;
if (osName != null) {
if (Platform.isWindows && osName != 'windows') osMatches = false;
if (Platform.isMacOS && osName != 'osx') osMatches = false;
if (Platform.isLinux && osName != 'linux') osMatches = false;
}
}
if (osMatches) {
allowed = action == 'allow';
}
}
if (!allowed) continue;
}
final downloads = library.downloads;
if (downloads == null) continue;
final artifact = downloads.artifact;
if (artifact == null) continue;
final path = artifact.path;
if (path == null) continue;
final libraryPath = _normalizePath(p.join(_librariesDir, path));
bool fileExists = await File(libraryPath).exists();
if (fileExists) {
classpath.add(libraryPath);
debugPrint('Added library to classpath: $libraryPath');
} else {
missingLibraries++;
debugPrint('Library not found: $libraryPath');
try {
await downloadLibraries(versionInfo);
if (await File(libraryPath).exists()) {
classpath.add(libraryPath);
debugPrint(
'Downloaded and added library to classpath: $libraryPath',
);
} else {
debugPrint(
'Library still not found after download attempt: $libraryPath',
);
}
} catch (e) {
debugPrint('Failed to download library: $e');
}
}
}
if (missingLibraries > 0) {
debugPrint('Warning: $missingLibraries library files not found');
}
debugPrint('Number of JAR files in classpath: ${classpath.length}');
return classpath;
}