using System; using System.Collections.Generic; namespace IFramework { /// /// 框架入口 /// public static class Framework { #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 private static Dictionary envs = new Dictionary(); private static object _lock = new object(); public static IEnvironment current { get { return Environment.current; } } #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 /// /// 实例化环境 /// /// 环境类型 /// 环境 public static IEnvironment CreateEnv(EnvironmentType envType) { lock (_lock) { IEnvironment env; if (envs.TryGetValue((int)envType, out env)) { throw new Exception(string.Format("The EnvironmentType {0} is not null ", envType)); } else { env = new Environment(envType); envs.Add((int)envType, env); return env; } } } /// /// 根据序号获取环境 /// /// 环境类型 /// public static IEnvironment GetEnv(EnvironmentType envType) { lock (_lock) { IEnvironment env; if (envs.TryGetValue((int)envType, out env)) { return env; } else { throw new Exception(string.Format("The EnvironmentType {0} Error Not Find ,Please Check ", envType)); } } } /// /// 释放环境 /// /// public static void DisposeEnv(EnvironmentType envType) { var env = GetEnv(envType); if (env != null) { env.Dispose(); lock (_lock) { envs.Remove((int)envType); } } } /// /// 绑顶 方法 到一个环境的 Update /// /// 方法 /// 环境 public static void BindEnvUpdate(this Action action, IEnvironment env) { env.BindUpdate(action); } /// /// 解除绑顶 方法 到一个环境的 Update /// /// 方法 /// 环境 public static void UnBindEnvUpdate(this Action action, IEnvironment env) { env.UnBindUpdate(action); } /// /// 绑顶 方法 到一个环境的 Dispose /// /// 方法 /// 环境 public static void BindEnvDispose(this Action action, IEnvironment env) { env.BindDispose(action); } /// /// 解除绑顶 方法 到一个环境的 Dispose /// /// 方法 /// 环境 public static void UnBindEnvDispose(this Action action, IEnvironment env) { env.UnBindDispose(action); } /// /// 绑顶 方法 到一个环境的 Update /// /// 方法 /// public static void BindEnvUpdate(this Action action, EnvironmentType envType) { action.BindEnvUpdate(GetEnv(envType)); } /// /// 解除绑顶 方法 到一个环境的 Update /// /// 方法 /// public static void UnBindEnvUpdate(this Action action, EnvironmentType envType) { action.UnBindEnvUpdate(GetEnv(envType)); } /// /// 绑顶 方法 到一个环境的 Dispose /// /// 方法 /// public static void BindEnvDispose(this Action action, EnvironmentType envType) { action.BindEnvDispose(GetEnv(envType)); } /// /// 解除绑顶 方法 到一个环境的 Dispose /// /// 方法 /// public static void UnBindEnvDispose(this Action action, EnvironmentType envType) { action.UnBindEnvDispose(GetEnv(envType)); } private class GlobalPool : BaseTypePool { protected override IObjectPool CreatePool(Type type) { if (type.IsArray) { var poolType = typeof(ArrayPool<>).MakeGenericType(type.GetElementType()); return Activator.CreateInstance(poolType) as IObjectPool; } return null; } } static private GlobalPool _globalPool = new GlobalPool(); /// /// 获取全局对象池数量 /// /// public static int GetGlbalPoolCount() { return _globalPool.GetPoolCount(); } /// /// 设置全局对象池 /// /// /// public static void SetGlbalPool(ObjectPool pool) { _globalPool.SetPool(pool); } /// /// 全局分配 /// /// /// /// public static T GlobalAllocate(IEventArgs arg = null)where T: class { return _globalPool.Get(arg); } /// /// 全局回收 /// /// /// /// public static void GlobalRecyle(this T t, IEventArgs arg = null)where T :class { _globalPool.Set(t, arg); } /// /// 分配数组 /// /// /// /// public static T[] GlobalAllocateArray(int length) { var result = GlobalAllocate(new ArrayPoolArg(length)); return result; } } }