unity - two or three things to communicate with skynet (sproto,crypt)

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:

Using sproto

  1. 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 \
  2. 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

  3. Add dll import interface, add in LuaDll.cs

    [DllImport(LUADLL, CallingConvention = CallingConvention.Cdecl)]
    public static extern int luaopen_sproto_core(IntPtr L);
  4. 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
  5. 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.

  1. 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

      1. 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

      1. Modify lua-crypt.c by removing the LUAMOD_API macro
  2. Then compile tolua.dll, discard it in unit and replace it

  3. 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);
  4. Modify the tolua-related c#source code to register crypt in the LuaClient.cs script

    protected virtual void OpenLibs()
    {
     ...
           luaState.OpenLibs(LuaDLL.luaopen_crypt);
    }
  5. Then test it and use it if there are no errors

    local crypt = require "crypt"
    local key = crypt.randomkey()
    print(key)
  6. Reference material: http://www.jianshu.com/p/3c5db3e6b4e7

Keywords: github Unity JSON git

Added by cnagra on Thu, 23 May 2019 19:39:06 +0300