2009-02-17 15:02:16 +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.
|
|
|
|
*
|
|
|
|
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 22:50:35 +00:00
|
|
|
#include "common/stream.h"
|
|
|
|
#include "common/system.h"
|
2009-04-27 12:31:27 +00:00
|
|
|
#include "common/func.h"
|
2009-03-15 20:31:29 +00:00
|
|
|
#include "common/serializer.h"
|
|
|
|
|
|
|
|
#include <time.h> // FIXME: For struct tm
|
|
|
|
|
|
|
|
|
2009-05-13 16:52:41 +00:00
|
|
|
#include "sci/sci.h"
|
2009-02-24 20:39:34 +00:00
|
|
|
#include "sci/gfx/operations.h"
|
2009-02-21 22:50:35 +00:00
|
|
|
#include "sci/gfx/menubar.h"
|
2009-04-24 10:46:20 +00:00
|
|
|
#include "sci/gfx/gfx_state_internal.h" // required for GfxPort, GfxContainer
|
2009-03-01 06:17:52 +00:00
|
|
|
#include "sci/sfx/core.h"
|
2009-03-06 18:11:12 +00:00
|
|
|
#include "sci/sfx/iterator.h"
|
2009-02-27 02:23:40 +00:00
|
|
|
#include "sci/engine/state.h"
|
2009-02-24 23:16:42 +00:00
|
|
|
#include "sci/engine/intmap.h"
|
2009-03-12 03:26:21 +00:00
|
|
|
#include "sci/engine/savegame.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// from ksound.cpp:
|
2009-05-12 23:32:32 +00:00
|
|
|
SongIterator *build_iterator(EngineState *s, int song_nr, SongIteratorType type, songit_id_t id);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
#pragma mark -
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// TODO: Many of the following sync_*() methods should be turned into member funcs
|
|
|
|
// of the classes they are syncing.
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_MemObjPtr(Common::Serializer &s, MemObject *&obj);
|
|
|
|
static void sync_songlib_t(Common::Serializer &s, songlib_t &obj);
|
2009-02-20 20:31:08 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_reg_t(Common::Serializer &s, reg_t &obj) {
|
|
|
|
s.syncAsUint16LE(obj.segment);
|
|
|
|
s.syncAsUint16LE(obj.offset);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: Sync a C string, using malloc/free storage.
|
|
|
|
// Much better to replace all of these by Common::String
|
|
|
|
static void syncCStr(Common::Serializer &s, char **str) {
|
|
|
|
Common::String tmp;
|
|
|
|
if (s.isSaving() && *str)
|
|
|
|
tmp = *str;
|
|
|
|
s.syncString(tmp);
|
|
|
|
if (s.isLoading()) {
|
|
|
|
//free(*str);
|
|
|
|
*str = strdup(tmp.c_str());
|
2009-02-21 11:04:47 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_song_t(Common::Serializer &s, song_t &obj) {
|
|
|
|
s.syncAsSint32LE(obj.handle);
|
|
|
|
s.syncAsSint32LE(obj.resource_num);
|
|
|
|
s.syncAsSint32LE(obj.priority);
|
|
|
|
s.syncAsSint32LE(obj.status);
|
|
|
|
s.syncAsSint32LE(obj.restore_behavior);
|
|
|
|
s.syncAsSint32LE(obj.restore_time);
|
|
|
|
s.syncAsSint32LE(obj.loops);
|
|
|
|
s.syncAsSint32LE(obj.hold);
|
|
|
|
|
|
|
|
if (s.isLoading()) {
|
|
|
|
obj._delay = 0;
|
|
|
|
obj.it = 0;
|
|
|
|
obj.next_playing = 0;
|
|
|
|
obj.next_stopping = 0;
|
|
|
|
obj.next = 0;
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-24 11:31:16 +00:00
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
// Experimental hack: Use syncWithSerializer to sync. By default, this assume
|
|
|
|
// the object to be synced is a subclass of Serializable and thus tries to invoke
|
|
|
|
// the saveLoadWithSerializer() method. But it is possible to specialize this
|
|
|
|
// template function to handle stuff that is not implementing that interface.
|
|
|
|
template<typename T>
|
|
|
|
void syncWithSerializer(Common::Serializer &s, T &obj) {
|
|
|
|
obj.saveLoadWithSerializer(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
// By default, sync using syncWithSerializer, which in turn can easily be overloaded.
|
|
|
|
template <typename T>
|
|
|
|
struct DefaultSyncer : Common::BinaryFunction<Common::Serializer, T, void> {
|
|
|
|
void operator()(Common::Serializer &s, T &obj) const {
|
|
|
|
//obj.saveLoadWithSerializer(s);
|
|
|
|
syncWithSerializer(s, obj);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-03-24 11:31:16 +00:00
|
|
|
/**
|
|
|
|
* Sync a Common::Array using a Common::Serializer.
|
|
|
|
* When saving, this writes the length of the array, then syncs (writes) all entries.
|
|
|
|
* When loading, it loads the length of the array, then resizes it accordingly, before
|
|
|
|
* syncing all entries.
|
|
|
|
*
|
2009-05-04 15:05:11 +00:00
|
|
|
* Note: This shouldn't be in common/array.h nor in common/serializer.h, after
|
|
|
|
* all, not all code using arrays wants to use the serializer, and vice versa.
|
|
|
|
* But we could put this into a separate header file in common/ at some point.
|
|
|
|
* Something like common/serializer-extras.h or so.
|
2009-03-24 11:31:16 +00:00
|
|
|
*
|
|
|
|
* TODO: Add something like this for lists, queues....
|
|
|
|
*/
|
2009-05-04 15:05:11 +00:00
|
|
|
template <typename T, class Syncer = DefaultSyncer<T> >
|
|
|
|
struct ArraySyncer : Common::BinaryFunction<Common::Serializer, T, void> {
|
|
|
|
void operator()(Common::Serializer &s, Common::Array<T> &arr) const {
|
|
|
|
uint len = arr.size();
|
|
|
|
s.syncAsUint32LE(len);
|
|
|
|
Syncer sync;
|
|
|
|
|
|
|
|
// Resize the array if loading.
|
|
|
|
if (s.isLoading())
|
|
|
|
arr.resize(len);
|
|
|
|
|
|
|
|
typename Common::Array<T>::iterator i;
|
|
|
|
for (i = arr.begin(); i != arr.end(); ++i) {
|
|
|
|
sync(s, *i);
|
|
|
|
}
|
2009-03-24 11:31:16 +00:00
|
|
|
}
|
2009-05-04 15:05:11 +00:00
|
|
|
};
|
2009-02-28 11:07:36 +00:00
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
// Convenience wrapper
|
2009-04-27 12:31:27 +00:00
|
|
|
template<typename T>
|
2009-05-04 15:05:11 +00:00
|
|
|
void syncArray(Common::Serializer &s, Common::Array<T> &arr) {
|
|
|
|
ArraySyncer<T> sync;
|
|
|
|
sync(s, arr);
|
2009-04-27 12:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-12 23:30:42 +00:00
|
|
|
template <>
|
|
|
|
void syncWithSerializer(Common::Serializer &s, reg_t &obj) {
|
|
|
|
sync_reg_t(s, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
|
2009-03-24 11:31:16 +00:00
|
|
|
void MenuItem::saveLoadWithSerializer(Common::Serializer &s) {
|
|
|
|
s.syncAsSint32LE(_type);
|
|
|
|
s.syncString(_keytext);
|
|
|
|
s.skip(4); // Used to be keytext_size (an already unused field)
|
|
|
|
|
|
|
|
s.syncAsSint32LE(_flags);
|
|
|
|
s.syncBytes(_said, MENU_SAID_SPEC_SIZE);
|
|
|
|
sync_reg_t(s, _saidPos);
|
|
|
|
s.syncString(_text);
|
|
|
|
sync_reg_t(s, _textPos);
|
|
|
|
s.syncAsSint32LE(_modifiers);
|
|
|
|
s.syncAsSint32LE(_key);
|
|
|
|
s.syncAsSint32LE(_enabled);
|
|
|
|
s.syncAsSint32LE(_tag);
|
|
|
|
}
|
2009-03-15 20:31:29 +00:00
|
|
|
|
2009-03-24 11:31:16 +00:00
|
|
|
void Menu::saveLoadWithSerializer(Common::Serializer &s) {
|
|
|
|
s.syncString(_title);
|
|
|
|
s.syncAsSint32LE(_titleWidth);
|
|
|
|
s.syncAsSint32LE(_width);
|
2009-03-15 20:31:29 +00:00
|
|
|
|
2009-03-24 11:31:16 +00:00
|
|
|
syncArray<MenuItem>(s, _items);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-20 20:31:08 +00:00
|
|
|
|
2009-03-24 11:31:16 +00:00
|
|
|
void Menubar::saveLoadWithSerializer(Common::Serializer &s) {
|
|
|
|
syncArray<Menu>(s, _menus);
|
2009-03-15 20:31:29 +00:00
|
|
|
}
|
2009-02-20 20:31:08 +00:00
|
|
|
|
2009-05-03 09:25:15 +00:00
|
|
|
void SegManager::saveLoadWithSerializer(Common::Serializer &s) {
|
|
|
|
s.syncAsSint32LE(reserved_id);
|
|
|
|
s.syncAsSint32LE(exports_wide);
|
|
|
|
s.syncAsSint32LE(gc_mark_bits);
|
|
|
|
|
|
|
|
id_seg_map->saveLoadWithSerializer(s);
|
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
uint sync_heap_size = _heap.size();
|
|
|
|
s.syncAsUint32LE(sync_heap_size);
|
2009-05-03 22:46:11 +00:00
|
|
|
_heap.resize(sync_heap_size);
|
|
|
|
for (uint i = 0; i < sync_heap_size; ++i)
|
|
|
|
sync_MemObjPtr(s, _heap[i]);
|
2009-05-03 09:25:15 +00:00
|
|
|
|
|
|
|
s.syncAsSint32LE(Clones_seg_id);
|
|
|
|
s.syncAsSint32LE(Lists_seg_id);
|
|
|
|
s.syncAsSint32LE(Nodes_seg_id);
|
2009-02-28 11:12:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_SegManagerPtr(Common::Serializer &s, SegManager *&obj) {
|
|
|
|
bool sci11 = false;
|
|
|
|
|
|
|
|
if (s.isSaving()) {
|
|
|
|
assert(obj);
|
|
|
|
sci11 = obj->isSci1_1;
|
|
|
|
}
|
|
|
|
|
|
|
|
s.syncAsByte(sci11);
|
|
|
|
|
|
|
|
if (s.isLoading()) {
|
|
|
|
// FIXME: Do in-place loading at some point, instead of creating a new EngineState instance from scratch.
|
|
|
|
delete obj;
|
|
|
|
obj = new SegManager(sci11);
|
2009-02-28 11:12:59 +00:00
|
|
|
}
|
2009-03-15 20:31:29 +00:00
|
|
|
|
2009-05-03 09:25:15 +00:00
|
|
|
obj->saveLoadWithSerializer(s);
|
2009-02-28 11:12:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
template <>
|
|
|
|
void syncWithSerializer(Common::Serializer &s, Class &obj) {
|
2009-03-15 20:31:29 +00:00
|
|
|
s.syncAsSint32LE(obj.script);
|
|
|
|
sync_reg_t(s, obj.reg);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_sfx_state_t(Common::Serializer &s, sfx_state_t &obj) {
|
|
|
|
sync_songlib_t(s, obj.songlib);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_SavegameMetadata(Common::Serializer &s, SavegameMetadata &obj) {
|
|
|
|
// TODO: It would be a good idea to store a magic number & a header size here,
|
|
|
|
// so that we can implement backward compatibility if the savegame format changes.
|
2009-02-20 20:31:08 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
s.syncString(obj.savegame_name);
|
|
|
|
s.syncAsSint32LE(obj.savegame_version);
|
|
|
|
s.syncString(obj.game_version);
|
|
|
|
s.syncAsSint32LE(obj.version);
|
|
|
|
s.syncAsSint32LE(obj.savegame_date);
|
|
|
|
s.syncAsSint32LE(obj.savegame_time);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
void EngineState::saveLoadWithSerializer(Common::Serializer &s) {
|
|
|
|
s.syncAsSint32LE(savegame_version);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
syncCStr(s, &game_version);
|
|
|
|
s.syncAsSint32LE(version);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: Do in-place loading at some point, instead of creating a new EngineState instance from scratch.
|
|
|
|
if (s.isLoading()) {
|
|
|
|
//free(menubar);
|
2009-03-24 11:31:16 +00:00
|
|
|
_menubar = new Menubar();
|
2009-03-15 20:31:29 +00:00
|
|
|
} else
|
2009-03-24 11:31:16 +00:00
|
|
|
assert(_menubar);
|
|
|
|
_menubar->saveLoadWithSerializer(s);
|
2009-02-15 22:28:12 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
s.syncAsSint32LE(status_bar_foreground);
|
|
|
|
s.syncAsSint32LE(status_bar_background);
|
|
|
|
|
|
|
|
sync_SegManagerPtr(s, seg_manager);
|
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
syncArray<Class>(s, _classtable);
|
2009-03-15 20:31:29 +00:00
|
|
|
|
|
|
|
sync_sfx_state_t(s, sound);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_LocalVariables(Common::Serializer &s, LocalVariables &obj) {
|
|
|
|
s.syncAsSint32LE(obj.script_id);
|
2009-05-12 23:30:42 +00:00
|
|
|
syncArray<reg_t>(s, obj._locals);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-10 19:17:51 +00:00
|
|
|
template <>
|
|
|
|
void syncWithSerializer(Common::Serializer &s, Object &obj) {
|
2009-03-15 20:31:29 +00:00
|
|
|
s.syncAsSint32LE(obj.flags);
|
|
|
|
sync_reg_t(s, obj.pos);
|
|
|
|
s.syncAsSint32LE(obj.variable_names_nr);
|
|
|
|
s.syncAsSint32LE(obj.methods_nr);
|
|
|
|
|
2009-05-12 23:30:42 +00:00
|
|
|
syncArray<reg_t>(s, obj._variables);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
template <>
|
2009-05-11 13:32:00 +00:00
|
|
|
void syncWithSerializer(Common::Serializer &s, Table<Clone>::Entry &obj) {
|
|
|
|
s.syncAsSint32LE(obj.next_free);
|
|
|
|
|
|
|
|
syncWithSerializer<Object>(s, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
|
|
|
void syncWithSerializer(Common::Serializer &s, Table<List>::Entry &obj) {
|
|
|
|
s.syncAsSint32LE(obj.next_free);
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
sync_reg_t(s, obj.first);
|
|
|
|
sync_reg_t(s, obj.last);
|
2009-02-24 02:59:50 +00:00
|
|
|
}
|
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
template <>
|
2009-05-11 13:32:00 +00:00
|
|
|
void syncWithSerializer(Common::Serializer &s, Table<Node>::Entry &obj) {
|
|
|
|
s.syncAsSint32LE(obj.next_free);
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
sync_reg_t(s, obj.pred);
|
|
|
|
sync_reg_t(s, obj.succ);
|
|
|
|
sync_reg_t(s, obj.key);
|
|
|
|
sync_reg_t(s, obj.value);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-04 15:05:11 +00:00
|
|
|
template <typename T>
|
2009-05-11 13:32:00 +00:00
|
|
|
void sync_Table(Common::Serializer &s, T &obj) {
|
|
|
|
// TODO: Change this to use syncArray. This involves breaking the savegame format compatibility.
|
2009-03-15 20:31:29 +00:00
|
|
|
s.syncAsSint32LE(obj.first_free);
|
|
|
|
s.syncAsSint32LE(obj.entries_used);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
syncArray<typename T::Entry>(s, obj._table);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_Script(Common::Serializer &s, Script &obj) {
|
|
|
|
s.syncAsSint32LE(obj.nr);
|
|
|
|
s.syncAsUint32LE(obj.buf_size);
|
|
|
|
s.syncAsUint32LE(obj.script_size);
|
|
|
|
s.syncAsUint32LE(obj.heap_size);
|
2009-02-24 02:59:50 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: revamp obj_indices handling
|
|
|
|
if (!obj.obj_indices) {
|
|
|
|
assert(s.isLoading());
|
|
|
|
obj.obj_indices = new IntMapper();
|
2009-02-24 02:59:50 +00:00
|
|
|
}
|
2009-05-03 09:25:15 +00:00
|
|
|
|
|
|
|
obj.obj_indices->saveLoadWithSerializer(s);
|
2009-02-24 02:59:50 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
s.syncAsSint32LE(obj.exports_nr);
|
|
|
|
s.syncAsSint32LE(obj.synonyms_nr);
|
|
|
|
s.syncAsSint32LE(obj.lockers);
|
|
|
|
|
2009-05-10 19:17:51 +00:00
|
|
|
syncArray<Object>(s, obj._objects);
|
2009-03-15 20:31:29 +00:00
|
|
|
|
|
|
|
s.syncAsSint32LE(obj.locals_offset);
|
|
|
|
s.syncAsSint32LE(obj.locals_segment);
|
|
|
|
|
|
|
|
s.syncAsSint32LE(obj.marked_as_deleted);
|
2009-02-24 02:59:50 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_SystemString(Common::Serializer &s, SystemString &obj) {
|
|
|
|
syncCStr(s, &obj.name);
|
|
|
|
s.syncAsSint32LE(obj.max_size);
|
|
|
|
|
|
|
|
// FIXME: This is a *WEIRD* hack: We sync a reg_t* as if it was a string.
|
|
|
|
// No idea why, but this mimicks what the old save/load code used to do.
|
|
|
|
syncCStr(s, (char **)&obj.value);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
static void sync_SystemStrings(Common::Serializer &s, SystemStrings &obj) {
|
|
|
|
for (int i = 0; i < SYS_STRINGS_MAX; ++i)
|
|
|
|
sync_SystemString(s, obj.strings[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sync_DynMem(Common::Serializer &s, DynMem &obj) {
|
2009-05-03 22:47:04 +00:00
|
|
|
s.syncAsSint32LE(obj._size);
|
|
|
|
syncCStr(s, &obj._description);
|
|
|
|
if (!obj._buf && obj._size) {
|
2009-05-12 23:30:10 +00:00
|
|
|
obj._buf = (byte *)calloc(obj._size, 1);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-05-03 22:47:04 +00:00
|
|
|
if (obj._size)
|
|
|
|
s.syncBytes(obj._buf, obj._size);
|
2009-03-15 20:31:29 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
static void sync_DataStack(Common::Serializer &s, DataStack &obj) {
|
|
|
|
s.syncAsUint32LE(obj.nr);
|
|
|
|
if (s.isLoading()) {
|
|
|
|
//free(obj.entries);
|
2009-05-12 23:30:10 +00:00
|
|
|
obj.entries = (reg_t *)calloc(obj.nr, sizeof(reg_t));
|
2009-05-11 13:32:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
#pragma mark -
|
|
|
|
|
|
|
|
static void sync_songlib_t(Common::Serializer &s, songlib_t &obj) {
|
|
|
|
int songcount = 0;
|
|
|
|
if (s.isSaving())
|
|
|
|
songcount = song_lib_count(obj);
|
|
|
|
s.syncAsUint32LE(songcount);
|
|
|
|
|
|
|
|
if (s.isLoading()) {
|
|
|
|
song_lib_init(&obj);
|
|
|
|
while (songcount--) {
|
|
|
|
song_t *newsong = (song_t *)calloc(1, sizeof(song_t));
|
|
|
|
sync_song_t(s, *newsong);
|
|
|
|
song_lib_add(obj, newsong);
|
2009-02-20 20:31:08 +00:00
|
|
|
}
|
2009-03-15 20:31:29 +00:00
|
|
|
} else {
|
|
|
|
song_t *seeker = *(obj.lib);
|
|
|
|
while (seeker) {
|
|
|
|
seeker->restore_time = seeker->it->getTimepos();
|
|
|
|
sync_song_t(s, *seeker);
|
|
|
|
seeker = seeker->next;
|
2009-02-20 20:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
2009-03-15 20:31:29 +00:00
|
|
|
}
|
|
|
|
|
2009-05-03 22:46:38 +00:00
|
|
|
static void sync_MemObjPtr(Common::Serializer &s, MemObject *&mobj) {
|
2009-03-15 20:31:29 +00:00
|
|
|
// Sync the memobj type
|
2009-05-03 22:46:38 +00:00
|
|
|
MemObjectType type = (s.isSaving() && mobj) ? mobj->getType() : MEM_OBJ_INVALID;
|
2009-03-15 20:31:29 +00:00
|
|
|
s.syncAsUint32LE(type);
|
|
|
|
|
2009-05-03 22:46:38 +00:00
|
|
|
// If we were saving and mobj == 0, or if we are loading and this is an
|
2009-03-15 20:31:29 +00:00
|
|
|
// entry marked as empty -> we are done.
|
|
|
|
if (type == MEM_OBJ_INVALID) {
|
2009-05-03 22:46:38 +00:00
|
|
|
mobj = 0;
|
2009-03-15 20:31:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.isLoading()) {
|
2009-05-03 22:46:38 +00:00
|
|
|
//assert(!mobj);
|
|
|
|
mobj = MemObject::createMemObject(type);
|
2009-03-15 20:31:29 +00:00
|
|
|
} else {
|
2009-05-03 22:46:38 +00:00
|
|
|
assert(mobj);
|
2009-02-20 20:31:08 +00:00
|
|
|
}
|
2009-03-15 20:31:29 +00:00
|
|
|
|
2009-05-03 22:46:38 +00:00
|
|
|
s.syncAsSint32LE(mobj->_segmgrId);
|
2009-05-03 09:30:33 +00:00
|
|
|
switch (type) {
|
2009-03-15 20:31:29 +00:00
|
|
|
case MEM_OBJ_SCRIPT:
|
2009-05-03 22:46:38 +00:00
|
|
|
sync_Script(s, *(Script *)mobj);
|
2009-03-15 20:31:15 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
case MEM_OBJ_CLONES:
|
2009-05-04 15:05:11 +00:00
|
|
|
sync_Table<CloneTable>(s, *(CloneTable *)mobj);
|
2009-03-15 20:31:15 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
case MEM_OBJ_LOCALS:
|
2009-05-03 22:46:38 +00:00
|
|
|
sync_LocalVariables(s, *(LocalVariables *)mobj);
|
2009-03-15 20:31:15 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
case MEM_OBJ_SYS_STRINGS:
|
2009-05-03 22:46:38 +00:00
|
|
|
sync_SystemStrings(s, *(SystemStrings *)mobj);
|
2009-03-15 20:31:15 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
case MEM_OBJ_STACK:
|
2009-05-11 13:32:00 +00:00
|
|
|
sync_DataStack(s, *(DataStack *)mobj);
|
2009-03-15 20:31:15 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
case MEM_OBJ_HUNK:
|
2009-03-15 20:31:29 +00:00
|
|
|
if (s.isLoading()) {
|
2009-05-03 22:46:38 +00:00
|
|
|
(*(HunkTable *)mobj).initTable();
|
2009-03-15 20:31:29 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
break;
|
2009-03-15 20:31:15 +00:00
|
|
|
case MEM_OBJ_STRING_FRAG:
|
|
|
|
break;
|
2009-03-15 20:31:29 +00:00
|
|
|
case MEM_OBJ_LISTS:
|
2009-05-04 15:05:11 +00:00
|
|
|
sync_Table<ListTable>(s, *(ListTable *)mobj);
|
2009-03-15 20:31:29 +00:00
|
|
|
break;
|
|
|
|
case MEM_OBJ_NODES:
|
2009-05-04 15:05:11 +00:00
|
|
|
sync_Table<NodeTable>(s, *(NodeTable *)mobj);
|
2009-03-15 20:31:29 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
case MEM_OBJ_DYNMEM:
|
2009-05-03 22:46:38 +00:00
|
|
|
sync_DynMem(s, *(DynMem *)mobj);
|
2009-03-15 20:31:15 +00:00
|
|
|
break;
|
2009-02-21 23:27:24 +00:00
|
|
|
default:
|
2009-05-03 09:30:33 +00:00
|
|
|
error("Unknown MemObject type %d", type);
|
2009-02-21 23:27:24 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-28 11:07:36 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
#pragma mark -
|
2009-02-23 00:14:51 +00:00
|
|
|
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
int gamestate_save(EngineState *s, Common::WriteStream *fh, const char* savename) {
|
2009-02-20 23:41:15 +00:00
|
|
|
tm curTime;
|
|
|
|
g_system->getTimeAndDate(curTime);
|
|
|
|
|
2009-02-28 11:07:36 +00:00
|
|
|
SavegameMetadata meta;
|
2009-03-17 08:03:42 +00:00
|
|
|
meta.savegame_version = CURRENT_SAVEGAME_VERSION;
|
2009-02-28 11:07:36 +00:00
|
|
|
meta.savegame_name = savename;
|
|
|
|
meta.version = s->version;
|
|
|
|
meta.game_version = s->game_version;
|
|
|
|
meta.savegame_date = ((curTime.tm_mday & 0xFF) << 24) | (((curTime.tm_mon + 1) & 0xFF) << 16) | ((curTime.tm_year + 1900) & 0xFFFF);
|
|
|
|
meta.savegame_time = ((curTime.tm_hour & 0xFF) << 16) | (((curTime.tm_min) & 0xFF) << 8) | ((curTime.tm_sec) & 0xFF);
|
2009-02-20 23:41:15 +00:00
|
|
|
|
2009-03-17 08:03:42 +00:00
|
|
|
s->savegame_version = CURRENT_SAVEGAME_VERSION;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
if (s->execution_stack_base) {
|
|
|
|
sciprintf("Cannot save from below kernel function\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2009-02-16 00:35:22 +00:00
|
|
|
/*
|
|
|
|
if (s->sound_server) {
|
|
|
|
if ((s->sound_server->save)(s, dirname)) {
|
|
|
|
sciprintf("Saving failed for the sound subsystem\n");
|
|
|
|
chdir("..");
|
|
|
|
return 1;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-16 00:35:22 +00:00
|
|
|
}
|
|
|
|
*/
|
2009-02-20 19:08:38 +00:00
|
|
|
// Calculate the time spent with this game
|
2009-02-25 21:43:57 +00:00
|
|
|
s->game_time = (g_system->getMillis() - s->game_start_time) / 1000;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
Common::Serializer ser(0, fh);
|
|
|
|
sync_SavegameMetadata(ser, meta);
|
|
|
|
s->saveLoadWithSerializer(ser); // FIXME: Error handling?
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: This should probably be turned into a SegManager method
|
2009-02-28 11:12:59 +00:00
|
|
|
static SegmentId find_unique_seg_by_type(SegManager *self, int type) {
|
2009-05-03 22:46:11 +00:00
|
|
|
for (uint i = 0; i < self->_heap.size(); i++)
|
|
|
|
if (self->_heap[i] &&
|
|
|
|
self->_heap[i]->getType() == type)
|
2009-02-15 06:10:59 +00:00
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
static byte *find_unique_script_block(EngineState *s, byte *buf, int type) {
|
2009-05-14 09:12:27 +00:00
|
|
|
if (s->flags & GF_SCI0_OLD)
|
|
|
|
buf += 2;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
do {
|
2009-03-07 16:14:20 +00:00
|
|
|
int seeker_type = READ_LE_UINT16(buf);
|
2009-02-15 06:10:59 +00:00
|
|
|
int seeker_size;
|
|
|
|
|
|
|
|
if (seeker_type == 0) break;
|
|
|
|
if (seeker_type == type) return buf;
|
|
|
|
|
2009-03-07 16:14:20 +00:00
|
|
|
seeker_size = READ_LE_UINT16(buf + 2);
|
2009-02-15 06:10:59 +00:00
|
|
|
buf += seeker_size;
|
2009-02-16 00:35:22 +00:00
|
|
|
} while(1);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: This should probably be turned into an EngineState method
|
2009-02-21 10:47:56 +00:00
|
|
|
static void reconstruct_stack(EngineState *retval) {
|
2009-02-28 11:12:59 +00:00
|
|
|
SegmentId stack_seg = find_unique_seg_by_type(retval->seg_manager, MEM_OBJ_STACK);
|
2009-05-08 09:53:31 +00:00
|
|
|
DataStack *stack = (DataStack *)(retval->seg_manager->_heap[stack_seg]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
retval->stack_segment = stack_seg;
|
|
|
|
retval->stack_base = stack->entries;
|
|
|
|
retval->stack_top = retval->stack_base + VM_STACK_SIZE;
|
|
|
|
}
|
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
static bool clone_entry_used(CloneTable *table, int n) {
|
2009-02-15 06:10:59 +00:00
|
|
|
int seeker = table->first_free;
|
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
while (seeker != CloneTable::HEAPENTRY_INVALID) {
|
|
|
|
if (seeker == n)
|
|
|
|
return false;
|
|
|
|
seeker = table->_table[seeker].next_free;
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
return true;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-28 11:12:59 +00:00
|
|
|
static void load_script(EngineState *s, SegmentId seg) {
|
2009-02-28 21:59:49 +00:00
|
|
|
Resource *script, *heap = NULL;
|
2009-05-03 22:46:38 +00:00
|
|
|
Script *scr = (Script *)(s->seg_manager->_heap[seg]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
scr->buf = (byte *)malloc(scr->buf_size);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-28 23:46:50 +00:00
|
|
|
script = s->resmgr->findResource(kResourceTypeScript, scr->nr, 0);
|
2009-02-16 00:35:22 +00:00
|
|
|
if (s->version >= SCI_VERSION(1,001,000))
|
2009-02-28 23:46:50 +00:00
|
|
|
heap = s->resmgr->findResource(kResourceTypeHeap, scr->nr, 0);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 23:27:24 +00:00
|
|
|
switch (s->seg_manager->isSci1_1) {
|
2009-02-15 06:10:59 +00:00
|
|
|
case 0 :
|
2009-02-21 23:27:24 +00:00
|
|
|
s->seg_manager->mcpyInOut(0, script->data, script->size, seg, SEG_ID);
|
2009-02-15 06:10:59 +00:00
|
|
|
break;
|
|
|
|
case 1 :
|
2009-02-21 23:27:24 +00:00
|
|
|
s->seg_manager->mcpyInOut(0, script->data, script->size, seg, SEG_ID);
|
|
|
|
s->seg_manager->mcpyInOut(scr->script_size, heap->data, heap->size, seg, SEG_ID);
|
2009-02-15 06:10:59 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: The following should likely become a SegManager method
|
2009-02-21 11:04:47 +00:00
|
|
|
static void reconstruct_scripts(EngineState *s, SegManager *self) {
|
2009-05-10 19:17:51 +00:00
|
|
|
uint i, j;
|
2009-02-28 11:12:59 +00:00
|
|
|
MemObject *mobj;
|
2009-05-03 22:46:11 +00:00
|
|
|
for (i = 0; i < self->_heap.size(); i++) {
|
|
|
|
if (self->_heap[i]) {
|
|
|
|
mobj = self->_heap[i];
|
2009-05-03 09:30:33 +00:00
|
|
|
switch (mobj->getType()) {
|
2009-02-15 22:28:12 +00:00
|
|
|
case MEM_OBJ_SCRIPT: {
|
2009-05-03 22:46:38 +00:00
|
|
|
Script *scr = (Script *)mobj;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
load_script(s, i);
|
2009-05-03 22:46:38 +00:00
|
|
|
scr->locals_block = scr->locals_segment == 0 ? NULL : (LocalVariables *)(s->seg_manager->_heap[scr->locals_segment]);
|
2009-02-21 21:16:41 +00:00
|
|
|
scr->export_table = (uint16 *) find_unique_script_block(s, scr->buf, sci_obj_exports);
|
2009-02-15 06:10:59 +00:00
|
|
|
scr->synonyms = find_unique_script_block(s, scr->buf, sci_obj_synonyms);
|
2009-05-10 20:13:12 +00:00
|
|
|
scr->_codeBlocks.clear();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 23:27:24 +00:00
|
|
|
if (!self->isSci1_1)
|
2009-02-15 06:10:59 +00:00
|
|
|
scr->export_table += 3;
|
2009-02-22 13:11:43 +00:00
|
|
|
|
2009-05-10 19:17:51 +00:00
|
|
|
for (j = 0; j < scr->_objects.size(); j++) {
|
|
|
|
byte *data = scr->buf + scr->_objects[j].pos.offset;
|
|
|
|
scr->_objects[j].base = scr->buf;
|
|
|
|
scr->_objects[j].base_obj = data;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-21 23:27:24 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-21 23:27:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-20 19:08:38 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-03 22:46:11 +00:00
|
|
|
for (i = 0; i < self->_heap.size(); i++) {
|
|
|
|
if (self->_heap[i]) {
|
|
|
|
mobj = self->_heap[i];
|
2009-05-03 09:30:33 +00:00
|
|
|
switch (mobj->getType()) {
|
2009-02-15 22:28:12 +00:00
|
|
|
case MEM_OBJ_SCRIPT: {
|
2009-05-03 22:46:38 +00:00
|
|
|
Script *scr = (Script *)mobj;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-10 19:17:51 +00:00
|
|
|
for (j = 0; j < scr->_objects.size(); j++) {
|
|
|
|
byte *data = scr->buf + scr->_objects[j].pos.offset;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 23:27:24 +00:00
|
|
|
if (self->isSci1_1) {
|
2009-03-07 16:14:20 +00:00
|
|
|
uint16 *funct_area = (uint16 *) (scr->buf + READ_LE_UINT16( data + 6 ));
|
|
|
|
uint16 *prop_area = (uint16 *) (scr->buf + READ_LE_UINT16( data + 4 ));
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-10 19:17:51 +00:00
|
|
|
scr->_objects[j].base_method = funct_area;
|
|
|
|
scr->_objects[j].base_vars = prop_area;
|
2009-02-15 22:28:12 +00:00
|
|
|
} else {
|
2009-03-07 16:14:20 +00:00
|
|
|
int funct_area = READ_LE_UINT16( data + SCRIPT_FUNCTAREAPTR_OFFSET );
|
2009-02-28 11:12:59 +00:00
|
|
|
Object *base_obj;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-12 23:30:42 +00:00
|
|
|
base_obj = obj_get(s, scr->_objects[j]._variables[SCRIPT_SPECIES_SELECTOR]);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-15 22:28:12 +00:00
|
|
|
if (!base_obj) {
|
2009-02-15 06:10:59 +00:00
|
|
|
sciprintf("Object without a base class: Script %d, index %d (reg address "PREG"\n",
|
2009-05-12 23:30:42 +00:00
|
|
|
scr->nr, j, PRINT_REG(scr->_objects[j]._variables[SCRIPT_SPECIES_SELECTOR]));
|
2009-02-15 06:10:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-12 23:30:42 +00:00
|
|
|
scr->_objects[j].variable_names_nr = base_obj->_variables.size();
|
2009-05-10 19:17:51 +00:00
|
|
|
scr->_objects[j].base_obj = base_obj->base_obj;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-10 19:17:51 +00:00
|
|
|
scr->_objects[j].base_method = (uint16 *)(data + funct_area);
|
|
|
|
scr->_objects[j].base_vars = (uint16 *)(data + scr->_objects[j].variable_names_nr * 2 + SCRIPT_SELECTOR_OFFSET);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-21 23:27:24 +00:00
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-21 23:27:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-20 19:08:38 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: The following should likely become a SegManager method
|
|
|
|
static void reconstruct_clones(EngineState *s, SegManager *self) {
|
2009-05-11 13:32:00 +00:00
|
|
|
for (uint i = 0; i < self->_heap.size(); i++) {
|
2009-05-03 22:46:11 +00:00
|
|
|
if (self->_heap[i]) {
|
2009-05-11 13:32:00 +00:00
|
|
|
MemObject *mobj = self->_heap[i];
|
2009-05-03 09:30:33 +00:00
|
|
|
switch (mobj->getType()) {
|
2009-02-15 22:28:12 +00:00
|
|
|
case MEM_OBJ_CLONES: {
|
2009-05-11 13:32:00 +00:00
|
|
|
CloneTable *ct = (CloneTable *)mobj;
|
2009-02-22 13:11:43 +00:00
|
|
|
|
2009-04-02 16:54:21 +00:00
|
|
|
/*
|
2009-02-15 06:10:59 +00:00
|
|
|
sciprintf("Free list: ");
|
2009-05-11 13:32:00 +00:00
|
|
|
for (uint j = ct->first_free; j != HEAPENTRY_INVALID; j = ct->_table[j].next_free) {
|
2009-02-15 06:10:59 +00:00
|
|
|
sciprintf("%d ", j);
|
|
|
|
}
|
|
|
|
sciprintf("\n");
|
|
|
|
|
|
|
|
sciprintf("Entries w/zero vars: ");
|
2009-05-11 13:32:00 +00:00
|
|
|
for (uint j = 0; j < ct->_table.size(); j++) {
|
|
|
|
if (ct->_table[j].variables == NULL)
|
2009-02-15 06:10:59 +00:00
|
|
|
sciprintf("%d ", j);
|
|
|
|
}
|
|
|
|
sciprintf("\n");
|
2009-04-02 16:54:21 +00:00
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
for (uint j = 0; j < ct->_table.size(); j++) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Object *base_obj;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-05-11 13:32:00 +00:00
|
|
|
if (!clone_entry_used(ct, j)) {
|
2009-02-15 06:10:59 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-05-11 13:32:00 +00:00
|
|
|
CloneTable::Entry &seeker = ct->_table[j];
|
2009-05-12 23:30:42 +00:00
|
|
|
base_obj = obj_get(s, seeker._variables[SCRIPT_SPECIES_SELECTOR]);
|
2009-02-15 22:28:12 +00:00
|
|
|
if (!base_obj) {
|
2009-02-15 06:10:59 +00:00
|
|
|
sciprintf("Clone entry without a base class: %d\n", j);
|
2009-05-11 13:32:00 +00:00
|
|
|
seeker.base = seeker.base_obj = NULL;
|
|
|
|
seeker.base_vars = seeker.base_method = NULL;
|
|
|
|
} else {
|
|
|
|
seeker.base = base_obj->base;
|
|
|
|
seeker.base_obj = base_obj->base_obj;
|
|
|
|
seeker.base_vars = base_obj->base_vars;
|
|
|
|
seeker.base_method = base_obj->base_method;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2009-02-21 23:27:24 +00:00
|
|
|
default:
|
|
|
|
break;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-20 19:08:38 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
int _reset_graphics_input(EngineState *s);
|
2009-02-20 19:08:38 +00:00
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
static void reconstruct_sounds(EngineState *s) {
|
2009-02-20 19:08:38 +00:00
|
|
|
song_t *seeker;
|
2009-05-12 23:32:32 +00:00
|
|
|
SongIteratorType it_type = s->resmgr->_sciVersion >= SCI_VERSION_01 ? SCI_SONG_ITERATOR_TYPE_SCI1 : SCI_SONG_ITERATOR_TYPE_SCI0;
|
2009-02-20 19:08:38 +00:00
|
|
|
|
|
|
|
if (s->sound.songlib.lib)
|
|
|
|
seeker = *(s->sound.songlib.lib);
|
|
|
|
else {
|
|
|
|
song_lib_init(&s->sound.songlib);
|
|
|
|
seeker = NULL;
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
while (seeker) {
|
2009-03-06 07:25:06 +00:00
|
|
|
SongIterator *base, *ff;
|
2009-02-20 19:08:38 +00:00
|
|
|
int oldstatus;
|
2009-03-10 02:42:22 +00:00
|
|
|
SongIterator::Message msg;
|
2009-02-20 19:08:38 +00:00
|
|
|
|
|
|
|
base = ff = build_iterator(s, seeker->resource_num, it_type, seeker->handle);
|
|
|
|
if (seeker->restore_behavior == RESTORE_BEHAVIOR_CONTINUE)
|
2009-03-07 06:56:39 +00:00
|
|
|
ff = new_fast_forward_iterator(base, seeker->restore_time);
|
2009-03-06 07:25:48 +00:00
|
|
|
ff->init();
|
2009-02-20 19:08:38 +00:00
|
|
|
|
2009-03-10 02:42:22 +00:00
|
|
|
msg = SongIterator::Message(seeker->handle, SIMSG_SET_LOOPS(seeker->loops));
|
2009-02-20 19:08:38 +00:00
|
|
|
songit_handle_message(&ff, msg);
|
2009-03-10 02:42:22 +00:00
|
|
|
msg = SongIterator::Message(seeker->handle, SIMSG_SET_HOLD(seeker->hold));
|
2009-02-20 19:08:38 +00:00
|
|
|
songit_handle_message(&ff, msg);
|
|
|
|
|
|
|
|
oldstatus = seeker->status;
|
|
|
|
seeker->status = SOUND_STATUS_STOPPED;
|
|
|
|
seeker->it = ff;
|
|
|
|
sfx_song_set_status(&s->sound, seeker->handle, oldstatus);
|
|
|
|
seeker = seeker->next;
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-03-10 14:52:02 +00:00
|
|
|
void internal_stringfrag_strncpy(EngineState *s, reg_t *dest, reg_t *src, int len);
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
EngineState *gamestate_restore(EngineState *s, Common::SeekableReadStream *fh) {
|
|
|
|
EngineState *retval;
|
2009-02-15 06:10:59 +00:00
|
|
|
songlib_t temp;
|
|
|
|
|
2009-02-16 00:35:22 +00:00
|
|
|
/*
|
|
|
|
if (s->sound_server) {
|
|
|
|
if ((s->sound_server->restore)(s, dirname)) {
|
|
|
|
sciprintf("Restoring failed for the sound subsystem\n");
|
|
|
|
return NULL;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-16 00:35:22 +00:00
|
|
|
}
|
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
// FIXME: Do in-place loading at some point, instead of creating a new EngineState instance from scratch.
|
2009-02-22 21:38:46 +00:00
|
|
|
retval = new EngineState();
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
retval->savegame_version = -1;
|
|
|
|
retval->gfx_state = s->gfx_state;
|
|
|
|
|
2009-02-28 11:07:36 +00:00
|
|
|
SavegameMetadata meta;
|
2009-02-20 23:41:15 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
Common::Serializer ser(fh, 0);
|
|
|
|
sync_SavegameMetadata(ser, meta);
|
2009-02-27 19:50:22 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
if (fh->eos())
|
2009-02-28 11:07:36 +00:00
|
|
|
return false;
|
|
|
|
|
2009-03-17 08:03:42 +00:00
|
|
|
if ((meta.savegame_version < MINIMUM_SAVEGAME_VERSION) ||
|
|
|
|
(meta.savegame_version > CURRENT_SAVEGAME_VERSION)) {
|
|
|
|
if (meta.savegame_version < MINIMUM_SAVEGAME_VERSION)
|
2009-02-20 23:41:15 +00:00
|
|
|
sciprintf("Old savegame version detected- can't load\n");
|
|
|
|
else
|
2009-03-17 08:03:42 +00:00
|
|
|
sciprintf("Savegame version is %d- maximum supported is %0d\n", meta.savegame_version, CURRENT_SAVEGAME_VERSION);
|
2009-02-20 23:41:15 +00:00
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
// Backwards compatibility settings
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->dyn_views = NULL;
|
|
|
|
retval->drop_views = NULL;
|
|
|
|
retval->port = NULL;
|
|
|
|
retval->save_dir_copy_buf = NULL;
|
|
|
|
|
|
|
|
retval->sound_mute = s->sound_mute;
|
|
|
|
retval->sound_volume = s->sound_volume;
|
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
retval->saveLoadWithSerializer(ser); // FIXME: Error handling?
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
sfx_exit(&s->sound);
|
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
// Set exec stack base to zero
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->execution_stack_base = 0;
|
|
|
|
retval->execution_stack_pos = 0;
|
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
// Now copy all current state information
|
|
|
|
// Graphics and input state:
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->animation_delay = s->animation_delay;
|
|
|
|
retval->animation_granularity = s->animation_granularity;
|
|
|
|
retval->gfx_state = s->gfx_state;
|
|
|
|
|
|
|
|
retval->resmgr = s->resmgr;
|
|
|
|
|
|
|
|
temp = retval->sound.songlib;
|
|
|
|
sfx_init(&retval->sound, retval->resmgr, s->sfx_init_flags);
|
|
|
|
retval->sfx_init_flags = s->sfx_init_flags;
|
|
|
|
song_lib_free(retval->sound.songlib);
|
|
|
|
retval->sound.songlib = temp;
|
|
|
|
|
|
|
|
_reset_graphics_input(retval);
|
|
|
|
reconstruct_stack(retval);
|
2009-02-21 23:27:24 +00:00
|
|
|
reconstruct_scripts(retval, retval->seg_manager);
|
|
|
|
reconstruct_clones(retval, retval->seg_manager);
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->game_obj = s->game_obj;
|
2009-05-03 22:46:38 +00:00
|
|
|
retval->script_000 = retval->seg_manager->getScript(script_get_segment(s, 0, SCRIPT_GET_DONT_LOAD), SEG_ID);
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->gc_countdown = GC_INTERVAL - 1;
|
|
|
|
retval->save_dir_copy = make_reg(s->sys_strings_segment, SYS_STRING_SAVEDIR);
|
|
|
|
retval->save_dir_edit_offset = 0;
|
2009-02-21 23:27:24 +00:00
|
|
|
retval->sys_strings_segment = find_unique_seg_by_type(retval->seg_manager, MEM_OBJ_SYS_STRINGS);
|
2009-05-03 22:46:38 +00:00
|
|
|
retval->sys_strings = (SystemStrings *)GET_SEGMENT(*retval->seg_manager, retval->sys_strings_segment, MEM_OBJ_SYS_STRINGS);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-26 23:03:35 +00:00
|
|
|
// Restore system strings
|
|
|
|
SystemString *str;
|
|
|
|
|
|
|
|
// First, pad memory
|
|
|
|
for (int i = 0; i < SYS_STRINGS_MAX; i++) {
|
|
|
|
str = &retval->sys_strings->strings[i];
|
2009-05-05 12:23:05 +00:00
|
|
|
char *data = (char *)str->value;
|
2009-02-26 23:03:35 +00:00
|
|
|
if (data) {
|
2009-05-05 12:23:05 +00:00
|
|
|
str->value = (reg_t *)calloc(str->max_size+1, sizeof(char)); // FIXME -- sizeof(char) or sizeof(reg_t) ??
|
|
|
|
strncpy((char *)str->value, data, str->max_size+1); // FIXME -- strncpy or internal_stringfrag_strncpy ?
|
2009-02-26 23:03:35 +00:00
|
|
|
free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
str = &retval->sys_strings->strings[SYS_STRING_SAVEDIR];
|
2009-03-10 14:52:02 +00:00
|
|
|
internal_stringfrag_strncpy(s, str->value, s->sys_strings->strings[SYS_STRING_SAVEDIR].value, str->max_size);
|
2009-05-05 12:00:51 +00:00
|
|
|
str->value[str->max_size - 1].segment = s->string_frag_segment; // Make sure to terminate
|
|
|
|
str->value[str->max_size - 1].offset &= 0xff00; // Make sure to terminate
|
2009-02-26 23:03:35 +00:00
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
// Time state:
|
2009-02-21 15:40:14 +00:00
|
|
|
retval->last_wait_time = g_system->getMillis();
|
2009-02-25 21:43:57 +00:00
|
|
|
retval->game_start_time = g_system->getMillis() - retval->game_time * 1000;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
// static parser information:
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->parser_rules = s->parser_rules;
|
2009-03-24 17:42:12 +00:00
|
|
|
retval->_parserWords = s->_parserWords;
|
2009-03-08 08:17:43 +00:00
|
|
|
retval->_parserSuffixes = s->_parserSuffixes;
|
2009-04-24 10:42:53 +00:00
|
|
|
retval->_parserBranches = s->_parserBranches;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
// static VM/Kernel information:
|
2009-02-23 03:04:52 +00:00
|
|
|
retval->_selectorNames = s->_selectorNames;
|
2009-04-22 17:53:29 +00:00
|
|
|
retval->_kernelNames = s->_kernelNames;
|
2009-04-22 17:54:11 +00:00
|
|
|
retval->_kfuncTable = s->_kfuncTable;
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->opcodes = s->opcodes;
|
|
|
|
|
|
|
|
memcpy(&(retval->selector_map), &(s->selector_map), sizeof(selector_map_t));
|
|
|
|
|
|
|
|
retval->parser_base = make_reg(s->sys_strings_segment, SYS_STRING_PARSER_BASE);
|
|
|
|
|
2009-02-20 19:08:38 +00:00
|
|
|
// Copy breakpoint information from current game instance
|
2009-02-15 06:10:59 +00:00
|
|
|
retval->have_bp = s->have_bp;
|
|
|
|
retval->bp_list = s->bp_list;
|
|
|
|
|
|
|
|
retval->debug_mode = s->debug_mode;
|
|
|
|
|
|
|
|
retval->kernel_opt_flags = 0;
|
2009-02-17 22:53:09 +00:00
|
|
|
retval->have_mouse_flag = 1;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
retval->successor = NULL;
|
2009-03-15 20:31:29 +00:00
|
|
|
retval->pic_priority_table = (int*)gfxop_get_pic_metainfo(retval->gfx_state);
|
2009-03-24 12:46:48 +00:00
|
|
|
retval->_gameName = obj_get_name(retval, retval->game_obj);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
retval->sound.it = NULL;
|
|
|
|
retval->sound.flags = s->sound.flags;
|
|
|
|
retval->sound.song = NULL;
|
|
|
|
retval->sound.suspended = s->sound.suspended;
|
|
|
|
retval->sound.debug = s->sound.debug;
|
|
|
|
reconstruct_sounds(retval);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2009-02-20 23:41:15 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
bool get_savegame_metadata(Common::SeekableReadStream *stream, SavegameMetadata *meta) {
|
|
|
|
assert(stream);
|
|
|
|
assert(meta);
|
|
|
|
|
|
|
|
Common::Serializer ser(stream, 0);
|
|
|
|
sync_SavegameMetadata(ser, *meta);
|
2009-02-20 23:41:15 +00:00
|
|
|
|
2009-03-15 20:31:29 +00:00
|
|
|
if (stream->eos())
|
2009-02-20 23:41:15 +00:00
|
|
|
return false;
|
|
|
|
|
2009-03-17 08:03:42 +00:00
|
|
|
if ((meta->savegame_version < MINIMUM_SAVEGAME_VERSION) ||
|
|
|
|
(meta->savegame_version > CURRENT_SAVEGAME_VERSION)) {
|
|
|
|
if (meta->savegame_version < MINIMUM_SAVEGAME_VERSION)
|
2009-02-20 23:41:15 +00:00
|
|
|
sciprintf("Old savegame version detected- can't load\n");
|
|
|
|
else
|
2009-03-17 08:03:42 +00:00
|
|
|
sciprintf("Savegame version is %d- maximum supported is %0d\n", meta->savegame_version, CURRENT_SAVEGAME_VERSION);
|
2009-02-20 23:41:15 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
} // End of namespace Sci
|