feat: more things for this.
This commit is contained in:
parent
53375ea11c
commit
7a43d233e3
8 changed files with 127 additions and 35 deletions
|
|
@ -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",
|
||||
|
|
|
|||
63
src/compilers/ui/buildcache.ts
Normal file
63
src/compilers/ui/buildcache.ts
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
import fs from "fs/promises"
|
||||
|
||||
export class BuildCache {
|
||||
private static queue: Promise<void> = Promise.resolve()
|
||||
|
||||
private static async enqueue<T>(task: () => Promise<T>): Promise<T> {
|
||||
let result!: T
|
||||
this.queue = this.queue.then(async () => {
|
||||
result = await task()
|
||||
})
|
||||
return this.queue.then(() => result)
|
||||
}
|
||||
|
||||
static async get<T = unknown>(key: string): Promise<T | null> {
|
||||
return this.enqueue<T>(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<T = unknown>(key: string, defaultValue: (() => T) | T): Promise<T> {
|
||||
const outVal = typeof defaultValue === "function" ? (defaultValue as () => T)() : defaultValue
|
||||
return this.get<T>(key).then(value => value ?? outVal)
|
||||
}
|
||||
|
||||
static async getWithSetDefault<T>(key: string, defaultValue: (() => T) | T): Promise<T> {
|
||||
const outVal = typeof defaultValue === "function" ? (defaultValue as () => T)() : defaultValue
|
||||
|
||||
return this.enqueue(async () => {
|
||||
let data: Record<string, any> = {}
|
||||
|
||||
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")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -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++
|
||||
}),
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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()]
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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],
|
||||
},
|
||||
],
|
||||
})
|
||||
}
|
||||
|
|
@ -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"
|
||||
|
|
|
|||
35
test/app.ts
35
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 ".."
|
||||
|
|
|
|||
Reference in a new issue