1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "pink/pink.h"
24 #include "pink/objects/module.h"
25 #include "pink/objects/pages/game_page.h"
26 #include "pink/objects/actors/lead_actor.h"
27 
28 namespace Pink {
29 
ModuleProxy(const Common::String & name)30 ModuleProxy::ModuleProxy(const Common::String &name)
31 		: NamedObject(name) {}
32 
ModuleProxy()33 ModuleProxy::ModuleProxy() {}
34 
Module(PinkEngine * game,const Common::String & name)35 Module::Module(PinkEngine *game, const Common::String &name)
36 		: NamedObject(name), _game(game), _page(nullptr) {}
37 
~Module()38 Module::~Module() {
39 	for (uint i = 0; i < _pages.size(); ++i) {
40 		delete _pages[i];
41 	}
42 }
43 
load(Archive & archive)44 void Module::load(Archive &archive) {
45 	archive.mapObject(this);
46 	NamedObject::deserialize(archive);
47 
48 	archive.skipString(); // skip directory
49 
50 	_invMgr.deserialize(archive);
51 	_pages.deserialize(archive);
52 }
53 
init(bool isLoadingSave,const Common::String & pageName)54 void Module::init(bool isLoadingSave, const Common::String &pageName) {
55 	// 0 0  - new game
56 	// 0 1 - module changed
57 	// 1 0 - from save
58 	if (!pageName.empty())
59 		_page = findPage(pageName);
60 	else if (!_page)
61 		_page = _pages[0];
62 
63 	_page->init(isLoadingSave);
64 }
65 
changePage(const Common::String & pageName)66 void Module::changePage(const Common::String &pageName) {
67 	_page->unload();
68 	_page = findPage(pageName);
69 	_page->init(kLoadingNewGame);
70 }
71 
findPage(const Common::String & pageName) const72 GamePage *Module::findPage(const Common::String &pageName) const {
73 	for (uint i = 0; i < _pages.size(); ++i) {
74 		if (_pages[i]->getName() == pageName)
75 			return _pages[i];
76 	}
77 	return nullptr;
78 }
79 
checkValueOfVariable(const Common::String & variable,const Common::String & value) const80 bool Module::checkValueOfVariable(const Common::String &variable, const Common::String &value) const {
81 	if (!_variables.contains(variable))
82 		return value == kUndefinedValue;
83 	return _variables[variable] == value;
84 }
85 
loadState(Archive & archive)86 void Module::loadState(Archive &archive) {
87 	_invMgr.loadState(archive);
88 	_variables.deserialize(archive);
89 
90 	for (uint i = 0; i < _pages.size(); ++i) {
91 		_pages[i]->loadState(archive);
92 	}
93 
94 	_page = findPage(archive.readString());
95 	_page->loadManagers();
96 	_page->getLeadActor()->loadState(archive);
97 }
98 
saveState(Archive & archive)99 void Module::saveState(Archive &archive) {
100 	_invMgr.saveState(archive);
101 	_variables.serialize(archive);
102 
103 	for (uint i = 0; i < _pages.size(); ++i) {
104 		_pages[i]->saveState(archive);
105 	}
106 
107 	archive.writeString(_page->getName());
108 	_page->getLeadActor()->saveState(archive);
109 }
110 
111 } // End of namespace Pink
112