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

Function Load

engine/gamesys/src/gamesys/scripts/script_resource.cpp:389–421  ·  view source on GitHub ↗

load a resource * Loads the resource data for a specific resource. * * @name resource.load * * @param path [type:string] The path to the resource * @return buffer [type:buffer] Returns the buffer stored on disc * * @examples * * ```lua * -- read custom resource data into buffer * local buffer = resource.load("/resources/datafile") * ``` * * In order for the engine to include custom

Source from the content-addressed store, hash-verified

387 * ```
388 */
389static int Load(lua_State* L)
390{
391 int top = lua_gettop(L);
392 const char* name = luaL_checkstring(L, 1);
393
394 void* resource = 0;
395 uint32_t resourcesize = 0;
396 dmResource::Result r = dmResource::GetRaw(g_ResourceModule.m_Factory, name, &resource, &resourcesize);
397
398 if( r != dmResource::RESULT_OK )
399 {
400 assert(top == lua_gettop(L));
401 return ReportPathError(L, r, dmHashString64(name));
402 }
403
404 dmBuffer::StreamDeclaration streams_decl[] = {
405 {dmHashString64("data"), dmBuffer::VALUE_TYPE_UINT8, 1}
406 };
407
408 dmBuffer::HBuffer buffer = 0;
409 dmBuffer::Create(resourcesize, streams_decl, 1, &buffer);
410
411 uint8_t* data = 0;
412 uint32_t datasize = 0;
413 dmBuffer::GetBytes(buffer, (void**)&data, &datasize);
414
415 memcpy(data, resource, resourcesize);
416
417 dmScript::LuaHBuffer luabuf(buffer, dmScript::OWNER_LUA);
418 dmScript::PushBuffer(L, luabuf);
419 assert(top + 1 == lua_gettop(L));
420 return 1;
421}
422
423
424static int DoCheckError(lua_State* L, const char* attr_name, const char* expected_type)

Callers 5

Image_LoadFunction · 0.70
Image_LoadBufferFunction · 0.70
mainFunction · 0.50
SetUpMethod · 0.50
mainFunction · 0.50

Calls 7

lua_gettopFunction · 0.85
GetRawFunction · 0.85
ReportPathErrorFunction · 0.85
PushBufferFunction · 0.85
CreateFunction · 0.70
GetBytesFunction · 0.70
assertFunction · 0.50

Tested by 3

mainFunction · 0.40
SetUpMethod · 0.40
mainFunction · 0.40