From eedf403690aeda50f972f8f61a49c7dec575e975 Mon Sep 17 00:00:00 2001 From: zerodegress Date: Thu, 26 Jan 2023 11:24:13 +0800 Subject: [PATCH] changed README.md and build changes before --- README.md | 3 ++ lib/builder/classic.d.ts | 20 ++++--- lib/builder/classic.js | 109 +++++++++++++++++++++++++++++++++------ 3 files changed, 107 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 55c490d..38d8754 100644 --- a/README.md +++ b/README.md @@ -9,5 +9,8 @@ rw-toml is avaliable via npm. npm install rw-toml ```` +## Usage +rw-toml are mostlt used by another project named [rw-build](https://github.com/zerodegress/rw-build),which is a CLI Rusted Warfare mod builder running on [Node.js](https://nodejs.org). + ## License rw-toml is licensed under the GNU-GPL v3 license agreement. See the LICENSE file for more information. \ No newline at end of file diff --git a/lib/builder/classic.d.ts b/lib/builder/classic.d.ts index 8c539d7..eb688fb 100644 --- a/lib/builder/classic.d.ts +++ b/lib/builder/classic.d.ts @@ -2,6 +2,7 @@ import { Optional } from "../optional"; import { Builder } from "."; import { Result } from "../result"; import { StandardIni, CommonToml } from "../config"; +export declare function normalize(pat: string): string; export interface FileLike { filename: string; dirname: string; @@ -11,14 +12,14 @@ export declare class PathLike { constructor(path: string); } export declare enum ClassicSourceFileType { - TOML = 0, - TXT = 1, - PNG = 2, - JPEG = 3, - OGG = 4, - WAV = 5, - MP3 = 6, - UNKNOWN_ASSET = 7 + TOML = "TOML", + TXT = "TXT", + PNG = "PNG", + JPEG = "JPEG", + OGG = "OGG", + WAV = "WAV", + MP3 = "MP3", + UNKNOWN_ASSET = "UNKNOWN_ASSET" } export declare class ClassicSourceFile { private type; @@ -34,6 +35,9 @@ export declare class ClassicSourceFile { isUnknownAsset(): boolean; toml(callback: (content: CommonToml) => void): ClassicSourceFile; txt(callback: (content: string) => void): ClassicSourceFile; + image(callback: (content: PathLike) => void): ClassicSourceFile; + soundOrMusic(callback: (content: PathLike) => void): ClassicSourceFile; + unknownAsset(callback: (content: PathLike) => void): ClassicSourceFile; } export declare class ClassicSource implements FileLike { filename: string; diff --git a/lib/builder/classic.js b/lib/builder/classic.js index d9df8b7..1bbc3fb 100644 --- a/lib/builder/classic.js +++ b/lib/builder/classic.js @@ -3,11 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.ClassicBuilderSync = exports.ClassicTarget = exports.ClassicTargetFile = exports.CopyFromSource = exports.ClassicTargetFileType = exports.ClassicSource = exports.ClassicSourceFile = exports.ClassicSourceFileType = exports.PathLike = void 0; +exports.ClassicBuilderSync = exports.ClassicTarget = exports.ClassicTargetFile = exports.CopyFromSource = exports.ClassicTargetFileType = exports.ClassicSource = exports.ClassicSourceFile = exports.ClassicSourceFileType = exports.PathLike = exports.normalize = void 0; const optional_1 = require("../optional"); const path_browserify_1 = __importDefault(require("path-browserify")); +const normalize_path_1 = __importDefault(require("normalize-path")); const result_1 = require("../result"); const config_1 = require("../config"); +function normalize(pat) { + return (0, normalize_path_1.default)(path_browserify_1.default.normalize(pat)); +} +exports.normalize = normalize; class PathLike { constructor(path) { this.path = path; @@ -16,18 +21,21 @@ class PathLike { exports.PathLike = PathLike; var ClassicSourceFileType; (function (ClassicSourceFileType) { - ClassicSourceFileType[ClassicSourceFileType["TOML"] = 0] = "TOML"; - ClassicSourceFileType[ClassicSourceFileType["TXT"] = 1] = "TXT"; - ClassicSourceFileType[ClassicSourceFileType["PNG"] = 2] = "PNG"; - ClassicSourceFileType[ClassicSourceFileType["JPEG"] = 3] = "JPEG"; - ClassicSourceFileType[ClassicSourceFileType["OGG"] = 4] = "OGG"; - ClassicSourceFileType[ClassicSourceFileType["WAV"] = 5] = "WAV"; - ClassicSourceFileType[ClassicSourceFileType["MP3"] = 6] = "MP3"; - ClassicSourceFileType[ClassicSourceFileType["UNKNOWN_ASSET"] = 7] = "UNKNOWN_ASSET"; + ClassicSourceFileType["TOML"] = "TOML"; + ClassicSourceFileType["TXT"] = "TXT"; + ClassicSourceFileType["PNG"] = "PNG"; + ClassicSourceFileType["JPEG"] = "JPEG"; + ClassicSourceFileType["OGG"] = "OGG"; + ClassicSourceFileType["WAV"] = "WAV"; + ClassicSourceFileType["MP3"] = "MP3"; + ClassicSourceFileType["UNKNOWN_ASSET"] = "UNKNOWN_ASSET"; })(ClassicSourceFileType = exports.ClassicSourceFileType || (exports.ClassicSourceFileType = {})); class ClassicSourceFile { constructor(type, content) { this.type = type; + if (content instanceof PathLike) { + content.path = normalize(content.path); + } this.content = content; } static toml(content) { @@ -65,7 +73,7 @@ class ClassicSourceFile { return this.type == ClassicSourceFileType.TXT; } isImage() { - return this.type == ClassicSourceFileType.PNG; + return this.type == ClassicSourceFileType.PNG || this.type == ClassicSourceFileType.JPEG; } isSoundOrMusic() { return this.type == ClassicSourceFileType.OGG || this.type == ClassicSourceFileType.MP3 || this.type == ClassicSourceFileType.WAV; @@ -95,15 +103,49 @@ class ClassicSourceFile { } return this; } + image(callback) { + if (this.isImage()) { + if (this.content instanceof PathLike) { + callback(this.content); + } + else { + throw new Error('internal error'); + } + } + return this; + } + soundOrMusic(callback) { + if (this.isSoundOrMusic()) { + if (this.content instanceof PathLike) { + callback(this.content); + } + else { + throw new Error('internal error'); + } + } + return this; + } + unknownAsset(callback) { + if (this.isUnknownAsset()) { + if (this.content instanceof PathLike) { + callback(this.content); + } + else { + throw new Error('internal error'); + } + } + return this; + } } exports.ClassicSourceFile = ClassicSourceFile; class ClassicSource { constructor(filename, dirname, path, content) { this.filename = filename; - this.dirname = dirname; - this.path = path; + this.dirname = normalize(dirname); + this.path = normalize(path); this.target = (0, optional_1.none)(); if (content instanceof PathLike) { + content.path = normalize(content.path); this.sourceFile = ClassicSourceFile.asset(content); } else if (typeof content == 'object') { @@ -166,7 +208,9 @@ exports.ClassicTargetFile = ClassicTargetFile; class ClassicTarget { constructor(filename, dirname, source, targetFile) { this.filename = filename; - this.dirname = dirname; + this.dirname = normalize(dirname); + source.dirname = normalize(source.dirname); + source.path = normalize(source.path); this.source = source; this.targetFile = targetFile; } @@ -209,9 +253,9 @@ class ClassicBuilderSync { }); (0, optional_1.optional)(this.context.sources.find((value) => value.path == path)).some((source) => { source.built((target) => { throw (0, result_1.ok)({ source, target }); }).unbuilt(() => { - this.buildSync(path).ok((target) => { throw (0, result_1.ok)({ source, target }); }).err((error) => { throw (0, result_1.err)(error); }); + this.buildSync(path, starterPath).ok((target) => { throw (0, result_1.ok)({ source, target }); }).err((error) => { throw (0, result_1.err)(error); }); }); - }).none(() => { throw (0, result_1.err)(new Error('requirement does not exists')); }); + }).none(() => { throw (0, result_1.err)(new Error(`requirement "${path}" does not exists`)); }); throw new Error('unreachable!'); } catch (result) { @@ -257,7 +301,20 @@ class ClassicBuilderSync { return value.join(); } else { - return value.toString(); + let outputValue = value.toString(); + if (secMain == 'core' && key == 'copyFrom') { + let requirePath = normalize(value.toString().trim()); + if (requirePath.startsWith('/')) { + requirePath = requirePath.replace(/^\//, ''); + outputValue = 'ROOT:' + requirePath; + } + else { + requirePath = normalize(path_browserify_1.default.join(source.dirname, requirePath)); + outputValue = requirePath; + } + this.requireSync(requirePath, starterPath != undefined ? starterPath : source.path).err((error) => { throw (0, result_1.err)(error); }); + } + return outputValue.replace(/\.toml$/, '.ini'); } })(); }); @@ -268,8 +325,26 @@ class ClassicBuilderSync { break; } } - const target = new ClassicTarget(source.filename.replace('.toml', '.ini'), source.dirname, source, ClassicTargetFile.ini(ini)); + const target = new ClassicTarget(source.filename.replace(/\.toml$/, '.ini'), source.dirname, source, ClassicTargetFile.ini(ini)); + source.target = (0, optional_1.some)(target); + this.context.targets.push(target); + throw (0, result_1.ok)(target); + }).txt((content) => { + const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.txt(content)); source.target = (0, optional_1.some)(target); + this.context.targets.push(target); + throw (0, result_1.ok)(target); + }).image((content) => { + const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.asset()); + this.context.targets.push(target); + throw (0, result_1.ok)(target); + }).soundOrMusic((content) => { + const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.asset()); + this.context.targets.push(target); + throw (0, result_1.ok)(target); + }).unknownAsset((content) => { + const target = new ClassicTarget(source.filename, source.dirname, source, ClassicTargetFile.asset()); + this.context.targets.push(target); throw (0, result_1.ok)(target); }); }).none(() => { throw (0, result_1.err)(new Error('source does not exists')); });