diff options
-rw-r--r-- | lua/src/modules/w32resembed.c | 110 | ||||
-rw-r--r-- | lua/src/modules/w32resembed.h | 17 | ||||
-rw-r--r-- | sandbox/luaconf/lua_conf.cpp | 136 | ||||
-rw-r--r-- | sandbox/luaconf/lua_conf.lua | 41 | ||||
-rw-r--r-- | sandbox/luaconf/lua_conf.rc | 3 | ||||
-rw-r--r-- | sandbox/luaconf/lua_conf.vcproj | 4 |
6 files changed, 220 insertions, 91 deletions
diff --git a/lua/src/modules/w32resembed.c b/lua/src/modules/w32resembed.c new file mode 100644 index 0000000..3e0c235 --- /dev/null +++ b/lua/src/modules/w32resembed.c @@ -0,0 +1,110 @@ +///////////////////////////////////////////////////////////////////////////////
+///
+/// Written 2011-2012, Oliver Schneider (assarbad.net) - PUBLIC DOMAIN/CC0
+///
+/// Original filename: w32embed.h
+/// Project : WinDirStat
+/// Author(s) : Oliver Schneider
+///
+/// Purpose : Lua package to load Lua code from the resource section
+/// of a PE file.
+///
+///////////////////////////////////////////////////////////////////////////////
+
+#include <Windows.h>
+#include <tchar.h>
+#include <lua.h>
+#include "w32resembed.h"
+
+#define WINRES_MODNAME "winres"
+
+// Forward declaration
+static int c_load_chunk_from_resources(lua_State* L);
+
+static BOOL CALLBACK enumLuaScriptsLanguageCallback(HANDLE hModule, LPCTSTR lpszType, LPCTSTR lpszName, WORD wIDLanguage, lua_State* L)
+{
+ UNREFERENCED_PARAMETER(hModule);
+ UNREFERENCED_PARAMETER(lpszName);
+ if((0 == lstrcmp(RT_LUASCRIPT,lpszType)) && (LANG_NEUTRAL == PRIMARYLANGID(wIDLanguage)))
+ {
+ lua_pushtstring(L, lpszName);
+ lua_pushcfunction(L, c_load_chunk_from_resources);
+ lua_rawset(L, -3);
+ }
+ return TRUE;
+}
+
+static BOOL CALLBACK enumLuaScriptsNameCallback(HANDLE hModule, LPCTSTR lpszType, LPCTSTR lpszName, lua_State* L)
+{
+ UNREFERENCED_PARAMETER(hModule);
+ // First string table entry that we encounter will be grabbed
+ if(0 == lstrcmp(RT_LUASCRIPT,lpszType))
+ {
+ // Now enumerate the languages of this entry ...
+ EnumResourceLanguages((HMODULE)hModule, lpszType, lpszName, (ENUMRESLANGPROC)enumLuaScriptsLanguageCallback, (LONG_PTR)L);
+ }
+ return TRUE;
+}
+
+static BOOL getResourcePointer(HINSTANCE Instance, LPCTSTR ResName, LPCTSTR ResType, LPVOID* ppRes, DWORD* pdwResSize)
+{
+ if(ppRes && pdwResSize)
+ {
+ HRSRC hRsrc;
+ if(hRsrc = FindResource((HMODULE)Instance, ResName, ResType))
+ {
+ HGLOBAL hGlob;
+ if(hGlob = LoadResource(Instance, hRsrc))
+ {
+ *ppRes = LockResource(hGlob);
+ *pdwResSize = SizeofResource(Instance, hRsrc);
+ return (*ppRes && *pdwResSize);
+ }
+ }
+ }
+ return FALSE;
+}
+
+static HMODULE getMyModuleHandle()
+{
+ static int s_somevar = 0;
+ MEMORY_BASIC_INFORMATION mbi;
+ if(!VirtualQuery(&s_somevar, &mbi, sizeof(mbi)))
+ {
+ return NULL;
+ }
+ return (HMODULE)mbi.AllocationBase;
+}
+
+void enumerateEmbeddedLuaScripts(lua_State* L)
+{
+ lua_newtable(L);
+ lua_pushstring(L, "scripts");
+ lua_newtable(L);
+ EnumResourceNames(getMyModuleHandle(), RT_LUASCRIPT, (ENUMRESNAMEPROC)enumLuaScriptsNameCallback, (LONG_PTR)L);
+ lua_rawset(L, -3);
+ lua_setglobal(L, WINRES_MODNAME);
+}
+
+static const char* callback_resource_lua_Reader(lua_State* L, void* data, size_t* size)
+{
+ const char* retval = NULL;
+ DWORD retsize = 0;
+ LPCTSTR resName = lua_totstring(L, 1);
+ if(getResourcePointer(getMyModuleHandle(), resName, RT_LUASCRIPT, ((LPVOID*)&retval), &retsize))
+ {
+ *size = (size_t)retsize;
+ }
+ else
+ {
+ *size = 0;
+ retval = NULL;
+ }
+ return retval;
+}
+
+static int c_load_chunk_from_resources(lua_State* L)
+{
+ const char* ref_name = lua_tostring(L, 1);
+ return lua_load(L, callback_resource_lua_Reader, NULL, ref_name);
+}
diff --git a/lua/src/modules/w32resembed.h b/lua/src/modules/w32resembed.h new file mode 100644 index 0000000..4dfa79c --- /dev/null +++ b/lua/src/modules/w32resembed.h @@ -0,0 +1,17 @@ +#ifndef __W32RESEMBED_H_VER__
+#define __W32RESEMBED_H_VER__ 2012110900
+// $Id$
+#if (defined(_MSC_VER) && (_MSC_VER >= 1020)) || defined(__MCPP)
+#pragma once
+#endif // Check for "#pragma once" support
+
+#ifdef RC_INVOKED
+# include <WinNT.rh>
+# define RT_LUASCRIPT "LUA"
+#else
+# define RT_LUASCRIPT TEXT("LUA")
+
+EXTERN_C void enumerateEmbeddedLuaScripts(lua_State* L);
+#endif
+
+#endif // __W32RESEMBED_H_VER__
diff --git a/sandbox/luaconf/lua_conf.cpp b/sandbox/luaconf/lua_conf.cpp index a385a14..5f9de14 100644 --- a/sandbox/luaconf/lua_conf.cpp +++ b/sandbox/luaconf/lua_conf.cpp @@ -3,6 +3,7 @@ #include "stdafx.h"
#include "lua_conf.h"
+#include "modules/w32resembed.h"
#ifdef _DEBUG
#define new DEBUG_NEW
@@ -10,61 +11,61 @@ namespace
{
- static int traceback (lua_State *L) { - if (!lua_isstring(L, 1)) /* 'message' not a string? */ - return 1; /* keep it intact */ - lua_getfield(L, LUA_GLOBALSINDEX, "debug"); - if (!lua_istable(L, -1)) { - lua_pop(L, 1); - return 1; - } - lua_getfield(L, -1, "traceback"); - if (!lua_isfunction(L, -1)) { - lua_pop(L, 2); - return 1; - } - lua_pushvalue(L, 1); /* pass error message */ - lua_pushinteger(L, 2); /* skip this function and traceback */ - lua_call(L, 2, 1); /* call debug.traceback */ - return 1; - } - - static void l_message (const char *pname, const char *msg) - { - if (pname) fprintf(stderr, "%s: ", pname); - fprintf(stderr, "%s\n", msg); - fflush(stderr); - } - - - static int report (lua_State *L, int status) - { - if (status && !lua_isnil(L, -1)) { - const char *msg = lua_tostring(L, -1); - if (msg == NULL) msg = "(error object is not a string)"; - l_message(__FILE__, msg); - lua_pop(L, 1); - } - return status; - } - - static int docall (lua_State *L, int narg, int clear) - { - int status; - int base = lua_gettop(L) - narg; /* function index */ - lua_pushcfunction(L, traceback); /* push traceback function */ - lua_insert(L, base); /* put it under chunk and args */ - status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base); - lua_remove(L, base); /* remove traceback function */ - /* force a complete garbage collection in case of errors */ - if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0); - return status; - } + static int traceback (lua_State *L) {
+ if (!lua_isstring(L, 1)) /* 'message' not a string? */
+ return 1; /* keep it intact */
+ lua_getfield(L, LUA_GLOBALSINDEX, "debug");
+ if (!lua_istable(L, -1)) {
+ lua_pop(L, 1);
+ return 1;
+ }
+ lua_getfield(L, -1, "traceback");
+ if (!lua_isfunction(L, -1)) {
+ lua_pop(L, 2);
+ return 1;
+ }
+ lua_pushvalue(L, 1); /* pass error message */
+ lua_pushinteger(L, 2); /* skip this function and traceback */
+ lua_call(L, 2, 1); /* call debug.traceback */
+ return 1;
+ }
+
+ static void l_message (const char *pname, const char *msg)
+ {
+ if (pname) fprintf(stderr, "%s: ", pname);
+ fprintf(stderr, "%s\n", msg);
+ fflush(stderr);
+ }
+
+
+ static int report (lua_State *L, int status)
+ {
+ if (status && !lua_isnil(L, -1)) {
+ const char *msg = lua_tostring(L, -1);
+ if (msg == NULL) msg = "(error object is not a string)";
+ l_message(__FILE__, msg);
+ lua_pop(L, 1);
+ }
+ return status;
+ }
+
+ static int docall (lua_State *L, int narg, int clear)
+ {
+ int status;
+ int base = lua_gettop(L) - narg; /* function index */
+ lua_pushcfunction(L, traceback); /* push traceback function */
+ lua_insert(L, base); /* put it under chunk and args */
+ status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
+ lua_remove(L, base); /* remove traceback function */
+ /* force a complete garbage collection in case of errors */
+ if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
+ return status;
+ }
- static int dostring (lua_State *L, const char *s) { - const char *name = "dostring"; - int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1); - return report(L, status); + static int dostring (lua_State *L, const char *s) {
+ const char *name = "dostring";
+ int status = luaL_loadbuffer(L, s, strlen(s), name) || docall(L, 0, 1);
+ return report(L, status);
}
}
@@ -75,33 +76,24 @@ int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) lua_State* L = luaWDS_open();
if(L)
{
- const char* _dostring = "function dumptable(T,t)\n\
- print '--------------------------'\n\
- print(T)\n\
- print '--------------------------'\n\
- for k,v in pairs(t)do print(k,v) end\n\
- end\n\
- \n\
- dumptable('package', package)\n\
- dumptable('package.preload', package.preload)\n\
- dumptable('package.loaded', package.loaded)\n\
- dumptable('package.loaders', package.loaders)\n\
- dumptable('package.loaded._G', package.loaded._G)\n\
- print '--------------------------'\n\
- print('_G = ', _G)\n\
- print('package.loaded._G = ', package.loaded._G)\n\
- ";
- dostring(L, _dostring);
- fprintf(stderr, "--------------------------\n");
+ int stackTop = lua_gettop(L);
+ fprintf(stderr, "[STACK TOP] %i (line %i)\n", stackTop, __LINE__);
+ enumerateEmbeddedLuaScripts(L);
+ fprintf(stderr, "[STACK TOP] %i (line %i)\n", stackTop, __LINE__);
int ret = luaL_dofile(L, "..\\lua_conf.lua");
+ fprintf(stderr, "[STACK TOP] %i (line %i)\n", stackTop, __LINE__);
if(ret)
{
fprintf(stderr, "%s", lua_tostring(L, -1));
lua_pop(L, 1); /* pop error message from the stack */
+ fprintf(stderr, "[STACK TOP] %i (line %i)\n", stackTop, __LINE__);
lua_close(L);
+ fprintf(stderr, "[STACK TOP] %i (line %i)\n", stackTop, __LINE__);
return EXIT_FAILURE;
}
+ fprintf(stderr, "[STACK TOP] %i (line %i)\n", stackTop, __LINE__);
lua_close(L);
+ fprintf(stderr, "[STACK TOP] %i (line %i)\n", stackTop, __LINE__);
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
diff --git a/sandbox/luaconf/lua_conf.lua b/sandbox/luaconf/lua_conf.lua index c35e3cd..4538355 100644 --- a/sandbox/luaconf/lua_conf.lua +++ b/sandbox/luaconf/lua_conf.lua @@ -12,34 +12,37 @@ else print "I'm NOT a WOW64 process"
end
-print "-----------------"
+print '--------------------------'
function test_loader(...)
- print(...)
+ print("LOADER FUNC: ", ...)
local mod = {}
package.loaded[...] = mod
return mod
end
-
package.preload["mytest"] = test_loader
require "mytest"
-for k,v in pairs(package.loaded)do print(k,v) end
---[[
-local function load(modulename)
- local errmsg = ""
- -- Find source
- local modulepath = string.gsub(modulename, "%.", "/")
- for path in string.gmatch(package.path, "([^;]+)") do
- local filename = string.gsub(path, "%?", modulepath)
- local file = io.open(filename, "rb")
- if file then
- -- Compile and return the module
- return assert(loadstring(assert(file:read("*a")), filename))
+function dumptable(T,t)
+ print '--------------------------'
+ print(T)
+ print '--------------------------'
+ for k,v in pairs(t)do print(k,v) end
+end
+
+dumptable('package', package)
+dumptable('package.preload', package.preload)
+dumptable('package.loaded', package.loaded)
+dumptable('package.loaders', package.loaders)
+dumptable('package.loaded._G', package.loaded._G)
+dumptable('package.loaded.winreg', package.loaded.winreg)
+if winres then
+ dumptable('winres', winres)
+ if winres.scripts then
+ dumptable('winres.scripts', winres.scripts)
end
- errmsg = errmsg.."\n\tno file '"..filename.."' (checked with custom loader)"
- end
- return errmsg
end
-]]
\ No newline at end of file +print '--------------------------'
+print('_G = ', _G)
+print('package.loaded._G = ', package.loaded._G)
diff --git a/sandbox/luaconf/lua_conf.rc b/sandbox/luaconf/lua_conf.rc index 114c303..2856341 100644 --- a/sandbox/luaconf/lua_conf.rc +++ b/sandbox/luaconf/lua_conf.rc @@ -54,7 +54,10 @@ END #endif
/////////////////////////////////////////////////////////////////////////////
+#include "modules/w32resembed.h"
+LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
+lua_conf LUA "../../sandbox/luaconf/lua_conf.lua"
#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
diff --git a/sandbox/luaconf/lua_conf.vcproj b/sandbox/luaconf/lua_conf.vcproj index 92e017c..6e47726 100644 --- a/sandbox/luaconf/lua_conf.vcproj +++ b/sandbox/luaconf/lua_conf.vcproj @@ -62,6 +62,7 @@ <Tool
Name="VCResourceCompilerTool"
AdditionalOptions="/nologo"
+ AdditionalIncludeDirectories="$(ProjectDir)\..\..\lua\src"
/>
<Tool
Name="VCPreLinkEventTool"
@@ -144,6 +145,7 @@ <Tool
Name="VCResourceCompilerTool"
AdditionalOptions="/nologo"
+ AdditionalIncludeDirectories="$(ProjectDir)\..\..\lua\src"
/>
<Tool
Name="VCPreLinkEventTool"
@@ -223,6 +225,7 @@ <Tool
Name="VCResourceCompilerTool"
AdditionalOptions="/nologo"
+ AdditionalIncludeDirectories="$(ProjectDir)\..\..\lua\src"
/>
<Tool
Name="VCPreLinkEventTool"
@@ -305,6 +308,7 @@ <Tool
Name="VCResourceCompilerTool"
AdditionalOptions="/nologo"
+ AdditionalIncludeDirectories="$(ProjectDir)\..\..\lua\src"
/>
<Tool
Name="VCPreLinkEventTool"
|