Skip to content

Commit c63eeaa

Browse files
committed
Fix .ajmeta problems
1 parent 4e9f524 commit c63eeaa

File tree

6 files changed

+160
-150
lines changed

6 files changed

+160
-150
lines changed

exporters/datapackExporter/exporter/gen/datapack.ts

Lines changed: 13 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,23 @@ export interface IFolders {
2323
}
2424
}
2525

26-
export interface IAJMeta {
27-
datapack: {
28-
projects: Record<
29-
string, // UUID
30-
{
31-
project_name: string
32-
files: string[]
33-
}
34-
>
35-
}
36-
resourcepack: object
37-
}
26+
async function processAJMeta(folders: IFolders) {
27+
const { LimitClock } = AnimatedJava.API
28+
const ajmeta = new AnimatedJava.API.AJMetaFile()
3829

39-
async function loadAJMeta() {
40-
let ajMeta: IAJMeta
4130
const oldPath = PathModule.join(G.DATAPACK_EXPORT_PATH, '.ajmeta')
42-
const newPath = PathModule.join(G.DATAPACK_EXPORT_PATH, '.ajmeta')
43-
const path = (await fileExists(oldPath)) ? oldPath : newPath
31+
const newPath = PathModule.join(G.DATAPACK_EXPORT_PATH, 'datapack.ajmeta')
4432

45-
if (await fileExists(path)) {
46-
ajMeta = await loadJsonFile(path).catch(e => {
47-
console.warn(`Failed to load .ajmeta file:\n${e.stack}`)
48-
})
33+
if (await fileExists(newPath)) await ajmeta.load(newPath)
34+
else if (await fileExists(oldPath)) {
35+
await ajmeta.load(oldPath)
36+
await fs.promises.unlink(oldPath).catch(() => {})
4937
}
50-
if (!ajMeta) ajMeta = {} as IAJMeta
51-
52-
ajMeta.datapack ??= { projects: {} }
53-
ajMeta.datapack.projects ??= {}
54-
55-
if (path === oldPath) await fs.promises.rm(path).catch(() => {})
56-
57-
return ajMeta
58-
}
59-
60-
async function processAJMeta(folders: IFolders) {
61-
const { LimitClock } = AnimatedJava.API
62-
const ajMetaContent = await loadAJMeta()
6338

64-
let project =
65-
ajMetaContent.datapack.projects[Project!.animated_java_uuid!] ??
66-
({} as IAJMeta['datapack']['projects'][string])
67-
ajMetaContent.datapack.projects[Project!.animated_java_uuid!] = project
68-
project.project_name = G.PROJECT_NAME
69-
project.files ??= []
39+
let project = ajmeta.getProject(Project!.animated_java_uuid!)
40+
if (!project) project = ajmeta.addProject(Project!.animated_java_uuid!, G.PROJECT_NAME, [])
7041

71-
const oldFiles = project.files
42+
const oldFiles = project.file_list
7243
const newFiles = folders.datapack.getAllFilePaths()
7344
const filesToRemove = oldFiles.filter(f => !newFiles.includes(f))
7445

@@ -89,9 +60,9 @@ async function processAJMeta(folders: IFolders) {
8960
}
9061
progress.finish()
9162

92-
project.files = newFiles
63+
project.file_list = newFiles
9364

94-
folders.datapack.newFile('.ajmeta', ajMetaContent)
65+
folders.datapack.newFile('datapack.ajmeta', ajmeta.toJSON())
9566
}
9667

9768
export async function generateDatapack(exportData: ExportData) {

src/ajmeta.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import * as fs from 'fs/promises'
2+
import * as PACKAGE from '../package.json'
3+
4+
export interface IAJMetaJSON {
5+
version?: string
6+
projects: Record<string, IAJMetaProject>
7+
}
8+
9+
export interface IAJMetaProject {
10+
project_name: string
11+
file_list: string[]
12+
}
13+
14+
export class AJMetaFile {
15+
version: string = PACKAGE.version
16+
projects: Record<string, IAJMetaProject> = {}
17+
18+
constructor() {
19+
this.projects = {}
20+
}
21+
22+
addProject(uuid: string, projectName: string, fileList: string[]): IAJMetaProject {
23+
const project = { project_name: projectName, file_list: fileList }
24+
this.projects[uuid] = project
25+
return project
26+
}
27+
28+
getProject(uuid: string): IAJMetaProject | undefined {
29+
return this.projects[uuid]
30+
}
31+
32+
toJSON(): IAJMetaJSON {
33+
return {
34+
version: PACKAGE.version,
35+
projects: this.projects,
36+
}
37+
}
38+
39+
async load(path: string): Promise<AJMetaFile> {
40+
this.projects = {}
41+
42+
const stringContent = await fs.readFile(path, 'utf-8').catch(e => {
43+
console.warn(`Could not read ajmeta file at ${path}: ${e as string}`)
44+
return '{}'
45+
})
46+
47+
let content: IAJMetaJSON
48+
try {
49+
content = JSON.parse(stringContent)
50+
} catch (e: any) {
51+
throw new Error(`Invalid ajmeta file: ${e as string}`)
52+
}
53+
54+
this.version = content.version || PACKAGE.version
55+
this.projects = content.projects || {}
56+
57+
return this
58+
}
59+
}

src/index.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import * as VirtualFileSystem from './util/virtualFileSystem'
4949
import { openUnexpectedErrorDialog } from './ui/popups/unexpectedError'
5050
import * as minecraft from './minecraft'
5151
import { openAJExportInProgressDialog } from './ui/ajExportInProgress'
52+
import { AJMetaFile } from './ajmeta'
5253

5354
Prism.languages.mcfunction = {}
5455

@@ -76,23 +77,24 @@ globalThis.AnimatedJava = {
7677
openAJExportInProgressDialog,
7778

7879
API: {
79-
Settings: AJSettings,
80-
Exporter: AnimatedJavaExporter,
81-
translate,
8280
addTranslations,
81+
AJMetaFile,
82+
columnToRowMajor: transposeMatrix,
83+
createInfo,
84+
deepslate,
85+
ExpectedError,
86+
Exporter: AnimatedJavaExporter,
8387
formatStr,
88+
generateSearchTree,
89+
JsonText,
90+
LimitClock,
91+
minecraft,
92+
ProgressBarController,
8493
roundTo,
8594
roundToN,
95+
Settings: AJSettings,
96+
translate,
8697
VirtualFileSystem,
87-
deepslate,
88-
ProgressBarController,
89-
createInfo,
90-
JsonText,
91-
columnToRowMajor: transposeMatrix,
92-
generateSearchTree,
93-
minecraft,
94-
ExpectedError,
95-
LimitClock,
9698
},
9799
}
98100
// Uninstall events

src/modelDataFixerUpper.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,38 @@ export function process(model: any) {
1313
console.log('Upgrading model from version', model.meta.format_version, 'to', FORMAT_VERSION)
1414

1515
try {
16-
if (compareVersions('1.0', model.meta.format_version)) updateModelTo1_0(model)
17-
if (compareVersions('1.1', model.meta.format_version)) updateModelTo1_1(model)
18-
if (compareVersions('1.2', model.meta.format_version)) updateModelTo1_2(model)
19-
if (compareVersions('1.3', model.meta.format_version)) updateModelTo1_3(model)
20-
if (compareVersions('1.4', model.meta.format_version)) updateModelTo1_4(model)
16+
console.group('Upgrade process')
17+
if (model.meta.format_version.length === 3) {
18+
if (compareVersions('1.0', model.meta.format_version)) updateModelTo1_0(model)
19+
if (compareVersions('1.1', model.meta.format_version)) updateModelTo1_1(model)
20+
if (compareVersions('1.2', model.meta.format_version)) updateModelTo1_2(model)
21+
if (compareVersions('1.3', model.meta.format_version)) updateModelTo1_3(model)
22+
if (compareVersions('1.4', model.meta.format_version)) updateModelTo1_4(model)
23+
model.meta.format_version = '0.3.9'
24+
}
25+
// Versions below this are post 0.3.10. I changed the versioning system to use the AJ version instead of a unique format version.
26+
if (compareVersions('0.3.10', model.meta.format_version)) updateModelTo0_3_10(model)
27+
console.groupEnd()
2128
} catch (e) {
2229
console.error(e)
2330
openUnexpectedErrorDialog(e)
2431
void Project?.close(true)
2532
return
2633
}
2734

28-
model.meta.format_version ??= FORMAT_VERSION
35+
model.meta.format_version = FORMAT_VERSION
2936

3037
console.log('Upgrade complete')
3138
}
3239

40+
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unused-vars
41+
function updateModelTo0_3_10(model: any) {
42+
console.log('Processing model for AJ 0.3.10')
43+
}
44+
3345
// eslint-disable-next-line @typescript-eslint/naming-convention
3446
function updateModelTo1_4(model: any) {
47+
console.log('Processing model format 1.4')
3548
if (
3649
model.animated_java.exporter_settings['animated_java:datapack_exporter']
3750
.outdated_rig_warning !== undefined
@@ -49,6 +62,7 @@ function updateModelTo1_4(model: any) {
4962

5063
// eslint-disable-next-line @typescript-eslint/naming-convention
5164
function updateModelTo1_3(model: any) {
65+
console.log('Processing model format 1.3')
5266
if (model.animated_java.settings.exporter === 'animated_java:animation_exporter') {
5367
model.animated_java.settings.exporter = 'animated_java:datapack_exporter'
5468
}
@@ -61,6 +75,7 @@ function updateModelTo1_3(model: any) {
6175

6276
// eslint-disable-next-line @typescript-eslint/naming-convention
6377
function updateModelTo1_2(model: any) {
78+
console.log('Processing model format 1.2')
6479
for (const variant of model.animated_java.variants) {
6580
for (const [from, to] of Object.entries(variant.textureMap as Record<string, string>)) {
6681
const fromUUID = from.split('::')[0]
@@ -73,6 +88,7 @@ function updateModelTo1_2(model: any) {
7388

7489
// eslint-disable-next-line @typescript-eslint/naming-convention
7590
function updateModelTo1_1(model: any) {
91+
console.log('Processing model format 1.1')
7692
model.animated_java.settings.resource_pack_mcmeta =
7793
model.animated_java.settings.resource_pack_folder
7894
delete model.animated_java.settings.resource_pack_folder
@@ -84,6 +100,7 @@ function updateModelTo1_1(model: any) {
84100

85101
// eslint-disable-next-line @typescript-eslint/naming-convention
86102
function updateModelTo1_0(model: any) {
103+
console.log('Processing model format 1.0')
87104
if (model.meta.settings) {
88105
console.log('Upgrading settings...')
89106
const animatedJava: IAnimatedJavaModel['animated_java'] = {

src/modelFormat.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { injectStartScreen } from './ui/ajStartScreen'
88
import { consoleGroup, consoleGroupCollapsed } from './util/console'
99
import { createBlockbenchMod } from './util/moddingTools'
1010
import { IBoneConfig, TextureMap, Variant, VariantsContainer } from './variants'
11+
import * as PACKAGE from '../package.json'
1112

12-
export const FORMAT_VERSION = '1.4'
13+
export const FORMAT_VERSION = PACKAGE.version
1314

1415
function addProjectToRecentProjects(file: FileResult) {
1516
if (!Project || !file.path) return

0 commit comments

Comments
 (0)