Skip to content

Commit

Permalink
Десереализация классов из JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
JoCat committed Apr 17, 2021
1 parent c34638b commit a81ade4
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 76 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

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

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@
},
"dependencies": {
"adm-zip": "^0.5.4",
"class-transformer": "^0.4.0",
"cli-progress": "^3.9.0",
"colors": "^1.4.0",
"p-map": "^4.0.0",
"progress-stream": "^2.0.0",
"reflect-metadata": "^0.1.13",
"semver": "^7.3.5",
"source-map-support": "^0.5.19",
"uuid": "^8.3.2",
Expand Down
7 changes: 4 additions & 3 deletions src/main/LauncherServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import 'source-map-support/register';

const version = require("../../package").version
import "source-map-support/register"
import "reflect-metadata"

import { EventEmitter } from "events"

Expand All @@ -35,6 +34,8 @@ import { ModulesManager } from "./modules/ModulesManager"
import { ProfilesManager } from "./profiles/ProfilesManager"
import { UpdatesManager } from "./updates/UpdatesManager"

const version = require("../../package").version

export class LauncherServer extends EventEmitter {
private _ConfigManager: ConfigManager
private _LangManager: LangManager
Expand Down
4 changes: 2 additions & 2 deletions src/main/api/WebSocketManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class WebSocketManager {
// }

try {
parsedMessage = JsonHelper.toJSON(message)
parsedMessage = JsonHelper.fromJSON(message)
} catch (error) {
return this.wsSend(ws, {
uuid: NIL_UUID,
Expand Down Expand Up @@ -88,6 +88,6 @@ export class WebSocketManager {
}

private wsSend(ws: ws, data: wsResponse | wsErrorResponse): void {
ws.send(JsonHelper.toString(data))
ws.send(JsonHelper.toJSON(data))
}
}
18 changes: 18 additions & 0 deletions src/main/api/types/ErrorResponse.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/**
* 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/>.
*/

export interface ErrorResponse {
code: number
message: string
Expand Down
18 changes: 18 additions & 0 deletions src/main/api/types/Request.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/**
* 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/>.
*/

export type RequestData = object

export interface Request {
Expand Down
18 changes: 18 additions & 0 deletions src/main/api/types/Response.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/**
* 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/>.
*/

export type ResponseData = object

export interface Response {
Expand Down
13 changes: 7 additions & 6 deletions src/main/config/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { JsonHelper } from "../helpers/JsonHelper"
import * as fs from "fs"

import { LogHelper } from "../helpers/LogHelper"
import { StorageHelper } from "../helpers/StorageHelper"
import { LauncherServerConfig } from "./LauncherServerConfig"
import fs = require("fs")
import { LauncherServerConfig } from "./types/LauncherServerConfig"

export class ConfigManager {
private config: LauncherServerConfig
Expand All @@ -41,18 +41,19 @@ export class ConfigManager {
}

private load(): void {
const config = fs.readFileSync(StorageHelper.configFile)
const config = fs.readFileSync(StorageHelper.configFile).toString()
try {
this.config = JsonHelper.toJSON(config.toString())
this.config = LauncherServerConfig.fromJSON(config)
} catch (e) {
if (e instanceof SyntaxError) {
LogHelper.error(e)
LogHelper.fatal("Json syntax broken. Try fix or delete LauncherServerConfig.json")
}
LogHelper.fatal(e)
}
}

private save(): void {
fs.writeFileSync(StorageHelper.configFile, JsonHelper.toString(this.config, true))
fs.writeFileSync(StorageHelper.configFile, this.config.toJSON())
}
}
35 changes: 35 additions & 0 deletions src/main/config/types/AuthConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* 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 { AbstractAuthProvider, AbstractAuthProviderConfig } from "../../auth/authProviders/AbstractAuthProvider"
import {
AbstractTextureProvider,
AbstractTextureProviderConfig,
} from "../../auth/textureProviders/AbstractTextureProvider"

export class AuthConfig {
authProvider: AbstractAuthProviderConfig
textureProvider: AbstractTextureProviderConfig

static getDefaults(): AuthConfig {
const defaults = new AuthConfig()
defaults.authProvider = AbstractAuthProvider.getDefaultConfig()
defaults.textureProvider = AbstractTextureProvider.getDefaultConfig()
return defaults
}
}
59 changes: 59 additions & 0 deletions src/main/config/types/LauncherServerConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* 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 { classToPlain, deserialize } from "class-transformer"

import { JsonHelper } from "../../helpers/JsonHelper"
import { AuthConfig } from "./AuthConfig"
import { WebSocketConfig } from "./WebSocketConfig"

// TODO Инфа на будущее, пригодится при версионировании конфигов
// https://github.com/typestack/class-transformer/tree/v0.4.0#using-versioning-to-control-exposed-and-excluded-properties
export class LauncherServerConfig {
configVersion: number
lang: "ru" | "en"
env: Envirovement
mirrors: string[]
auth: AuthConfig
ws: WebSocketConfig

static getDefaults(): LauncherServerConfig {
const config = new LauncherServerConfig()
config.configVersion = 0
config.lang = "ru"
config.env = Envirovement.DEV
config.mirrors = ["https://mirror.aurora-launcher.ru/"]
config.auth = AuthConfig.getDefaults()
config.ws = WebSocketConfig.getDefaults()
return config
}

public toJSON(): string {
return JsonHelper.toJSON(classToPlain(this), true)
}

public static fromJSON(json: string): LauncherServerConfig {
return deserialize(LauncherServerConfig, json)
}
}

export enum Envirovement {
PRODUCTION = "prod",
DEBUG = "debug",
DEV = "dev",
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

import { AbstractAuthProvider, AbstractAuthProviderConfig } from "../auth/authProviders/AbstractAuthProvider"
import {
AbstractTextureProvider,
AbstractTextureProviderConfig,
} from "../auth/textureProviders/AbstractTextureProvider"

export class WebSocketConfig {
address: string
ip: string
Expand Down Expand Up @@ -49,45 +43,3 @@ export class WebSocketConfig {
return defaults
}
}

export class AuthConfig {
authProvider: AbstractAuthProviderConfig
textureProvider: AbstractTextureProviderConfig

static getDefaults(): AuthConfig {
const defaults = new AuthConfig()
defaults.authProvider = AbstractAuthProvider.getDefaultConfig()
defaults.textureProvider = AbstractTextureProvider.getDefaultConfig()
return defaults
}
}

export enum Envirovement {
PRODUCTION = "prod",
DEBUG = "debug",
DEV = "dev",
}

export class LauncherServerConfig {
configVersion: string
lang: string

env: Envirovement

updatesUrl: Array<string>

auth: AuthConfig

ws: WebSocketConfig

static getDefaults(): LauncherServerConfig {
const config = new LauncherServerConfig()
config.configVersion = "0"
config.lang = "ru"
config.env = Envirovement.DEV
config.updatesUrl = ["https://mirror.aurora-launcher.ru/"]
config.auth = AuthConfig.getDefaults()
config.ws = WebSocketConfig.getDefaults()
return config
}
}
4 changes: 2 additions & 2 deletions src/main/download/FabricManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class FabricManager extends MojangManager {

let loaders: any[]
try {
loaders = JsonHelper.toJSON(loadersData)
loaders = JsonHelper.fromJSON(loadersData)
} catch (error) {
LogHelper.debug(error)
LogHelper.error("Error parsing JSON data")
Expand All @@ -99,7 +99,7 @@ export class FabricManager extends MojangManager {
}

try {
return JsonHelper.toJSON(versionData)
return JsonHelper.fromJSON(versionData)
} catch (error) {
LogHelper.debug(error)
return
Expand Down
6 changes: 3 additions & 3 deletions src/main/download/MirrorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class MirrorManager {
* @param dirName - Название конечной папки
*/
async downloadClient(clientName: string, dirName: string): Promise<void> {
const mirrors: string[] = App.ConfigManager.getConfig().updatesUrl
const mirrors: string[] = App.ConfigManager.getConfig().mirrors
const clientDir = path.resolve(StorageHelper.updatesDir, dirName)
if (fs.existsSync(clientDir)) return LogHelper.error(App.LangManager.getTranslate("MirrorManager.dirExist"))

Expand Down Expand Up @@ -82,7 +82,7 @@ export class MirrorManager {

//Profiles
App.ProfilesManager.createProfile({
...JsonHelper.toJSON(profile),
...JsonHelper.fromJSON(profile),
clientDir: dirName,
} as ClientProfileConfig)
LogHelper.info(App.LangManager.getTranslate("MirrorManager.client.success"))
Expand All @@ -94,7 +94,7 @@ export class MirrorManager {
* @param dirName - Название конечной папки
*/
async downloadAssets(assetsName: string, dirName: string): Promise<void> {
const mirrors: string[] = App.ConfigManager.getConfig().updatesUrl
const mirrors: string[] = App.ConfigManager.getConfig().mirrors
const assetsDir = path.resolve(StorageHelper.updatesDir, dirName)
if (fs.existsSync(assetsDir)) return LogHelper.error(App.LangManager.getTranslate("MirrorManager.dirExist"))

Expand Down
Loading

0 comments on commit a81ade4

Please sign in to comment.