├── .gitignore ├── Example-Readme ├── Bind.md ├── Coroutine.md ├── Inject.md ├── Message.md ├── Module.md ├── Mvvm.md ├── Pool.md ├── PriorityQueue.md ├── Record.md ├── RecyclableObject.md ├── Serialization.md └── Singleton.md ├── Framework ├── Example │ ├── Example.csproj │ ├── Examples │ │ ├── CoroutineTest.cs │ │ ├── InjectTest.cs │ │ ├── MessageExample.cs │ │ ├── MouduleTest.cs │ │ ├── PriorityQueueTest.cs │ │ ├── RecorderTest.cs │ │ ├── SingletonTest.cs │ │ └── TimerTest.cs │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ └── app.config ├── IFramework.sln └── IFramework │ ├── IFramework.csproj │ ├── IFramework │ ├── Environment │ │ ├── Environment.cs │ │ ├── EnvironmentType.cs │ │ ├── Framework.cs │ │ ├── IEnvironment.cs │ │ ├── ITimeCalculator.cs │ │ ├── OnEnvironmentInitAttribute.cs │ │ └── TimeCalculator.cs │ ├── Ex.cs │ ├── IAwaitable.cs │ ├── IAwaiter.cs │ ├── Log │ │ ├── CSLogger.cs │ │ ├── ILogRecorder.cs │ │ ├── ILoger.cs │ │ └── Log.cs │ ├── Modules │ │ ├── Coroutine │ │ │ ├── Coroutine.cs │ │ │ ├── CoroutineAwaiter.cs │ │ │ ├── CoroutineState.cs │ │ │ ├── ICoroutine.cs │ │ │ ├── Instruction │ │ │ │ ├── WaitForDays.cs │ │ │ │ ├── WaitForFrame.cs │ │ │ │ ├── WaitForFrames.cs │ │ │ │ ├── WaitForHours.cs │ │ │ │ ├── WaitForMilliseconds.cs │ │ │ │ ├── WaitForMinutes.cs │ │ │ │ ├── WaitForSeconds.cs │ │ │ │ ├── WaitForTicks.cs │ │ │ │ ├── WaitForTimeSpan.cs │ │ │ │ ├── WaitUtil.cs │ │ │ │ ├── WaitWhile.cs │ │ │ │ └── YieldInstruction.cs │ │ │ └── Module │ │ │ │ ├── CoroutineModule.cs │ │ │ │ └── ICoroutineModule.cs │ │ ├── IModules.cs │ │ ├── Inject │ │ │ ├── IInjectModule.cs │ │ │ ├── InjectAttribute.cs │ │ │ ├── InjectModule.InjectInstanceMap.cs │ │ │ ├── InjectModule.InjectMap.cs │ │ │ ├── InjectModule.InjectTypeMap.cs │ │ │ └── InjectModule.cs │ │ ├── Loom │ │ │ └── LoomModule.cs │ │ ├── Message │ │ │ ├── Message │ │ │ │ ├── IEventArgs.cs │ │ │ │ ├── IMessage.cs │ │ │ │ ├── IMessageListener.cs │ │ │ │ ├── Message.cs │ │ │ │ ├── MessageAwaiter.cs │ │ │ │ ├── MessageErrorCode.cs │ │ │ │ ├── MessageListener.cs │ │ │ │ ├── MessageState.cs │ │ │ │ ├── MessageUrgency.cs │ │ │ │ └── MessageUrgencyType.cs │ │ │ └── Module │ │ │ │ ├── HandlerQueue.cs │ │ │ │ ├── MessageQueue.cs │ │ │ │ ├── String │ │ │ │ ├── IStringMessageModule.cs │ │ │ │ ├── StringHandlerQueue.cs │ │ │ │ ├── StringMessageModule.cs │ │ │ │ └── StringMessageQueue.cs │ │ │ │ ├── Subject.cs │ │ │ │ ├── SubjectType.cs │ │ │ │ ├── SubscribeAction.cs │ │ │ │ └── Type │ │ │ │ ├── IMessageModule.cs │ │ │ │ ├── MessageModule.cs │ │ │ │ ├── TypeHandlerQueue.cs │ │ │ │ └── TypeMessageQueue.cs │ │ ├── Module.cs │ │ ├── ModulePriority.cs │ │ ├── Modules.cs │ │ ├── Recorder │ │ │ ├── Module │ │ │ │ ├── ICommand.cs │ │ │ │ ├── IOperationRecorderModule.cs │ │ │ │ ├── OperationRecorderEx.cs │ │ │ │ ├── OperationRecorderModule.HeadState.cs │ │ │ │ └── OperationRecorderModule.cs │ │ │ └── States │ │ │ │ ├── ActionGroupState.cs │ │ │ │ ├── ActionState.cs │ │ │ │ ├── BaseState.cs │ │ │ │ ├── CommandGroupState.cs │ │ │ │ └── CommandState.cs │ │ ├── Timer │ │ │ ├── Entity │ │ │ │ ├── EntityState.cs │ │ │ │ ├── ITimerEntity.cs │ │ │ │ ├── InnerType.cs │ │ │ │ └── TimerEntity.cs │ │ │ └── Module │ │ │ │ ├── ITimerModule.cs │ │ │ │ └── TimerModule.cs │ │ └── UpdateModule.cs │ ├── ObjectPool.cs │ ├── Queue │ │ ├── FastPriorityQueue.cs │ │ ├── FastPriorityQueueNode.cs │ │ ├── GenericPriorityQueue.cs │ │ ├── GenericPriorityQueueNode.cs │ │ ├── IFixedSizePriorityQueue.cs │ │ ├── IPriorityQueue.cs │ │ ├── SimplePriorityQueue.cs │ │ ├── StablePriorityQueue.cs │ │ └── StablePriorityQueueNode.cs │ ├── Singleton │ │ ├── ISingleton.cs │ │ ├── Singleton.cs │ │ ├── SingletonCollection.cs │ │ ├── SingletonCreator.cs │ │ ├── SingletonProperty.cs │ │ └── SingletonPropertyClass.cs │ └── Unit.cs │ └── Properties │ └── AssemblyInfo.cs ├── README.md ├── 制作src分支.bat └── 清除记录.bat /.gitignore: -------------------------------------------------------------------------------- 1 | /.vs 2 | /IFramework/Properties 3 | /IFramework/bin 4 | Framework/.vs/ 5 | Framework/Example/bin/ 6 | Plugins/ 7 | Framework/IFramework/obj/ 8 | Framework/IFramework.UnityPlugin/obj/ 9 | Framework/Example/obj/ 10 | Framework/packages/ 11 | Framework/IFramework.Tests/bin/ 12 | Framework/IFramework.Tests/obj/ 13 | -------------------------------------------------------------------------------- /Example-Readme/Bind.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 数据绑定 4 | 5 | ## 单向绑定(数据监听) 6 | 7 | 特点: 8 | * 监听单个值的变化 9 | 10 | 单向绑定使用示例: 11 | 12 | ``` csharp 13 | //监听的类需要继承ObservableObject 14 | class Observable_A : ObservableObject 15 | { 16 | private int _value; 17 | 18 | public int value 19 | { 20 | get 21 | { 22 | return GetProperty(ref _value); 23 | } 24 | set 25 | { 26 | SetProperty(ref _value, value); 27 | } 28 | } 29 | } 30 | 31 | //创建一个监听器 32 | ObservableObjectHandler binder = new ObservableObjectHandler(); 33 | //创建监听的实例 34 | Observable_A observedA = new Observable_A(); 35 | 36 | //监听value 37 | binder.BindProperty( 38 | (value) => 39 | { 40 | observedA.value = value; 41 | Log.L($"值被改变,新值为{value}"); 42 | }, 43 | () => 44 | { 45 | return observedA.value; 46 | } 47 | ); 48 | 49 | observedA.value = 1; 50 | 51 | observedA.value = 2; 52 | //观察控制台的输出 53 | 54 | ``` 55 | --- 56 | 57 | ## 多向绑定(数据同步变化) 58 | 特点: 59 | * 多个绑定,值同步变化 60 | 61 | 多向绑定使用示例: 62 | ``` csharp 63 | //监听的类需要继承BindableObject 64 | class Binder_A : BindableObject 65 | { 66 | private int _value; 67 | 68 | public int value { get { return GetProperty(ref _value); } set { SetProperty(ref _value, value); } } 69 | } 70 | class Binder_B : BindableObject 71 | { 72 | private int _value; 73 | 74 | public int value { get { return GetProperty(ref _value); } set { SetProperty(ref _value, value); } } 75 | } 76 | 77 | class Binder_C : BindableObject 78 | { 79 | private int _value; 80 | 81 | public int value { get { return GetProperty(ref _value); } set { SetProperty(ref _value, value); } } 82 | } 83 | 84 | //创建监听器 85 | BindableObjectHandler binder = new BindableObjectHandler(); 86 | 87 | //创建实例 88 | Binder_A a = new Binder_A(); 89 | Binder_B b = new Binder_B(); 90 | Binder_C c = new Binder_C(); 91 | 92 | //绑定 93 | binder.BindProperty((value) => { a.value = value; }, () => { return a.value; }); 94 | binder.BindProperty((value) => { b.value = value; }, () => { return b.value; }); 95 | binder.BindProperty((value) => { c.value = value; }, () => { return c.value; }); 96 | 97 | 98 | Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n"); 99 | a.value = 1; 100 | Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n"); 101 | c.value = 2; 102 | Log.L($"a的value值为{a.value}\tb的value值为{b.value}\tc的value值为{c.value}\n"); 103 | ``` 104 | 105 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/BindTest.cs) 106 | 107 | --- 108 | [回到顶部](#) 109 | 110 | -------------------------------------------------------------------------------- /Example-Readme/Coroutine.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 协程 4 | 更好地利用线程资源 5 | 6 | 模块特点: 7 | * 支持多种等待时间延迟执行 8 | * 支持多种等待判断 9 | * 支持快速结束某个协程 10 | * 支持异步等待 11 | 12 | 使用示例: 13 | ``` csharp 14 | //tip:环境的Update需要定时执行 15 | 16 | //获取协程模块 17 | coroutineModule = Framework.GetEnv(EnvironmentType.Ev0).modules.Coroutine; 18 | 19 | //开启协程WaitExample 20 | coroutineModule.StartCoroutine(WaitExample()); 21 | 22 | IEnumerator WaitExample() 23 | { 24 | Log.L("协程WaitExample开始"); 25 | 26 | yield return new WaitForFrame(); 27 | Log.L("一帧过去了"); 28 | 29 | yield return new WaitForFrames(20); 30 | Log.L("20帧过去了"); 31 | 32 | yield return new WaitForHours(0.0004); 33 | Log.L("0.0004小时过去了"); 34 | } 35 | 36 | ``` 37 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/CoroutineTest.cs) 38 | 39 | --- 40 | 41 | [回到顶部](#) 42 | 43 | -------------------------------------------------------------------------------- /Example-Readme/Inject.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 依赖注入 4 | 5 | 放什么拿什么 6 | 7 | 特点: 8 | * 支持直接注入和反射注入 9 | * 支持根据name注入同类型的多个实例 10 | 11 | 直接注入使用示例: 12 | 13 | ``` csharp 14 | public interface ICommonInjectObject 15 | { 16 | void ToDo(); 17 | } 18 | 19 | public class CommonObject : ICommonInjectObject 20 | { 21 | public void ToDo() 22 | { 23 | //Do sth 24 | } 25 | } 26 | 27 | //获取容器 28 | container = Framework.GetEnv(EnvironmentType.Ev0).container; 29 | 30 | //往容器中注册实例 31 | container.SubscribeInstance(new CommonObject()); 32 | 33 | //获取实例 34 | var _commonObject = container.GetValue(); 35 | 36 | //通过name区分来注册多个同类型实例 37 | container.SubscribeInstance(new CommonObject(), "Instance1"); 38 | //根据name获取指定类型的实例 39 | _commonObject = container.GetValue("Instance1"); 40 | 41 | //使用容器的Clear方法清除当前所有注入的实例 42 | container.Clear(); 43 | ``` 44 | --- 45 | 46 | ***反射注入强调性能之处慎用*** 47 | 48 | 反射注入使用示例: 49 | ``` csharp 50 | public interface IReflectInjectObject 51 | { 52 | void ToDo(); 53 | } 54 | 55 | public class ReflectObject : IReflectInjectObject 56 | { 57 | public void ToDo() 58 | { 59 | //Do sth 60 | } 61 | } 62 | 63 | //使用Inject这个Attribute标记需要反射注入的变量 64 | [Inject] 65 | public IReflectInjectObject _reflectObject; 66 | //Attribute增加name属性 67 | [Inject("Instance1")] 68 | public IReflectInjectObject _reflectMultiObject1; 69 | 70 | //以IReflectInjectObject的名义注入ReflectObject类型 71 | container.Subscribe(); 72 | 73 | container.Subscribe("Instance1"); 74 | 75 | //为当前类的含有[Inject]这个Attribute的对应类型的变量注入数据 76 | container.Inject(this); 77 | ``` 78 | 79 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/InjectTest.cs) 80 | 81 | --- 82 | [回到顶部](#) 83 | 84 | -------------------------------------------------------------------------------- /Example-Readme/Message.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 消息 4 | 5 | 6 | > A:回复T即可退订
7 | B:T
8 | A:回复T即可退订
9 | B:T即可
10 | A:退订成功 11 | 12 | 消息是软件开发中减少模块耦合的有效方式 13 | 14 | 模块特点: 15 | * 支持流量控制 16 | * 支持子类型匹配(根据子类型监听父类型) 17 | * 支持异步等待 18 | * 支持消息优先级 19 | 20 | 使用示例: 21 | ``` csharp 22 | //需要监听的类 23 | public interface IPub { } 24 | public class Pub : IPub { } 25 | 26 | //自定义监听器类 27 | public class Listener : IMessageListener 28 | { 29 | public Listener() 30 | { 31 | //构造函数,在这里可以直接写对某个类型的监听 32 | } 33 | 34 | //实现接口里的消息回调方法 35 | public void Listen(IMessage message) 36 | { 37 | Log.L($"收到类型为{message.subject}的消息,消息值为{message.code}"); 38 | } 39 | } 40 | //获取消息模块 41 | messageModule = Framework.GetEnv(EnvironmentType.Ev0).modules.Message; 42 | //匹配子类型开关 43 | messageModule.fitSubType = true; 44 | //每帧处理的消息量 45 | messageModule.processesPerFrame = 1; 46 | 47 | //实例化一个消息监听器 48 | Listener listenner = new Listener(); 49 | 50 | //根据IPub监听 51 | messageModule.Subscribe(listenner); 52 | 53 | //发送一条重要程度为Important的消息,消息值为100 54 | messageModule.Publish(null,MessageUrgencyType.Important).SetCode(100); 55 | ``` 56 | 57 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/MessageExample.cs) 58 | 59 | --- 60 | [回到顶部](#) -------------------------------------------------------------------------------- /Example-Readme/Module.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 自定义模块 4 | 5 | 自己造轮子 6 | 7 | 特点: 8 | * 自己定义一个符合框架生命周期的模块 9 | 10 | 使用示例: 11 | 12 | ``` csharp 13 | //继承UpdateModule创建一个自定义模块 14 | private class MyModule : IFramework.Modules.UpdateModule 15 | { 16 | protected override void Awake() 17 | { 18 | Log.L("Awake"); 19 | } 20 | protected override void OnEnable() 21 | { 22 | Log.L("OnEnable"); 23 | } 24 | protected override void OnUpdate() 25 | { 26 | Log.L("OnUpdate"); 27 | } 28 | protected override void OnDisable() 29 | { 30 | Log.L("OnDisable"); 31 | } 32 | public void Do() 33 | { 34 | //Do sth 35 | } 36 | protected override void OnDispose() 37 | { 38 | Log.L("OnDispose"); 39 | } 40 | } 41 | 42 | //获取模块容器 43 | IModules moduleContainer= Framework.GetEnv(EnvironmentType.Ev0).modules; 44 | 45 | //创建模块实例 46 | moduleContainer.CreateModule(); 47 | //获取模块实例 48 | //也可以使用GetModule在没有实例的时候自动创建实例 49 | var module = moduleContainer.FindModule(); 50 | 51 | ``` 52 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/MouduleTest.cs) 53 | 54 | --- 55 | [回到顶部](#) -------------------------------------------------------------------------------- /Example-Readme/Mvvm.md: -------------------------------------------------------------------------------- 1 | [[返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework)](../README.md) 2 | 3 | # Mvvm 4 | 特点: 5 | * 提供对应的类简化编写 6 | 7 | 使用示例: 8 | ``` csharp 9 | //model 10 | class MyModel : IModel 11 | { 12 | public int value = 2; 13 | } 14 | 15 | //viewModel 16 | class MyViewModel : ViewModel 17 | { 18 | private int _value; 19 | 20 | //需要监听的字段 21 | public int value 22 | { 23 | get { return GetProperty(ref _value); } 24 | set 25 | { 26 | //将值同步到Model 27 | Tmodel.value = value; 28 | SetProperty(ref _value, value); 29 | } 30 | } 31 | 32 | protected override void Initialize() 33 | { 34 | Log.L("ViewModel:初始化操作"); 35 | value = 5; 36 | } 37 | 38 | protected override void Listen(IEventArgs message) 39 | { 40 | Log.E("ViewModel:接收到消息,数据+1"); 41 | value++; 42 | } 43 | 44 | protected override void SyncModelValue() 45 | { 46 | Log.L("ViewModel:同步操作"); 47 | value = Tmodel.value; 48 | } 49 | } 50 | 51 | //view 52 | class MyView : View 53 | { 54 | //View类里面有监听器,这里只需要重写监听方法就可以了 55 | protected override void BindProperty() 56 | { 57 | base.BindProperty(); 58 | this.handler.BindProperty(() => 59 | { 60 | //在这里写对UI的更变操作 61 | var value = Tcontext.value; 62 | Log.E($"View:数据出现更改,当前的值为:{value}"); 63 | }); 64 | 65 | Log.E("View:发送空消息"); 66 | //发送消息 67 | Publish(null); 68 | } 69 | } 70 | 71 | ``` 72 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/MvvmTest.cs) 73 | 74 | --- 75 | [回到顶部](#) -------------------------------------------------------------------------------- /Example-Readme/Pool.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 对象池 4 | 提供多种对象池供开发者使用 5 | ## 1、自动管理对象池 6 | 最简单的对象池使用方式 7 | 8 | 特点: 9 | * 内部自动实例化新对象,创建之后直接使用,非常简便 10 | * 支持含有多个参数的构造方法的对象 11 | 12 | 使用示例: 13 | ``` csharp 14 | //定义一个类 15 | public class Human 16 | { 17 | public readonly int age; 18 | public readonly string name; 19 | 20 | public Human(int age, string name) 21 | { 22 | this.age = age; 23 | this.name = name; 24 | } 25 | 26 | public void Say() 27 | { 28 | Log.L($"我的名字叫{name},{age}岁"); 29 | } 30 | } 31 | 32 | //创建对象池 33 | ActivatorCreatePool pool = new ActivatorCreatePool(33, "jljy"); 34 | //从对象池中获取对象 35 | var human = pool.Get(); 36 | human.Say(); 37 | //将对象放回对象池 38 | pool.Set(human); 39 | ``` 40 | 41 | # 2、定制生命周期的对象池 42 | 自己定制生命周期,特别好用的对象池 43 | 44 | 特点: 45 | * 可以对对象的生成、获取、放回等生命周期进行定制 46 | 47 | 示例代码: 48 | ``` csharp 49 | public class Obj_A { } 50 | //继承ObjectPool创建一个对象池类型MyPool 51 | private class MyPool : ObjectPool 52 | { 53 | protected override Obj_A CreatNew(IEventArgs arg) 54 | { 55 | Log.L("*对象池:创建了一个实例"); 56 | return new Obj_A(); 57 | } 58 | protected override void OnCreate(Obj_A t, IEventArgs arg) 59 | { 60 | Log.L("*对象池:实例正在被创建"); 61 | base.OnCreate(t, arg); 62 | } 63 | protected override void OnGet(Obj_A t, IEventArgs arg) 64 | { 65 | Log.L("*对象池:正在获取实例"); 66 | base.OnGet(t, arg); 67 | } 68 | protected override bool OnSet(Obj_A t, IEventArgs arg) 69 | { 70 | Log.L("*对象池:正在回收实例"); 71 | return base.OnSet(t, arg); 72 | } 73 | protected override void OnClear(Obj_A t, IEventArgs arg) 74 | { 75 | Log.L("*对象池:正在清除池子里的实例"); 76 | base.OnClear(t, arg); 77 | } 78 | protected override void OnDispose() 79 | { 80 | Log.L("*对象池:池子正在被释放"); 81 | base.OnDispose(); 82 | } 83 | } 84 | ``` 85 | # 3、基类对象池 86 | 鱼池里有什么鱼? 87 | 88 | 特点: 89 | * 能从对象池中拿出父类对象 90 | 91 | 示例代码: 92 | ``` csharp 93 | //定义一个IObject接口和父类Obj_A,Obj_B 94 | public interface IObject { } 95 | public class Obj_A : IObject { } 96 | public class Obj_B : IObject { } 97 | 98 | //创建基类对象池类型 99 | private class MutiPool : BaseTypePool { } 100 | 101 | //创建对象池 102 | MutiPool pool = new MutiPool(); 103 | //获取Obj_A类型的对象 104 | IObject _obj = pool.Get(); 105 | pool.Set(_obj); 106 | //获取Obj_B类型的对象 107 | _obj = pool.Get(typeof(Obj_B)); 108 | pool.Set(_obj); 109 | ``` 110 | # 4、全局对象池 111 | 我的资源竟然是公家的! 112 | 113 | 特点: 114 | * 对象池不需要自己创建,获取对象直接从框架中申请 115 | * ***注意回收时要自己初始化数据!!!*** 116 | 117 | 示例代码: 118 | ``` csharp 119 | //从全局对象池中获取一个长度为10的数组对象 120 | var arr = Framework.GlobalAllocateArray(10); 121 | //Human类的定义在第一个示例 122 | arr[0] = new Human(33, "吉良吉影"); 123 | arr[0].Say(); 124 | //回收掉这个数组对象 125 | arr.GlobalRecyle(); 126 | 重新从全局对象池中获取长度为10的数组对象 127 | arr = Framework.GlobalAllocateArray(10); 128 | arr[0].Say(); 129 | arr.GlobalRecyle(); 130 | //可以发现是和原先同样的对象 131 | //因此回收的时候需要注意初始化数据!!!! 132 | ``` 133 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/PoolTest.cs) 134 | 135 | 136 | --- 137 | [回到顶部](#) 138 | 139 | -------------------------------------------------------------------------------- /Example-Readme/PriorityQueue.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 优先级队列 4 | > A:请不要插队
5 | B:我有VIP
6 | A:前面全是SVIP在等 7 | 8 | 根据优先级自动排序的队列,消息模块就用到了这个队列来根据紧急程度排序 9 | 10 | ## 1、快速优先级队列 11 | 12 | 特点: 13 | * 优先级值低的排在队列前面 14 | * 相同优先级的节点元素的排序是**随机的** 15 | 16 | 使用示例: 17 | ``` csharp 18 | //节点 19 | public class FastNode : FastPriorityQueueNode 20 | { 21 | public int value; 22 | } 23 | 24 | //创建优先级队列 25 | FastPriorityQueue nodes = new FastPriorityQueue(10); 26 | 27 | //节点元素入队 28 | for (int i = 10; i > 0; i--) 29 | { 30 | nodes.Enqueue(new FastNode() { value = i }, 10 - i); 31 | } 32 | 33 | //节点元素出队 34 | while (nodes.count != 0) 35 | { 36 | Log.L($"出队元素的值为:{nodes.Dequeue().value}"); 37 | } 38 | ``` 39 | 40 | ## 2、泛型优先级队列 41 | 特点: 42 | * 优先级值使用可以对比值大小的泛型 43 | * 优先级值低的排在队列前面 44 | * 相同优先级的节点元素的排序是**先进先出的** 45 | 46 | 使用示例: 47 | ``` csharp 48 | //节点 49 | public class GenericNode : GenericPriorityQueueNode 50 | { 51 | public int value; 52 | } 53 | 54 | //创建队列,这里以char为例 55 | GenericPriorityQueue nodes = new GenericPriorityQueue(10); 56 | //节点元素入队 57 | for (int i = 10; i > 0; i--) 58 | { 59 | nodes.Enqueue(new GenericNode() { value = i }, Convert.ToChar(64 + i)); 60 | } 61 | 62 | //节点元素出队 63 | while (nodes.count != 0) 64 | { 65 | Log.L($"出队元素的值为:{nodes.Dequeue().value}"); 66 | } 67 | ``` 68 | 69 | ## 3、稳定优先级队列 70 | 特点: 71 | * 优先级值低的排在队列前面 72 | * 相同优先级的节点元素的排序是**先进先出的** 73 | 74 | 和快速优先级队列的区别就只在于出现相同优先级的判断…… 75 | 76 | ## 4、简单优先级队列 77 | 特点: 78 | * 队列没有大小限制 79 | * 支持添加空节点元素和重复的节点元素 80 | * 优先级值低的排在队列前面 81 | * 相同优先级的节点元素的排序是**先进先出的** 82 | 83 | 84 | 使用示例: 85 | ``` csharp 86 | //创建队列 87 | SimplePriorityQueue nodes = new SimplePriorityQueue(); 88 | 89 | //节点元素入队 90 | for (int i = 1; i <= 10; i++) 91 | { 92 | nodes.Enqueue($"{i}", i); 93 | } 94 | //空节点元素和重复节点元素入队 95 | string item = "重复值"; 96 | nodes.Enqueue(null, 1); 97 | nodes.Enqueue(null, 3); 98 | nodes.Enqueue(item, 4); 99 | nodes.Enqueue(item, 5); 100 | 101 | //节点元素出队 102 | while (nodes.count != 0) 103 | { 104 | Log.L($"出队元素的值为:{nodes.Dequeue()}"); 105 | } 106 | ``` 107 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/PriorityQueueTest.cs) 108 | 109 | --- 110 | [回到顶部](#) 111 | 112 | -------------------------------------------------------------------------------- /Example-Readme/Record.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 记录模块 4 | 5 | 拿小本本记下一举一动 6 | 7 | 模块特点: 8 | * 撤销与反撤销 9 | * 支持以Command或Action作为操作 10 | * 支持将多个操作合并成一个组调用 11 | 12 | 使用示例: 13 | ``` csharp 14 | //获取模块 15 | IOperationRecorderModule recorderModule=Framework.GetEnv(EnvironmentType.Ev0).modules.Recoder; 16 | 17 | static int value = 0; 18 | 19 | //增加数值Command 20 | struct AddValueCommand : ICommand 21 | { 22 | public void Excute() 23 | { 24 | value++; 25 | } 26 | } 27 | //减少数值Command 28 | struct SubValueCommand : ICommand 29 | { 30 | public void Excute() 31 | { 32 | value--; 33 | } 34 | } 35 | 36 | //注册Action操作和其回退操作 37 | var actionState = recorderModule.AllocateAction().SetCommand(() => { value += 16; }, () => { value -= 16; }); 38 | actionState.SetName("增加了16"); 39 | actionState.Subscribe(); 40 | //State可以多次调用 41 | //但是请【不要】在注册之后修改State,否则会对前面的操作有影响 42 | actionState.Subscribe(); 43 | actionState.Subscribe(); 44 | 45 | //注册Command操作和其回退操作 46 | var commandState = recorderModule.AllocateCommand().SetCommand(new AddValueCommand(), new SubValueCommand()); 47 | commandState.SetName("增加了1"); 48 | commandState.Subscribe(); 49 | 50 | //注册Action一组操作和其回退操作 51 | var actionGroupState = 52 | recorderModule.AllocateActionGroup() 53 | .SetGroupCommand(() => { value += 1; }, () => { value -= 1; }) 54 | .SetGroupCommand(() => { value += 2; }, () => { value -= 2; }) 55 | actionGroupState.SetName("增加了3"); 56 | actionGroupState.Subscribe(); 57 | 58 | Log.L(value); 59 | //撤销操作 返回值为是否撤销成功 60 | bool bo = recorderModule.Undo(); 61 | Log.L(value); 62 | //反撤销操作 返回值为是否反撤销成功 63 | bool bo = recorderModule.Redo(); 64 | Log.L(value); 65 | ``` 66 | [示例代码](https://github.com/OnClick9927/IFramework/blob/master/Framework/Example/Examples/RecorderTest.cs) 67 | 68 | --- 69 | [回到顶部](#) -------------------------------------------------------------------------------- /Example-Readme/RecyclableObject.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 可回收对象 4 | 5 | 6 | 一生尽在掌握 7 | 8 | 特点: 9 | * 从环境中分配出来,用完还回去(内部实现为对象池) 10 | * 可回收对象带有生命周期 11 | 12 | 13 | 使用示例: 14 | ``` csharp 15 | //继承RecyclableObject创建可回收对象类 16 | public class MyObject : RecyclableObject 17 | { 18 | private int value; 19 | protected override void OnAllocate() 20 | { 21 | base.OnAllocate(); 22 | Log.L("OnAllocate"); 23 | //数值变动之后要SetDirty 24 | //没有设置Dirty表示没有变动,回收时不会重置数据 25 | value = 10; 26 | SetDataDirty(); 27 | } 28 | protected override void OnRecyle() 29 | { 30 | Log.L("OnRecyle"); 31 | base.OnRecyle(); 32 | } 33 | protected override void OnDispose() 34 | { 35 | Log.L("OnDispose"); 36 | } 37 | protected override void OnDataReset() 38 | { 39 | //设置对象的重置 40 | Log.L("OnDataReset"); 41 | value = default; 42 | } 43 | 44 | public void PrintValue() 45 | { 46 | Log.L($"value的值为{value}"); 47 | } 48 | } 49 | 50 | 51 | //从环境中分配一个对象 52 | MyObject _object = MyObject.Allocate(EnvironmentType.Ev0); 53 | _object.PrintValue(); 54 | //回收 55 | _object.Recyle(); 56 | ``` 57 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/RecyclableObjectTest.cs) 58 | 59 | --- 60 | [回到顶部](#) -------------------------------------------------------------------------------- /Example-Readme/Serialization.md: -------------------------------------------------------------------------------- 1 | [返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework) 2 | 3 | # 序列化 4 | 5 | 我先变成你,你再变成我,是谁变成了我,而我又变成了谁 6 | ## 1、对象与字符串的互相转化 7 | 支持`struct` 、`class`、`array`、`list`、`dictionary`外加部分集合转换成`string` 8 | 9 | 使用示例: 10 | ``` csharp 11 | HuMan hu = new HuMan() { age = 6, Name = "abc", sex = "sex", heigth = 16, width = 20 }; 12 | Dictionary hus = new Dictionary() { 13 | { 1, hu }, 14 | { 2, hu } 15 | }; 16 | //对象转换成字符串 17 | var str = StringConvert.ConvertToString(hus); 18 | //字符串转换成对象 19 | Dictionary hus2; 20 | bool success=StringConvert.TryConvert(str, out hus2); 21 | ``` 22 | 23 | ## 2、数据表格(csv)的读写 24 | 将数据写入csv文件或者从csv文件还原数据 25 | 26 | 特点: 27 | * 支持表头的重命名、表头忽略、按索引读写等 28 | * 支持自定义分离符 29 | 30 | 使用示例: 31 | ``` csharp 32 | //结构体定义 33 | struct HuMan 34 | { 35 | public int age; 36 | [DataColumnName("The Sex")] 37 | public string sex; 38 | public string Name; 39 | [DataReadColumnIndex(0)] 40 | public int heigth; 41 | [DataIgnore] 42 | [NonSerialized] 43 | public int width; 44 | } 45 | string path = "Mans.csv"; 46 | 47 | //创建一个列表 48 | List cs = new List() 49 | { 50 | new HuMan(){ age=1,sex="m",Name="xm",heigth=0}, 51 | new HuMan(){ age=2,sex="m1",Name="xm1",heigth=0}, 52 | new HuMan(){ age=3,sex="m2",Name="xm2",heigth=0}, 53 | }; 54 | 55 | //写入CSV 56 | var w = DataTableTool.CreateWriter(new System.IO.StreamWriter(path, false), 57 | new DataRow(), 58 | new DataExplainer()); //在这里可以设置分离符 59 | w.Write(cs); 60 | w.Dispose(); 61 | 62 | //读取CSV 63 | var r = 64 | DataTableTool.CreateReader(new System.IO.StreamReader(path, System.Text.Encoding.UTF8), 65 | new DataRow(), 66 | new DataExplainer()); //分离符要与写入时一致 67 | var cc = r.Get(); 68 | r.Dispose(); 69 | ``` 70 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/SerializationTest.cs) 71 | 72 | --- 73 | [回到顶部](#) -------------------------------------------------------------------------------- /Example-Readme/Singleton.md: -------------------------------------------------------------------------------- 1 | [[返回README](../README.md)   [返回主页](https://github.com/OnClick9927/IFramework)](../README.md) 2 | 3 | # 单例 4 | 快速创建单例类,使其全局唯一 5 | 6 | 使用方式:继承`SingletonPropertyClass`<`T`> 7 | 8 | 特点: 9 | * 在第一次调用时会调用构造函数和初始化函数 10 | * 使用静态方法,**不允许**外部访问`instance` 11 | 12 | 使用示例: 13 | ``` csharp 14 | //继承SingletonPropertyClass 15 | public class MySingletonClass : SingletonPropertyClass 16 | { 17 | private int a; 18 | private string b; 19 | private MySingletonClass() 20 | { 21 | //构造函数 22 | a = 1; 23 | b = "this is a string"; 24 | } 25 | 26 | protected override void OnSingletonInit() 27 | { 28 | //初始化 29 | } 30 | 31 | public static void DO() 32 | { 33 | //注意:使用【静态方法】访问实例化对象instance,instance 【不能】 在外部访问 34 | System.Console.WriteLine($"int:{instance.a} string :{instance.b}"); 35 | } 36 | 37 | protected override void OnDispose() 38 | { 39 | 40 | } 41 | } 42 | 43 | //方法调用 44 | MySingletonClass.DO(); 45 | ``` 46 | [示例代码](https://github.com/OnClick9927/IFramework_CS/blob/master/Framework/Example/Examples/SingletonTest.cs) 47 | 48 | --- 49 | [回到顶部](#) -------------------------------------------------------------------------------- /Framework/Example/Example.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {A26FFA53-1C22-4871-93B2-ADE332BEB037} 8 | Exe 9 | Example 10 | Example 11 | v4.8 12 | 512 13 | true 14 | 15 | 16 | 17 | 18 | 19 | AnyCPU 20 | true 21 | full 22 | false 23 | bin\Debug\ 24 | DEBUG;TRACE 25 | prompt 26 | 4 27 | false 28 | 29 | 30 | AnyCPU 31 | pdbonly 32 | true 33 | bin\Release\ 34 | TRACE 35 | prompt 36 | 4 37 | false 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | {cc07690f-50ee-492c-b67f-632b074a8af0} 66 | IFramework 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /Framework/Example/Examples/CoroutineTest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Threading; 3 | using IFramework; 4 | using IFramework.Coroutine; 5 | 6 | namespace Example 7 | { 8 | //协程测试 9 | public class CoroutineTest : Test 10 | { 11 | private ICoroutineModule coroutineModule; //协程模块 12 | private bool flag = false;//用于等待条件的判断 13 | private ICoroutine myCoroutine; 14 | protected override void Start() 15 | { 16 | //获取协程模块 17 | coroutineModule = Framework.GetEnv(EnvironmentType.Ev0).modules.GetModule(); 18 | 19 | //调用协程示例方法 20 | CoroutineExample(); 21 | } 22 | 23 | /// 24 | /// 协程示例方法(异步) 25 | /// 26 | async void CoroutineExample() 27 | { 28 | Log.L("使用协程模块的StartCoroutine方法开始运行协程:\n"); 29 | await coroutineModule.StartCoroutine(StartExample()); 30 | Log.L("协程示例结束"); 31 | } 32 | 33 | /// 34 | /// 协程开始 35 | /// 36 | /// 37 | IEnumerator StartExample() 38 | { 39 | Log.L("协程StartExample开始"); 40 | Log.L("使用嵌套的协程WaitExample\n"); 41 | yield return WaitExample(); 42 | Log.L("协程StartExample结束"); 43 | } 44 | 45 | /// 46 | /// 协程 时间等待 例子 47 | /// 48 | /// 49 | IEnumerator WaitExample() 50 | { 51 | Log.L("协程WaitExample开始"); 52 | 53 | yield return new WaitForFrame(); 54 | Log.L("一帧过去了"); 55 | 56 | yield return new WaitForFrames(20); 57 | Log.L("20帧过去了"); 58 | 59 | yield return new WaitForHours(0.0004); 60 | Log.L("0.0004小时过去了"); 61 | 62 | yield return new WaitForMinutes(0.02); 63 | Log.L("0.02分钟过去了"); 64 | 65 | yield return new WaitForSeconds(3); 66 | Log.L("3秒过去了"); 67 | 68 | yield return new WaitForMilliseconds(2000); 69 | Log.L("2000毫秒过去了"); 70 | 71 | yield return new WaitForTicks(100000000); 72 | Log.L("一亿个一百纳秒过去了"); 73 | 74 | //本质上都是以这个为原型的 75 | yield return new WaitForTimeSpan(new System.TimeSpan(0, 0, 1)); 76 | Log.L("1秒的间隔过去了\n"); 77 | 78 | Log.L("以下开始条件等待的示例"); 79 | Log.L("使用嵌套的协程ConditionExample"); 80 | yield return ConditionExample(); 81 | Log.L("协程WaitExample结束"); 82 | } 83 | 84 | /// 85 | /// 条件等待例子 86 | /// 87 | /// 88 | IEnumerator ConditionExample() 89 | { 90 | flag = false; 91 | Log.L("协程ConditionExample开始"); 92 | myCoroutine = coroutineModule.StartCoroutine(WaitAndChangeFlag()); 93 | 94 | //等待 条件成立 95 | yield return new WaitUtil(CatchFlag); 96 | Log.L("ConditionExample:flag已变为true"); 97 | 98 | //等待 条件不成立 99 | yield return new WaitWhile(CatchFlag); 100 | Log.L("ConditionExample:flag已变为false"); 101 | 102 | Log.L("使用Compelete方法强制停止协程WaitAndChangeFlag"); 103 | myCoroutine.Compelete(); 104 | //coroutineModule.StopCoroutine(myCoroutine); 105 | Log.L("协程ConditionExample结束"); 106 | } 107 | 108 | /// 109 | /// 一定时间后更改flag的值 110 | /// 111 | /// 112 | IEnumerator WaitAndChangeFlag() 113 | { 114 | yield return new WaitForSeconds(3); 115 | flag = true; 116 | Log.L("将flag改为true"); 117 | 118 | yield return new WaitForSeconds(3); 119 | flag = false; 120 | Log.L("将flag改为false"); 121 | 122 | yield return new WaitForSeconds(10); 123 | Log.L("WaitAndChangeFlag结束"); 124 | //但是因为在上面调用了complete提前结束了所以不会调用到 125 | } 126 | 127 | /// 128 | /// 返回flag 129 | /// 130 | /// 131 | bool CatchFlag() 132 | { 133 | //Log.L("当前flag是" + flag); 134 | return flag; 135 | } 136 | 137 | protected override void Stop() 138 | { 139 | } 140 | 141 | protected override void Update() 142 | { 143 | } 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /Framework/Example/Examples/MessageExample.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using IFramework; 3 | using IFramework.Message; 4 | 5 | namespace Example 6 | { 7 | //消息模块示例 8 | public class MessageExample : Test 9 | { 10 | /// 11 | /// 消息类接口 12 | /// 13 | public interface IPub { } 14 | /// 15 | /// 消息类的定义 16 | /// 17 | public class Pub : IPub { } 18 | /// 19 | /// 消息监听器类 20 | /// 21 | public class Listener : IMessageListener 22 | { 23 | public Listener() 24 | { 25 | //以Listen方法为回调监听IPub的消息 26 | Framework.GetEnv(EnvironmentType.Ev0).modules.GetModule().Subscribe(Listen); 27 | 28 | //以监听器类注册监听MessageExample的消息 29 | Framework.GetEnv(EnvironmentType.Ev0).modules.GetModule().Subscribe(this); 30 | Log.L("已对IPub和MessageExample的消息监听进行注册"); 31 | } 32 | 33 | //实现接口里的消息回调方法 34 | public void Listen(IMessage message) 35 | { 36 | Log.L($"收到类型为{message.subject}的消息,消息值为{message.code}"); 37 | } 38 | } 39 | 40 | IMessageModule messageModule; //消息模块 41 | protected override void Start() 42 | { 43 | //获取消息模块 44 | messageModule = Framework.GetEnv(EnvironmentType.Ev0).modules.GetModule(); 45 | 46 | Log.L("设置可以监听注册的类型的所有子类型"); 47 | messageModule.fitSubType = true; 48 | Log.L("设置每帧处理的消息量为1"); 49 | messageModule.processesPerFrame = 1; 50 | 51 | Log.L(""); 52 | Log.L("注册监听使用消息模块的Subscribe方法"); 53 | Log.L("Subscribe方法可以对一个监听器进行注册,也可以对单独的委托方法进行注册"); 54 | Log.L("注意:使用监听器为参数注册的实质是注册了监听器的Listen方法"); 55 | Log.L(""); 56 | Log.L("在消息监听器的构造函数中就可以直接设置对IPub和MessageExample的监听"); 57 | Log.L("开始实例化一个消息监听器"); 58 | 59 | Listener listenner = new Listener(); //实例化一个消息监听器 60 | 61 | Log.L(""); 62 | 63 | Log.L("使用消息模块的UnSubscribe方法可以对监听取消注册"); 64 | //可以对监听器实例进行解绑 65 | messageModule.UnSubscribe(listenner); 66 | //也可以对监听器里的Listen回调方法进行解绑 67 | messageModule.UnSubscribe(listenner.Listen); 68 | Log.L("已取消对IPub和MessageExample的监听"); 69 | 70 | Log.L(""); 71 | 72 | Log.L("也可以用重载的非泛型的Subscribe方法注册监听,取消监听也一样有非泛型的方法"); 73 | messageModule.Subscribe(typeof(IPub), listenner); 74 | messageModule.Subscribe(typeof(MessageExample), listenner.Listen); 75 | 76 | Log.L("已重新对IPub和MessageExample的消息监听进行注册"); 77 | Log.L(""); 78 | 79 | Log.L("以Pub的名义发送一个消息值为100的消息"); 80 | messageModule.Publish(null) 81 | .SetCode(100) 82 | .OnCompelete((msg) => 83 | { 84 | Log.L("可以看到我们只监听了IPub但是还是收到了Pub的消息"); 85 | Log.L($"当前消息的状态为{msg.state} 错误码为{msg.errorCode}"); 86 | Log.L(""); 87 | Log.L("发送的消息可以添加MessageUrgencyType参数来提高优先级"); 88 | Log.L("紧急程度为立即的消息不受processesPerFrame规则影响"); 89 | Log.L("----------------开始按键----------------"); 90 | Log.L("按键之后将分别发送三条普通、三条不重要、三条非常紧急、一条立即"); 91 | Log.L("然后再以异步的方式发送一条重要的消息"); 92 | }); 93 | 94 | } 95 | 96 | //此处的Update支持异步调用 97 | protected async override void Update() 98 | { 99 | var key = Console.ReadKey(); 100 | if (key.Key == ConsoleKey.Escape) return; 101 | Log.L($"检测到按键 {key.Key} 并且发送消息 , 按键序号为 {(int)key.Key}"); 102 | 103 | Log.L($"剩余消息条数 {messageModule.count}"); 104 | SendMessage(); 105 | 106 | Log.L("开始发送紧急类型为重要的一条消息"); 107 | await messageModule.PublishByNumber(this, null, (int)MessageUrgencyType.Important).SetCode(10); 108 | Log.L("已发送紧急类型为重要的一条消息"); 109 | 110 | } 111 | /// 112 | /// 发送三条普通、三条不重要、三条非常紧急程度的消息 113 | /// 114 | private void SendMessage() 115 | { 116 | for (int i = 0; i < 3; i++) 117 | { 118 | messageModule.Publish(this, null, 0, MessageUrgencyType.Common).SetCode(i + 1); 119 | } 120 | Log.L("已发送紧急类型为普通的三条消息"); 121 | for (int i = 0; i < 3; i++) 122 | { 123 | messageModule.Publish(null, 0, MessageUrgencyType.Unimportant).SetCode(i + 4); 124 | } 125 | Log.L("已发送紧急类型为不重要的三条消息"); 126 | for (int i = 0; i < 3; i++) 127 | { 128 | messageModule.Publish(this, null, 0, MessageUrgencyType.VeryUrgent).SetCode(i + 7); 129 | } 130 | Log.L("已发送紧急类型为非常紧急的三条消息"); 131 | 132 | messageModule.PublishByNumber(this, null, MessageUrgency.Immediately); 133 | Log.L("已发送紧急类型为立即的一条消息"); 134 | Log.L("立即执行的消息不在等待队列中,因此无法设置值\n"); 135 | 136 | } 137 | 138 | protected override void Stop() 139 | { 140 | 141 | } 142 | } 143 | 144 | } 145 | -------------------------------------------------------------------------------- /Framework/Example/Examples/MouduleTest.cs: -------------------------------------------------------------------------------- 1 | using IFramework; 2 | using IFramework; 3 | 4 | namespace Example 5 | { 6 | //自定义模块示例 7 | public class MouduleTest : Test 8 | { 9 | /// 10 | /// 自定义模块类 11 | /// 12 | private class MyModule : IFramework.UpdateModule 13 | { 14 | protected override void Awake() 15 | { 16 | Log.L("这是模块Awake方法的调用" + GetHashCode()); 17 | } 18 | protected override void OnEnable() 19 | { 20 | Log.L("这是模块OnEnable方法的调用" + GetHashCode()); 21 | 22 | } 23 | protected override void OnUpdate() 24 | { 25 | Log.L("这是模块OnUpdate方法的调用" + GetHashCode()); 26 | 27 | } 28 | protected override void OnDisable() 29 | { 30 | Log.L("这是模块OnDisable方法的调用" + GetHashCode()); 31 | 32 | } 33 | public void Say() 34 | { 35 | Log.L("说了句话……" + GetHashCode()); 36 | 37 | } 38 | protected override void OnDispose() 39 | { 40 | Log.L("这是模块OnDispose方法的调用" + GetHashCode()); 41 | 42 | } 43 | } 44 | 45 | //获取模块 46 | IModules moduleContainer { get { return Framework.GetEnv(EnvironmentType.Ev0).modules; } } 47 | protected override void Start() 48 | { 49 | Log.L("模块的创建需要继承IFramework.Module这个类\n"); 50 | Log.L("如果需要有生命周期的管理则可以继承IFramework.UpdateModule这个类"); 51 | Log.L("并对其Awake、OnEnable、OnUpdate、OnDisable、OnDispose这几个生命周期进行重写\n"); 52 | 53 | Log.L("用module的模块容器的CreateModule方法创建对应的模块\n"); 54 | moduleContainer.CreateModule(); 55 | 56 | Log.L(""); 57 | Log.L("用module的模块容器的FindModule方法获取容器内对应的对象"); 58 | var module = moduleContainer.FindModule(); 59 | 60 | Log.L("模块HashCodde是" + module.GetHashCode()); 61 | 62 | Log.L(""); 63 | Log.L("调用模块的Say方法:"); 64 | module.Say(); 65 | 66 | //Log.L("使用模块的UnBind方法将模块从容器中去除"); 67 | //Log.L("UnBind方法传入参数dispose默认为true,在没有任何引用时释放掉这个对象"); 68 | //module.UnBind(false); 69 | 70 | //Log.L("可以使用Bind方法将模块绑定到容器中\n"); 71 | //module.Bind(moduleContainer); 72 | 73 | //module.UnBind(); 74 | //Log.L("由于当前方法块中还存在module变量引用这个模块,所以还是能调用模块的Say方法"); 75 | //module.Say(); 76 | 77 | Log.L(""); 78 | Log.L("使用容器的GetModule方法可以获取容器中对应的模块,如果不存在则自动创建"); 79 | Log.L("指定name属性可以注册多个同一类型的模块"); 80 | for (int i = 0; i < 10; i++) 81 | { 82 | Log.L(moduleContainer.GetModule("XXL").GetHashCode()); 83 | } 84 | } 85 | 86 | protected override void Stop() 87 | { 88 | } 89 | 90 | protected override void Update() 91 | { 92 | } 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /Framework/Example/Examples/RecorderTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using IFramework; 3 | using IFramework.Message; 4 | using IFramework.Recorder; 5 | 6 | namespace Example 7 | { 8 | //记录模块例子(撤销与反撤销) 9 | public class RecorderTest : Test 10 | { 11 | static int value = 0; //本例子的主角,全都改在它身上 12 | 13 | //让Value增加1的Command操作 14 | struct AddValueCommand : ICommand 15 | { 16 | public void Excute() 17 | { 18 | value++; 19 | } 20 | } 21 | 22 | //让Value减少1的Command操作 23 | struct SubValueCommand : ICommand 24 | { 25 | public void Excute() 26 | { 27 | value--; 28 | } 29 | } 30 | 31 | //获取模块 32 | IOperationRecorderModule recorderModule { get { return Framework.GetEnv(EnvironmentType.Ev0).modules.GetModule(); } } 33 | 34 | protected override void Start() 35 | { 36 | 37 | Log.L("操作分为CommandState、ActionState、CommandGroupState、ActionGroupState"); 38 | Log.L("State为一次操作的声明,其中包含了Redo和Undo两个步骤,也就是操作和撤回"); 39 | Log.L("使用State的Subscribe方法将操作注册到记录模块中,此时就会开始State的Redo步骤"); 40 | 41 | Log.L($"例子开始:Value的值是{value} \n"); 42 | 43 | Log.L("注册一个ActionState,使Value的值增加16,并给这一步操作赋予一个名字 "); 44 | var actionState = recorderModule.AllocateAction().SetCommand(() => { value += 16; }, () => { value -= 16; }); 45 | actionState.SetName("增加了16"); 46 | actionState.Subscribe(); 47 | Log.L($"此时Value的值为{value} \n"); 48 | 49 | Log.L("注册一个CommandState,使Value的值增加1"); 50 | var commandState = recorderModule.AllocateCommand().SetCommand(new AddValueCommand(), new SubValueCommand()); 51 | commandState.SetName("增加了1"); 52 | commandState.Subscribe(); 53 | Log.L($"此时Value的值为{value} \n"); 54 | 55 | Log.L("注册一组ActionState,使Value的值依次增加1、2、3"); 56 | var actionGroupState = recorderModule.AllocateActionGroup() 57 | .SetGroupCommand(() => { value += 1; }, () => { value -= 1; }) 58 | .SetGroupCommand(() => { value += 2; }, () => { value -= 2; }) 59 | .SetGroupCommand(() => { value += 3; }, () => { value -= 3; }); 60 | actionGroupState.SetName("增加了6"); 61 | actionGroupState.Subscribe(); 62 | Log.L($"此时Value的值为{value} \n"); 63 | 64 | Log.L("注册一组CommandState,使Value的值增加三次1"); 65 | var commandGroupState = recorderModule.AllocateCommandGroup() 66 | .SetGroupCommand(new AddValueCommand(), new SubValueCommand()) 67 | .SetGroupCommand(new AddValueCommand(), new SubValueCommand()) 68 | .SetGroupCommand(new AddValueCommand(), new SubValueCommand()); 69 | commandGroupState.SetName("增加了3"); 70 | commandGroupState.Subscribe(); 71 | Log.L($"此时Value的值为{value} \n"); 72 | 73 | Log.L("保存的state可以多次注册,将commandGroupState调用三次"); 74 | commandGroupState.Subscribe(); 75 | commandGroupState.Subscribe(); 76 | commandGroupState.Subscribe(); 77 | Log.L($"此时Value的值为{value} \n"); 78 | 79 | 80 | Log.L("按A撤销,按D反撤销,按S注册新的操作,按W获取当前操作记录\n"); 81 | } 82 | 83 | protected override void Stop() 84 | { 85 | } 86 | 87 | protected override void Update() 88 | { 89 | if (Console.ReadKey().Key == ConsoleKey.A) 90 | { 91 | bool bo = recorderModule.Undo(); 92 | Log.L(bo?"撤销成功" : "撤销失败"); 93 | Log.L($"此时Value的值为{value},当前的操作是{recorderModule.GetCurrentRecordName()}\n"); 94 | } 95 | if (Console.ReadKey().Key == ConsoleKey.D) 96 | { 97 | bool bo = recorderModule.Redo(); 98 | Log.L(bo ? "反撤销成功" : "反撤销失败"); 99 | Log.L($"此时Value的值为{value},当前的操作是{recorderModule.GetCurrentRecordName()}\n"); 100 | } 101 | if (Console.ReadKey().Key == ConsoleKey.S) 102 | { 103 | int i = new Random().Next(20); 104 | var state = recorderModule.AllocateAction().SetCommand(() => { value += i; }, () => { value -= i; }); 105 | state.SetName($"增加了{i}"); 106 | state.Subscribe(); 107 | Log.L($"此时Value的值为{value},当前的操作是{recorderModule.GetCurrentRecordName()}\n"); 108 | } 109 | if (Console.ReadKey().Key == ConsoleKey.W) 110 | { 111 | var records = recorderModule.GetRecordNames(out int index); 112 | Log.L("当前的操作列表为:"); 113 | foreach (var item in records) 114 | { 115 | Console.WriteLine(item); 116 | } 117 | Console.WriteLine($"当前的操作位置为:{index}"); 118 | } 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /Framework/Example/Examples/SingletonTest.cs: -------------------------------------------------------------------------------- 1 | using IFramework; 2 | using IFramework.Singleton; 3 | 4 | namespace Example 5 | { 6 | public class SingletonTest : Test 7 | { 8 | 9 | public class MySingletonClass : SingletonPropertyClass 10 | { 11 | private int a; 12 | private string b; 13 | private MySingletonClass() 14 | { 15 | Log.L("*构造函数调用方法"); 16 | a = 1; 17 | b = "this is a string"; 18 | } 19 | 20 | protected override void OnSingletonInit() 21 | { 22 | Log.L("*单例被创建时调用方法"); 23 | } 24 | 25 | public static void DO() 26 | { 27 | Log.L("注意:使用静态方法访问实例化对象instance,instance不能在外部访问"); 28 | Log.L($"*调用Do方法,获取instance实例化对象的的字段值:int:{instance.a} string :{instance.b}"); 29 | } 30 | protected override void OnDispose() 31 | { 32 | 33 | } 34 | } 35 | protected override void Start() 36 | { 37 | System.Console.WriteLine(); 38 | 39 | MySingletonClass.DO(); 40 | 41 | System.Console.WriteLine(); 42 | Log.L("以上流程的执行顺序为:"); 43 | Log.L("1、调用静态方法Do"); 44 | Log.L("2、开始内部实例化出对象instance"); 45 | Log.L("3、调用构造函数"); 46 | Log.L("4、调用OnSingletonInit"); 47 | Log.L("5、DO方法调用完毕"); 48 | } 49 | 50 | protected override void Stop() 51 | { 52 | } 53 | 54 | protected override void Update() 55 | { 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Framework/Example/Examples/TimerTest.cs: -------------------------------------------------------------------------------- 1 | using IFramework; 2 | using IFramework; 3 | using IFramework.Timer; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Example 11 | { 12 | public class TimerTest : Test 13 | { 14 | IModules moduleContainer { get { return Framework.GetEnv(EnvironmentType.Ev0).modules; } } 15 | protected override void Start() 16 | { 17 | moduleContainer.CreateModule(); 18 | 19 | var timeModule = moduleContainer.GetModule(); 20 | 21 | ITimerEntity actionItem = timeModule.Allocate(say, 4000,1,5000f,3f); 22 | actionItem.SetTimeScale(2f); 23 | ITimerEntity actionItem1 = timeModule.Allocate(say1, 4000, 5, 2f); 24 | ITimerEntity actionItem2 = timeModule.Allocate(say2, 4000, -1, 2f); 25 | actionItem1.SetInnerTimer(actionItem2); 26 | actionItem1.Subscribe(); 27 | actionItem.Subscribe(); 28 | } 29 | 30 | void say() 31 | { 32 | Log.L($"111111一次调用{DateTime.Now.ToString("HH:mm:ss,FFFF")}"); 33 | } 34 | 35 | int n = 0; 36 | void say1() 37 | { 38 | n++; 39 | Log.L($"22222第{n}次调用{DateTime.Now.ToString("HH:mm:ss,FFFF")}"); 40 | } 41 | 42 | int m = 0; 43 | void say2() 44 | { 45 | m++; 46 | Log.L($"333333第{m}次调用{DateTime.Now.ToString("HH:mm:ss,FFFF")}"); 47 | } 48 | 49 | protected override void Stop() 50 | { 51 | 52 | } 53 | 54 | protected override void Update() 55 | { 56 | 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Framework/Example/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Timers; 4 | using IFramework; 5 | 6 | namespace Example 7 | { 8 | public abstract class Test : Unit 9 | { 10 | public Test() 11 | { 12 | Framework.BindEnvUpdate(Update, EnvironmentType.Ev0); 13 | Framework.BindEnvDispose(Dispose, EnvironmentType.Ev0); 14 | 15 | Log.L("开始测试----------->" + GetType()); 16 | Start(); 17 | } 18 | protected abstract void Start(); 19 | 20 | protected abstract void Update(); 21 | protected abstract void Stop(); 22 | 23 | protected override void OnDispose() 24 | { 25 | Stop(); 26 | Framework.UnBindEnvDispose(Dispose, EnvironmentType.Ev0); 27 | 28 | Framework.UnBindEnvUpdate(Update, EnvironmentType.Ev0); 29 | } 30 | } 31 | class Program 32 | { 33 | private static System.Timers.Timer timer = new System.Timers.Timer(10); 34 | static void Main(string[] args) 35 | { 36 | timer.Elapsed += Timer_Elapsed; 37 | Log.L("按键盘 esc 关闭测试环境 "); 38 | Log.L("开启 0 号环境 测试 "); 39 | var env= Framework.CreateEnv(EnvironmentType.Ev0); 40 | env.InitWithAttribute(); 41 | timer.Start(); 42 | 43 | TestScripts(); 44 | while (Console.ReadKey().Key != ConsoleKey.Escape) { } 45 | Framework.GetEnv(EnvironmentType.Ev0).Dispose(); 46 | Log.L(" 0 号环境 已经关闭 "); 47 | 48 | Log.L("按键盘 esc 退出 "); 49 | while (Console.ReadKey().Key != ConsoleKey.Escape) { } 50 | } 51 | private static void Timer_Elapsed(object sender, ElapsedEventArgs e) 52 | { 53 | Framework.GetEnv(EnvironmentType.Ev0).Update(); 54 | } 55 | 56 | private static void TestScripts() 57 | { 58 | // new SerializationTest(); // 数据表 CSV 59 | //new SingletonTest(); //单例测试 60 | //new PoolTest(); //对象池测试 61 | // new MessageExample(); //消息模块 62 | 63 | // new RecorderTest(); //操作记录模块 64 | //new InjectTest(); //依赖注入 65 | //new CoroutineTest(); //协程 模块 66 | //new FsmTest(); //状态机模块 67 | //new MouduleTest(); //自定义模块 68 | //new RecyclableObjectTest(); //可回收对象测试 69 | 70 | //new BindTest(); //数据绑定 (单向/数据变化监听,双向/数值同步变化) 1 71 | //new MvvmTest(); 1 72 | //new PriorityQueueTest(); //优先级队列 1 73 | // new NetTest(); // 网络测试 1 74 | new TimerTest(); 75 | } 76 | 77 | } 78 | 79 | 80 | 81 | } 82 | -------------------------------------------------------------------------------- /Framework/Example/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("Example")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Example")] 13 | [assembly: AssemblyCopyright("Copyright © 2021")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("a26ffa53-1c22-4871-93b2-ade332beb037")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Framework/Example/app.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /Framework/IFramework.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.1.32210.238 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IFramework", "IFramework\IFramework.csproj", "{CC07690F-50EE-492C-B67F-632B074A8AF0}" 7 | EndProject 8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Example", "Example\Example.csproj", "{A26FFA53-1C22-4871-93B2-ADE332BEB037}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {CC07690F-50EE-492C-B67F-632B074A8AF0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {CC07690F-50EE-492C-B67F-632B074A8AF0}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {CC07690F-50EE-492C-B67F-632B074A8AF0}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {CC07690F-50EE-492C-B67F-632B074A8AF0}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {A26FFA53-1C22-4871-93B2-ADE332BEB037}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {A26FFA53-1C22-4871-93B2-ADE332BEB037}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {A26FFA53-1C22-4871-93B2-ADE332BEB037}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {A26FFA53-1C22-4871-93B2-ADE332BEB037}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {6883D5EF-3A9B-4499-AF69-3439DB7F24C8} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Environment/Environment.cs: -------------------------------------------------------------------------------- 1 | using IFramework; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Reflection; 6 | using System.Runtime.CompilerServices; 7 | 8 | namespace IFramework 9 | { 10 | /// 11 | /// 框架运行环境 12 | /// 13 | class Environment : Unit, IEnvironment 14 | { 15 | private bool _inited; 16 | private Modules _modules; 17 | private EnvironmentType _envType; 18 | private LoomModule _loom; 19 | private event Action update; 20 | private event Action onDispose; 21 | private static object _envsetlock = new object(); 22 | private static Environment _current; 23 | private TimeCalculator _time; 24 | /// 25 | /// 环境是否已经初始化 26 | /// 27 | public bool inited { get { return _inited; } } 28 | 29 | /// 30 | /// 环境下自带的模块容器 31 | /// 32 | public IModules modules { get { return _modules; } } 33 | /// 34 | /// 环境类型 35 | /// 36 | public EnvironmentType envType { get { return _envType; } } 37 | 38 | 39 | 40 | /// 41 | /// 当前环境 42 | /// 43 | public static Environment current 44 | { 45 | get { return _current; } 46 | private set 47 | { 48 | lock (_envsetlock) 49 | { 50 | _current = value; 51 | } 52 | } 53 | } 54 | 55 | public ITimeCalculator time { get { return _time; } } 56 | 57 | /// 58 | /// ctor 59 | /// 60 | /// 61 | public Environment(EnvironmentType envType) 62 | { 63 | this._envType = envType; 64 | } 65 | 66 | /// 67 | /// 初始化环境,3.5 使用 68 | /// 69 | /// 需要初始化调用的静态类 70 | public void Init(IEnumerable types) 71 | { 72 | if (_inited) return; 73 | current = this; 74 | _modules = new Modules(); 75 | //cycleCollection = new RecyclableObjectCollection(); 76 | if (types != null) 77 | { 78 | foreach (var type in types) 79 | { 80 | TypeAttributes ta = type.Attributes; 81 | if ((ta & TypeAttributes.Abstract) != 0 && ((ta & TypeAttributes.Sealed) != 0)) 82 | RuntimeHelpers.RunClassConstructor(type.TypeHandle); 83 | } 84 | } 85 | _loom = LoomModule.CreatInstance(""); 86 | _time = new TimeCalculator(); 87 | _inited = true; 88 | } 89 | /// 90 | /// 初始化环境,4.X 使用 91 | /// 92 | public void InitWithAttribute() 93 | { 94 | var types = AppDomain.CurrentDomain.GetAssemblies() 95 | .SelectMany(item => item.GetTypes()) 96 | .Where(item => item.IsDefined(typeof(OnEnvironmentInitAttribute), false)) 97 | .Select((type) => 98 | { 99 | var attr = type.GetCustomAttributes(typeof(OnEnvironmentInitAttribute), false).First() as OnEnvironmentInitAttribute; 100 | var _type = attr.type; 101 | if ((_type & this.envType) != 0 || (_type & EnvironmentType.None) != 0) 102 | return type; 103 | return null; 104 | }).ToList(); 105 | types.RemoveAll((type) => { return type == null; }); 106 | Init(types); 107 | } 108 | /// 109 | /// 释放 110 | /// 111 | protected override void OnDispose() 112 | { 113 | if (disposed || !inited) return; 114 | 115 | if (onDispose != null) onDispose(); 116 | _modules.Dispose(); 117 | _loom.Dispose(); 118 | _time.Dispose(); 119 | _modules = null; 120 | update = null; 121 | onDispose = null; 122 | } 123 | /// 124 | /// 刷新环境 125 | /// 126 | public void Update() 127 | { 128 | if (disposed) return; 129 | current = this; 130 | _time.BeginDelta(); 131 | _loom.Update(); 132 | _modules.Update(); 133 | if (update != null) update(); 134 | _time.EndDelta(); 135 | } 136 | 137 | /// 138 | /// 等待 环境的 update,即等到该环境的线程来临 139 | /// 140 | /// 141 | public void WaitEnvironmentFrame(T action) 142 | { 143 | if (disposed) return; 144 | _loom.RunDelay(action); 145 | } 146 | public void SubscribeWaitEnvironmentFrameHandler(Action action) 147 | { 148 | if (disposed) return; 149 | _loom.AddDelayHandler(action); 150 | } 151 | public void UnSubscribeWaitEnvironmentFrameHandler(Action action) 152 | { 153 | if (disposed) return; 154 | _loom.RemoveDelayHandler(action); 155 | } 156 | 157 | /// 158 | /// 绑定帧 159 | /// 160 | /// 161 | public void BindUpdate(Action action) 162 | { 163 | if (disposed) return; 164 | update += action; 165 | } 166 | /// 167 | /// 移除绑定帧 168 | /// 169 | /// 170 | public void UnBindUpdate(Action action) 171 | { 172 | if (disposed) return; 173 | update -= action; 174 | } 175 | /// 176 | /// 绑定dispose 177 | /// 178 | /// 179 | public void BindDispose(Action action) 180 | { 181 | if (disposed) return; 182 | onDispose += action; 183 | } 184 | /// 185 | /// 移除绑定dispose 186 | /// 187 | /// 188 | public void UnBindDispose(Action action) 189 | { 190 | if (disposed) return; 191 | onDispose -= action; 192 | } 193 | } 194 | } 195 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Environment/EnvironmentType.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | /// 6 | /// 环境类型 7 | /// 8 | [Flags] 9 | public enum EnvironmentType : int 10 | { 11 | /// 12 | /// 所有,配合环境初始化 13 | /// 14 | None = 1, 15 | /// 16 | /// 环境0 17 | /// 18 | Ev0 = 2, 19 | /// 20 | /// 环境1 21 | /// 22 | Ev1 = 4, 23 | /// 24 | /// 环境2 25 | /// 26 | Ev2 = 8, 27 | /// 28 | /// 环境3 29 | /// 30 | Ev3 = 16, 31 | /// 32 | /// 环境4 33 | /// 34 | Ev4 = 32, 35 | /// 36 | /// 环境5 37 | /// 38 | Ev5 = 64, 39 | /// 40 | /// 环境6 41 | /// 42 | Ev6 = 128, 43 | /// 44 | /// 环境7 45 | /// 46 | Ev7 = 256, 47 | /// 48 | /// 环境8 49 | /// 50 | Ev8 = 512, 51 | /// 52 | /// 环境9 53 | /// 54 | Ev9 = 1024, 55 | 56 | /// 57 | /// 环境10 58 | /// 59 | Ev10 = 2048, 60 | /// 61 | /// 62 | /// 63 | Ev11 = 4096, 64 | /// 65 | /// 66 | /// 67 | Ev12 = 8192, 68 | 69 | /// 70 | /// 额外的1 71 | /// 72 | Extra0 = 16384, 73 | /// 74 | /// 额外的1 75 | /// 76 | Extra1 = 32768, 77 | /// 78 | /// 额外的2 79 | /// 80 | Extra2 = 65536, 81 | /// 82 | /// 额外的3 83 | /// 84 | Extra3 = 131072, 85 | /// 86 | /// 额外的4 87 | /// 88 | Extra4 = 262144, 89 | /// 90 | /// 91 | /// 92 | Extra5 = 524288, 93 | } 94 | 95 | } 96 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Environment/Framework.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework 5 | { 6 | /// 7 | /// 框架入口 8 | /// 9 | public static class Framework 10 | { 11 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 12 | private static Dictionary envs = new Dictionary(); 13 | private static object _lock = new object(); 14 | 15 | public static IEnvironment current { get { return Environment.current; } } 16 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 17 | /// 18 | /// 实例化环境 19 | /// 20 | /// 环境类型 21 | /// 环境 22 | public static IEnvironment CreateEnv(EnvironmentType envType) 23 | { 24 | lock (_lock) 25 | { 26 | IEnvironment env; 27 | if (envs.TryGetValue((int)envType, out env)) 28 | { 29 | throw new Exception(string.Format("The EnvironmentType {0} is not null ", envType)); 30 | } 31 | else 32 | { 33 | env = new Environment(envType); 34 | envs.Add((int)envType, env); 35 | return env; 36 | } 37 | } 38 | } 39 | /// 40 | /// 根据序号获取环境 41 | /// 42 | /// 环境类型 43 | /// 44 | public static IEnvironment GetEnv(EnvironmentType envType) 45 | { 46 | lock (_lock) 47 | { 48 | IEnvironment env; 49 | if (envs.TryGetValue((int)envType, out env)) 50 | { 51 | return env; 52 | } 53 | else 54 | { 55 | throw new Exception(string.Format("The EnvironmentType {0} Error Not Find ,Please Check ", envType)); 56 | } 57 | } 58 | } 59 | /// 60 | /// 释放环境 61 | /// 62 | /// 63 | public static void DisposeEnv(EnvironmentType envType) 64 | { 65 | var env = GetEnv(envType); 66 | if (env != null) 67 | { 68 | env.Dispose(); 69 | lock (_lock) 70 | { 71 | envs.Remove((int)envType); 72 | } 73 | } 74 | } 75 | 76 | /// 77 | /// 绑顶 方法 到一个环境的 Update 78 | /// 79 | /// 方法 80 | /// 环境 81 | public static void BindEnvUpdate(this Action action, IEnvironment env) 82 | { 83 | env.BindUpdate(action); 84 | } 85 | /// 86 | /// 解除绑顶 方法 到一个环境的 Update 87 | /// 88 | /// 方法 89 | /// 环境 90 | public static void UnBindEnvUpdate(this Action action, IEnvironment env) 91 | { 92 | env.UnBindUpdate(action); 93 | } 94 | /// 95 | /// 绑顶 方法 到一个环境的 Dispose 96 | /// 97 | /// 方法 98 | /// 环境 99 | public static void BindEnvDispose(this Action action, IEnvironment env) 100 | { 101 | env.BindDispose(action); 102 | } 103 | /// 104 | /// 解除绑顶 方法 到一个环境的 Dispose 105 | /// 106 | /// 方法 107 | /// 环境 108 | public static void UnBindEnvDispose(this Action action, IEnvironment env) 109 | { 110 | env.UnBindDispose(action); 111 | } 112 | 113 | /// 114 | /// 绑顶 方法 到一个环境的 Update 115 | /// 116 | /// 方法 117 | /// 118 | public static void BindEnvUpdate(this Action action, EnvironmentType envType) 119 | { 120 | action.BindEnvUpdate(GetEnv(envType)); 121 | } 122 | /// 123 | /// 解除绑顶 方法 到一个环境的 Update 124 | /// 125 | /// 方法 126 | /// 127 | public static void UnBindEnvUpdate(this Action action, EnvironmentType envType) 128 | { 129 | action.UnBindEnvUpdate(GetEnv(envType)); 130 | } 131 | /// 132 | /// 绑顶 方法 到一个环境的 Dispose 133 | /// 134 | /// 方法 135 | /// 136 | public static void BindEnvDispose(this Action action, EnvironmentType envType) 137 | { 138 | action.BindEnvDispose(GetEnv(envType)); 139 | } 140 | /// 141 | /// 解除绑顶 方法 到一个环境的 Dispose 142 | /// 143 | /// 方法 144 | /// 145 | public static void UnBindEnvDispose(this Action action, EnvironmentType envType) 146 | { 147 | action.UnBindEnvDispose(GetEnv(envType)); 148 | } 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Environment/IEnvironment.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using IFramework; 4 | 5 | namespace IFramework 6 | { 7 | /// 8 | /// 环境 9 | /// 10 | public interface IEnvironment: IDisposable 11 | { 12 | /// 13 | /// 环境类型 14 | /// 15 | EnvironmentType envType { get; } 16 | /// 17 | /// 是否初始化完成 18 | /// 19 | bool inited { get; } 20 | /// 21 | /// 模块容器 22 | /// 23 | IModules modules { get; } 24 | /// 25 | /// 时间 26 | /// 27 | ITimeCalculator time { get; } 28 | /// 29 | /// 绑定 Dispose 30 | /// 31 | /// 32 | void BindDispose(Action action); 33 | /// 34 | /// 绑定 Update 35 | /// 36 | /// 37 | void BindUpdate(Action action); 38 | /// 39 | /// 初始化 40 | /// 41 | /// 42 | void Init(IEnumerable types); 43 | /// 44 | /// 初始化 45 | /// 46 | void InitWithAttribute(); 47 | /// 48 | /// 解绑 Dispose 49 | /// 50 | /// 51 | void UnBindDispose(Action action); 52 | /// 53 | /// 解绑 Update 54 | /// 55 | /// 56 | void UnBindUpdate(Action action); 57 | /// 58 | /// 刷新 59 | /// 60 | void Update(); 61 | /// 62 | /// 等待环境刷新 63 | /// 64 | /// 65 | void WaitEnvironmentFrame(T action); 66 | /// 67 | /// 68 | /// 69 | /// 70 | /// 71 | void UnSubscribeWaitEnvironmentFrameHandler(Action action); 72 | /// 73 | /// 74 | /// 75 | /// 76 | /// 77 | void SubscribeWaitEnvironmentFrameHandler(Action action); 78 | } 79 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Environment/ITimeCalculator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | /// 6 | /// 时间计算 7 | /// 8 | public interface ITimeCalculator 9 | { 10 | /// 11 | /// 每次刷新的时间 12 | /// 13 | TimeSpan deltaTime { get; } 14 | /// 15 | /// 初始化-现在时间 16 | /// 17 | TimeSpan timeSinceInit { get; } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Environment/OnEnvironmentInitAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | /// 6 | /// 环境初始化时候调用被标记的静态类 7 | /// 8 | [AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] 9 | public class OnEnvironmentInitAttribute : Attribute 10 | { 11 | /// 12 | /// 配合初始化的版本 0, 13 | /// 默认初始化,其他自行规定,用于区分环境, 14 | /// 一般某个环境特有的静态类和环境编号一致 15 | /// 16 | public EnvironmentType type { get; } 17 | /// 18 | /// Ctor 19 | /// 20 | /// 21 | public OnEnvironmentInitAttribute(EnvironmentType type = EnvironmentType.None) 22 | { 23 | this.type = type; 24 | } 25 | } 26 | 27 | 28 | } 29 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Environment/TimeCalculator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | 4 | namespace IFramework 5 | { 6 | class TimeCalculator : Unit, ITimeCalculator 7 | { 8 | private Stopwatch sw_init; 9 | private Stopwatch sw_delta; 10 | 11 | public TimeCalculator() 12 | { 13 | deltaTime = TimeSpan.Zero; 14 | sw_delta = new Stopwatch(); 15 | sw_init = new Stopwatch(); 16 | sw_init.Start(); 17 | } 18 | public TimeSpan deltaTime { get; private set; } 19 | public TimeSpan timeSinceInit 20 | { 21 | get 22 | { 23 | if (sw_init == null) return TimeSpan.Zero; 24 | sw_init.Stop(); 25 | var span = sw_init.Elapsed; 26 | sw_init.Start(); 27 | return span; 28 | } 29 | } 30 | 31 | public void BeginDelta() 32 | { 33 | sw_delta.Reset(); 34 | sw_delta.Start(); 35 | } 36 | public void EndDelta() 37 | { 38 | sw_delta.Stop(); 39 | deltaTime = sw_delta.Elapsed; 40 | } 41 | 42 | protected override void OnDispose() 43 | { 44 | 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Ex.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Reflection; 6 | 7 | namespace IFramework 8 | { 9 | public static class Ex 10 | { 11 | /// 12 | /// 拼接路径 13 | /// 14 | /// 15 | /// 16 | /// 17 | public static string CombinePath(this string path, string toCombinePath) 18 | { 19 | return Path.Combine(path, toCombinePath).ToRegularPath(); 20 | } 21 | 22 | 23 | /// 24 | /// 规范路径 25 | /// 26 | /// 27 | /// 28 | public static string ToRegularPath(this string path) 29 | { 30 | path = path.Replace('\\', '/'); 31 | return path; 32 | } 33 | 34 | 35 | 36 | /// 37 | /// 获取当前程序集中的类型的子类,3.5有问题 38 | /// 39 | /// 40 | /// 41 | public static IEnumerable GetSubTypesInAssembly(this Type self) 42 | { 43 | if (self.IsInterface) 44 | return Assembly.GetExecutingAssembly() 45 | .GetTypes() 46 | .Where(item => item.GetInterfaces().Contains(self)); 47 | return Assembly.GetExecutingAssembly() 48 | .GetTypes() 49 | .Where(item => item.IsSubclassOf(self)); 50 | } 51 | /// 52 | /// 获取所有程序集中的类型的子类,3.5有问题 53 | /// 54 | /// 55 | /// 56 | public static IEnumerable GetSubTypesInAssemblys(this Type self) 57 | { 58 | if (self.IsInterface) 59 | return AppDomain.CurrentDomain.GetAssemblies() 60 | .SelectMany(item => item.GetTypes()) 61 | .Where(item => item.GetInterfaces().Contains(self)); 62 | return AppDomain.CurrentDomain.GetAssemblies() 63 | .SelectMany(item => item.GetTypes()) 64 | .Where(item => item.IsSubclassOf(self)); 65 | } 66 | 67 | 68 | /// 69 | /// 字符串结尾转Unix编码 70 | /// 71 | /// 72 | /// 73 | public static string ToUnixLineEndings(this string self) 74 | { 75 | return self.Replace("\r\n", "\n").Replace("\r", "\n"); 76 | } 77 | 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/IAwaitable.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace IFramework 4 | { 5 | /// 6 | /// 表示一个可等待对象,如果一个方法返回此类型的实例,则此方法可以使用 `await` 异步等待。 7 | /// 8 | /// 用于给 await 确定返回时机的 IAwaiter 的实例。 9 | public interface IAwaitable where TAwaiter : IAwaiter 10 | { 11 | /// 12 | /// 获取一个可用于 await 关键字异步等待的异步等待对象。 13 | /// 此方法会被编译器自动调用。 14 | /// 15 | TAwaiter GetAwaiter(); 16 | } 17 | 18 | /// 19 | /// 表示一个包含返回值的可等待对象,如果一个方法返回此类型的实例,则此方法可以使用 `await` 异步等待返回值。 20 | /// 21 | /// 用于给 await 确定返回时机的 IAwaiter{} 的实例。 22 | /// 异步返回的返回值类型。 23 | public interface IAwaitable where TAwaiter : IAwaiter 24 | { 25 | /// 26 | /// 获取一个可用于 await 关键字异步等待的异步等待对象。 27 | /// 此方法会被编译器自动调用。 28 | /// 29 | TAwaiter GetAwaiter(); 30 | } 31 | 32 | 33 | } 34 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/IAwaiter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.CompilerServices; 3 | 4 | namespace IFramework 5 | { 6 | /// 7 | /// 用于给 await 确定异步返回的时机。 8 | /// 9 | public interface IAwaiter : INotifyCompletion, ICriticalNotifyCompletion 10 | { 11 | /// 12 | /// 获取一个状态,该状态表示正在异步等待的操作已经完成(成功完成或发生了异常);此状态会被编译器自动调用。 13 | /// 在实现中,为了达到各种效果,可以灵活应用其值:可以始终为 true,或者始终为 false。 14 | /// 15 | bool IsCompleted { get; } 16 | 17 | /// 18 | /// 此方法会被编译器在 await 结束时自动调用以获取返回状态(包括异常)。 19 | /// 20 | void GetResult(); 21 | } 22 | /// 23 | /// 用于给 await 确定异步返回的时机,并获取到返回值。 24 | /// 25 | /// 异步返回的返回值类型。 26 | public interface IAwaiter : INotifyCompletion, ICriticalNotifyCompletion 27 | { 28 | /// 29 | /// 获取一个状态,该状态表示正在异步等待的操作已经完成(成功完成或发生了异常);此状态会被编译器自动调用。 30 | /// 在实现中,为了达到各种效果,可以灵活应用其值:可以始终为 true,或者始终为 false。 31 | /// 32 | bool IsCompleted { get; } 33 | 34 | /// 35 | /// 获取此异步等待操作的返回值,此方法会被编译器在 await 结束时自动调用以获取返回值(包括异常)。 36 | /// 37 | /// 异步操作的返回值。 38 | TResult GetResult(); 39 | } 40 | 41 | 42 | } 43 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Log/CSLogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | class CSLogger : ILoger 6 | { 7 | public void Error(object messages, params object[] paras) 8 | { 9 | Console.WriteLine(string.Format("[Err] {0}:\t{1}", DateTime.Now.ToString(), messages), paras); 10 | } 11 | 12 | public void Exception(Exception ex) 13 | { 14 | throw ex; 15 | } 16 | 17 | public void Log(object messages, params object[] paras) 18 | { 19 | Console.WriteLine(string.Format("[Log] {0}:\t{1}", DateTime.Now.ToString(), messages), paras); 20 | } 21 | 22 | public void Warn(object messages, params object[] paras) 23 | { 24 | Console.WriteLine(string.Format("[Warn] {0}:\t{1}", DateTime.Now.ToString(), messages), paras); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Log/ILogRecorder.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework 2 | { 3 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 4 | public interface ILogRecorder: ILoger 5 | { 6 | 7 | } 8 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 9 | } 10 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Log/ILoger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 6 | public interface ILoger 7 | { 8 | void Log(object messages ,params object[] paras); 9 | void Warn(object messages, params object[] paras); 10 | void Error(object messages, params object[] paras); 11 | void Exception(Exception ex); 12 | } 13 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 14 | } 15 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Log/Log.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 6 | public class Log 7 | { 8 | public static bool enable = true; 9 | public static bool enable_L = true; 10 | public static bool enable_W = true; 11 | public static bool enable_E = true; 12 | public static ILoger loger { get; set; } 13 | public static ILogRecorder recorder { get; set; } 14 | static Log() 15 | { 16 | loger = new CSLogger(); 17 | 18 | } 19 | 20 | public static void L(object message, params object[] paras) 21 | { 22 | if (!enable) return; 23 | if (!enable_L) return; 24 | if (loger!=null) 25 | loger.Log(message, paras); 26 | if (recorder!=null) 27 | recorder.Log(message, paras); 28 | } 29 | public static void W(object message, params object[] paras) 30 | { 31 | if (!enable) return; 32 | if (!enable_W) return; 33 | if (loger!=null) 34 | loger.Warn(message, paras); 35 | if (recorder != null) 36 | recorder.Warn(message, paras); 37 | } 38 | public static void E(object message, params object[] paras) 39 | { 40 | if (!enable) return; 41 | if (!enable_E) return; 42 | if (loger != null) 43 | loger.Error( message, paras); 44 | if (recorder != null) 45 | recorder.Log(message, paras); 46 | } 47 | public static void Exception(Exception ex) 48 | { 49 | if (!enable) return; 50 | if (!enable_E) return; 51 | if (loger != null) 52 | loger.Exception(ex); 53 | if (recorder != null) 54 | recorder.Exception(ex); 55 | } 56 | } 57 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 58 | } 59 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Coroutine.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IFramework.Coroutine 5 | { 6 | /// 7 | /// 协程 模拟 8 | /// 9 | internal class Coroutine : YieldInstruction, ICoroutine 10 | { 11 | private Coroutine _innerAction; 12 | private CoroutineState _state; 13 | internal CoroutineModule _module; 14 | internal IEnumerator _routine; 15 | internal event Action onCompelete; 16 | public CoroutineState state { get { return _state; } } 17 | 18 | /// 19 | /// 协程完成时候回调 20 | /// 21 | /// 22 | /// 手动结束协程 23 | /// 24 | public void Compelete() 25 | { 26 | if (_innerAction != null) 27 | { 28 | _innerAction.Compelete(); 29 | } 30 | if (onCompelete != null) 31 | onCompelete(); 32 | 33 | onCompelete = null; 34 | _innerAction = null; 35 | _routine = null; 36 | _state = CoroutineState.Rest; 37 | 38 | _module.Set(this); 39 | } 40 | 41 | public CoroutineAwaiter GetAwaiter() 42 | { 43 | return new CoroutineAwaiter(this); 44 | } 45 | 46 | protected override bool IsCompelete() 47 | { 48 | if (state != CoroutineState.Working) return false; 49 | if (_innerAction == null) 50 | { 51 | if (!_routine.MoveNext()) 52 | { 53 | return true; 54 | } 55 | if (_routine.Current != null) 56 | { 57 | IEnumerator ie = null; 58 | if (_routine.Current is YieldInstruction) 59 | ie = (_routine.Current as YieldInstruction).AsEnumerator(); 60 | else if (_routine.Current is IEnumerator) 61 | ie = _routine.Current as IEnumerator; 62 | if (ie != null) 63 | { 64 | _innerAction = _module.CreateCoroutine(ie) as Coroutine; 65 | _innerAction.Resume(); 66 | } 67 | } 68 | } 69 | if (_innerAction != null) 70 | { 71 | if (_innerAction.isDone) 72 | { 73 | _innerAction.Compelete(); 74 | _innerAction = null; 75 | } 76 | } 77 | 78 | return false; 79 | } 80 | 81 | 82 | public void Pause() 83 | { 84 | _state = CoroutineState.Yied; 85 | } 86 | public void Resume() 87 | { 88 | _state = CoroutineState.Working; 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/CoroutineAwaiter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Coroutine 5 | { 6 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 7 | public struct CoroutineAwaiter : IAwaiter 8 | { 9 | private Coroutine cor; 10 | private Queue calls; 11 | 12 | public CoroutineAwaiter(ICoroutine cor) 13 | { 14 | this.cor = cor as Coroutine; 15 | calls = new Queue(); 16 | this.cor.onCompelete += Task_completed; 17 | } 18 | 19 | private void Task_completed() 20 | { 21 | while (calls.Count != 0) 22 | { 23 | calls.Dequeue()?.Invoke(); 24 | } 25 | } 26 | 27 | public bool IsCompleted { get { return cor.isDone; } } 28 | 29 | public void GetResult() 30 | { 31 | if (!IsCompleted) 32 | throw new Exception("The task is not finished yet"); 33 | } 34 | 35 | public void OnCompleted(Action continuation) 36 | { 37 | UnsafeOnCompleted(continuation); 38 | } 39 | 40 | public void UnsafeOnCompleted(Action continuation) 41 | { 42 | if (continuation == null) 43 | throw new ArgumentNullException("continuation"); 44 | calls.Enqueue(continuation); 45 | } 46 | } 47 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/CoroutineState.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Coroutine 2 | { 3 | /// 4 | /// 携程的状态 5 | /// 6 | public enum CoroutineState 7 | { 8 | /// 9 | /// 干活中 10 | /// 11 | Working, 12 | /// 13 | /// 挂起中,刚创建/主动挂起了 14 | /// 15 | Yied, 16 | /// 17 | /// 干完了,第一帧,处于等待被回收 18 | /// 休息中 19 | /// 20 | Rest, 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/ICoroutine.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Coroutine 2 | { 3 | /// 4 | /// 协程实体 5 | /// 6 | public interface ICoroutine : IAwaitable 7 | { 8 | /// 9 | /// 目前状态 10 | /// 11 | CoroutineState state { get; } 12 | /// 13 | /// 手动结束 14 | /// 15 | void Compelete(); 16 | /// 17 | /// 挂起 18 | /// 19 | void Pause(); 20 | /// 21 | /// 恢复运行 22 | /// 23 | void Resume(); 24 | } 25 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForDays.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 等待日子 7 | /// 8 | public class WaitForDays : WaitForTimeSpan 9 | { 10 | /// 11 | /// Ctor 12 | /// 13 | /// 天数 14 | public WaitForDays(double days) : base(TimeSpan.FromDays(days)) 15 | { 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForFrame.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Coroutine 2 | { 3 | /// 4 | /// 等一帧 5 | /// 6 | public class WaitForFrame : WaitForFrames 7 | { 8 | /// 9 | /// Ctor 10 | /// 11 | public WaitForFrame() : base(1) 12 | { 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForFrames.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 等待帧数 7 | /// 8 | public class WaitForFrames : YieldInstruction 9 | { 10 | /// 11 | /// Ctor 12 | /// 13 | /// 帧数 14 | public WaitForFrames(int count) 15 | { 16 | _curCount = 0; 17 | this._count = count; 18 | } 19 | private int _curCount; 20 | private int _count { get; } 21 | 22 | /// 23 | /// over 24 | /// 25 | /// 26 | protected override bool IsCompelete() 27 | { 28 | _curCount++; 29 | return _curCount >= _count; 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForHours.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 等待小时 7 | /// 8 | public class WaitForHours : WaitForTimeSpan 9 | { 10 | /// 11 | /// Ctor 12 | /// 13 | /// 小时 14 | public WaitForHours(double hours) : base(TimeSpan.FromHours(hours)) 15 | { 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForMilliseconds.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 等待毫秒 7 | /// 8 | public class WaitForMilliseconds : WaitForTimeSpan 9 | { 10 | /// 11 | /// Ctor 12 | /// 13 | /// 毫秒 14 | public WaitForMilliseconds(double milliseconds) : base(TimeSpan.FromMilliseconds(milliseconds)) 15 | { 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForMinutes.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 等待分钟 7 | /// 8 | public class WaitForMinutes : WaitForTimeSpan 9 | { 10 | /// 11 | /// Ctor 12 | /// 13 | /// 等待分钟数 14 | public WaitForMinutes(double minutes) : base(TimeSpan.FromMinutes(minutes)) 15 | { 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForSeconds.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 等待秒 7 | /// 8 | public class WaitForSeconds : WaitForTimeSpan 9 | { 10 | /// 11 | /// Ctor 12 | /// 13 | /// 等待秒数 14 | public WaitForSeconds(double seconds) : base(TimeSpan.FromSeconds(seconds)) 15 | { 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForTicks.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 等待ticks(100微秒) 7 | /// 8 | public class WaitForTicks : WaitForTimeSpan 9 | { 10 | /// 11 | /// Ctor 12 | /// 13 | /// 等待的tick数 14 | public WaitForTicks(long ticks) : base(TimeSpan.FromTicks(ticks)) 15 | { 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitForTimeSpan.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IFramework.Coroutine 5 | { 6 | /// 7 | /// 等待时间 8 | /// 9 | public class WaitForTimeSpan : YieldInstruction 10 | { 11 | private DateTime _setTime; 12 | /// 13 | /// Ctor 14 | /// 15 | /// 等待时间 16 | public WaitForTimeSpan(TimeSpan span) : base() 17 | { 18 | _setTime = DateTime.Now + span; 19 | } 20 | /// 21 | /// override 22 | /// 23 | /// 24 | protected override bool IsCompelete() 25 | { 26 | return DateTime.Now >= _setTime; 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitUtil.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IFramework.Coroutine 5 | { 6 | /// 7 | /// 等待条件成立 8 | /// 9 | public class WaitUtil : YieldInstruction 10 | { 11 | /// 12 | /// Ctor 13 | /// 14 | /// 等待成立条件 15 | public WaitUtil(Func condition) 16 | { 17 | _condition = condition; 18 | } 19 | 20 | private Func _condition { get; } 21 | 22 | /// 23 | /// override 24 | /// 25 | /// 26 | protected override bool IsCompelete() 27 | { 28 | return _condition.Invoke(); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/WaitWhile.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace IFramework.Coroutine 5 | { 6 | /// 7 | /// 等待条件不成立 8 | /// 9 | public class WaitWhile : YieldInstruction 10 | { 11 | /// 12 | /// Ctor 13 | /// 14 | /// 等待不成立条件 15 | public WaitWhile(Func condition) 16 | { 17 | _condition = condition; 18 | } 19 | 20 | private Func _condition { get; } 21 | /// 22 | /// override 23 | /// 24 | /// 25 | protected override bool IsCompelete() 26 | { 27 | return !_condition.Invoke(); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Instruction/YieldInstruction.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 所有等待类的基类 7 | /// 8 | public abstract class YieldInstruction 9 | { 10 | 11 | /// 12 | /// 是否结束 13 | /// 14 | public virtual bool isDone 15 | { 16 | get 17 | { 18 | return IsCompelete(); 19 | } 20 | } 21 | 22 | /// 23 | /// 是否结束 24 | /// 25 | /// 26 | protected abstract bool IsCompelete(); 27 | 28 | internal IEnumerator AsEnumerator() 29 | { 30 | while (!isDone) 31 | { 32 | yield return false; 33 | } 34 | yield return true; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Module/CoroutineModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace IFramework.Coroutine 6 | { 7 | /// 8 | /// 协程模块 9 | /// 10 | public class CoroutineModule : UpdateModule, ICoroutineModule 11 | { 12 | private List _cors; 13 | private Queue _recyle; 14 | 15 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 16 | /// 17 | /// 优先级 18 | /// 19 | protected override ModulePriority OnGetDefautPriority() 20 | { 21 | return ModulePriority.Coroutine; 22 | } 23 | protected override void OnUpdate() 24 | { 25 | { 26 | var count = _recyle.Count; 27 | for (int i = 0; i < count; i++) 28 | { 29 | var _cor = _recyle.Dequeue(); 30 | _cors.Remove(_cor); 31 | } 32 | } 33 | { 34 | var count = _cors.Count; 35 | for (int i = 0; i < count; i++) 36 | { 37 | var _cor = _cors[i]; 38 | if (_cor.state == CoroutineState.Working) 39 | { 40 | if ((_cor as Coroutine).isDone) 41 | { 42 | _cor.Compelete(); 43 | } 44 | } 45 | } 46 | } 47 | 48 | 49 | } 50 | protected override void OnDispose() 51 | { 52 | _recyle.Clear(); 53 | _cors.Clear(); 54 | } 55 | 56 | protected override void Awake() 57 | { 58 | _cors = new List(); 59 | _recyle = new Queue(); 60 | } 61 | internal void Set(Coroutine routine) 62 | { 63 | _recyle.Enqueue(routine); 64 | } 65 | 66 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 67 | 68 | /// 69 | /// 创建一个携程不跑 70 | /// 71 | /// 72 | /// 73 | public ICoroutine CreateCoroutine(IEnumerator routine) 74 | { 75 | var coroutine = new Coroutine(); 76 | coroutine._routine = routine; 77 | coroutine._module = this; 78 | PauseCoroutine(coroutine); 79 | return coroutine; 80 | } 81 | /// 82 | /// 开启一个协程 83 | /// 84 | /// 85 | /// 86 | public ICoroutine StartCoroutine(IEnumerator routine) 87 | { 88 | var coroutine = CreateCoroutine(routine); 89 | _cors.Add(coroutine); 90 | ResumeCoroutine(coroutine); 91 | return coroutine; 92 | } 93 | 94 | /// 95 | /// 挂起携程 96 | /// 97 | /// 98 | public void PauseCoroutine(ICoroutine coroutine) 99 | { 100 | coroutine.Pause(); 101 | } 102 | /// 103 | /// 恢复运行 104 | /// 105 | /// 106 | public void ResumeCoroutine(ICoroutine coroutine) 107 | { 108 | coroutine.Resume(); 109 | } 110 | 111 | /// 112 | /// 关闭一个携程 113 | /// 114 | /// 115 | public void StopCoroutine(ICoroutine coroutine) 116 | { 117 | coroutine.Compelete(); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Coroutine/Module/ICoroutineModule.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | 3 | namespace IFramework.Coroutine 4 | { 5 | /// 6 | /// 协程模块 7 | /// 8 | public interface ICoroutineModule 9 | { 10 | /// 11 | /// 创建一个携程不跑 12 | /// 13 | /// 14 | /// 15 | ICoroutine CreateCoroutine(IEnumerator routine); 16 | /// 17 | /// 开启一个协程 18 | /// CreateCoroutine + ResumeCoroutine 19 | /// 20 | /// 21 | /// 22 | ICoroutine StartCoroutine(IEnumerator routine); 23 | /// 24 | /// 挂起携程 25 | /// 26 | /// 27 | void PauseCoroutine(ICoroutine coroutine); 28 | /// 29 | /// 恢复运行 30 | /// 31 | /// 32 | void ResumeCoroutine(ICoroutine coroutine); 33 | 34 | /// 35 | /// 关闭一个携程 36 | /// 37 | /// 38 | void StopCoroutine(ICoroutine coroutine); 39 | } 40 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/IModules.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | /// 6 | /// 模块组 7 | /// 8 | public interface IModules 9 | { 10 | /// 11 | /// 创建模块 12 | /// 13 | /// 14 | /// 15 | /// 16 | /// 17 | Module CreateModule(Type type, string name = Module.defaultName, int priority = 0); 18 | /// 19 | /// 创建模块 20 | /// 21 | /// 22 | /// 23 | /// 24 | /// 25 | T CreateModule(string name = Module.defaultName, int priority = 0) where T : Module; 26 | /// 27 | /// 获取模块 28 | /// 29 | /// 30 | /// 31 | /// 32 | /// 33 | Module GetModule(Type type, string name = Module.defaultName, int priority = 0); 34 | /// 35 | /// 获取模块 36 | /// 37 | /// 38 | /// 39 | /// 40 | /// 41 | T GetModule(string name = Module.defaultName, int priority = 0) where T : Module; 42 | /// 43 | /// 查找模块 44 | /// 45 | /// 46 | /// 47 | /// 48 | Module FindModule(Type type, string name = Module.defaultName); 49 | /// 50 | /// 查找模块 51 | /// 52 | /// 53 | /// 54 | /// 55 | T FindModule(string name = Module.defaultName) where T : Module; 56 | } 57 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Inject/IInjectModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Inject 5 | { 6 | /// 7 | /// 8 | /// 9 | public interface IInjectModule 10 | { 11 | /// 12 | /// 清除 13 | /// 14 | void Clear(); 15 | 16 | /// 17 | /// 反射注入 18 | /// 19 | /// 20 | void Inject(object obj); 21 | 22 | /// 23 | /// 注入所有 24 | /// 25 | void InjectInstances(); 26 | 27 | /// 28 | /// 注册 29 | /// 30 | /// 31 | /// 32 | void Subscribe(string name = null); 33 | /// 34 | /// 注册 35 | /// 36 | /// 37 | /// 38 | /// 39 | void Subscribe(string name = null) where Type : BaseType; 40 | /// 41 | /// 注册 42 | /// 43 | /// 44 | /// 45 | /// 46 | void Subscribe(Type source, Type target, string name = null); 47 | /// 48 | /// 注册实例 49 | /// 50 | /// 51 | /// 52 | /// 53 | /// 54 | void SubscribeInstance(Type instance, string name = null, bool inject = true) where Type : class; 55 | 56 | /// 57 | /// 注册实例 58 | /// 59 | /// 60 | /// 61 | /// 62 | /// 63 | /// 64 | void SubscribeInstance(Type baseType, object instance, string name = null, bool inject = true); 65 | /// 66 | /// 获取 67 | /// 68 | /// 69 | /// 70 | /// 71 | /// 72 | T GetValue(string name = null, params object[] args) where T : class; 73 | /// 74 | /// 获取 75 | /// 76 | /// 77 | /// 78 | /// 79 | /// 80 | object GetValue(Type baseType, string name = null, params object[] constructorArgs); 81 | /// 82 | /// 获取 83 | /// 84 | /// 85 | /// 86 | IEnumerable GetValues(Type type); 87 | /// 88 | /// 获取 89 | /// 90 | /// 91 | /// 92 | IEnumerable GetValues(); 93 | } 94 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Inject/InjectAttribute.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Inject 4 | { 5 | /// 6 | /// 注入标记 7 | /// 8 | [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,Inherited =false,AllowMultiple =true)] 9 | public class InjectAttribute : Attribute 10 | { 11 | /// 12 | /// 注入名 13 | /// 14 | public string name { get;} 15 | 16 | /// 17 | /// ctor 18 | /// 19 | /// 20 | public InjectAttribute(string name="") 21 | { 22 | this.name = name; 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Inject/InjectModule.InjectInstanceMap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Inject 5 | { 6 | public partial class InjectModule 7 | { 8 | private class InjectInstanceMap : InjectMap 9 | { 10 | public IEnumerable GetInstances(Type type) 11 | { 12 | List list; 13 | if (map.TryGetValue(type, out list)) 14 | { 15 | for (int i = list.Count - 1; i >= 0; i--) 16 | { 17 | int index = list[i]; 18 | var value = containers[index]; 19 | yield return value.value; 20 | } 21 | } 22 | } 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Inject/InjectModule.InjectMap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Inject 5 | { 6 | public partial class InjectModule 7 | { 8 | private class InjectMap 9 | { 10 | public class Value 11 | { 12 | public string key; 13 | public T value { get; set; } 14 | } 15 | 16 | protected List containers; 17 | protected Dictionary> map; 18 | public InjectMap() 19 | { 20 | containers = new List(); 21 | map = new Dictionary>(); 22 | } 23 | public void Set(Type super, string key, T t) 24 | { 25 | List list; 26 | if (!map.TryGetValue(super, out list)) 27 | { 28 | list = new List(); 29 | map.Add(super, list); 30 | } 31 | 32 | for (int i = list.Count - 1; i >= 0; i--) 33 | { 34 | int index = list[i]; 35 | var value = containers[index]; 36 | if (value.key == key) 37 | { 38 | value.value = t; 39 | return; 40 | } 41 | } 42 | list.Add(containers.Count); 43 | containers.Add(new Value() { key = key, value = t }); 44 | } 45 | public T Get(Type super, string key) 46 | { 47 | List list; 48 | if (map.TryGetValue(super, out list)) 49 | { 50 | for (int i = list.Count - 1; i >= 0; i--) 51 | { 52 | int index = list[i]; 53 | var value = containers[index]; 54 | if (value.key == key) 55 | { 56 | return value.value; 57 | } 58 | } 59 | } 60 | return default(T); 61 | } 62 | 63 | protected T GetByindex(int index) 64 | { 65 | return containers[index].value; 66 | } 67 | public void Clear() 68 | { 69 | map.Clear(); 70 | containers.Clear(); 71 | } 72 | 73 | public IEnumerable Values 74 | { 75 | get 76 | { 77 | for (int i = 0; i < containers.Count; i++) 78 | { 79 | yield return GetByindex(i); 80 | } 81 | } 82 | } 83 | } 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Inject/InjectModule.InjectTypeMap.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Inject 5 | { 6 | public partial class InjectModule 7 | { 8 | private class InjectTypeMap : InjectMap 9 | { 10 | public IEnumerable GetTypes(Type type) 11 | { 12 | foreach (var _type in map.Keys) 13 | { 14 | if (type.IsAssignableFrom(_type)) 15 | { 16 | foreach (var it in map[_type]) 17 | { 18 | yield return GetByindex(it); 19 | } 20 | } 21 | 22 | } 23 | 24 | } 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Loom/LoomModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using static System.Collections.Specialized.BitVector32; 4 | 5 | namespace IFramework 6 | { 7 | /// 8 | /// 线程反馈模块 9 | /// 10 | partial class LoomModule : UpdateModule 11 | { 12 | private interface IQueue 13 | { 14 | void RunFirst(); 15 | 16 | } 17 | private class MyQueue : IQueue 18 | { 19 | public LoomModule loom; 20 | private Queue queue = new Queue(); 21 | public event Action action; 22 | public void Enqueue(T t) 23 | { 24 | queue.Enqueue(t); 25 | } 26 | public void RunFirst() 27 | { 28 | T t = queue.Dequeue(); 29 | action?.Invoke(t); 30 | } 31 | } 32 | private Dictionary queues = new Dictionary(); 33 | 34 | private Queue _delay = new Queue(); 35 | 36 | 37 | 38 | private MyQueue GetQueue() 39 | { 40 | var type = typeof(T); 41 | if (!queues.ContainsKey(type)) 42 | { 43 | var queue = new MyQueue(); 44 | queue.loom = this; 45 | queues.Add(type, queue); 46 | } 47 | return queues[type] as MyQueue; 48 | } 49 | 50 | 51 | public void RunDelay(T t) 52 | { 53 | lock (_delay) 54 | { 55 | MyQueue queue = GetQueue(); 56 | queue.Enqueue(t); 57 | _delay.Enqueue(queue); 58 | } 59 | } 60 | public void AddDelayHandler(Action action) 61 | { 62 | MyQueue queue = GetQueue(); 63 | queue.action += action; 64 | } 65 | public void RemoveDelayHandler(Action action) 66 | { 67 | MyQueue queue = GetQueue(); 68 | queue.action -= action; 69 | } 70 | protected override ModulePriority OnGetDefautPriority() 71 | { 72 | return ModulePriority.Loom; 73 | } 74 | 75 | protected override void OnUpdate() 76 | { 77 | lock (_delay) 78 | { 79 | int count = _delay.Count; 80 | if (count <= 0) return; 81 | for (int i = 0; i < count; i++) 82 | { 83 | _delay.Dequeue().RunFirst(); 84 | } 85 | } 86 | 87 | } 88 | protected override void OnDispose() 89 | { 90 | _delay.Clear(); 91 | _delay = null; 92 | } 93 | protected override void Awake() 94 | { 95 | 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/IEventArgs.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 框架内传递的所有消息的基类 5 | /// 6 | public interface IEventArgs { } 7 | } 8 | 9 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/IMessage.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Message 4 | { 5 | /// 6 | /// 消息 7 | /// 8 | public interface IMessage:IAwaitable 9 | { 10 | /// 11 | /// 发送消息类型 12 | /// 13 | Type subject { get; } 14 | 15 | /// 16 | /// 发送消息类型 17 | /// 18 | string stringSubject { get; } 19 | /// 20 | /// 承载消息内容 21 | /// 22 | IEventArgs args { get; } 23 | /// 24 | /// code,帮助区分 args 25 | /// 26 | int code { get; } 27 | 28 | /// 29 | /// 消息状态 30 | /// 31 | MessageState state { get; } 32 | 33 | /// 34 | /// 消息发送结果 35 | /// 36 | MessageErrorCode errorCode { get; } 37 | /// 38 | /// 消息主题类型 39 | /// 40 | SubjectType type { get; } 41 | 42 | #region 仅在 state 为 MessageState.Wait时有效 43 | 44 | /// 45 | /// 设置Code, 46 | /// 仅在 state 为 MessageState.Wait时有效 47 | /// 48 | /// 49 | IMessage SetCode(int code); 50 | 51 | 52 | /// 53 | /// 仅在 state 为 MessageState.Wait时有效 54 | /// 消息发布完成时的引用 55 | /// 56 | /// 57 | IMessage OnCompelete(Action action); 58 | #endregion 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/IMessageListener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Message 4 | { 5 | /// 6 | /// 消息监听者 7 | /// 8 | public interface IMessageListener 9 | { 10 | /// 11 | /// 收到消息回调 12 | /// 13 | /// 14 | void Listen(IMessage message); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/Message.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace IFramework.Message 3 | { 4 | class Message : IMessage 5 | { 6 | 7 | private int _code = 0; 8 | private Type _subject; 9 | private string _stringSubject; 10 | private IEventArgs _args; 11 | private MessageState _state; 12 | private MessageErrorCode _errCode; 13 | private SubjectType _type; 14 | private event Action onRecycle; 15 | public int code { get { return _code; } } 16 | public Type subject { get { return _subject; } } 17 | public string stringSubject { get { return _stringSubject; } } 18 | 19 | public IEventArgs args { get { return _args; } } 20 | public MessageState state { get { return _state; } } 21 | public MessageErrorCode errorCode { get { return _errCode; } } 22 | 23 | public SubjectType type => _type; 24 | 25 | private void Reset() 26 | { 27 | this._errCode = MessageErrorCode.None; 28 | _code = int.MinValue; 29 | _args = null; 30 | _stringSubject = string.Empty; 31 | _type = SubjectType.None; 32 | _subject = null; 33 | onRecycle = null; 34 | } 35 | internal void Begin() 36 | { 37 | Reset(); 38 | _state = MessageState.Wait; 39 | } 40 | internal Message SetType(Type type) 41 | { 42 | this._subject = type; 43 | _type = SubjectType.Type; 44 | return this; 45 | } 46 | internal Message SetType(string type) 47 | { 48 | this._stringSubject = type; 49 | _type = SubjectType.String; 50 | 51 | return this; 52 | } 53 | internal Message SetArgs(IEventArgs args) 54 | { 55 | this._args = args; 56 | return this; 57 | } 58 | public IMessage SetCode(int code) 59 | { 60 | if (_state != MessageState.Wait) 61 | { 62 | Log.E(string.Format("you can not set the code now, the state is {0}", _state)); 63 | return this; 64 | } 65 | this._code = code; 66 | return this; 67 | } 68 | 69 | public IMessage OnCompelete(Action action) 70 | { 71 | if (_state != MessageState.Wait) 72 | { 73 | Log.E(string.Format("you can not bind the action now, the state is {0}", _state)); 74 | return this; 75 | } 76 | onRecycle += action; 77 | return this; 78 | } 79 | internal void Lock() 80 | { 81 | if (_state == MessageState.Wait) 82 | { 83 | _state = MessageState.Lock; 84 | } 85 | else 86 | { 87 | Log.E("unknown Exception occured with this message"); 88 | } 89 | } 90 | internal void SetErrorCode(MessageErrorCode code) 91 | { 92 | if (_state == MessageState.Lock) 93 | { 94 | this._errCode = code; 95 | } 96 | else 97 | { 98 | Log.E("unknown Exception occured with this message"); 99 | } 100 | } 101 | internal void End() 102 | { 103 | if (_state != MessageState.Lock) 104 | { 105 | Log.E("unknown Exception occured with this message"); 106 | } 107 | _state = MessageState.Rest; 108 | if (onRecycle != null) 109 | { 110 | onRecycle.Invoke(this); 111 | } 112 | Reset(); 113 | } 114 | 115 | public MessageAwaiter GetAwaiter() 116 | { 117 | return new MessageAwaiter(this); 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/MessageAwaiter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Message 5 | { 6 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 7 | public struct MessageAwaiter : IAwaiter 8 | { 9 | private IMessage cor; 10 | private Queue calls; 11 | 12 | public MessageAwaiter(IMessage cor) 13 | { 14 | this.cor = cor; 15 | calls = new Queue(); 16 | this.cor.OnCompelete(Task_completed); 17 | } 18 | 19 | private void Task_completed(IMessage obj) 20 | { 21 | while (calls.Count != 0) 22 | { 23 | calls.Dequeue()?.Invoke(); 24 | } 25 | } 26 | 27 | public bool IsCompleted { get { return cor.state == MessageState.Rest; } } 28 | 29 | public void GetResult() 30 | { 31 | if (!IsCompleted) 32 | throw new Exception("The task is not finished yet"); 33 | } 34 | 35 | public void OnCompleted(Action continuation) 36 | { 37 | UnsafeOnCompleted(continuation); 38 | } 39 | 40 | public void UnsafeOnCompleted(Action continuation) 41 | { 42 | if (continuation == null) 43 | throw new ArgumentNullException("continuation"); 44 | calls.Enqueue(continuation); 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/MessageErrorCode.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 消息发送结果 5 | /// 6 | public enum MessageErrorCode 7 | { 8 | /// 9 | /// 无状态,即不在使用 10 | /// 11 | None, 12 | /// 13 | /// 成功 14 | /// 15 | Success, 16 | /// 17 | /// 无人监听 18 | /// 19 | NoneListen, 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/MessageListener.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Message 4 | { 5 | /// 6 | /// 消息监听 7 | /// 8 | /// 9 | public delegate void MessageListener(IMessage message); 10 | } 11 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/MessageState.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 消息状态 5 | /// 6 | public enum MessageState 7 | { 8 | /// 9 | /// 休息 10 | /// 11 | Rest, 12 | /// 13 | /// 等待被发布 14 | /// 15 | Wait, 16 | /// 17 | /// 正在发布中 18 | /// 19 | Lock, 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/MessageUrgency.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 消息紧急程度 5 | /// 6 | public static class MessageUrgency 7 | { 8 | /// 9 | /// 立刻 10 | /// 11 | public const int Immediately = -1; 12 | /// 13 | /// 非常紧急 14 | /// 15 | public const int VeryUrgent =32; 16 | /// 17 | /// 紧急 18 | /// 19 | public const int Urgent = 128; 20 | /// 21 | /// 重要的 22 | /// 23 | public const int Important = 256; 24 | /// 25 | /// 普通的 26 | /// 27 | public const int Common = 512; 28 | /// 29 | /// 不重要的 30 | /// 31 | public const int Unimportant = 1024; 32 | /// 33 | /// 可有可无的 34 | /// 35 | public const int Dispensable = 4096; 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Message/MessageUrgencyType.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 消息紧急程度 5 | /// 6 | public enum MessageUrgencyType 7 | { 8 | /// 9 | /// 立刻 10 | /// 11 | Immediately= MessageUrgency.Immediately, 12 | /// 13 | /// 非常紧急 14 | /// 15 | VeryUrgent = MessageUrgency.VeryUrgent, 16 | /// 17 | /// 紧急 18 | /// 19 | Urgent = MessageUrgency.Urgent, 20 | /// 21 | /// 重要的 22 | /// 23 | Important = MessageUrgency.Important, 24 | /// 25 | /// 普通的 26 | /// 27 | Common = MessageUrgency.Common, 28 | /// 29 | /// 不重要的 30 | /// 31 | Unimportant = MessageUrgency.Unimportant, 32 | /// 33 | /// 可有可无的 34 | /// 35 | Dispensable= MessageUrgency.Dispensable, 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/HandlerQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | namespace IFramework.Message 4 | { 5 | abstract class HandlerQueue : IDisposable 6 | { 7 | private Queue> _subscribeQueue = new Queue>(); 8 | protected List> subjects = new List>(); 9 | protected Dictionary subjectMap = new Dictionary(); 10 | private object _lock = new object(); 11 | 12 | 13 | private void CreateSubscribeAction(T type, MessageListener listener, bool subscribe) 14 | { 15 | lock (_lock) 16 | { 17 | _subscribeQueue.Enqueue(new SubscribeAction(type, listener, subscribe)); 18 | } 19 | } 20 | public void Subscribe(T type, MessageListener listener) 21 | { 22 | CreateSubscribeAction(type, listener, true); 23 | } 24 | public void UnSubscribe(T type, MessageListener listener) 25 | { 26 | CreateSubscribeAction(type, listener, false); 27 | } 28 | public abstract bool Publish(IMessage message); 29 | public void Update() 30 | { 31 | int count = 0; 32 | 33 | lock (_lock) 34 | { 35 | int index = 0; 36 | T type = default; 37 | count = _subscribeQueue.Count; 38 | for (int i = 0; i < count; i++) 39 | { 40 | var value = _subscribeQueue.Dequeue(); 41 | type = value.type; 42 | if (!value.subscribe) 43 | { 44 | if (subjectMap.TryGetValue(type, out index)) 45 | { 46 | subjects[subjectMap[type]].UnSubscribe(value.value); 47 | } 48 | } 49 | else 50 | { 51 | Subject en; 52 | if (!subjectMap.TryGetValue(type, out index)) 53 | { 54 | index = subjects.Count; 55 | en = new Subject(type); 56 | subjectMap.Add(type, index); 57 | subjects.Add(en); 58 | } 59 | else 60 | { 61 | en = subjects[subjectMap[type]]; 62 | } 63 | en.Subscribe(value.value); 64 | } 65 | } 66 | } 67 | 68 | } 69 | 70 | public void Dispose() 71 | { 72 | for (int i = 0; i < subjects.Count; i++) subjects[i].Dispose(); 73 | subjects.Clear(); 74 | subjectMap.Clear(); 75 | _subscribeQueue.Clear(); 76 | _subscribeQueue = null; 77 | subjects = null; 78 | subjectMap = null; 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/MessageQueue.cs: -------------------------------------------------------------------------------- 1 | using IFramework.Queue; 2 | using System; 3 | using System.Collections.Generic; 4 | namespace IFramework.Message 5 | { 6 | 7 | 8 | abstract class MessageQueue : IDisposable 9 | { 10 | public int count 11 | { 12 | get 13 | { 14 | lock (_lock) 15 | { 16 | return _priorityQueue.count; 17 | } 18 | } 19 | } 20 | 21 | protected readonly HandlerQueue handler; 22 | private int _processesPerFrame = -1; 23 | private object _lock = new object(); 24 | 25 | 26 | private StablePriorityQueue _priorityQueue; 27 | private List _updatelist; 28 | private Dictionary excuteMap; 29 | public int processesPerFrame { get { return _processesPerFrame; } set { _processesPerFrame = value; } } 30 | 31 | public MessageQueue(HandlerQueue handler) 32 | { 33 | excuteMap = new Dictionary(); 34 | _priorityQueue = new StablePriorityQueue(); 35 | _updatelist = new List(); 36 | this.handler = handler; 37 | } 38 | protected abstract void SetMessage(Message message, T type); 39 | public IMessage PublishByNumber(T type, IEventArgs args, int code, int priority = MessageUrgency.Common) 40 | { 41 | var message = new Message(); 42 | message.Begin(); 43 | message.SetCode(code); 44 | message.SetArgs(args); 45 | SetMessage(message, type); 46 | if (priority < 0) 47 | { 48 | HandleMessage(message); 49 | } 50 | else 51 | { 52 | lock (_lock) 53 | { 54 | if (_priorityQueue.count == _priorityQueue.capcity) 55 | { 56 | _priorityQueue.Resize(_priorityQueue.capcity * 2); 57 | } 58 | StablePriorityQueueNode node = new StablePriorityQueueNode(); 59 | _priorityQueue.Enqueue(node, priority); 60 | excuteMap.Add(node, message); 61 | _updatelist.Add(node); 62 | } 63 | } 64 | return message; 65 | } 66 | 67 | private void HandleMessage(Message message) 68 | { 69 | message.Lock(); 70 | bool sucess = false; 71 | sucess |= handler.Publish(message); 72 | message.SetErrorCode(sucess ? MessageErrorCode.Success : MessageErrorCode.NoneListen); 73 | message.End(); 74 | } 75 | Queue _tmp = new Queue(); 76 | public void Update() 77 | { 78 | int count = 0; 79 | lock (_lock) 80 | { 81 | count = processesPerFrame == -1 ? _priorityQueue.count : Math.Min(processesPerFrame, _priorityQueue.count); 82 | if (count == 0) return; 83 | for (int i = 0; i < count; i++) 84 | { 85 | StablePriorityQueueNode node = _priorityQueue.Dequeue(); 86 | Message message; 87 | if (excuteMap.TryGetValue(node, out message)) 88 | { 89 | _tmp.Enqueue(message); 90 | excuteMap.Remove(node); 91 | } 92 | _updatelist.Remove(node); 93 | } 94 | if (_updatelist.Count > 0) 95 | { 96 | for (int i = _updatelist.Count - 1; i >= 0; i--) 97 | { 98 | _priorityQueue.UpdatePriority(_updatelist[i], _updatelist[i].priority - 1); 99 | } 100 | } 101 | } 102 | for (int i = 0; i < count; i++) 103 | { 104 | var message = _tmp.Dequeue(); 105 | HandleMessage(message); 106 | } 107 | } 108 | 109 | 110 | public void Dispose() 111 | { 112 | 113 | _updatelist.Clear(); 114 | excuteMap.Clear(); 115 | _priorityQueue = null; 116 | _updatelist = null; 117 | excuteMap = null; 118 | } 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/String/IStringMessageModule.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 消息模块(String版本) 5 | /// 6 | public interface IStringMessageModule 7 | { 8 | /// 9 | /// 剩余消息数目 10 | /// 11 | int count { get; } 12 | /// 13 | /// 每一帧处理消息上限 14 | /// 15 | int processesPerFrame { get; set; } 16 | 17 | /// 18 | /// 发布消息 19 | /// 20 | /// 21 | /// 22 | /// 23 | /// 24 | /// 25 | IMessage Publish(string type, IEventArgs args, int code = 0, MessageUrgencyType priority = MessageUrgencyType.Common); 26 | /// 27 | /// 发布消息 28 | /// 29 | /// 30 | /// 31 | /// 32 | /// 33 | /// 34 | IMessage PublishByNumber(string type, IEventArgs args, int code = 0, int priority = MessageUrgency.Common); 35 | /// 36 | /// 注册 37 | /// 38 | /// 39 | /// 40 | void Subscribe(string type, IMessageListener listener); 41 | /// 42 | /// 注册 43 | /// 44 | /// 45 | /// 46 | void Subscribe(string type, MessageListener listener); 47 | /// 48 | /// 移除 49 | /// 50 | /// 51 | /// 52 | void UnSubscribe(string type, IMessageListener listener); 53 | /// 54 | /// 移除 55 | /// 56 | /// 57 | /// 58 | void UnSubscribe(string type, MessageListener listener); 59 | } 60 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/String/StringHandlerQueue.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | class StringHandlerQueue : HandlerQueue 4 | { 5 | public override bool Publish(IMessage message) 6 | { 7 | bool success = false; 8 | var type = message.stringSubject; 9 | if (subjectMap.ContainsKey(type)) 10 | { 11 | success |= subjects[subjectMap[type]].Publish(message); 12 | } 13 | return success; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/String/StringMessageModule.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 消息模块(String版本) 5 | /// 6 | public class StringMessageModule : UpdateModule, IStringMessageModule 7 | { 8 | private StringMessageQueue messages; 9 | private StringHandlerQueue handlers; 10 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 11 | protected override ModulePriority OnGetDefautPriority() 12 | { 13 | return ModulePriority.Message; 14 | } 15 | protected override void OnDispose() 16 | { 17 | handlers.Dispose(); 18 | messages.Dispose(); 19 | } 20 | protected override void Awake() 21 | { 22 | handlers = new StringHandlerQueue(); 23 | messages = new StringMessageQueue(handlers); 24 | } 25 | 26 | 27 | protected override void OnUpdate() 28 | { 29 | handlers.Update(); 30 | messages.Update(); 31 | } 32 | 33 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 34 | /// 35 | /// 剩余消息数目 36 | /// 37 | public int count 38 | { 39 | get 40 | { 41 | return messages.count; 42 | } 43 | } 44 | 45 | /// 46 | /// 每帧处理消息个数 47 | /// 48 | public int processesPerFrame { get { return messages.processesPerFrame; } set { messages.processesPerFrame = value; } } 49 | 50 | 51 | 52 | 53 | /// 54 | /// 注册监听 55 | /// 56 | /// 57 | /// 58 | /// 59 | public void Subscribe(string type, IMessageListener listener) 60 | { 61 | Subscribe(type, listener.Listen); 62 | } 63 | 64 | /// 65 | /// 解除注册监听 66 | /// 67 | /// 68 | /// 69 | /// 70 | public void UnSubscribe(string type, IMessageListener listener) 71 | { 72 | UnSubscribe(type, listener.Listen); 73 | } 74 | 75 | /// 76 | /// 注册监听 77 | /// 78 | /// 79 | /// 80 | /// 81 | public void Subscribe(string type, MessageListener listener) 82 | { 83 | handlers.Subscribe(type, listener); 84 | } 85 | 86 | /// 87 | /// 解除注册监听 88 | /// 89 | /// 90 | /// 91 | /// 92 | public void UnSubscribe(string type, MessageListener listener) 93 | { 94 | handlers.UnSubscribe(type, listener); 95 | } 96 | 97 | /// 98 | /// 发布消息 99 | /// 100 | /// 101 | /// 102 | /// 103 | /// 104 | /// 105 | public IMessage Publish(string type, IEventArgs args, int code = 0, MessageUrgencyType priority = MessageUrgencyType.Common) 106 | { 107 | return PublishByNumber(type, args, code, (int)priority); 108 | } 109 | /// 110 | /// 发布消息 111 | /// 112 | /// 113 | /// 114 | /// 115 | /// 越大处理越晚 116 | /// 117 | public IMessage PublishByNumber(string type, IEventArgs args, int code = 0, int priority = MessageUrgency.Common) 118 | { 119 | return messages.PublishByNumber(type, args, code, priority); 120 | } 121 | } 122 | 123 | } 124 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/String/StringMessageQueue.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | class StringMessageQueue : MessageQueue 4 | { 5 | public StringMessageQueue(HandlerQueue handler) : base(handler) 6 | { 7 | } 8 | 9 | protected override void SetMessage(Message message, string type) 10 | { 11 | message.SetType(type); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/Subject.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | namespace IFramework.Message 4 | { 5 | class Subject : IDisposable 6 | { 7 | private readonly T _listenType; 8 | public T listenType { get { return _listenType; } } 9 | private List _listeners; 10 | public Subject(T listenType) 11 | { 12 | this._listenType = listenType; 13 | _listeners = new List(); 14 | } 15 | public bool Subscribe(MessageListener listener) 16 | { 17 | if (_listeners.Contains(listener)) return false; 18 | else _listeners.Add(listener); return true; 19 | } 20 | public bool UnSubscribe(MessageListener listener) 21 | { 22 | if (!_listeners.Contains(listener)) return false; 23 | else _listeners.Remove(listener); return true; 24 | } 25 | public bool Publish(IMessage message) 26 | { 27 | if (_listeners.Count == 0) return false; 28 | for (int i = _listeners.Count - 1; i >= 0; i--) 29 | { 30 | var listener = _listeners[i]; 31 | if (listener != null) listener.Invoke(message); 32 | else _listeners.Remove(listener); 33 | } 34 | return true; 35 | } 36 | public void Dispose() 37 | { 38 | _listeners.Clear(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/SubjectType.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Message 2 | { 3 | /// 4 | /// 消息主题类型 5 | /// 6 | public enum SubjectType 7 | { 8 | /// 9 | /// 未知 10 | /// 11 | None, 12 | /// 13 | /// String 14 | /// 15 | String, 16 | /// 17 | /// Type 18 | /// 19 | Type, 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/SubscribeAction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace IFramework.Message 3 | { 4 | struct SubscribeAction 5 | { 6 | public readonly bool subscribe; 7 | public readonly T type; 8 | public readonly MessageListener value; 9 | 10 | public SubscribeAction(T type, MessageListener value, bool subscribe) 11 | { 12 | this.subscribe = subscribe; 13 | this.type = type; 14 | this.value = value; 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/Type/IMessageModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Message 4 | { 5 | /// 6 | /// 消息模块 7 | /// 8 | public interface IMessageModule 9 | { 10 | /// 11 | /// 剩余消息数目 12 | /// 13 | int count { get; } 14 | /// 15 | /// 适配子类型 16 | /// 17 | bool fitSubType { get; set; } 18 | /// 19 | /// 每一帧处理消息上限 20 | /// 21 | int processesPerFrame { get; set; } 22 | /// 23 | /// 发布消息 24 | /// 25 | /// 26 | /// 27 | /// 28 | /// 29 | IMessage Publish(Type type, IEventArgs args, int code = 0, MessageUrgencyType priority = MessageUrgencyType.Common); 30 | /// 31 | /// 发布消息 32 | /// 33 | /// 34 | /// 35 | /// 36 | /// 37 | IMessage Publish(IEventArgs args, int code = 0, MessageUrgencyType priority = MessageUrgencyType.Common); 38 | /// 39 | /// 发布消息 40 | /// 41 | /// 42 | /// 43 | /// 44 | /// 45 | /// 46 | IMessage Publish(T t, IEventArgs args, int code = 0, MessageUrgencyType priority = MessageUrgencyType.Common); 47 | /// 48 | /// 发布消息 49 | /// 50 | /// 51 | /// 52 | /// 53 | /// 54 | /// 55 | IMessage PublishByNumber(T t, IEventArgs args, int code = 0, int priority = MessageUrgency.Common); 56 | /// 57 | /// 发布消息 58 | /// 59 | /// 60 | /// 61 | /// 62 | /// 63 | IMessage PublishByNumber(IEventArgs args, int code = 0, int priority = MessageUrgency.Common); 64 | /// 65 | /// 发布消息 66 | /// 67 | /// 68 | /// 69 | /// 70 | /// 71 | IMessage PublishByNumber(Type type, IEventArgs args, int code = 0, int priority = MessageUrgency.Common); 72 | 73 | /// 74 | /// 注册 75 | /// 76 | /// 77 | /// 78 | /// 79 | void Subscribe(Type type, IMessageListener listener); 80 | /// 81 | /// 注册 82 | /// 83 | /// 84 | /// 85 | /// 86 | void Subscribe(Type type, MessageListener listener); 87 | /// 88 | /// 注册 89 | /// 90 | /// 91 | /// 92 | /// 93 | void Subscribe(IMessageListener listener); 94 | /// 95 | /// 注册 96 | /// 97 | /// 98 | /// 99 | /// 100 | void Subscribe(MessageListener listener); 101 | /// 102 | /// 移除 103 | /// 104 | /// 105 | /// 106 | /// 107 | void UnSubscribe(Type type, IMessageListener listener); 108 | /// 109 | /// 移除 110 | /// 111 | /// 112 | /// 113 | /// 114 | void UnSubscribe(Type type, MessageListener listener); 115 | /// 116 | /// 移除 117 | /// 118 | /// 119 | /// 120 | /// 121 | void UnSubscribe(IMessageListener listener); 122 | /// 123 | /// 移除 124 | /// 125 | /// 126 | /// 127 | /// 128 | void UnSubscribe(MessageListener listener); 129 | } 130 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/Type/TypeHandlerQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace IFramework.Message 5 | { 6 | class TypeHandlerQueue : HandlerQueue 7 | { 8 | private bool _fitSubType = false; 9 | 10 | public bool fitSubType { get { return _fitSubType; } set { _fitSubType = value; } } 11 | 12 | public override bool Publish(IMessage message) 13 | { 14 | bool success = false; 15 | var type = message.subject; 16 | if (fitSubType) 17 | { 18 | foreach (var _listenType in subjectMap.Keys) 19 | { 20 | 21 | if (type.GetInterfaces().Contains(_listenType) || type.IsSubclassOf(_listenType) || type == _listenType) 22 | { 23 | success |= subjects[subjectMap[_listenType]].Publish(message); 24 | } 25 | } 26 | } 27 | else 28 | { 29 | if (subjectMap.ContainsKey(type)) 30 | { 31 | success |= subjects[subjectMap[type]].Publish(message); 32 | } 33 | } 34 | return success; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Message/Module/Type/TypeMessageQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace IFramework.Message 3 | { 4 | class TypeMessageQueue : MessageQueue 5 | { 6 | public TypeMessageQueue(HandlerQueue handler) : base(handler) 7 | { 8 | } 9 | 10 | protected override void SetMessage(Message message, Type type) 11 | { 12 | message.SetType(type); 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Module.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework 4 | { 5 | /// 6 | /// 模块 7 | /// 8 | public abstract class Module : Unit 9 | { 10 | /// 11 | /// 默认名字 12 | /// 13 | public const string defaultName = "default"; 14 | 15 | 16 | private bool _binded; 17 | private int _priority; 18 | 19 | /// 20 | /// 优先级(越大释放越早释放,越小越先 update) 21 | /// 22 | public int priority { get { return _priority; } } 23 | 24 | /// 25 | /// 是否绑定了 26 | /// 27 | public bool binded { get { return _binded; } internal set { _binded = value; } } 28 | 29 | /// 30 | /// 名字 31 | /// 32 | public string name { get; set; } 33 | 34 | 35 | 36 | /// 37 | /// 初始化 38 | /// 39 | protected abstract void Awake(); 40 | 41 | /// 42 | /// 阻止 New 43 | /// 44 | protected Module() { } 45 | /// 46 | /// 创建实例 47 | /// 48 | /// 模块类型 49 | /// 模块名称 50 | /// 51 | /// 52 | public static Module CreatInstance(Type type, string name = defaultName, int priority = 0) 53 | { 54 | Module moudle = Activator.CreateInstance(type) as Module; 55 | if (moudle != null) 56 | { 57 | moudle._binded = false; 58 | moudle.name = name; 59 | moudle._priority = moudle.OnGetDefautPriority().value + priority; 60 | moudle.Awake(); 61 | if (moudle is UpdateModule) 62 | { 63 | (moudle as UpdateModule).enable = true; 64 | } 65 | } 66 | else 67 | Log.E(string.Format("Type: {0} Non Public Ctor With 0 para Not Find", type)); 68 | 69 | return moudle; 70 | } 71 | /// 72 | /// 设置优先级 73 | /// 74 | /// 75 | protected virtual ModulePriority OnGetDefautPriority() 76 | { 77 | return ModulePriority.Custom; 78 | } 79 | 80 | /// 81 | /// 创建实例 82 | /// 83 | /// 模块名称 84 | /// 85 | /// 86 | public static T CreatInstance(string name = defaultName, int priority = 0) where T : Module 87 | { 88 | return CreatInstance(typeof(T), name, priority) as T; 89 | } 90 | 91 | 92 | 93 | 94 | 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/ModulePriority.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework 2 | { 3 | /// 4 | /// 默认的模块的优先级 5 | /// 6 | public struct ModulePriority 7 | { 8 | /// 9 | /// 配置表 10 | /// 11 | public const int Config = 0; 12 | /// 13 | /// 环境等待 14 | /// 15 | public const int Loom = 10; 16 | /// 17 | /// undo 18 | /// 19 | public const int Recorder = 30; 20 | 21 | /// 22 | /// 协程 23 | /// 24 | public const int Coroutine = 70; 25 | /// 26 | /// 消息转发 27 | /// 28 | public const int Message = 120; 29 | /// 30 | /// ecs 31 | /// 32 | public const int ECS = 400; 33 | /// 34 | /// fsm 35 | /// 36 | public const int FSM = 500; 37 | /// 38 | /// 计时器 39 | /// 40 | public const int Timer = 600; 41 | /// 42 | /// 其他 43 | /// 44 | public const int Custom = 1000; 45 | private int _value; 46 | /// 47 | /// ctor 48 | /// 49 | /// 50 | public ModulePriority(int value) 51 | { 52 | _value = value; 53 | } 54 | /// 55 | /// 具体的值 56 | /// 57 | public int value { get { return _value; }set { _value = value; } } 58 | 59 | /// 60 | /// 61 | /// 62 | /// 63 | /// 64 | public static ModulePriority FromValue(int value) 65 | { 66 | return new ModulePriority(value); 67 | } 68 | /// 69 | /// 70 | /// 71 | /// 72 | public static implicit operator int(ModulePriority value) 73 | { 74 | return value.value; 75 | } 76 | /// 77 | /// 78 | /// 79 | /// 80 | public static implicit operator ModulePriority(int value) 81 | { 82 | return new ModulePriority(value); 83 | } 84 | /// 85 | /// 86 | /// 87 | /// 88 | /// 89 | /// 90 | public static ModulePriority operator +(ModulePriority a, ModulePriority b) 91 | { 92 | return new ModulePriority(a.value + b.value); 93 | } 94 | /// 95 | /// 96 | /// 97 | /// 98 | /// 99 | /// 100 | public static ModulePriority operator -(ModulePriority a, ModulePriority b) 101 | { 102 | return new ModulePriority(a.value - b.value); 103 | } 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Modules.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using IFramework.Queue; 4 | 5 | namespace IFramework 6 | { 7 | 8 | /// 9 | /// 模块容器 10 | /// 11 | public class Modules : Unit, IModules 12 | { 13 | private object _lock = new object(); 14 | private Dictionary> _dic; 15 | 16 | private SimplePriorityQueue _queue; 17 | private List _updateModules; 18 | 19 | /// 20 | /// 创建一个模块,创建完了自动绑定 21 | /// 22 | /// 23 | /// 24 | /// 25 | /// 26 | public Module CreateModule(Type type, string name = Module.defaultName, int priority = 0) 27 | { 28 | var mou = Module.CreatInstance(type, name, priority); 29 | if (SubscribeModule(mou)) 30 | mou.binded = true; 31 | return mou; 32 | } 33 | /// 34 | /// 创建模块 35 | /// 36 | /// 37 | /// 38 | /// 39 | /// 40 | public T CreateModule(string name = Module.defaultName, int priority = 0) where T : Module 41 | { 42 | return CreateModule(typeof(T), name, priority) as T; 43 | } 44 | 45 | 46 | /// 47 | /// 查找模块 48 | /// 49 | /// 模块类型 50 | /// 模块名称 51 | /// 52 | public Module FindModule(Type type, string name = Module.defaultName) 53 | { 54 | if (string.IsNullOrEmpty(name)) 55 | name = type.Name; 56 | if (!_dic.ContainsKey(type)) return null; 57 | if (!_dic[type].ContainsKey(name)) return null; 58 | var module = _dic[type][name]; 59 | return module; 60 | 61 | } 62 | /// 63 | /// 获取模块 64 | /// 65 | /// 66 | /// 67 | /// 68 | /// 69 | public Module GetModule(Type type, string name = Module.defaultName, int priority = 0) 70 | { 71 | var tmp = FindModule(type, name); 72 | if (tmp == null) 73 | { 74 | tmp = CreateModule(type, name, priority); 75 | } 76 | return tmp; 77 | } 78 | 79 | 80 | /// 81 | /// 查找模块 82 | /// 83 | /// 84 | /// 85 | /// 86 | public T FindModule(string name = Module.defaultName) where T : Module 87 | { 88 | return FindModule(typeof(T), name) as T; 89 | } 90 | /// 91 | /// 获取模块 92 | /// 93 | /// 94 | /// 95 | /// 96 | /// 97 | public T GetModule(string name = Module.defaultName, int priority = 0) where T : Module 98 | { 99 | return GetModule(typeof(T), name, priority) as T; 100 | } 101 | 102 | /// 103 | /// Ctor 104 | /// 105 | public Modules() 106 | { 107 | _dic = new Dictionary>(); 108 | _queue = new SimplePriorityQueue(); 109 | _updateModules = new List(); 110 | } 111 | /// 112 | /// 绑定环境 113 | /// 114 | 115 | /// 116 | /// 释放 117 | /// 118 | protected override void OnDispose() 119 | { 120 | // using (new LockWait(ref _lock)) 121 | { 122 | int count = _queue.count; 123 | Stack _modules = new Stack(); 124 | for (int i = 0; i < count; i++) 125 | { 126 | var item = _queue.Dequeue(); 127 | 128 | _modules.Push(item); 129 | 130 | } 131 | 132 | for (int i = 0; i < count; i++) 133 | { 134 | var item = _modules.Pop(); 135 | UnSubscribeBindModule(item); 136 | item.Dispose(); 137 | } 138 | _updateModules.Clear(); 139 | _queue = null; 140 | _dic.Clear(); 141 | _dic = null; 142 | } 143 | } 144 | internal void Update() 145 | { 146 | for (int i = 0; i < _updateModules.Count; i++) 147 | { 148 | _updateModules[i].Update(); 149 | } 150 | } 151 | private void SyncUpdateList() 152 | { 153 | _updateModules.Clear(); 154 | foreach (var item in _queue) 155 | { 156 | if (item is UpdateModule) 157 | { 158 | _updateModules.Add(item as UpdateModule); 159 | } 160 | } 161 | } 162 | 163 | private bool SubscribeModule(Module moudle) 164 | { 165 | lock (_lock) 166 | { 167 | Type type = moudle.GetType(); 168 | if (!_dic.ContainsKey(type)) 169 | _dic.Add(type, new Dictionary()); 170 | var list = _dic[type]; 171 | if (list.ContainsKey(moudle.name)) 172 | { 173 | Log.E(string.Format("Have Bind Module | Type {0} Name {1}", type, moudle.name)); 174 | return false; 175 | } 176 | else 177 | { 178 | list.Add(moudle.name, moudle); 179 | 180 | _queue.Enqueue(moudle, moudle.priority); 181 | SyncUpdateList(); 182 | return true; 183 | } 184 | } 185 | 186 | 187 | } 188 | private bool UnSubscribeBindModule(Module moudle) 189 | { 190 | if (!moudle.binded) return false; 191 | moudle.binded = false; 192 | Type type = moudle.GetType(); 193 | if (!_dic.ContainsKey(type)) 194 | { 195 | Log.E(string.Format("01,Have Not Bind Module | Type {0} Name {1}", type, moudle.name)); 196 | return false; 197 | } 198 | else 199 | { 200 | var list = _dic[type]; 201 | 202 | if (!list.ContainsKey(moudle.name)) 203 | { 204 | Log.E(string.Format("02,Have Not Bind Module | Type {0} Name {1}", type, moudle.name)); 205 | return false; 206 | } 207 | else 208 | { 209 | _dic[type].Remove(moudle.name); 210 | if (_queue.Contains(moudle)) 211 | { 212 | _queue.Remove(moudle); 213 | SyncUpdateList(); 214 | } 215 | return true; 216 | } 217 | } 218 | 219 | } 220 | 221 | 222 | } 223 | 224 | } 225 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/Module/ICommand.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Recorder 2 | { 3 | /// 4 | /// 命令 5 | /// 6 | public interface ICommand 7 | { 8 | /// 9 | /// 处理 10 | /// 11 | void Excute(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/Module/IOperationRecorderModule.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace IFramework.Recorder 4 | { 5 | /// 6 | /// 操作记录 7 | /// 8 | public interface IOperationRecorderModule 9 | { 10 | /// 11 | /// 分配 12 | /// 13 | /// 14 | /// 15 | T Allocate() where T : BaseState, new(); 16 | /// 17 | /// 执行 18 | /// 19 | /// 20 | bool Redo(); 21 | /// 22 | /// 注册 23 | /// 24 | /// 25 | /// 26 | void Subscribe(BaseState state, bool redo = true); 27 | 28 | /// 29 | /// 撤回 30 | /// 31 | /// 32 | bool Undo(); 33 | 34 | /// 35 | /// 获取记录列表 36 | /// 37 | /// 38 | List GetRecordNames(out int index); 39 | /// 40 | /// 获取当前节点的名字 41 | /// 42 | /// 43 | string GetCurrentRecordName(); 44 | } 45 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/Module/OperationRecorderEx.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Recorder 4 | { 5 | /// 6 | /// 扩展 7 | /// 8 | public static class OperationRecorderEx 9 | { 10 | /// 11 | /// 分配命令 12 | /// 13 | /// 14 | /// 15 | public static CommandState AllocateCommand(this IOperationRecorderModule t) 16 | { 17 | return t.Allocate(); 18 | } 19 | /// 20 | /// 分配回调 21 | /// 22 | /// 23 | /// 24 | public static ActionState AllocateAction(this IOperationRecorderModule t) 25 | { 26 | return t.Allocate(); 27 | } 28 | /// 29 | /// 分配命令组 30 | /// 31 | /// 32 | /// 33 | public static CommandGroupState AllocateCommandGroup(this IOperationRecorderModule t) 34 | { 35 | return t.Allocate(); 36 | } 37 | /// 38 | /// 分配回调组 39 | /// 40 | /// 41 | /// 42 | public static ActionGroupState AllocateActionGroup(this IOperationRecorderModule t) 43 | { 44 | return t.Allocate(); 45 | } 46 | 47 | /// 48 | /// 设置值 49 | /// 50 | /// 51 | /// 52 | /// 53 | /// 54 | /// 55 | public static T SetCommand(this T t, ICommand redo, ICommand undo) where T : CommandState 56 | { 57 | t.SetValue(redo, undo); 58 | return t; 59 | } 60 | /// 61 | /// 设置值 62 | /// 63 | /// 64 | /// 65 | /// 66 | /// 67 | /// 68 | public static T SetGroupCommand(this T t, ICommand redo, ICommand undo) where T : CommandGroupState 69 | { 70 | t.SetValue(redo, undo); 71 | return t; 72 | } 73 | /// 74 | /// 设置值 75 | /// 76 | /// 77 | /// 78 | /// 79 | /// 80 | /// 81 | public static T SetCommand(this T t, Action redo, Action undo) where T : ActionState 82 | { 83 | t.SetValue(redo, undo); 84 | return t; 85 | } 86 | /// 87 | /// 设置值 88 | /// 89 | /// 90 | /// 91 | /// 92 | /// 93 | /// 94 | public static T SetGroupCommand(this T t, Action redo, Action undo) where T : ActionGroupState 95 | { 96 | t.SetValue(redo, undo); 97 | return t; 98 | } 99 | /// 100 | /// 注册 101 | /// 102 | /// 103 | /// 104 | /// 105 | /// 106 | public static T Subscribe(this T t, bool redo = true) where T : BaseState 107 | { 108 | t.recorder.Subscribe(t, redo); 109 | return t; 110 | } 111 | } 112 | } -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/Module/OperationRecorderModule.HeadState.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Recorder 2 | { 3 | public partial class OperationRecorderModule 4 | { 5 | private class HeadState : BaseState 6 | { 7 | protected override void OnRedo() { } 8 | 9 | protected override void OnUndo() { } 10 | 11 | protected override void OnReset() { } 12 | 13 | public override object Clone() 14 | { 15 | return null; 16 | } 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/Module/OperationRecorderModule.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace IFramework.Recorder 4 | { 5 | /// 6 | /// 操作记录 7 | /// 8 | public partial class OperationRecorderModule : Module, IOperationRecorderModule 9 | { 10 | private HeadState _head; 11 | private BaseState _current; 12 | 13 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 14 | protected override ModulePriority OnGetDefautPriority() 15 | { 16 | return ModulePriority.Recorder; 17 | } 18 | protected override void Awake() 19 | { 20 | _head = Allocate(); 21 | _head.SetName("head"); 22 | _current = _head; 23 | } 24 | protected override void OnDispose() 25 | { 26 | Recyle(_head); 27 | _current = _head = null; 28 | } 29 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 30 | 31 | /// 32 | /// 分配状态 33 | /// 34 | /// 35 | /// 36 | public T Allocate() where T : BaseState, new() 37 | { 38 | var state = new T(); 39 | state.recorder = this; 40 | state.SetName(null); 41 | return state; 42 | } 43 | /// 44 | /// 注册 45 | /// 46 | /// 47 | /// 48 | public void Subscribe(BaseState state, bool redo = true) 49 | { 50 | if (state.recorder == null) 51 | Log.E("Don't Create new State ,Use Allocate"); 52 | if (_current == null) _current = _head; 53 | if (state.guid == _current.guid) 54 | { 55 | state = state.Clone() as BaseState; 56 | state.SetName($"{_current.name}#Clone"); 57 | } 58 | if (_current.next != null) 59 | { 60 | Recyle(_current.next); 61 | } 62 | _current.next = state; 63 | state.front = _current; 64 | _current = state; 65 | if (redo) state.Redo(); 66 | } 67 | /// 68 | /// 获取记录列表 69 | /// 70 | /// 当前记录的位置 71 | /// 72 | public List GetRecordNames(out int index) 73 | { 74 | index = 0; 75 | List names = new List(); 76 | BaseState baseState = _head; 77 | while (baseState != null) 78 | { 79 | if (_current == baseState) 80 | { 81 | index = names.Count; 82 | } 83 | names.Add(baseState.name); 84 | baseState = baseState.next; 85 | } 86 | return names; 87 | } 88 | 89 | 90 | 91 | Queue queue = new Queue(); 92 | private void Recyle(BaseState state) 93 | { 94 | do 95 | { 96 | var now = state; 97 | state = state.next; 98 | now.Reset(); 99 | queue.Enqueue(now); 100 | } while (state != null); 101 | while (queue.Count != 0) 102 | { 103 | queue.Dequeue(); 104 | } 105 | } 106 | /// 107 | /// 撤回 108 | /// 109 | /// 110 | public bool Undo() 111 | { 112 | if (_current == _head) return false; 113 | _current.Undo(); 114 | _current = _current.front; 115 | return true; 116 | } 117 | /// 118 | /// 执行 119 | /// 120 | /// 121 | public bool Redo() 122 | { 123 | if (_current.next == null) return false; 124 | _current = _current.next; 125 | _current.Redo(); 126 | return true; 127 | } 128 | 129 | /// 130 | /// 获取当前节点的名字 131 | /// 132 | /// 133 | public string GetCurrentRecordName() 134 | { 135 | return _current.name; 136 | } 137 | } 138 | } 139 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/States/ActionGroupState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Recorder 5 | { 6 | /// 7 | /// 回调组 8 | /// 9 | public class ActionGroupState : BaseState 10 | { 11 | internal void SetValue(Action redo, Action undo) 12 | { 13 | this.redo.Add(redo); 14 | this.undo.Add(undo); 15 | } 16 | /// 17 | /// 执行 18 | /// 19 | protected override void OnRedo() 20 | { 21 | for (int i = 0; i < redo.Count; i++) 22 | { 23 | redo[i](); 24 | } 25 | } 26 | /// 27 | /// 撤回 28 | /// 29 | protected override void OnUndo() 30 | { 31 | for (int i = redo.Count - 1; i >= 0; i--) 32 | { 33 | undo[i](); 34 | } 35 | } 36 | /// 37 | /// 重置数据 38 | /// 39 | protected override void OnReset() 40 | { 41 | redo.Clear(); 42 | undo.Clear(); 43 | } 44 | 45 | /// 46 | /// 赋值 47 | /// 48 | /// 49 | public override object Clone() 50 | { 51 | return new ActionGroupState() 52 | { 53 | recorder = recorder, 54 | redo = new List(redo), 55 | undo = new List(undo), 56 | _id = _id 57 | }; 58 | } 59 | 60 | private List redo = new List(); 61 | private List undo = new List(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/States/ActionState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Recorder 4 | { 5 | /// 6 | /// 回调状态 7 | /// 8 | public class ActionState : BaseState 9 | { 10 | internal void SetValue(Action redo, Action undo) 11 | { 12 | this.redo = redo; 13 | this.undo = undo; 14 | } 15 | /// 16 | /// 执行 17 | /// 18 | protected override void OnRedo() 19 | { 20 | redo(); 21 | } 22 | /// 23 | /// 撤回 24 | /// 25 | protected override void OnUndo() 26 | { 27 | undo(); 28 | } 29 | /// 30 | /// 重置数据 31 | /// 32 | protected override void OnReset() 33 | { 34 | redo = null; 35 | undo = null; 36 | } 37 | /// 38 | /// 复制 39 | /// 40 | /// 41 | public override object Clone() 42 | { 43 | return new ActionState() 44 | { 45 | recorder = recorder, 46 | redo = redo, 47 | undo = undo, 48 | _id = _id 49 | }; 50 | } 51 | 52 | private Action redo; 53 | private Action undo; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/States/BaseState.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Recorder 4 | { 5 | /// 6 | /// 状态基类 7 | /// 8 | public abstract class BaseState : ICloneable 9 | { 10 | internal BaseState front; 11 | internal BaseState next; 12 | internal OperationRecorderModule recorder; 13 | /// 14 | /// id 15 | /// 16 | protected Guid _id = Guid.NewGuid(); 17 | 18 | /// 19 | /// id 20 | /// 21 | public Guid guid { get { return _id; } } 22 | /// 23 | /// 名字 24 | /// 25 | public string name { get; private set; } 26 | internal void Redo() { OnRedo(); } 27 | internal void Undo() { OnUndo(); } 28 | internal virtual void Reset() 29 | { 30 | front = null; 31 | next = null; 32 | recorder = null; 33 | OnReset(); 34 | } 35 | 36 | 37 | /// 38 | /// 执行 39 | /// 40 | protected abstract void OnRedo(); 41 | /// 42 | /// 撤回 43 | /// 44 | protected abstract void OnUndo(); 45 | /// 46 | /// 重置数据 47 | /// 48 | protected abstract void OnReset(); 49 | /// 50 | /// 复制 51 | /// 52 | /// 53 | public abstract object Clone(); 54 | 55 | /// 56 | /// 设置名字 57 | /// 58 | /// 名字 59 | public void SetName(string name) 60 | { 61 | this.name = name; 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/States/CommandGroupState.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace IFramework.Recorder 4 | { 5 | /// 6 | /// 命令组 7 | /// 8 | public class CommandGroupState : BaseState 9 | { 10 | internal void SetValue(ICommand redo, ICommand undo) 11 | { 12 | this.redo.Add(redo); 13 | this.undo.Add(undo); 14 | } 15 | /// 16 | /// 执行 17 | /// 18 | protected override void OnRedo() 19 | { 20 | for (int i = 0; i < redo.Count; i++) 21 | { 22 | redo[i].Excute(); 23 | } 24 | } 25 | /// 26 | /// 撤回 27 | /// 28 | protected override void OnUndo() 29 | { 30 | for (int i = redo.Count - 1; i >= 0; i--) 31 | { 32 | undo[i].Excute(); 33 | } 34 | } 35 | /// 36 | /// 重置数据 37 | /// 38 | protected override void OnReset() 39 | { 40 | redo.Clear(); 41 | undo.Clear(); 42 | } 43 | /// 44 | /// 复制 45 | /// 46 | /// 47 | public override object Clone() 48 | { 49 | return new CommandGroupState() 50 | { 51 | recorder = recorder, 52 | redo = new List(redo), 53 | undo = new List(undo), 54 | _id = _id 55 | }; 56 | } 57 | 58 | private List redo = new List(); 59 | private List undo = new List(); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Recorder/States/CommandState.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Recorder 2 | { 3 | /// 4 | /// 命令状态 5 | /// 6 | public class CommandState: BaseState 7 | { 8 | internal void SetValue(ICommand redo,ICommand undo) 9 | { 10 | this.redo = redo; 11 | this.undo = undo; 12 | } 13 | /// 14 | /// 执行 15 | /// 16 | protected override void OnRedo() 17 | { 18 | redo.Excute(); 19 | } 20 | /// 21 | /// 撤回 22 | /// 23 | protected override void OnUndo() 24 | { 25 | undo.Excute(); 26 | } 27 | 28 | /// 29 | /// 重置数据 30 | /// 31 | protected override void OnReset() 32 | { 33 | redo = null; 34 | undo = null; 35 | } 36 | //复制 37 | public override object Clone() 38 | { 39 | return new CommandState() 40 | { 41 | recorder = recorder, 42 | redo = redo, 43 | undo = undo, 44 | _id = _id 45 | }; 46 | } 47 | 48 | private ICommand redo; 49 | private ICommand undo; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Timer/Entity/EntityState.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Timer 2 | { 3 | /// 4 | /// 当前时间的状态 5 | /// 6 | public enum EntityState 7 | { 8 | /// 9 | /// 未开始 10 | /// 11 | NotStart, 12 | /// 13 | /// 正在等待 14 | /// 15 | Waiting, 16 | /// 17 | /// 正在执行 18 | /// 19 | Running, 20 | /// 21 | /// 暂停 22 | /// 23 | Pause, 24 | /// 25 | /// 完成 26 | /// 27 | Done, 28 | /// 29 | /// 无状态(默认状态) 30 | /// 31 | None 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Timer/Entity/ITimerEntity.cs: -------------------------------------------------------------------------------- 1 |  2 | using System; 3 | 4 | namespace IFramework.Timer 5 | { 6 | /// 7 | /// TimeEntity接口 8 | /// 9 | public interface ITimerEntity 10 | { 11 | /// 12 | /// 每次调用的等待时间 13 | /// 14 | float repeatDelay { get; } 15 | /// 16 | /// 开始调用的等待时间 17 | /// 18 | float delay { get; } 19 | /// 20 | /// 执行次数 21 | /// 22 | int repeat { get; } 23 | /// 24 | /// 时间比例 25 | /// 26 | float timeScale { get; } 27 | /// 28 | /// 状态 29 | /// 30 | EntityState state { get; } 31 | /// 32 | /// 继续 33 | /// 34 | void Start(); 35 | /// 36 | /// 暂停 37 | /// 38 | void Pause(); 39 | /// 40 | /// 取消 41 | /// 42 | void Cancel(bool callComplete); 43 | /// 44 | /// 设置子定时器 45 | /// 46 | /// 子定时器 47 | /// 子定时器类型 48 | void SetInnerTimer(ITimerEntity timerEntity, InnerType type = InnerType.Parallel); 49 | /// 50 | /// 注册 51 | /// 52 | void Subscribe(); 53 | /// 54 | /// 设置时间比例 55 | /// 56 | void SetTimeScale(float scale); 57 | 58 | /// 59 | /// 注册开始调用的回调方法 60 | /// 61 | /// 回调方法 62 | void SubscribeStart(Action startAction); 63 | /// 64 | /// 解绑开始调用的回调方法 65 | /// 66 | /// 回调方法 67 | void UnSubscribeStart(Action startAction); 68 | /// 69 | /// 注册每帧的回调方法 70 | /// 71 | /// 回调方法 72 | void SubsribeUpdate(Action updateAction); 73 | /// 74 | /// 解绑每帧的回调方法 75 | /// 76 | /// 回调方法 77 | void UnSubsribeUpdate(Action updateAction); 78 | /// 79 | /// 注册完成的回调方法 80 | /// 81 | /// 回调函数 82 | void SubscribeComplete(Action completeAction); 83 | /// 84 | /// 解绑完成的回调方法 85 | /// 86 | /// 回调函数 87 | void UnSubscribeComplete(Action completeAction); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Timer/Entity/InnerType.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Timer 2 | { 3 | /// 4 | /// 当前时间的状态 5 | /// 6 | public enum InnerType 7 | { 8 | /// 9 | /// 串行 10 | /// 11 | Parallel, 12 | /// 13 | /// 并行 14 | /// 15 | Serial 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Timer/Module/ITimerModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Timer 4 | { 5 | /// 6 | /// 计时器 7 | /// 8 | public interface ITimerModule 9 | { 10 | /// 11 | /// 注册方法 12 | /// 13 | /// 14 | void Subscribe(ITimerEntity actionItem); 15 | /// 16 | /// 清除所有定时方法 17 | /// 18 | void Clear(); 19 | /// 20 | /// 分配 21 | /// 22 | /// 23 | ITimerEntity Allocate(Action action, float repeatDelay, int repeat = 1, float delay = 0, float timeScale = 1f); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/Timer/Module/TimerModule.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Timer 5 | { 6 | /// 7 | /// 时间模块 8 | /// 9 | public class TimerModule : UpdateModule, ITimerModule 10 | { 11 | /// 12 | /// 优先级 13 | /// 14 | /// 15 | protected override ModulePriority OnGetDefautPriority() 16 | { 17 | return ModulePriority.Timer; 18 | } 19 | 20 | private List _entities; 21 | DateTime _lastTime; //保存上一次的时间,便于计算DeltaTime; 22 | /// 23 | /// 清除方法 24 | /// 25 | public void Clear() 26 | { 27 | _entities.Clear(); 28 | } 29 | /// 30 | /// 注册定时方法 31 | /// 32 | /// 33 | public void Subscribe(ITimerEntity entity) 34 | { 35 | 36 | if (entity.state != EntityState.NotStart) 37 | { 38 | Log.E("Don't Subscribe an Used entity"); 39 | } 40 | 41 | if ((entity as TimerEntity).timer == null) 42 | Log.E("Don't Create new entity ,Use Allocate"); 43 | 44 | if (_entities.Contains((entity as TimerEntity))) 45 | { 46 | throw new InvalidOperationException("This item has been already subscribed"); 47 | } 48 | _entities.Add((entity as TimerEntity)); 49 | entity.Start(); 50 | } 51 | /// 52 | /// awake 53 | /// 54 | protected override void Awake() 55 | { 56 | _entities = new List(); 57 | _lastTime = DateTime.Now; 58 | } 59 | 60 | /// 61 | /// dispose 62 | /// 63 | protected override void OnDispose() 64 | { 65 | for (int i = 0; i < _entities.Count; i++) 66 | { 67 | _entities[i].Reset(); 68 | } 69 | _entities.Clear(); 70 | } 71 | 72 | /// 73 | /// update 74 | /// 75 | protected override void OnUpdate() 76 | { 77 | if (_entities.Count <= 0) return; 78 | 79 | var deltaTime = (float)(DateTime.Now - _lastTime).TotalMilliseconds; 80 | 81 | _lastTime = DateTime.Now; 82 | 83 | for (int i = _entities.Count - 1; i >= 0; i--) 84 | { 85 | var entity = _entities[i]; 86 | if (entity.state == EntityState.Done) 87 | { 88 | _entities.Remove(entity); 89 | entity.Reset(); 90 | continue; 91 | } 92 | 93 | entity.Update(deltaTime); 94 | } 95 | } 96 | 97 | /// 98 | /// 全局分配 99 | /// 100 | /// 调用的方法 101 | /// 延迟时间 102 | /// 执行次数 103 | /// 开始定时器的等待时间 104 | /// 时间比例 105 | /// 获取的TimerEntity 106 | public ITimerEntity Allocate(Action action, float repeatDelay, int repeat = 1,float delay = 0, float timeScale = 1f) 107 | { 108 | var entity = new TimerEntity(); 109 | entity.timer = this; 110 | entity._action = action; 111 | entity._repeatDelay = repeatDelay; 112 | entity._repeat = repeat; 113 | entity._timeScale = timeScale; 114 | entity._delay = delay; 115 | entity._state = EntityState.NotStart; 116 | return entity; 117 | } 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Modules/UpdateModule.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework 2 | { 3 | /// 4 | /// OnUpdate OnEnable OnDisable 5 | /// 6 | public abstract class UpdateModule: Module 7 | { 8 | private bool _enable; 9 | /// 10 | /// 开启关闭 Update 11 | /// 12 | public bool enable 13 | { 14 | get { return _enable; } 15 | set 16 | { 17 | if (_enable != value) 18 | _enable = value; 19 | if (_enable) 20 | OnEnable(); 21 | else 22 | OnDisable(); 23 | } 24 | } 25 | /// 26 | /// 改变 enable 27 | /// 28 | /// 29 | public void SetActive(bool enable) { this.enable = enable; } 30 | /// 31 | /// 释放 32 | /// 33 | public override void Dispose() 34 | { 35 | enable = false; 36 | base.Dispose(); 37 | } 38 | /// 39 | /// 刷新 40 | /// 41 | public void Update() 42 | { 43 | if (!enable || disposed) return; 44 | OnUpdate(); 45 | } 46 | #pragma warning disable CS1591 // 缺少对公共可见类型或成员的 XML 注释 47 | protected abstract void OnUpdate(); 48 | protected virtual void OnEnable() { } 49 | protected virtual void OnDisable() { } 50 | #pragma warning restore CS1591 // 缺少对公共可见类型或成员的 XML 注释 51 | 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/ObjectPool.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework 5 | { 6 | /// 7 | /// 基础对象池 8 | /// 9 | /// 10 | internal abstract class ObjectPool : Unit, IDisposable 11 | { 12 | /// 13 | /// 数据容器 14 | /// 15 | protected Queue pool { get { return _lazy.Value; } } 16 | private Lazy> _lazy = new Lazy>(() => { return new Queue(); }, true); 17 | /// 18 | /// 自旋锁 19 | /// 20 | protected object para = new object(); 21 | /// 22 | /// 存储数据类型 23 | /// 24 | public virtual Type type { get { return typeof(T); } } 25 | 26 | /// 27 | /// 池子数量 28 | /// 29 | public int count { get { return pool.Count; } } 30 | 31 | 32 | /// 33 | /// 释放时 34 | /// 35 | protected override void OnDispose() 36 | { 37 | Clear(); 38 | } 39 | 40 | /// 41 | /// 获取 42 | /// 43 | /// 44 | public virtual T Get() 45 | { 46 | lock (para) 47 | { 48 | T t; 49 | if (pool.Count > 0) 50 | { 51 | t = pool.Dequeue(); 52 | } 53 | else 54 | { 55 | t = CreateNew(); 56 | OnCreate(t); 57 | } 58 | OnGet(t); 59 | return t; 60 | } 61 | } 62 | 63 | /// 64 | /// 回收 65 | /// 66 | /// 67 | /// 68 | public virtual bool Set(T t) 69 | { 70 | lock (para) 71 | { 72 | if (!pool.Contains(t)) 73 | { 74 | if (OnSet(t)) 75 | { 76 | pool.Enqueue(t); 77 | } 78 | return true; 79 | } 80 | else 81 | { 82 | Log.E("Set Err: Exist " + type); 83 | return false; 84 | } 85 | } 86 | } 87 | 88 | /// 89 | /// 清除 90 | /// 91 | public void Clear() 92 | { 93 | lock (para) 94 | { 95 | while (pool.Count > 0) 96 | { 97 | var t = pool.Dequeue(); 98 | OnClear(t); 99 | IDisposable dispose = t as IDisposable; 100 | if (dispose != null) 101 | dispose.Dispose(); 102 | } 103 | } 104 | } 105 | /// 106 | /// 清除 107 | /// 108 | /// 109 | public void Clear(int count) 110 | { 111 | lock (para) 112 | { 113 | count = count > pool.Count ? 0 : pool.Count - count; 114 | while (pool.Count > count) 115 | { 116 | var t = pool.Dequeue(); 117 | OnClear(t); 118 | } 119 | } 120 | } 121 | /// 122 | /// 创建一个新对象 123 | /// 124 | /// 125 | protected abstract T CreateNew(); 126 | /// 127 | /// 数据被清除时 128 | /// 129 | /// 130 | protected virtual void OnClear(T t) { } 131 | /// 132 | /// 数据被回收时,返回true可以回收 133 | /// 134 | /// 135 | /// 136 | protected virtual bool OnSet(T t) 137 | { 138 | return true; 139 | } 140 | /// 141 | /// 数据被获取时 142 | /// 143 | /// 144 | protected virtual void OnGet(T t) { } 145 | /// 146 | /// 数据被创建时 147 | /// 148 | /// 149 | protected virtual void OnCreate(T t) { } 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Queue/FastPriorityQueueNode.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Queue 2 | { 3 | /// 4 | /// 快速优先级队列节点 5 | /// 6 | public class FastPriorityQueueNode 7 | { 8 | /// 9 | /// 节点元素的优先级
. 10 | /// 无法修改 - 请查阅 queue.Enqueue() 和 queue.UpdatePriority() 11 | ///
12 | public float priority { get; protected internal set; } 13 | 14 | /// 15 | /// 当前元素在队列里的位置 16 | /// 17 | public int position { get; internal set; } 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Queue/GenericPriorityQueueNode.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Queue 2 | { 3 | /// 4 | /// 泛型优先级队列节点 5 | /// 6 | /// 优先程度 7 | public class GenericPriorityQueueNode 8 | { 9 | /// 10 | /// 节点元素的优先级
11 | /// 无法修改 - 请查阅 queue.Enqueue() 和 queue.UpdatePriority() 12 | ///
13 | public TPriority priority { get; protected internal set; } 14 | 15 | /// 16 | /// 当前元素在队列中的位置 17 | /// 18 | public int position { get; internal set; } 19 | 20 | /// 21 | /// 元素插入时的序号 22 | /// 23 | public long insertPosition { get; internal set; } 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Queue/IFixedSizePriorityQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Queue 4 | { 5 | /// 6 | /// 可变大小的优先级队列 7 | /// 8 | /// 队列元素 9 | /// 紧急程度 10 | internal interface IFixedSizePriorityQueue : IPriorityQueue where TPriority : IComparable 11 | { 12 | /// 13 | /// 重新设置队列的大小,使其可以容纳更多的节点,当前的所有节点都会保留
14 | /// 如果试图将队列大小设置成比当前队列中的数量小时,会导致未定义的操作 15 | ///
16 | /// 要设置的最大数量 17 | void Resize(int maxNodes); 18 | 19 | /// 20 | /// 队列中可以入队的最大数量
21 | /// 如果入队数量超过队列的大小(Count == MaxSize时Enqueue()),会导致未定义的操作 22 | ///
23 | int capcity { get; } 24 | 25 | /// 26 | /// 默认情况下在队列中的元素不能添加到另一个队列
27 | /// 如果需要这么做,则在添加到另一个队列前调用当前队列的此方法 28 | ///
29 | void ResetNode(TItem node); 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Queue/IPriorityQueue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Queue 5 | { 6 | /// 7 | /// 优先级队列接口 8 | /// 9 | /// 队列元素 10 | /// 紧急程度 11 | public interface IPriorityQueue : IEnumerable where TPriority : IComparable 12 | { 13 | /// 14 | /// 将优先级节点入队,优先级值小的将会排在队列前面(越小越紧急) 15 | /// 16 | void Enqueue(TItem node, TPriority priority); 17 | 18 | /// 19 | /// 将队列的第一个元素从队列中出队 20 | /// 21 | TItem Dequeue(); 22 | 23 | /// 24 | /// 清除队列里的所有元素 25 | /// 26 | void Clear(); 27 | 28 | /// 29 | /// 判断队列中是否包含所给元素 30 | /// 31 | /// 需要判断是否包含的元素 32 | /// 如果包含则返回true,否则返回false 33 | bool Contains(TItem node); 34 | 35 | /// 36 | /// 在队列中移除匹配的第一个的所给元素 37 | /// 38 | void Remove(TItem node); 39 | 40 | /// 41 | /// 更新节点元素的优先程度 42 | /// 43 | void UpdatePriority(TItem node, TPriority priority); 44 | 45 | /// 46 | /// 队列的第一个元素 47 | /// 48 | TItem first { get; } 49 | 50 | /// 51 | /// 元素数量 52 | /// 53 | int count { get; } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Queue/StablePriorityQueueNode.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Queue 2 | { 3 | /// 4 | /// 稳定优先级队列节点 5 | /// 6 | public class StablePriorityQueueNode : FastPriorityQueueNode 7 | { 8 | /// 9 | /// 元素插入时的序号 10 | /// 11 | public long insertPosition { get; internal set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Singleton/ISingleton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Singleton 4 | { 5 | /// 6 | /// 单例 7 | /// 8 | public interface ISingleton : IDisposable 9 | { 10 | /// 11 | /// 单例初始化 12 | /// 13 | void OnSingletonInit(); 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Singleton/Singleton.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Singleton 4 | { 5 | /// 6 | /// 单例基类 7 | /// 8 | /// 9 | public abstract class Singleton : Unit, ISingleton where T : Singleton 10 | { 11 | //防止线程优化 12 | private volatile static T _instance; 13 | static object lockObj = new object(); 14 | /// 15 | /// 实例 16 | /// 17 | public static T instance 18 | { 19 | get 20 | { 21 | //通过double check优化 22 | if (_instance == null) 23 | { 24 | lock (lockObj) 25 | { 26 | if (_instance == null) 27 | { 28 | _instance = SingletonCreator.CreateSingleton(); 29 | SingletonCollection.Set(_instance); 30 | } 31 | } 32 | } 33 | return _instance; 34 | } 35 | } 36 | /// 37 | /// ctror 38 | /// 39 | protected Singleton() { } 40 | /// 41 | /// 初始化 42 | /// 43 | protected virtual void OnSingletonInit() { } 44 | /// 45 | /// 注销 46 | /// 47 | public override void Dispose() 48 | { 49 | base.Dispose(); 50 | if (!disposed) 51 | { 52 | _instance = null; 53 | } 54 | } 55 | 56 | void ISingleton.OnSingletonInit() 57 | { 58 | OnSingletonInit(); 59 | } 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Singleton/SingletonCollection.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace IFramework.Singleton 5 | { 6 | /// 7 | /// 单例合集 8 | /// 9 | public static class SingletonCollection 10 | { 11 | static Dictionary pairs; 12 | static SingletonCollection() 13 | { 14 | pairs = new Dictionary(); 15 | // Framework.onDispose += Dispose; 16 | } 17 | /// 18 | /// 注入单例 19 | /// 20 | /// 21 | /// 22 | public static void Set(T singleton) where T : ISingleton 23 | { 24 | Type type = typeof(T); 25 | if (!pairs.ContainsKey(type)) 26 | pairs.Add(type, singleton); 27 | else 28 | throw new Exception("Singleton Err"); 29 | } 30 | /// 31 | /// 注销一个单例 32 | /// 33 | /// 34 | public static void Dispose() where T : ISingleton 35 | { 36 | Type type = typeof(T); 37 | if (pairs.ContainsKey(type)) 38 | { 39 | pairs[type].Dispose(); 40 | pairs.Remove(type); 41 | } 42 | else 43 | throw new Exception("SingletonPool dispose Err "+typeof(T)); 44 | } 45 | /// 46 | /// 注销所有单例 47 | /// 48 | public static void Dispose() 49 | { 50 | foreach (var item in pairs.Values) 51 | { 52 | item.Dispose(); 53 | } 54 | pairs.Clear(); 55 | } 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Singleton/SingletonCreator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | 4 | namespace IFramework.Singleton 5 | { 6 | static class SingletonCreator 7 | { 8 | public static T CreateSingleton() where T : class, ISingleton 9 | { 10 | var ctors = typeof(T).GetConstructors(BindingFlags.Instance | BindingFlags.NonPublic); 11 | var ctor = Array.Find(ctors, c => c.GetParameters().Length == 0); 12 | if (ctor == null) throw new Exception("Non-Public Constructor() not found! in " + typeof(T)); 13 | var retInstance = ctor.Invoke(null) as T; 14 | retInstance.OnSingletonInit(); 15 | return retInstance; 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Singleton/SingletonProperty.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework.Singleton 2 | { 3 | /// 4 | /// 单例属性 5 | /// 6 | /// 7 | public static class SingletonProperty where T : class, ISingleton 8 | { 9 | //防止线程优化 10 | private volatile static T _instance; 11 | private static readonly object lockObj = new object(); 12 | /// 13 | /// 实例 14 | /// 15 | public static T instance 16 | { 17 | get 18 | { 19 | //通过double check优化 20 | if (_instance == null) 21 | { 22 | lock (lockObj) 23 | { 24 | if (_instance == null) 25 | { 26 | _instance = SingletonCreator.CreateSingleton(); 27 | SingletonCollection.Set(_instance); 28 | } 29 | } 30 | } 31 | return _instance; 32 | } 33 | } 34 | /// 35 | /// 注销 36 | /// 37 | public static void Dispose() { _instance = null; } 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Singleton/SingletonPropertyClass.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace IFramework.Singleton 4 | { 5 | /// 6 | /// 单例属性类 7 | /// 8 | /// 9 | public abstract class SingletonPropertyClass : Unit, ISingleton where T : SingletonPropertyClass 10 | { 11 | /// 12 | /// 实例 13 | /// 14 | protected static T instance { get { return SingletonProperty.instance; } } 15 | /// 16 | /// ctor 17 | /// 18 | protected SingletonPropertyClass() { } 19 | /// 20 | /// 初始化 21 | /// 22 | protected abstract void OnSingletonInit(); 23 | 24 | /// 25 | /// 注销 26 | /// 27 | public override void Dispose() 28 | { 29 | base.Dispose(); 30 | if (!disposed) 31 | { 32 | SingletonProperty.Dispose(); 33 | } 34 | } 35 | 36 | 37 | 38 | void ISingleton.OnSingletonInit() 39 | { 40 | OnSingletonInit(); 41 | } 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /Framework/IFramework/IFramework/Unit.cs: -------------------------------------------------------------------------------- 1 | namespace IFramework 2 | { 3 | /// 4 | /// 基类 5 | /// 6 | public abstract class Unit 7 | { 8 | private bool _disposed; 9 | /// 10 | /// 是否已经释放 11 | /// 12 | public bool disposed { get { return _disposed; } } 13 | 14 | /// 15 | /// 释放时 16 | /// 17 | protected abstract void OnDispose(); 18 | /// 19 | /// 释放 20 | /// 21 | public virtual void Dispose() 22 | { 23 | if (_disposed) return; 24 | OnDispose(); 25 | _disposed = true; 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Framework/IFramework/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // 有关程序集的一般信息由以下 6 | // 控制。更改这些特性值可修改 7 | // 与程序集关联的信息。 8 | [assembly: AssemblyTitle("IFramework")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("IFramework")] 13 | [assembly: AssemblyCopyright("Copyright 2021")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // 将 ComVisible 设置为 false 会使此程序集中的类型 18 | //对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 19 | //请将此类型的 ComVisible 特性设置为 true。 20 | [assembly: ComVisible(false)] 21 | 22 | // 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID 23 | [assembly: Guid("a26ffa53-1c22-4871-93b2-ade332beb038")] 24 | 25 | // 程序集的版本信息由下列四个值组成: 26 | // 27 | // 主版本 28 | // 次版本 29 | // 生成号 30 | // 修订号 31 | // 32 | // 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号 33 | // 方法是按如下所示使用“*”: : 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | 38 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | # IFramework [Doc](https://onclick9927.github.io/2023/07/24/Doc/IFramework/1-IFramework-%E7%AE%80%E4%BB%8B/) 4 | 5 | Interface | Inject | Interesting | Increase C# Framework 6 | 7 | ``` csharp 8 | while(true) 9 | Console.Write("Thanks For EveryOne Who Used It Once!") 10 | ``` 11 | 12 | [![Stargazers over time](https://starchart.cc/OnClick9927/IFramework.svg?variant=adaptive)](https://starchart.cc/OnClick9927/IFramework) 13 | 14 | 15 | # FAQ 16 | 17 | ## 如何在C#项目中使用IFramework? 18 | 从[Release](https://github.com/OnClick9927/IFramework/releases)下载或者自行编译IFramework,将文件放入项目中引用即可 19 | 20 | 若在Unity中使用此框架请查看这个仓库:[IFramework-Unity](https://github.com/OnClick9927/IFramework-Unity) 21 | 22 | ## 如何在不同的C#环境下使用IFramework? 23 | 将IFramework项目源码编译之后,从`IFramework/Plugins`文件中获取编译的文件。 24 | 25 | ## 如何获取更多帮助? 26 | 遇到问题或者有更好的想法,欢迎加入QQ讨论群 [加群链接](https://jq.qq.com/?_wv=1027&k=sbKbmsTY) 27 | -------------------------------------------------------------------------------- /制作src分支.bat: -------------------------------------------------------------------------------- 1 | git subtree split --prefix=Framework/IFramework/IFramework --branch src 2 | git push origin src:src -------------------------------------------------------------------------------- /清除记录.bat: -------------------------------------------------------------------------------- 1 | chcp 65001 2 | @echo off 3 | git checkout --orphan latest_branch 4 | git add -A 5 | git commit -am 清除提交记录 6 | git branch -D master 7 | git branch -m master 8 | git push -f origin master 9 | git pull 10 | echo "已清除全部的历史记录!" 11 | echo "查看新仓库信息:" 12 | git log --pretty=oneline 13 | git branch -a 14 | git tag 15 | git ls-remote --tags 16 | pause 17 | popd 18 | exit --------------------------------------------------------------------------------