Tencent / InjectFix

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

请问有啥好方法可以验证一个il2cpp打包的apk成功被inject了呢 #420

Open LxFee opened 3 months ago

LxFee commented 3 months ago

像unity 2022.3升级以后真机inject失效了,如果没有真机验证过挺危险的

zero-lb commented 1 month ago

1、注入失败,参考这里的解决方案,我试了可以的;我的版本是unity2021.3.20f1;链接:https://github.com/Tencent/InjectFix/issues/417 2、对于你说的真机测试注入检测,我的实现方式是;走AB加载先读取有没有需要热更的C#代码ab文件,没有ab文件,写了测试代码,将fix的测试文件放入Resources目录,然后通过Resources.Load()加载进行测试;代码如下: ` **using System; using System.Collections.Generic; using System.IO; using System.Linq; using IFix.Core; using UnityEngine; using XCore.Log; using XUnityCore;

///

/// C#代码热补丁管理器 /// public static class InjectFixManager {

  /// <summary>
  /// 初始化热补丁文件信息
  /// </summary>
  /// <param name="action">初始化完成回调</param>
  public static void InitPatch(Action action)
  {
      if (ResourceManager.IS_BUNDLE)
      {
          string loadPath = "InjectFix";
          var abInfo = AbManager.Instance.manifest.GetABInfo(loadPath);
          // 补丁文件是否存在
          if (abInfo != null)
          {
              ResourceManager.Instance.LoadAssetBundle(loadPath, ((ab, abPath) =>
              {
                  if (ab != null)
                  {
                      Log.Info("InjectFixAB补丁文件存在:" + abPath);
                      TextAsset[] assets = ab.LoadAllAssets<TextAsset>();
                      if (assets != null)
                      {
                          for (int i = 0; i < assets.Length; ++i)
                          {
                              OnPatch(assets[i]);
                          }
                      }
                  }
                  else
                  {
                      Log.Info("InjectFixAB补丁文件不存在:" + abPath);
                  }
              }), true);
          }
          else
          {
              // todo 用于真机包,resources快速测试是否注入成功?
              bool isTest = false;
              if (isTest)
              {
                  List<string> paths = new List<string>();
                  paths.Add("InjectFix/Assembly-CSharp.patch");
                  for (int i = 0; i < paths.Count; i++)
                  {
                      var asset = Resources.Load(paths[i]);
                      Debug.LogError("Resources测试加载C#热更文件:" + paths[i] + "," + (asset != null ? asset.name : "NULL"));
                      OnPatch(asset as TextAsset);
                  }
              }
          }
      }
      else
      {

if UNITY_EDITOR

          string filePath = Application.dataPath + "/Resources/InjectFix";
          string[] paths = Directory.GetFiles(filePath).Where(s => !s.ToLower().EndsWith(".meta")).ToArray();
          if (paths.Length > 0)
          {
              Log.Info("InjectFixRes补丁文件存在!");
              for (int i = 0; i < paths.Length; i++)
              {
                  var fileName = Path.GetFileNameWithoutExtension(paths[i]);
                  string path = "InjectFix/" + fileName;
                  ResourceManager.Instance.LoadObject(path, ((obj, abPath) =>
                  {
                      OnPatch(obj as TextAsset);
                  }), true);
              }
          }

endif

      }
      action?.Invoke();
      // todo 测试
      TestInjectFix();
  }

  // 注入补丁数据
  private static void OnPatch(TextAsset asset)
  {
      if (asset != null)
      {
          PatchManager.Load(new MemoryStream(asset.bytes));
      }
  }

  // 用于测试C#代码热更的逻辑
 //  [IFix.Patch]
  private static void TestInjectFix()
  {
    //  int range = UnityEngine.Random.Range(0, 489489);
    //  Log.Info("热更测试随机数:" + range);
  }

}**

`