kratos-core
Version:
kratos-core is an open-source, elegant and battery-included Minecraft Launcher API module written in TypeScript in order to be the based of KratosLauncher
1 lines • 5 kB
JavaScript
"use strict";var __awaiter=function(e,t,r,i){return new(r||(r=Promise))(function(s,n){function o(e){try{c(i.next(e))}catch(e){n(e)}}function a(e){try{c(i.throw(e))}catch(e){n(e)}}function c(e){var t;e.done?s(e.value):((t=e.value)instanceof r?t:new r(function(e){e(t)})).then(o,a)}c((i=i.apply(e,t||[])).next())})};Object.defineProperty(exports,"__esModule",{value:!0}),exports.VersionWorkspace=exports.AssetWorkspace=exports.LauncherWorkspace=exports.Workspace=void 0;const fs_extra_1=require("fs-extra"),path_1=require("path"),utils_1=require("./utils"),path=require("path");class Workspace{getDirectory(){return this.directory}makeDirectory(e){return __awaiter(this,void 0,void 0,function*(){utils_1.Preconditions.notNull(e);let t=(0,path_1.join)(this.directory.toString(),e.toString());return this.ensurePath(t),yield(0,fs_extra_1.mkdir)((0,path_1.join)(this.directory.toString(),e.toString())),t})}ensurePath(e){return __awaiter(this,void 0,void 0,function*(){let t=(0,path_1.dirname)(e.toString());yield(0,fs_extra_1.ensureDir)(t)})}createWriter(e,t){utils_1.Preconditions.notNull(e);let r=(0,path_1.join)(this.getDirectory().toString(),e.toString()),i=(0,path_1.dirname)(r.toString());return(0,fs_extra_1.ensureDirSync)(i),(0,fs_extra_1.createWriteStream)(r,t)}writeAsFile(e,t,r){return __awaiter(this,void 0,void 0,function*(){utils_1.Preconditions.notNull(e),utils_1.Preconditions.notNull(t);let i=(0,path_1.join)(this.getDirectory().toString(),e.toString());return yield this.ensurePath(i),yield(0,fs_extra_1.writeFile)(i,t,r),i})}constructor(e){utils_1.Preconditions.notNull(e),this.directory=e,(0,fs_extra_1.existsSync)(this.directory)||(0,fs_extra_1.ensureDirSync)(this.directory.toString())}}exports.Workspace=Workspace,exports.LauncherWorkspace=class extends Workspace{getAssetWorkspace(){return this.assetWorkspace}getVersionWorkspace(){return this.versionWorkspace}getLibraryWorkspace(){return this.libraryWorkspace}constructor(e){super(e),this.assetWorkspace=new AssetWorkspace((0,path_1.join)(e.toString(),"assets")),this.versionWorkspace=new VersionWorkspace((0,path_1.join)(e.toString(),"versions")),this.libraryWorkspace=new LibraryWorkspace((0,path_1.join)(e.toString(),"libraries"))}};class AssetWorkspace extends Workspace{getObjectsPath(){return(0,path_1.join)(this.getDirectory().toString(),"objects")}createAssetWriter(e,t){let r=(0,path_1.join)("objects",e.slice(0,2),e);return this.createWriter(r,t)}getAssetIndexesWorkspace(){return this.indexesWorkspace}constructor(e){super(e),this.indexesWorkspace=new AssetIndexWorkspace(path.join(e.toString(),"indexes"))}}exports.AssetWorkspace=AssetWorkspace;class VersionWorkspace extends Workspace{getManifestPath(){return path.join(this.getDirectory().toString(),"version_manifest_v2.json")}existsManifest(){return(0,fs_extra_1.exists)(this.getManifestPath())}writeManifest(e,t){return __awaiter(this,void 0,void 0,function*(){return this.writeAsFile("version_manifest_v2.json",e,t)})}readManifest(){return __awaiter(this,void 0,void 0,function*(){if(!(yield this.existsManifest()))throw Error("Manifest file is not exist.");return yield(0,fs_extra_1.readJson)(this.getManifestPath(),{throws:!0})})}getPackageDirectory(){return __awaiter(this,void 0,void 0,function*(){return path.join(this.getDirectory().toString(),"packages")})}getPackageVersionFilePath(e){return __awaiter(this,void 0,void 0,function*(){return utils_1.Preconditions.notNull(e),path.join((yield this.getPackageDirectory()).toString(),[e,"json"].join("."))})}hasPackageVersion(e){return __awaiter(this,void 0,void 0,function*(){return(0,fs_extra_1.exists)((yield this.getPackageVersionFilePath(e)))})}writeVersionPackage(e,t,r){return __awaiter(this,void 0,void 0,function*(){yield(0,fs_extra_1.ensureDir)((yield this.getPackageDirectory())),yield(0,fs_extra_1.writeFile)((yield this.getPackageVersionFilePath(e)),t,r)})}readVersionPackage(e){return __awaiter(this,void 0,void 0,function*(){let t=yield this.getPackageVersionFilePath(e);if(!(yield(0,fs_extra_1.exists)(t)))throw Error(`Version file path not found: ${t}`);return yield(0,fs_extra_1.readJson)(t,{throws:!0})})}constructor(e){super(e)}}exports.VersionWorkspace=VersionWorkspace;class LibraryWorkspace extends Workspace{ensureDirname(e){utils_1.Preconditions.notNull(path);let t=path.resolve(this.getDirectory().toString(),e);(0,fs_extra_1.ensureDirSync)((0,path_1.dirname)(t))}constructor(e){super(e)}}class AssetIndexWorkspace extends Workspace{getIndexFilePath(e){return path.join(this.getDirectory().toString(),e)}getIndex(e){let t=this.getIndexFilePath(e);if(!(0,fs_extra_1.existsSync)(t))throw Error(`Asset index file not exists: ${t}`);return(0,fs_extra_1.readJsonSync)(t,{throws:!0})}writeIndex(e,t){let r=this.getIndexFilePath(e);(0,fs_extra_1.writeJsonSync)(r,t)}hasIndex(e){let t=this.getIndexFilePath(e);return(0,fs_extra_1.existsSync)(t)}getAllIndexes(e){let t=(0,fs_extra_1.readdirSync)(this.getDirectory());return void 0!==e&&(t=t.filter(t=>!e.includes(t))),t}constructor(e){super(e)}}