Skip to content

Commit

Permalink
JSYSTEM: Add JUTResType enum, attempt to add a bit of type safety to …
Browse files Browse the repository at this point in the history
…ResourceResolver

The conditionally typed callback isn't working well. See discord discussion: https://discord.com/channels/551621619152584706/551624375875665930/1322685048566190150
  • Loading branch information
themikelester committed Dec 28, 2024
1 parent e2df94a commit 404908c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
30 changes: 20 additions & 10 deletions src/Common/JSYSTEM/J2Dv1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import ArrayBufferSlice from "../../ArrayBufferSlice.js";
import { JSystemFileReaderHelper } from "./J3D/J3DLoader.js";
import { align, assert, assertExists, readString } from "../../util.js";
import { align, assert, readString } from "../../util.js";
import { Color, colorNewFromRGBA8 } from "../../Color.js";
import { GfxRenderInst, GfxRenderInstManager } from "../../gfx/render/GfxRenderInstManager.js";
import * as GX_Material from '../../gx/gx_material.js';
Expand All @@ -24,18 +24,28 @@ const drawParams = new DrawParams();

const scratchMat = mat4.create();

export const enum JUTResType {
'TIMG', 'TLUT', 'FONT'
}

export type JUTResAssetType<T extends JUTResType> =
T extends JUTResType.TIMG ? BTIData :
T extends JUTResType.TLUT ? null :
T extends JUTResType.FONT ? null :
unknown;

// TODO: Find a better home for this
export type ResourceResolver = (resType: string, resName: string) => any | null;
export type ResourceResolver<T extends JUTResType> = (resType: T, resName: string) => JUTResAssetType<T> | null;

interface ResRef {
refType: number;
resType: string;
resType: JUTResType;
resName: string;
arcName: string | null;
_nextOffset: number;
}

function parseResourceReference(buffer: ArrayBufferSlice, offset: number, resType: string, arcName: string | null): ResRef {
function parseResourceReference(buffer: ArrayBufferSlice, offset: number, resType: JUTResType, arcName: string | null): ResRef {
const dataView = buffer.createDataView();
const refType = dataView.getUint8(offset + 0);
const nameLen = dataView.getUint8(offset + 1);
Expand Down Expand Up @@ -112,8 +122,8 @@ function readPIC1Chunk(buffer: ArrayBufferSlice, parent: PAN1 | null): PIC1 {
const dataCount = view.getUint8(pane.offset + 0);
let offset = pane.offset + 1;

const timg = parseResourceReference(buffer, offset, 'TIMG', null);
const tlut = parseResourceReference(buffer, timg._nextOffset, 'TLUT', null);
const timg = parseResourceReference(buffer, offset, JUTResType.TIMG, null);
const tlut = parseResourceReference(buffer, timg._nextOffset, JUTResType.TLUT, null);
const binding = view.getUint8(tlut._nextOffset);
offset = tlut._nextOffset + 1;

Expand Down Expand Up @@ -335,7 +345,7 @@ export class J2DPane {
}
}

protected resolveReferences(resolver: ResourceResolver) {
protected resolveReferences(resolver: ResourceResolver<JUTResType>) {
for(let child of this.children) {
child.resolveReferences(resolver);
}
Expand Down Expand Up @@ -472,10 +482,10 @@ export class J2DPicture extends J2DPane {
this.materialHelper = new GXMaterialHelperGfx(mb.finish());
}

protected override resolveReferences(resolver: ResourceResolver) {
protected override resolveReferences(resolver: ResourceResolver<JUTResType.TIMG>) {
if(this.data.timg.refType > 1) {
if (this.data.timg.refType !== 2) { console.warn('Untested J2D feature'); }
this.tex = resolver(this.data.timg.resType, this.data.timg.resName);
this.tex = resolver(JUTResType.TIMG, this.data.timg.resName);
if (this.tex) { this.prepare(); }
}
assert(this.data.tlut.refType === 0, 'TLUT references currently unsupported');
Expand All @@ -487,7 +497,7 @@ export class J2DPicture extends J2DPane {
export class J2DScreen extends J2DPane {
public color: Color

constructor(data: SCRN, cache: GfxRenderCache, resolver: ResourceResolver) {
constructor(data: SCRN, cache: GfxRenderCache, resolver: ResourceResolver<JUTResType>) {
super(data, cache, null);
this.color = data.color;
this.resolveReferences(resolver);
Expand Down
12 changes: 6 additions & 6 deletions src/ZeldaWindWaker/d_resorce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { dGlobals } from "./Main.js";
import { cPhs__Status } from "./framework.js";
import { cBgD_t } from "./d_bg.js";
import { NamedArrayBufferSlice } from "../DataFetcher.js";
import { BLO, ResourceResolver, SCRN } from "../Common/JSYSTEM/J2Dv1.js";
import { BLO, JUTResType, ResourceResolver, SCRN } from "../Common/JSYSTEM/J2Dv1.js";

export interface DZSChunkHeader {
type: string;
Expand Down Expand Up @@ -115,12 +115,12 @@ export class dRes_control_c {
return resInfo.getResByID(resType, resID);
}

public getResResolver(arcName: string): ResourceResolver {
return (resType: string, resName: string) => {
public getResResolver(arcName: string): ResourceResolver<JUTResType> {
return (resType: JUTResType, resName: string) => {
switch(resType) {
case 'TIMG': return this.getObjectResByName(ResType.Bti, arcName, resName);
case 'TLUT': console.warn('TLUT resource references not yet supported'); debugger; return null;
case 'FONT': console.warn('FONT resource references not yet supported'); debugger; return null;
case JUTResType.TIMG: return this.getObjectResByName(ResType.Bti, arcName, resName);
case JUTResType.TLUT: console.warn('TLUT resource references not yet supported'); debugger; return null;
case JUTResType.FONT: console.warn('FONT resource references not yet supported'); debugger; return null;
default: return null;
}
}
Expand Down

0 comments on commit 404908c

Please sign in to comment.