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",
"src/**.h", "src/**.c",
"modules/**.h", "modules/**.c",
"modules/**"
}
excludes

View File

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

View File

@ -92,6 +92,11 @@ static const luaL_Reg os_functions[] = {
{ NULL, NULL }
};
static const luaL_Reg premake_functions[] = {
{ "getEmbeddedResource", premake_getEmbeddedResource },
{ NULL, NULL }
};
static const luaL_Reg string_functions[] = {
{ "endswith", string_endswith },
{ "hash", string_hash },
@ -168,6 +173,7 @@ int premake_init(lua_State* L)
{
const char* value;
luaL_register(L, "premake", premake_functions);
luaL_register(L, "criteria", criteria_functions);
luaL_register(L, "debug", debug_functions);
luaL_register(L, "path", path_functions);
@ -215,10 +221,6 @@ int premake_init(lua_State* L)
os_getcwd(L);
lua_setglobal(L, "_WORKING_DIR");
/* start the premake namespace */
lua_newtable(L);
lua_setglobal(L, "premake");
#if !defined(PREMAKE_NO_BUILTIN_SCRIPTS)
/* let native modules initialize themselves */
registerModules(L);
@ -650,3 +652,20 @@ int premake_load_embedded_script(lua_State* L, const char* filename)
/* Load the chunk */
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_touchfile(lua_State* L);
int os_compile(lua_State* L);
int premake_getEmbeddedResource(lua_State* L);
int string_endswith(lua_State* L);
int string_hash(lua_State* L);
int string_sha1(lua_State* L);