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 "hopkins/hopkins.h"
24
25 #include "base/plugins.h"
26 #include "common/savefile.h"
27 #include "common/str-array.h"
28 #include "common/memstream.h"
29 #include "engines/advancedDetector.h"
30 #include "common/system.h"
31 #include "graphics/surface.h"
32
33 #include "hopkins/detection.h"
34
35 #define MAX_SAVES 99
36
37 namespace Hopkins {
38
getFeatures() const39 uint32 HopkinsEngine::getFeatures() const {
40 return _gameDescription->desc.flags;
41 }
42
getLanguage() const43 Common::Language HopkinsEngine::getLanguage() const {
44 return _gameDescription->desc.language;
45 }
46
getPlatform() const47 Common::Platform HopkinsEngine::getPlatform() const {
48 return _gameDescription->desc.platform;
49 }
50
getIsDemo() const51 bool HopkinsEngine::getIsDemo() const {
52 return _gameDescription->desc.flags & ADGF_DEMO;
53 }
54
getTargetName() const55 const Common::String &HopkinsEngine::getTargetName() const {
56 return _targetName;
57 }
58
59 } // End of namespace Hopkins
60
61 class HopkinsMetaEngine : public AdvancedMetaEngine {
62 public:
getName() const63 const char *getName() const override {
64 return "hopkins";
65 }
66
67 bool hasFeature(MetaEngineFeature f) const override;
68 Common::Error createInstance(OSystem *syst, Engine **engine, const ADGameDescription *desc) const override;
69
70 SaveStateList listSaves(const char *target) const override;
71 int getMaximumSaveSlot() const override;
72 void removeSaveState(const char *target, int slot) const override;
73 SaveStateDescriptor querySaveMetaInfos(const char *target, int slot) const override;
74 };
75
hasFeature(MetaEngineFeature f) const76 bool HopkinsMetaEngine::hasFeature(MetaEngineFeature f) const {
77 return
78 (f == kSupportsListSaves) ||
79 (f == kSupportsLoadingDuringStartup) ||
80 (f == kSupportsDeleteSave) ||
81 (f == kSavesSupportMetaInfo) ||
82 (f == kSavesSupportThumbnail) ||
83 (f == kSimpleSavesNames);
84 }
85
hasFeature(EngineFeature f) const86 bool Hopkins::HopkinsEngine::hasFeature(EngineFeature f) const {
87 return
88 (f == kSupportsReturnToLauncher) ||
89 (f == kSupportsLoadingDuringRuntime) ||
90 (f == kSupportsSavingDuringRuntime);
91 }
92
createInstance(OSystem * syst,Engine ** engine,const ADGameDescription * desc) const93 Common::Error HopkinsMetaEngine::createInstance(OSystem *syst, Engine **engine, const ADGameDescription *desc) const {
94 *engine = new Hopkins::HopkinsEngine(syst, (const Hopkins::HopkinsGameDescription *)desc);
95 return Common::kNoError;
96 }
97
listSaves(const char * target) const98 SaveStateList HopkinsMetaEngine::listSaves(const char *target) const {
99 Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
100 Common::StringArray filenames;
101 Common::String saveDesc;
102 Common::String pattern = Common::String::format("%s.0##", target);
103
104 filenames = saveFileMan->listSavefiles(pattern);
105
106 Hopkins::hopkinsSavegameHeader header;
107
108 SaveStateList saveList;
109 for (Common::StringArray::const_iterator file = filenames.begin(); file != filenames.end(); ++file) {
110 const char *ext = strrchr(file->c_str(), '.');
111 int slot = ext ? atoi(ext + 1) : -1;
112
113 if (slot >= 0 && slot < MAX_SAVES) {
114 Common::InSaveFile *in = g_system->getSavefileManager()->openForLoading(*file);
115
116 if (in) {
117 if (Hopkins::SaveLoadManager::readSavegameHeader(in, header)) {
118 saveList.push_back(SaveStateDescriptor(this, slot, header._saveName));
119 }
120
121 delete in;
122 }
123 }
124 }
125
126 // Sort saves based on slot number.
127 Common::sort(saveList.begin(), saveList.end(), SaveStateDescriptorSlotComparator());
128 return saveList;
129 }
130
getMaximumSaveSlot() const131 int HopkinsMetaEngine::getMaximumSaveSlot() const {
132 return MAX_SAVES;
133 }
134
removeSaveState(const char * target,int slot) const135 void HopkinsMetaEngine::removeSaveState(const char *target, int slot) const {
136 Common::String filename = Common::String::format("%s.%03d", target, slot);
137 g_system->getSavefileManager()->removeSavefile(filename);
138 }
139
querySaveMetaInfos(const char * target,int slot) const140 SaveStateDescriptor HopkinsMetaEngine::querySaveMetaInfos(const char *target, int slot) const {
141 Common::String filename = Common::String::format("%s.%03d", target, slot);
142 Common::InSaveFile *f = g_system->getSavefileManager()->openForLoading(filename);
143
144 if (f) {
145 Hopkins::hopkinsSavegameHeader header;
146 if (!Hopkins::SaveLoadManager::readSavegameHeader(f, header, false)) {
147 delete f;
148 return SaveStateDescriptor();
149 }
150
151 delete f;
152
153 // Create the return descriptor
154 SaveStateDescriptor desc(this, slot, header._saveName);
155 desc.setThumbnail(header._thumbnail);
156 desc.setSaveDate(header._year, header._month, header._day);
157 desc.setSaveTime(header._hour, header._minute);
158 desc.setPlayTime(header._totalFrames * GAME_FRAME_TIME);
159
160 return desc;
161 }
162
163 return SaveStateDescriptor();
164 }
165
166 #if PLUGIN_ENABLED_DYNAMIC(HOPKINS)
167 REGISTER_PLUGIN_DYNAMIC(HOPKINS, PLUGIN_TYPE_ENGINE, HopkinsMetaEngine);
168 #else
169 REGISTER_PLUGIN_STATIC(HOPKINS, PLUGIN_TYPE_ENGINE, HopkinsMetaEngine);
170 #endif
171