UseLua dynamic libraryReplaceScript. NETMediumLua interpreterThe method is described in this article,Script. NETSupportedLuaEdit and debug the script.Lua interpreterPlug-ins are built in Lua5.1.2LuaDynamic library, but if the correspondingLua dynamic libraryReplacing the dynamic library cannot be run becauseScript. NETUsedLuaA function is added to the library to set the lua library path, Script. NET Lua interpreter plug-in calls this function to set the lua library path to Script.. NET.
In some cases, if you want to replaceLua dynamic libraryFor example, if you have modified the lua dynamic library to implement special functions, you can refer to the following method, that is, Script.. NET modified code is transplanted to the custom lua library to recompile the dynamic library.
Add the following code to lualib. h:
- /* Set lua library path * add by blueant **/
- LUALIB_API void (luaL_setlibpath) (lua_State * L, const char * path );
- Add by blueant is added in the modified Code in lualib. c ):
- Static char g_lualibpath [MAX_PATH + 1];
- /* Set lua library path * add by blueant **/
- LUALIB_API void luaL_setlibpath (lua_State * L, const char * path ){
- Memset (g_lualibpath, 0, MAX_PATH + 1 );
- Strcpy (g_lualibpath, path );
- }
-
- # Undef setprogdir
- Static void setprogdir (lua_State * L ){
- Char buff [MAX_PATH + 1];
- Char * lb;
- DWORD nsize = sizeof (buff)/sizeof (char );
- DWORD n;
- /// // Add by blueant //////////////////// ///////
- HKEY hKey;
- LONG lRet;
- DWORD dwType = REG_SZ;
- DWORD dwSize = MAX_PATH;
- // First, determine whether the static variable g_lualibpath is set for the repository path. If so, use this variable.
- If (strlen (g_lualibpath)> 0)
- {
- LuaL_gsub (L, lua_tostring (L,-1), LUA_EXECDIR, g_lualibpath );
- Lua_remove (L,-2);/* remove original string */
- Return;
- }
- // Otherwise, find the installation path of Script. NET in the registry.
- LRet = RegOpenKeyExA (HKEY_CURRENT_USER,
- "Software \ Blueant \ Script. NET \ 2.0 \ Settings", 0, KEY_READ | KEY_QUERY_VALUE, & hKey );
- If (lRet = ERROR_SUCCESS)
- {
- Memset (buff, 0, MAX_PATH + 1 );
- LRet = RegQueryValueExA (hKey, "Install", NULL, & dwType, (LPBYTE) buff, & dwSize );
- RegCloseKey (hKey );
- }
- If (lRet = ERROR_SUCCESS)
- {
- Strcat (buff, "Interps \ Lua \ bin ");
- LuaL_gsub (L, lua_tostring (L,-1), LUA_EXECDIR, buff );
- Lua_remove (L,-2);/* remove original string */
- Return;
- }
- //////////////////////////////////////// /////////////////
- N = GetModuleFileNameA (NULL, buff, nsize );
- If (n = 0 | n = nsize | (lb = strrchr (buff, '\') = NULL)
- LuaL_error (L, "unable to get ModuleFileName ");
- Else {
- * Lb = '\ 0 ';
- LuaL_gsub (L, lua_tostring (L,-1), LUA_EXECDIR, buff );
- Lua_remove (L,-2);/* remove original string */
- }
- }
-
- Static void pusherror (lua_State * L ){
- Int error = GetLastError ();
- Char buffer [128];
- If (FormatMessageA (FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
- NULL, error, 0, buffer, sizeof (buffer), NULL ))
- Lua_pushstring (L, buffer );
- Else
- Lua_pushfstring (L, "system error % d \ n", error );
- }
- Static void ll_unloadlib (void * lib ){
- FreeLibrary (HINSTANCE) lib );
- }
-
- Static void * ll_load (lua_State * L, const char * path ){
- HINSTANCE lib = NULL;
- /// // Add by blueant //////////////////// ///////
- // Record the current path and set the current path
- Char szOldPath [MAX_PATH + 1];
- Char szdrive [MAX_PATH + 1];
- Char szdir [MAX_PATH + 1];
- DWORD dwLen = 0;
- Memset (szOldPath, 0, MAX_PATH + 1 );
- DwLen = GetCurrentDirectory (255, szOldPath );
- Memset (szdrive, 0, MAX_PATH + 1 );
- Memset (szdir, 0, MAX_PATH + 1 );
- _ Splitpath (path, szdrive, szdir, NULL, NULL );
- Strcat (szdrive, szdir );
- SetCurrentDirectory (szdrive );
- //////////////////////////////////////// //////////////////
- Lib = LoadLibraryA (path );
- /// // Add by blueant ///////////////////// ///////
- SetCurrentDirectory (szOldPath );
- //////////////////////////////////////// //////////////////
- If (lib = NULL) pusherror (L );
- Return lib;
- }
Summary: UseLua dynamic libraryThe content of the method used to replace the Lua interpreter in Script. NET has been introduced. I hope this article will be helpful to you!