Skip to content

fix async lifecycle bug #462

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

Merged
merged 1 commit into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@



# JENGINE v0.8.0
# JENGINE v0.8.0f1

**JEngine is an out-of-the-box framework designed for Unity developers. It encapsulates powerful functions. Beginners can also get started quickly and easily create games that can be updated in runtime.**

Expand Down
2 changes: 1 addition & 1 deletion README_zh_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@



# JENGINE v0.8.0
# JENGINE v0.8.0f1

JEngine是针对Unity开发者设计的**开箱即用**的框架,封装了强大的功能,小白也能**快速上手**,**轻松制作**可以**热更新的游戏**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@ namespace JEngine.Core
{
public partial class ClassBindMgr : MonoBehaviour
{
public static void Instantiate()
public static async Task Instantiate()
{
if (_instance != null)
return;

_instance = new GameObject("ClassBindMgr").AddComponent<ClassBindMgr>();
DontDestroyOnLoad(_instance);
SceneManager.sceneLoaded += _instance.OnSceneLoaded;
SceneManager.sceneUnloaded += _instance.OnSceneUnloaded;
LoadedScenes.Add(SceneManager.GetActiveScene());
await DoBind();
}

private static ClassBindMgr _instance;
Expand All @@ -51,11 +55,6 @@ private void Awake()
{
DestroyImmediate(this);
}

SceneManager.sceneLoaded += OnSceneLoaded;
SceneManager.sceneUnloaded += OnSceneUnloaded;
LoadedScenes.Add(SceneManager.GetActiveScene());
_ = DoBind();
}

private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
Expand All @@ -73,11 +72,11 @@ private void OnDestroy()
{
if (_instance == this)
{
_instance = null;
SceneManager.sceneLoaded -= OnSceneLoaded;
SceneManager.sceneUnloaded -= OnSceneUnloaded;
SceneManager.sceneLoaded -= _instance.OnSceneLoaded;
SceneManager.sceneUnloaded -= _instance.OnSceneUnloaded;
LoadedScenes.Clear();
Cbs.Clear();
_instance = null;
}
}

Expand Down Expand Up @@ -121,7 +120,7 @@ public static async Task DoBind(List<ClassBind> cbs)
continue;
}

cb.Active(data);
await cb.Active(data);
}
}
}
Expand Down
58 changes: 39 additions & 19 deletions UnityProject/Assets/Dependencies/JEngine/Core/Util/ClassBind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ILRuntime.CLR.Utils;
using ILRuntime.Reflection;
using System.Threading.Tasks;
using JEngine.Core.DO_NOT_USE;
using ILRuntime.CLR.TypeSystem;
using UnityEngine.Serialization;
using ILRuntime.Runtime.Enviorment;
Expand Down Expand Up @@ -443,7 +444,7 @@ void BindVal(MemberInfo mi)
/// Active
/// </summary>
/// <param name="classData"></param>
public void Active(ClassData classData)
public async Task Active(ClassData classData)
{
string classType =
$"{(string.IsNullOrEmpty(classData.classNamespace) ? String.Empty : $"{classData.classNamespace}.")}{classData.className}";
Expand All @@ -458,31 +459,50 @@ public void Active(ClassData classData)
Log.PrintError($"自动绑定{name}出错:{classType}没有成功绑定数据,自动激活成功,但可能会抛出空异常!");
}

//不管是啥类型,直接invoke这个awake方法
var flags = BindingFlags.Default | BindingFlags.Public
| BindingFlags.Instance | BindingFlags.FlattenHierarchy |
BindingFlags.NonPublic | BindingFlags.Static;
var awakeMethod = clrInstance.GetType().GetMethod("Awake",flags);
if (awakeMethod == null)
if (classData.ClrInstance is MonoBehaviourAdapter.Adaptor mb)
{
awakeMethod = t.GetMethod("Awake", flags);
mb.Awake();
}
else
else if (classData.ClrInstance is ClassBindNonMonoBehaviourAdapter.Adaptor mb2)
{
awakeMethod.Invoke(clrInstance, null);
classData.Activated = true;
mb2.Awake();
}

if (awakeMethod == null)
else
{
Log.PrintError($"{t.FullName}不包含Awake方法,无法激活,已跳过");
//不管是啥类型,直接invoke这个awake方法
var flags = BindingFlags.Default | BindingFlags.Public
| BindingFlags.Instance | BindingFlags.FlattenHierarchy |
BindingFlags.NonPublic | BindingFlags.Static;
var awakeMethod = clrInstance.GetType().GetMethod("Awake",flags);
if (awakeMethod == null)
{
awakeMethod = t.GetMethod("Awake", flags);
}
else
{
awakeMethod.Invoke(clrInstance, null);
classData.Activated = true;
}

if (awakeMethod == null)
{
Log.PrintError($"{t.FullName}不包含Awake方法,无法激活,已跳过");
}
else if (!classData.Activated)
{
awakeMethod.Invoke(clrInstance, null);
}
}
else if (!classData.Activated)

TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
LifeCycleMgr.Instance.AddTask(() =>
{
awakeMethod.Invoke(clrInstance, null);
}
((MonoBehaviour)clrInstance).enabled = true;
classData.Activated = true;

((MonoBehaviour)clrInstance).enabled = true;
classData.Activated = true;
tcs.SetResult(true);
});
await tcs.Task;
}

Remove();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public async Task LoadHotUpdateCallback()
//调用SetupGame周期
Tools.InvokeHotMethod(HotMainType, SetupGameMethod);
//初始化ClassBind
ClassBindMgr.Instantiate();
await ClassBindMgr.Instantiate();
//调用RunGame周期
Tools.InvokeHotMethod(HotMainType, RunGameMethod);
//调用在主工程的热更代码加载完毕后的周期
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void OnGUI()
//文本
Key = EditorGUILayout.TextField("Encrypt Key (加密密码)", Key);
GUILayout.Space(30);
if (GUILayout.Button("Build Bundles (打包资源)"))
if (GUILayout.Button("Encrypt Dll (加密Dll)"))
{
if (Key == null)
{
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 1 addition & 2 deletions UnityProject/HotUpdateScripts/JEngine/Core/JBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ private static string AddClassBind(GameObject gameObject, bool activeAfter, Type
activeAfter = activeAfter,
};
var id = ((JBehaviour)cb.AddClass(cd))._instanceID;
cb.Active(cd);
UnityEngine.Object.Destroy(cb);
_ = cb.Active(cd);
return id;
}

Expand Down
4 changes: 2 additions & 2 deletions UnityProject/UserSettings/EditorUserSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ EditorUserSettings:
value: 2242470311464673021a393214224b1524120b25393a25353e663c37e6cf3a69add435ece93f70283c11fb721130082beb
flags: 0
RecentlyUsedScenePath-1:
value: 2242470311464673021a393214224b1524120b25393a25353e663032ebee7b0be1e238eca81d3e313c4cfa320d2a18
value: 22424703114646720307186c052d56040f
flags: 0
RecentlyUsedScenePath-2:
value: 22424703114646720307186c052d56040f
value: 2242470311464673021a393214224b1524120b25393a25353e663032ebee7b0be1e238eca81d3e313c4cfa320d2a18
flags: 0
vcSharedLogLevel:
value: 0d5e400f0650
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.jasonxudeveloper.jengine",
"version": "0.8.0",
"version": "0.8.0f1",
"displayName": "JEngine",
"description": "The solution that allows unity games update in runtime.",
"license": "MIT",
Expand Down