Skip to content

Commit

Permalink
Displaying known resource types
Browse files Browse the repository at this point in the history
  • Loading branch information
xesf committed Mar 28, 2020
1 parent 24b8aa9 commit 8fa9bbb
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 274 deletions.
2 changes: 1 addition & 1 deletion src/dump/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ function dumpADSScripts(filepath, resindex) {
}
}

const filepath = path.join(__dirname, '../../data');
const filepath = path.join(__dirname, '../../data/castway');
const fc = fs.readFileSync(path.join(filepath, 'RESOURCE.MAP'));
const buffer = fc.buffer.slice(fc.byteOffset, fc.byteOffset + fc.byteLength);

Expand Down
61 changes: 30 additions & 31 deletions src/resources/bmp.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,38 +37,37 @@ export function loadBMPResourceEntry(entry) {
images[i].height = h;
}
block = getString(entry.data, offset, 3);
if (block !== 'BIN') {
throw `Invalid Type ${block}: expecting block type BIN`;
}
let blockSize = entry.data.getUint32(offset + 4, true);
const compressionType = entry.data.getUint8(offset + 8, true);
/* const uncompressedSize = */ entry.data.getUint32(offset + 9, true);
offset += 13;
blockSize -= 5; // take type and size out of the block
const compressedData = new DataView(entry.buffer.slice(offset, offset + blockSize));
const data = decompress(compressionType, compressedData, 0, compressedData.byteLength);
let dataIndex = 0;
let pixelIndex = 0;
for (let i = 0; i < numImages; i += 1) {
const image = images[i];
for (let h = 0; h < image.height; h += 1) {
for (let w = 0; w < image.width; w += 1) {
let c = data[dataIndex];
if (pixelIndex % 2 === 0) {
c >>= 4;
} else {
c &= 0x0f;
dataIndex += 1;
if (block === 'BIN') {
let blockSize = entry.data.getUint32(offset + 4, true);
const compressionType = entry.data.getUint8(offset + 8, true);
/* const uncompressedSize = */ entry.data.getUint32(offset + 9, true);
offset += 13;
blockSize -= 5; // take type and size out of the block
const compressedData = new DataView(entry.buffer.slice(offset, offset + blockSize));
const data = decompress(compressionType, compressedData, 0, compressedData.byteLength);
let dataIndex = 0;
let pixelIndex = 0;
for (let i = 0; i < numImages; i += 1) {
const image = images[i];
for (let h = 0; h < image.height; h += 1) {
for (let w = 0; w < image.width; w += 1) {
let c = data[dataIndex];
if (pixelIndex % 2 === 0) {
c >>= 4;
} else {
c &= 0x0f;
dataIndex += 1;
}
image.buffer[w + image.width * h] = c;
image.pixels[w + image.width * h] = {
index: c,
a: PALETTE[c].a,
r: PALETTE[c].r,
g: PALETTE[c].g,
b: PALETTE[c].b,
};
pixelIndex += 1;
}
image.buffer[w + image.width * h] = c;
image.pixels[w + image.width * h] = {
index: c,
a: PALETTE[c].a,
r: PALETTE[c].r,
g: PALETTE[c].g,
b: PALETTE[c].b,
};
pixelIndex += 1;
}
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/resources/data/scripting.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@

export const TTMCommandType = [
{ opcode: 0x0020, command: 'SAVE_BACKGROUND' }, // not used
{ opcode: 0x0020, command: 'SAVE_BACKGROUND' },
{ opcode: 0x0080, command: 'DRAW_BACKGROUND' },
{ opcode: 0x0110, command: 'PURGE' },
{ opcode: 0x0FF0, command: 'UPDATE' },
{ opcode: 0x1020, command: 'SET_DELAY' },
{ opcode: 0x1050, command: 'SLOT_IMAGE' },
{ opcode: 0x1060, command: 'SLOT_PALETTE' },
{ opcode: 0x1100, command: 'UNKNOWN_0' }, // Scene related?
{ opcode: 0x1100, command: 'UNKNOWN_0' },
{ opcode: 0x1110, command: 'SET_SCENE' },
{ opcode: 0x1120, command: 'SET_BACKGROUND' },
{ opcode: 0x1200, command: 'GOTO' },
{ opcode: 0x2000, command: 'SET_COLORS' },
{ opcode: 0x2010, command: 'SET_FRAME1' },
{ opcode: 0x2020, command: 'UNKNOWN_3' }, // SET_FRAME2 ???
{ opcode: 0x2010, command: 'SET_FRAME' },
{ opcode: 0x2020, command: 'SET_TIMER' },
{ opcode: 0x4000, command: 'SET_CLIP_REGION' },
{ opcode: 0x4110, command: 'FADE_OUT' },
{ opcode: 0x4120, command: 'FADE_IN' },
{ opcode: 0x4200, command: 'SAVE_IMAGE0' },
{ opcode: 0x4210, command: 'SAVE_IMAGE1' },
{ opcode: 0xA000, command: 'UNKNOWN_4' }, // Draw Line related?
{ opcode: 0xA050, command: 'UNKNOWN_5' }, // Draw Line related?
{ opcode: 0xA060, command: 'UNKNOWN_6' }, // Draw Line related?
{ opcode: 0x4200, command: 'DRAW_BACKGROUND_REGION' },
{ opcode: 0x4210, command: 'SAVE_IMAGE_REGION' },
{ opcode: 0xA000, command: 'UNKNOWN_4' },
{ opcode: 0xA050, command: 'SAVE_REGION' },
{ opcode: 0xA060, command: 'RESTORE_REGION' },
{ opcode: 0xA0A0, command: 'DRAW_LINE' },
{ opcode: 0xA100, command: 'DRAW_RECT' },
{ opcode: 0xA400, command: 'DRAW_BUBBLE' },
{ opcode: 0xA500, command: 'DRAW_SPRITE' },
{ opcode: 0xA510, command: 'DRAW_SPRITE1' }, // not used
{ opcode: 0xA510, command: 'DRAW_SPRITE1' },
{ opcode: 0xA520, command: 'DRAW_SPRITE_FLIP' },
{ opcode: 0xA530, command: 'DRAW_SPRITE3' }, // not used
{ opcode: 0xA530, command: 'DRAW_SPRITE3' },
{ opcode: 0xA600, command: 'CLEAR_SCREEN' },
{ opcode: 0xB600, command: 'DRAW_SCREEN' },
{ opcode: 0xC020, command: 'LOAD_SAMPLE' },
Expand Down
14 changes: 0 additions & 14 deletions src/resources/data/types.js

This file was deleted.

199 changes: 129 additions & 70 deletions src/resources/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
import { INDEX_STRING_SIZE } from '../constants';
import { getString } from '../utils/string';

import { ResourceType } from './data/types';
import { loadADSResourceEntry } from './ads';
import { loadBMPResourceEntry } from './bmp';
import { loadPALResourceEntry } from './pal';
import { loadSCRResourceEntry } from './scr';
import { loadTTMResourceEntry } from './ttm';

const NOP = () => {};

export const ResourceType = [
{ type: 'ADS', callback: loadADSResourceEntry }, // Animation sequences
{ type: 'BMP', callback: loadBMPResourceEntry }, // Various raw images
{ type: 'PAL', callback: loadPALResourceEntry }, // VGA palette
{ type: 'SCR', callback: loadSCRResourceEntry }, // Background raw images
{ type: 'TTM', callback: loadTTMResourceEntry }, // Scripting macros
{ type: 'VIN', callback: NOP }, //
{ type: 'SDS', callback: NOP }, //
{ type: 'FNT', callback: NOP }, //
{ type: 'DAT', callback: NOP }, //
{ type: 'DDS', callback: NOP }, //
{ type: 'TDS', callback: NOP }, //
{ type: 'REQ', callback: NOP }, //
{ type: 'WLD', callback: NOP }, //
{ type: 'SNG', callback: NOP }, //
{ type: 'ADL', callback: NOP }, //
{ type: 'ADH', callback: NOP }, //
{ type: 'RST', callback: NOP }, //
{ type: 'OVL', callback: NOP }, //
{ type: 'GDS', callback: NOP }, //
{ type: 'RST', callback: NOP }, //
{ type: 'SX', callback: NOP }, //
{ type: 'RES', callback: NOP }, //
];

const INDEX_HEADER_SIZE = 6;

Expand All @@ -10,7 +41,7 @@ const INDEX_HEADER_SIZE = 6;
* @param {*} filepath Full path of the file
* @param {*} filename File name
*/
export const loadResources = (buffer) => {
export const loadResourceMap = (buffer) => {
let offset = 0; // current resource offest
const resources = []; // list of resource files
const data = new DataView(buffer, offset, buffer.byteLength);
Expand Down Expand Up @@ -60,75 +91,103 @@ export const loadResources = (buffer) => {
};
};

export function loadResourceEntry(entry) {
/**
* Load all Resource details based on index resource file
* @param {*} filepath Full path of the file
* @param {*} filename File name
*/
export const loadResourcebyName = (resource, resbuffer) => {
const res = { ...resource };
res.size = resbuffer.byteLength;
const resData = new DataView(resbuffer, 0, res.size);

for (let e = 0; e < res.numEntries; e += 1) {
const entry = res.entries[e];
const name = getString(resData, entry.offset, INDEX_STRING_SIZE);
const entryCompressedSize = resData.getUint32(entry.offset + 13, true);
const startOffset = entry.offset + 17;
const endOffset = startOffset + entryCompressedSize;

entry.name = name;
entry.type = name.split('.')[1];
entry.compressedSize = entryCompressedSize;
entry.buffer = resbuffer.slice(startOffset, endOffset);
entry.data = new DataView(resbuffer, startOffset, entryCompressedSize);

res.entries.push({ ...entry });
}

return res;
};

export const loadResourceEntry = (entry) => {
const resType = ResourceType.find((r) => r.type === entry.type);
return resType.callback(entry);
}
};

/**
* Load all Resource details based on index resource file
* @param {*} filepath Full path of the file
* @param {*} filename File name
*/
export function loadResources(buffer, resbuffer) {
let offset = 0; // current resource offest
const resources = []; // list of resource files
const data = new DataView(buffer, offset, buffer.byteLength);

const header = {
unk0: data.getUint8(offset, true),
unk1: data.getUint8(offset + 1, true), // number of entries?
unk2: data.getUint8(offset + 2, true),
unk3: data.getUint8(offset + 3, true),
numResources: data.getUint8(offset + 4, true),
unk5: data.getUint8(offset + 5, true),
};
offset += INDEX_HEADER_SIZE;

// Read resource files and entries
// Read number of resource files (castaway only uses a single one)
for (let r = 0; r < header.numResources; r += 1) {
let innerOffset = offset;
const res = {
name: getString(data, innerOffset, INDEX_STRING_SIZE),
numEntries: data.getUint16(innerOffset + 13, true),
size: 0,
entries: [],
};
resources.push(res);
innerOffset += 15;

res.size = resbuffer.byteLength;
const resData = new DataView(resbuffer, 0, res.size);

for (let e = 0; e < res.numEntries; e += 1) {
// from index
const entrySize = data.getUint16(innerOffset, true); // uncompressed size
const entryOffset = data.getUint32(innerOffset + 4, true);
// from resource
const name = getString(resData, entryOffset, INDEX_STRING_SIZE);
const entryCompressedSize = resData.getUint32(entryOffset + 13, true);
const startOffset = entryOffset + 17;
const endOffset = startOffset + entryCompressedSize;

// /**
// * Load all Resource details based on index resource file
// * @param {*} filepath Full path of the file
// * @param {*} filename File name
// */
// export function loadResources(buffer, resbuffer) {
// let offset = 0; // current resource offest
// const resources = []; // list of resource files
// const data = new DataView(buffer, offset, buffer.byteLength);

// const header = {
// unk0: data.getUint8(offset, true),
// unk1: data.getUint8(offset + 1, true), // number of entries?
// unk2: data.getUint8(offset + 2, true),
// unk3: data.getUint8(offset + 3, true),
// numResources: data.getUint8(offset + 4, true),
// unk5: data.getUint8(offset + 5, true),
// };
// offset += INDEX_HEADER_SIZE;

// // Read resource files and entries
// // Read number of resource files (castaway only uses a single one)
// for (let r = 0; r < header.numResources; r += 1) {
// let innerOffset = offset;
// const res = {
// name: getString(data, innerOffset, INDEX_STRING_SIZE),
// numEntries: data.getUint16(innerOffset + 13, true),
// size: 0,
// entries: [],
// };
// resources.push(res);
// innerOffset += 15;

// res.size = resbuffer.byteLength;
// const resData = new DataView(resbuffer, 0, res.size);

// for (let e = 0; e < res.numEntries; e += 1) {
// // from index
// const entrySize = data.getUint16(innerOffset, true); // uncompressed size
// const entryOffset = data.getUint32(innerOffset + 4, true);
// // from resource
// const name = getString(resData, entryOffset, INDEX_STRING_SIZE);
// const entryCompressedSize = resData.getUint32(entryOffset + 13, true);
// const startOffset = entryOffset + 17;
// const endOffset = startOffset + entryCompressedSize;

// const entry = {
// name,
// type: name.split('.')[1],
// size: entrySize, // uncompressed size
// offset: entryOffset,
// compressedSize: entryCompressedSize,
// buffer: resbuffer.slice(startOffset, endOffset),
// data: new DataView(resbuffer, startOffset, entryCompressedSize),
// };
// innerOffset += 8;

// res.entries.push(entry);
// }
// }

// return {
// header,
// resources
// };
// }
const entry = {
name,
type: name.split('.')[1],
size: entrySize, // uncompressed size
offset: entryOffset,
compressedSize: entryCompressedSize,
buffer: resbuffer.slice(startOffset, endOffset),
data: new DataView(resbuffer, startOffset, entryCompressedSize),
};
innerOffset += 8;

res.entries.push(entry);
}
}

return {
header,
resources
};
}
Loading

0 comments on commit 8fa9bbb

Please sign in to comment.