Skip to content

Commit

Permalink
Скачивание клиентов с фабриком by 𝓟𝓱𝓸𝓽𝓸𝓷
Browse files Browse the repository at this point in the history
Co-Authored-By: FIXxp <41650742+FIXxp@users.noreply.github.com>
  • Loading branch information
JoCat and FIXxp committed Feb 18, 2021
1 parent 9ce7359 commit 4ada635
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 1 deletion.
55 changes: 55 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"author": "AuroraTeam",
"contributors": [
"JoCat (https://github.com/JoCat)",
"LoomeL (https://github.com/LoomeL)"
"LoomeL (https://github.com/LoomeL)",
"FIXxp (https://github.com/FIXxp)"
],
"main": "LauncherServer.js",
"scripts": {
Expand Down Expand Up @@ -64,5 +65,8 @@
"style": "module",
"parser": "typescript"
}
},
"dependencies": {
"mvn-artifact-download": "^5.1.0"
}
}
37 changes: 37 additions & 0 deletions src/main/commands/updates/DownloadFabricClientCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* AuroraLauncher LauncherServer - Server for AuroraLauncher
* Copyright (C) 2020 - 2021 AuroraTeam
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { FabricManager } from "../../download/FabricManager"
import { LogHelper } from "../../helpers/LogHelper"
import { App } from "../../LauncherServer"
import { AbstractCommand, Category } from "../AbstractCommand"

export class DownloadFabricClientCommand extends AbstractCommand {
constructor() {
super("downloadfabrciclient", "Загрузить клиент c Fabric", Category.UPDATES, "<version> <folder name>")
}

async invoke(...args: string[]): Promise<void> {
const [clientVer, dirName] = args
if (!clientVer) return LogHelper.error("Укажите название/версию клиента!")
if (!dirName) return LogHelper.error("Укажите название папки для клиента!")
App.CommandsManager.console.pause()
new FabricManager().downloadClient(clientVer, dirName)
App.CommandsManager.console.resume()
}
}
172 changes: 172 additions & 0 deletions src/main/download/FabricManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/**
* AuroraLauncher LauncherServer - Server for AuroraLauncher
* Copyright (C) 2020 - 2021 AuroraTeam
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

// TODO Ещё больше try/catch для отлова возможных ошибок
// TODO Перевод

import * as fs from "fs"
import * as path from "path"
import { URL } from "url"

import * as download from "mvn-artifact-download"
import * as rimraf from "rimraf"

import { LogHelper } from "../helpers/LogHelper"
import { StorageHelper } from "../helpers/StorageHelper"
import { ZipHelper } from "../helpers/ZipHelper"
import { App } from "../LauncherServer"
import { ClientProfileConfig } from "../profiles/ProfileConfig"
import { MojangManager } from "./MojangManager"

export class FabricManager extends MojangManager {
/**
* Скачивание клиента с зеркала Mojang
* @param clientVer - Версия клиента
* @param dirName - Название конечной папки
*/
async downloadClient(clientVer: string, dirName: string): Promise<void> {
const fabricVersion: any = await this.getFabricVersionInfo(clientVer)
if (fabricVersion === undefined) return

const mojangVersion: any = await this.getVersionInfo(clientVer)
if (mojangVersion === undefined) return

const client: any = mojangVersion.downloads.client
const libraries: any[] = mojangVersion.libraries

// Client
const clientDir = path.resolve(StorageHelper.updatesDir, dirName)
if (fs.existsSync(clientDir)) return LogHelper.error(App.LangManager.getTranslate("MirrorManager.dirExist"))
fs.mkdirSync(clientDir)

const clientFile = await this.downloadFile(new URL(client.url))
fs.copyFileSync(clientFile, path.resolve(clientDir, "minecraft.jar"))

// Libraries
const librariesDir = path.resolve(clientDir, "libraries")
fs.mkdirSync(librariesDir)

LogHelper.info("Download libraries and natives, please wait...")
const librariesList = this.librariesParse(libraries)

await Promise.all(
Array.from(librariesList.libraries).map(async (lib) => {
const libFile = await this.downloadFile(new URL(lib, "https://libraries.minecraft.net/"), false)
fs.mkdirSync(path.resolve(librariesDir, path.dirname(lib)), { recursive: true })
fs.copyFileSync(libFile, path.resolve(librariesDir, lib))
})
)

LogHelper.info("Download Fabric libraries, please wait...")
const librariesListFabric = this.librariesParseFabric(fabricVersion.libraries)

//Хз мб говно, но вроде работает
await Promise.all(
Array.from(librariesListFabric.libraries).map(async (lib) => {
// TODO Поменять :)
//Скачивается тупо в папку с библиотеками....
download.default(lib, librariesDir, "https://maven.fabricmc.net/")
})
)

// Natives
const nativesDir = path.resolve(clientDir, "natives")
fs.mkdirSync(nativesDir)

await Promise.all(
Array.from(librariesList.natives).map(async (native) => {
const nativeFile = await this.downloadFile(new URL(native, "https://libraries.minecraft.net/"), false)
await ZipHelper.unzipArchive(nativeFile, nativesDir, [".dll", ".so", ".dylib", ".jnilib"])
})
)

rimraf(path.resolve(StorageHelper.tempDir, "*"), (e) => {
if (e !== null) LogHelper.warn(e)
})

//Profiles
App.ProfilesManager.createProfile({
version: clientVer,
clientDir: dirName,
assetsDir: `assets${mojangVersion.assets}`,
assetsIndex: mojangVersion.assets,
mainClass: fabricVersion.mainClass,
} as ClientProfileConfig)
LogHelper.info("Done")
}

/**
* @param libraries Объект со списком библиотек от Fabric
*/
librariesParseFabric(libraries: any[]): { libraries: Set<string> } {
const filteredData = {
libraries: new Set() as Set<string>,
}
libraries.forEach((lib) => {
filteredData.libraries.add(lib.name)
})

return filteredData
}

async getFabricVersionInfo(version: string): Promise<any> {
let loadersData
try {
loadersData = await this.readFile(`https://meta.fabricmc.net/v2/versions/loader/${version}`)
} catch (error) {
LogHelper.debug(error)
LogHelper.error("FabricMC site unavailable")
return
}

let loaders: any[]
try {
loaders = JSON.parse(loadersData)
} catch (error) {
LogHelper.debug(error)
LogHelper.error("Error parsing JSON data")
return
}

//Не мы такие, жизнь такая...
if (loaders.length === 0) {
LogHelper.error("Version %s not found", version)
return
}

const data = loaders.find((data: any) => data.loader.stable === true)

let versionData
try {
versionData = await this.readFile(
`https://meta.fabricmc.net/v2/versions/loader/${version}/${data.loader.version}/profile/json`
)
} catch (error) {
LogHelper.debug(error)
LogHelper.error("FabricMC site unavailable") // TODO Поменять
return
}

try {
return JSON.parse(versionData)
} catch (error) {
LogHelper.debug(error)
return
}
}
}

0 comments on commit 4ada635

Please sign in to comment.