2004-04-12 21:40:49 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2005-01-01 16:20:17 +00:00
|
|
|
* Copyright (C) 2004-2005 The ScummVM project
|
2004-04-12 21:40:49 +00:00
|
|
|
*
|
|
|
|
* The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 13:19:15 +00:00
|
|
|
// RSC Resource file management module
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/rscfile_mod.h"
|
|
|
|
#include "saga/rscfile.h"
|
2004-12-15 00:24:12 +00:00
|
|
|
#include "saga/stream.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
2005-01-02 20:29:27 +00:00
|
|
|
static struct Substitutes {
|
|
|
|
uint32 id;
|
|
|
|
const char *fname;
|
|
|
|
} substitutes[] = {
|
|
|
|
{ 1529, "wyrm.pak" },
|
|
|
|
{ 1530, "wyrm1.dlt" },
|
|
|
|
{ 1531, "wyrm2.dlt" },
|
|
|
|
{ 1532, "wyrm3.dlt" },
|
|
|
|
{ 1533, "wyrm4.dlt" },
|
|
|
|
{ 1796, "credit3n.dlt" },
|
|
|
|
{ 1797, "credit4n.dlt" }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
RSCFILE_CONTEXT *RSC_CreateContext() {
|
|
|
|
RSCFILE_CONTEXT empty_context;
|
2004-04-23 11:40:51 +00:00
|
|
|
empty_context.rc_file_fspec = NULL;
|
|
|
|
empty_context.rc_file_loaded = 0;
|
|
|
|
empty_context.rc_res_table = NULL;
|
|
|
|
empty_context.rc_res_ct = 0;
|
2004-08-22 18:28:42 +00:00
|
|
|
empty_context.rc_file = new File();
|
2004-10-27 21:32:28 +00:00
|
|
|
RSCFILE_CONTEXT *new_context;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-02 14:52:11 +00:00
|
|
|
new_context = (RSCFILE_CONTEXT *)malloc(sizeof(*new_context));
|
2004-04-12 21:40:49 +00:00
|
|
|
if (new_context == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*new_context = empty_context;
|
|
|
|
|
|
|
|
return new_context;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_OpenContext(RSCFILE_CONTEXT *rsc_context, const char *fspec) {
|
2004-08-22 18:28:42 +00:00
|
|
|
if (rsc_context->rc_file->isOpen()) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
if (!rsc_context->rc_file->open(fspec)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rsc_context->rc_file_fspec = fspec;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
if (RSC_LoadRSC(rsc_context) != SUCCESS) {
|
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rsc_context->rc_file_loaded = 1;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_CloseContext(RSCFILE_CONTEXT *rsc_context) {
|
2004-08-22 18:28:42 +00:00
|
|
|
if (rsc_context->rc_file->isOpen()) {
|
|
|
|
rsc_context->rc_file->close();
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RSC_FreeRSC(rsc_context);
|
|
|
|
|
|
|
|
rsc_context->rc_file_loaded = 0;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_DestroyContext(RSCFILE_CONTEXT *rsc_context) {
|
2004-04-12 21:40:49 +00:00
|
|
|
RSC_CloseContext(rsc_context);
|
|
|
|
|
|
|
|
if (rsc_context->rc_file_loaded) {
|
|
|
|
free(rsc_context->rc_res_table);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(rsc_context);
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_LoadRSC(RSCFILE_CONTEXT *rsc) {
|
2004-04-30 23:02:23 +00:00
|
|
|
uint32 res_tbl_ct;
|
|
|
|
uint32 res_tbl_offset;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
byte tblinfo_buf[RSC_TABLEINFO_SIZE];
|
|
|
|
byte *tbl_buf;
|
2004-04-12 21:40:49 +00:00
|
|
|
size_t tbl_len;
|
2004-04-30 23:02:23 +00:00
|
|
|
uint32 i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
RSCFILE_RESOURCE *rsc_restbl;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
if (rsc->rc_file->size() < RSC_MIN_FILESIZE) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 13:19:15 +00:00
|
|
|
// Read resource table info from the rear end of file
|
2004-08-22 18:28:42 +00:00
|
|
|
rsc->rc_file->seek((long)(rsc->rc_file->size() - 8), SEEK_SET);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
if (rsc->rc_file->read(tblinfo_buf, RSC_TABLEINFO_SIZE) != RSC_TABLEINFO_SIZE) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-12-15 00:24:12 +00:00
|
|
|
|
|
|
|
MemoryReadStreamEndian readS(tblinfo_buf, RSC_TABLEINFO_SIZE, IS_BIG_ENDIAN);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
res_tbl_offset = readS.readUint32();
|
|
|
|
res_tbl_ct = readS.readUint32();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 13:19:15 +00:00
|
|
|
// Check for sane table offset
|
2004-08-22 18:28:42 +00:00
|
|
|
if (res_tbl_offset != rsc->rc_file->size() - RSC_TABLEINFO_SIZE - RSC_TABLEENTRY_SIZE * res_tbl_ct) {
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 13:19:15 +00:00
|
|
|
// Load resource table
|
2004-04-12 21:40:49 +00:00
|
|
|
tbl_len = RSC_TABLEENTRY_SIZE * res_tbl_ct;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
tbl_buf = (byte *)malloc(tbl_len);
|
2004-04-12 21:40:49 +00:00
|
|
|
if (tbl_buf == NULL) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
rsc->rc_file->seek((long)res_tbl_offset, SEEK_SET);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
if (rsc->rc_file->read(tbl_buf, tbl_len) != tbl_len) {
|
2004-04-12 21:40:49 +00:00
|
|
|
free(tbl_buf);
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-02 14:52:11 +00:00
|
|
|
rsc_restbl = (RSCFILE_RESOURCE *)malloc(res_tbl_ct * sizeof(*rsc_restbl));
|
2004-04-12 21:40:49 +00:00
|
|
|
if (rsc_restbl == NULL) {
|
|
|
|
free(tbl_buf);
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
MemoryReadStreamEndian readS1(tbl_buf, tbl_len, IS_BIG_ENDIAN);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
debug(9, "RSC %s", rsc->rc_file_fspec);
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < res_tbl_ct; i++) {
|
2004-12-15 00:24:12 +00:00
|
|
|
rsc_restbl[i].res_offset = readS1.readUint32();
|
|
|
|
rsc_restbl[i].res_size = readS1.readUint32();
|
|
|
|
//debug(9, "#%x Offset:%x Size:%x", i, rsc_restbl[i].res_offset, rsc_restbl[i].res_size);
|
2004-08-22 18:28:42 +00:00
|
|
|
if ((rsc_restbl[i].res_offset > rsc->rc_file->size()) || (rsc_restbl[i].res_size > rsc->rc_file->size())) {
|
2004-04-12 21:40:49 +00:00
|
|
|
free(tbl_buf);
|
|
|
|
free(rsc_restbl);
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rsc->rc_res_table = rsc_restbl;
|
|
|
|
rsc->rc_res_ct = res_tbl_ct;
|
|
|
|
|
|
|
|
free(tbl_buf);
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_FreeRSC(RSCFILE_CONTEXT *rsc) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if (!rsc->rc_file_loaded) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-08-22 18:28:42 +00:00
|
|
|
delete rsc->rc_file;
|
|
|
|
rsc->rc_file = NULL;
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
free(rsc->rc_res_table);
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
uint32 RSC_GetResourceCount(RSCFILE_CONTEXT *rsc) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return (rsc == NULL) ? 0 : rsc->rc_res_ct;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_GetResourceSize(RSCFILE_CONTEXT *rsc, uint32 res_num, uint32 *res_size) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if ((rsc == NULL) || (res_size == NULL)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res_num > (rsc->rc_res_ct - 1)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*res_size = rsc->rc_res_table[res_num].res_size;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_GetResourceOffset(RSCFILE_CONTEXT *rsc, uint32 res_num, uint32 *res_offset) {
|
2004-04-12 21:40:49 +00:00
|
|
|
if ((rsc == NULL) || (res_offset == NULL)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res_num > (rsc->rc_res_ct - 1)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*res_offset = rsc->rc_res_table[res_num].res_offset;
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-11-15 03:03:48 +00:00
|
|
|
const char *RSC_FileName(RSCFILE_CONTEXT *rsc) {
|
|
|
|
return rsc->rc_file_fspec;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
int RSC_LoadResource(RSCFILE_CONTEXT *rsc, uint32 res_num, byte **res_p, size_t *res_size_p) {
|
2004-04-30 23:02:23 +00:00
|
|
|
uint32 res_offset;
|
2005-01-02 20:29:27 +00:00
|
|
|
size_t res_size = 0;
|
|
|
|
byte *res_buf = NULL;
|
|
|
|
int substnum = -1;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if ((rsc == NULL) || (res_p == NULL)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (res_num > (rsc->rc_res_ct - 1)) {
|
2004-10-27 21:32:28 +00:00
|
|
|
return FAILURE;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-02 20:29:27 +00:00
|
|
|
debug(8, "LoadResource %d", res_num);
|
|
|
|
for (int i = 0; i < ARRAYSIZE(substitutes); i++)
|
|
|
|
if (substitutes[i].id == res_num) {
|
|
|
|
substnum = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (!(_vm->getFeatures() & GF_WYRMKEEP))
|
2005-01-06 14:02:53 +00:00
|
|
|
substnum = -1;
|
|
|
|
|
2005-01-02 20:29:27 +00:00
|
|
|
if (substnum != -1) {
|
|
|
|
File in;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-02 20:29:27 +00:00
|
|
|
if (in.open(substitutes[substnum].fname)) {
|
|
|
|
res_size = in.size();
|
|
|
|
if ((res_buf = (byte *)malloc(res_size)) == NULL)
|
|
|
|
return MEM;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2005-01-02 20:29:27 +00:00
|
|
|
in.read(res_buf, res_size);
|
|
|
|
in.close();
|
|
|
|
debug(8, "LoadResource: substituted resource by %s", substitutes[substnum].fname);
|
|
|
|
} else {
|
|
|
|
substnum = -1;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2005-01-02 20:29:27 +00:00
|
|
|
if (substnum == -1) {
|
|
|
|
res_offset = rsc->rc_res_table[res_num].res_offset;
|
|
|
|
res_size = rsc->rc_res_table[res_num].res_size;
|
|
|
|
|
|
|
|
if ((res_buf = (byte *)malloc(res_size)) == NULL)
|
|
|
|
return MEM;
|
|
|
|
|
|
|
|
rsc->rc_file->seek((long)res_offset, SEEK_SET);
|
|
|
|
|
|
|
|
if (rsc->rc_file->read(res_buf, res_size) != res_size) {
|
|
|
|
free(res_buf);
|
|
|
|
return FAILURE;
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*res_p = res_buf;
|
|
|
|
|
|
|
|
if (res_size_p != NULL) {
|
|
|
|
*res_size_p = res_size;
|
|
|
|
}
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 13:19:15 +00:00
|
|
|
int RSC_FreeResource(byte *resource_ptr) {
|
2004-04-12 21:40:49 +00:00
|
|
|
free(resource_ptr);
|
|
|
|
|
2004-10-27 21:32:28 +00:00
|
|
|
return SUCCESS;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-12-15 00:24:12 +00:00
|
|
|
int RSC_ConvertID(int id) {
|
|
|
|
int res = id;
|
|
|
|
|
2005-01-11 21:10:36 +00:00
|
|
|
if (_vm->getFeatures() & GF_MAC_RESOURCES) {
|
2004-12-15 00:24:12 +00:00
|
|
|
if (res > 1537)
|
|
|
|
res -= 2;
|
|
|
|
else if (res == 1535 || res == 1536) {
|
|
|
|
error ("Wrong resource number %d for Mac ITE");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2004-04-12 21:40:49 +00:00
|
|
|
} // End of namespace Saga
|