| 1 | // Copyright (C) 2006-2009 by Tor Andersson. |
| 2 | // Copyright (C) 2009 by Baltasar García Perez-Schofield. |
| 3 | // Copyright (C) 2010 by Ben Cressey. |
| 4 | // |
| 5 | // This file is part of Gargoyle. |
| 6 | // |
| 7 | // Gargoyle is free software; you can redistribute it and/or modify |
| 8 | // it under the terms of the GNU General Public License as published by |
| 9 | // the Free Software Foundation; either version 2 of the License, or |
| 10 | // (at your option) any later version. |
| 11 | // |
| 12 | // Gargoyle 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 Gargoyle; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | |
| 21 | #include <array> |
| 22 | #include <cctype> |
| 23 | #include <cstdint> |
| 24 | #include <fstream> |
| 25 | #include <ios> |
| 26 | #include <regex> |
| 27 | #include <sstream> |
| 28 | #include <stdexcept> |
| 29 | #include <string> |
| 30 | #include <unordered_map> |
| 31 | #include <utility> |
| 32 | #include <vector> |
| 33 | |
| 34 | #if __cplusplus >= 201703L |
| 35 | #include <filesystem> |
| 36 | #endif |
| 37 | |
| 38 | #include "format.h" |
| 39 | #include "optional.hpp" |
| 40 | |
| 41 | #include "glk.h" |
| 42 | #include "glkstart.h" |
| 43 | #include "gi_blorb.h" |
| 44 | #include "garglk.h" |
| 45 | #include "launcher.h" |
| 46 | |
| 47 | #define T_ADRIFT "scare" |
| 48 | #define T_ADRIFT5 "FrankenDrift.GlkRunner.Gargoyle" |
| 49 | #define T_ADVSYS "advsys" |
| 50 | #define T_AGT "agility" |
| 51 | #define T_ALAN2 "alan2" |
| 52 | #define T_ALAN3 "alan3" |
| 53 | #define T_GLULX "git" |
| 54 | #define T_HUGO "hugo" |
| 55 | #define T_JACL "jacl" |
| 56 | #define T_LEV9 "level9" |
| 57 | #define T_MGSR "magnetic" |
| 58 | #define T_PLUS "plus" |
| 59 | #define T_SCOTT "scott" |
| 60 | #define T_TADS "tadsr" |
| 61 | #define T_TAYLOR "taylor" |
| 62 | #define T_ZCODE "bocfel" |
| 63 | |
| 64 | #define ID_ZCOD (giblorb_make_id('Z', 'C', 'O', 'D')) |
| 65 | #define ID_GLUL (giblorb_make_id('G', 'L', 'U', 'L')) |
| 66 | #define ID_ADRI (giblorb_make_id('A', 'D', 'R', 'I')) |
| 67 | |
| 68 | namespace { |
| 69 | |
| 70 | struct Interpreter { |
| 71 | explicit Interpreter(std::string terp_, std::vector<std::string> flags_ = {}) : |
| 72 | terp(std::move(terp_)), |
| 73 | flags(std::move(flags_)) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | std::string terp; |
| 78 | std::vector<std::string> flags; |
| 79 | }; |
| 80 | |
| 81 | } |
| 82 | |
| 83 | enum class Format { |
| 84 | Adrift, |
| 85 | Adrift5, |
| 86 | AdvSys, |
| 87 | AGT, |
| 88 | Alan2, |
| 89 | Alan3, |
| 90 | Glulx, |
| 91 | Hugo, |
| 92 | JACL, |
| 93 | Level9, |
| 94 | Magnetic, |
| 95 | Plus, |
| 96 | Scott, |
| 97 | TADS, |
| 98 | Taylor, |
| 99 | ZCode, |
| 100 | }; |
| 101 | |
| 102 | static nonstd::optional<Format> probe(const std::array<char, 32> &header) |
| 103 | { |
| 104 | std::vector<std::pair<std::string, Format>> magic = { |
| 105 | {R"(^[\x01-\x08][\s\S]{17}\d{6})", Format::ZCode}, |
| 106 | {R"(^TADS2 bin\x0a\x0d\x1a)", Format::TADS}, |
| 107 | {R"(^T3-image\x0d\x0a\x1a[\x01\x02]\x00)", Format::TADS}, |
| 108 | {R"(^Glul)", Format::Glulx}, |
| 109 | {R"(^MaSc[\s\S]{4}\x00\x00\x00\x2a\x00[\x00\x01\x02\x03\x04])", Format::Magnetic}, |
| 110 | {R"(^\x3c\x42\x3f\xc9\x6a\x87\xc2\xcf[\x93\x94]\x45)", Format::Adrift}, |
| 111 | {R"(^\x3c\x42\x3f\xc9\x6a\x87\xc2\xcf[\x92]\x45)", Format::Adrift5}, |
| 112 | {R"(^\x58\xc7\xc1\x51)", Format::AGT}, |
| 113 | {R"(^[\s\S]{2}\xa0\x9d\x8b\x8e\x88\x8e)", Format::AdvSys}, |
| 114 | {R"(^[\x16\x18\x19\x1e\x1f][\s\S]{2}\d\d-\d\d-\d\d)", Format::Hugo}, |
| 115 | {R"(^[\s\S]{3}\x9b\x36\x21[\s\S]{18}\xff)", Format::Level9}, |
| 116 | {R"(^\x02(\x07\x05|\x08[\x01\x02\x03\x07]))", Format::Alan2}, |
| 117 | {R"(^ALAN\x03)", Format::Alan3}, |
| 118 | }; |
| 119 | |
| 120 | for (const auto &pair : magic) { |
| 121 | if (std::regex_search(header.begin(), header.end(), std::regex(pair.first))) { |
| 122 | return pair.second; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | return nonstd::nullopt; |
| 127 | } |
| 128 | |
| 129 | // Map extensions to formats |
| 130 | static const std::unordered_map<std::string, Format> extensions = { |
| 131 | {"taf", Format::Adrift}, |
| 132 | {"agx", Format::AGT}, |
| 133 | {"d$$", Format::AGT}, |
| 134 | {"acd", Format::Alan2}, |
| 135 | {"a3c", Format::Alan3}, |
| 136 | {"ulx", Format::Glulx}, |
| 137 | {"hex", Format::Hugo}, |
| 138 | {"j2", Format::JACL}, |
| 139 | {"jacl", Format::JACL}, |
| 140 | {"l9", Format::Level9}, |
| 141 | {"sna", Format::Level9}, |
| 142 | {"mag", Format::Magnetic}, |
| 143 | {"plus", Format::Plus}, |
| 144 | {"saga", Format::Scott}, |
| 145 | {"gam", Format::TADS}, |
| 146 | {"t3", Format::TADS}, |
| 147 | {"tay", Format::Taylor}, |
| 148 | {"dat", Format::Scott}, |
| 149 | {"z1", Format::ZCode}, |
| 150 | {"z2", Format::ZCode}, |
| 151 | {"z3", Format::ZCode}, |
| 152 | {"z4", Format::ZCode}, |
| 153 | {"z5", Format::ZCode}, |
| 154 | {"z6", Format::ZCode}, |
| 155 | {"z7", Format::ZCode}, |
| 156 | {"z8", Format::ZCode}, |
| 157 | }; |
| 158 | |
| 159 | // Map formats to default interpreters |
| 160 | static const std::unordered_map<Format, Interpreter> interpreters = { |
| 161 | {Format::Adrift, Interpreter(T_ADRIFT)}, |
| 162 | #ifdef WITH_FRANKENDRIFT |
| 163 | {Format::Adrift5, Interpreter(T_ADRIFT5)}, |
| 164 | #endif |
| 165 | {Format::AdvSys, Interpreter(T_ADVSYS)}, |
| 166 | {Format::AGT, Interpreter(T_AGT, {"-gl"})}, |
| 167 | {Format::Alan2, Interpreter(T_ALAN2)}, |
| 168 | {Format::Alan3, Interpreter(T_ALAN3)}, |
| 169 | {Format::Glulx, Interpreter(T_GLULX)}, |
| 170 | {Format::Hugo, Interpreter(T_HUGO)}, |
| 171 | {Format::JACL, Interpreter(T_JACL)}, |
| 172 | {Format::Level9, Interpreter(T_LEV9)}, |
| 173 | {Format::Magnetic, Interpreter(T_MGSR)}, |
| 174 | {Format::Plus, Interpreter(T_PLUS)}, |
| 175 | {Format::Scott, Interpreter(T_SCOTT)}, |
| 176 | {Format::TADS, Interpreter(T_TADS)}, |
| 177 | {Format::Taylor, Interpreter(T_TAYLOR)}, |
| 178 | {Format::ZCode, Interpreter(T_ZCODE)}, |
| 179 | }; |
| 180 | |
| 181 | static bool call_winterp(const Interpreter &interpreter, const std::string &game) |
| 182 | { |
| 183 | return garglk::winterp(GARGLKPRE + interpreter.terp, interpreter.flags, game); |
| 184 | } |
| 185 | |
| 186 | static bool call_winterp(Format format, const std::string &game) |
| 187 | { |
| 188 | try { |
| 189 | return call_winterp(interpreters.at(format), game); |
| 190 | } catch (const std::out_of_range &) { |
| 191 | // The FrankenDrift interpreter for Adrift 5 games is not supported on all |
| 192 | // platforms. Let the user know when this is the case. |
| 193 | #ifndef WITH_FRANKENDRIFT |
| 194 | if (format == Format::Adrift5) { |
| 195 | garglk::winmsg("Adrift 5 games are not supported by this build of Gargoyle"); |
| 196 | return false; |
| 197 | } |
| 198 | #endif |
| 199 | |
| 200 | // This should never occur: apart from Adrift 5, handled above, |
| 201 | // all known game formats always have interpreters. |
| 202 | garglk::winmsg("This game type is not supported"); |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | // Adrift 5 can create invalid Blorb files: the reported file size is |
| 208 | // smaller than the actual file size. gi_blorb (correctly) fails to load |
| 209 | // these files, but they still exist, so must be dealt with. If the |
| 210 | // overall size is ignored and the Blorb file is just read as though |
| 211 | // it's valid, all the chunks can be found (which is to say, the only |
| 212 | // problem with them is the file size, and that's not needed to iterate |
| 213 | // through the file). So, do just enough work here to determine if |
| 214 | // there's an Exec resource of type ADRI. If so, assume this is an |
| 215 | // Adrift file and leave it to the interpreter to run it properly. |
| 216 | static nonstd::optional<Format> find_adrift_blorb_format(const std::string &game) |
| 217 | { |
| 218 | std::ifstream f(game, std::ios::binary); |
| 219 | try { |
| 220 | f.exceptions(std::ifstream::badbit | std::ifstream::failbit | std::ifstream::eofbit); |
| 221 | |
| 222 | auto be32 = [&f]() { |
| 223 | std::array<unsigned char, 4> bytes; |
| 224 | f.read(reinterpret_cast<char *>(bytes.data()), bytes.size()); |
| 225 | |
| 226 | return (static_cast<std::uint32_t>(bytes[0]) << 24) | |
| 227 | (static_cast<std::uint32_t>(bytes[1]) << 16) | |
| 228 | (static_cast<std::uint32_t>(bytes[2]) << 8) | |
| 229 | (static_cast<std::uint32_t>(bytes[3]) << 0); |
| 230 | }; |
| 231 | |
| 232 | // Probing has already determined that this file starts with |
| 233 | // FORM....IFRSRidx, so there's no need to validate that. |
| 234 | f.seekg(20, std::ios::beg); |
| 235 | |
| 236 | auto nresources = be32(); |
| 237 | |
| 238 | for (std::uint32_t i = 0; i < nresources; i++) { |
| 239 | if (be32() == giblorb_ID_Exec) { |
| 240 | f.seekg(4, std::ios::cur); |
| 241 | f.seekg(be32(), std::ios::beg); |
| 242 | if (be32() != ID_ADRI) { |
| 243 | return nonstd::nullopt; |
| 244 | } |
| 245 | |
| 246 | f.seekg(12, std::ios::cur); |
| 247 | unsigned char version; |
| 248 | f.read(reinterpret_cast<char *>(&version), 1); |
| 249 | |
| 250 | if (version == 0x92) { |
| 251 | return Format::Adrift5; |
| 252 | } else if (version == 0x93 || version == 0x94) { |
| 253 | return Format::Adrift; |
| 254 | } else { |
| 255 | return nonstd::nullopt; |
| 256 | } |
| 257 | } else { |
| 258 | f.seekg(8, std::ios::cur); |
| 259 | } |
| 260 | } |
| 261 | } catch (const std::ifstream::failure &) { |
| 262 | } |
| 263 | |
| 264 | return nonstd::nullopt; |
| 265 | } |
| 266 | |
| 267 | static bool runblorb(const std::string &game) |
| 268 | { |
| 269 | class BlorbError : public std::runtime_error { |
| 270 | public: |
| 271 | explicit BlorbError(const std::string &msg) : std::runtime_error(msg) { |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | auto adrift_format = find_adrift_blorb_format(game); |
| 276 | if (adrift_format.has_value()) { |
| 277 | return call_winterp(*adrift_format, game); |
| 278 | } |
| 279 | |
| 280 | try { |
| 281 | giblorb_result_t res; |
| 282 | giblorb_map_t *basemap; |
| 283 | |
| 284 | auto file = garglk::unique(glkunix_stream_open_pathname(const_cast<char *>(game.c_str()), 0, 0), [](strid_t file) { |
| 285 | glk_stream_close(file, nullptr); |
| 286 | }); |
| 287 | if (!file) { |
| 288 | throw BlorbError("Unable to open file"); |
| 289 | } |
| 290 | |
| 291 | if (giblorb_create_map(file.get(), &basemap) != giblorb_err_None) { |
| 292 | throw BlorbError("Does not appear to be a valid Blorb file"); |
| 293 | } |
| 294 | |
| 295 | auto map = garglk::unique(basemap, giblorb_destroy_map); |
| 296 | |
| 297 | if (giblorb_load_resource(map.get(), giblorb_method_FilePos, &res, giblorb_ID_Exec, 0) != giblorb_err_None) { |
| 298 | throw BlorbError("Does not contain a story file (look for a corresponding game file to load instead)"); |
| 299 | } |
| 300 | |
| 301 | if (res.chunktype == ID_ZCOD) { |
| 302 | return call_winterp(Format::ZCode, game); |
| 303 | } else if (res.chunktype == ID_GLUL) { |
| 304 | return call_winterp(Format::Glulx, game); |
| 305 | } |
| 306 | |
| 307 | std::string name; |
| 308 | auto val = [](unsigned char c) -> char { |
| 309 | return std::isprint(c) ? c : '?'; |
| 310 | }; |
| 311 | auto ck = res.chunktype; |
| 312 | |
| 313 | auto msg = Format("Unknown game type: {:#08x} ({}{}{}{})", ck, val(ck >> 24), val(ck >> 16), val(ck >> 8), val(ck)); |
| 314 | throw BlorbError(msg); |
| 315 | } catch (const BlorbError &e) { |
| 316 | garglk::winmsg(Format("Could not load Blorb file {}:\n{}", game, e.what())); |
| 317 | return false; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | static nonstd::optional<Interpreter> findterp(const std::string &file, const std::string &target) |
| 322 | { |
| 323 | std::vector<std::string> matches = {target}; |
| 324 | |
| 325 | nonstd::optional<Interpreter> interpreter; |
| 326 | |
| 327 | garglk::config_entries(file, false, matches, [&interpreter](const std::string &cmd, const std::string &arg, int) { |
| 328 | if (cmd == "terp") { |
| 329 | std::istringstream argstream(arg); |
| 330 | std::string terp, flag; |
| 331 | std::vector<std::string> flags; |
| 332 | |
| 333 | if (argstream >> terp) { |
| 334 | while (argstream >> flag) { |
| 335 | flags.push_back(flag); |
| 336 | } |
| 337 | |
| 338 | interpreter.emplace(terp, flags); |
| 339 | } |
| 340 | } |
| 341 | }); |
| 342 | |
| 343 | return interpreter; |
| 344 | } |
| 345 | |
| 346 | // Find a possible interpreter specified in the config file. |
| 347 | static nonstd::optional<Interpreter> configterp(const std::string &gamepath) |
| 348 | { |
| 349 | std::string story = gamepath; |
| 350 | |
| 351 | // set up story |
| 352 | #if __cplusplus >= 201703L |
| 353 | story = std::filesystem::path(story) |
| 354 | .filename() |
| 355 | .string(); |
| 356 | #else |
| 357 | auto slash = story.rfind('\\'); |
| 358 | if (slash == std::string::npos) { |
| 359 | slash = story.find_last_of('/'); |
| 360 | } |
| 361 | if (slash != std::string::npos) { |
| 362 | story = story.substr(slash + 1); |
| 363 | } |
| 364 | #endif |
| 365 | |
| 366 | if (story.empty()) { |
| 367 | return nonstd::nullopt; |
| 368 | } |
| 369 | |
| 370 | for (const auto &config : garglk::configs(gamepath)) { |
| 371 | auto interpreter = findterp(config.path, story); |
| 372 | if (interpreter.has_value()) { |
| 373 | return interpreter; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | return nonstd::nullopt; |
| 378 | } |
| 379 | |
| 380 | bool garglk::rungame(const std::string &game) |
| 381 | { |
| 382 | std::array<char, 32> header; |
| 383 | |
| 384 | auto interpreter = configterp(game); |
| 385 | if (interpreter.has_value()) { |
| 386 | return call_winterp(*interpreter, game); |
| 387 | } |
| 388 | |
| 389 | std::ifstream f(game, std::ios::binary); |
| 390 | if (!f.is_open()) { |
| 391 | garglk::winmsg(Format("Unable to open {}", game)); |
| 392 | return false; |
| 393 | } |
| 394 | |
| 395 | if (f.read(header.data(), header.size())) { |
| 396 | auto is_blorb = std::regex_search(header.begin(), header.end(), std::regex(R"(^FORM[\s\S]{4}IFRSRIdx)")); |
| 397 | if (is_blorb) { |
| 398 | return runblorb(game); |
| 399 | } |
| 400 | |
| 401 | auto format = probe(header); |
| 402 | if (format.has_value()) { |
| 403 | return call_winterp(*format, game); |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | std::string ext = ""; |
| 408 | auto dot = game.rfind('.'); |
| 409 | if (dot != std::string::npos) { |
| 410 | ext = garglk::downcase(game.substr(dot + 1)); |
| 411 | } |
| 412 | |
| 413 | try { |
| 414 | return call_winterp(extensions.at(ext), game); |
| 415 | } catch (const std::out_of_range &) { |
| 416 | garglk::winmsg("Unknown file type"); |
| 417 | } |
| 418 | |
| 419 | return false; |
| 420 | } |