From 7a43d233e31d315f414f1d5d1a5009c417953c68 Mon Sep 17 00:00:00 2001 From: Asaki Yuki Date: Wed, 28 Jan 2026 10:45:47 +0700 Subject: [PATCH] feat: more things for this. --- package.json | 2 +- src/compilers/ui/buildcache.ts | 63 ++++++++++++++++++++++++++++++++++ src/compilers/ui/builder.ts | 1 + src/compilers/ui/installer.ts | 17 +++++++++ src/compilers/ui/linker.ts | 22 ++++++++++++ src/compilers/ui/manifest.ts | 21 ++++++++++++ src/index.ts | 1 + test/app.ts | 35 +------------------ 8 files changed, 127 insertions(+), 35 deletions(-) create mode 100644 src/compilers/ui/buildcache.ts diff --git a/package.json b/package.json index 7d29401..614cd68 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "asajs", - "version": "4.0.0", + "version": "4.0.0-indev-1", "description": "Create your Minecraft JSON-UI resource packs using JavaScript", "keywords": [ "Minecraft", diff --git a/src/compilers/ui/buildcache.ts b/src/compilers/ui/buildcache.ts new file mode 100644 index 0000000..97b1f5b --- /dev/null +++ b/src/compilers/ui/buildcache.ts @@ -0,0 +1,63 @@ +import fs from "fs/promises" + +export class BuildCache { + private static queue: Promise = Promise.resolve() + + private static async enqueue(task: () => Promise): Promise { + let result!: T + this.queue = this.queue.then(async () => { + result = await task() + }) + return this.queue.then(() => result) + } + + static async get(key: string): Promise { + return this.enqueue(async () => { + try { + return await fs.readFile("build/cache.json", "utf-8").then(data => JSON.parse(data)[key] ?? null) + } catch (error) { + return null + } + }) + } + + static async getWithDefault(key: string, defaultValue: (() => T) | T): Promise { + const outVal = typeof defaultValue === "function" ? (defaultValue as () => T)() : defaultValue + return this.get(key).then(value => value ?? outVal) + } + + static async getWithSetDefault(key: string, defaultValue: (() => T) | T): Promise { + const outVal = typeof defaultValue === "function" ? (defaultValue as () => T)() : defaultValue + + return this.enqueue(async () => { + let data: Record = {} + + try { + data = JSON.parse(await fs.readFile("build/cache.json", "utf-8")) + } catch {} + + if (key in data) return data[key] + data[key] = outVal + + await fs.writeFile("build/cache.json", JSON.stringify(data), "utf-8") + return outVal + }) + } + + static async set(key: string, value: unknown) { + return this.enqueue(async () => { + try { + return fs.writeFile( + "build/cache.json", + JSON.stringify({ + ...(await fs.readFile("build/cache.json", "utf-8").then(data => JSON.parse(data))), + [key]: value, + }), + "utf-8", + ) + } catch (error) { + return fs.writeFile("build/cache.json", JSON.stringify({ [key]: value }), "utf-8") + } + }) + } +} diff --git a/src/compilers/ui/builder.ts b/src/compilers/ui/builder.ts index 47c9830..efde52f 100644 --- a/src/compilers/ui/builder.ts +++ b/src/compilers/ui/builder.ts @@ -19,6 +19,7 @@ async function buildUI() { .catch(async () => await fs.mkdir(outFile.split(/\\|\//g).slice(0, -1).join("/"), { recursive: true })) await fs.writeFile(outFile, JSON.stringify(value), "utf-8") + build.delete(file) i++ }), ) diff --git a/src/compilers/ui/installer.ts b/src/compilers/ui/installer.ts index e69de29..191aa05 100644 --- a/src/compilers/ui/installer.ts +++ b/src/compilers/ui/installer.ts @@ -0,0 +1,17 @@ +import os from "os" +import path from "path" + +function getGamedataPath() { + switch (os.platform()) { + case "win32": { + if (/Windows (10|11)/.test(os.version())) { + return path.join(process.env.APPDATA!, "Minecraft Bedrock\\Users\\Shared\\games\\com.mojang") + } + } + + default: { + console.error(`Your platform is not supported the install feature yet! \nYour OS version: ${os.version()}`) + process.exit(1) + } + } +} diff --git a/src/compilers/ui/linker.ts b/src/compilers/ui/linker.ts index e4fd71b..abf8692 100644 --- a/src/compilers/ui/linker.ts +++ b/src/compilers/ui/linker.ts @@ -1,4 +1,16 @@ import fs from "fs/promises" +import { BuildCache } from "./buildcache.js" +import { RandomString } from "../../components/Utils.js" + +const HEX: string[] = Array.from({ length: 256 }, (_, i) => i.toString(16).toUpperCase().padStart(2, "0")) +function genUUID() { + const b = Array.from({ length: 16 }, () => Math.floor(Math.random() * 256)) + return `${HEX[b[0]]}${HEX[b[1]]}${HEX[b[2]]}${HEX[b[3]]}-${HEX[b[4]]}${HEX[b[5]]}-${ + HEX[b[6]] + }${HEX[b[7]]}-${HEX[b[8]]}${HEX[b[9]]}-${HEX[b[10]]}${HEX[b[11]]}${HEX[b[12]]}${ + HEX[b[13]] + }${HEX[b[14]]}${HEX[b[15]]}` +} export async function clearBuild() { await fs.rm("build/build", { recursive: true, force: true }) @@ -10,3 +22,13 @@ export async function createBuildFolder() { .catch(() => fs.mkdir("build")) .then(() => clearBuild()) } + +export async function getBuildFolderName() { + return await BuildCache.getWithSetDefault("build-key", () => RandomString(16)) +} + +export async function getUUID(): Promise<[string, string]> { + return await BuildCache.getWithSetDefault("uuid", () => { + return [genUUID(), genUUID()] + }) +} diff --git a/src/compilers/ui/manifest.ts b/src/compilers/ui/manifest.ts index e69de29..69a57e4 100644 --- a/src/compilers/ui/manifest.ts +++ b/src/compilers/ui/manifest.ts @@ -0,0 +1,21 @@ +import { getUUID } from "./linker.js" + +export async function genManifest() { + const [uuid1, uuid2] = await getUUID() + return JSON.stringify({ + format_version: 2, + header: { + name: "AsaJS UI", + uuid: uuid1, + description: "A framework for creating UIs for AsaJS.", + version: [4, 0, 0], + }, + modules: [ + { + type: "resources", + uuid: uuid2, + version: [4, 0, 0], + }, + ], + }) +} diff --git a/src/index.ts b/src/index.ts index a250a13..e337632 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,6 @@ import "./compilers/PreCompile.js" import "./compilers/ui/builder.js" +import "./compilers/ui/installer.js" export { Animation } from "./components/Animation.js" export { AnimationKeyframe } from "./components/AnimationKeyframe.js" diff --git a/test/app.ts b/test/app.ts index 3ca856a..42ea928 100644 --- a/test/app.ts +++ b/test/app.ts @@ -1,34 +1 @@ -import { Anchor, AnimationOffset, Easing, KeyframeSize, Panel } from ".." - -const animation = AnimationOffset( - "smooth_loop", - { - to: [10, 10], - duration: 1.5, - }, - { - to: [1, 1], - }, - 1, - { - from: [10, 10], - to: [20, 20], - }, - { - to: [1, 1], - }, -).setLoop(true) - -const panel = Panel({ - anchor: Anchor.BOTTOM_LEFT, -}).addAnimations( - animation, - KeyframeSize({ - from: [10, 10], - to: [20, 20], - duration: 0.3, - easing: Easing.LINEAR, - }), -) - -console.log(animation, panel) +import { Panel } from ".."