Debian Codegraph @

CorsixTH/Src/th_lua_iso.cpp

raw
1 /*
2 Copyright (c) 2019,2025 Stephen "TheCycoONE" Baker
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #include "config.h"
24
25 #include <stdexcept>
26 #include <string_view>
27
28 #include "iso_fs.h"
29 #include "lua.hpp"
30 #include "th_lua.h"
31 #include "th_lua_internal.h"
32
33 namespace {
34
35 /**
36 * Lua binding to construct a new iso_filesystem object.
37 *
38 * Expects the lua stack to contain:
39 * -# metatable for iso_fs
40 * -# path to .iso file
41 * -# (optional) path separator, defaults to '/'
42 *
43 * Returns on the lua stack:
44 * -# userdata (iso_filesystem) or nil on failure
45 * -# string (path) or nil on failure
46 * -# error message if the first return value is nil
47 *
48 * @param L Lua stack
49 * @return Number of return values
50 */
51 int l_isofs_new(lua_State* L) {
52 const char* path = luaL_checkstring(L, 2);
53 std::string_view sep = luaL_optstring(L, 3, "/");
54 if (sep.length() != 1) {
55 luaL_argerror(L, 3, "Path separator must be a single character");
56 }
57
58 try {
59 luaT_stdnew<iso_filesystem>(L, luaT_environindex, true, path, sep.at(0));
60 } catch (std::runtime_error& e) {
61 lua_pushnil(L);
62 lua_pushnil(L);
63 lua_pushfstring(L, "%s", e.what());
64 return 3;
65 }
66
67 // L
68 // 1 - table (metatable)
69 // 2 - path
70 // 3 - separator
71 // 4 - userdata (iso_filesystem)
72
73 lua_pushvalue(L, 2);
74 luaT_setenvfield(L, 4, "string");
75 lua_pushvalue(L, 2);
76 return 2;
77 }
78
79 int l_isofs_is_valid_root(lua_State* L) {
80 // Static method so we don't need an instance
81 try {
82 iso_filesystem fs(luaL_checkstring(L, 1));
83 lua_pushboolean(L, true);
84 } catch (std::runtime_error&) {
85 lua_pushboolean(L, false);
86 }
87 return 1;
88 }
89
90 int l_isofs_file_exists(lua_State* L) {
91 iso_filesystem* pSelf = luaT_testuserdata<iso_filesystem>(L);
92 const char* sFilename = luaL_checkstring(L, 2);
93 iso_filesystem::file_handle iFile = pSelf->find_file(sFilename);
94 if (!iso_filesystem::is_handle_good(iFile)) {
95 lua_pushnil(L);
96 lua_pushfstring(L, "Could not find \'%s\' in .iso image", sFilename);
97 return 2;
98 }
99 lua_pushboolean(L, true);
100 return 1;
101 }
102
103 int l_isofs_file_size(lua_State* L) {
104 iso_filesystem* pSelf = luaT_testuserdata<iso_filesystem>(L);
105 const char* sFilename = luaL_checkstring(L, 2);
106 iso_filesystem::file_handle iFile = pSelf->find_file(sFilename);
107 if (!iso_filesystem::is_handle_good(iFile)) {
108 lua_pushnil(L);
109 lua_pushfstring(L, "Could not find \'%s\' in .iso image", sFilename);
110 return 2;
111 }
112 lua_pushinteger(L, pSelf->get_file_size(iFile));
113 return 1;
114 }
115
116 //! Get the start and end bytes of a given file.
117 /*!
118 Called on an iso_filesystem, passing in a filename. If the file exists in
119 the iso then the byte position of the start of the file inclusive and the
120 end of the file exclusive is returned to the caller.
121
122 If the file is not found then an error is passed as the third return value.
123
124 \param L The Lua State
125 */
126 int l_isofs_file_offsets(lua_State* L) {
127 iso_filesystem* pSelf = luaT_testuserdata<iso_filesystem>(L);
128 const char* sFilename = luaL_checkstring(L, 2);
129 iso_filesystem::file_handle iFile = pSelf->find_file(sFilename);
130 if (!iso_filesystem::is_handle_good(iFile)) {
131 lua_pushnil(L);
132 lua_pushnil(L);
133 lua_pushfstring(L, "Could not find \'%s\' in .iso image", sFilename);
134 return 3;
135 }
136 uint32_t start = pSelf->get_file_start(iFile);
137 lua_pushinteger(L, start);
138 lua_pushinteger(L, start + pSelf->get_file_size(iFile));
139 return 2;
140 }
141
142 int l_isofs_read_contents(lua_State* L) {
143 iso_filesystem* pSelf = luaT_testuserdata<iso_filesystem>(L);
144 const char* sFilename = luaL_checkstring(L, 2);
145 iso_filesystem::file_handle iFile = pSelf->find_file(sFilename);
146 if (!iso_filesystem::is_handle_good(iFile)) {
147 lua_pushnil(L);
148 lua_pushfstring(L, "Could not find \'%s\' in .iso image", sFilename);
149 return 2;
150 }
151 void* pBuffer = lua_newuserdata(L, pSelf->get_file_size(iFile));
152 if (!pSelf->get_file_data(iFile, reinterpret_cast<uint8_t*>(pBuffer))) {
153 lua_pushnil(L);
154 lua_pushlstring(L, pSelf->get_error().data(), pSelf->get_error().size());
155 return 2;
156 }
157 lua_pushlstring(L, reinterpret_cast<char*>(pBuffer),
158 pSelf->get_file_size(iFile));
159 return 1;
160 }
161
162 void l_isofs_list_files_callback(void* p, const char* name, const char* path) {
163 lua_State* L = reinterpret_cast<lua_State*>(p);
164 lua_pushstring(L, name);
165 lua_pushstring(L, path);
166 lua_settable(L, 3);
167 }
168
169 int l_isofs_list_files(lua_State* L) {
170 iso_filesystem* pSelf = luaT_testuserdata<iso_filesystem>(L);
171 const char* sPath = luaL_checkstring(L, 2);
172 lua_settop(L, 2);
173 lua_newtable(L);
174 pSelf->visit_directory_files(sPath, l_isofs_list_files_callback, L);
175 return 1;
176 }
177
178 } // namespace
179
180 void lua_register_iso_fs(const lua_register_state* pState) {
181 lua_class_binding<iso_filesystem> lcb(pState, "iso_fs", l_isofs_new,
182 lua_metatable::iso_fs);
183 lcb.add_function(l_isofs_file_exists, "fileExists");
184 lcb.add_function(l_isofs_file_size, "fileSize");
185 lcb.add_function(l_isofs_file_offsets, "fileOffsets");
186 lcb.add_function(l_isofs_read_contents, "readContents");
187 lcb.add_function(l_isofs_list_files, "listFiles");
188 lcb.add_function(l_isofs_is_valid_root, "isValidRoot");
189 }