//------------------------------------------------------------ // Game Framework // Copyright © 2013-2021 loyalsoft. All rights reserved. // Homepage: http://www.game7000.com/ // Feedback: http://www.game7000.com/ //------------------------------------------------------------ using System; using System.Collections.Generic; namespace GameFramework { /// /// 游戏框架入口。 /// public static class GameFrameworkEntry { private static readonly GameFrameworkLinkedList s_GameFrameworkModules = new GameFrameworkLinkedList(); /// /// 所有游戏框架模块轮询。 /// /// 逻辑流逝时间,以秒为单位。 /// 真实流逝时间,以秒为单位。 public static void Update(float elapseSeconds, float realElapseSeconds) { foreach (GameFrameworkModule module in s_GameFrameworkModules) { module.Update(elapseSeconds, realElapseSeconds); } } /// /// 关闭并清理所有游戏框架模块。 /// public static void Shutdown() { for (LinkedListNode current = s_GameFrameworkModules.Last; current != null; current = current.Previous) { current.Value.Shutdown(); } s_GameFrameworkModules.Clear(); ReferencePool.ClearAll(); Utility.Marshal.FreeCachedHGlobal(); GameFrameworkLog.SetLogHelper(null); } /// /// 获取游戏框架模块。 /// /// 要获取的游戏框架模块类型。 /// 要获取的游戏框架模块。 /// 如果要获取的游戏框架模块不存在,则自动创建该游戏框架模块。 public static T GetModule() where T : class { Type interfaceType = typeof(T); if (!interfaceType.IsInterface) { throw new GameFrameworkException(Utility.Text.Format("You must get module by interface, but '{0}' is not.", interfaceType.FullName)); } if (!interfaceType.FullName.StartsWith("GameFramework.", StringComparison.Ordinal)) { throw new GameFrameworkException(Utility.Text.Format("You must get a Game Framework 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 GameFrameworkException(Utility.Text.Format("Can not find Game Framework module type '{0}'.", moduleName)); } return GetModule(moduleType) as T; } /// /// 获取游戏框架模块。 /// /// 要获取的游戏框架模块类型。 /// 要获取的游戏框架模块。 /// 如果要获取的游戏框架模块不存在,则自动创建该游戏框架模块。 private static GameFrameworkModule GetModule(Type moduleType) { foreach (GameFrameworkModule module in s_GameFrameworkModules) { if (module.GetType() == moduleType) { return module; } } return CreateModule(moduleType); } /// /// 创建游戏框架模块。 /// /// 要创建的游戏框架模块类型。 /// 要创建的游戏框架模块。 private static GameFrameworkModule CreateModule(Type moduleType) { GameFrameworkModule module = (GameFrameworkModule)Activator.CreateInstance(moduleType); if (module == null) { throw new GameFrameworkException(Utility.Text.Format("Can not create module '{0}'.", moduleType.FullName)); } LinkedListNode current = s_GameFrameworkModules.First; while (current != null) { if (module.Priority > current.Value.Priority) { break; } current = current.Next; } if (current != null) { s_GameFrameworkModules.AddBefore(current, module); } else { s_GameFrameworkModules.AddLast(module); } return module; } } }