InjectFix 热更新使用记录

一,准备工作

首先将附件内IFixToolKit工具放在和Assets同级目录下。之后将附件内IIFix文件夹放在Assets子目录内。最后将IFix.Core.dll放在Plugins目录下。

二,代码部分

IFix\Editor文件夹下放入自己游戏所需的配置文件,代码如下:

using System.Collections.Generic;
using IFix;
using System;
using System.Reflection;
using System.Linq;


//配置标签
[Configure]
public class GameConfig1
{
    [IFix]
    static IEnumerable<Type> hotfix
    {
        get
        {
            UnityEngine.Debug.Log("GameConfig1GameConfig1");
            return (from type in Assembly.Load("Assembly-CSharp").GetTypes()
                    where type.Namespace == "YourGameName"
                    select type).ToList();
        }
    }
}


注意配置类必须打上[Configure]标签,属性必须打IFix标签并且声明为 **static** 。

IFixEditor.cs代码内主要涉及两个方法,一个是fix生成patch文件的的方法,一个是inject注入版本的方法。默认的patch文件生成路径和core文件路径可以修改,位置如下:

        public static void Patch()
        {
            foreach (var assembly in injectAssemblys)
            {
                GenPatch(assembly, string.Format("./Library/ScriptAssemblies/{0}.dll", assembly),
                    IFIXCORE_PATH, string.Format(PATCH_PATH+"{0}.patch.bytes", assembly));
            }
        }

三,生成补丁

在需要生成补丁的类的响应方法前,加上[IFix.Patch]标签,之后将代码逻辑修改为正确逻辑,点击InjectFix/Fix按钮,会在上面配置好的路径下生成Assembly-CSharp.patch.bytes补丁文件。然后将代码重新改回错误逻辑,点击点击InjectFix/Inject注入版本,此时运行项目,执行以下方法,加载响应的补丁(前提是Assembly-CSharp.patch.bytes文件放在了待加载路径),会发现执行结果是修改后的正确逻辑,加载方法如下:

    public static class IFixManager
    {
        const string PATCH_PATH = "Assets/SharedAssets/GamePatch/";
        //游戏加载时调用
        public static void LoadPatches(Framework.Resource.ResourceManager m_ResManager)
        {
            var patch = m_ResManager.Load<TextAsset>(PATCH_PATH+"Assembly-CSharp.patch.bytes");
            if (patch != null)
            {
                UnityEngine.Debug.Log("loading Assembly-CSharp.patch ...");
                var sw = Stopwatch.StartNew();
                PatchManager.Load(new MemoryStream(patch.bytes));
                UnityEngine.Debug.Log("patch Assembly-CSharp.patch, using " + sw.ElapsedMilliseconds + " ms");
            }
            patch = m_ResManager.Load<TextAsset>(PATCH_PATH + "Assembly-CSharp-firstpass.patch.bytes");
            if (patch != null)
            {
                UnityEngine.Debug.Log("loading Assembly-CSharp-firstpass ...");
                var sw = Stopwatch.StartNew();
                PatchManager.Load(new MemoryStream(patch.bytes));
                UnityEngine.Debug.Log("patch Assembly-CSharp-firstpass, using " + sw.ElapsedMilliseconds + " ms");
            }
        }
    }

具体插件和相关描述文件都是由XLua作者开发的,也开源了,文件地址如下:

https://download.csdn.net/download/qq302756113/11819787

原文链接如下:

https://segmentfault.com/a/1190000020375313

 

------------------------------------------------------------------------------------------------------------------------------------------------------------------

假如补丁内有A类A方法,但是配置文件里面并没有配置这个A类:

在Editor下运行,程序是会修复的,也就是执行补丁内的A方法;但是打包至手机端,则会报错。

个人猜测在Editor下C#应该会通过反射去补丁内找到该方法,但是手机端则不会。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

国家一级摸鱼选手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值