How to replace the lua interpreter in Script. NET with the Lua dynamic library

Source: Internet
Author: User

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:

 
 
  1. /* Set lua library path * add by blueant **/
  2. LUALIB_API void (luaL_setlibpath) (lua_State * L, const char * path );
  3. Add by blueant is added in the modified Code in lualib. c ):
  4. Static char g_lualibpath [MAX_PATH + 1];
  5. /* Set lua library path * add by blueant **/
  6. LUALIB_API void luaL_setlibpath (lua_State * L, const char * path ){
  7. Memset (g_lualibpath, 0, MAX_PATH + 1 );
  8. Strcpy (g_lualibpath, path );
  9. }
  10.  
  11. # Undef setprogdir
  12. Static void setprogdir (lua_State * L ){
  13. Char buff [MAX_PATH + 1];
  14. Char * lb;
  15. DWORD nsize = sizeof (buff)/sizeof (char );
  16. DWORD n;
  17. /// // Add by blueant //////////////////// ///////
  18. HKEY hKey;
  19. LONG lRet;
  20. DWORD dwType = REG_SZ;
  21. DWORD dwSize = MAX_PATH;
  22. // First, determine whether the static variable g_lualibpath is set for the repository path. If so, use this variable.
  23. If (strlen (g_lualibpath)> 0)
  24. {
  25. LuaL_gsub (L, lua_tostring (L,-1), LUA_EXECDIR, g_lualibpath );
  26. Lua_remove (L,-2);/* remove original string */
  27. Return;
  28. }
  29. // Otherwise, find the installation path of Script. NET in the registry.
  30. LRet = RegOpenKeyExA (HKEY_CURRENT_USER,
  31. "Software \ Blueant \ Script. NET \ 2.0 \ Settings", 0, KEY_READ | KEY_QUERY_VALUE, & hKey );
  32. If (lRet = ERROR_SUCCESS)
  33. {
  34. Memset (buff, 0, MAX_PATH + 1 );
  35. LRet = RegQueryValueExA (hKey, "Install", NULL, & dwType, (LPBYTE) buff, & dwSize );
  36. RegCloseKey (hKey );
  37. }
  38. If (lRet = ERROR_SUCCESS)
  39. {
  40. Strcat (buff, "Interps \ Lua \ bin ");
  41. LuaL_gsub (L, lua_tostring (L,-1), LUA_EXECDIR, buff );
  42. Lua_remove (L,-2);/* remove original string */
  43. Return;
  44. }
  45. //////////////////////////////////////// /////////////////
  46. N = GetModuleFileNameA (NULL, buff, nsize );
  47. If (n = 0 | n = nsize | (lb = strrchr (buff, '\') = NULL)
  48. LuaL_error (L, "unable to get ModuleFileName ");
  49. Else {
  50. * Lb = '\ 0 ';
  51. LuaL_gsub (L, lua_tostring (L,-1), LUA_EXECDIR, buff );
  52. Lua_remove (L,-2);/* remove original string */
  53. }
  54. }
  55.  
  56. Static void pusherror (lua_State * L ){
  57. Int error = GetLastError ();
  58. Char buffer [128];
  59. If (FormatMessageA (FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
  60. NULL, error, 0, buffer, sizeof (buffer), NULL ))
  61. Lua_pushstring (L, buffer );
  62. Else
  63. Lua_pushfstring (L, "system error % d \ n", error );
  64. }
  65. Static void ll_unloadlib (void * lib ){
  66. FreeLibrary (HINSTANCE) lib );
  67. }
  68.  
  69. Static void * ll_load (lua_State * L, const char * path ){
  70. HINSTANCE lib = NULL;
  71. /// // Add by blueant //////////////////// ///////
  72. // Record the current path and set the current path
  73. Char szOldPath [MAX_PATH + 1];
  74. Char szdrive [MAX_PATH + 1];
  75. Char szdir [MAX_PATH + 1];
  76. DWORD dwLen = 0;
  77. Memset (szOldPath, 0, MAX_PATH + 1 );
  78. DwLen = GetCurrentDirectory (255, szOldPath );
  79. Memset (szdrive, 0, MAX_PATH + 1 );
  80. Memset (szdir, 0, MAX_PATH + 1 );
  81. _ Splitpath (path, szdrive, szdir, NULL, NULL );
  82. Strcat (szdrive, szdir );
  83. SetCurrentDirectory (szdrive );
  84. //////////////////////////////////////// //////////////////
  85. Lib = LoadLibraryA (path );
  86. /// // Add by blueant ///////////////////// ///////
  87. SetCurrentDirectory (szOldPath );
  88. //////////////////////////////////////// //////////////////
  89. If (lib = NULL) pusherror (L );
  90. Return lib;
  91. }

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.