Skip to content

Commit 384304e

Browse files
committed
Fix .ajmeta conflicts when exporting both Resource Pack and Data Pack to the same folder
1 parent e02cd2f commit 384304e

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

exporters/datapackExporter/exporter/datapackGen.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ export function loadDataPackGenerator() {
3434
)
3535

3636
interface IAJMeta {
37-
projects: Record<string, { file_list: string[] }>
37+
datapack: {
38+
projects: Record<string, { file_list: string[] }>
39+
}
40+
resourcepack: object
3841
}
3942

4043
let content: IAJMeta | undefined
@@ -49,7 +52,19 @@ export function loadDataPackGenerator() {
4952
if (existingMetaFile !== undefined) {
5053
content = await fs.promises.readFile(existingMetaFile, 'utf-8').then(JSON.parse)
5154

52-
if (!content.projects) {
55+
// Upgrade from old format
56+
// @ts-ignore
57+
if (!content.datapack && content.projects) {
58+
// @ts-ignore
59+
content.datapack = {}
60+
content.resourcepack = {}
61+
// @ts-ignore
62+
content.datapack.projects = content.projects
63+
// @ts-ignore
64+
delete content.projects
65+
}
66+
67+
if (!content.datapack.projects) {
5368
const message = `Failed to read the .ajmeta file. (Missing projects). Please delete the file and try again.`
5469
Blockbench.showMessageBox({
5570
title: 'Failed to read .ajmeta',
@@ -58,13 +73,13 @@ export function loadDataPackGenerator() {
5873
throw new ExpectedError(message)
5974
}
6075

61-
const project = content.projects[G.NAMESPACE] || {
76+
const project = content.datapack.projects[G.NAMESPACE] || {
6277
namespace: G.NAMESPACE,
6378
tick_functions: tickFunctionTag.content.values,
6479
load_functions: loadFunctionTag.content.values,
6580
file_list: [],
6681
}
67-
content.projects[G.NAMESPACE] = project
82+
content.datapack.projects[G.NAMESPACE] = project
6883

6984
if (!project.file_list) {
7085
const message = `Failed to read the .ajmeta file. (Missing project file_list). Please delete the file and try again.`
@@ -97,9 +112,12 @@ export function loadDataPackGenerator() {
97112
await fs.promises.rm(existingMetaFile)
98113
} else {
99114
content = {
100-
projects: {
101-
[G.NAMESPACE]: { file_list: G.DATAPACK.getAllFilePaths() },
115+
datapack: {
116+
projects: {
117+
[G.NAMESPACE]: { file_list: G.DATAPACK.getAllFilePaths() },
118+
},
102119
},
120+
resourcepack: {},
103121
}
104122
}
105123
G.DATAPACK.newFile('.ajmeta', content)

src/resourcePackExporter.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,10 @@ export async function exportResources(
222222
)
223223

224224
interface IAJMeta {
225-
projects: Record<string, { file_list: string[] }>
225+
datapack: object
226+
resourcepack: {
227+
projects: Record<string, { file_list: string[] }>
228+
}
226229
}
227230

228231
async function processAJMeta(filePaths: string[]) {
@@ -248,24 +251,36 @@ export async function exportResources(
248251
if (!content)
249252
throw new Error('Failed to read .ajmeta file as JSON. Content is undefined.')
250253

251-
if (!content.projects) {
254+
// Upgrade from old format
255+
// @ts-ignore
256+
if (!content.resourcepack && content.projects) {
257+
// @ts-ignore
258+
content.resourcepack = {}
259+
content.datapack = {}
260+
// @ts-ignore
261+
content.resourcepack.projects = content.projects
262+
// @ts-ignore
263+
delete content.projects
264+
}
265+
266+
if (!content.resourcepack.projects) {
252267
console.warn('Found existing .ajmeta file, but it is missing "projects" key.')
253-
content.projects = {}
268+
content.resourcepack.projects = {}
254269
}
255270

256-
if (!content.projects[NAMESPACE]) {
271+
if (!content.resourcepack.projects[NAMESPACE]) {
257272
console.warn('Found existing .ajmeta file, but it is missing this project.')
258-
content.projects[NAMESPACE] = {
273+
content.resourcepack.projects[NAMESPACE] = {
259274
file_list: [],
260275
}
261276
} else {
262277
const progress = new ProgressBarController(
263278
'Cleaning up old Resource Pack files...',
264-
content.projects[NAMESPACE].file_list.length
279+
content.resourcepack.projects[NAMESPACE].file_list.length
265280
)
266281
// Clean out old files from disk
267282
const clock = new LimitClock(10)
268-
for (let path of content.projects[NAMESPACE].file_list) {
283+
for (let path of content.resourcepack.projects[NAMESPACE].file_list) {
269284
await clock.sync().then(b => b && progress.update())
270285
path = PathModule.join(resourcePackPath, path)
271286
await fs.promises.unlink(path).catch(() => undefined)
@@ -278,15 +293,18 @@ export async function exportResources(
278293
progress.finish()
279294
}
280295

281-
content.projects[NAMESPACE].file_list = filePaths
296+
content.resourcepack.projects[NAMESPACE].file_list = filePaths
282297
}
283298

284299
if (!content) {
285300
console.warn('.ajmeta does not exist. Creating new .ajmeta file.')
286301
content = {
287-
projects: {
288-
[NAMESPACE]: {
289-
file_list: filePaths,
302+
datapack: {},
303+
resourcepack: {
304+
projects: {
305+
[NAMESPACE]: {
306+
file_list: filePaths,
307+
},
290308
},
291309
},
292310
}

0 commit comments

Comments
 (0)