| 1 | // Copyright (C) 2022 by Chris Spiegel |
| 2 | // |
| 3 | // This file is part of Gargoyle. |
| 4 | // |
| 5 | // Gargoyle is free software; you can redistribute it and/or modify |
| 6 | // it under the terms of the GNU General Public License as published by |
| 7 | // the Free Software Foundation; either version 2 of the License, or |
| 8 | // (at your option) any later version. |
| 9 | // |
| 10 | // Gargoyle is distributed in the hope that it will be useful, |
| 11 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | // GNU General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with Gargoyle; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | |
| 19 | #include <cstdlib> |
| 20 | #include <fstream> |
| 21 | #include <ios> |
| 22 | #include <map> |
| 23 | #include <stdexcept> |
| 24 | #include <string> |
| 25 | #include <tuple> |
| 26 | #include <utility> |
| 27 | |
| 28 | #if __cplusplus >= 201703L |
| 29 | #include <filesystem> |
| 30 | #endif |
| 31 | |
| 32 | #include "format.h" |
| 33 | |
| 34 | #include "garglk.h" |
| 35 | |
| 36 | std::string gli_program_name = "Unknown"; |
| 37 | std::string gli_program_info; |
| 38 | std::string gli_story_name; |
| 39 | std::string gli_story_title; |
| 40 | |
| 41 | bool gli_exiting = false; |
| 42 | |
| 43 | void garglk_set_program_name(const char *name) |
| 44 | { |
| 45 | gli_program_name = name; |
| 46 | wintitle(); |
| 47 | } |
| 48 | |
| 49 | void garglk_set_program_info(const char *info) |
| 50 | { |
| 51 | gli_program_info = info; |
| 52 | } |
| 53 | |
| 54 | void garglk_set_story_name(const char *name) |
| 55 | { |
| 56 | gli_story_name = name; |
| 57 | wintitle(); |
| 58 | } |
| 59 | |
| 60 | void garglk_set_story_title(const char *title) |
| 61 | { |
| 62 | gli_story_title = title; |
| 63 | wintitle(); |
| 64 | } |
| 65 | |
| 66 | int garglk_tads_os_banner_size(winid_t win) |
| 67 | { |
| 68 | window_textbuffer_t *dwin = win->winbuffer(); |
| 69 | int size = dwin->scrollmax; |
| 70 | if (dwin->numchars != 0) { |
| 71 | size++; |
| 72 | } |
| 73 | return size; |
| 74 | } |
| 75 | |
| 76 | // All normal program termination should go through here instead of |
| 77 | // directly calling std::exit() to ensure that gli_exiting is properly |
| 78 | // set. Some code in destructors needs to be careful what it's doing |
| 79 | // during program exit, and uses this flag to check that. |
| 80 | void gli_exit(int status) |
| 81 | { |
| 82 | gli_exiting = true; |
| 83 | std::exit(status); |
| 84 | } |
| 85 | |
| 86 | bool garglk::read_file(const std::string &filename, std::vector<unsigned char> &buf) |
| 87 | { |
| 88 | std::ifstream f(filename, std::ios::binary); |
| 89 | if (!f.is_open()) { |
| 90 | return false; |
| 91 | } |
| 92 | |
| 93 | try { |
| 94 | buf.assign((std::istreambuf_iterator<char>(f)), std::istreambuf_iterator<char>()); |
| 95 | } catch (const std::bad_alloc &) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | return !f.fail(); |
| 100 | } |
| 101 | |
| 102 | void garglk_window_get_size_pixels(window_t *win, glui32 *width, glui32 *height) |
| 103 | { |
| 104 | glui32 wid; |
| 105 | glui32 hgt; |
| 106 | |
| 107 | // glk_window_get_size() already does what we need for blank, pair, |
| 108 | // and graphics windows. |
| 109 | glk_window_get_size(win, &wid, &hgt); |
| 110 | |
| 111 | // For text windows, the size should be reported in pixels, and |
| 112 | // since glk_window_get_size() does integer division, this can't |
| 113 | // just multiply by gli_cellw/gli_cellh; calculate directly. |
| 114 | if (win->type == wintype_TextGrid) { |
| 115 | wid = win->bbox.x1 - win->bbox.x0; |
| 116 | hgt = win->bbox.y1 - win->bbox.y0; |
| 117 | } else if (win->type == wintype_TextBuffer) { |
| 118 | wid = win->bbox.x1 - win->bbox.x0; |
| 119 | wid -= std::min<glui32>(wid, gli_tmarginx * 2); |
| 120 | hgt = win->bbox.y1 - win->bbox.y0; |
| 121 | hgt -= std::min<glui32>(hgt, gli_tmarginy * 2); |
| 122 | } |
| 123 | |
| 124 | if (width != nullptr) { |
| 125 | *width = wid / gli_zoom; |
| 126 | } |
| 127 | |
| 128 | if (height != nullptr) { |
| 129 | *height = hgt / gli_zoom; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // Map tuples of (usage, filename, offset) to IDs |
| 134 | static std::map<std::tuple<glui32, std::string, glui32>, glui32> resource_ids; |
| 135 | |
| 136 | // Map IDs to chunks loaded from files |
| 137 | static std::map<glui32, std::map<glui32, std::vector<unsigned char>>> resource_maps; |
| 138 | |
| 139 | // Insert a data chunk into the specified resource map, returning a resource ID |
| 140 | static glui32 gli_insert_resource(glui32 usage, std::vector<unsigned char> data) |
| 141 | { |
| 142 | auto &map = resource_maps[usage]; |
| 143 | |
| 144 | glui32 id = 1; |
| 145 | if (!map.empty()) { |
| 146 | id = map.rbegin()->first + 1; |
| 147 | } |
| 148 | |
| 149 | map.insert({id, std::move(data)}); |
| 150 | |
| 151 | return id; |
| 152 | } |
| 153 | |
| 154 | const std::map<glui32, std::vector<unsigned char>> &gli_get_resource_map(glui32 usage) |
| 155 | { |
| 156 | return resource_maps[usage]; |
| 157 | } |
| 158 | |
| 159 | glui32 garglk_add_resource_from_file(glui32 usage, const char *filename_, glui32 offset, glui32 len) |
| 160 | { |
| 161 | std::string filename(filename_); |
| 162 | |
| 163 | #if __cplusplus >= 201703L |
| 164 | if (std::filesystem::path(filename).has_parent_path()) { |
| 165 | return 0; |
| 166 | } |
| 167 | #else |
| 168 | #ifdef _WIN32 |
| 169 | std::string sep = "/\\"; |
| 170 | #else |
| 171 | std::string sep = "/"; |
| 172 | #endif |
| 173 | |
| 174 | if (filename.find_first_of(sep) != std::string::npos) { |
| 175 | return 0; |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | #ifdef _WIN32 |
| 180 | static const std::vector<std::string> reserved_names = { |
| 181 | "con", "prn", "aux", "nul", "com1", "com2", "com3", "com4", |
| 182 | "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", |
| 183 | "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", |
| 184 | }; |
| 185 | |
| 186 | auto lower = garglk::downcase(filename); |
| 187 | for (const auto &reserved_name : reserved_names) { |
| 188 | if (lower == reserved_name || lower.find(reserved_name + ".") == 0) { |
| 189 | return 0; |
| 190 | } |
| 191 | } |
| 192 | #endif |
| 193 | |
| 194 | auto key = std::make_tuple(usage, filename, offset); |
| 195 | |
| 196 | try { |
| 197 | return resource_ids.at(key); |
| 198 | } catch (const std::out_of_range &) { |
| 199 | } |
| 200 | |
| 201 | filename = Format("{}/{}", gli_workdir, filename_); |
| 202 | |
| 203 | std::ifstream f(filename, std::ios::binary); |
| 204 | if (!f.is_open()) { |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | if (!f.seekg(offset, std::ios::beg)) { |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | // vector resize/map insert can throw memory-related exceptions. |
| 213 | try { |
| 214 | std::vector<unsigned char> data(len); |
| 215 | if (!f.read(reinterpret_cast<char *>(data.data()), len)) { |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | auto id = gli_insert_resource(usage, std::move(data)); |
| 220 | |
| 221 | resource_ids.insert({key, id}); |
| 222 | |
| 223 | return id; |
| 224 | } catch (...) { |
| 225 | return 0; |
| 226 | } |
| 227 | } |