Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LxFee opened this issue Aug 1, 2024 · 1 comment

Comments

@LxFee
Copy link

LxFee commented Aug 1, 2024

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

@zero-lb
Copy link

zero-lb commented Sep 13, 2024

1、注入失败,参考这里的解决方案,我试了可以的;我的版本是unity2021.3.20f1;链接:#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);
  }

}**

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants