2007-05-30 21:56:52 +00:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers , whose names
* are too numerous to list here . Please refer to the COPYRIGHT
* file distributed with this source distribution .
2003-07-28 01:44:38 +00:00
*
2007-05-31 20:28:29 +00:00
* Additional copyright for this file :
* Copyright ( C ) 1994 - 1998 Revolution Software Ltd .
*
2003-07-28 01:44:38 +00:00
* 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
2005-10-18 01:30:26 +00:00
* Foundation , Inc . , 51 Franklin Street , Fifth Floor , Boston , MA 02110 - 1301 , USA .
2003-07-28 01:44:38 +00:00
*
2006-02-09 15:12:44 +00:00
* $ URL $
* $ Id $
2003-07-28 01:44:38 +00:00
*/
2007-09-19 08:40:12 +00:00
2003-11-16 14:18:29 +00:00
# include "common/file.h"
2005-05-08 13:05:31 +00:00
# include "common/system.h"
2006-02-17 15:07:36 +00:00
2003-10-28 19:51:30 +00:00
# include "sword2/sword2.h"
# include "sword2/defs.h"
2006-02-17 15:07:36 +00:00
# include "sword2/header.h"
2005-02-22 07:37:50 +00:00
# include "sword2/console.h"
2004-02-05 14:19:07 +00:00
# include "sword2/logic.h"
2004-11-14 15:00:01 +00:00
# include "sword2/memory.h"
2004-02-05 14:19:07 +00:00
# include "sword2/resman.h"
2004-11-14 15:00:01 +00:00
# include "sword2/router.h"
2006-02-17 15:07:36 +00:00
# include "sword2/screen.h"
Began what I hope is the final major restructuring of the BS2 engine.
In this first step, I have moved all opcode functions into functions.cpp,
instead of having them scattered all over the place.
To get things to compile again, I had to rewrite the overly complicated
sound effects handling. It's much simpler now.
The next step will be to move any non-trivial code out of the opcode
functions and into the appropriate object. This, I hope, will make it
easier to create well-separated objects, instead of the current mess.
I also want to tear down the artificial boundary between the main directory
and the "driver" directory. We already have a cross-platform layer; there's
no need to have yet another one. (Actually, the rewriting of the sound
effects code took one first step in this direction.)
At the final stage, I'd like to get rid of the "drivers" directory
completely, but I'll probably need some help with that if I want to
preserve the CVS history of the code.
Things will probably be a bit bumpy along the way, but I seem to have
reached a point of relative stability again, which is why I'm commiting
this now.
svn-id: r16668
2005-01-28 16:33:14 +00:00
# include "sword2/sound.h"
2003-07-28 01:44:38 +00:00
2003-11-19 18:40:53 +00:00
# define Debug_Printf _vm->_debugger->DebugPrintf
2003-10-04 00:52:27 +00:00
namespace Sword2 {
2003-11-23 13:40:24 +00:00
// Welcome to the easy resource manager - written in simple code for easy
2003-09-02 09:55:11 +00:00
// maintenance
2005-07-30 21:11:48 +00:00
//
2003-11-23 13:40:24 +00:00
// The resource compiler will create two files
2003-09-02 09:55:11 +00:00
//
2003-07-28 01:44:38 +00:00
// resource.inf which is a list of ascii cluster file names
2003-09-02 09:55:11 +00:00
// resource.tab which is a table which tells us which cluster a resource
2005-02-25 14:19:30 +00:00
// is located in and the number within the cluster
2004-04-23 07:02:11 +00:00
2006-01-01 16:29:23 +00:00
enum {
BOTH = 0x0 , // Cluster is on both CDs
CD1 = 0x1 , // Cluster is on CD1 only
CD2 = 0x2 , // Cluster is on CD2 only
LOCAL_CACHE = 0x4 , // Cluster is cached on HDD
LOCAL_PERM = 0x8 // Cluster is on HDD.
} ;
2003-12-28 15:08:12 +00:00
struct CdInf {
2003-09-02 09:55:11 +00:00
uint8 clusterName [ 20 ] ; // Null terminated cluster name.
uint8 cd ; // Cd cluster is on and whether it is on the local drive or not.
Applied my own patch #1341495, in an attempt to fix alignment issues
reported by Crilith.
To elaborate a bit, the engine no longer accesses resource data through
packed structs. Instead it uses memory streams and the READ/WRITE
functions.
If data is mainly read, not written, I have replaced the old struct with a
new one with a read() function to read the whole thing from memory into the
struct's variables, and a write() function to dump the struct's variables
to memory. In fact, most of these write() functions remain unused.
If data is both read and written, I have replaced the struct with a class
with individual get/set functions to replace the old variables. This
manipulates memory directly.
Since I'm fairly sure that these structs are frequently stored as local
variables for a script, all script variables (both local and global) are
stored as little-endian and accessed through the READ/WRITE functions,
rather than being treated as arrays of 32-bit integers.
On a positive note, the functions for doing endian conversion of resources
and save games have been removed, and some general cleanups have been made
to assist in the rewrite.
Initial reports indicate that this patch indeed fixes alignment issues, and
that I have not - surprisingly - broken the game on big-endian platforms.
At least not in any immediately obvious way. And there's still plenty of
time to fix regressions before 0.9.0, too.
svn-id: r19366
2005-10-29 21:24:54 +00:00
} ;
2003-07-28 01:44:38 +00:00
2003-11-08 15:47:51 +00:00
ResourceManager : : ResourceManager ( Sword2Engine * vm ) {
_vm = vm ;
2006-04-26 06:12:03 +00:00
_totalClusters = 0 ;
2006-05-18 14:28:19 +00:00
_resList = NULL ;
2006-04-26 06:12:03 +00:00
_resConvTable = NULL ;
_cacheStart = NULL ;
_cacheEnd = NULL ;
_usedMem = 0 ;
}
ResourceManager : : ~ ResourceManager ( ) {
Resource * res = _cacheStart ;
while ( res ) {
_vm - > _memory - > memFree ( res - > ptr ) ;
res = res - > next ;
}
for ( uint i = 0 ; i < _totalClusters ; i + + )
free ( _resFiles [ i ] . entryTab ) ;
free ( _resList ) ;
free ( _resConvTable ) ;
}
2007-09-19 08:40:12 +00:00
2006-04-26 06:12:03 +00:00
bool ResourceManager : : init ( ) {
uint32 i , j ;
2004-03-04 08:03:32 +00:00
// Until proven differently, assume we're on CD 1. This is so the start
// dialog will be able to play any music at all.
2006-04-01 21:42:11 +00:00
2006-01-01 16:29:23 +00:00
setCD ( 1 ) ;
2004-03-04 08:03:32 +00:00
2003-09-02 09:55:11 +00:00
// We read in the resource info which tells us the names of the
// resource cluster files ultimately, although there might be groups
// within the clusters at this point it makes no difference. We only
// wish to know what resource files there are and what is in each
2003-07-28 01:44:38 +00:00
2005-05-10 22:56:25 +00:00
Common : : File file ;
2003-07-28 01:44:38 +00:00
2006-04-26 06:12:03 +00:00
if ( ! file . open ( " resource.inf " ) ) {
2008-10-02 17:48:01 +00:00
GUIErrorMessage ( " Broken Sword 2: Cannot open resource.inf " ) ;
2006-04-26 06:12:03 +00:00
return false ;
}
2003-07-28 01:44:38 +00:00
2006-04-01 21:42:11 +00:00
// The resource.inf file is a simple text file containing the names of
// all the resource files.
2004-04-23 07:02:11 +00:00
2008-12-03 21:52:51 +00:00
while ( 1 ) {
char * buf = _resFiles [ _totalClusters ] . fileName ;
uint len = sizeof ( _resFiles [ _totalClusters ] . fileName ) ;
if ( ! file . readLine_NEW ( buf , len ) )
break ;
int pos = strlen ( buf ) ;
if ( buf [ pos - 1 ] = = 0x0A )
buf [ pos - 1 ] = 0 ;
2005-02-21 02:29:18 +00:00
_resFiles [ _totalClusters ] . numEntries = - 1 ;
_resFiles [ _totalClusters ] . entryTab = NULL ;
2006-04-26 06:12:03 +00:00
if ( + + _totalClusters > = MAX_res_files ) {
2008-10-02 17:48:01 +00:00
GUIErrorMessage ( " Broken Sword 2: Too many entries in resource.inf " ) ;
2006-04-26 06:12:03 +00:00
return false ;
}
2006-04-01 21:42:11 +00:00
}
2003-07-28 01:44:38 +00:00
2006-04-01 21:42:11 +00:00
file . close ( ) ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
// Now load in the binary id to res conversion table
2006-04-26 06:12:03 +00:00
if ( ! file . open ( " resource.tab " ) ) {
2008-10-02 17:48:01 +00:00
GUIErrorMessage ( " Broken Sword 2: Cannot open resource.tab " ) ;
2006-04-26 06:12:03 +00:00
return false ;
}
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
// Find how many resources
2006-04-01 21:42:11 +00:00
uint32 size = file . size ( ) ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
_totalResFiles = size / 4 ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
// Table seems ok so malloc some space
2005-05-12 13:12:15 +00:00
_resConvTable = ( uint16 * ) malloc ( size ) ;
2004-04-23 07:02:11 +00:00
for ( i = 0 ; i < size / 2 ; i + + )
_resConvTable [ i ] = file . readUint16LE ( ) ;
2003-07-28 01:44:38 +00:00
2003-09-02 09:55:11 +00:00
if ( file . ioFailed ( ) ) {
2003-07-28 01:44:38 +00:00
file . close ( ) ;
2008-10-02 17:48:01 +00:00
GUIErrorMessage ( " Broken Sword 2: Cannot read resource.tab " ) ;
2006-04-26 06:12:03 +00:00
return false ;
2003-07-28 01:44:38 +00:00
}
2003-09-02 09:55:11 +00:00
file . close ( ) ;
2003-07-28 01:44:38 +00:00
2009-04-07 19:52:46 +00:00
// Check that we have cd.inf file, unless we are running PSX
// version, which has all files on one disc.
if ( ! file . open ( " cd.inf " ) & & ! Sword2Engine : : isPsx ( ) ) {
2008-10-02 17:48:01 +00:00
GUIErrorMessage ( " Broken Sword 2: Cannot open cd.inf " ) ;
2006-04-26 06:12:03 +00:00
return false ;
}
2003-07-28 01:44:38 +00:00
2003-12-28 15:08:12 +00:00
CdInf * cdInf = new CdInf [ _totalClusters ] ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
for ( i = 0 ; i < _totalClusters ; i + + ) {
2006-01-01 13:46:42 +00:00
2009-04-07 19:52:46 +00:00
if ( Sword2Engine : : isPsx ( ) ) { // We are running PSX version, artificially fill CdInf structure
cdInf [ i ] . cd = CD1 ;
} else { // We are running PC version, read cd.inf file
file . read ( cdInf [ i ] . clusterName , sizeof ( cdInf [ i ] . clusterName ) ) ;
cdInf [ i ] . cd = file . readByte ( ) ;
if ( file . ioFailed ( ) ) {
delete cdInf ;
file . close ( ) ;
GUIErrorMessage ( " Broken Sword 2: Cannot read cd.inf " ) ;
return false ;
}
2005-07-30 21:11:48 +00:00
2006-04-26 06:12:03 +00:00
}
2006-01-01 13:46:42 +00:00
// It has been reported that there are two different versions
// of the cd.inf file: One where all clusters on CD also have
// the LOCAL_CACHE bit set. This bit is no longer used. To
// avoid future problems, let's normalize the flag once and for
// all here.
if ( cdInf [ i ] . cd & LOCAL_PERM )
2006-01-01 16:29:23 +00:00
cdInf [ i ] . cd = 0 ;
else if ( cdInf [ i ] . cd & CD1 )
cdInf [ i ] . cd = 1 ;
2006-01-01 13:46:42 +00:00
else if ( cdInf [ i ] . cd & CD2 )
2006-01-01 16:29:23 +00:00
cdInf [ i ] . cd = 2 ;
2006-01-01 13:46:42 +00:00
else
2006-01-01 16:29:23 +00:00
cdInf [ i ] . cd = 0 ;
2006-04-26 06:12:03 +00:00
// Any file on "CD 0" may be needed at all times. Verify that
// it exists. Any other missing cluster will be requested with
// an "insert CD" message. Of course, the file may still vanish
// during game-play (oh, that wascally wabbit!) in which case
// the resource manager will print a fatal error.
2006-05-24 04:48:39 +00:00
if ( cdInf [ i ] . cd = = 0 & & ! Common : : File : : exists ( ( char * ) cdInf [ i ] . clusterName ) ) {
2008-10-02 17:48:01 +00:00
GUIErrorMessage ( " Broken Sword 2: Cannot find " + Common : : String ( ( char * ) cdInf [ i ] . clusterName ) ) ;
2008-05-07 14:31:45 +00:00
delete [ ] cdInf ;
2006-04-26 06:12:03 +00:00
return false ;
}
2003-07-28 01:44:38 +00:00
}
file . close ( ) ;
2009-04-07 19:52:46 +00:00
// We check the presence of resource files in cd.inf
// This is ok in PC version, but in PSX version we don't
// have cd.inf so we'll have to skip this.
if ( ! Sword2Engine : : isPsx ( ) ) {
for ( i = 0 ; i < _totalClusters ; i + + ) {
for ( j = 0 ; j < _totalClusters ; j + + ) {
if ( scumm_stricmp ( ( char * ) cdInf [ j ] . clusterName , _resFiles [ i ] . fileName ) = = 0 )
break ;
}
2003-07-28 01:44:38 +00:00
2009-04-07 19:52:46 +00:00
if ( j = = _totalClusters ) {
delete [ ] cdInf ;
GUIErrorMessage ( Common : : String ( _resFiles [ i ] . fileName ) + " is not in cd.inf " ) ;
return false ;
}
2003-07-28 01:44:38 +00:00
2009-04-07 19:52:46 +00:00
_resFiles [ i ] . cd = cdInf [ j ] . cd ;
}
2003-07-28 01:44:38 +00:00
}
2008-05-07 14:31:45 +00:00
delete [ ] cdInf ;
2003-11-16 14:18:29 +00:00
2004-04-23 07:02:11 +00:00
debug ( 1 , " %d resources in %d cluster files " , _totalResFiles , _totalClusters ) ;
for ( i = 0 ; i < _totalClusters ; i + + )
2005-12-30 19:27:45 +00:00
debug ( 2 , " filename of cluster %d: -%s (%d) " , i , _resFiles [ i ] . fileName , _resFiles [ i ] . cd ) ;
2003-07-28 01:44:38 +00:00
2005-05-12 13:12:15 +00:00
_resList = ( Resource * ) malloc ( _totalResFiles * sizeof ( Resource ) ) ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
for ( i = 0 ; i < _totalResFiles ; i + + ) {
_resList [ i ] . ptr = NULL ;
_resList [ i ] . size = 0 ;
_resList [ i ] . refCount = 0 ;
2005-02-21 02:29:18 +00:00
_resList [ i ] . prev = _resList [ i ] . next = NULL ;
2003-07-28 01:44:38 +00:00
}
2006-04-26 06:12:03 +00:00
return true ;
2003-07-28 01:44:38 +00:00
}
2004-04-23 07:02:11 +00:00
/**
* Returns the address of a resource . Loads if not in memory . Retains a count .
*/
byte * ResourceManager : : openResource ( uint32 res , bool dump ) {
2003-11-23 13:40:24 +00:00
assert ( res < _totalResFiles ) ;
2003-09-12 20:48:28 +00:00
2009-04-07 19:52:46 +00:00
// FIXME: In PSX edition, not all top menu icons are present (TOP menu is not used).
// Though, at present state, the engine still ask for the resources.
if ( Sword2Engine : : isPsx ( ) ) { // We need to "rewire" missing icons
if ( res = = 342 ) res = 364 ; // Rewire RESTORE ICON to SAVE ICON
}
2004-04-23 07:02:11 +00:00
// Is the resource in memory already? If not, load it.
if ( ! _resList [ res ] . ptr ) {
// Fetch the correct file and read in the correct portion.
2005-02-21 02:29:18 +00:00
uint16 cluFileNum = _resConvTable [ res * 2 ] ; // points to the number of the ascii filename
2009-04-07 19:52:46 +00:00
2005-02-21 02:29:18 +00:00
assert ( cluFileNum ! = 0xffff ) ;
2003-09-12 20:48:28 +00:00
2004-04-23 07:02:11 +00:00
// Relative resource within the file
// First we have to find the file via the _resConvTable
2005-02-21 02:29:18 +00:00
uint16 actual_res = _resConvTable [ ( res * 2 ) + 1 ] ;
2005-07-30 21:11:48 +00:00
2005-02-21 02:29:18 +00:00
debug ( 5 , " openResource %s res %d " , _resFiles [ cluFileNum ] . fileName , res ) ;
2003-09-12 20:48:28 +00:00
// If we're loading a cluster that's only available from one
// of the CDs, remember which one so that we can play the
2005-12-21 10:57:48 +00:00
// correct speech and music.
2003-09-12 20:48:28 +00:00
2009-04-07 19:52:46 +00:00
if ( Sword2Engine : : isPsx ( ) ) // We have only one disk in PSX version
setCD ( CD1 ) ;
else
setCD ( _resFiles [ cluFileNum ] . cd ) ;
2003-09-12 20:48:28 +00:00
Re-enabled the CD swapping code, after rewriting it a bit.
If a cluster file isn't found the resource manager will first check if it's
one of the files that it expects to find on the hard disk. If so, it's
considered a fatal error.
Otherwise it will present the user with an "Insert CD1" or "Insert CD2"
message, just like the original did. Unlike the original, the user will
have to press a button or click the mouse to indicate when he's done. I
don't know if we even can detect the CD automatically in any portable way.
As far as I can see, we'll need at least two separate path settings for
this to actually work: one for the HD install directory, and one or two for
the CDs. The file that are supposed to be found on the HD are only on one
of the CDs, so the amount of CD swapping would probably be unbearable
otherwise.
As a consequence, I haven't actually tried running the game from CD yet.
By the way, the old caching code has been removed completely now. All it
did was to copy the cluster file to HD for faster access. ScummVM never did
that, but so far no one has complained.
svn-id: r11273
2003-11-13 07:59:52 +00:00
// Actually, as long as the file can be found we don't really
// care which CD it's on. But if we can't find it, keep asking
// for the CD until we do.
2003-09-12 20:48:28 +00:00
2005-05-10 22:56:25 +00:00
Common : : File * file = openCluFile ( cluFileNum ) ;
2004-04-23 07:02:11 +00:00
2005-02-21 02:29:18 +00:00
if ( _resFiles [ cluFileNum ] . entryTab = = NULL ) {
// we didn't read from this file before, get its index table
readCluIndex ( cluFileNum , file ) ;
Re-enabled the CD swapping code, after rewriting it a bit.
If a cluster file isn't found the resource manager will first check if it's
one of the files that it expects to find on the hard disk. If so, it's
considered a fatal error.
Otherwise it will present the user with an "Insert CD1" or "Insert CD2"
message, just like the original did. Unlike the original, the user will
have to press a button or click the mouse to indicate when he's done. I
don't know if we even can detect the CD automatically in any portable way.
As far as I can see, we'll need at least two separate path settings for
this to actually work: one for the HD install directory, and one or two for
the CDs. The file that are supposed to be found on the HD are only on one
of the CDs, so the amount of CD swapping would probably be unbearable
otherwise.
As a consequence, I haven't actually tried running the game from CD yet.
By the way, the old caching code has been removed completely now. All it
did was to copy the cluster file to HD for faster access. ScummVM never did
that, but so far no one has complained.
svn-id: r11273
2003-11-13 07:59:52 +00:00
}
2003-09-12 20:48:28 +00:00
2005-02-21 02:29:18 +00:00
uint32 pos = _resFiles [ cluFileNum ] . entryTab [ actual_res * 2 + 0 ] ;
uint32 len = _resFiles [ cluFileNum ] . entryTab [ actual_res * 2 + 1 ] ;
2003-09-12 20:48:28 +00:00
2005-02-21 02:29:18 +00:00
file - > seek ( pos , SEEK_SET ) ;
2003-09-12 20:48:28 +00:00
2004-04-23 07:02:11 +00:00
debug ( 6 , " res len %d " , len ) ;
2003-09-12 20:48:28 +00:00
2004-04-23 07:02:11 +00:00
// Ok, we know the length so try and allocate the memory.
_resList [ res ] . ptr = _vm - > _memory - > memAlloc ( len , res ) ;
_resList [ res ] . size = len ;
2004-04-23 11:36:06 +00:00
_resList [ res ] . refCount = 0 ;
2003-09-12 20:48:28 +00:00
2005-02-21 02:29:18 +00:00
file - > read ( _resList [ res ] . ptr , len ) ;
2003-09-12 20:48:28 +00:00
2006-07-08 15:39:26 +00:00
debug ( 3 , " Loaded resource '%s' (%d) from '%s' on CD %d (%d) " , fetchName ( _resList [ res ] . ptr ) , res , _resFiles [ cluFileNum ] . fileName , getCD ( ) , _resFiles [ cluFileNum ] . cd ) ;
2005-12-21 10:57:48 +00:00
2003-11-23 13:40:24 +00:00
if ( dump ) {
char buf [ 256 ] ;
2004-09-12 17:10:59 +00:00
const char * tag ;
2003-11-23 13:40:24 +00:00
Applied my own patch #1341495, in an attempt to fix alignment issues
reported by Crilith.
To elaborate a bit, the engine no longer accesses resource data through
packed structs. Instead it uses memory streams and the READ/WRITE
functions.
If data is mainly read, not written, I have replaced the old struct with a
new one with a read() function to read the whole thing from memory into the
struct's variables, and a write() function to dump the struct's variables
to memory. In fact, most of these write() functions remain unused.
If data is both read and written, I have replaced the struct with a class
with individual get/set functions to replace the old variables. This
manipulates memory directly.
Since I'm fairly sure that these structs are frequently stored as local
variables for a script, all script variables (both local and global) are
stored as little-endian and accessed through the READ/WRITE functions,
rather than being treated as arrays of 32-bit integers.
On a positive note, the functions for doing endian conversion of resources
and save games have been removed, and some general cleanups have been made
to assist in the rewrite.
Initial reports indicate that this patch indeed fixes alignment issues, and
that I have not - surprisingly - broken the game on big-endian platforms.
At least not in any immediately obvious way. And there's still plenty of
time to fix regressions before 0.9.0, too.
svn-id: r19366
2005-10-29 21:24:54 +00:00
switch ( fetchType ( _resList [ res ] . ptr ) ) {
2003-11-23 13:40:24 +00:00
case ANIMATION_FILE :
2004-09-12 17:10:59 +00:00
tag = " anim " ;
2003-11-23 13:40:24 +00:00
break ;
case SCREEN_FILE :
2004-09-12 17:10:59 +00:00
tag = " layer " ;
2003-11-23 13:40:24 +00:00
break ;
case GAME_OBJECT :
2004-09-12 17:10:59 +00:00
tag = " object " ;
2003-11-23 13:40:24 +00:00
break ;
case WALK_GRID_FILE :
2004-09-12 17:10:59 +00:00
tag = " walkgrid " ;
2003-11-23 13:40:24 +00:00
break ;
case GLOBAL_VAR_FILE :
2004-09-12 17:10:59 +00:00
tag = " globals " ;
2003-11-23 13:40:24 +00:00
break ;
case PARALLAX_FILE_null :
2004-09-12 17:10:59 +00:00
tag = " parallax " ; // Not used!
2003-11-23 13:40:24 +00:00
break ;
case RUN_LIST :
2004-09-12 17:10:59 +00:00
tag = " runlist " ;
2003-11-23 13:40:24 +00:00
break ;
case TEXT_FILE :
2004-09-12 17:10:59 +00:00
tag = " text " ;
2003-11-23 13:40:24 +00:00
break ;
case SCREEN_MANAGER :
2004-09-12 17:10:59 +00:00
tag = " screen " ;
2003-11-23 13:40:24 +00:00
break ;
case MOUSE_FILE :
2004-09-12 17:10:59 +00:00
tag = " mouse " ;
2003-11-23 13:40:24 +00:00
break ;
2003-11-27 07:34:19 +00:00
case WAV_FILE :
2004-09-12 17:10:59 +00:00
tag = " wav " ;
2003-11-27 07:34:19 +00:00
break ;
2003-11-23 13:40:24 +00:00
case ICON_FILE :
2004-09-12 17:10:59 +00:00
tag = " icon " ;
2003-11-23 13:40:24 +00:00
break ;
2003-11-27 07:34:19 +00:00
case PALETTE_FILE :
2004-09-12 17:10:59 +00:00
tag = " palette " ;
2003-11-27 07:34:19 +00:00
break ;
2003-11-23 13:40:24 +00:00
default :
2004-09-12 17:10:59 +00:00
tag = " unknown " ;
2003-11-23 13:40:24 +00:00
break ;
}
2003-11-27 07:34:19 +00:00
sprintf ( buf , " dumps/%s-%d.dmp " , tag , res ) ;
2003-11-23 13:40:24 +00:00
2006-04-26 08:34:01 +00:00
if ( ! Common : : File : : exists ( buf ) ) {
2008-07-29 16:09:10 +00:00
Common : : DumpFile out ;
if ( out . open ( buf ) )
2004-04-23 07:02:11 +00:00
out . write ( _resList [ res ] . ptr , len ) ;
2003-11-23 13:40:24 +00:00
}
}
2003-11-03 07:47:42 +00:00
// close the cluster
2005-02-21 02:29:18 +00:00
file - > close ( ) ;
delete file ;
_usedMem + = len ;
checkMemUsage ( ) ;
} else if ( _resList [ res ] . refCount = = 0 )
2005-02-25 14:19:30 +00:00
removeFromCacheList ( _resList + res ) ;
2003-09-12 20:48:28 +00:00
2004-04-23 07:02:11 +00:00
_resList [ res ] . refCount + + ;
return _resList [ res ] . ptr ;
}
2003-09-12 20:48:28 +00:00
2004-04-23 07:02:11 +00:00
void ResourceManager : : closeResource ( uint32 res ) {
assert ( res < _totalResFiles ) ;
2005-10-17 06:45:54 +00:00
// Don't try to close the resource if it has already been forcibly
// closed, e.g. by fnResetGlobals().
if ( _resList [ res ] . ptr = = NULL )
return ;
2004-04-23 07:02:11 +00:00
assert ( _resList [ res ] . refCount > 0 ) ;
2003-09-12 20:48:28 +00:00
2004-04-23 07:02:11 +00:00
_resList [ res ] . refCount - - ;
2005-02-21 02:29:18 +00:00
if ( _resList [ res ] . refCount = = 0 )
addToCacheList ( _resList + res ) ;
2003-09-12 20:48:28 +00:00
2004-09-12 17:10:59 +00:00
// It's tempting to free the resource immediately when refCount
// reaches zero, but that'd be a mistake. Closing a resource does not
// mean "I'm not going to use this resource any more". It means that
// "the next time I use this resource I'm going to ask for a new
// pointer to it".
//
// Since the original memory manager had to deal with memory
// fragmentation, keeping a resource open - and thus locked down to a
// specific memory address - was considered a bad thing.
2004-04-23 07:02:11 +00:00
}
2003-07-28 01:44:38 +00:00
2005-02-21 02:29:18 +00:00
void ResourceManager : : removeFromCacheList ( Resource * res ) {
if ( _cacheStart = = res )
_cacheStart = res - > next ;
if ( _cacheEnd = = res )
_cacheEnd = res - > prev ;
if ( res - > prev )
res - > prev - > next = res - > next ;
if ( res - > next )
res - > next - > prev = res - > prev ;
res - > prev = res - > next = NULL ;
}
void ResourceManager : : addToCacheList ( Resource * res ) {
res - > prev = NULL ;
res - > next = _cacheStart ;
if ( _cacheStart )
_cacheStart - > prev = res ;
_cacheStart = res ;
if ( ! _cacheEnd )
_cacheEnd = res ;
}
2005-05-10 22:56:25 +00:00
Common : : File * ResourceManager : : openCluFile ( uint16 fileNum ) {
Common : : File * file = new Common : : File ;
2005-02-21 02:29:18 +00:00
while ( ! file - > open ( _resFiles [ fileNum ] . fileName ) ) {
2005-05-08 13:05:31 +00:00
// HACK: We have to check for this, or it'll be impossible to
// quit while the game is asking for the user to insert a CD.
// But recovering from this situation gracefully is just too
// much trouble, so quit now.
2008-09-30 12:27:38 +00:00
if ( _vm - > shouldQuit ( ) )
2005-05-08 13:05:31 +00:00
g_system - > quit ( ) ;
2005-02-21 02:29:18 +00:00
// If the file is supposed to be on hard disk, or we're
// playing a demo, then we're in trouble if the file
// can't be found!
2006-01-01 16:29:23 +00:00
if ( ( _vm - > _features & GF_DEMO ) | | _resFiles [ fileNum ] . cd = = 0 )
2005-02-21 02:29:18 +00:00
error ( " Could not find '%s' " , _resFiles [ fileNum ] . fileName ) ;
2006-01-01 16:29:23 +00:00
askForCD ( _resFiles [ fileNum ] . cd ) ;
2005-02-21 02:29:18 +00:00
}
return file ;
}
2005-05-10 22:56:25 +00:00
void ResourceManager : : readCluIndex ( uint16 fileNum , Common : : File * file ) {
2007-03-01 17:15:26 +00:00
// we didn't read from this file before, get its index table
assert ( _resFiles [ fileNum ] . entryTab = = NULL ) ;
assert ( file ) ;
// 1st DWORD of a cluster is an offset to the look-up table
uint32 table_offset = file - > readUint32LE ( ) ;
debug ( 6 , " table offset = %d " , table_offset ) ;
uint32 tableSize = file - > size ( ) - table_offset ; // the table is stored at the end of the file
file - > seek ( table_offset ) ;
assert ( ( tableSize % 8 ) = = 0 ) ;
_resFiles [ fileNum ] . entryTab = ( uint32 * ) malloc ( tableSize ) ;
_resFiles [ fileNum ] . numEntries = tableSize / 8 ;
file - > read ( _resFiles [ fileNum ] . entryTab , tableSize ) ;
if ( file - > ioFailed ( ) )
error ( " unable to read index table from file %s \n " , _resFiles [ fileNum ] . fileName ) ;
2005-02-21 02:29:18 +00:00
# ifdef SCUMM_BIG_ENDIAN
2007-03-01 17:15:26 +00:00
for ( int tabCnt = 0 ; tabCnt < _resFiles [ fileNum ] . numEntries * 2 ; tabCnt + + )
_resFiles [ fileNum ] . entryTab [ tabCnt ] = FROM_LE_32 ( _resFiles [ fileNum ] . entryTab [ tabCnt ] ) ;
2005-02-21 02:29:18 +00:00
# endif
}
2004-04-23 07:02:11 +00:00
/**
* Returns true if resource is valid , otherwise false .
*/
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
bool ResourceManager : : checkValid ( uint32 res ) {
// Resource number out of range
2003-09-30 09:27:27 +00:00
if ( res > = _totalResFiles )
2004-04-23 07:02:11 +00:00
return false ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
// Points to the number of the ascii filename
uint16 parent_res_file = _resConvTable [ res * 2 ] ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
// Null & void resource
2003-09-02 09:55:11 +00:00
if ( parent_res_file = = 0xffff )
2004-04-23 07:02:11 +00:00
return false ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
return true ;
2003-07-28 01:44:38 +00:00
}
2009-04-07 19:52:46 +00:00
/**
* Fetch resource type
*/
uint8 ResourceManager : : fetchType ( byte * ptr ) {
if ( ! Sword2Engine : : isPsx ( ) ) {
return ptr [ 0 ] ;
} else { // in PSX version, some files got a "garbled" resource header, with type stored in ninth byte
if ( ptr [ 0 ] ) {
return ptr [ 0 ] ;
} else if ( ptr [ 8 ] ) {
return ptr [ 8 ] ;
} else { // In PSX version there is no resource header for audio files,
return WAV_FILE ; // but hopefully all audio files got first 16 bytes zeroed,
} // Allowing us to check for this condition.
2009-04-18 08:37:22 +00:00
// Alas, this doesn't work with PSX DEMO audio files.
2009-04-07 19:52:46 +00:00
}
}
2004-04-23 07:02:11 +00:00
/**
* Returns the total file length of a resource - i . e . all headers are included
* too .
*/
2003-07-28 01:44:38 +00:00
2003-10-04 01:09:29 +00:00
uint32 ResourceManager : : fetchLen ( uint32 res ) {
2004-04-23 07:02:11 +00:00
if ( _resList [ res ] . ptr )
return _resList [ res ] . size ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
// Does this ever happen?
warning ( " fetchLen: Resource %u is not loaded; reading length from file " , res ) ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
// Points to the number of the ascii filename
uint16 parent_res_file = _resConvTable [ res * 2 ] ;
2003-07-28 01:44:38 +00:00
2003-09-02 09:55:11 +00:00
// relative resource within the file
2004-04-23 07:02:11 +00:00
uint16 actual_res = _resConvTable [ ( res * 2 ) + 1 ] ;
2003-07-28 01:44:38 +00:00
2003-09-30 09:27:27 +00:00
// first we have to find the file via the _resConvTable
2003-08-23 14:33:57 +00:00
// open the cluster file
2003-07-28 01:44:38 +00:00
2005-02-21 02:29:18 +00:00
if ( _resFiles [ parent_res_file ] . entryTab = = NULL ) {
2007-03-01 17:13:23 +00:00
Common : : File * file = openCluFile ( parent_res_file ) ;
readCluIndex ( parent_res_file , file ) ;
delete file ;
2005-02-21 02:29:18 +00:00
}
return _resFiles [ parent_res_file ] . entryTab [ actual_res * 2 + 1 ] ;
2003-09-02 09:55:11 +00:00
}
2003-07-28 01:44:38 +00:00
2005-02-22 07:37:50 +00:00
void ResourceManager : : checkMemUsage ( ) {
2005-02-21 02:29:18 +00:00
while ( _usedMem > MAX_MEM_CACHE ) {
// we're using up more memory than we wanted to. free some old stuff.
// Newly loaded objects are added to the start of the list,
// we start freeing from the end, to free the oldest items first
if ( _cacheEnd ) {
Resource * tmp = _cacheEnd ;
assert ( ( tmp - > refCount = = 0 ) & & ( tmp - > ptr ) & & ( tmp - > next = = NULL ) ) ;
removeFromCacheList ( tmp ) ;
_vm - > _memory - > memFree ( tmp - > ptr ) ;
tmp - > ptr = NULL ;
_usedMem - = tmp - > size ;
} else {
2006-03-08 11:37:25 +00:00
warning ( " %d bytes of memory used, but cache list is empty! \n " , _usedMem ) ;
2005-02-21 02:29:18 +00:00
return ;
2003-07-28 01:44:38 +00:00
}
2003-09-02 09:55:11 +00:00
}
2003-07-28 01:44:38 +00:00
}
2004-04-23 07:02:11 +00:00
void ResourceManager : : remove ( int res ) {
if ( _resList [ res ] . ptr ) {
2005-02-21 02:29:18 +00:00
removeFromCacheList ( _resList + res ) ;
2004-04-23 07:02:11 +00:00
_vm - > _memory - > memFree ( _resList [ res ] . ptr ) ;
_resList [ res ] . ptr = NULL ;
2004-04-23 11:36:06 +00:00
_resList [ res ] . refCount = 0 ;
2005-02-21 02:29:18 +00:00
_usedMem - = _resList [ res ] . size ;
2004-04-23 07:02:11 +00:00
}
}
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
/**
* Remove all res files from memory - ready for a total restart . This includes
* the player object and global variables resource .
*/
2003-07-28 01:44:38 +00:00
2005-02-22 07:37:50 +00:00
void ResourceManager : : removeAll ( ) {
2004-05-09 13:20:12 +00:00
// We need to clear the FX queue, because otherwise the sound system
2007-01-26 18:21:41 +00:00
// will still believe that the sound resources are in memory. We also
2008-01-28 00:14:17 +00:00
// need to kill the movie lead-in/out.
2004-05-09 13:20:12 +00:00
2007-01-27 22:10:50 +00:00
_vm - > _sound - > clearFxQueue ( true ) ;
2004-05-09 13:20:12 +00:00
2004-04-23 07:02:11 +00:00
for ( uint i = 0 ; i < _totalResFiles ; i + + )
remove ( i ) ;
2003-07-28 01:44:38 +00:00
}
2003-09-02 09:55:11 +00:00
2004-04-23 07:02:11 +00:00
/**
* Remove all resources from memory .
*/
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
void ResourceManager : : killAll ( bool wantInfo ) {
int nuked = 0 ;
2003-07-28 01:44:38 +00:00
2004-05-01 10:42:23 +00:00
// We need to clear the FX queue, because otherwise the sound system
2007-01-26 18:21:41 +00:00
// will still believe that the sound resources are in memory. We also
// need to kill the movie lead-in/out.
2004-05-01 10:42:23 +00:00
2007-01-26 18:21:41 +00:00
_vm - > _sound - > clearFxQueue ( true ) ;
2004-05-01 10:42:23 +00:00
2004-04-23 07:02:11 +00:00
for ( uint i = 0 ; i < _totalResFiles ; i + + ) {
// Don't nuke the global variables or the player object!
if ( i = = 1 | | i = = CUR_PLAYER_ID )
continue ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
if ( _resList [ i ] . ptr ) {
if ( wantInfo )
Applied my own patch #1341495, in an attempt to fix alignment issues
reported by Crilith.
To elaborate a bit, the engine no longer accesses resource data through
packed structs. Instead it uses memory streams and the READ/WRITE
functions.
If data is mainly read, not written, I have replaced the old struct with a
new one with a read() function to read the whole thing from memory into the
struct's variables, and a write() function to dump the struct's variables
to memory. In fact, most of these write() functions remain unused.
If data is both read and written, I have replaced the struct with a class
with individual get/set functions to replace the old variables. This
manipulates memory directly.
Since I'm fairly sure that these structs are frequently stored as local
variables for a script, all script variables (both local and global) are
stored as little-endian and accessed through the READ/WRITE functions,
rather than being treated as arrays of 32-bit integers.
On a positive note, the functions for doing endian conversion of resources
and save games have been removed, and some general cleanups have been made
to assist in the rewrite.
Initial reports indicate that this patch indeed fixes alignment issues, and
that I have not - surprisingly - broken the game on big-endian platforms.
At least not in any immediately obvious way. And there's still plenty of
time to fix regressions before 0.9.0, too.
svn-id: r19366
2005-10-29 21:24:54 +00:00
Debug_Printf ( " Nuked %5d: %s \n " , i , fetchName ( _resList [ i ] . ptr ) ) ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
remove ( i ) ;
nuked + + ;
2003-07-28 01:44:38 +00:00
}
}
2004-04-23 07:02:11 +00:00
if ( wantInfo )
Debug_Printf ( " Expelled %d resources \n " , nuked ) ;
2003-07-28 01:44:38 +00:00
}
2003-09-02 09:55:11 +00:00
2004-04-23 07:02:11 +00:00
/**
* Like killAll but only kills objects ( except George & the variable table of
* course ) - ie . forcing them to reload & restart their scripts , which
* simulates the effect of a save & restore , thus checking that each object ' s
* re - entrant logic works correctly , and doesn ' t cause a statuette to
* disappear forever , or some plaster - filled holes in sand to crash the game &
* get James in trouble again .
*/
2003-09-02 09:55:11 +00:00
2003-10-26 15:42:49 +00:00
void ResourceManager : : killAllObjects ( bool wantInfo ) {
2004-04-23 07:02:11 +00:00
int nuked = 0 ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
for ( uint i = 0 ; i < _totalResFiles ; i + + ) {
// Don't nuke the global variables or the player object!
if ( i = = 1 | | i = = CUR_PLAYER_ID )
continue ;
2003-07-28 01:44:38 +00:00
2004-04-23 07:02:11 +00:00
if ( _resList [ i ] . ptr ) {
Applied my own patch #1341495, in an attempt to fix alignment issues
reported by Crilith.
To elaborate a bit, the engine no longer accesses resource data through
packed structs. Instead it uses memory streams and the READ/WRITE
functions.
If data is mainly read, not written, I have replaced the old struct with a
new one with a read() function to read the whole thing from memory into the
struct's variables, and a write() function to dump the struct's variables
to memory. In fact, most of these write() functions remain unused.
If data is both read and written, I have replaced the struct with a class
with individual get/set functions to replace the old variables. This
manipulates memory directly.
Since I'm fairly sure that these structs are frequently stored as local
variables for a script, all script variables (both local and global) are
stored as little-endian and accessed through the READ/WRITE functions,
rather than being treated as arrays of 32-bit integers.
On a positive note, the functions for doing endian conversion of resources
and save games have been removed, and some general cleanups have been made
to assist in the rewrite.
Initial reports indicate that this patch indeed fixes alignment issues, and
that I have not - surprisingly - broken the game on big-endian platforms.
At least not in any immediately obvious way. And there's still plenty of
time to fix regressions before 0.9.0, too.
svn-id: r19366
2005-10-29 21:24:54 +00:00
if ( fetchType ( _resList [ i ] . ptr ) = = GAME_OBJECT ) {
2004-04-23 07:02:11 +00:00
if ( wantInfo )
Applied my own patch #1341495, in an attempt to fix alignment issues
reported by Crilith.
To elaborate a bit, the engine no longer accesses resource data through
packed structs. Instead it uses memory streams and the READ/WRITE
functions.
If data is mainly read, not written, I have replaced the old struct with a
new one with a read() function to read the whole thing from memory into the
struct's variables, and a write() function to dump the struct's variables
to memory. In fact, most of these write() functions remain unused.
If data is both read and written, I have replaced the struct with a class
with individual get/set functions to replace the old variables. This
manipulates memory directly.
Since I'm fairly sure that these structs are frequently stored as local
variables for a script, all script variables (both local and global) are
stored as little-endian and accessed through the READ/WRITE functions,
rather than being treated as arrays of 32-bit integers.
On a positive note, the functions for doing endian conversion of resources
and save games have been removed, and some general cleanups have been made
to assist in the rewrite.
Initial reports indicate that this patch indeed fixes alignment issues, and
that I have not - surprisingly - broken the game on big-endian platforms.
At least not in any immediately obvious way. And there's still plenty of
time to fix regressions before 0.9.0, too.
svn-id: r19366
2005-10-29 21:24:54 +00:00
Debug_Printf ( " Nuked %5d: %s \n " , i , fetchName ( _resList [ i ] . ptr ) ) ;
2004-04-23 07:02:11 +00:00
remove ( i ) ;
nuked + + ;
2003-07-28 01:44:38 +00:00
}
}
2004-04-23 07:02:11 +00:00
}
2003-07-28 01:44:38 +00:00
2003-10-26 15:42:49 +00:00
if ( wantInfo )
2004-04-23 07:02:11 +00:00
Debug_Printf ( " Expelled %d resources \n " , nuked ) ;
2003-07-28 01:44:38 +00:00
}
2005-12-21 10:57:48 +00:00
void ResourceManager : : askForCD ( int cd ) {
2004-04-23 07:02:11 +00:00
byte * textRes ;
2003-09-02 09:55:11 +00:00
2005-02-08 08:32:50 +00:00
// Stop any music from playing - so the system no longer needs the
2003-09-02 09:55:11 +00:00
// current CD - otherwise when we take out the CD, Windows will
// complain!
2003-07-28 01:44:38 +00:00
2005-02-08 08:32:50 +00:00
_vm - > _sound - > stopMusic ( true ) ;
2003-07-28 01:44:38 +00:00
2003-11-03 07:47:42 +00:00
textRes = openResource ( 2283 ) ;
2005-02-19 14:02:16 +00:00
_vm - > _screen - > displayMsg ( _vm - > fetchTextLine ( textRes , 5 + cd ) + 2 , 0 ) ;
2003-11-03 07:47:42 +00:00
closeResource ( 2283 ) ;
2003-07-28 01:44:38 +00:00
Re-enabled the CD swapping code, after rewriting it a bit.
If a cluster file isn't found the resource manager will first check if it's
one of the files that it expects to find on the hard disk. If so, it's
considered a fatal error.
Otherwise it will present the user with an "Insert CD1" or "Insert CD2"
message, just like the original did. Unlike the original, the user will
have to press a button or click the mouse to indicate when he's done. I
don't know if we even can detect the CD automatically in any portable way.
As far as I can see, we'll need at least two separate path settings for
this to actually work: one for the HD install directory, and one or two for
the CDs. The file that are supposed to be found on the HD are only on one
of the CDs, so the amount of CD swapping would probably be unbearable
otherwise.
As a consequence, I haven't actually tried running the game from CD yet.
By the way, the old caching code has been removed completely now. All it
did was to copy the cluster file to HD for faster access. ScummVM never did
that, but so far no one has complained.
svn-id: r11273
2003-11-13 07:59:52 +00:00
// The original code probably determined automagically when the correct
// CD had been inserted, but our backend doesn't support that, and
// anyway I don't know if all systems allow that sort of thing. So we
// wait for the user to press any key instead, or click the mouse.
2003-11-14 07:41:22 +00:00
//
// But just in case we ever try to identify the CDs by their labels,
// they should be:
//
// CD1: "RBSII1" (or "PCF76" for the PCF76 version, whatever that is)
// CD2: "RBSII2"
2003-07-28 01:44:38 +00:00
}
2003-10-04 00:52:27 +00:00
} // End of namespace Sword2