TheInitialProject/Assets/CaoCao/Libraries/CaoCao/Base/FrameworkEntry.cs
2024-10-23 16:59:02 +08:00

128 lines
4.4 KiB
C#

using System;
using System.Collections.Generic;
namespace CaoCao
{
/// <summary>
/// 游戏框架入口。
/// </summary>
//[DoNotRenameAttribute]
public static class FrameworkEntry
{
private static readonly GameLinkedList<GameModule> s_Modules = new GameLinkedList<GameModule>();
/// <summary>
/// 所有游戏框架模块轮询。
/// </summary>
/// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
/// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
public static void Update(float elapseSeconds, float realElapseSeconds)
{
foreach (GameModule module in s_Modules)
{
module.Update(elapseSeconds, realElapseSeconds);
}
}
/// <summary>
/// 关闭并清理所有游戏框架模块。
/// </summary>
public static void Shutdown()
{
for (LinkedListNode<GameModule> current = s_Modules.Last; current != null; current = current.Previous)
{
current.Value.Shutdown();
}
s_Modules.Clear();
ReferencePool.ClearAll();
Utility.Marshal.FreeCachedHGlobal();
CaoCaoLog.SetLogHelper(null);
}
/// <summary>
/// 获取游戏框架模块。
/// </summary>
/// <typeparam name="T">要获取的游戏框架模块类型。</typeparam>
/// <returns>要获取的游戏框架模块。</returns>
/// <remarks>如果要获取的游戏框架模块不存在,则自动创建该游戏框架模块。</remarks>
public static T GetModule<T>() where T : class
{
Type interfaceType = typeof(T);
if (!interfaceType.IsInterface)
{
throw new GameException(Utility.Text.Format("You must get module by interface, but '{0}' is not.", interfaceType.FullName));
}
if (!interfaceType.FullName.StartsWith("CaoCao.", StringComparison.Ordinal))
{
throw new GameException(Utility.Text.Format("You must get a CaoCao module, but '{0}' is not.", interfaceType.FullName));
}
string moduleName = Utility.Text.Format("{0}.{1}", interfaceType.Namespace, interfaceType.Name.Substring(1));
Type moduleType = Type.GetType(moduleName);
if (moduleType == null)
{
throw new GameException(Utility.Text.Format("Can not find CaoCao module type '{0}'.", moduleName));
}
return GetModule(moduleType) as T;
}
/// <summary>
/// 获取游戏框架模块。
/// </summary>
/// <param name="moduleType">要获取的游戏框架模块类型。</param>
/// <returns>要获取的游戏框架模块。</returns>
/// <remarks>如果要获取的游戏框架模块不存在,则自动创建该游戏框架模块。</remarks>
private static GameModule GetModule(Type moduleType)
{
foreach (GameModule module in s_Modules)
{
if (module.GetType() == moduleType)
{
return module;
}
}
return CreateModule(moduleType);
}
/// <summary>
/// 创建游戏框架模块。
/// </summary>
/// <param name="moduleType">要创建的游戏框架模块类型。</param>
/// <returns>要创建的游戏框架模块。</returns>
private static GameModule CreateModule(Type moduleType)
{
GameModule module = (GameModule)Activator.CreateInstance(moduleType);
if (module == null)
{
throw new GameException(Utility.Text.Format("Can not create module '{0}'.", moduleType.FullName));
}
LinkedListNode<GameModule> current = s_Modules.First;
while (current != null)
{
if (module.Priority > current.Value.Priority)
{
break;
}
current = current.Next;
}
if (current != null)
{
s_Modules.AddBefore(current, module);
}
else
{
s_Modules.AddLast(module);
}
return module;
}
}
}