Unity uses tolua and communicates with skynet, rpc uses sproto, and accesses the crypt encryption libraries used in the login process, modifying the tolua source to join both libraries
Only win64 platform chestnuts are picked up here
skynet does not specifically recommend using sproto and can switch to other methods such as json, pbc-lua, etc. Sproto is more convenient and efficient than both. See the sproto instructions for more details
Related git links:
tolua source: https://github.com/topameng/tolua_runtime
Edited tolua library: https://github.com/topameng/tolua (without sproto, if I remember correctly)
-
tolua's ugui infrastructure: https://github.com/jarjin/LuaFramework_UGUI (including sproto, but not up-to-date)
The framework ugui is not well integrated with lua. The encapsulated number should be a Lua class corresponding to a ui. It is also convenient to detect Lua leaks and instantiate multiple identical uis, which contain a global table with UI corresponding to a lua.
Using sproto
-
The tolua source code takes it and changes the sproto inside to a new one. The new sproto modifies build_win64.sh in the folder sproto.new
sproto/sproto.c \ sproto/lsproto.c \
Modify to
sproto.new/sproto.c \ sproto.new/lsproto.c \
Double-click build_win64.sh to compile the dll, path E:\tolua_rumtime\Plugins\x86_64tolua.dll, and drop it in unit, how to drop the DLL to see the official website documents
-
Add dll import interface, add in LuaDll.cs
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_sproto_core(IntPtr L);
-
Modify the tolua-related c#source code to register sproto in the LuaClient.cs script
protected virtual void OpenLibs() { ... luaState.OpenLibs(LuaDLL.luaopen_sproto_core); }
- Normally I will not modify this directly, override this method in custom subclasses, modify the third-party library as little as possible for future upgrades
You can then test it in lua, referencing the code in tolua's ugui framework.
Use crypt
Because the crypt library is used by the server (skynet) during the login process, two files lua-crypt.c and lsha1.c are extracted directly from Skynet and entered into tolua.
-
Extract lua-crypt.c and lsha1.c files from skynet, throw them into tolua_runtime source code, modify compiled file build_win64.sh, increase
crypt/lsha1.c \ crypt/lua-crypt.c \
-
Direct compilation then reports several link errors that need to be modified
- Modify lua-crypt.c:
LUALIB_API int luaopen_crypt(lua_State *L) { // Modify 0: Modify the macro LUAMOD_API(skynet built-in) to LUALIB_API, and the function modifies Wie luaopen_crypt, using require "crypt" (without adding skynet). //luaL_checkversion(L); //Modify 1:skynet uses lua5.3, with this function, tolua is 5.1.4, so kill static int init = 0; if (!init) { // Don't need call srandom more than once. init = 1 ; srand(time(NULL)); // Modify 2:srandom is not a standard c library, so switch to srand } luaL_Reg l[] = { { "hashkey", lhashkey }, { "randomkey", lrandomkey }, { "desencode", ldesencode }, { "desdecode", ldesdecode }, { "hexencode", ltohex }, { "hexdecode", lfromhex }, { "hmac64", lhmac64 }, { "hmac64_md5", lhmac64_md5 }, { "dhexchange", ldhexchange }, { "dhsecret", ldhsecret }, { "base64encode", lb64encode }, { "base64decode", lb64decode }, { "sha1", lsha1 }, { "hmac_sha1", lhmac_sha1 }, { "hmac_hash", lhmac_hash }, //{"xor_str", lxor_str}, //Modify 3: This function does not exist in lua5.1. It may be added to skynet or lua53. Dry it { NULL, NULL }, }; //luaL_newlib(L,l); //Modify 4, also a lua version issue #if LUA_VERSION_NUM < 502 luaL_register(L, "crypt", l); #else luaL_newlib(L, l); #endif return 1; }
The second modification to this file, also known as the random number function, is used in the lrandomkey function to change random to rand, also because random is not something in the c standard library. Reference: http://www.itguai.com/c/a4898411.html
- Modify lua-crypt.c by removing the LUAMOD_API macro
-
Then compile tolua.dll, discard it in unit and replace it
-
Register this library in tolua's c# code, similar to steps 3 or 4 of sproto above, adding dll import interface and LuaDll.cs
[DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)] public static extern int luaopen_crypt(IntPtr L);
-
Modify the tolua-related c#source code to register crypt in the LuaClient.cs script
protected virtual void OpenLibs() { ... luaState.OpenLibs(LuaDLL.luaopen_crypt); }
-
Then test it and use it if there are no errors
local crypt = require "crypt" local key = crypt.randomkey() print(key)
Reference material: http://www.jianshu.com/p/3c5db3e6b4e7