Skip to content

Commit

Permalink
Merge pull request #330 from JasonXuDeveloper/development
Browse files Browse the repository at this point in the history
supported pdb in local & build mode + fixed bug in JBehaviour
  • Loading branch information
JasonXuDeveloper authored May 22, 2022
2 parents 30ea037 + 17855f9 commit 811c8d9
Show file tree
Hide file tree
Showing 15 changed files with 125 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MonoBehaviour:
m_Name: AssetsLoadSetting_1
m_EditorClassIdentifier:
BuildName: Main
BuildIndex: 0
BuildIndex: 1
BundleVariant: je
NameByHash: 1
BuildAssetBundleOptions: 256
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ MonoBehaviour:
m_Name: AssetsLoadSetting_2
m_EditorClassIdentifier:
BuildName: AddOn1
BuildIndex: 0
BuildIndex: 1
BundleVariant: je
NameByHash: 0
BuildAssetBundleOptions: 256
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static partial class ConstMgr
{
public const string DLLSourceFolder = "Assets/HotUpdateResources/Dll/Hidden~/";
public const string PdbSourceFolder = "Assets/HotUpdateResources/Dll/Hidden~/";
public const string PdbBytesFolder = "Assets/HotUpdateResources/Dll/";
public const string DLLBytesFolder = "Assets/HotUpdateResources/Dll/";
public const string MainHotDLLName = "HotUpdateScripts";
public const string DLLExtension = ".dll";
Expand Down
72 changes: 54 additions & 18 deletions UnityProject/Assets/Dependencies/JEngine/Core/Manager/DllMgr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static string GetDllInEditorPath(string name)
Tools.EnsureEndWith(ref name, ConstMgr.DLLExtension);
return Path.Combine(ConstMgr.DLLSourceFolder, name);
}

/// <summary>
/// Get DLL path in the runtime (located in HotUpdateResources/DLL)
/// </summary>
Expand All @@ -53,7 +53,30 @@ public static string GetDllInRuntimePath(string name)
Tools.EnsureEndWith(ref name, ConstMgr.BytesExtension);
return Path.Combine(ConstMgr.DLLBytesFolder, name);
}


/// <summary>
/// Get PDB path in the editor (located at hidden file with .pdb extension)
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GetPdbInEditorPath(string name)
{
Tools.EnsureEndWith(ref name, ConstMgr.PdbExtension);
return Path.Combine(ConstMgr.PdbSourceFolder, name);
}

/// <summary>
/// Get PDB path in the runtime (located in HotUpdateResources/DLL)
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static string GetPdbInRuntimePath(string name)
{
Tools.EnsureEndWith(ref name, ConstMgr.PdbExtension);
Tools.EnsureEndWith(ref name, ConstMgr.BytesExtension);
return Path.Combine(ConstMgr.PdbBytesFolder, name);
}

/// <summary>
/// Get DLL binary data
/// </summary>
Expand All @@ -72,6 +95,7 @@ public static byte[] GetDllBytes(string name, bool editor = false)
{
return FileMgr.FileToByte(path);
}

throw new FileNotFoundException($"DLL not found in: {path}");
}

Expand All @@ -90,26 +114,37 @@ public static byte[] GetDllBytes(string name, bool editor = false)
/// Get PDB binary data
/// </summary>
/// <param name="name"></param>
/// <param name="editor"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static byte[] GetPdbBytes(string name)
public static byte[] GetPdbBytes(string name, bool editor = true)
{
//DLL文件
string dllName = name;
Tools.EnsureEndWith(ref dllName, ConstMgr.DLLExtension);
var dllPath = Path.Combine(ConstMgr.DLLSourceFolder, name);

//PDB文件
Tools.EnsureEndWith(ref name, ConstMgr.PdbExtension);
string path = Path.Combine(ConstMgr.PdbSourceFolder, name);
//查看是否有PDB文件并且是最新的
if (File.Exists(path) &&
(File.GetLastWriteTime(dllPath) - File.GetLastWriteTime(path)).Seconds < 30)
if (editor)
{
//DLL文件
var dllPath = GetDllInEditorPath(name);

//PDB文件
string path = GetPdbInEditorPath(name);
//查看是否有PDB文件并且是最新的
if (File.Exists(path) &&
(File.GetLastWriteTime(dllPath) - File.GetLastWriteTime(path)).Seconds < 30)
{
return FileMgr.FileToByte(path);
}

throw new InvalidOperationException("Pdb is invalid");
}

var pdbPath = GetPdbInRuntimePath(name);
var pdbFile = (TextAsset)AssetMgr.Load(pdbPath,
AssetComponentConfig.DefaultBundlePackageName);
if (pdbFile == null)
{
return FileMgr.FileToByte(path);
throw new FileNotFoundException($"Pdb not found in: {pdbPath}");
}

throw new InvalidOperationException("Pdb is invalid");
return pdbFile.bytes;
}

/// <summary>
Expand All @@ -118,13 +153,14 @@ public static byte[] GetPdbBytes(string name)
/// <param name="source">plaintext data</param>
/// <param name="key">key</param>
/// <exception cref="InvalidOperationException"></exception>
public static void SimulateEncryption(ref byte[] source,string key)
public static void SimulateEncryption(ref byte[] source, string key)
{
if (key.Length != 16)
{
throw new InvalidOperationException("key to encrypt has to be length of 16");
}

source = CryptoMgr.AesEncrypt(source, key);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@
// THE SOFTWARE.

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

namespace JEngine.Core
{
public partial class LifeCycleMgr : MonoBehaviour
{
private class LifeCycleItem
{
public object Instance;
public MethodInfo Method;
public readonly object ItemInstance;
public readonly MethodInfo Method;

public LifeCycleItem(object instance, MethodInfo method)
public LifeCycleItem(object itemInstance, MethodInfo method)
{
Instance = instance;
ItemInstance = itemInstance;
Method = method;
}
}
Expand All @@ -64,7 +64,7 @@ public LifeCycleItem(object instance, MethodInfo method)
/// <summary>
/// 单帧处理过的对象
/// </summary>
private List<object> _instances = null;
private List<object> _instances;

/// <summary>
/// All awake methods
Expand Down Expand Up @@ -142,7 +142,7 @@ public void AddUpdateItem(object instance, MethodInfo method)
/// <param name="instance"></param>
public void RemoveUpdateItem(object instance)
{
_updateItems.RemoveWhere(i => i.Instance == instance);
_updateItems.RemoveWhere(i => i.ItemInstance == instance);
}

/// <summary>
Expand All @@ -161,7 +161,7 @@ public void AddLateUpdateItem(object instance, MethodInfo method)
/// <param name="instance"></param>
public void RemoveLateUpdateItem(object instance)
{
_lateUpdateItems.RemoveWhere(i => i.Instance == instance);
_lateUpdateItems.RemoveWhere(i => i.ItemInstance == instance);
}

/// <summary>
Expand All @@ -180,7 +180,7 @@ public void AddFixedUpdateItem(object instance, MethodInfo method)
/// <param name="instance"></param>
public void RemoveFixedUpdateItem(object instance)
{
_fixedUpdateItems.RemoveWhere(i => i.Instance == instance);
_fixedUpdateItems.RemoveWhere(i => i.ItemInstance == instance);
}

/// <summary>
Expand All @@ -207,17 +207,17 @@ private void ExecuteItems(HashSet<LifeCycleItem> items, bool removeAfterInvoke =
foreach (var item in cloned)
{
//忽略
if (ignoreCondition != null && ignoreCondition(item.Instance))
if (ignoreCondition != null && ignoreCondition(item.ItemInstance))
{
continue;
}

//执行
if (item.Instance != null && item.Method != null)
if (item.ItemInstance != null && item.Method != null)
{
try
{
item.Method.Invoke(item.Instance, ConstMgr.NullObjects);
item.Method.Invoke(item.ItemInstance, ConstMgr.NullObjects);
}
catch(Exception ex)
{
Expand Down Expand Up @@ -263,7 +263,7 @@ private void FixedUpdate()
{
_instances = new List<object>();
}
_instances.AddRange(_awakeItems.Select(i=>i.Instance));
_instances.AddRange(_awakeItems.Select(i=>i.ItemInstance));
ExecuteItems(_awakeItems);
}

Expand All @@ -274,8 +274,8 @@ private void FixedUpdate()
if (_instances != null && _instances.Count > 0)
{
//调用enable,并记录本帧处理的对象
_instances.AddRange(_enableItems.ToList().FindAll(i => !_instances.Contains(i.Instance))
.Select(i => i.Instance));
_instances.AddRange(_enableItems.ToList().FindAll(i => !_instances.Contains(i.ItemInstance))
.Select(i => i.ItemInstance));
ExecuteItems(_enableItems,true, _instances.Contains);
}
else
Expand All @@ -285,7 +285,7 @@ private void FixedUpdate()
{
_instances = new List<object>();
}
_instances.AddRange(_enableItems.Select(i=>i.Instance));
_instances.AddRange(_enableItems.Select(i=>i.ItemInstance));
ExecuteItems(_enableItems);
}
}
Expand All @@ -297,8 +297,8 @@ private void FixedUpdate()
if (_instances != null && _instances.Count > 0)
{
//调用start,并记录本帧处理的对象
_instances.AddRange(_startItems.ToList().FindAll(i => !_instances.Contains(i.Instance))
.Select(i => i.Instance));
_instances.AddRange(_startItems.ToList().FindAll(i => !_instances.Contains(i.ItemInstance))
.Select(i => i.ItemInstance));
ExecuteItems(_startItems,true, _instances.Contains);
}
else
Expand All @@ -308,7 +308,7 @@ private void FixedUpdate()
{
_instances = new List<object>();
}
_instances.AddRange(_startItems.Select(i=>i.Instance));
_instances.AddRange(_startItems.Select(i=>i.ItemInstance));
ExecuteItems(_startItems);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

namespace JEngine.Core
{
[HelpURL("https://xgamedev.uoyou.com/classbind-v0-6.html")]
[HelpURL("https://docs.xgamedev.net/zh/documents/0.7/classbind.html")]
public class ClassBind : MonoBehaviour
{
[FormerlySerializedAs("ScriptsToBind")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class BuildBundles
[MenuItem("Tools/BuildAsset/构建AssetBundle %#&B")]
private static void BuildAssetBundles()
{
FileMgr.Delete( DllMgr.GetDllInRuntimePath(ConstMgr.MainHotDLLName));
FileMgr.Delete(DllMgr.GetDllInRuntimePath(ConstMgr.MainHotDLLName));
FileMgr.Delete(DllMgr.GetPdbInRuntimePath(ConstMgr.MainHotDLLName));

Action<string> buildAct = async s =>
{
Expand All @@ -34,6 +35,20 @@ private static void BuildAssetBundles()
return;
}

watch.Reset();
watch.Start();
string pdbPath = DllMgr.GetPdbInEditorPath(ConstMgr.MainHotDLLName);
bytes = FileMgr.FileToByte(pdbPath);
result = FileMgr.ByteToFile(bytes,
DllMgr.GetPdbInRuntimePath(ConstMgr.MainHotDLLName));
watch.Stop();
Log.Print("Convert PDBs in: " + watch.ElapsedMilliseconds + " ms.");
if (!result)
{
Log.PrintError("PDB转Byte[]出错!");
return;
}

Setting.EncryptPassword = s;

await Task.Delay(3);
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified UnityProject/Assets/HotUpdateResources/Dll/HotUpdateScripts.bytes
Binary file not shown.
Binary file not shown.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions UnityProject/Assets/Scripts/InitJEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public void LoadHotUpdateCallback()
ET.ETTask.ExceptionHandler += Debug.LogException;
//加载热更DLL
Instance.LoadHotFixAssembly();
//初始化LifeCycle
LifeCycleMgr.Initialize();
//调用SetupGame周期
Tools.InvokeHotMethod(HotMainType, SetupGameMethod);
#if INIT_JE
//初始化LifeCycle
LifeCycleMgr.Initialize();
//初始化ClassBind
ClassBindMgr.Instantiate();
#endif
Expand Down Expand Up @@ -109,6 +109,12 @@ private void LoadHotFixAssembly()
DllMgr.SimulateEncryption(ref dll, key);
pdb = DllMgr.GetPdbBytes(DllName);
}
else if (usePdb)
{
var pdbFileBytes = DllMgr.GetPdbBytes(DllName, false);
pdb = new byte[pdbFileBytes.Length];
Array.Copy(pdbFileBytes, pdb, pdbFileBytes.Length);
}

//生成缓冲区,复制加密dll数据
var buffer = new byte[dll.Length];
Expand All @@ -125,7 +131,14 @@ private void LoadHotFixAssembly()
}

//加载dll
Appdomain.LoadAssembly(_fs, _pdb, new PdbReaderProvider());
if (usePdb)
{
Appdomain.LoadAssembly(_fs, _pdb, new PdbReaderProvider());
}
else
{
Appdomain.LoadAssembly(_fs);
}
}
catch (Exception e)
{
Expand Down
1 change: 1 addition & 0 deletions UnityProject/HotUpdateScripts/BPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class BPath
public const string Assets_HotUpdateResources_Controller_Controller__txt = "Assets/HotUpdateResources/Controller/Controller.txt";
public const string Assets_HotUpdateResources_Dll___DS_Store = "Assets/HotUpdateResources/Dll/.DS_Store";
public const string Assets_HotUpdateResources_Dll_HotUpdateScripts__bytes = "Assets/HotUpdateResources/Dll/HotUpdateScripts.bytes";
public const string Assets_HotUpdateResources_Dll_HotUpdateScripts__pdb__bytes = "Assets/HotUpdateResources/Dll/HotUpdateScripts.pdb.bytes";
public const string Assets_HotUpdateResources_Material_Material__txt = "Assets/HotUpdateResources/Material/Material.txt";
public const string Assets_HotUpdateResources_Other_impact__ttf = "Assets/HotUpdateResources/Other/impact.ttf";
public const string Assets_HotUpdateResources_Prefab_InstantiateDemo__prefab = "Assets/HotUpdateResources/Prefab/InstantiateDemo.prefab";
Expand Down
Loading

0 comments on commit 811c8d9

Please sign in to comment.