Tencent / InjectFix

InjectFix is a hot-fix solution library for Unity
Other
1.97k stars 419 forks source link

CustomBridge的新增类只支持interface,不支持class #125

Open nmkooxx opened 4 years ago

nmkooxx commented 4 years ago
#define HOTFIX
using System;
using System.Collections.Generic;
using UnityEngine;
using IFix.Core;
using System.IO;

public abstract class AbsClass {
    public abstract int GetNum();
    public virtual void Print() {
        Debug.Log("AbsClass");
    }
}

public class SubClass1 : AbsClass {
    public override int GetNum() {
        return 1;
    }
    public override void Print() {
        Debug.Log("SubClass1");
    }
}

#if HOTFIX
[IFix.Interpret]
public class SubClass2 : AbsClass {
    public override int GetNum() {
        return 2;
    }
    public override void Print() {
        Debug.Log("SubClass2");
    }
}
#endif

public class NewClassTest2 : MonoBehaviour
{
    void Awake()
    {
        var patch = Resources.Load<TextAsset>("Assembly-CSharp.patch");
        if (patch != null)
        {
            Debug.Log("loading Assembly-CSharp.patch ...");
            var sw = System.Diagnostics.Stopwatch.StartNew();
            PatchManager.Load(new MemoryStream(patch.bytes));
            Debug.Log("patch Assembly-CSharp.patch, using " + sw.ElapsedMilliseconds + " ms");
        }
        Init();
    }

    [IFix.Patch]
    private void Init()
    {
        var sub1 = new SubClass1();
        sub1.Print();
        Debug.Log("SubClass1 GetNum:" + sub1.GetNum());
#if HOTFIX
        var sub2 = new SubClass2();
        sub2.Print();
        Debug.Log("SubClass2 GetNum:" + sub1.GetNum());
#endif
    }
}

[IFix.CustomBridge]
public static class AdditionalBridge
{
    static List<Type> bridge = new List<Type>()
    {
        typeof(AbsClass),
    };
}

这个测试用例在打包Fix和Inject时没有报错,但是热更运行时报错

Exception: can not load type [SubClass2, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null] IFix.Core.PatchManager.Load (System.IO.Stream stream) (at <41cfd5c36e90497b9237a5dcdb075650>:0) NewClassTest.Awake () (at <86c6e9e5595a4ef396f9c1f4fdd59c48>:0)

请问现在新增类是只支持接口,不支持基类吗? 新增的类是不能支持新增非接口的公用函数的吗?我测试新增私有函数、公用成员都是可以的。

chexiongsheng commented 4 years ago

不能继承原生类,可以实现原生接口。 继承解析类在做。