| 1 | // Copyright 2013-2024 Chris Spiegel. |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <cstring> |
| 6 | #include <map> |
| 7 | #include <memory> |
| 8 | #include <new> |
| 9 | #include <set> |
| 10 | #include <sstream> |
| 11 | #include <stdexcept> |
| 12 | #include <utility> |
| 13 | #include <vector> |
| 14 | |
| 15 | #include "meta.h" |
| 16 | #include "io.h" |
| 17 | #include "memory.h" |
| 18 | #include "objects.h" |
| 19 | #include "options.h" |
| 20 | #include "osdep.h" |
| 21 | #include "process.h" |
| 22 | #include "screen.h" |
| 23 | #include "stack.h" |
| 24 | #include "stash.h" |
| 25 | #include "types.h" |
| 26 | #include "unicode.h" |
| 27 | #include "util.h" |
| 28 | #include "zterp.h" |
| 29 | |
| 30 | using namespace std::literals; |
| 31 | |
| 32 | static bool uni_isdigit(char c) |
| 33 | { |
| 34 | return c >= '0' && c <= '9'; |
| 35 | } |
| 36 | |
| 37 | static bool uni_isxdigit(char c) |
| 38 | { |
| 39 | return "0123456789abcdef"s.find(c) != std::string::npos; |
| 40 | } |
| 41 | |
| 42 | static void try_user_save(const char *desc) |
| 43 | { |
| 44 | if (in_interrupt()) { |
| 45 | screen_puts("[Cannot save while in an interrupt]"); |
| 46 | } else { |
| 47 | if (push_save(SaveStackType::User, SaveType::Meta, SaveOpcode::Read, desc) == SaveResult::Success) { |
| 48 | screen_puts("[Save pushed]"); |
| 49 | } else { |
| 50 | screen_puts("[Save failed]"); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // On success, this does not return. |
| 56 | static void try_user_restore(size_t i) |
| 57 | { |
| 58 | SaveOpcode saveopcode; |
| 59 | |
| 60 | if (pop_save(SaveStackType::User, i, saveopcode)) { |
| 61 | screen_message_prompt("[Restored]"); |
| 62 | throw Operation::Restore(saveopcode); |
| 63 | } else { |
| 64 | screen_puts("[Restore failed]"); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | static void try_user_drop(size_t i) |
| 69 | { |
| 70 | if (drop_save(SaveStackType::User, i + 1)) { |
| 71 | screen_puts("[Dropped]"); |
| 72 | } else { |
| 73 | screen_puts("[Drop failed]"); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static std::vector<uint8_t> debug_change_memory; |
| 78 | static std::set<uint16_t> debug_change_invalid; |
| 79 | |
| 80 | static void meta_debug_change_start() |
| 81 | { |
| 82 | debug_change_memory.assign(memory.begin(), memory.begin() + header.static_start); |
| 83 | debug_change_invalid.clear(); |
| 84 | screen_puts("[Debug change reset]"); |
| 85 | } |
| 86 | |
| 87 | static void meta_debug_change_inc_dec(const std::string &string) |
| 88 | { |
| 89 | if (debug_change_memory.empty()) { |
| 90 | screen_puts("[Debug change not started]"); |
| 91 | } else { |
| 92 | bool saw_change = false; |
| 93 | |
| 94 | for (uint16_t addr = 0; addr < header.static_start - 2; addr++) { |
| 95 | if (debug_change_invalid.find(addr) == debug_change_invalid.end()) { |
| 96 | int16_t newval = as_signed(word(addr)); |
| 97 | int16_t oldval = as_signed((debug_change_memory[addr] << 8) | debug_change_memory[addr + 1]); |
| 98 | |
| 99 | #define CMP(a, b) (string == "inc" ? (a) > (b) : (a) < (b)) |
| 100 | if (CMP(newval, oldval)) { |
| 101 | #undef CMP |
| 102 | // The Z-machine does not require aligned memory access, so |
| 103 | // both even and odd addresses must be checked. However, |
| 104 | // global variables are word-sized, so if an address inside |
| 105 | // the global variables has changed, report only if the |
| 106 | // address is the base of globals plus a multiple of two. |
| 107 | if (is_global(addr) || !in_globals(addr)) { |
| 108 | screen_printf("%s: %ld -> %ld\n", addrstring(addr).c_str(), static_cast<long>(oldval), static_cast<long>(newval)); |
| 109 | saw_change = true; |
| 110 | } |
| 111 | } else { |
| 112 | debug_change_invalid.insert(addr); |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | if (!saw_change) { |
| 118 | screen_puts("[No changes]"); |
| 119 | } |
| 120 | |
| 121 | std::copy(memory.begin(), memory.begin() + header.static_start, debug_change_memory.begin()); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static bool meta_debug_change(const std::string &string) |
| 126 | { |
| 127 | if (string == "start") { |
| 128 | meta_debug_change_start(); |
| 129 | } else if (string == "inc" || string == "dec") { |
| 130 | meta_debug_change_inc_dec(string); |
| 131 | } else { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | static bool meta_debug_scan(const std::string &string) |
| 139 | { |
| 140 | static std::set<uint16_t> invalid_addr; |
| 141 | |
| 142 | if (string == "start") { |
| 143 | invalid_addr.clear(); |
| 144 | screen_puts("[Debug scan reset]"); |
| 145 | } else if (string == "show") { |
| 146 | for (uint16_t addr = 0; addr < header.static_start - 2; addr++) { |
| 147 | if (invalid_addr.find(addr) == invalid_addr.end()) { |
| 148 | if (is_global(addr) || !in_globals(addr)) { |
| 149 | screen_puts(addrstring(addr)); |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | } else if (uni_isdigit(string[0]) || (string[0] == '-' && uni_isdigit(string[1]))) { |
| 154 | bool valid; |
| 155 | long value = parseint(string, 0, valid); |
| 156 | if (!valid) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | if (value < -0x8000 || value > 0xffff) { |
| 161 | screen_puts("[Value is outside the range of a 16-bit integer and will never be found]"); |
| 162 | } else { |
| 163 | size_t count = 0; |
| 164 | |
| 165 | if (value < 0) { |
| 166 | value += 0x10000; |
| 167 | } |
| 168 | |
| 169 | for (uint16_t addr = 0; addr < header.static_start - 2; addr++) { |
| 170 | if (word(addr) != value) { |
| 171 | invalid_addr.insert(addr); |
| 172 | } |
| 173 | if (invalid_addr.find(addr) == invalid_addr.end()) { |
| 174 | count++; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | screen_printf("[%zu location%s]\n", count, count == 1 ? "" : "s"); |
| 179 | } |
| 180 | } else { |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | return true; |
| 185 | } |
| 186 | |
| 187 | static long parse_address(const std::string &string, bool &valid) |
| 188 | { |
| 189 | long addr; |
| 190 | |
| 191 | if (string[0] == 'G' && uni_isxdigit(string[1]) && uni_isxdigit(string[2]) && string[3] == 0) { |
| 192 | addr = parseint(&string[1], 16, valid); |
| 193 | if (addr < 0 || addr > 239) { |
| 194 | valid = false; |
| 195 | } |
| 196 | addr = header.globals + (2 * addr); |
| 197 | } else { |
| 198 | addr = parseint(string, 16, valid); |
| 199 | } |
| 200 | |
| 201 | return addr; |
| 202 | } |
| 203 | |
| 204 | static bool validate_address(long addr, bool print) |
| 205 | { |
| 206 | if (addr < 0 || addr > memory_size - 2) { |
| 207 | if (print) { |
| 208 | screen_printf("[Address out of range: must be [0, 0x%lx]]\n", static_cast<unsigned long>(memory_size) - 2); |
| 209 | } |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |
| 216 | static bool meta_debug_print(const std::string &string) |
| 217 | { |
| 218 | bool valid; |
| 219 | long addr = parse_address(string, valid); |
| 220 | |
| 221 | if (!valid) { |
| 222 | return false; |
| 223 | } |
| 224 | if (!validate_address(addr, true)) { |
| 225 | return true; |
| 226 | } |
| 227 | |
| 228 | screen_printf("%ld (0x%04lx)\n", static_cast<long>(as_signed(word(addr))), static_cast<unsigned long>(word(addr))); |
| 229 | |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | #ifndef ZTERP_NO_CHEAT |
| 234 | // Map addresses to frozen values. |
| 235 | static std::map<uint16_t, uint16_t> frozen_addresses; |
| 236 | |
| 237 | bool cheat_add(const std::string &how, bool print) |
| 238 | { |
| 239 | std::istringstream ss(how); |
| 240 | std::string type, addrstr, valstr; |
| 241 | |
| 242 | if (!std::getline(ss, type, ':') || |
| 243 | !std::getline(ss, addrstr, ':') || |
| 244 | !std::getline(ss, valstr)) |
| 245 | { |
| 246 | return false; |
| 247 | } |
| 248 | |
| 249 | if (type == "freeze" || type == "freezew") { |
| 250 | long addr; |
| 251 | long value; |
| 252 | bool valid; |
| 253 | |
| 254 | addr = parse_address(addrstr, valid); |
| 255 | if (!valid) { |
| 256 | return false; |
| 257 | } |
| 258 | if (!validate_address(addr, print)) { |
| 259 | return true; |
| 260 | } |
| 261 | |
| 262 | value = parseint(valstr, 0, valid); |
| 263 | if (!valid) { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | if (value < 0 || value > UINT16_MAX) { |
| 268 | if (print) { |
| 269 | screen_puts("[Values must be in the range [0, 65535]]"); |
| 270 | } |
| 271 | return true; |
| 272 | } |
| 273 | |
| 274 | frozen_addresses[addr] = value; |
| 275 | } else { |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | if (print) { |
| 280 | screen_puts("[Frozen]"); |
| 281 | } |
| 282 | |
| 283 | return true; |
| 284 | } |
| 285 | |
| 286 | static bool cheat_remove(uint16_t addr) |
| 287 | { |
| 288 | return frozen_addresses.erase(addr) > 0; |
| 289 | } |
| 290 | |
| 291 | bool cheat_find_freeze(uint32_t addr, uint16_t &val) |
| 292 | { |
| 293 | auto found = frozen_addresses.find(addr); |
| 294 | if (found == frozen_addresses.end()) { |
| 295 | return false; |
| 296 | } |
| 297 | |
| 298 | val = found->second; |
| 299 | |
| 300 | return true; |
| 301 | } |
| 302 | |
| 303 | static bool meta_debug_freeze(const std::string &string) |
| 304 | { |
| 305 | std::istringstream ss(string); |
| 306 | std::string addrstr, valstr; |
| 307 | |
| 308 | if (!std::getline(ss, addrstr, ' ') || |
| 309 | !std::getline(ss, valstr)) |
| 310 | { |
| 311 | return false; |
| 312 | } |
| 313 | |
| 314 | std::string cheat = "freeze:" + addrstr + ":" + valstr; |
| 315 | |
| 316 | return cheat_add(cheat, true); |
| 317 | } |
| 318 | |
| 319 | static bool meta_debug_unfreeze(const std::string &string) |
| 320 | { |
| 321 | bool valid; |
| 322 | long addr = parse_address(string, valid); |
| 323 | if (!valid) { |
| 324 | return false; |
| 325 | } |
| 326 | if (!validate_address(addr, true)) { |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | if (cheat_remove(addr)) { |
| 331 | screen_puts("[Unfrozen]"); |
| 332 | } else { |
| 333 | screen_puts("[Address not frozen]"); |
| 334 | } |
| 335 | |
| 336 | return true; |
| 337 | } |
| 338 | |
| 339 | static bool meta_debug_show_freeze() |
| 340 | { |
| 341 | if (frozen_addresses.empty()) { |
| 342 | screen_puts("[No frozen values]"); |
| 343 | } else { |
| 344 | for (const auto &pair : frozen_addresses) { |
| 345 | screen_printf("%s: %lu\n", addrstring(pair.first).c_str(), static_cast<unsigned long>(pair.second)); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return true; |
| 350 | } |
| 351 | #endif |
| 352 | |
| 353 | #ifndef ZTERP_NO_WATCHPOINTS |
| 354 | static std::set<uint16_t> watch_addresses; |
| 355 | |
| 356 | static void watch_add(uint16_t addr) |
| 357 | { |
| 358 | watch_addresses.insert(addr); |
| 359 | } |
| 360 | |
| 361 | static void watch_all() |
| 362 | { |
| 363 | for (unsigned long addr = 0; addr < UINT16_MAX + 1UL; addr++) { |
| 364 | watch_addresses.insert(addr); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | static bool watch_remove(uint16_t addr) |
| 369 | { |
| 370 | return watch_addresses.erase(addr) == 1; |
| 371 | } |
| 372 | |
| 373 | static void watch_none() |
| 374 | { |
| 375 | watch_addresses.clear(); |
| 376 | } |
| 377 | |
| 378 | void watch_check(uint16_t addr, unsigned long oldval, unsigned long newval) |
| 379 | { |
| 380 | if (watch_addresses.find(addr) != watch_addresses.end() && oldval != newval) { |
| 381 | screen_printf("[%s changed: %lu -> %lu (pc = 0x%lx)]\n", addrstring(addr).c_str(), oldval, newval, current_instruction); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | static bool meta_debug_watch_helper(const std::string &string, bool do_watch) |
| 386 | { |
| 387 | if (string == "all") { |
| 388 | if (do_watch) { |
| 389 | watch_all(); |
| 390 | screen_puts("[Watching all addresses for changes]"); |
| 391 | } else { |
| 392 | watch_none(); |
| 393 | screen_puts("[Not watching any addresses for changes]"); |
| 394 | } |
| 395 | } else { |
| 396 | bool valid; |
| 397 | long addr = parse_address(string, valid); |
| 398 | |
| 399 | if (!valid) { |
| 400 | return false; |
| 401 | } |
| 402 | if (!validate_address(addr, true)) { |
| 403 | return true; |
| 404 | } |
| 405 | |
| 406 | if (do_watch) { |
| 407 | watch_add(addr); |
| 408 | screen_printf("[Watching %s for changes]\n", addrstring(addr).c_str()); |
| 409 | } else { |
| 410 | if (watch_remove(addr)) { |
| 411 | screen_printf("[No longer watching %s for changes]\n", addrstring(addr).c_str()); |
| 412 | } else { |
| 413 | screen_printf("[%s is not currently being watched]\n", addrstring(addr).c_str()); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | return true; |
| 419 | } |
| 420 | |
| 421 | static bool meta_debug_watch(const std::string &string) |
| 422 | { |
| 423 | return meta_debug_watch_helper(string, true); |
| 424 | } |
| 425 | |
| 426 | static bool meta_debug_unwatch(const std::string &string) |
| 427 | { |
| 428 | return meta_debug_watch_helper(string, false); |
| 429 | } |
| 430 | |
| 431 | static bool meta_debug_show_watch() |
| 432 | { |
| 433 | if (watch_addresses.empty()) { |
| 434 | screen_puts("[No watched values]"); |
| 435 | } else { |
| 436 | for (const auto &addr : watch_addresses) { |
| 437 | screen_puts(addrstring(addr)); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | return true; |
| 442 | } |
| 443 | #endif |
| 444 | |
| 445 | static void meta_debug_help() |
| 446 | { |
| 447 | screen_print( |
| 448 | "Debug commands are accessed as \"/debug [command]\". Commands are:\n\n" |
| 449 | "change start: restart change tracking\n" |
| 450 | "change dec: display all words which have been decremented since the last check\n" |
| 451 | "change inc: display all words which have been incremented since the last check\n" |
| 452 | "scan start: restart scan tracking\n" |
| 453 | "scan [n]: update scan list with all words equal to [n]; if [n] starts with 0x it is hexadecimal, 0 it is octal, decimal otherwise\n" |
| 454 | "scan show: print all locations matching scan criteria\n" |
| 455 | "print [address]: print the word at address [address]\n" |
| 456 | #ifndef ZTERP_NO_CHEAT |
| 457 | "freeze [address] [value]: freeze the 16-bit [value] at [address]; [value] can be decimal, hexadecimal, or octal, with a leading 0x signifying hexadecimal and a leading 0 signifying octal\n" |
| 458 | "unfreeze [address]: unfreeze the value currently frozen at [address]\n" |
| 459 | "show_freeze: show all frozen values\n" |
| 460 | #endif |
| 461 | #ifndef ZTERP_NO_WATCHPOINTS |
| 462 | "watch [address]: report any changes to the word at [address]\n" |
| 463 | "watch all: report any changes to words at all addresses\n" |
| 464 | "unwatch [address]: stop watching [address]\n" |
| 465 | "unwatch all: stop watching all addresses\n" |
| 466 | "show_watch: show all watched-for addresses\n" |
| 467 | #endif |
| 468 | "\nAddresses are either absolute, specified in hexadecimal with an optional leading 0x, or global variables. Global variables have the syntax Gxx, where xx is a hexadecimal value in the range [00, ef], corresponding to global variables 0 to 239.\n" |
| 469 | ); |
| 470 | } |
| 471 | |
| 472 | static void meta_debug(const std::string &string) |
| 473 | { |
| 474 | bool ok = false; |
| 475 | std::istringstream ss(rtrim(string)); |
| 476 | std::string cmd, args; |
| 477 | |
| 478 | ss >> cmd; |
| 479 | std::getline(ss >> std::ws, args); |
| 480 | |
| 481 | if (cmd == "change") { |
| 482 | ok = meta_debug_change(args); |
| 483 | } else if (cmd == "scan") { |
| 484 | ok = meta_debug_scan(args); |
| 485 | } else if (cmd == "print") { |
| 486 | ok = meta_debug_print(args); |
| 487 | } |
| 488 | #ifndef ZTERP_NO_CHEAT |
| 489 | else if (cmd == "freeze") { |
| 490 | ok = meta_debug_freeze(args); |
| 491 | } else if (cmd == "unfreeze") { |
| 492 | ok = meta_debug_unfreeze(args); |
| 493 | } else if (string == "show_freeze") { |
| 494 | ok = meta_debug_show_freeze(); |
| 495 | } |
| 496 | #endif |
| 497 | #ifndef ZTERP_NO_WATCHPOINTS |
| 498 | else if (cmd == "watch") { |
| 499 | ok = meta_debug_watch(args); |
| 500 | } else if (cmd == "unwatch") { |
| 501 | ok = meta_debug_unwatch(args); |
| 502 | } else if (string == "show_watch") { |
| 503 | ok = meta_debug_show_watch(); |
| 504 | } |
| 505 | #endif |
| 506 | |
| 507 | if (!ok) { |
| 508 | meta_debug_help(); |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | static void meta_info() |
| 513 | { |
| 514 | screen_printf("File: %s\n", game_file.c_str()); |
| 515 | screen_printf("Z-machine version: %d\n", memory[0]); |
| 516 | screen_printf("ID: %s\n", get_story_id().c_str()); |
| 517 | } |
| 518 | |
| 519 | static std::vector<char> meta_notes; |
| 520 | |
| 521 | IFF::TypeID meta_write_bfnt(IO &savefile) |
| 522 | { |
| 523 | if (meta_notes.empty()) { |
| 524 | return IFF::TypeID(); |
| 525 | } |
| 526 | |
| 527 | savefile.write32(0); // Version |
| 528 | savefile.write_exact(meta_notes.data(), meta_notes.size()); |
| 529 | |
| 530 | return IFF::TypeID("Bfnt"); |
| 531 | } |
| 532 | |
| 533 | void meta_read_bfnt(IO &io, uint32_t size) |
| 534 | { |
| 535 | uint32_t version; |
| 536 | |
| 537 | meta_notes.clear(); |
| 538 | |
| 539 | if (size < 4) { |
| 540 | show_message("Corrupted Bfnt entry (too small)"); |
| 541 | return; |
| 542 | } |
| 543 | |
| 544 | try { |
| 545 | version = io.read32(); |
| 546 | } catch (const IO::IOError &) { |
| 547 | show_message("Unable to read notes size from save file"); |
| 548 | return; |
| 549 | } |
| 550 | |
| 551 | if (version != 0) { |
| 552 | show_message("Unsupported Bfnt version %lu", static_cast<unsigned long>(version)); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | size -= 4; |
| 557 | |
| 558 | if (size > 0) { |
| 559 | try { |
| 560 | meta_notes.resize(size); |
| 561 | } catch (const std::bad_alloc &) { |
| 562 | show_message("Unable to allocate memory for notes (requested %lu bytes)", static_cast<unsigned long>(size)); |
| 563 | return; |
| 564 | } |
| 565 | |
| 566 | try { |
| 567 | io.read_exact(meta_notes.data(), size); |
| 568 | } catch (const IO::IOError &) { |
| 569 | meta_notes.clear(); |
| 570 | show_message("Unable to read notes from save file"); |
| 571 | return; |
| 572 | } |
| 573 | } |
| 574 | } |
| 575 | |
| 576 | class NotesStasher : public Stasher { |
| 577 | public: |
| 578 | void backup() override { |
| 579 | m_notes = meta_notes; |
| 580 | } |
| 581 | |
| 582 | bool restore() override { |
| 583 | meta_notes = m_notes; |
| 584 | |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | void free() override { |
| 589 | m_notes.clear(); |
| 590 | } |
| 591 | |
| 592 | private: |
| 593 | std::vector<char> m_notes; |
| 594 | }; |
| 595 | |
| 596 | // Try to parse a meta command. If input should be re-requested, return |
| 597 | // <MetaResult::Rerequest, "">. If a portion of the passed-in string |
| 598 | // should be processed as user input, return <MetaResult::Say, string>. |
| 599 | // |
| 600 | // There is also the possibility that a meta-command causes a saved game |
| 601 | // of some kind to be restored (/undo, /restore, and /pop). In these |
| 602 | // cases Operation::Restore is thrown, so nothing is returned. |
| 603 | std::pair<MetaResult, std::string> handle_meta_command(const uint16_t *string, uint8_t len) |
| 604 | { |
| 605 | std::string command, rest; |
| 606 | std::string converted; |
| 607 | |
| 608 | // Convert the input string to UTF-8 using memory-backed I/O. |
| 609 | try { |
| 610 | IO io(std::vector<uint8_t>(), IO::Mode::WriteOnly); |
| 611 | |
| 612 | for (size_t i = 0; i < len; i++) { |
| 613 | try { |
| 614 | io.putc(string[i]); |
| 615 | } catch (const IO::IOError &) { |
| 616 | return {MetaResult::Rerequest, ""}; |
| 617 | } |
| 618 | } |
| 619 | |
| 620 | const auto &buf = io.get_memory(); |
| 621 | converted = rtrim(std::string(buf.begin(), buf.end())); |
| 622 | } catch (const IO::OpenError &) { |
| 623 | return {MetaResult::Rerequest, ""}; |
| 624 | } |
| 625 | |
| 626 | std::istringstream ss(converted); |
| 627 | ss >> command; |
| 628 | std::getline(ss >> std::ws, rest); |
| 629 | |
| 630 | // Require a leading slash. |
| 631 | if (command[0] != '/') { |
| 632 | return {MetaResult::Rerequest, ""}; |
| 633 | }; |
| 634 | command.erase(0, 1); |
| 635 | |
| 636 | #define ZEROARG(name) (command == (name)) if (!rest.empty()) { screen_printf("[/%s takes no arguments]\n", name); } else |
| 637 | |
| 638 | if ZEROARG("undo") { |
| 639 | SaveOpcode saveopcode; |
| 640 | if (pop_save(SaveStackType::Game, 0, saveopcode)) { |
| 641 | if (seen_save_undo) { |
| 642 | store(2); |
| 643 | } else { |
| 644 | screen_message_prompt("[Undone]"); |
| 645 | } |
| 646 | |
| 647 | throw Operation::Restore(saveopcode); |
| 648 | } else { |
| 649 | screen_puts("[No save found, unable to undo]"); |
| 650 | } |
| 651 | } else if ZEROARG("scripton") { |
| 652 | if (output_stream(OSTREAM_TRANSCRIPT, 0)) { |
| 653 | screen_puts("[Transcripting on]"); |
| 654 | } else { |
| 655 | screen_puts("[Transcripting failed]"); |
| 656 | } |
| 657 | } else if ZEROARG("scriptoff") { |
| 658 | output_stream(-OSTREAM_TRANSCRIPT, 0); |
| 659 | screen_puts("[Transcripting off]"); |
| 660 | } else if ZEROARG("recon") { |
| 661 | if (output_stream(OSTREAM_RECORD, 0)) { |
| 662 | screen_puts("[Command recording on]"); |
| 663 | } else { |
| 664 | screen_puts("[Command recording failed]"); |
| 665 | } |
| 666 | } else if ZEROARG("recoff") { |
| 667 | output_stream(-OSTREAM_RECORD, 0); |
| 668 | screen_puts("[Command recording off]"); |
| 669 | } else if ZEROARG("replay") { |
| 670 | if (input_stream(ISTREAM_FILE)) { |
| 671 | screen_puts("[Replaying commands]"); |
| 672 | } else { |
| 673 | screen_puts("[Replaying commands failed]"); |
| 674 | } |
| 675 | } else if ZEROARG("save") { |
| 676 | if (in_interrupt()) { |
| 677 | screen_puts("[Cannot call /save while in an interrupt]"); |
| 678 | } else { |
| 679 | if (do_save(SaveType::Meta, SaveOpcode::Read)) { |
| 680 | screen_puts("[Saved]"); |
| 681 | } else { |
| 682 | screen_puts("[Save failed]"); |
| 683 | } |
| 684 | } |
| 685 | } else if ZEROARG("restore") { |
| 686 | SaveOpcode saveopcode; |
| 687 | |
| 688 | if (do_restore(SaveType::Meta, saveopcode)) { |
| 689 | screen_message_prompt("[Restored]"); |
| 690 | throw Operation::Restore(saveopcode); |
| 691 | } else { |
| 692 | screen_puts("[Restore failed]"); |
| 693 | } |
| 694 | } else if (command == "ps") { |
| 695 | if (rest.empty()) { |
| 696 | try_user_save(nullptr); |
| 697 | } else { |
| 698 | try_user_save(rest.c_str()); |
| 699 | } |
| 700 | } else if (command == "pop" || command == "drop") { |
| 701 | void (*restore_or_drop)(size_t) = command == "pop" ? try_user_restore : try_user_drop; |
| 702 | |
| 703 | if (restore_or_drop == try_user_drop && rest == "all") { |
| 704 | while (drop_save(SaveStackType::User, 1)) { |
| 705 | } |
| 706 | |
| 707 | screen_puts("[All saves dropped]"); |
| 708 | } else if (rest[0] == 0) { |
| 709 | restore_or_drop(0); |
| 710 | } else { |
| 711 | bool valid; |
| 712 | long saveno = parseint(rest, 10, valid); |
| 713 | if (!valid || saveno < 1) { |
| 714 | screen_puts("[Invalid index]"); |
| 715 | return {MetaResult::Rerequest, ""}; |
| 716 | } |
| 717 | |
| 718 | restore_or_drop(saveno - 1); |
| 719 | } |
| 720 | } else if ZEROARG("ls") { |
| 721 | list_saves(SaveStackType::User); |
| 722 | } else if ZEROARG("savetranscript") { |
| 723 | screen_save_persistent_transcript(); |
| 724 | } else if ZEROARG("showtranscript") { |
| 725 | screen_show_persistent_transcript(); |
| 726 | } else if ZEROARG("notes") { |
| 727 | screen_puts("[Editing notes]"); |
| 728 | screen_flush(); |
| 729 | try { |
| 730 | meta_notes = zterp_os_edit_notes(meta_notes); |
| 731 | screen_puts("[Notes saved]"); |
| 732 | } catch (const std::runtime_error &e) { |
| 733 | screen_printf("[Error updating notes: %s]\n", e.what()); |
| 734 | } |
| 735 | } else if ZEROARG("shownotes") { |
| 736 | if (meta_notes.empty()) { |
| 737 | screen_puts("[No notes taken]"); |
| 738 | } else { |
| 739 | screen_puts("[Start of notes]"); |
| 740 | screen_puts(std::string(meta_notes.data(), meta_notes.size())); |
| 741 | screen_puts("[End of notes]"); |
| 742 | } |
| 743 | } else if ZEROARG("savenotes") { |
| 744 | if (meta_notes.empty()) { |
| 745 | screen_puts("[No notes taken]"); |
| 746 | } else { |
| 747 | try { |
| 748 | IO io(nullptr, IO::Mode::WriteOnly, IO::Purpose::Data); |
| 749 | try { |
| 750 | io.write_exact(meta_notes.data(), meta_notes.size()); |
| 751 | screen_puts("[Saved notes to file]"); |
| 752 | } catch (const IO::IOError &) { |
| 753 | screen_puts("[Unable to write notes file]"); |
| 754 | } |
| 755 | } catch (const IO::OpenError &) { |
| 756 | screen_puts("[Unable to open notes file]"); |
| 757 | } |
| 758 | } |
| 759 | } else if ZEROARG("status") { |
| 760 | if (zversion > 3) { |
| 761 | screen_puts("[/status is only available in V1, V2, and V3]"); |
| 762 | } else { |
| 763 | long first = as_signed(variable(0x11)), second = as_signed(variable(0x12)); |
| 764 | |
| 765 | print_object(variable(0x10), [](uint8_t c) { |
| 766 | screen_putc(zscii_to_unicode[c]); |
| 767 | }); |
| 768 | |
| 769 | screen_print("\n"); |
| 770 | |
| 771 | if (status_is_time()) { |
| 772 | auto fmt = screen_format_time(first, second); |
| 773 | screen_puts(fmt); |
| 774 | } else { |
| 775 | if (is_game(Game::Planetfall) || is_game(Game::Stationfall)) { |
| 776 | screen_printf("Score: %ld\nTime: %ld\n", first, second); |
| 777 | } else { |
| 778 | screen_printf("Score: %ld\nMoves: %ld\n", first, second); |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | } else if ZEROARG("disable") { |
| 783 | options.disable_meta_commands = true; |
| 784 | screen_puts("[Meta commands disabled]"); |
| 785 | } else if (command == "say") { |
| 786 | return {MetaResult::Say, rest}; |
| 787 | } else if ZEROARG("config") { |
| 788 | auto config_file = zterp_os_rcfile(true); |
| 789 | if (config_file != nullptr) { |
| 790 | screen_puts("[Editing configuration file]"); |
| 791 | screen_flush(); |
| 792 | try { |
| 793 | zterp_os_edit_file(*config_file); |
| 794 | screen_puts("[Done; changes will be reflected after Bocfel is restarted]"); |
| 795 | } catch (const std::runtime_error &e) { |
| 796 | screen_printf("[Error editing configuration file: %s]\n", e.what()); |
| 797 | } |
| 798 | } else { |
| 799 | screen_puts("[Cannot determine configuration file location]"); |
| 800 | } |
| 801 | } else if ZEROARG("fixed") { |
| 802 | if (screen_toggle_force_fixed()) { |
| 803 | screen_puts("[Force fixed font on]"); |
| 804 | } else { |
| 805 | screen_puts("[Force fixed font off]"); |
| 806 | } |
| 807 | } else if (command == "debug") { |
| 808 | meta_debug(rest); |
| 809 | } else if ZEROARG("quit") { |
| 810 | zquit(); |
| 811 | } else if ZEROARG("info") { |
| 812 | meta_info(); |
| 813 | } else { |
| 814 | if (command != "help") { |
| 815 | screen_printf("Unknown command: /%s\n\n", command.c_str()); |
| 816 | } |
| 817 | |
| 818 | screen_print( |
| 819 | "The following commands are provided by the interpreter, not the game:\n\n" |
| 820 | "/undo: undo a turn\n" |
| 821 | "/scripton: start a transcript\n" |
| 822 | "/scriptoff: stop a transcript\n" |
| 823 | "/recon: start a command record\n" |
| 824 | "/recoff: stop a command record\n" |
| 825 | "/replay: replay a command record\n" |
| 826 | "/save: save the game\n" |
| 827 | "/restore: restore a game saved by /save\n" |
| 828 | "/ps: add a new in-memory save state\n" |
| 829 | "/ps [description]: add a new in-memory save state called [description]\n" |
| 830 | "/pop: restore the last-saved in-memory state\n" |
| 831 | "/pop [n]: restore the specified in-memory state\n" |
| 832 | "/drop: drop the last-saved in-memory state\n" |
| 833 | "/drop [n]: drop the specified in-memory state\n" |
| 834 | "/drop all: drop all in-memory states\n" |
| 835 | "/ls: list all in-memory save states\n" |
| 836 | "/savetranscript: save persistent transcript (if active) to a file\n" |
| 837 | "/notes: open a text editor to take notes\n" |
| 838 | "/shownotes: display notes taken, if any\n" |
| 839 | "/savenotes: save notes taken, if any, to a file\n" |
| 840 | "/status: display the status line (V1, V2, and V3 games only)\n" |
| 841 | "/disable: disable these commands for the rest of this session\n" |
| 842 | "/say [command]: pretend like [command] was typed\n" |
| 843 | "/config: open the config file in a text editor\n" |
| 844 | "/fixed: toggle forced fixed-width font in the main window\n" |
| 845 | "/debug [...]: perform a debugging operation\n" |
| 846 | "/quit: quit immediately (as if the @quit opcode were executed)\n" |
| 847 | "/info: display some info about the current game\n" |
| 848 | ); |
| 849 | } |
| 850 | |
| 851 | return {MetaResult::Rerequest, ""}; |
| 852 | } |
| 853 | |
| 854 | void init_meta(bool first_run) |
| 855 | { |
| 856 | if (first_run) { |
| 857 | stash_register(std::make_unique<NotesStasher>()); |
| 858 | } |
| 859 | } |