Allow embedding of binary resources.

This commit is contained in:
Tom van Dijck 2017-08-02 14:03:58 -07:00
parent bf1ea2445f
commit 9f33db3ba7
4 changed files with 40 additions and 16 deletions

View File

@ -144,7 +144,7 @@
{ {
"*.txt", "**.lua", "*.txt", "**.lua",
"src/**.h", "src/**.c", "src/**.h", "src/**.c",
"modules/**.h", "modules/**.c", "modules/**"
} }
excludes excludes

View File

@ -67,20 +67,24 @@
local function addScript(result, filename, name, data) local function addScript(result, filename, name, data)
if not data then if not data then
if _OPTIONS["bytecode"] then if not path.hasextension(filename, ".lua") then
verbosef("Compiling... " .. filename) data = loadScript(filename)
local output = path.replaceextension(filename, ".luac") else
local res, err = os.compile(filename, output); if _OPTIONS["bytecode"] then
if res ~= nil then verbosef("Compiling... " .. filename)
data = loadScript(output) local output = path.replaceextension(filename, ".luac")
os.remove(output) local res, err = os.compile(filename, output);
if res ~= nil then
data = loadScript(output)
os.remove(output)
else
print(err)
print("Embedding source instead.")
data = stripScript(loadScript(filename))
end
else else
print(err)
print("Embedding source instead.")
data = stripScript(loadScript(filename)) data = stripScript(loadScript(filename))
end end
else
data = stripScript(loadScript(filename))
end end
end end

View File

@ -92,6 +92,11 @@ static const luaL_Reg os_functions[] = {
{ NULL, NULL } { NULL, NULL }
}; };
static const luaL_Reg premake_functions[] = {
{ "getEmbeddedResource", premake_getEmbeddedResource },
{ NULL, NULL }
};
static const luaL_Reg string_functions[] = { static const luaL_Reg string_functions[] = {
{ "endswith", string_endswith }, { "endswith", string_endswith },
{ "hash", string_hash }, { "hash", string_hash },
@ -168,6 +173,7 @@ int premake_init(lua_State* L)
{ {
const char* value; const char* value;
luaL_register(L, "premake", premake_functions);
luaL_register(L, "criteria", criteria_functions); luaL_register(L, "criteria", criteria_functions);
luaL_register(L, "debug", debug_functions); luaL_register(L, "debug", debug_functions);
luaL_register(L, "path", path_functions); luaL_register(L, "path", path_functions);
@ -215,10 +221,6 @@ int premake_init(lua_State* L)
os_getcwd(L); os_getcwd(L);
lua_setglobal(L, "_WORKING_DIR"); lua_setglobal(L, "_WORKING_DIR");
/* start the premake namespace */
lua_newtable(L);
lua_setglobal(L, "premake");
#if !defined(PREMAKE_NO_BUILTIN_SCRIPTS) #if !defined(PREMAKE_NO_BUILTIN_SCRIPTS)
/* let native modules initialize themselves */ /* let native modules initialize themselves */
registerModules(L); registerModules(L);
@ -650,3 +652,20 @@ int premake_load_embedded_script(lua_State* L, const char* filename)
/* Load the chunk */ /* Load the chunk */
return luaL_loadbuffer(L, (const char*)chunk->bytecode, chunk->length, filename); return luaL_loadbuffer(L, (const char*)chunk->bytecode, chunk->length, filename);
} }
/**
* Give the lua runtime raw access to embedded files.
*/
int premake_getEmbeddedResource(lua_State* L)
{
const char* filename = luaL_checkstring(L, 1);
const buildin_mapping* chunk = premake_find_embedded_script(filename);
if (chunk == NULL)
{
return 0;
}
lua_pushlstring(L, (const char*)chunk->bytecode, chunk->length);
return 1;
}

View File

@ -143,6 +143,7 @@ int os_uuid(lua_State* L);
int os_writefile_ifnotequal(lua_State* L); int os_writefile_ifnotequal(lua_State* L);
int os_touchfile(lua_State* L); int os_touchfile(lua_State* L);
int os_compile(lua_State* L); int os_compile(lua_State* L);
int premake_getEmbeddedResource(lua_State* L);
int string_endswith(lua_State* L); int string_endswith(lua_State* L);
int string_hash(lua_State* L); int string_hash(lua_State* L);
int string_sha1(lua_State* L); int string_sha1(lua_State* L);