Skip to content

Commit

Permalink
Merge pull request #500 from JasonXuDeveloper/development
Browse files Browse the repository at this point in the history
Development
  • Loading branch information
JasonXuDeveloper authored Oct 20, 2023
2 parents eec3c03 + 3792bd4 commit 1c26081
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ private void Awake()
/// </summary>
private void OnDestroy()
{
ExecuteItems(_onDestroyTaskItems);
UnsafeUtility.Free(ItemList, Allocator.Persistent);
UnsafeUtility.Free(UsageList, Allocator.Persistent);
GC.RemoveMemoryPressure(sizeof(LifeCycleItem) * MaxSize);
Expand Down Expand Up @@ -204,6 +205,11 @@ private void OnDestroy()
/// </summary>
private readonly List<IntPtr> _onceTaskItems = new List<IntPtr>(100);

/// <summary>
/// All on destory task methods
/// </summary>
private readonly List<IntPtr> _onDestroyTaskItems = new List<IntPtr>(100);

/// <summary>
/// no gc search for awake objs
/// </summary>
Expand Down Expand Up @@ -287,7 +293,8 @@ public void AddUpdateItem(object instance, MethodInfo method)
/// <param name="method"></param>
/// <param name="parent"></param>
/// <param name="cond"></param>
public void AddUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null) where T : class
public void AddUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null)
where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_updateItems.Add(GetLifeCycleItem(in ptr, in address,
Expand Down Expand Up @@ -362,11 +369,12 @@ public void AddLateUpdateItem(object instance, MethodInfo method)
/// <param name="method"></param>
/// <param name="parent"></param>
/// <param name="cond"></param>
public void AddLateUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null) where T : class
public void AddLateUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null)
where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_lateUpdateItems.Add(GetLifeCycleItem(in ptr, in address,
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => cond == null ? parent.activeInHierarchy : parent.activeInHierarchy && cond.Invoke()));
}

Expand Down Expand Up @@ -408,11 +416,12 @@ public void AddFixedUpdateItem(object instance, MethodInfo method)
/// <param name="method"></param>
/// <param name="parent"></param>
/// <param name="cond"></param>
public void AddFixedUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null) where T : class
public void AddFixedUpdateItem<T>(T instance, MethodInfo method, GameObject parent, Func<bool> cond = null)
where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_fixedUpdateItems.Add(GetLifeCycleItem(in ptr, in address,
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => method?.Invoke(instance, ConstMgr.NullObjects),
() => cond == null ? parent.activeInHierarchy : parent.activeInHierarchy && cond.Invoke()));
}

Expand Down Expand Up @@ -465,7 +474,8 @@ public Guid AddTask(Action action, Func<bool> condition)
/// <param name="instance"></param>
/// <param name="action"></param>
/// <returns></returns>
public void AddTask<T>(T instance, Action action) => AddTask(action, () => true);
public void AddTask<T>(T instance, Action action) where T : class
=> AddTask(instance, action, () => true);

/// <summary>
/// Add a task that will call once in the main thread when condition is true
Expand All @@ -480,6 +490,55 @@ public void AddTask<T>(T instance, Action action, Func<bool> condition) where T
_onceTaskItems.Add(GetLifeCycleItem(in ptr, in address, action, condition));
}

/// <summary>
/// Add a task that will call once in the main thread when application is quitting
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
public Guid AddOnDestroyTask(Action action) => AddOnDestroyTask(action, () => true);

/// <summary>
/// Add a task that will call once in the main thread when condition is true when application is quitting
/// </summary>
/// <param name="action"></param>
/// <param name="condition"></param>
/// <returns></returns>
public Guid AddOnDestroyTask(Action action, Func<bool> condition)
{
Guid guid = Guid.NewGuid();
var guidIdent = new IntPtr(guid.GetHashCode());
while (_onDestroyTaskItems.Exists(i => ((LifeCycleItem*)i)->InstancePtr == guidIdent))
{
guid = Guid.NewGuid();
guidIdent = new IntPtr(guid.GetHashCode());
}

_onDestroyTaskItems.Add(GetLifeCycleItem((void*)guidIdent, 0, action, condition));
return guid;
}

/// <summary>
/// Add a task that will call once in the main thread when application is quitting
/// </summary>
/// <param name="instance"></param>
/// <param name="action"></param>
/// <returns></returns>
public void AddOnDestroyTask<T>(T instance, Action action) where T : class
=> AddOnDestroyTask(instance, action, () => true);

/// <summary>
/// Add a task that will call once in the main thread when condition is true when application is quitting
/// </summary>
/// <param name="instance"></param>
/// <param name="action"></param>
/// <param name="condition"></param>
/// <returns></returns>
public void AddOnDestroyTask<T>(T instance, Action action, Func<bool> condition) where T : class
{
void* ptr = UnsafeUtility.PinGCObjectAndGetAddress(instance, out var address);
_onDestroyTaskItems.Add(GetLifeCycleItem(in ptr, in address, action, condition));
}

/// <summary>
/// Remove a task that will call once in the main thread
/// </summary>
Expand Down Expand Up @@ -717,7 +776,7 @@ private void LateUpdate()
_instances.Add(item->InstancePtr);
}
}

//调用start,并记录本帧处理的对象
ExecuteItems(_startItems, true, InstancesContains,
iterate: il => _startObjs.Remove(il.InstancePtr));
Expand All @@ -731,7 +790,7 @@ private void LateUpdate()
item = (LifeCycleItem*)_startItems[i];
_instances.Add(item->InstancePtr);
}

ExecuteItems(_startItems, iterate: il => _startObjs.Remove(il.InstancePtr));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,17 @@ private static async void DoChange()
var op = SceneManager.LoadSceneAsync(name);
while (SceneManager.GetActiveScene().path != path)
{
if (!Application.isPlaying) return;
EditorUtility.DisplayProgressBar("JEngine", Setting.GetString(SettingString.JumpToStartUpScene), op.progress);
await Task.Delay(100);
}
EditorUtility.ClearProgressBar();
DynamicGI.UpdateEnvironment();
}

var comp = Object.FindFirstObjectByType<InitJEngine>();
var comp = Object.FindObjectOfType<InitJEngine>();
if (comp == null)
{
Debug.LogWarning("没有找到InitJEngine脚本,无法检验秘钥是否正确");
// Debug.LogWarning("没有找到InitJEngine脚本,无法检验秘钥是否正确");
return;
}
var key = comp.key;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal static class SetData
{
public static bool HasAdded;
private static string _path = "JEngine.proj";
private static JEngineProjData _data = new JEngineProjData();
private static JEngineProjData _data;

public static void UpdateData(Action<JEngineProjData> func)
{
Expand All @@ -23,54 +23,59 @@ public static void UpdateData(Action<JEngineProjData> func)

public static string GetPrefix()
{
//看看文件存不存在,不存在就创建和提示
string fPath = Path.Combine(Application.dataPath, _path);
if (!File.Exists(fPath))
if (_data == null)
{
_data = new JEngineProjData();
//兼容老版本
bool flag = false;
if (File.Exists(Path.Combine(Application.dataPath, "JEngine.lock")))
//看看文件存不存在,不存在就创建和提示
string fPath = Path.Combine(Application.dataPath, _path);
if (!File.Exists(fPath))
{
_data.Prefix = File.ReadAllText(Path.Combine(Application.dataPath, "JEngine.lock"));
_data.EncryptPassword = PlayerPrefs.GetString($"{_data.Prefix}.EncryptPassword", "");
File.Delete(Path.Combine(Application.dataPath, "JEngine.lock"));
}
else
{
_data.Prefix = Guid.NewGuid().ToString();
flag = true;
}
Span<byte> data = stackalloc byte[_data.Size()];
_data.AsBinary(ref data);
File.WriteAllBytes(fPath, data.ToArray());
if (flag)
{
//提示看文档
Debug.LogError(Setting.GetString(SettingString.NoticeText));
EditorUtility.DisplayDialog(Setting.GetString(SettingString.Notice),
Setting.GetString(SettingString.NoticeText), Setting.GetString(SettingString.Done));
if (Setting.Language == JEngineLanguage.English)
//兼容老版本
bool flag = false;
if (File.Exists(Path.Combine(Application.dataPath, "JEngine.lock")))
{
Application.OpenURL("https://docs.xgamedev.net/documents/0.8/");
_data.Prefix = File.ReadAllText(Path.Combine(Application.dataPath, "JEngine.lock"));
_data.EncryptPassword = PlayerPrefs.GetString($"{_data.Prefix}.EncryptPassword", "");
File.Delete(Path.Combine(Application.dataPath, "JEngine.lock"));
}
else
{
Application.OpenURL("https://docs.xgamedev.net/zh/documents/0.8/");
_data.Prefix = Guid.NewGuid().ToString();
flag = true;
}

Span<byte> data = stackalloc byte[_data.Size()];
_data.AsBinary(ref data);
File.WriteAllBytes(fPath, data.ToArray());
if (flag)
{
//提示看文档
Debug.LogError(Setting.GetString(SettingString.NoticeText));
EditorUtility.DisplayDialog(Setting.GetString(SettingString.Notice),
Setting.GetString(SettingString.NoticeText), Setting.GetString(SettingString.Done));
if (Setting.Language == JEngineLanguage.English)
{
Application.OpenURL("https://docs.xgamedev.net/documents/0.8/");
}
else
{
Application.OpenURL("https://docs.xgamedev.net/zh/documents/0.8/");
}
}

InjectDefineSymbol();
}
else
{
//读取文件
Span<byte> data = File.ReadAllBytes(fPath);
_data.FromBinary(ref data);
}
InjectDefineSymbol();
}
else
{
//读取文件
Span<byte> data = File.ReadAllBytes(fPath);
_data.FromBinary(ref data);
}

return _data.Prefix;
}

public static void Update()
{
string prefix = GetPrefix();
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
16 changes: 11 additions & 5 deletions UnityProject/HotUpdateScripts/JEngine/Core/JBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ static JBehaviour()
{
CoroutineMgr.Instance.StartCoroutine(JBehavioursLoop());
});
LifeCycleMgr.Instance.AddOnDestroyTask(() =>
{
foreach (var jb in JBehaviours.Values.ToList())
{
if (!jb._hidden && !jb._paused)
{
jb.Destroy();
}
}
});
}

/// <summary>
Expand All @@ -86,7 +96,6 @@ private static IEnumerator JBehavioursLoop()
Stopwatch sw = new Stopwatch();
for (;;)
{
if (!Application.isPlaying) break;
yield return ConstMgr.WaitFor1Sec;
int cnt = LoopJBehaviours.Count;
for (int i = 0; i < cnt; i++)
Expand Down Expand Up @@ -575,10 +584,7 @@ private void Destroy()
JBehaviours.Remove(_instanceID);
GameObjectJBehaviours.Remove(_gameObject);
_gameObject = null;
if (Application.isPlaying)
{
End();
}
End();
}


Expand Down

0 comments on commit 1c26081

Please sign in to comment.