MCPcopy Create free account
hub / github.com/defold/defold / Sys_Load

Function Sys_Load

engine/script/src/script_sys.cpp:246–276  ·  view source on GitHub ↗

loads a lua table from a file on disk * If the file exists, it must have been created by sys.save to be loaded. * This function will raise a Lua error if an error occurs while loading the file. * * @name sys.load * @param filename [type:string] file to read from * @return loaded [type:table] lua table, which is empty if the file could not be found *

Source from the content-addressed store, hash-verified

244 * ```
245 */
246 static int Sys_Load(lua_State* L)
247 {
248 const char* filename = luaL_checkstring(L, 1);
249 FILE* file = fopen(filename, "rb");
250 if (file == 0x0)
251 {
252 lua_newtable(L);
253 return 1;
254 }
255
256 fseek(file, 0L, SEEK_END);
257 uint32_t file_size = ftell(file);
258 fseek(file, 0L, SEEK_SET);
259
260 char* buffer = Sys_SetupTableSerializationBuffer(file_size);
261 if (!buffer)
262 {
263 return luaL_error(L, "Could not allocate %d bytes for table deserialization.", file_size);
264 }
265 size_t nread = fread(buffer, 1, file_size, file);
266 bool result = ferror(file) == 0;
267 fclose(file);
268 if (!result)
269 {
270 Sys_FreeTableSerializationBuffer(buffer);
271 return luaL_error(L, "Could not read from the file %s.", filename);
272 }
273 PushTable(L, buffer, nread);
274 Sys_FreeTableSerializationBuffer(buffer);
275 return 1;
276 }
277
278 /*# check if a path exists
279 * Check if a path exists

Callers

nothing calls this directly

Calls 4

luaL_errorFunction · 0.85
PushTableFunction · 0.85

Tested by

no test coverage detected