Skip to content

Commit 6450185

Browse files
committed
Data Pack Exporter refactor
1 parent 43f26b1 commit 6450185

File tree

11 files changed

+1207
-1082
lines changed

11 files changed

+1207
-1082
lines changed

exporters/datapackExporter/datapackExporter.ts

Lines changed: 21 additions & 1079 deletions
Large diffs are not rendered by default.
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import { generateNamespaceFolder } from './datapackGen/namespaceFolderGen'
2+
import { generateAnimatedJavaFolder } from './datapackGen/animatedJavaFolderGen'
3+
import type { loadExporter } from '../datapackExporter'
4+
import { loadUtil } from './util'
5+
import { Globals as G } from './datapackGen/globals'
6+
7+
// I am not sure if this is the best way to do this, but it works 🤓👍
8+
type ExporterSettings = ReturnType<ReturnType<typeof loadExporter>['getSettings']>
9+
export type ExportData = AnimatedJava.IAnimatedJavaExportData<ExporterSettings>
10+
11+
export interface DataPackContext {
12+
scoreboards: {}
13+
}
14+
15+
export function loadDataPackGenerator() {
16+
const { formatStr, ProgressBarController, ExpectedError, LimitClock } = AnimatedJava.API
17+
const { NbtTag, NbtCompound, NbtString, NbtList, NbtInt } = AnimatedJava.API.deepslate
18+
const { fileExists, matrixToNbtFloatArray } = loadUtil()
19+
20+
return async (exportData: ExportData) => {
21+
if (!Project?.animated_java_variants) throw new Error('No variants found')
22+
console.log('Export Options', exportData)
23+
G.loadExportData(exportData)
24+
25+
console.log('Beginning export process...')
26+
27+
generateNamespaceFolder()
28+
generateAnimatedJavaFolder()
29+
30+
//--------------------------------------------
31+
// ANCHOR Export Data Pack
32+
//--------------------------------------------
33+
const tickFunctionTag = G.DATA_FOLDER.accessFile('minecraft/tags/functions/tick.json')
34+
const loadFunctionTag = G.DATA_FOLDER.accessFile('minecraft/tags/functions/load.json')
35+
36+
const progress = new ProgressBarController(
37+
'Writing Data Pack to disk...',
38+
G.DATAPACK.childCount
39+
)
40+
41+
interface IAJMeta {
42+
projects: Record<
43+
string,
44+
{
45+
tick_functions: string[]
46+
load_functions: string[]
47+
file_list: string[]
48+
}
49+
>
50+
}
51+
52+
let content: IAJMeta | undefined
53+
54+
const oldAJMetaPath = PathModule.join(G.DATAPACK_EXPORT_PATH, 'animated_java.mcmeta')
55+
const ajMetaPath = PathModule.join(G.DATAPACK_EXPORT_PATH, '.ajmeta')
56+
const existingMetaFile = (await fileExists(oldAJMetaPath))
57+
? oldAJMetaPath
58+
: (await fileExists(ajMetaPath))
59+
? ajMetaPath
60+
: undefined
61+
if (existingMetaFile !== undefined) {
62+
content = await fs.promises.readFile(existingMetaFile, 'utf-8').then(JSON.parse)
63+
64+
if (!content.projects) {
65+
const message = `Failed to read the animated_java.mcdata file. (Missing projects). Please delete the file and try again.`
66+
Blockbench.showMessageBox({
67+
title: 'Failed to read .ajmeta',
68+
message,
69+
})
70+
throw new ExpectedError(message)
71+
}
72+
73+
const project = content.projects[G.NAMESPACE] || {
74+
namespace: G.NAMESPACE,
75+
tick_functions: tickFunctionTag.content.values,
76+
load_functions: loadFunctionTag.content.values,
77+
file_list: [],
78+
}
79+
content.projects[G.NAMESPACE] = project
80+
81+
if (!project.file_list) {
82+
const message = `Failed to read the animated_java.mcdata file. (Missing project file_list). Please delete the file and try again.`
83+
Blockbench.showMessageBox({
84+
title: 'Failed to read .ajmeta',
85+
message,
86+
})
87+
throw new ExpectedError(message)
88+
}
89+
90+
if (!project.tick_functions) {
91+
const message = `Failed to read the animated_java.mcdata file. (Missing project tick_functions). Please delete the file and try again.`
92+
Blockbench.showMessageBox({
93+
title: 'Failed to read .ajmeta',
94+
message,
95+
})
96+
throw new ExpectedError(message)
97+
}
98+
99+
if (!project.load_functions) {
100+
const message = `Failed to read the animated_java.mcdata file. (Missing project load_functions). Please delete the file and try again.`
101+
Blockbench.showMessageBox({
102+
title: 'Failed to read .ajmeta',
103+
message,
104+
})
105+
throw new ExpectedError(message)
106+
}
107+
108+
progress.total += project.file_list.length
109+
const clock = new LimitClock(10)
110+
for (let path of project.file_list) {
111+
progress.add(1)
112+
await clock.sync().then(b => b && progress.update())
113+
if (path.endsWith('tick.json') || path.endsWith('load.json')) continue
114+
path = PathModule.join(G.DATAPACK_EXPORT_PATH, path)
115+
await fs.promises.unlink(path).catch(() => {})
116+
const dirPath = PathModule.dirname(path)
117+
const contents = await fs.promises.readdir(dirPath).catch(() => {})
118+
if (contents && contents.length === 0)
119+
await fs.promises.rmdir(dirPath).catch(() => {})
120+
}
121+
project.file_list = G.DATAPACK.getAllFilePaths()
122+
123+
tickFunctionTag.customJsonMerger = (a, b) => {
124+
a.values = a.values.filter(v => !project.tick_functions.includes(v))
125+
a.values.push(...b.values)
126+
return a
127+
}
128+
129+
loadFunctionTag.customJsonMerger = (a, b) => {
130+
a.values = a.values.filter(v => !project.load_functions.includes(v))
131+
a.values.push(...b.values)
132+
return a
133+
}
134+
135+
await fs.promises.rm(existingMetaFile)
136+
} else {
137+
content = {
138+
projects: {
139+
[G.NAMESPACE]: {
140+
tick_functions: tickFunctionTag.content.values,
141+
load_functions: loadFunctionTag.content.values,
142+
file_list: G.DATAPACK.getAllFilePaths(),
143+
},
144+
},
145+
}
146+
}
147+
G.DATAPACK.newFile('.ajmeta', content)
148+
await Promise.all(
149+
G.DATAPACK.children.map(
150+
async child => await child.writeToDisk(G.DATAPACK_EXPORT_PATH, progress)
151+
)
152+
)
153+
progress.finish()
154+
console.log('Export Complete!')
155+
}
156+
}

0 commit comments

Comments
 (0)