-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathglobal.ts
More file actions
183 lines (161 loc) · 4.52 KB
/
global.ts
File metadata and controls
183 lines (161 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { normalizePath } from '../util/fileUtil'
import { IntentionalExportError, IntentionalExportErrorFromInvalidFile } from './errors'
import { sortObjectKeys } from './util'
export enum SUPPORTED_MINECRAFT_VERSIONS {
'1.20.4' = '1.20.4',
'1.20.5' = '1.20.5',
'1.21.2' = '1.21.2',
'1.21.4' = '1.21.4',
'1.21.5' = '1.21.5',
'1.21.6' = '1.21.6',
'1.21.9' = '1.21.9',
}
type OldSerializedAJMeta = Record<
string,
{
files?: string[]
}
>
interface SerializedAJMeta {
formatVersion?: string
rigs?: Record<
string,
{
coreFiles?: string[]
versionedFiles?: string[]
}
>
}
export class AJMeta {
coreFiles = new Set<string>()
previousCoreFiles = new Set<string>()
versionedFiles = new Set<string>()
previousVersionedFiles = new Set<string>()
private previousAJMeta: SerializedAJMeta = {}
constructor(
public path: string,
public exportNamespace: string,
public lastUsedExportNamespace: string,
public rootFolder: string
) {}
read() {
if (!fs.existsSync(this.path)) return
try {
this.previousAJMeta = JSON.parse(fs.readFileSync(this.path, 'utf-8'))
} catch (e) {
throw new IntentionalExportError(`Failed to read existing AJMeta file: ${e}`)
}
if (this.previousAJMeta.formatVersion !== '1.0.0') {
// TODO - Use our new standardized solution to handle file versioning.
// Assume the file is outdated, and update it.
const outdated = this.previousAJMeta as OldSerializedAJMeta
this.previousAJMeta = {
formatVersion: '1.0.0',
rigs: {},
}
for (const [key, value] of Object.entries(outdated)) {
this.previousAJMeta.rigs![key] = {
versionedFiles: value.files,
}
}
}
this.previousAJMeta.rigs ??= {}
const lastNamespaceData = this.previousAJMeta.rigs[this.lastUsedExportNamespace]
if (lastNamespaceData) {
if (!Array.isArray(lastNamespaceData.versionedFiles))
lastNamespaceData.versionedFiles = []
for (const file of lastNamespaceData.versionedFiles) {
this.previousVersionedFiles.add(PathModule.join(this.rootFolder, file))
}
delete this.previousAJMeta.rigs[this.lastUsedExportNamespace]
}
const namespaceData = this.previousAJMeta.rigs[this.exportNamespace]
if (namespaceData) {
if (!Array.isArray(namespaceData.versionedFiles)) namespaceData.versionedFiles = []
for (const file of namespaceData.versionedFiles) {
this.previousVersionedFiles.add(PathModule.join(this.rootFolder, file))
}
delete this.previousAJMeta.rigs[this.exportNamespace]
}
}
write() {
const resourcePackFolder = PathModule.dirname(this.path)
const content: SerializedAJMeta = {
formatVersion: '1.0.0',
rigs: sortObjectKeys({
...this.previousAJMeta.rigs,
[this.exportNamespace]: {
coreFiles: Array.from(this.coreFiles)
.map(v => normalizePath(PathModule.relative(resourcePackFolder, v)))
.sort(),
versionedFiles: Array.from(this.versionedFiles)
.map(v => normalizePath(PathModule.relative(resourcePackFolder, v)))
.sort(),
},
}),
}
fs.writeFileSync(this.path, autoStringify(content))
}
}
export type PackMetaFormats = number | number[] | { min_inclusive: number; max_inclusive: number }
interface OverlayEntry {
directory?: string
/** Minecraft enforces this field does not to exist if the pack doesn't support versions older than 1.21.9 */
formats?: PackMetaFormats
// Below since 1.21.9
min_format?: number
max_format?: number
}
export interface SerializedPackMeta {
pack?: {
pack_format?: number
/** Minecraft enforces this field does not to exist if the pack doesn't support versions older than 1.21.9 */
supported_formats?: PackMetaFormats
description?: string
// Below since 1.21.9
min_format?: number
max_format?: number
}
features?: {
enabled?: string[]
}
filter?: {
block?: Array<{
namespace: string
path: string
}>
}
overlays?: {
entries?: OverlayEntry[]
}
language?: {
name?: string
region?: string
bidirectional?: boolean
}
}
/**
* A class that reads and writes pack.mcmeta files.
*
* Designed to only modify parts of the file we care about, and leave the rest as-is.
*/
export class PackMeta {
content: SerializedPackMeta = {}
static fromFile(path: string) {
const meta = new PackMeta()
if (!fs.existsSync(path)) {
console.warn(`Pack meta file does not exist at ${path}`)
return meta
}
const raw = fs.readFileSync(path, 'utf-8')
try {
meta.content = JSON.parse(raw)
} catch (e: any) {
throw new IntentionalExportErrorFromInvalidFile(path, e)
}
return meta
}
toJSON(): SerializedPackMeta {
return structuredClone(this.content)
}
}