|
| 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 function loadDataPackGenerator() { |
| 12 | + const { ProgressBarController, ExpectedError, LimitClock } = AnimatedJava.API |
| 13 | + const { fileExists } = loadUtil() |
| 14 | + |
| 15 | + return async (exportData: ExportData) => { |
| 16 | + if (!Project?.animated_java_variants) throw new Error('No variants found') |
| 17 | + console.log('Export Options', exportData) |
| 18 | + G.loadExportData(exportData) |
| 19 | + |
| 20 | + console.log('Beginning export process...') |
| 21 | + |
| 22 | + generateNamespaceFolder() |
| 23 | + generateAnimatedJavaFolder() |
| 24 | + |
| 25 | + //-------------------------------------------- |
| 26 | + // ANCHOR Export Data Pack |
| 27 | + //-------------------------------------------- |
| 28 | + const tickFunctionTag = G.DATA_FOLDER.accessFile('minecraft/tags/functions/tick.json') |
| 29 | + const loadFunctionTag = G.DATA_FOLDER.accessFile('minecraft/tags/functions/load.json') |
| 30 | + |
| 31 | + const progress = new ProgressBarController( |
| 32 | + 'Writing Data Pack to disk...', |
| 33 | + G.DATAPACK.childCount |
| 34 | + ) |
| 35 | + |
| 36 | + interface IAJMeta { |
| 37 | + projects: Record<string, { file_list: string[] }> |
| 38 | + } |
| 39 | + |
| 40 | + let content: IAJMeta | undefined |
| 41 | + |
| 42 | + const oldAJMetaPath = PathModule.join(G.DATAPACK_EXPORT_PATH, 'animated_java.mcmeta') |
| 43 | + const ajMetaPath = PathModule.join(G.DATAPACK_EXPORT_PATH, '.ajmeta') |
| 44 | + const existingMetaFile = (await fileExists(oldAJMetaPath)) |
| 45 | + ? oldAJMetaPath |
| 46 | + : (await fileExists(ajMetaPath)) |
| 47 | + ? ajMetaPath |
| 48 | + : undefined |
| 49 | + if (existingMetaFile !== undefined) { |
| 50 | + content = await fs.promises.readFile(existingMetaFile, 'utf-8').then(JSON.parse) |
| 51 | + |
| 52 | + if (!content.projects) { |
| 53 | + const message = `Failed to read the .ajmeta file. (Missing projects). Please delete the file and try again.` |
| 54 | + Blockbench.showMessageBox({ |
| 55 | + title: 'Failed to read .ajmeta', |
| 56 | + message, |
| 57 | + }) |
| 58 | + throw new ExpectedError(message) |
| 59 | + } |
| 60 | + |
| 61 | + const project = content.projects[G.NAMESPACE] || { |
| 62 | + namespace: G.NAMESPACE, |
| 63 | + tick_functions: tickFunctionTag.content.values, |
| 64 | + load_functions: loadFunctionTag.content.values, |
| 65 | + file_list: [], |
| 66 | + } |
| 67 | + content.projects[G.NAMESPACE] = project |
| 68 | + |
| 69 | + if (!project.file_list) { |
| 70 | + const message = `Failed to read the .ajmeta file. (Missing project file_list). Please delete the file and try again.` |
| 71 | + Blockbench.showMessageBox({ |
| 72 | + title: 'Failed to read .ajmeta', |
| 73 | + message, |
| 74 | + }) |
| 75 | + throw new ExpectedError(message) |
| 76 | + } |
| 77 | + |
| 78 | + progress.total += project.file_list.length |
| 79 | + const clock = new LimitClock(10) |
| 80 | + for (let path of project.file_list) { |
| 81 | + progress.add(1) |
| 82 | + await clock.sync().then(b => b && progress.update()) |
| 83 | + if ( |
| 84 | + PathModule.basename(path) === 'tick.json' || |
| 85 | + PathModule.basename(path) === 'load.json' |
| 86 | + ) |
| 87 | + continue |
| 88 | + path = PathModule.join(G.DATAPACK_EXPORT_PATH, path) |
| 89 | + await fs.promises.unlink(path).catch(() => {}) |
| 90 | + const dirPath = PathModule.dirname(path) |
| 91 | + const contents = await fs.promises.readdir(dirPath).catch(() => {}) |
| 92 | + if (contents && contents.length === 0) |
| 93 | + await fs.promises.rmdir(dirPath).catch(() => {}) |
| 94 | + } |
| 95 | + project.file_list = G.DATAPACK.getAllFilePaths() |
| 96 | + |
| 97 | + await fs.promises.rm(existingMetaFile) |
| 98 | + } else { |
| 99 | + content = { |
| 100 | + projects: { |
| 101 | + [G.NAMESPACE]: { file_list: G.DATAPACK.getAllFilePaths() }, |
| 102 | + }, |
| 103 | + } |
| 104 | + } |
| 105 | + G.DATAPACK.newFile('.ajmeta', content) |
| 106 | + await Promise.all( |
| 107 | + G.DATAPACK.children.map( |
| 108 | + async child => await child.writeToDisk(G.DATAPACK_EXPORT_PATH, progress) |
| 109 | + ) |
| 110 | + ) |
| 111 | + progress.finish() |
| 112 | + console.log('Export Complete!') |
| 113 | + } |
| 114 | +} |
0 commit comments