Esempio n. 1
0
    void LoadHotFixAssembly()
    {
        Appdomain = new AppDomain();
        _pdb      = null;

        byte[] dll;

#if UNITY_EDITOR
        //开发模式
        if (!AssetMgr.RuntimeMode)
        {
            if (File.Exists(DLLPath)) //直接读DLL
            {
                dll = DLLMgr.FileToByte(DLLPath);

                //模拟加密
                dll = CryptoHelper.AesEncrypt(dll, key);
            }
            else
            {
                Log.PrintError("DLL文件不存在");
                return;
            }

            //查看是否有PDB文件
            if (File.Exists(PdbPath) && usePdb &&
                (File.GetLastWriteTime(DLLPath) - File.GetLastWriteTime(PdbPath)).Seconds < 30)
            {
                _pdb = new MemoryStream(DLLMgr.FileToByte(PdbPath));
            }
        }
        else //真机模式解密加载
#endif
        {
            var dllFile = (TextAsset)AssetMgr.Load(DllName, typeof(TextAsset));
            if (dllFile == null)
            {
                return;
            }

            dll = dllFile.bytes;
        }

        var buffer = new byte[dll.Length];
        Array.Copy(dll, buffer, dll.Length);
        AssetMgr.Unload(DllName, true);

        try
        {
            //这里默认用分块解密,JStream
            _fs = new JStream(buffer, key);

            /*
             * 如果一定要直接解密然后不进行分块解密加载Dll,可以这样:
             * var original = CryptoHelper.AesDecrypt(dll.bytes, Key);
             * _fs = new JStream(original, Key);
             * _fs.Encrypted = false;
             */

            Appdomain.LoadAssembly(_fs, _pdb, new PdbReaderProvider());
        }
        catch (Exception e)
        {
            Log.PrintError("加载热更DLL错误:\n" + e);
            if (!usePdb)
            {
                Log.PrintError(
                    "加载热更DLL失败,请确保HotUpdateResources/Dll里面有HotUpdateScripts.bytes文件,并且Build Bundle后将DLC传入服务器");
                Log.PrintError("也有可能是密码不匹配或密码包含特殊字符导致的");
            }
            else
            {
                Log.PrintError("PDB不可用,可能是DLL和PDB版本不一致,可能DLL是Release,如果是Release出包,请取消UsePdb选项,本次已跳过使用PDB");
                usePdb = false;
                LoadHotFixAssembly();
            }

            return;
        }

        Success = true;
        LoadILRuntime.InitializeILRuntime(Appdomain);
    }