Skip to content

Commit

Permalink
✨ Support special resource pack files (#1676)
Browse files Browse the repository at this point in the history
  • Loading branch information
misode authored Dec 14, 2024
1 parent 23b1798 commit 1bfd90e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/symbol/Symbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,16 +215,21 @@ export const AssetsFileCategories = Object.freeze(
'font/ttf',
'font/otf',
'font/unihex',
'gpu_warnlist',
'item_definition', // items
'lang',
'lang/deprecated',
'model',
'particle',
'post_effect',
'regional_compliancies',
'shader',
'shader/fragment',
'shader/vertex',
'sound',
'sounds', // sounds.json
'texture',
'texture_meta', // *.png.mcmeta
] as const,
)
export type AssetsFileCategory = (typeof AssetsFileCategories)[number]
Expand Down
43 changes: 36 additions & 7 deletions packages/java-edition/src/binder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface Resource {
category: FileCategory
ext: `.${string}`
pack: 'data' | 'assets'
identifier?: string
since?: ReleaseVersion
until?: ReleaseVersion
}
Expand Down Expand Up @@ -129,13 +130,13 @@ for (const registry of TaggableResourceLocationCategories) {
// Resource pack
resource('atlases', { pack: 'assets', category: 'atlas', since: '1.19.3' })
resource('blockstates', { pack: 'assets', category: 'block_definition' })
resource('equipment', { pack: 'assets', category: 'equipment', since: '1.21.4' })
resource('font', { pack: 'assets', category: 'font', since: '1.16' })
resource('equipment', { pack: 'assets', since: '1.21.4' })
resource('font', { pack: 'assets', since: '1.16' })
resource('font', { pack: 'assets', category: 'font/ttf', since: '1.16', ext: '.ttf' })
resource('font', { pack: 'assets', category: 'font/otf', since: '1.16', ext: '.otf' })
resource('font', { pack: 'assets', category: 'font/unihex', since: '1.20', ext: '.zip' })
resource('items', { pack: 'assets', category: 'item_definition', since: '1.21.4' })
resource('lang', { pack: 'assets', category: 'lang' })
resource('lang', { pack: 'assets' })
resource('models', { pack: 'assets', category: 'model' })
resource('models/equipment', {
pack: 'assets',
Expand All @@ -151,6 +152,16 @@ resource('shaders', { pack: 'assets', category: 'shader/fragment', ext: '.fsh' }
resource('shaders', { pack: 'assets', category: 'shader/vertex', ext: '.vsh' })
resource('sounds', { pack: 'assets', category: 'sound', ext: '.ogg' })
resource('textures', { pack: 'assets', category: 'texture', ext: '.png' })
resource('textures', { pack: 'assets', category: 'texture_meta', ext: '.png.mcmeta' })

resource('lang', { pack: 'assets', category: 'lang/deprecated', identifier: 'deprecated' })
resource('', { pack: 'assets', category: 'sounds', identifier: 'sounds' })
resource('', {
pack: 'assets',
category: 'regional_compliancies',
identifier: 'regional_compliancies',
})
resource('', { pack: 'assets', category: 'gpu_warnlist', identifier: 'gpu_warnlist' })

export function* getRels(
uri: string,
Expand All @@ -177,14 +188,31 @@ export function dissectUri(uri: string, ctx: UriBinderContext) {

for (const rel of rels) {
const parts = rel.split('/')
if (parts.length < 4) {
if (parts.length < 3) {
continue
}
const [pack, namespace, ...rest] = parts
if (pack !== 'data' && pack !== 'assets') {
continue
}
const candidateResources: [Resource, string][] = []
if (rest.length === 1) {
const resources = Resources.get('')
for (const res of resources ?? []) {
if (res.pack !== pack) {
continue
}
let identifier = rest[0]
if (!identifier.endsWith(res.ext)) {
continue
}
identifier = identifier.slice(0, -res.ext.length)
if (res.identifier && identifier !== res.identifier) {
continue
}
candidateResources.push([res, identifier])
}
}
for (let i = 1; i < rest.length; i += 1) {
const resources = Resources.get(rest.slice(0, i).join('/'))
for (const res of resources ?? []) {
Expand All @@ -196,6 +224,9 @@ export function dissectUri(uri: string, ctx: UriBinderContext) {
continue
}
identifier = identifier.slice(0, -res.ext.length)
if (res.identifier && identifier !== res.identifier) {
continue
}
candidateResources.push([res, identifier])
}
}
Expand Down Expand Up @@ -240,9 +271,7 @@ export const uriBinder: UriBinder = (uris: readonly string[], ctx: UriBinderCont

export function registerCustomResources(config: Config) {
for (const [path, res] of Object.entries(config.env.customResources)) {
if (res.pack === undefined || res.pack === 'data') {
resource(path, { ...res, category: res.category as FileCategory })
}
resource(path, { ...res, category: res.category as FileCategory })
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/java-edition/src/json/checker/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const file: core.Checker<json.JsonFileNode> = (node, ctx) => {
}
const parts = dissectUri(ctx.doc.uri, ctx)
if (parts?.ok) {
if (parts?.category.startsWith('tag/')) {
if (parts.category.startsWith('tag/')) {
const type = createTagDefinition(parts.category.slice(4))
return json.checker.index(type)(child, ctx)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/vscode-extension/src/extension.mts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ export async function activate(context: vsc.ExtensionContext) {
{ language: 'mcdoc' },
{ language: 'snbt' },
{ language: 'mcmeta' },
{ language: 'json', pattern: '**/data/*/*/**/*.json' },
{ language: 'json', pattern: '**/assets/*/*/**/*.json' },
{ language: 'json', pattern: '**/data/*/**/*.json' },
{ language: 'json', pattern: '**/assets/*/**/*.json' },
]

const gameVersion = vsc.workspace.getConfiguration('spyglassmc.env').get('gameVersion')
Expand Down

0 comments on commit 1bfd90e

Please sign in to comment.