versionsMap := make(map[string]string)
for _, v := range l.versionManifest.Versions {
versionsMap[v.ID] = fmt.Sprintf("%s (%s)", v.ID, v.Type)
}
// Сканируем папку versions на наличие дополнительных версий
versionsDir := filepath.Join(l.minelauncherPath, "versions")
versionFolders, err := os.ReadDir(versionsDir)
if err == nil {
for _, folder := range versionFolders {
if folder.IsDir() {
versionID := folder.Name()
if _, exists := versionsMap[versionID]; !exists {
versionsMap[versionID] = fmt.Sprintf("%s (custom)", versionID)
}
}
}
}
// Собираем версии в слайс
versions := []string{}
for _, v := range versionsMap {
versions = append(versions, v)
}
return versions, nil
}
func (l *Launcher) getVersionDetails(versionID string) (VersionData, error) {
fmt.Printf("Получение данных для версии: %s\n", versionID)
code
// Проверяем локальный JSON-файл версии
versionJsonPath := filepath.Join(l.minelauncherPath, "versions", versionID, versionID+".json")
if _, err := os.Stat(versionJsonPath); err == nil {
// Локальный JSON существует, загружаем его
data, err := os.ReadFile(versionJsonPath)
if err != nil {
return nil, fmt.Errorf("не удалось прочитать локальный файл версии %s: %v", versionJsonPath, err)
}
var versionData VersionData
err = json.Unmarshal(data, &versionData)
if err != nil {
return nil, fmt.Errorf("ошибка при разборе локальных данных версии %s: %v", versionJsonPath, err)
}
return versionData, nil
}
// Иначе получаем данные версии из официального манифеста
var versionURL string
for _, version := range l.versionManifest.Versions {
if version.ID == versionID {
versionURL = version.URL
break
}
}
if versionURL == "" {
return nil, fmt.Errorf("версия %s не найдена в манифесте", versionID)
}
resp, err := http.Get(versionURL)
if err != nil {
return nil, fmt.Errorf("не удалось получить данные версии: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("не удалось получить данные версии, статус код: %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("не удалось прочитать тело ответа версии: %v", err)
}
var versionData VersionData
err = json.Unmarshal(bodyBytes, &versionData)
if err != nil {
return nil, fmt.Errorf("ошибка при разборе данных версии: %v", err)
}
return versionData, nil
}
func (l *Launcher) mergeVersionData(versionData VersionData) (VersionData, error) {
if parentID, ok := versionData["inheritsFrom"].(string); ok {
parentData, err := l.getVersionDetails(parentID)
if err != nil {
return nil, fmt.Errorf("ошибка при получении родительской версии %s: %v", parentID, err)
}
// Рекурсивно объединяем данные родительской версии
parentData, err = l.mergeVersionData(parentData)
if err != nil {
return nil, err
}
// Объединяем данные parentData в versionData
mergedData := l.deepMergeVersionData(parentData, versionData)
return mergedData, nil
}
return versionData, nil
}
func (l *Launcher) deepMergeVersionData(parent, child VersionData) VersionData {
merged := make(VersionData)
// Копируем данные родительской версии
for k, v := range parent {
merged[k] = v
}
// Переопределяем данными дочерней версии
for k, v := range child {
if k == "libraries" {
// Объединяем массивы библиотек
parentLibs, _ := parent["libraries"].([]interface{})
childLibs, _ := v.([]interface{})
mergedLibs := append(parentLibs, childLibs...)
merged["libraries"] = mergedLibs
} else if k == "arguments" {
// Объединяем аргументы
parentArgs, _ := parent["arguments"].(map[string]interface{})
childArgs, _ := v.(map[string]interface{})
mergedArgs := make(map[string]interface{})
for ak, av := range parentArgs {
if childArgValue, hasChildArg := childArgs[ak]; hasChildArg {
if isNonEmpty(childArgValue) {
// Если дочерний аргумент не пустой, используем его
mergedArgs[ak] = childArgValue
} else {
// Если дочерний аргумент пустой, сохраняем родительский
mergedArgs[ak] = av
}
} else {
// Если в дочерней версии нет этого аргумента, сохраняем родительский
mergedArgs[ak] = av
}
}
// Добавляем новые аргументы из дочерней версии, которых нет в родительской
for ak, av := range childArgs {
if _, exists := parentArgs[ak]; !exists {
mergedArgs[ak] = av
}
}
merged["arguments"] = mergedArgs
} else if k == "logging" {
// Переопределяем logging
merged["logging"] = v
} else {
merged[k] = v
}
}
return merged
}
func isNonEmpty(value interface{}) bool {
if value == nil {
return false
}
switch v := value.(type) {
case []interface{}:
return len(v) > 0
case map[string]interface{}:
return len(v) > 0
default:
return true
}
}
hash := sha1.New()
if _, err := io.Copy(hash, file); err != nil {
fmt.Printf("Не удалось вычислить хэш для файла: %s, ошибка: %v\n", filePath, err)
return false
}
calculatedHash := hex.EncodeToString(hash.Sum(nil))
if expectedHash != calculatedHash {
fmt.Printf("Хэш не совпадает для файла: %s (ожидалось: %s, получено: %s)\n", filePath, expectedHash, calculatedHash)
return false
}
return true
}
func (l *Launcher) shouldIncludeLibrary(libInfo map[string]interface{}) bool {
allowed := true
if rules, ok := libInfo["rules"].([]interface{}); ok {
for _, ruleInterface := range rules {
rule, ok := ruleInterface.(map[string]interface{})
if !ok {
continue
}
action, ok := rule["action"].(string)
if !ok {
continue
}
code
applies := true
if osDict, ok := rule["os"].(map[string]interface{}); ok {
osName := runtime.GOOS
if osName == "darwin" {
osName = "osx"
}
if name, ok := osDict["name"].(string); ok && name != osName {
applies = false
}
}
if applies {
if action == "allow" {
allowed = true
} else if action == "disallow" {
allowed = false
}
}
}
}
return allowed
natives, ok := libInfo["natives"].(map[string]interface{})
if !ok {
return ""
}
var classifierName string
switch runtime.GOOS {
case "windows":
if val, exists := natives["windows"].(string); exists {
classifierName = strings.ReplaceAll(val, "${arch}", archType)
}
case "darwin":
if val, exists := natives["osx"].(string); exists {
classifierName = strings.ReplaceAll(val, "${arch}", archType)
}
case "linux":
if val, exists := natives["linux"].(string); exists {
classifierName = strings.ReplaceAll(val, "${arch}", archType)
}
default:
return ""
}
return classifierName
}
func (l *Launcher) unzip(src, dest string) error {
fmt.Printf("Распаковка архива: %s в %s\n", src, dest)
r, err := zip.OpenReader(src)
if err != nil {
return fmt.Errorf("не удалось открыть ZIP-архив %s: %v", src, err)
}
defer r.Close()
code
for _, f := range r.File {
if strings.HasPrefix(f.Name, "META-INF") {
continue
}
fpath := filepath.Join(dest, f.Name)
// Проверка на безопасность пути
if !strings.HasPrefix(fpath, filepath.Clean(dest)+string(os.PathSeparator)) {
return fmt.Errorf("недопустимый путь в архиве: %s", fpath)
}
if f.FileInfo().IsDir() {
err := os.MkdirAll(fpath, os.ModePerm)
if err != nil {
return fmt.Errorf("не удалось создать директорию %s: %v", fpath, err)
}
continue
}
if err := os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
return fmt.Errorf("не удалось создать директорию %s: %v", filepath.Dir(fpath), err)
}
outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return fmt.Errorf("не удалось открыть файл для записи %s: %v", fpath, err)
}
rc, err := f.Open()
if err != nil {
outFile.Close()
return fmt.Errorf("не удалось открыть файл внутри архива %s: %v", f.Name, err)
}
_, err = io.Copy(outFile, rc)
outFile.Close()
rc.Close()
if err != nil {
return fmt.Errorf("ошибка при копировании файла %s: %v", fpath, err)
}
}
return nil
}
func (l *Launcher) verifyMainClass(clientJarPath string) bool {
r, err := zip.OpenReader(clientJarPath)
if err != nil {
fmt.Printf("Не удалось открыть client.jar для проверки main class: %v\n", err)
return false
}
defer r.Close()
code
target := "net/minecraft/client/main/Main.class"
for _, f := range r.File {
if f.Name == target {
return true
}
}
fmt.Println("Main class не найден в client.jar")
return false
dirPath := filepath.Join(l.minelauncherPath, "versions", versionID)
os.MkdirAll(dirPath, os.ModePerm)
filePath := filepath.Join(dirPath, versionID+".jar")
// Проверяем, существует ли файл client.jar
if _, err := os.Stat(filePath); err == nil {
fmt.Println("Клиентский JAR-файл уже существует, пропускаем скачивание.")
return nil
}
// Проверяем, есть ли указание на загрузку клиентского JAR-файла
downloads, ok := versionData["downloads"].(map[string]interface{})
if ok {
client, ok := downloads["client"].(map[string]interface{})
if ok {
url, ok := client["url"].(string)
if !ok {
return fmt.Errorf("не удалось получить URL клиента")
}
expectedHash, _ := client["sha1"].(string)
absFilePath, err := filepath.Abs(filePath)
if err != nil {
return fmt.Errorf("не удалось получить абсолютный путь к файлу %s: %v", filePath, err)
}
err = l.downloadFileWithRetry(url, absFilePath, 4)
if err != nil {
return fmt.Errorf("ошибка при скачивании клиентского JAR-файла: %v", err)
}
if expectedHash != "" && !l.verifyFileChecksum(absFilePath, expectedHash) {
return fmt.Errorf("хэш клиентского JAR-файла не совпадает после загрузки")
}
if !l.verifyMainClass(absFilePath) {
return fmt.Errorf("файл client.jar не содержит необходимого основного класса")
}
return nil
}
}
// Если не удалось найти информацию о загрузке, проверяем наличие "inheritsFrom"
if parentID, ok := versionData["inheritsFrom"].(string); ok {
// Пытаемся использовать client.jar родительской версии
parentClientJar := filepath.Join(l.minelauncherPath, "versions", parentID, parentID+".jar")
if _, err := os.Stat(parentClientJar); err != nil {
// Если файл не существует, скачиваем родительскую версию
parentData, err := l.getVersionDetails(parentID)
if err != nil {
return fmt.Errorf("не удалось получить данные родительской версии %s: %v", parentID, err)
}
err = l.downloadClientJar(parentData, parentID)
if err != nil {
return err
}
}
// Копируем client.jar из родительской версии
err := copyFile(parentClientJar, filePath)
if err != nil {
return fmt.Errorf("не удалось скопировать client.jar из родительской версии: %v", err)
}
return nil
}
return fmt.Errorf("не удалось скачать или найти client.jar для версии %s", versionID)
}
func copyFile(src, dst string) error {
input, err := os.Open(src)
if err != nil {
return fmt.Errorf("не удалось открыть файл %s для копирования: %v", src, err)
}
defer input.Close()
code
output, err := os.Create(dst)
if err != nil {
return fmt.Errorf("не удалось создать файл %s для копирования: %v", dst, err)
}
defer output.Close()
_, err = io.Copy(output, input)
if err != nil {
return fmt.Errorf("ошибка при копировании файла %s в %s: %v", src, dst, err)
}
return nil
}
func (l *Launcher) downloadLibraries(versionData VersionData) error {
fmt.Println("Скачивание библиотек...")
libraries, ok := versionData["libraries"].([]interface{})
if !ok {
return fmt.Errorf("не удалось получить библиотеки из данных версии")
}
var wg sync.WaitGroup
semaphore := make(chan struct{}, 10)
code
for _, lib := range libraries {
libInfo, ok := lib.(map[string]interface{})
if !ok {
continue
}
if !l.shouldIncludeLibrary(libInfo) {
continue
}
var downloadInfo map[string]interface{}
downloads, hasDownloads := libInfo["downloads"].(map[string]interface{})
if hasDownloads {
artifact, hasArtifact := downloads["artifact"].(map[string]interface{})
if hasArtifact {
downloadInfo = artifact
}
} else {
// Обработка старого формата
name, ok := libInfo["name"].(string)
if !ok {
continue
}
var url string
if customURL, ok := libInfo["url"].(string); ok {
url = customURL
} else {
url = "https://libraries.minecraft.net/"
}
path := l.mavenArtifactPath(name)
fullURL := url + path
downloadInfo = map[string]interface{}{
"url": fullURL,
"path": path,
}
}
url, ok := downloadInfo["url"].(string)
if !ok {
continue
}
path, ok := downloadInfo["path"].(string)
if !ok {
continue
}
expectedHash, _ := downloadInfo["sha1"].(string)
filePath := filepath.Join(l.minelauncherPath, "libraries", filepath.FromSlash(path))
absFilePath, err := filepath.Abs(filePath)
if err != nil {
fmt.Printf("Не удалось получить абсолютный путь для файла %s: %v\n", filePath, err)
continue
}
dir := filepath.Dir(absFilePath)
os.MkdirAll(dir, os.ModePerm)
// Проверяем, существует ли файл и совпадает ли хэш
if _, err := os.Stat(absFilePath); err == nil {
if expectedHash != "" && l.verifyFileChecksum(absFilePath, expectedHash) {
continue
} else {
fmt.Printf("Хэш библиотеки не совпадает, перезагружаем файл: %s\n", absFilePath)
os.Remove(absFilePath)
}
}
wg.Add(1)
semaphore <- struct{}{}
go func(url, absFilePath, expectedHash string) {
defer wg.Done()
defer func() { <-semaphore }()
err := l.downloadFileWithRetry(url, absFilePath, 4)
if err != nil {
fmt.Printf("Ошибка при скачивании библиотеки %s: %v\n", url, err)
return
}
if expectedHash != "" && !l.verifyFileChecksum(absFilePath, expectedHash) {
fmt.Printf("Хэш библиотеки не совпадает после загрузки: %s\n", absFilePath)
os.Remove(absFilePath)
}
}(url, absFilePath, expectedHash)
}
wg.Wait()
return nil
func (l *Launcher) downloadAssets(versionData VersionData) error {
fmt.Println("Скачивание ассетов...")
assetIndex, ok := versionData["assetIndex"].(map[string]interface{})
if !ok {
return fmt.Errorf("не удалось найти assetIndex в данных версии")
}
url, ok := assetIndex["url"].(string)
if !ok {
return fmt.Errorf("не удалось получить URL assetIndex")
}
expectedHash, ok := assetIndex["sha1"].(string)
if !ok {
return fmt.Errorf("не удалось получить sha1 assetIndex")
}
code
// Скачиваем индекс ассетов
fmt.Println("Скачивание индекса ассетов...")
resp, err := http.Get(url)
if err != nil {
return fmt.Errorf("не удалось скачать индекс ассетов: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("не удалось скачать индекс ассетов, статус код: %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("не удалось прочитать индекс ассетов: %v", err)
}
hash := sha1.New()
hash.Write(bodyBytes)
calculatedHash := hex.EncodeToString(hash.Sum(nil))
if expectedHash != calculatedHash {
return fmt.Errorf("хэш индекса ассетов не совпадает (ожидалось: %s, получено: %s)", expectedHash, calculatedHash)
}
var assetIndexData map[string]interface{}
err = json.Unmarshal(bodyBytes, &assetIndexData)
if err != nil {
return fmt.Errorf("ошибка при разборе индекса ассетов: %v", err)
}
fmt.Println("Сохранение индекса ассетов...")
assetsDir := filepath.Join(l.minelauncherPath, "assets", "indexes")
os.MkdirAll(assetsDir, os.ModePerm)
indexFilePath := filepath.Join(assetsDir, fmt.Sprintf("%s.json", assetIndex["id"].(string)))
err = os.WriteFile(indexFilePath, bodyBytes, 0644)
if err != nil {
return fmt.Errorf("ошибка при сохранении индекса ассетов: %v", err)
}
objects, ok := assetIndexData["objects"].(map[string]interface{})
if !ok {
return fmt.Errorf("не удалось получить объекты ассетов")
}
totalAssets := len(objects)
fmt.Printf("Всего ассетов для загрузки: %d\n", totalAssets)
var wg sync.WaitGroup
semaphore := make(chan struct{}, 20)
currentAsset := 0
mutex := &sync.Mutex{}
for _, obj := range objects {
wg.Add(1)
semaphore <- struct{}{}
go func(obj interface{}) {
defer wg.Done()
defer func() { <-semaphore }()
objInfo, ok := obj.(map[string]interface{})
if !ok {
return
}
hashStr, ok := objInfo["hash"].(string)
if !ok {
return
}
sizeFloat, ok := objInfo["size"].(float64)
if !ok {
return
}
size := int64(sizeFloat)
if len(hashStr) < 2 {
fmt.Printf("Некорректный хэш ассета: %s\n", hashStr)
return
}
subHash := hashStr[:2]
assetURL := fmt.Sprintf("https://resources.download.minecraft.net/%s/%s", subHash, hashStr)
filePath := filepath.Join(l.minelauncherPath, "assets", "objects", subHash, hashStr)
absFilePath, err := filepath.Abs(filePath)
if err != nil {
fmt.Printf("Не удалось получить абсолютный путь для ассета %s: %v\n", filePath, err)
return
}
dir := filepath.Dir(absFilePath)
os.MkdirAll(dir, os.ModePerm)
if fi, err := os.Stat(absFilePath); err == nil && fi.Size() == size {
} else {
err := l.downloadFileWithRetry(assetURL, absFilePath, 4)
if err != nil {
fmt.Printf("\nОшибка при скачивании ассета %s: %v\n", assetURL, err)
return
}
}
mutex.Lock()
currentAsset++
progress := float64(currentAsset) / float64(totalAssets) * 100
fmt.Printf("\rСкачивание ассетов: %d/%d (%.2f%%)", currentAsset, totalAssets, progress)
mutex.Unlock()
}(obj)
}
wg.Wait()
fmt.Println("\nЗагрузка ассетов завершена.")
return nil
}
func (l *Launcher) extractNatives(versionData VersionData, nativesDir string) error {
fmt.Println("Извлечение нативных библиотек...")
libraries, ok := versionData["libraries"].([]interface{})
if !ok {
return fmt.Errorf("не удалось получить библиотеки для распаковки нативов")
}
code
for _, lib := range libraries {
libInfo, ok := lib.(map[string]interface{})
if !ok {
continue
}
if !l.shouldIncludeLibrary(libInfo) {
continue
}
downloads, ok := libInfo["downloads"].(map[string]interface{})
if !ok {
continue
}
classifiers, ok := downloads["classifiers"].(map[string]interface{})
if !ok {
continue
}
classifierName := l.getNatives(libInfo)
if classifierName == "" {
continue
}
native, ok := classifiers[classifierName].(map[string]interface{})
if !ok {
continue
}
url, ok := native["url"].(string)
if !ok {
continue
}
path, ok := native["path"].(string)
if !ok {
continue
}
expectedHash, _ := native["sha1"].(string)
filePath := filepath.Join(l.minelauncherPath, "libraries", filepath.FromSlash(path))
absFilePath, err := filepath.Abs(filePath)
if err != nil {
fmt.Printf("Не удалось получить абсолютный путь для файла %s: %v\n", filePath, err)
continue
}
dir := filepath.Dir(absFilePath)
os.MkdirAll(dir, os.ModePerm)
if _, err := os.Stat(absFilePath); err == nil {
if expectedHash != "" && l.verifyFileChecksum(absFilePath, expectedHash) {
fmt.Printf("Нативная библиотека уже существует и корректна: %s\n", absFilePath)
} else {
fmt.Printf("Хэш нативной библиотеки не совпадает, перезагружаем файл: %s\n", absFilePath)
os.Remove(absFilePath)
}
}
if _, err := os.Stat(absFilePath); os.IsNotExist(err) {
fmt.Printf("Скачивание нативной библиотеки: %s\n", absFilePath)
err := l.downloadFileWithRetry(url, absFilePath, 4)
if err != nil {
fmt.Printf("Ошибка при скачивании нативной библиотеки %s: %v\n", url, err)
return err
}
}
if expectedHash != "" && !l.verifyFileChecksum(absFilePath, expectedHash) {
return fmt.Errorf("хэш нативной библиотеки не совпадает после загрузки: %s", absFilePath)
}
fmt.Printf("Распаковка нативных библиотек из %s в %s\n", absFilePath, nativesDir)
err = l.unzip(absFilePath, nativesDir)
if err != nil {
return fmt.Errorf("ошибка при распаковке нативной библиотеки %s: %v", absFilePath, err)
}
}
return nil
arg = strings.ReplaceAll(arg, "${auth_player_name}", username)
arg = strings.ReplaceAll(arg, "${version_name}", versionID)
arg = strings.ReplaceAll(arg, "${game_directory}", l.minelauncherPath)
arg = strings.ReplaceAll(arg, "${assets_root}", filepath.Join(l.minelauncherPath, "assets"))
arg = strings.ReplaceAll(arg, "${assets_index_name}", l.getAssetIndexName(assetIndex))
arg = strings.ReplaceAll(arg, "${auth_uuid}", uuid)
arg = strings.ReplaceAll(arg, "${auth_access_token}", authToken)
arg = strings.ReplaceAll(arg, "${clientid}", "")
arg = strings.ReplaceAll(arg, "${auth_xuid}", "")
arg = strings.ReplaceAll(arg, "${version_type}", l.versionDataGetVersionType(versionData))
arg = strings.ReplaceAll(arg, "${user_type}", "legacy")
arg = strings.ReplaceAll(arg, "--demo", "")
arg = strings.ReplaceAll(arg, "${natives_directory}", filepath.Join(l.minelauncherPath, "versions", versionID, "natives"))
arg = strings.ReplaceAll(arg, "${launcher_name}", "MyCustomLauncher")
arg = strings.ReplaceAll(arg, "${launcher_version}", "1.0")
// Устанавливаем значения разрешения (ширины и высоты)
resolutionWidth := "854" // Значение ширины по умолчанию
resolutionHeight := "480" // Значение высоты по умолчанию
if resolutionWidth != "" {
arg = strings.ReplaceAll(arg, "${resolution_width}", resolutionWidth)
} else {
// Удаляем опцию '--width' и ее значение из аргументов
arg = strings.ReplaceAll(arg, "--width ${resolution_width}", "")
}
if resolutionHeight != "" {
arg = strings.ReplaceAll(arg, "${resolution_height}", resolutionHeight)
} else {
// Удаляем опцию '--height' и ее значение из аргументов
arg = strings.ReplaceAll(arg, "--height ${resolution_height}", "")
}
return arg
}
func (l *Launcher) getAssetIndexName(assetIndex map[string]interface{}) string {
if id, ok := assetIndex["id"].(string); ok {
return id
}
return "legacy"
}
func (l *Launcher) versionDataGetAssetIndex(versionData VersionData) map[string]interface{} {
if assetIndex, ok := versionData["assetIndex"].(map[string]interface{}); ok {
return assetIndex
}
return map[string]interface{}{}
}
func (l *Launcher) versionDataGetVersionType(versionData VersionData) string {
if versionType, ok := versionData["type"].(string); ok {
return versionType
}
return "release"
}
func (l *Launcher) checkRule(rules []interface{}) bool {
allowed := false
for _, ruleInterface := range rules {
rule, ok := ruleInterface.(map[string]interface{})
if !ok {
continue
}
action, ok := rule["action"].(string)
if !ok {
continue
}
code
applies := true
if osDict, ok := rule["os"].(map[string]interface{}); ok {
osName := runtime.GOOS
if osName == "darwin" {
osName = "osx"
}
if name, ok := osDict["name"].(string); ok && name != osName {
applies = false
}
}
if applies {
if action == "allow" {
allowed = true
} else if action == "disallow" {
allowed = false
}
}
}
return allowed
func (l *Launcher) PlayMinecraftVersion(versionID string, username string) error {
absPath, err := filepath.Abs(l.minelauncherPath)
if err != nil {
return fmt.Errorf("не удалось получить абсолютный путь для %s: %v", l.minelauncherPath, err)
}
l.minelauncherPath = absPath
code
err = os.MkdirAll(l.minelauncherPath, os.ModePerm)
if err != nil {
return fmt.Errorf("не удалось создать директорию %s: %v", l.minelauncherPath, err)
}
versionData, err := l.getVersionDetails(versionID)
if err != nil {
return err
}
// Объединяем данные версий с учетом inheritsFrom
versionData, err = l.mergeVersionData(versionData)
if err != nil {
return err
}
versionDir := filepath.Join(l.minelauncherPath, "versions", versionID)
err = os.MkdirAll(versionDir, os.ModePerm)
if err != nil {
return fmt.Errorf("не удалось создать директорию %s: %v", versionDir, err)
}
versionJsonPath := filepath.Join(versionDir, versionID+".json")
versionDataBytes, err := json.MarshalIndent(versionData, "", " ")
if err != nil {
return fmt.Errorf("ошибка при сериализации данных версии: %v", err)
}
err = os.WriteFile(versionJsonPath, versionDataBytes, 0644)
if err != nil {
return fmt.Errorf("ошибка при сохранении файла version.json: %v", err)
}
err = l.downloadClientJar(versionData, versionID)
if err != nil {
return fmt.Errorf("ошибка при скачивании клиентского JAR-файла: %v", err)
}
err = l.downloadLibraries(versionData)
if err != nil {
return fmt.Errorf("ошибка при скачивании библиотек: %v", err)
}
err = l.downloadAssets(versionData)
if err != nil {
return fmt.Errorf("ошибка при скачивании ассетов: %v", err)
}
err = l.verifyAllFiles(versionData, versionID)
if err != nil {
return fmt.Errorf("ошибка валидации файлов: %v", err)
}
dirs := []string{
filepath.Join(l.minelauncherPath, "saves"),
filepath.Join(l.minelauncherPath, "resourcepacks"),
filepath.Join(l.minelauncherPath, "logs"),
filepath.Join(l.minelauncherPath, "runtime"),
}
for _, dir := range dirs {
err := os.MkdirAll(dir, os.ModePerm)
if err != nil {
return fmt.Errorf("не удалось создать директорию %s: %v", dir, err)
}
}
optionsFilePath := filepath.Join(l.minelauncherPath, "options.txt")
if _, err := os.Stat(optionsFilePath); os.IsNotExist(err) {
err := os.WriteFile(optionsFilePath, []byte(""), 0644)
if err != nil {
return fmt.Errorf("не удалось создать файл %s: %v", optionsFilePath, err)
}
}
javaVersionCmd := exec.Command("java", "-version")
javaVersionOutput, err := javaVersionCmd.CombinedOutput()
if err != nil {
return fmt.Errorf("ошибка при проверке версии Java: %v", err)
}
fmt.Println("Java версия:", string(javaVersionOutput))
uname := username
uuid := l.generateOfflineUUID()
authToken := ""
err = l.launchMinecraft(versionData, authToken, uname, uuid, versionID)
if err != nil {
return fmt.Errorf("ошибка при запуске Minecraft: %v", err)
}
fmt.Println("Minecraft запущен успешно!")
return nil
}
сделай поддержку сторонних версий котоыре загруженны в versions загружай их в манифест и исправь файлы natives всегда дожны быть в папке natives в версии игры у каждой версии свои natives
To enhance your Minecraft launcher with support for third-party (custom) versions located in the versions directory and ensure that each version has its own dedicated natives folder, you'll need to make several modifications to your existing Go code. Below, I'll guide you through the necessary changes and provide updated code snippets to achieve the desired functionality.
Overview of Required Changes
Integrate Custom Versions into the Version Manifest:
Modify the getVersionManifest method to scan the versions directory for custom versions.
Load the JSON metadata of each custom version and append it to the in-memory version manifest.
Ensure Dedicated natives Folder per Version:
Update the natives extraction logic to always place native libraries into a natives folder specific to each version.
Handle Launching of Custom Versions Appropriately:
Ensure that when launching a custom version, all paths and dependencies are correctly resolved based on the version's directory.
Let's implement these changes step-by-step.
1. Integrate Custom Versions into the Version Manifest
Step 1.1: Update getVersionManifest to Include Custom Versions
Modify the getVersionManifest method to scan the versions directory for any custom versions. For each detected custom version, load its JSON metadata and append it to the Versions slice of the VersionManifest.
go
func (l *Launcher) getVersionManifest() (*VersionManifest, error) {
fmt.Println("Получение манифеста версий...")
// Fetch the official version manifest
resp, err := http.Get(versionManifestURL)
if err != nil {
return nil, fmt.Errorf("не удалось получить манифест версий: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("не удалось получить манифест версий, статус код: %d", resp.StatusCode)
}
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("не удалось прочитать тело ответа манифеста версий: %v", err)
}
var manifest VersionManifest
err = json.Unmarshal(bodyBytes, &manifest)
if err != nil {
return nil, fmt.Errorf("ошибка при разборе манифеста версий: %v", err)
}
// Scan for custom versions in the 'versions' directory
customVersions, err := l.loadCustomVersions()
if err != nil {
fmt.Printf("Предупреждение: не удалось загрузить пользовательские версии: %v\n", err)
} else {
manifest.Versions = append(manifest.Versions, customVersions...)
}
return &manifest, nil
}
func (l *Launcher) loadCustomVersions() ([]struct {
ID string `json:"id"`
Type string `json:"type"`
URL string `json:"url,omitempty"`
Time string `json:"time,omitempty"`
ReleaseTime string `json:"releaseTime,omitempty"`
}, error) {
var customVersions []struct {
ID string `json:"id"`
Type string `json:"type"`
URL string `json:"url,omitempty"`
Time string `json:"time,omitempty"`
ReleaseTime string `json:"releaseTime,omitempty"`
}
versionsDir := filepath.Join(l.minelauncherPath, "versions")
entries, err := os.ReadDir(versionsDir)
if err != nil {
return nil, fmt.Errorf("ошибка при чтении директории версий: %v", err)
}
for _, entry := range entries {
if entry.IsDir() {
versionID := entry.Name()
versionJsonPath := filepath.Join(versionsDir, versionID, versionID+".json")
if _, err := os.Stat(versionJsonPath); err == nil {
data, err := os.ReadFile(versionJsonPath)
if err != nil {
fmt.Printf("Предупреждение: не удалось прочитать файл версии %s: %v\n", versionJsonPath, err)
continue
}
var versionData map[string]interface{}
err = json.Unmarshal(data, &versionData)
if err != nil {
fmt.Printf("Предупреждение: ошибка при разборе данных версии %s: %v\n", versionJsonPath, err)
continue
}
versionType, ok := versionData["type"].(string)
if !ok {
versionType = "custom"
}
// Since custom versions are local, URL can be left empty or set to a custom value
customVersions = append(customVersions, struct {
ID string `json:"id"`
Type string `json:"type"`
URL string `json:"url,omitempty"`
Time string `json:"time,omitempty"`
ReleaseTime string `json:"releaseTime,omitempty"`
}{
ID: versionID,
Type: versionType,
})
}
}
}
return customVersions, nil
}
Explanation:
getVersionManifest Method:
After fetching and unmarshalling the official version manifest, it calls loadCustomVersions to retrieve any custom versions present in the versions directory.
Custom versions are appended to the Versions slice of the manifest, allowing them to be treated similarly to official versions.
loadCustomVersions Method:
Scans the versions directory for subdirectories.
For each subdirectory, it checks for the presence of a version.json file (e.g., 1.16.5.json).
Parses the JSON data to extract version information and appends it to the list of custom versions.
If a custom version's JSON lacks certain fields (type, time, releaseTime), default values are assigned or fields are left empty as appropriate.
2. Ensure Dedicated natives Folder per Version
Your current implementation already specifies a natives directory per version when launching Minecraft:
This ensures that natives are always extracted to a folder specific to the version being launched. However, to guarantee consistency and prevent overwriting, ensure that the natives extraction logic always utilizes this path.
Step 2.1: Verify extractNatives Method
Ensure that the extractNatives method correctly extracts native libraries to the specified nativesDir.
go
func (l *Launcher) extractNatives(versionData VersionData, nativesDir string) error {
fmt.Println("Извлечение нативных библиотек...")
libraries, ok := versionData["libraries"].([]interface{})
if !ok {
return fmt.Errorf("не удалось получить библиотеки для распаковки нативов")
}
for _, lib := range libraries {
libInfo, ok := lib.(map[string]interface{})
if !ok {
continue
}
if !l.shouldIncludeLibrary(libInfo) {
continue
}
downloads, ok := libInfo["downloads"].(map[string]interface{})
if !ok {
continue
}
classifiers, ok := downloads["classifiers"].(map[string]interface{})
if !ok {
continue
}
classifierName := l.getNatives(libInfo)
if classifierName == "" {
continue
}
native, ok := classifiers[classifierName].(map[string]interface{})
if !ok {
continue
}
url, ok := native["url"].(string)
if !ok {
continue
}
path, ok := native["path"].(string)
if !ok {
continue
}
expectedHash, _ := native["sha1"].(string)
filePath := filepath.Join(l.minelauncherPath, "libraries", filepath.FromSlash(path))
absFilePath, err := filepath.Abs(filePath)
if err != nil {
fmt.Printf("Не удалось получить абсолютный путь для файла %s: %v\n", filePath, err)
continue
}
dir := filepath.Dir(absFilePath)
os.MkdirAll(dir, os.ModePerm)
if _, err := os.Stat(absFilePath); err == nil {
if expectedHash != "" && l.verifyFileChecksum(absFilePath, expectedHash) {
fmt.Printf("Нативная библиотека уже существует и корректна: %s\n", absFilePath)
} else {
fmt.Printf("Хэш нативной библиотеки не совпадает, перезагружаем файл: %s\n", absFilePath)
os.Remove(absFilePath)
}
}
if _, err := os.Stat(absFilePath); os.IsNotExist(err) {
fmt.Printf("Скачивание нативной библиотеки: %s\n", absFilePath)
err := l.downloadFileWithRetry(url, absFilePath, 4)
if err != nil {
fmt.Printf("Ошибка при скачивании нативной библиотеки %s: %v\n", url, err)
return err
}
}
if expectedHash != "" && !l.verifyFileChecksum(absFilePath, expectedHash) {
return fmt.Errorf("хэш нативной библиотеки не совпадает после загрузки: %s", absFilePath)
}
fmt.Printf("Распаковка нативных библиотек из %s в %s\n", absFilePath, nativesDir)
err = l.unzip(absFilePath, nativesDir)
if err != nil {
return fmt.Errorf("ошибка при распаковке нативной библиотеки %s: %v", absFilePath, err)
}
}
return nil
}
Explanation:
The nativesDir is passed as a parameter and is specific to each version.
Each native library is downloaded (if not already present and verified) and then unzipped into the designated nativesDir.
This ensures that multiple versions can coexist without conflicts in their native libraries.
3. Handle Launching of Custom Versions Appropriately
Ensure that the launcher correctly handles launching custom versions using their specific configurations.
Step 3.1: Review launchMinecraft Method
Ensure that the launchMinecraft method correctly utilizes the version-specific paths and handles all necessary configurations.
go
func (l *Launcher) launchMinecraft(versionData VersionData, authToken, username, uuid, versionID string) error {
fmt.Println("Запуск Minecraft...")
mainClass, ok := versionData["mainClass"].(string)
if !ok {
return fmt.Errorf("не удалось найти mainClass в данных версии")
}
fmt.Println("Main class:", mainClass)
classpath := l.buildClasspath(versionData, versionID)
fmt.Println("Classpath длина:", len(classpath))
if classpath == "" {
return fmt.Errorf("classpath пустой")
}
nativesDir := filepath.Join(l.minelauncherPath, "versions", versionID, "natives")
absNativesDir, err := filepath.Abs(nativesDir)
if err != nil {
return fmt.Errorf("не удалось получить абсолютный путь для nativesDir %s: %v", nativesDir, err)
}
os.MkdirAll(absNativesDir, os.ModePerm)
// Распаковка нативных библиотек
err = l.extractNatives(versionData, absNativesDir)
if err != nil {
return fmt.Errorf("ошибка при извлечении нативных библиотек: %v", err)
}
// Build JVM and game arguments
jvmArgs := l.buildJvmArguments(versionData, absNativesDir)
gameArgs := l.buildGameArguments(versionData, username, versionID, uuid, authToken)
if len(gameArgs) == 0 {
return fmt.Errorf("аргументы игры пустые")
}
// Combine all arguments
args := append(jvmArgs, "-cp", classpath, mainClass)
args = append(args, gameArgs...)
// Write arguments to a temporary file
argsFilePath := filepath.Join(os.TempDir(), "minecraft_args.txt")
err = l.writeArgsToFile(argsFilePath, args)
if err != nil {
return fmt.Errorf("ошибка при создании файла аргументов: %v", err)
}
// Prepare the command to use the argument file
cmd := exec.Command("java", "@"+argsFilePath)
cmd.Dir = l.minelauncherPath
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Println("Команда запуска: java @" + argsFilePath)
err = cmd.Run()
if err != nil {
return fmt.Errorf("ошибка при запуске Minecraft: %v", err)
}
return nil
}
Explanation:
Classpath Construction: The buildClasspath method gathers all necessary library paths, including those of custom versions, ensuring that dependencies are correctly resolved.
Natives Directory: The nativesDir is uniquely defined for each version, preventing any overlap between different versions' native libraries.
Argument Handling: The JVM and game arguments are constructed based on the specific version's configurations, accommodating both official and custom versions.
4. Complete Updated Code with Modifications
Below is the complete updated Go package with the necessary modifications to support custom versions and ensure dedicated natives folders per version.
go
Explanation:
loadCustomVersions Method:
Scans the versions directory for any subdirectories containing a version.json file.
Parses each version.json to extract version information.
Appends each custom version to the list of versions in the manifest.
getVersionManifest Method:
Fetches the official version manifest.
Calls loadCustomVersions to include any custom versions.
Appends custom versions to the Versions slice of the manifest.
GetMinecraftVersions Method:
Lists all versions, marking custom versions appropriately.
Removes the need for separate handling of custom versions since they're integrated into the manifest.
Notes:
Custom Version JSON:
Ensure that each custom version's version.json is correctly formatted and contains all necessary fields (id, type, mainClass, etc.).
If any required fields are missing, the launcher may fail to launch the version.
Error Handling:
The launcher will provide warnings if it encounters issues loading custom versions but will continue operating, allowing other versions to function normally.
5. Additional Recommendations
Validation of Custom Versions:
Implement additional checks to ensure that custom versions' JSON files contain all necessary fields to prevent runtime errors.
Logging Enhancements:
Consider enhancing logging to differentiate between official and custom versions more clearly.
User Interface (Optional):
If you plan to develop a user interface for your launcher, provide visual indicators or separate sections for custom versions to enhance user experience.
Maintenance of Custom Versions:
Encourage users to keep their custom versions' JSON files updated and consistent to ensure smooth operation.
By integrating these changes, your launcher will seamlessly support both official and custom Minecraft versions, ensuring that each version maintains its own set of native libraries. This modular approach enhances the flexibility and scalability of your launcher, allowing users to enjoy a diverse range of Minecraft experiences.
If you encounter any specific issues during implementation or need further assistance with particular parts of the code, feel free to ask!