Skip to content

Commit 04fbe31

Browse files
committed
📝 assets.ajmeta
- Added an `assets.ajmeta` file to keep track of exported resources. - Renamed `.ajmeta` to `.data.ajmeta` to allow exporting both data and assets into the same folder. Yay beet users!
1 parent b3032a2 commit 04fbe31

File tree

2 files changed

+100
-46
lines changed

2 files changed

+100
-46
lines changed

src/systems/datapackCompiler.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -374,14 +374,10 @@ async function generateRootEntityPassengers(rig: IRenderedRig, rigHash: string)
374374
return passengers.toString()
375375
}
376376

377-
class AJMeta {
378-
public datapack = {
379-
files: new Set<string>(),
380-
}
381-
public oldDatapack = {
382-
files: new Set<string>(),
383-
}
384-
private oldContent: Record<string, { datapack: { files: string[] } }> = {}
377+
class DataPackAJMeta {
378+
public files = new Set<string>()
379+
public oldFiles = new Set<string>()
380+
private oldContent: Record<string, { files?: string[] }> = {}
385381

386382
constructor(
387383
public path: string,
@@ -396,29 +392,29 @@ class AJMeta {
396392
const data = this.oldContent[this.exportNamespace]
397393
const lastData = this.oldContent[this.lastUsedExportNamespace]
398394
if (lastData) {
399-
for (const file of lastData.datapack.files) {
400-
this.oldDatapack.files.add(PathModule.join(this.dataPackFolder, file))
395+
if (!Array.isArray(lastData.files)) lastData.files = []
396+
for (const file of lastData.files) {
397+
this.oldFiles.add(PathModule.join(this.dataPackFolder, file))
401398
}
402399
delete this.oldContent[this.lastUsedExportNamespace]
403400
}
404401
if (data) {
405-
for (const file of data.datapack.files) {
406-
this.oldDatapack.files.add(PathModule.join(this.dataPackFolder, file))
402+
if (!Array.isArray(data.files)) data.files = []
403+
for (const file of data.files) {
404+
this.oldFiles.add(PathModule.join(this.dataPackFolder, file))
407405
}
408406
delete this.oldContent[this.exportNamespace]
409407
}
410408
}
411409

412410
write() {
413411
const folder = PathModule.dirname(this.path)
414-
const content: AJMeta['oldContent'] = {
412+
const content: DataPackAJMeta['oldContent'] = {
415413
...this.oldContent,
416414
[this.exportNamespace]: {
417-
datapack: {
418-
files: Array.from(this.datapack.files).map(v =>
419-
PathModule.relative(folder, v).replace(/\\/g, '/')
420-
),
421-
},
415+
files: Array.from(this.files).map(v =>
416+
PathModule.relative(folder, v).replace(/\\/g, '/')
417+
),
422418
},
423419
}
424420
fs.writeFileSync(this.path, autoStringify(sortObjectKeys(content)))
@@ -548,10 +544,10 @@ export async function compileDataPack(options: {
548544
formatVersion: Infinity, // We are living in the future! 🤖
549545
})
550546

551-
let ajmeta: AJMeta | null = null
547+
let ajmeta: DataPackAJMeta | null = null
552548
if (aj.data_pack_export_mode === 'raw') {
553-
ajmeta = new AJMeta(
554-
PathModule.join(options.dataPackFolder, '.ajmeta'),
549+
ajmeta = new DataPackAJMeta(
550+
PathModule.join(options.dataPackFolder, 'data.ajmeta'),
555551
aj.export_namespace,
556552
Project!.last_used_export_namespace,
557553
options.dataPackFolder
@@ -560,9 +556,9 @@ export async function compileDataPack(options: {
560556

561557
PROGRESS_DESCRIPTION.set('Removing Old Data Pack Files...')
562558
PROGRESS.set(0)
563-
MAX_PROGRESS.set(ajmeta.oldDatapack.files.size)
559+
MAX_PROGRESS.set(ajmeta.oldFiles.size)
564560
const removedFolders = new Set<string>()
565-
for (const file of ajmeta.oldDatapack.files) {
561+
for (const file of ajmeta.oldFiles) {
566562
if (!isFunctionTagPath(file)) {
567563
if (fs.existsSync(file)) await fs.promises.unlink(file)
568564
} else if (aj.export_namespace !== Project!.last_used_export_namespace) {
@@ -604,7 +600,7 @@ export async function compileDataPack(options: {
604600
io.write = (localPath, content) => {
605601
const writePath = PathModule.join(options.dataPackFolder, localPath)
606602
exportedFiles.set(writePath, content)
607-
if (ajmeta) ajmeta.datapack.files.add(writePath)
603+
if (ajmeta) ajmeta.files.add(writePath)
608604
}
609605
return io
610606
}

src/systems/resourcepackCompiler.ts

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { MAX_PROGRESS, PROGRESS, PROGRESS_DESCRIPTION } from '../interface/expor
22
import { isResourcePackPath, toSafeFuntionName } from '../util/minecraftUtil'
33
import { TRANSPARENT_TEXTURE } from '../variants'
44
import { IRenderedNodes, IRenderedRig } from './rigRenderer'
5-
import { replacePathPart, sortObjectKeys, zip } from './util'
5+
import { sortObjectKeys, zip } from './util'
66

77
interface IPredicateItemModel {
88
parent: string
@@ -117,6 +117,53 @@ class PredicateItemModel {
117117
}
118118
}
119119

120+
class ResourcePackAJMeta {
121+
public files = new Set<string>()
122+
public oldFiles = new Set<string>()
123+
private oldContent: Record<string, { files?: string[] }> = {}
124+
125+
constructor(
126+
public path: string,
127+
public exportNamespace: string,
128+
public lastUsedExportNamespace: string,
129+
public resourcePackFolder: string
130+
) {}
131+
132+
read() {
133+
if (!fs.existsSync(this.path)) return
134+
this.oldContent = JSON.parse(fs.readFileSync(this.path, 'utf-8'))
135+
const data = this.oldContent[this.exportNamespace]
136+
const lastData = this.oldContent[this.lastUsedExportNamespace]
137+
if (lastData) {
138+
if (!Array.isArray(lastData.files)) lastData.files = []
139+
for (const file of lastData.files) {
140+
this.oldFiles.add(PathModule.join(this.resourcePackFolder, file))
141+
}
142+
delete this.oldContent[this.lastUsedExportNamespace]
143+
}
144+
if (data) {
145+
if (!Array.isArray(data.files)) data.files = []
146+
for (const file of data.files) {
147+
this.oldFiles.add(PathModule.join(this.resourcePackFolder, file))
148+
}
149+
delete this.oldContent[this.exportNamespace]
150+
}
151+
}
152+
153+
write() {
154+
const folder = PathModule.dirname(this.path)
155+
const content: ResourcePackAJMeta['oldContent'] = {
156+
...this.oldContent,
157+
[this.exportNamespace]: {
158+
files: Array.from(this.files).map(v =>
159+
PathModule.relative(folder, v).replace(/\\/g, '/')
160+
),
161+
},
162+
}
163+
fs.writeFileSync(this.path, autoStringify(sortObjectKeys(content)))
164+
}
165+
}
166+
120167
export async function compileResourcePack(options: {
121168
rig: IRenderedRig
122169
displayItemPath: string
@@ -131,6 +178,36 @@ export async function compileResourcePack(options: {
131178
PROGRESS_DESCRIPTION.set('Compiling Resource Pack...')
132179
console.log('Compiling resource pack...', options)
133180

181+
const ajmeta = new ResourcePackAJMeta(
182+
PathModule.join(options.resourcePackFolder, 'assets.ajmeta'),
183+
aj.export_namespace,
184+
lastUsedExportNamespace,
185+
options.resourcePackFolder
186+
)
187+
if (aj.resource_pack_export_mode === 'raw') {
188+
ajmeta.read()
189+
190+
PROGRESS_DESCRIPTION.set('Removing Old Resource Pack Files...')
191+
PROGRESS.set(0)
192+
MAX_PROGRESS.set(ajmeta.oldFiles.size)
193+
194+
const removedFolders = new Set<string>()
195+
for (const file of ajmeta.oldFiles) {
196+
if (fs.existsSync(file)) await fs.promises.unlink(file)
197+
let folder = PathModule.dirname(file)
198+
while (
199+
!removedFolders.has(folder) &&
200+
fs.existsSync(folder) &&
201+
(await fs.promises.readdir(folder)).length === 0
202+
) {
203+
await fs.promises.rm(folder, { recursive: true })
204+
removedFolders.add(folder)
205+
folder = PathModule.dirname(folder)
206+
}
207+
PROGRESS.set(PROGRESS.get() + 1)
208+
}
209+
}
210+
134211
const exportedFiles = new Map<string, string | Buffer>()
135212

136213
// Internal Models
@@ -228,27 +305,8 @@ export async function compileResourcePack(options: {
228305
// Do nothing
229306
console.log('Plugin mode enabled. Skipping resource pack export.')
230307
} else if (aj.resource_pack_export_mode === 'raw') {
231-
PROGRESS_DESCRIPTION.set('Removing Old Resource Pack Files...')
232-
233-
await fs.promises.rm(
234-
replacePathPart(modelExportFolder, aj.export_namespace, lastUsedExportNamespace),
235-
{
236-
recursive: true,
237-
force: true,
238-
}
239-
)
240-
await fs.promises.rm(
241-
replacePathPart(textureExportFolder, aj.export_namespace, lastUsedExportNamespace),
242-
{
243-
recursive: true,
244-
force: true,
245-
}
246-
)
247-
for (const variant of Object.keys(rig.variantModels)) {
248-
await fs.promises.mkdir(PathModule.join(modelExportFolder, variant), {
249-
recursive: true,
250-
})
251-
}
308+
ajmeta.files = new Set(exportedFiles.keys())
309+
ajmeta.write()
252310

253311
PROGRESS_DESCRIPTION.set('Writing Resource Pack...')
254312
PROGRESS.set(0)

0 commit comments

Comments
 (0)