Compare commits

..

145 Commits

Author SHA1 Message Date
ALEXTANG
d6dcd8851c 更新文档
更新文档
2023-08-18 17:49:28 +08:00
ALEXTANG
bbea9c4cee Update UIExtension.cs 2023-08-18 17:10:41 +08:00
ALEXTANG
b1ccb1fd53 Update Utility.Http.cs 2023-08-18 16:54:17 +08:00
ALEXTANG
0f2ad3c71d [+] UI循环列表拓展Grid 用法和List一样
[+] UI循环列表拓展Grid 用法和List一样
2023-08-18 16:32:08 +08:00
ALEXTANG
0e6851e691 Remove unused ogg
Remove unused ogg
2023-08-18 15:24:33 +08:00
ALEXTANG
75b9956261 流程初始音效设置开启 2023-08-18 14:33:49 +08:00
ALEXTANG
401c397fc8 流程初始音效设置开启
流程初始音效设置开启
2023-08-18 14:25:10 +08:00
ALEXTANG
94b314e91f TEngine设置面板增加从Hybrid同步AOT与热更程序集的按钮
TEngine设置面板增加从Hybrid同步AOT与热更程序集的按钮
2023-08-18 13:06:08 +08:00
ALEXTANG
a6573d9336 Update ConfigLoader.cs 2023-08-18 13:05:02 +08:00
ALEXTANG
abf5357f49 给不支持WEBGL的增加宏定义
给不支持WEBGL的增加宏定义
2023-08-18 13:04:53 +08:00
ALEXTANG
aab353cca3 Remove Unused Codes.
Remove Unused Codes.
2023-08-17 23:11:11 +08:00
ALEXTANG
e13071c4db 移除调试luban残留的日志
移除调试luban残留的日志
2023-08-17 23:10:14 +08:00
ALEXTANG
e071c20214 拓展自定义鲁班加载配置,支持懒加载 默认使用懒加载,未使用的配置不会载入内存
拓展自定义鲁班加载配置,支持懒加载 默认使用懒加载,未使用的配置不会载入内存
2023-08-17 23:07:07 +08:00
ALEXTANG
bf73ce333b 拓展自定义鲁班加载配置,支持UniTask异步 默认使用同步加载
拓展自定义鲁班加载配置,支持UniTask异步 默认使用同步加载
2023-08-17 22:00:55 +08:00
ALEXTANG
d7a60002d4 ErrorLog使用异步显示UI
ErrorLog使用异步显示UI
2023-08-17 21:41:30 +08:00
ALEXTANG
61f657322d 增加WebGLUpdateDataUrl
增加WebGLUpdateDataUrl
2023-08-17 21:41:07 +08:00
ALEXTANG
33223dc02e 处理DotNet网络层对WebGL导出的兼容 2023-08-17 21:38:21 +08:00
ALEXTANG
0661c59877 处理DotNet网络层对WebGL导出的兼容
处理DotNet网络层对WebGL导出的兼容
2023-08-17 14:06:09 +08:00
ALEXTANG
7c74e10857 提供单独销毁UIWidget的接口。增加UIWidget异步创建接口。
提供单独销毁UIWidget的接口。增加UIWidget异步创建接口。
2023-08-17 10:59:37 +08:00
ALEXTANG
db935bfb5f DotNet 加入热更域,修正GameProto的依赖
DotNet 加入热更域,修正GameProto的依赖
2023-08-16 15:31:16 +08:00
ALEXTANG
dc6b7bb21e 增加使用UniTask的常用资源加载接口
增加使用UniTask的常用资源加载接口
2023-08-15 17:24:02 +08:00
ALEXTANG
d32f3cb768 增加使用UniTask的常用资源加载接口
增加使用UniTask的常用资源加载接口
2023-08-15 17:22:05 +08:00
ALEXTANG
a32ab30444 修改luban模板配置,支持异步加载配置实例,客户端使用UniTask异步加载配置,避免WebGL不支持原生Task
修改luban模板配置,支持异步加载配置实例,客户端使用UniTask异步加载配置,避免WebGL不支持原生Task
2023-08-15 12:54:15 +08:00
ALEXTANG
aa0d40c758 修改luban模板配置,支持异步加载配置实例
修改luban模板配置,支持异步加载配置实例
2023-08-15 12:07:21 +08:00
ALEXTANGXIAO
5239b89326 DotNet 加入热更域
DotNet 加入热更域
2023-08-12 23:39:21 +08:00
ALEXTANG
53f0ecb6c1 [+] UI循环列表拓展与示例
[+] UI循环列表拓展与示例
2023-08-10 23:25:43 +08:00
ALEXTANG
ea9447d0ea Update ClientGm.cs 2023-08-10 10:10:50 +08:00
ALEXTANG
0c6d59f53e Fixed BehaviourSingleton #45
Fixed BehaviourSingleton #45
2023-08-10 10:09:59 +08:00
ALEXTANG
9986e22d18 [-] Remove Old ProtoGenerate
[-] Remove Old Proto Generate
2023-08-09 14:22:39 +08:00
ALEXTANG
7deb049769 更新Entity接口 泛型获取父级、Define增加unit[]类型
更新Entity接口 泛型获取父级、Define增加unit[]类型
2023-08-09 11:09:38 +08:00
ALEXTANG
fce8bd4d60 Update DebuggerModule.SettingsWindow.cs 2023-08-08 19:54:37 +08:00
ALEXTANG
7ec14c670f Luban配置表拓展分组-分组为c和client的为仅客户端字段s和server为仅服务器字段
Luban配置表拓展分组-分组为c和client的为仅客户端字段s和server为仅服务器字段
2023-08-07 15:14:43 +08:00
ALEXTANGXIAO
efe3d2b25b Update README
Update README
2023-08-06 00:36:29 +08:00
ALEXTANGXIAO
2107282368 Update Readme
Update Readme
2023-08-05 00:54:25 +08:00
ALEXTANG
fb26ea2297 Update AddressableManageComponent.cs 2023-08-04 01:42:26 +08:00
ALEXTANG
9ac150425f Update IntDictionaryConfig.cs 2023-08-04 01:42:00 +08:00
ALEXTANG
36d2c146b0 1、修复了MongoDB在2.18.0以后需要自定义注册ObjectSerializer的问题。 2、Addressable的AddAddressable接口增加isLock参数、用来决定是否需要添加携程锁。 3、修复了APackInfo因为网络多线程的原因导致线程安全的问题。
1、修复了MongoDB在2.18.0以后需要自定义注册ObjectSerializer的问题。
2、Addressable的AddAddressable接口增加isLock参数、用来决定是否需要添加携程锁。
3、修复了APackInfo因为网络多线程的原因导致线程安全的问题。
2023-08-04 01:41:31 +08:00
ALEXTANG
774b73bbbf 注释防裁剪脚本中相机权限的引用
注释防裁剪脚本中相机权限的引用
2023-08-01 16:57:43 +08:00
ALEXTANG
32366eb127 移除防裁剪脚本中相机权限的引用
移除防裁剪脚本中相机权限的引用
2023-08-01 16:54:49 +08:00
ALEXTANG
a843617e5f Update README.md 2023-08-01 10:44:15 +08:00
ALEXTANGXIAO
46b139f7cf 补充IOS运行快照到Books下
补充IOS运行快照到Books下
2023-08-01 00:06:07 +08:00
ALEXTANG
e6ff1dec3f Update BuglyAgent.cs 2023-07-31 20:36:44 +08:00
ALEXTANG
4429732010 Update ConfigLoader.cs 2023-07-31 15:01:11 +08:00
ALEXTANGXIAO
60a5caebae 1、Scene.Create接口增加了一个泛型方法接口。 2、SceneConfig增加了SceneTypeConfig和SceneSubType
1、Scene.Create接口增加了一个泛型方法接口。 2、SceneConfig增加了SceneTypeConfig和SceneSubType
2023-07-29 01:03:35 +08:00
ALEXTANGXIAO
f7c95d8216 服务器启动防呆提示需要转表
服务器启动防呆提示需要转表
2023-07-29 00:12:52 +08:00
ALEXTANGXIAO
98dcb80942 更新编辑器MenuItem导表与导出协议
更新编辑器MenuItem导表与导出协议
2023-07-29 00:03:54 +08:00
ALEXTANG
30192d52cf 调整网络demo场景路径
调整网络demo场景路径
2023-07-27 10:18:02 +08:00
ALEXTANGXIAO
b7b2262d53 合理化服务器命名,使开发者更容易理解。
Scene的routeId更名为locationId
2023-07-27 00:33:39 +08:00
ALEXTANG
d1c93f15d6 Application.OpenURL 调用bat的cmd运行时问题
Application.OpenURL 调用bat的cmd运行时问题
2023-07-26 15:39:20 +08:00
ALEXTANGXIAO
1ac1ff7d56 [+] Update HybridCLR v3.4.0
[+] Update HybridCLR v3.4.0
2023-07-26 00:07:47 +08:00
ALEXTANG
863788f303 Update UIWidget.cs 2023-07-25 23:37:02 +08:00
ALEXTANG
74790c7486 Update UIWindow.cs 2023-07-25 23:34:42 +08:00
ALEXTANG
1c223c8ad0 [+] DOTweenExtension
[+] DOTweenExtension
2023-07-25 17:08:57 +08:00
ALEXTANG
e75b3a4e66 [+] UniTask External YooAsset、Dotween、TextMeshPro 2023-07-25 16:28:09 +08:00
ALEXTANG
91b0995911 [+] UniTask External YooAsset、Dotween、TextMeshPro
[+] UniTask External YooAsset、Dotween、TextMeshPro
2023-07-25 16:22:13 +08:00
ALEXTANG
14c886ea8f [+] UniTask External YooAsset、Dotween、TextMeshPro
[+] UniTask External YooAsset、Dotween、TextMeshPro
2023-07-25 16:20:26 +08:00
ALEXTANG
9babc0ba85 [+] 合理化目录结构 精简非必要插件如确定性物理库以及Recast插件。
[+] 合理化目录结构 精简非必要插件如确定性物理库以及Recast插件。
2023-07-25 14:34:05 +08:00
ALEXTANG
f8056aef32 调整服务器逻辑
1、调整Entity.AddComponent方法添加的组件Id为父组件的Id。
2、增加了ISupportedSingleCollection和ISingleCollectionRoot接口.
3、增加了SingleCollection、用于把子组件保存到单独的数据库表的功能,配合ISupportedSingleCollection和ISingleCollectionRoot。
2023-07-24 16:49:23 +08:00
ALEXTANG
846dc4d4bc Fixed the session dispose
Fixed the session dispose
2023-07-24 13:17:59 +08:00
ALEXTANG
03ab7fb353 Create start_export.bat 2023-07-23 22:18:47 +08:00
ALEXTANG
3c11980e7c Create start_develop.bat 2023-07-23 22:18:45 +08:00
ALEXTANG
c23aa0bd71 Revert "Update Core.csproj"
This reverts commit df76d0b77a.
2023-07-23 21:32:22 +08:00
ALEXTANG
df76d0b77a Update Core.csproj 2023-07-23 21:32:01 +08:00
ALEXTANG
35d2012546 1.替换了KCP为C#版本 2.KCP增加了Memery<T>支持。 3.框架增加了对Memery<T>的支持,服务器网络消息采用Memery<T>,性能得大幅度的提升 4.优化了消息调度执行逻辑
1.替换了KCP为C#版本
2.KCP增加了Memery<T>支持。
3.框架增加了对Memery<T>的支持,服务器网络消息采用Memery<T>,性能得大幅度的提升
4.优化了消息调度执行逻辑
2023-07-23 00:25:47 +08:00
ALEXTANG
c96d20a89a [+] GameFrameworkModuleSystem
[+] GameFrameworkModuleSystem
2023-07-22 17:53:00 +08:00
ALEXTANG
5787d0f9dc [+] GameModuleSystem
[+] GameModuleSystem
2023-07-22 17:51:24 +08:00
ALEXTANG
8b35c8ca07 Update ProjectSettings.asset 2023-07-22 17:50:06 +08:00
ALEXTANG
e5456da482 合理化Address协议逻辑
合理化Address协议逻辑
2023-07-21 17:50:47 +08:00
ALEXTANG
144ba9f222 完善AddressableManageComponent与ClientNetworkComponent
完善AddressableManageComponent与ClientNetworkComponent
2023-07-21 14:32:20 +08:00
ALEXTANG
889fbdc8e1 Update UIGaussianBlurLayer.cs 2023-07-19 17:34:02 +08:00
ALEXTANG
0d177e6868 Update AssetGroup.cs 2023-07-19 17:33:56 +08:00
ALEXTANG
29135228be Update AssetReference.cs 2023-07-19 17:33:52 +08:00
ALEXTANG
c1a1de73cd Update ResourceManager.cs 2023-07-19 17:33:48 +08:00
ALEXTANG
612e9b7eba Update ResourceModule.cs 2023-07-19 17:33:44 +08:00
ALEXTANG
5ed6b8c378 Update UIModule.cs 2023-07-19 17:33:41 +08:00
ALEXTANG
06dad5a68a Update UIWidget.cs 2023-07-19 17:33:28 +08:00
ALEXTANG
9e0462043c Update WindowAttribute.cs 2023-07-19 17:33:25 +08:00
ALEXTANG
c1178e284b Update DUnityUtil.cs 2023-07-19 17:33:21 +08:00
ALEXTANG
8e3dd138a6 Merge pull request #44 from ALEXTANGXIAO/3.0.0-network
TEngine-3.0.0 NetWork And Server
2023-07-19 16:32:03 +08:00
ALEXTANG
405253c507 Update BuildAssetsCommand.cs 2023-07-19 16:03:02 +08:00
ALEXTANG
66ef50a9e0 WebGL适配 2023-07-19 15:51:16 +08:00
ALEXTANG
413f4dcda7 Update GameClient.cs 2023-07-19 15:23:46 +08:00
ALEXTANG
75fdb4d7de Update GameClient.cs 2023-07-19 15:20:05 +08:00
ALEXTANG
1ada1e4e33 Update H_C2G_LoginAddressRequestHandler.cs 2023-07-19 15:20:01 +08:00
ALEXTANG
3848b6aaaa Create AddressableSceneHelper.cs 2023-07-19 15:19:57 +08:00
ALEXTANG
2b33b405c0 Update SceneConfig.cs 2023-07-19 15:19:44 +08:00
ALEXTANG
6766b930d7 更新机器内网配置
更新机器内网配置
2023-07-19 13:08:03 +08:00
ALEXTANG
526baf45fa 完善网络框架,增加服务器断开连接回调 2023-07-18 17:36:48 +08:00
ALEXTANG
a473971cfb Update H_C2G_LoginAddressRequestHandler.cs 2023-07-18 13:06:00 +08:00
ALEXTANG
51effd2c97 Update TCPClientNetwork.cs 2023-07-18 13:05:57 +08:00
ALEXTANG
4205220b64 Network Example2
Network Example2
2023-07-18 13:05:48 +08:00
ALEXTANG
e95c18ca67 Update README.md
Update README.md
2023-07-18 10:08:46 +08:00
ALEXTANG
f5571716a2 Update SkillDisplayData.cs 2023-07-17 20:59:13 +08:00
ALEXTANG
d61a8dfa4b GMPanel
GMPanel
2023-07-17 20:59:04 +08:00
ALEXTANG
068ec709d0 [+] Add GamePlayScene
[+] Add GamePlayScene
2023-07-17 19:00:24 +08:00
ALEXTANG
8b26b790ee GameServerScene
GameServerScene
2023-07-17 18:38:50 +08:00
ALEXTANG
da35b4306b EnumHelper
EnumHelper
2023-07-17 18:29:41 +08:00
ALEXTANG
ee2147e3d9 Update BattleDemo
Update BattleDemo
2023-07-17 18:09:07 +08:00
ALEXTANG
a273e9d5f8 Entitas
Entitas
2023-07-17 15:28:27 +08:00
ALEXTANG
31d4d6c0b8 Update
Update
2023-07-17 01:05:12 +08:00
ALEXTANG
6a6f0591f1 完善Entity状态
完善Entity状态
2023-07-17 01:00:33 +08:00
ALEXTANG
9cd0bac81e 完善登录注册Demo
完善登录注册Demo
2023-07-17 01:00:10 +08:00
ALEXTANG
4abe10eecf IDataBase Last
IDataBase Last
2023-07-16 19:06:45 +08:00
ALEXTANG
bd10297dfa [+] HybridCLR=》v3.3.0
[+] HybridCLR=》v3.3.0
2023-07-16 15:51:44 +08:00
ALEXTANG
7ce72b8aa8 合理化框架
合理化框架
2023-07-16 15:37:38 +08:00
ALEXTANG
c6ecb48944 注册登录Demo
注册登录Demo
2023-07-16 00:57:20 +08:00
ALEXTANG
c178f8bb75 配置目录同步程序集调整
配置目录同步程序集调整
2023-07-15 18:45:38 +08:00
ALEXTANG
d4f160d284 合理化程序集,服务器Logic无需共享的代码存放解决方案目录,无需走Unity目录
合理化程序集,服务器Logic无需共享的代码存放解决方案目录,无需走Unity目录
2023-07-15 18:39:42 +08:00
ALEXTANG
d87653a9fb 调整对于Linux打包的支持
调整对于Linux打包的支持
2023-07-15 00:58:40 +08:00
ALEXTANG
04a5a0e048 Update Demo
Update Demo
2023-07-14 14:23:51 +08:00
ALEXTANG
c825f4d920 Streamline Unity Engineering
Streamline Unity Engineering
2023-07-14 13:38:05 +08:00
ALEXTANG
b3e59b92d6 Update ThirdParty.csproj 2023-07-14 12:47:52 +08:00
ALEXTANG
37af620d71 Update RootNameSpace
Update RootNameSpace
2023-07-14 12:45:06 +08:00
ALEXTANG
493172a925 Update
Update
2023-07-13 23:54:38 +08:00
ALEXTANG
1382db8c61 Update
Update
2023-07-13 21:13:29 +08:00
ALEXTANG
72b7149aa1 Update
Update
2023-07-13 21:02:29 +08:00
ALEXTANG
0c51ae7bdd Update
Update
2023-07-13 19:40:12 +08:00
ALEXTANG
79dc302f14 Update Utility.Http.cs 2023-07-13 19:34:30 +08:00
ALEXTANG
95e4dd93a8 Update
Update
2023-07-13 19:05:40 +08:00
ALEXTANG
2b310c00f2 Update ThirdParty.csproj 2023-07-13 18:22:01 +08:00
ALEXTANG
fbb26ea9ea Update
Update
2023-07-13 18:20:39 +08:00
ALEXTANG
bff8f20af3 Refresh Plugins 2023-07-13 17:41:23 +08:00
ALEXTANG
0c7ce5f28c Refresh Plugins 2023-07-13 17:37:44 +08:00
ALEXTANG
31fc9abdf9 Update OdinModuleConfig.asset 2023-07-13 17:36:37 +08:00
ALEXTANG
a087f40e8a [-] Remove Cysharp.MemoryPack
[-] Remove Cysharp.MemoryPack
2023-07-13 17:31:53 +08:00
ALEXTANG
7b2fc7ce9f Update DotNet sln
Update DotNet sln
2023-07-13 17:27:57 +08:00
ALEXTANG
dc4bb8cc9c Unity低版本语法兼容
Unity低版本语法兼容
2023-07-13 17:24:37 +08:00
ALEXTANG
0c8f3a5f92 [+] TEngineServer
[+] TEngineServer
2023-07-13 17:17:26 +08:00
ALEXTANG
a69f53592e 移除 重构
移除 重构
2023-07-13 12:46:26 +08:00
ALEXTANG
8757e1f550 [+] add et8 csproject
[+] add et8 csproject
2023-07-13 12:26:12 +08:00
ALEXTANG
336d4b2eb9 [+] 接入ET8服务端
[+] 接入ET8服务端
2023-07-13 12:23:48 +08:00
ALEXTANG
e0be062006 Remove old networkmodule 2023-07-12 17:36:29 +08:00
ALEXTANG
5e69129667 Remove old networkmodule
Remove old networkmodule
2023-07-12 17:35:05 +08:00
ALEXTANG
4c39ab79ca [+] Books 2023-07-12 17:28:08 +08:00
ALEXTANG
a710d1cb76 [+] Books 2023-07-10 13:59:56 +08:00
ALEXTANG
8472839394 [+] Books 2023-07-10 13:51:32 +08:00
ALEXTANG
4988c99d31 [+] Books
[+] Books
2023-07-10 12:51:13 +08:00
ALEXTANG
4fa62b2d79 Update README.md 2023-07-10 12:35:48 +08:00
ALEXTANG
f549514d7c Update ResourceManager.cs 2023-07-09 00:07:32 +08:00
ALEXTANG
f588038848 Update UIButtonScale.cs 2023-07-06 19:17:16 +08:00
ALEXTANG
9e0e3ebd50 Fixed the bug of CreateWidgetByPath
Fixed the bug of CreateWidgetByPath
2023-07-06 19:07:45 +08:00
ALEXTANG
b07ef836a9 Update ProcedureLoadAssembly.cs 2023-07-05 15:20:18 +08:00
ALEXTANG
7a1d593195 Update UnityExtension.cs 2023-07-05 15:20:12 +08:00
ALEXTANG
bc113c5c6e [+] Update版本更新时CDN地址也可更新
[+] Update版本更新时CDN地址也可更新
2023-07-04 20:57:26 +08:00
ALEXTANG
753bbdfb82 [+] HybridCLR IOS相机权限问题
大坑 没有任何异常、警告、日志 如果不写这个,就是没有响应
2023-06-29 11:25:33 +08:00
1319 changed files with 83257 additions and 16246 deletions

13
.gitignore vendored
View File

@@ -116,4 +116,17 @@ UserSettings/Layouts/default-2021.dwlt
Assets/UnityOnlineServiceData.meta
Assets/UnityOnlineServiceData
#MAC
.DS_Store
#Server_sln
DotNet/.idea/
DotNet/App/obj/
DotNet/Core/obj/
DotNet/Logic/obj/
DotNet/ThirdParty/obj/
Bin/
#Server_Config
DotNet/Config/GameConfig

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 22d1bcdd317c6f64daeb9bf9742495f2
guid: 6a824de3af698c34bb4343dbb911498b
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: afddbfe87a53e9049bf031ee18842f87
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 2287aec89d99c9d47bc787f282793669
guid: 8f3bcefaf67e76141a6d8edeb8354fea
TextScriptImporter:
externalObjects: {}
userData:

Binary file not shown.

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: d08838ffea10da9408f3fbc06d944a56
guid: 25113973d38eb5a48b064863830539a4
TextScriptImporter:
externalObjects: {}
userData:

Binary file not shown.

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 3b7641a560428bd45aa6bbd10d8bb4ab
guid: ef51ed54e72f97a4ab530b932680e08b
TextScriptImporter:
externalObjects: {}
userData:

Binary file not shown.

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 090163c612f34ac4fb80004ac5f057b4
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 930f8bca659c7504b8fe431ee8c40e7b
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: ed92f6b6b24543747bc10eba9141feb1
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: dbb608c870018e94d9603500e9cb4d5e
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: 6dff8f4f5dd94df49bf6d616fb11cc9c
guid: fdae3569c5cbfe54e97778a1f7e7af21
folderAsset: yes
DefaultImporter:
externalObjects: {}

View File

@@ -0,0 +1,117 @@
#if TENGINE_NET
using CommandLine;
using TEngine.Core;
using NLog;
namespace TEngine
{
public static class App
{
public static void Init()
{
try
{
// 设置默认的线程的同步上下文
SynchronizationContext.SetSynchronizationContext(ThreadSynchronizationContext.Main);
// 解析命令行参数
Parser.Default.ParseArguments<CommandLineOptions>(Environment.GetCommandLineArgs())
.WithNotParsed(error => throw new Exception("Command line format error!"))
.WithParsed(option => AppDefine.Options = option);
// 加载框架配置
TEngineSettingsHelper.Initialize();
// 检查启动参数
switch (AppDefine.Options.AppType)
{
case AppType.Game:
{
break;
}
case AppType.Export:
{
new Exporter().Start();
return;
}
default:
{
throw new NotSupportedException($"AppType is {AppDefine.Options.AppType} Unrecognized!");
}
}
// 根据不同的运行模式来选择日志的方式
switch (AppDefine.Options.Mode)
{
case Mode.Develop:
{
LogManager.Configuration.RemoveRuleByName("ConsoleTrace");
LogManager.Configuration.RemoveRuleByName("ConsoleDebug");
LogManager.Configuration.RemoveRuleByName("ConsoleInfo");
LogManager.Configuration.RemoveRuleByName("ConsoleWarn");
LogManager.Configuration.RemoveRuleByName("ConsoleError");
LogManager.Configuration.RemoveRuleByName("ServerDebug");
LogManager.Configuration.RemoveRuleByName("ServerTrace");
LogManager.Configuration.RemoveRuleByName("ServerInfo");
LogManager.Configuration.RemoveRuleByName("ServerWarn");
LogManager.Configuration.RemoveRuleByName("ServerError");
break;
}
case Mode.Release:
{
LogManager.Configuration.RemoveRuleByName("ConsoleTrace");
LogManager.Configuration.RemoveRuleByName("ConsoleDebug");
LogManager.Configuration.RemoveRuleByName("ConsoleInfo");
LogManager.Configuration.RemoveRuleByName("ConsoleWarn");
LogManager.Configuration.RemoveRuleByName("ConsoleError");
break;
}
}
// 初始化SingletonSystemCenter这个一定要放到最前面
// 因为SingletonSystem会注册AssemblyManager的OnLoadAssemblyEvent和OnUnLoadAssemblyEvent的事件
// 如果不这样、会无法把程序集的单例注册到SingletonManager中
SingletonSystem.Initialize();
// 加载核心程序集
AssemblyManager.Initialize();
Log.Info($"Start Server Param => {Parser.Default.FormatCommandLine(AppDefine.Options)}");
}
catch (Exception exception)
{
Log.Error(exception);
}
}
public static async FTask Start()
{
switch (AppDefine.Options.Mode)
{
case Mode.Develop:
{
// 开发模式默认所有Server都在一个进程中、方便调试、但网络还都是独立的
var serverConfigInfos = ConfigTableManage.AllServerConfig();
foreach (var serverConfig in serverConfigInfos)
{
await Server.Create(serverConfig.Id);
}
return;
}
case Mode.Release:
{
// 发布模式只会启动启动参数传递的Server、也就是只会启动一个Server
// 您可以做一个Server专门用于管理启动所有Server的工作
await Server.Create(AppDefine.Options.AppId);
return;
}
}
}
public static void Close()
{
SingletonSystem.Dispose();
AssemblyManager.Dispose();
}
}
}
#endif

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: fcc0172c723e55a4389ad2f62b68a3fe
guid: f6b4f60ee2b59b649835c9b25028d9e4
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,11 @@
#if TENGINE_NET
#pragma warning disable CS8618
namespace TEngine
{
public static class AppDefine
{
public static CommandLineOptions Options;
public static uint AppId => Options.AppId;
}
}
#endif

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: e3c2f9de3c34ffc409549b86ac3fc530
guid: e08a4791989b16843a924bf798cdaa34
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9d838cbc15b0f034bb83f3aa5ea5b72d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TEngine.DataStructure;
namespace TEngine.Core
{
public sealed class AssemblyInfo
{
public Assembly Assembly { get; private set; }
public readonly List<Type> AssemblyTypeList = new List<Type>();
public readonly OneToManyList<Type, Type> AssemblyTypeGroupList = new OneToManyList<Type, Type>();
public void Load(Assembly assembly)
{
Assembly = assembly;
var assemblyTypes = assembly.GetTypes().ToList();
foreach (var type in assemblyTypes)
{
if (type.IsAbstract || type.IsInterface)
{
continue;
}
var interfaces = type.GetInterfaces();
foreach (var interfaceType in interfaces)
{
AssemblyTypeGroupList.Add(interfaceType, type);
}
}
AssemblyTypeList.AddRange(assemblyTypes);
}
public void Unload()
{
AssemblyTypeList.Clear();
AssemblyTypeGroupList.Clear();
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: b0ca98e42147b3948bc8d30f24e9c366
guid: aee73bf0d744afd439ef9b6a5d531951
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,172 @@
using System;
using System.Collections.Generic;
using System.Reflection;
#if TENGINE_NET
using System.Runtime.Loader;
// ReSharper disable ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
#endif
#pragma warning disable CS8603
#pragma warning disable CS8618
namespace TEngine.Core
{
public static class AssemblyManager
{
public static event Action<int> OnLoadAssemblyEvent;
public static event Action<int> OnUnLoadAssemblyEvent;
public static event Action<int> OnReLoadAssemblyEvent;
private static readonly Dictionary<int, AssemblyInfo> AssemblyList = new Dictionary<int, AssemblyInfo>();
public static void Initialize()
{
LoadAssembly(int.MaxValue, typeof(AssemblyManager).Assembly);
}
public static void LoadAssembly(int assemblyName, Assembly assembly)
{
var isReLoad = false;
if (!AssemblyList.TryGetValue(assemblyName, out var assemblyInfo))
{
assemblyInfo = new AssemblyInfo();
AssemblyList.Add(assemblyName, assemblyInfo);
}
else
{
isReLoad = true;
assemblyInfo.Unload();
if (OnUnLoadAssemblyEvent != null)
{
OnUnLoadAssemblyEvent(assemblyName);
}
}
assemblyInfo.Load(assembly);
if (OnLoadAssemblyEvent != null)
{
OnLoadAssemblyEvent(assemblyName);
}
if (isReLoad && OnReLoadAssemblyEvent != null)
{
OnReLoadAssemblyEvent(assemblyName);
}
}
public static void Load(int assemblyName, Assembly assembly)
{
if (int.MaxValue == assemblyName)
{
throw new NotSupportedException("AssemblyName cannot be 2147483647");
}
LoadAssembly(assemblyName, assembly);
}
public static IEnumerable<int> ForEachAssemblyName()
{
foreach (var (key, _) in AssemblyList)
{
yield return key;
}
}
public static IEnumerable<Type> ForEach()
{
foreach (var (_, assemblyInfo) in AssemblyList)
{
foreach (var type in assemblyInfo.AssemblyTypeList)
{
yield return type;
}
}
}
public static IEnumerable<Type> ForEach(int assemblyName)
{
if (!AssemblyList.TryGetValue(assemblyName, out var assemblyInfo))
{
yield break;
}
foreach (var type in assemblyInfo.AssemblyTypeList)
{
yield return type;
}
}
public static IEnumerable<Type> ForEach(Type findType)
{
foreach (var (_, assemblyInfo) in AssemblyList)
{
if (!assemblyInfo.AssemblyTypeGroupList.TryGetValue(findType, out var assemblyLoad))
{
yield break;
}
foreach (var type in assemblyLoad)
{
yield return type;
}
}
}
public static IEnumerable<Type> ForEach(int assemblyName, Type findType)
{
if (!AssemblyList.TryGetValue(assemblyName, out var assemblyInfo))
{
yield break;
}
if (!assemblyInfo.AssemblyTypeGroupList.TryGetValue(findType, out var assemblyLoad))
{
yield break;
}
foreach (var type in assemblyLoad)
{
yield return type;
}
}
public static Assembly GetAssembly(int assemblyName)
{
return !AssemblyList.TryGetValue(assemblyName, out var assemblyInfo) ? null : assemblyInfo.Assembly;
}
public static void Dispose()
{
foreach (var (_, assemblyInfo) in AssemblyList)
{
assemblyInfo.Unload();
}
AssemblyList.Clear();
if (OnLoadAssemblyEvent != null)
{
foreach (var @delegate in OnLoadAssemblyEvent.GetInvocationList())
{
OnLoadAssemblyEvent -= @delegate as Action<int>;
}
}
if (OnUnLoadAssemblyEvent != null)
{
foreach (var @delegate in OnUnLoadAssemblyEvent.GetInvocationList())
{
OnUnLoadAssemblyEvent -= @delegate as Action<int>;
}
}
if (OnReLoadAssemblyEvent != null)
{
foreach (var @delegate in OnReLoadAssemblyEvent.GetInvocationList())
{
OnReLoadAssemblyEvent -= @delegate as Action<int>;
}
}
}
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: a9259bb6b548e46459cc766d8fe2faeb
guid: 8d7a578f700ea034a9b98a5f63491eb3
MonoImporter:
externalObjects: {}
serializedVersion: 2

View File

@@ -0,0 +1,67 @@
#if TENGINE_NET
using CommandLine;
using TEngine.Core;
#pragma warning disable CS8618
namespace TEngine;
public enum AppType
{
Game,
Export,
/// <summary>
/// 每台物理机一个守护进程,用来启动该物理机上的所有进程。
/// </summary>
Watcher,
}
public enum Mode
{
/// <summary>
/// Develop:所有Server都在一个进程中,Release:每个Server都在独立的进程中。
/// </summary>
Release,
Develop,
}
public class CommandLineOptions
{
/// <summary>
/// 进程Id
/// </summary>
[Option("AppId", Required = false, Default = (uint)0, HelpText = "Enter an AppId such as 1")]
public uint AppId { get; set; }
/// <summary>
/// App类型
/// Game - 游戏服务器App
/// Export - 导表App
/// </summary>
[Option("AppType", Required = false, Default = AppType.Game, HelpText = "AppType enum")]
public AppType AppType { get; set; }
/// <summary>
/// 服务器运行模式
/// Develop - 开发模式所有Server都在一个进程中
/// Release - 发布模式每个Server都在独立的进程中
/// </summary>
[Option("Mode", Required = false, Default = Mode.Develop, HelpText = "Mode enum")]
public Mode Mode { get; set; }
[Option("LogLevel", Required = false, Default = 2)]
public int LogLevel { get; set; }
#if TENGINE_NET
/// <summary>
/// 导表的类型
/// </summary>
[Option("ExcelExportType", Required = false, Default = ExportType.None, HelpText = "Increment,All")]
public ExportType ExportType { get; set; }
#endif
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c644dc25b4098d24683e949b5a14f754
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
namespace TEngine.Core
{
public class CoreErrorCode
{
public const uint ErrRpcFail = 100000002; // Rpc消息发送失败
public const uint ErrNotFoundRoute = 100000003; // 没有找到Route消息
public const uint ErrRouteTimeout = 100000004; // 发送Route消息超时
public const uint Error_NotFindEntity = 100000008; // 没有找到Entity
public const uint Error_CopyTimeout = 100000009; // CopyTimeout不能小于或等于0
public const uint Error_Transfer = 100000010;// 传送发生了错误
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d0b2fb99ac2580c418659e6a1328206b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 21519f62191bd1447bfafd815de9046d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,61 @@
using System;
using System.Collections.Generic;
namespace TEngine.Core
{
public sealed class CoroutineLockQueue : IDisposable
{
public long Key { get; private set; }
public CoroutineLockQueueType CoroutineLockQueueType { get; private set; }
private readonly Queue<WaitCoroutineLock> _waitCoroutineLocks = new Queue<WaitCoroutineLock>();
public static CoroutineLockQueue Create(long key, int time, CoroutineLockQueueType coroutineLockQueueType)
{
var coroutineLockQueue = Pool<CoroutineLockQueue>.Rent();
coroutineLockQueue.Key = key;
coroutineLockQueue.CoroutineLockQueueType = coroutineLockQueueType;
return coroutineLockQueue;
}
public void Dispose()
{
Key = 0;
CoroutineLockQueueType = null;
Pool<CoroutineLockQueue>.Return(this);
}
public async FTask<WaitCoroutineLock> Lock(string tag, int time)
{
#if TENGINE_DEVELOP
if (_waitCoroutineLocks.Count >= 100)
{
// 当等待队列超过100个、表示这个协程锁可能有问题、打印一个警告方便排查错误
Log.Warning($"too much waitCoroutineLock CoroutineLockQueueType:{CoroutineLockQueueType.Name} Key:{Key} Count: {_waitCoroutineLocks.Count} ");
}
#endif
var waitCoroutineLock = WaitCoroutineLock.Create(this, tag, time);
_waitCoroutineLocks.Enqueue(waitCoroutineLock);
return await waitCoroutineLock.Tcs;
}
public void Release()
{
if (_waitCoroutineLocks.Count == 0)
{
CoroutineLockQueueType.Remove(Key);
return;
}
while (_waitCoroutineLocks.TryDequeue(out var waitCoroutineLock))
{
if (waitCoroutineLock.IsDisposed)
{
continue;
}
waitCoroutineLock.SetResult();
break;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8f07b80619d455d4499aefbc3eab9f65
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
namespace TEngine.Core
{
public sealed class CoroutineLockQueueType
{
public readonly string Name;
private readonly Dictionary<long, CoroutineLockQueue> _coroutineLockQueues = new Dictionary<long, CoroutineLockQueue>();
private CoroutineLockQueueType() { }
public CoroutineLockQueueType(string name)
{
Name = name;
}
public async FTask<WaitCoroutineLock> Lock(long key, string tag = null, int time = 10000)
{
if (_coroutineLockQueues.TryGetValue(key, out var coroutineLockQueue))
{
return await coroutineLockQueue.Lock(tag,time);
}
coroutineLockQueue = CoroutineLockQueue.Create(key, time, this);
_coroutineLockQueues.Add(key, coroutineLockQueue);
return WaitCoroutineLock.Create(coroutineLockQueue, tag, time);
}
public void Remove(long key)
{
if (_coroutineLockQueues.Remove(key, out var coroutineLockQueue))
{
coroutineLockQueue.Dispose();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2067a4304a800324bbe9c547eefac9fa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
using System;
using TEngine.Core;
namespace TEngine.Core
{
public struct CoroutineLockTimeout
{
public long LockId;
public WaitCoroutineLock WaitCoroutineLock;
}
public sealed class OnCoroutineLockTimeout : EventSystem<CoroutineLockTimeout>
{
public override void Handler(CoroutineLockTimeout self)
{
if (self.LockId != self.WaitCoroutineLock.LockId)
{
return;
}
var coroutineLockQueue = self.WaitCoroutineLock.CoroutineLockQueue;
var coroutineLockQueueType = coroutineLockQueue.CoroutineLockQueueType;
var key = coroutineLockQueue.Key;
Log.Error($"coroutine lock timeout CoroutineLockQueueType:{coroutineLockQueueType.Name} Key:{key} Tag:{self.WaitCoroutineLock.Tag}");
}
}
public sealed class WaitCoroutineLock : IDisposable
{
private long _timerId;
public bool IsDisposed => LockId == 0;
public string Tag { get; private set; }
public long LockId { get; private set; }
public FTask<WaitCoroutineLock> Tcs { get; private set; }
public CoroutineLockQueue CoroutineLockQueue{ get; private set; }
public static WaitCoroutineLock Create(CoroutineLockQueue coroutineLockQueue, string tag, int timeOut)
{
var lockId = IdFactory.NextRunTimeId();
var waitCoroutineLock = Pool<WaitCoroutineLock>.Rent();
waitCoroutineLock.Tag = tag;
waitCoroutineLock.LockId = lockId;
waitCoroutineLock.CoroutineLockQueue = coroutineLockQueue;
waitCoroutineLock.Tcs = FTask<WaitCoroutineLock>.Create();
waitCoroutineLock._timerId = TimerScheduler.Instance.Core.OnceTimer(timeOut, new CoroutineLockTimeout()
{
LockId = lockId, WaitCoroutineLock = waitCoroutineLock
});
return waitCoroutineLock;
}
public void Dispose()
{
if (IsDisposed)
{
Log.Error("WaitCoroutineLock is Dispose");
return;
}
Release(CoroutineLockQueue);
LockId = 0;
Tcs = null;
Tag = null;
CoroutineLockQueue = null;
if (_timerId != 0)
{
TimerScheduler.Instance.Core.RemoveByRef(ref _timerId);
}
Pool<WaitCoroutineLock>.Return(this);
}
private static void Release(CoroutineLockQueue coroutineLockQueue)
{
// 放到下一帧执行释放锁、如果不这样、会导致逻辑的顺序不正常
ThreadSynchronizationContext.Main.Post(coroutineLockQueue.Release);
}
public void SetResult()
{
if (Tcs == null)
{
throw new NullReferenceException("SetResult tcs is null");
}
Tcs.SetResult(this);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1ee0d61ec53909c4695fe2ecc97f42bd
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 9f6012de0e164ce4da102c93fa2df9ea
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: bd10fb6ee217e1e4a82db518e161aaec
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,48 @@
#if TENGINE_NET
using System.Linq.Expressions;
#pragma warning disable CS8625
namespace TEngine.Core.DataBase;
public interface IDateBase
{
public static readonly CoroutineLockQueueType DataBaseLock = new CoroutineLockQueueType("DataBaseLock");
IDateBase Initialize(string connectionString, string dbName);
FTask<long> Count<T>(string collection = null) where T : Entity;
FTask<long> Count<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask<bool> Exist<T>(string collection = null) where T : Entity;
FTask<bool> Exist<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask<T> QueryNotLock<T>(long id, string collection = null) where T : Entity;
FTask<T> Query<T>(long id, string collection = null) where T : Entity;
FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity;
FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity;
FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity;
FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity;
FTask<List<T>> QueryByPageOrderBy<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity;
FTask<T> First<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask<T> First<T>(string json, string[] cols, string collection = null) where T : Entity;
FTask<T> Last<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask<List<T>> QueryOrderBy<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity;
FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity;
FTask Query(long id, List<string> collectionNames, List<Entity> result);
FTask<List<T>> QueryJson<T>(string json, string collection = null) where T : Entity;
FTask<List<T>> QueryJson<T>(string json, string[] cols, string collection = null) where T : Entity;
FTask<List<T>> QueryJson<T>(long taskId, string json, string collection = null) where T : Entity;
FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string[] cols, string collection = null) where T : class;
FTask Save<T>(T entity, string collection = null) where T : Entity, new();
FTask Save(long id, List<Entity> entities);
FTask Save<T>(object transactionSession, T entity, string collection = null) where T : Entity;
FTask Insert<T>(T entity, string collection = null) where T : Entity, new();
FTask InsertBatch<T>(IEnumerable<T> list, string collection = null) where T : Entity, new();
FTask InsertBatch<T>(object transactionSession, IEnumerable<T> list, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(object transactionSession, long id, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(long id, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(long id,object transactionSession, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new();
FTask<long> Remove<T>(long id, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new();
FTask<long> Sum<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> sumExpression, string collection = null) where T : Entity;
FTask CreateIndex<T>(string collection, params object[] keys) where T : Entity;
FTask CreateIndex<T>(params object[] keys) where T : Entity;
FTask CreateDB<T>() where T : Entity;
FTask CreateDB(Type type);
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 801a4f2fbf5d7944480423ade9d8a998
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
#if TENGINE_NET
namespace TEngine.Core.DataBase;
public sealed class World
{
public uint Id { get; private init; }
public IDateBase DateBase { get; private init; }
public WorldConfigInfo Config => ConfigTableManage.WorldConfigInfo(Id);
private static readonly Dictionary<uint, World> Worlds = new();
public World(WorldConfigInfo worldConfigInfo)
{
Id = worldConfigInfo.Id;
var dbType = worldConfigInfo.DbType.ToLower();
switch (dbType)
{
case "mongodb":
{
DateBase = new MongoDataBase();
DateBase.Initialize(worldConfigInfo.DbConnection, worldConfigInfo.DbName);
break;
}
default:
throw new Exception("No supported database");
}
}
public static World Create(uint id)
{
if (Worlds.TryGetValue(id, out var world))
{
return world;
}
var worldConfigInfo = ConfigTableManage.WorldConfigInfo(id);
world = new World(worldConfigInfo);
Worlds.Add(id, world);
return world;
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e207b62400a4d2742945b50eb84f8233
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
#if TENGINE_NET
namespace TEngine.Core.DataBase;
public class WorldConfigInfo
{
public uint Id;
public string WorldName;
public string DbConnection;
public string DbName;
public string DbType;
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1c1d63a41ee591348a2aeab0a1ff5b2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,556 @@
#if TENGINE_NET
using System.Linq.Expressions;
using TEngine.Core;
using MongoDB.Bson;
using MongoDB.Driver;
namespace TEngine.Core.DataBase;
public sealed class MongoDataBase : IDateBase
{
private string _dbName;
private string _connectionString;
private MongoClient _mongoClient;
private IMongoDatabase _mongoDatabase;
private readonly HashSet<string> _collections = new HashSet<string>();
public IDateBase Initialize(string connectionString, string dbName)
{
_dbName = dbName;
_connectionString = connectionString;
_mongoClient = new MongoClient(connectionString);
_mongoDatabase = _mongoClient.GetDatabase(dbName);
// 记录所有集合名
_collections.UnionWith(_mongoDatabase.ListCollectionNames().ToList());
return this;
}
#region Other
public async FTask<long> Sum<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> sumExpression, string collection = null) where T : Entity
{
var member = (MemberExpression) ((UnaryExpression) sumExpression.Body).Operand;
var projection =
new BsonDocument("_id", "null").Add("Result", new BsonDocument("$sum", $"${member.Member.Name}"));
var data = await GetCollection<T>(collection).Aggregate().Match(filter).Group(projection)
.FirstOrDefaultAsync();
return data == null ? 0 : Convert.ToInt64(data["Result"]);
}
#endregion
#region GetCollection
private IMongoCollection<T> GetCollection<T>(string collection = null)
{
return _mongoDatabase.GetCollection<T>(collection ?? typeof(T).Name);
}
private IMongoCollection<Entity> GetCollection(string name)
{
return _mongoDatabase.GetCollection<Entity>(name);
}
#endregion
#region Count
public async FTask<long> Count<T>(string collection = null) where T : Entity
{
return await GetCollection<T>(collection).CountDocumentsAsync(d => true);
}
public async FTask<long> Count<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
return await GetCollection<T>(collection).CountDocumentsAsync(filter);
}
#endregion
#region Exist
public async FTask<bool> Exist<T>(string collection = null) where T : Entity
{
return await Count<T>(collection) > 0;
}
public async FTask<bool> Exist<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
return await Count(filter, collection) > 0;
}
#endregion
#region Query
public async FTask<T> QueryNotLock<T>(long id, string collection = null) where T : Entity
{
var cursor = await GetCollection<T>(collection).FindAsync(d => d.Id == id);
var v = await cursor.FirstOrDefaultAsync();
return v;
}
public async FTask<T> Query<T>(long id, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(id))
{
var cursor = await GetCollection<T>(collection).FindAsync(d => d.Id == id);
var v = await cursor.FirstOrDefaultAsync();
return v;
}
}
public async FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var count = await Count(filter);
var dates = await QueryByPage(filter, pageIndex, pageSize, collection);
return ((int)count, dates);
}
}
public async FTask<(int count, List<T> dates)> QueryCountAndDatesByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var count = await Count(filter);
var dates = await QueryByPage(filter, pageIndex, pageSize, cols, collection);
return ((int) count, dates);
}
}
public async FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
return await GetCollection<T>(collection).Find(filter).Skip((pageIndex - 1) * pageSize)
.Limit(pageSize)
.ToListAsync();
}
}
public async FTask<List<T>> QueryByPage<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, string[] cols, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include("");
foreach (var col in cols)
{
projection = projection.Include(col);
}
return await GetCollection<T>(collection).Find(filter).Project<T>(projection)
.Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync();
}
}
public async FTask<List<T>> QueryByPageOrderBy<T>(Expression<Func<T, bool>> filter, int pageIndex, int pageSize, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
if (isAsc)
{
return await GetCollection<T>(collection).Find(filter).SortBy(orderByExpression)
.Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync();
}
return await GetCollection<T>(collection).Find(filter).SortByDescending(orderByExpression)
.Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToListAsync();
}
}
public async FTask<T> First<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var cursor = await GetCollection<T>(collection).FindAsync(filter);
return await cursor.FirstOrDefaultAsync();
}
}
public async FTask<T> First<T>(string json, string[] cols, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include("");
foreach (var col in cols)
{
projection = projection.Include(col);
}
var options = new FindOptions<T, T> {Projection = projection};
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition, options);
return await cursor.FirstOrDefaultAsync();
}
}
public async FTask<T> Last<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var cursor = await GetCollection<T>(collection).FindAsync(filter);
var list = await cursor.ToListAsync();
return list.LastOrDefault();
}
}
public async FTask<List<T>> QueryOrderBy<T>(Expression<Func<T, bool>> filter, Expression<Func<T, object>> orderByExpression, bool isAsc = true, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
if (isAsc)
{
return await GetCollection<T>(collection).Find(filter).SortBy(orderByExpression).ToListAsync();
}
return await GetCollection<T>(collection).Find(filter).SortByDescending(orderByExpression)
.ToListAsync();
}
}
public async FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var cursor = await GetCollection<T>(collection).FindAsync(filter);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask Query(long id, List<string> collectionNames, List<Entity> result)
{
using (await IDateBase.DataBaseLock.Lock(id))
{
if (collectionNames == null || collectionNames.Count == 0)
{
return;
}
foreach (var collectionName in collectionNames)
{
var cursor = await GetCollection(collectionName).FindAsync(d => d.Id == id);
var e = await cursor.FirstOrDefaultAsync();
if (e == null)
{
continue;
}
result.Add(e);
}
}
}
public async FTask<List<T>> QueryJson<T>(string json, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask<List<T>> QueryJson<T>(string json, string[] cols, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include("");
foreach (var col in cols)
{
projection = projection.Include(col);
}
var options = new FindOptions<T, T> {Projection = projection};
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition, options);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask<List<T>> QueryJson<T>(long taskId, string json, string collection = null) where T : Entity
{
using (await IDateBase.DataBaseLock.Lock(taskId))
{
FilterDefinition<T> filterDefinition = new JsonFilterDefinition<T>(json);
var cursor = await GetCollection<T>(collection).FindAsync(filterDefinition);
var v = await cursor.ToListAsync();
return v;
}
}
public async FTask<List<T>> Query<T>(Expression<Func<T, bool>> filter, string[] cols, string collection = null) where T : class
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
var projection = Builders<T>.Projection.Include(cols[0]);
for (var i = 1; i < cols.Length; i++)
{
projection = projection.Include(cols[i]);
}
return await GetCollection<T>(collection).Find(filter).Project<T>(projection).ToListAsync();
}
}
#endregion
#region Save
public async FTask Save<T>(object transactionSession, T entity, string collection = null) where T : Entity
{
if (entity == null)
{
Log.Error($"save entity is null: {typeof(T).Name}");
return;
}
var clone = MongoHelper.Instance.Clone(entity);
using (await IDateBase.DataBaseLock.Lock(clone.Id))
{
await GetCollection(collection ?? clone.GetType().Name).ReplaceOneAsync(
(IClientSessionHandle) transactionSession, d => d.Id == clone.Id, clone,
new ReplaceOptions {IsUpsert = true});
}
}
public async FTask Save<T>(T entity, string collection = null) where T : Entity, new()
{
if (entity == null)
{
Log.Error($"save entity is null: {typeof(T).Name}");
return;
}
var clone = MongoHelper.Instance.Clone(entity);
using (await IDateBase.DataBaseLock.Lock(clone.Id))
{
await GetCollection(collection ?? clone.GetType().Name).ReplaceOneAsync(d => d.Id == clone.Id, clone,
new ReplaceOptions {IsUpsert = true});
}
}
private async FTask SaveBase<T>(T entity, string collection = null) where T : Entity
{
if (entity == null)
{
Log.Error($"save entity is null: {typeof(T).Name}");
return;
}
var clone = MongoHelper.Instance.Clone(entity);
using (await IDateBase.DataBaseLock.Lock(clone.Id))
{
await GetCollection(collection ?? clone.GetType().Name).ReplaceOneAsync(d => d.Id == clone.Id, clone, new ReplaceOptions {IsUpsert = true});
}
}
public async FTask Save(long id, List<Entity> entities)
{
if (entities == null)
{
Log.Error("save entity is null");
return;
}
var clones = MongoHelper.Instance.Clone(entities);
using (await IDateBase.DataBaseLock.Lock(id))
{
foreach (Entity clone in clones)
{
try
{
await GetCollection(clone.GetType().Name).ReplaceOneAsync(d => d.Id == clone.Id, clone,
new ReplaceOptions {IsUpsert = true});
}
catch (Exception e)
{
Log.Error($"Save List Entity Error: {clone.GetType().Name} {clone}\n{e}");
}
}
}
}
#endregion
#region Insert
public FTask Insert<T>(T entity, string collection = null) where T : Entity, new()
{
return Save(entity);
}
public async FTask InsertBatch<T>(IEnumerable<T> list, string collection = null) where T : Entity, new()
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
await GetCollection<T>(collection ?? typeof(T).Name).InsertManyAsync(list);
}
}
public async FTask InsertBatch<T>(object transactionSession, IEnumerable<T> list, string collection = null) where T : Entity, new()
{
using (await IDateBase.DataBaseLock.Lock(RandomHelper.RandInt64()))
{
await GetCollection<T>(collection ?? typeof(T).Name).InsertManyAsync((IClientSessionHandle) transactionSession, list);
}
}
#endregion
#region Remove
public async FTask<long> Remove<T>(object transactionSession, long id, string collection = null) where T : Entity, new()
{
using (await IDateBase.DataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteOneAsync((IClientSessionHandle) transactionSession, d => d.Id == id);
return result.DeletedCount;
}
}
public async FTask<long> Remove<T>(long id, string collection = null) where T : Entity, new()
{
using (await IDateBase.DataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteOneAsync(d => d.Id == id);
return result.DeletedCount;
}
}
public async FTask<long> Remove<T>(long id, object transactionSession, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new()
{
using (await IDateBase.DataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteManyAsync((IClientSessionHandle) transactionSession, filter);
return result.DeletedCount;
}
}
public async FTask<long> Remove<T>(long id, Expression<Func<T, bool>> filter, string collection = null) where T : Entity, new()
{
using (await IDateBase.DataBaseLock.Lock(id))
{
var result = await GetCollection<T>(collection).DeleteManyAsync(filter);
return result.DeletedCount;
}
}
#endregion
#region Index
/// <summary>
/// 创建数据库索引
/// </summary>
/// <param name="collection"></param>
/// <param name="keys"></param>
/// <typeparam name="T"></typeparam>
/// <code>
/// 使用例子(可多个):
/// 1 : Builders.IndexKeys.Ascending(d=>d.Id)
/// 2 : Builders.IndexKeys.Descending(d=>d.Id).Ascending(d=>d.Name)
/// 3 : Builders.IndexKeys.Descending(d=>d.Id),Builders.IndexKeys.Descending(d=>d.Name)
/// </code>
public async FTask CreateIndex<T>(string collection, params object[] keys) where T : Entity
{
if (keys == null || keys.Length <= 0)
{
return;
}
var indexModels = new List<CreateIndexModel<T>>();
foreach (object key in keys)
{
IndexKeysDefinition<T> indexKeysDefinition = (IndexKeysDefinition<T>) key;
indexModels.Add(new CreateIndexModel<T>(indexKeysDefinition));
}
await GetCollection<T>(collection).Indexes.CreateManyAsync(indexModels);
}
public async FTask CreateIndex<T>(params object[] keys) where T : Entity
{
if (keys == null)
{
return;
}
List<CreateIndexModel<T>> indexModels = new List<CreateIndexModel<T>>();
foreach (object key in keys)
{
IndexKeysDefinition<T> indexKeysDefinition = (IndexKeysDefinition<T>) key;
indexModels.Add(new CreateIndexModel<T>(indexKeysDefinition));
}
await GetCollection<T>().Indexes.CreateManyAsync(indexModels);
}
#endregion
#region CreateDB
public async FTask CreateDB<T>() where T : Entity
{
// 已经存在数据库表
string name = typeof(T).Name;
if (_collections.Contains(name))
{
return;
}
await _mongoDatabase.CreateCollectionAsync(name);
_collections.Add(name);
}
public async FTask CreateDB(Type type)
{
string name = type.Name;
if (_collections.Contains(name))
{
return;
}
await _mongoDatabase.CreateCollectionAsync(name);
_collections.Add(name);
}
#endregion
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0aaa971dc58538e46b0e43df7cc72a2f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: ddb0edd718602754083d07d649e65d8f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: e9c6ac6ade3864b42887435f6eac263f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,261 @@
using System;
using System.Collections.Generic;
using System.IO;
#pragma warning disable CS8625
#pragma warning disable CS8618
namespace TEngine.DataStructure
{
/// <summary>
/// 环形缓存自动扩充、不会收缩缓存、所以不要用这个操作过大的IO流
/// 1、环大小8192溢出的会自动增加环的大小。
/// 2、每个块都是一个环形缓存当溢出的时候会自动添加到下一个环中。
/// 3、当读取完成后用过的环会放在缓存中不会销毁掉。
/// </summary>
public sealed class CircularBuffer : Stream, IDisposable
{
private byte[] _lastBuffer;
public const int ChunkSize = 8192; // 环形缓存块大小
private readonly Queue<byte[]> _bufferCache = new Queue<byte[]>();
private readonly Queue<byte[]> _bufferQueue = new Queue<byte[]>();
public int FirstIndex { get; set; }
public int LastIndex { get; set; }
public override long Length
{
get
{
if (_bufferQueue.Count == 0)
{
return 0;
}
return (_bufferQueue.Count - 1) * ChunkSize + LastIndex - FirstIndex;
}
}
public byte[] First
{
get
{
if (_bufferQueue.Count == 0)
{
AddLast();
}
return _bufferQueue.Peek();
}
}
public byte[] Last
{
get
{
if (_bufferQueue.Count == 0)
{
AddLast();
}
return _lastBuffer;
}
}
public void AddLast()
{
var buffer = _bufferCache.Count > 0 ? _bufferCache.Dequeue() : new byte[ChunkSize];
_bufferQueue.Enqueue(buffer);
_lastBuffer = buffer;
}
public void RemoveFirst()
{
_bufferCache.Enqueue(_bufferQueue.Dequeue());
}
public void Read(Stream stream, int count)
{
if (count > Length)
{
throw new Exception($"bufferList length < count, {Length} {count}");
}
var copyCount = 0;
while (copyCount < count)
{
var n = count - copyCount;
if (ChunkSize - FirstIndex > n)
{
stream.Write(First, FirstIndex, n);
FirstIndex += n;
copyCount += n;
}
else
{
stream.Write(First, FirstIndex, ChunkSize - FirstIndex);
copyCount += ChunkSize - FirstIndex;
FirstIndex = 0;
RemoveFirst();
}
}
}
public void Read(Memory<byte> memory, int count)
{
if (count > Length)
{
throw new Exception($"bufferList length < count, {Length} {count}");
}
var copyCount = 0;
while (copyCount < count)
{
var n = count - copyCount;
var asMemory = First.AsMemory();
if (ChunkSize - FirstIndex > n)
{
var slice = asMemory.Slice(FirstIndex, n);
slice.CopyTo(memory.Slice(copyCount, n));
FirstIndex += n;
copyCount += n;
}
else
{
var length = ChunkSize - FirstIndex;
var slice = asMemory.Slice(FirstIndex, length);
slice.CopyTo(memory.Slice(copyCount, length));
copyCount += ChunkSize - FirstIndex;
FirstIndex = 0;
RemoveFirst();
}
}
}
public override int Read(byte[] buffer, int offset, int count)
{
if (buffer.Length < offset + count)
{
throw new Exception($"buffer length < count, buffer length: {buffer.Length} {offset} {count}");
}
var length = Length;
if (length < count)
{
count = (int) length;
}
var copyCount = 0;
while (copyCount < count)
{
var copyLength = count - copyCount;
if (ChunkSize - FirstIndex > copyLength)
{
Array.Copy(First, FirstIndex, buffer, copyCount + offset, copyLength);
FirstIndex += copyLength;
copyCount += copyLength;
continue;
}
Array.Copy(First, FirstIndex, buffer, copyCount + offset, ChunkSize - FirstIndex);
copyCount += ChunkSize - FirstIndex;
FirstIndex = 0;
RemoveFirst();
}
return count;
}
public void Write(byte[] buffer)
{
Write(buffer, 0, buffer.Length);
}
public void Write(Stream stream)
{
var copyCount = 0;
var count = (int) (stream.Length - stream.Position);
while (copyCount < count)
{
if (LastIndex == ChunkSize)
{
AddLast();
LastIndex = 0;
}
var n = count - copyCount;
if (ChunkSize - LastIndex > n)
{
_ = stream.Read(Last, LastIndex, n);
LastIndex += count - copyCount;
copyCount += n;
}
else
{
_ = stream.Read(Last, LastIndex, ChunkSize - LastIndex);
copyCount += ChunkSize - LastIndex;
LastIndex = ChunkSize;
}
}
}
public override void Write(byte[] buffer, int offset, int count)
{
var copyCount = 0;
while (copyCount < count)
{
if (ChunkSize == LastIndex)
{
AddLast();
LastIndex = 0;
}
var byteLength = count - copyCount;
if (ChunkSize - LastIndex > byteLength)
{
Array.Copy(buffer, copyCount + offset, Last, LastIndex, byteLength);
LastIndex += byteLength;
copyCount += byteLength;
}
else
{
Array.Copy(buffer, copyCount + offset, _lastBuffer, LastIndex, ChunkSize - LastIndex);
copyCount += ChunkSize - LastIndex;
LastIndex = ChunkSize;
}
}
}
public override bool CanRead { get; } = true;
public override bool CanSeek { get; } = false;
public override bool CanWrite { get; } = true;
public override long Position { get; set; }
public override void Flush()
{
throw new NotImplementedException();
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
}
public override void SetLength(long value)
{
throw new NotImplementedException();
}
public new void Dispose()
{
_bufferQueue.Clear();
_lastBuffer = null;
FirstIndex = 0;
LastIndex = 0;
base.Dispose();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 28188b5e2acefe14a996fb93df6b2fc6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,115 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
#pragma warning disable CS8603
namespace TEngine.DataStructure
{
public class ConcurrentOneToManyListPool<TKey, TValue> : ConcurrentOneToManyList<TKey, TValue>, IDisposable where TKey : notnull
{
private bool _isDispose;
public static ConcurrentOneToManyListPool<TKey, TValue> Create()
{
var a = Pool<ConcurrentOneToManyListPool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<ConcurrentOneToManyListPool<TKey, TValue>>.Return(this);
}
}
public class ConcurrentOneToManyList<TKey, TValue> : ConcurrentDictionary<TKey, List<TValue>> where TKey : notnull
{
private readonly Queue<List<TValue>> _queue = new Queue<List<TValue>>();
private readonly int _recyclingLimit = 120;
public ConcurrentOneToManyList()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public ConcurrentOneToManyList(int recyclingLimit)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
public void Add(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Add(value);
base[key] = list;
return;
}
list.Add(value);
}
public TValue First(TKey key)
{
return !TryGetValue(key, out var list) ? default : list.FirstOrDefault();
}
public void RemoveValue(TKey key, TValue value)
{
if (!TryGetValue(key, out var list)) return;
list.Remove(value);
if (list.Count == 0) RemoveKey(key);
}
public void RemoveKey(TKey key)
{
if (!TryRemove(key, out var list)) return;
Recycle(list);
}
private List<TValue> Fetch()
{
return _queue.Count <= 0 ? new List<TValue>() : _queue.Dequeue();
}
private void Recycle(List<TValue> list)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(list);
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f35e267f0b797464e856a9b9244b4215
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,116 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
#pragma warning disable CS8603
namespace TEngine.DataStructure
{
public class ConcurrentOneToManyQueuePool<TKey, TValue> : ConcurrentOneToManyQueue<TKey, TValue>, IDisposable
where TKey : notnull
{
private bool _isDispose;
public static ConcurrentOneToManyQueuePool<TKey, TValue> Create()
{
var a = Pool<ConcurrentOneToManyQueuePool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<ConcurrentOneToManyQueue<TKey, TValue>>.Return(this);
}
}
public class ConcurrentOneToManyQueue<TKey, TValue> : ConcurrentDictionary<TKey, Queue<TValue>> where TKey : notnull
{
private readonly Queue<Queue<TValue>> _queue = new Queue<Queue<TValue>>();
private readonly int _recyclingLimit;
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public ConcurrentOneToManyQueue(int recyclingLimit = 0)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
public void Enqueue(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Enqueue(value);
TryAdd(key, list);
return;
}
list.Enqueue(value);
}
public TValue Dequeue(TKey key)
{
if (!TryGetValue(key, out var list) || list.Count == 0) return default;
var value = list.Dequeue();
if (list.Count == 0) RemoveKey(key);
return value;
}
public bool TryDequeue(TKey key, out TValue value)
{
value = Dequeue(key);
return value != null;
}
public void RemoveKey(TKey key)
{
if (!TryGetValue(key, out var list)) return;
TryRemove(key, out _);
Recycle(list);
}
private Queue<TValue> Fetch()
{
return _queue.Count <= 0 ? new Queue<TValue>() : _queue.Dequeue();
}
private void Recycle(Queue<TValue> list)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(list);
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 162f7b8459ead1940bfaef049f6d8e2c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public sealed class EntityList<T> : List<T>, IDisposable where T : IDisposable
{
private bool _isDispose;
public static EntityList<T> Create()
{
var list = Pool<EntityList<T>>.Rent();
list._isDispose = false;
return list;
}
public new void Clear()
{
for (var i = 0; i < this.Count; i++)
{
this[i].Dispose();
}
base.Clear();
}
public void ClearNotDispose()
{
base.Clear();
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<EntityList<T>>.Return(this);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 763e9615aee0ac1428f141df39057bf7
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public sealed class HashSetPool<T> : HashSet<T>, IDisposable
{
private bool _isDispose;
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<HashSetPool<T>>.Return(this);
}
public static HashSetPool<T> Create()
{
var list = Pool<HashSetPool<T>>.Rent();
list._isDispose = false;
return list;
}
}
public sealed class HashSetBasePool<T> : IDisposable
{
public HashSet<T> Set = new HashSet<T>();
public static HashSetBasePool<T> Create()
{
return Pool<HashSetBasePool<T>>.Rent();
}
public void Dispose()
{
Set.Clear();
Pool<HashSetBasePool<T>>.Return(this);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0f2f1a1f1a3f5f246a40be1e7bff871a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public sealed class ListPool<T> : List<T>, IDisposable
{
private bool _isDispose;
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<ListPool<T>>.Return(this);
}
public static ListPool<T> Create(params T[] args)
{
var list = Pool<ListPool<T>>.Rent();
list._isDispose = false;
if (args != null) list.AddRange(args);
return list;
}
public static ListPool<T> Create(List<T> args)
{
var list = Pool<ListPool<T>>.Rent();
list._isDispose = false;
if (args != null) list.AddRange(args);
return list;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3014ce89f46d33742acca54a28995242
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
#pragma warning disable CS8600
namespace TEngine.DataStructure
{
public class OneToManyHashSetPool<TKey, TValue> : OneToManyHashSet<TKey, TValue>, IDisposable where TKey : notnull
{
private bool _isDispose;
public static OneToManyHashSetPool<TKey, TValue> Create()
{
var a = Pool<OneToManyHashSetPool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<OneToManyHashSetPool<TKey, TValue>>.Return(this);
}
}
public class OneToManyHashSet<TKey, TValue> : Dictionary<TKey, HashSet<TValue>> where TKey : notnull
{
private readonly Queue<HashSet<TValue>> _queue = new Queue<HashSet<TValue>>();
private readonly int _recyclingLimit = 120;
private static HashSet<TValue> _empty = new HashSet<TValue>();
public OneToManyHashSet()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public OneToManyHashSet(int recyclingLimit)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
public void Add(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Add(value);
Add(key, list);
return;
}
list.Add(value);
}
public void RemoveValue(TKey key, TValue value)
{
if (!TryGetValue(key, out var list)) return;
list.Remove(value);
if (list.Count == 0) RemoveKey(key);
}
public void RemoveKey(TKey key)
{
if (!TryGetValue(key, out var list)) return;
Remove(key);
Recycle(list);
}
public HashSet<TValue> GetValue(TKey key)
{
if (TryGetValue(key, out HashSet<TValue> value))
{
return value;
}
return _empty;
}
private HashSet<TValue> Fetch()
{
return _queue.Count <= 0 ? new HashSet<TValue>() : _queue.Dequeue();
}
private void Recycle(HashSet<TValue> list)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(list);
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 393302e735c4b2f4885c21dd4235b64a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,141 @@
using System;
using System.Collections.Generic;
using System.Linq;
#pragma warning disable CS8600
#pragma warning disable CS8603
namespace TEngine.DataStructure
{
public class OneToManyListPool<TKey, TValue> : OneToManyList<TKey, TValue>, IDisposable where TKey : notnull
{
private bool _isDispose;
public static OneToManyListPool<TKey, TValue> Create()
{
var list = Pool<OneToManyListPool<TKey, TValue>>.Rent();
list._isDispose = false;
return list;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<OneToManyListPool<TKey, TValue>>.Return(this);
}
}
public class OneToManyList<TKey, TValue> : Dictionary<TKey, List<TValue>> where TKey : notnull
{
private readonly Queue<List<TValue>> _queue = new Queue<List<TValue>>();
private readonly int _recyclingLimit = 120;
private static List<TValue> _empty = new List<TValue>();
public OneToManyList()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public OneToManyList(int recyclingLimit)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
public void Add(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Add(value);
Add(key, list);
return;
}
list.Add(value);
}
public TValue First(TKey key)
{
return !TryGetValue(key, out var list) ? default : list.FirstOrDefault();
}
public bool RemoveValue(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
return true;
}
var isRemove = list.Remove(value);
if (list.Count == 0)
{
isRemove = RemoveByKey(key);
}
return isRemove;
}
public bool RemoveByKey(TKey key)
{
if (!TryGetValue(key, out var list))
{
return false;
}
Remove(key);
Recycle(list);
return true;
}
public List<TValue> GetValues(TKey key)
{
if (TryGetValue(key, out List<TValue> list))
{
return list;
}
return _empty;
}
public new void Clear()
{
foreach (var keyValuePair in this) Recycle(keyValuePair.Value);
base.Clear();
}
private List<TValue> Fetch()
{
return _queue.Count <= 0 ? new List<TValue>() : _queue.Dequeue();
}
private void Recycle(List<TValue> list)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(list);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7a6fdf7c8423c0e41804022df95a399d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
#pragma warning disable CS8603
namespace TEngine.DataStructure
{
public class OneToManyQueuePool<TKey, TValue> : OneToManyQueue<TKey, TValue>, IDisposable where TKey : notnull
{
private bool _isDispose;
public static OneToManyQueuePool<TKey, TValue> Create()
{
var a = Pool<OneToManyQueuePool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<OneToManyQueuePool<TKey, TValue>>.Return(this);
}
}
public class OneToManyQueue<TKey, TValue> : Dictionary<TKey, Queue<TValue>> where TKey : notnull
{
private readonly Queue<Queue<TValue>> _queue = new Queue<Queue<TValue>>();
private readonly int _recyclingLimit;
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public OneToManyQueue(int recyclingLimit = 0)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
public void Enqueue(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Enqueue(value);
Add(key, list);
return;
}
list.Enqueue(value);
}
public TValue Dequeue(TKey key)
{
if (!TryGetValue(key, out var list) || list.Count == 0)
{
return default;
}
var value = list.Dequeue();
if (list.Count == 0)
{
RemoveKey(key);
}
return value;
}
public bool TryDequeue(TKey key, out TValue value)
{
value = Dequeue(key);
return value != null;
}
public void RemoveKey(TKey key)
{
if (!TryGetValue(key, out var list)) return;
Remove(key);
Recycle(list);
}
private Queue<TValue> Fetch()
{
return _queue.Count <= 0 ? new Queue<TValue>() : _queue.Dequeue();
}
private void Recycle(Queue<TValue> list)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(list);
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 35d6a9fde65a99143b0abf6d9569c462
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,537 @@
// #if UNITY_5_3_OR_NEWER
// using System;
// using System.Collections;
// using System.Collections.Generic;
// using System.Diagnostics;
// using System.Diagnostics.CodeAnalysis;
// using System.Linq;
// using System.Runtime.CompilerServices;
// #pragma warning disable CS8600
//
// namespace System.Collections.Generic
// {
// public class PriorityQueue<TElement, TPriority>
// {
// private const int DefaultCapacity = 4;
//
// private readonly IComparer<TPriority> _priorityComparer;
//
// private HeapEntry[] _heap;
// private int _count;
// private int _version;
//
// private UnorderedItemsCollection? _unorderedItemsCollection;
//
// #region Constructors
// public PriorityQueue() : this(0, null)
// {
//
// }
//
// // public PriorityQueue(int initialCapacity) : this(initialCapacity, null)
// // {
// //
// // }
//
// // public PriorityQueue(IComparer<TPriority>? comparer) : this(0, comparer)
// // {
// //
// // }
//
// // public PriorityQueue(int initialCapacity, IComparer<TPriority>? comparer)
// // {
// // if (initialCapacity < 0)
// // {
// // throw new ArgumentOutOfRangeException(nameof(initialCapacity));
// // }
// //
// // if (initialCapacity == 0)
// // {
// // _heap = Array.Empty<HeapEntry>();
// // }
// // else
// // {
// // _heap = new HeapEntry[initialCapacity];
// // }
// //
// // _priorityComparer = comparer ?? Comparer<TPriority>.Default;
// // }
//
// public PriorityQueue(IEnumerable<(TElement Element, TPriority Priority)> values) : this(values, null)
// {
//
// }
//
// public PriorityQueue(IEnumerable<(TElement Element, TPriority Priority)> values, IComparer<TPriority>? comparer)
// {
// _priorityComparer = comparer ?? Comparer<TPriority>.Default;
// _heap = Array.Empty<HeapEntry>();
// _count = 0;
//
// AppendRaw(values);
// Heapify();
// }
// #endregion
//
// public int Count => _count;
// public IComparer<TPriority> Comparer => _priorityComparer;
//
// public void Enqueue(TElement element, TPriority priority)
// {
// _version++;
// if (_count == _heap.Length)
// {
// Resize(ref _heap);
// }
//
// SiftUp(index: _count++, in element, in priority);
// }
//
// public void EnqueueRange(IEnumerable<(TElement Element, TPriority Priority)> values)
// {
// _version++;
// if (_count == 0)
// {
// AppendRaw(values);
// Heapify();
// }
// else
// {
// foreach ((TElement element, TPriority priority) in values)
// {
// if (_count == _heap.Length)
// {
// Resize(ref _heap);
// }
//
// SiftUp(index: _count++, in element, in priority);
// }
// }
// }
//
// // TODO optimize
// public void EnqueueRange(IEnumerable<TElement> elements, TPriority priority) => EnqueueRange(elements.Select(e => (e, priority)));
//
// public TElement Peek()
// {
// if (_count == 0)
// {
// throw new InvalidOperationException();
// }
//
// return _heap[0].Element;
// }
//
// public bool TryPeek([MaybeNullWhen(false)] out TElement element, [MaybeNullWhen(false)] out TPriority priority)
// {
// if (_count == 0)
// {
// element = default;
// priority = default;
// return false;
// }
//
// (element, priority) = _heap[0];
// return true;
// }
//
// public TElement Dequeue()
// {
// if (_count == 0)
// {
// throw new InvalidOperationException();
// }
//
// _version++;
// RemoveIndex(index: 0, out TElement result, out _);
// return result;
// }
//
// public bool TryDequeue([MaybeNullWhen(false)] out TElement element, [MaybeNullWhen(false)] out TPriority priority)
// {
// if (_count == 0)
// {
// element = default;
// priority = default;
// return false;
// }
//
// _version++;
// RemoveIndex(index: 0, out element, out priority);
// return true;
// }
//
// public TElement EnqueueDequeue(TElement element, TPriority priority)
// {
// if (_count == 0)
// {
// return element;
// }
//
// ref HeapEntry minEntry = ref _heap[0];
// if (_priorityComparer.Compare(priority, minEntry.Priority) <= 0)
// {
// return element;
// }
//
// _version++;
// TElement minElement = minEntry.Element;
// #if SIFTDOWN_EMPTY_NODES
// SiftDownHeapPropertyRequired(index: 0, in element, in priority);
// #else
// SiftDown(index: 0, in element, in priority);
// #endif
// return minElement;
// }
//
// public void Clear()
// {
// _version++;
// if (_count > 0)
// {
// //if (RuntimeHelpers.IsReferenceOrContainsReferences<HeapEntry>())
// {
// Array.Clear(_heap, 0, _count);
// }
//
// _count = 0;
// }
// }
//
// public void TrimExcess()
// {
// int count = _count;
// int threshold = (int)(((double)_heap.Length) * 0.9);
// if (count < threshold)
// {
// Array.Resize(ref _heap, count);
// }
// }
//
// public void EnsureCapacity(int capacity)
// {
// if (capacity < 0)
// {
// throw new ArgumentOutOfRangeException();
// }
//
// if (capacity > _heap.Length)
// {
// Array.Resize(ref _heap, capacity);
// }
// }
//
// public UnorderedItemsCollection UnorderedItems => _unorderedItemsCollection ??= new UnorderedItemsCollection(this);
//
// public class UnorderedItemsCollection : IReadOnlyCollection<(TElement Element, TPriority Priority)>, ICollection
// {
// private readonly PriorityQueue<TElement, TPriority> _priorityQueue;
//
// internal UnorderedItemsCollection(PriorityQueue<TElement, TPriority> priorityQueue)
// {
// _priorityQueue = priorityQueue;
// }
//
// public int Count => _priorityQueue.Count;
// public bool IsSynchronized => false;
// public object SyncRoot => _priorityQueue;
//
// public Enumerator GetEnumerator() => new Enumerator(_priorityQueue);
// IEnumerator<(TElement Element, TPriority Priority)> IEnumerable<(TElement Element, TPriority Priority)>.GetEnumerator() => new Enumerator(_priorityQueue);
// IEnumerator IEnumerable.GetEnumerator() => new Enumerator(_priorityQueue);
//
// bool ICollection.IsSynchronized => false;
// object ICollection.SyncRoot => this;
// void ICollection.CopyTo(Array array, int index)
// {
// if (array == null)
// throw new ArgumentNullException(nameof(array));
// if (array.Rank != 1)
// throw new ArgumentException("SR.Arg_RankMultiDimNotSupported", nameof(array));
// if (index < 0)
// throw new ArgumentOutOfRangeException(nameof(index), "SR.ArgumentOutOfRange_Index");
//
// int arrayLen = array.Length;
// if (arrayLen - index < _priorityQueue._count)
// throw new ArgumentException("SR.Argument_InvalidOffLen");
//
// int numToCopy = _priorityQueue._count;
// HeapEntry[] heap = _priorityQueue._heap;
//
// for (int i = 0; i < numToCopy; i++)
// {
// ref HeapEntry entry = ref heap[i];
// array.SetValue((entry.Element, entry.Priority), index + i);
// }
// }
//
// public struct Enumerator : IEnumerator<(TElement Element, TPriority Priority)>, IEnumerator
// {
// private readonly PriorityQueue<TElement, TPriority> _queue;
// private readonly int _version;
// private int _index;
// private (TElement Element, TPriority Priority) _current;
//
// internal Enumerator(PriorityQueue<TElement, TPriority> queue)
// {
// _version = queue._version;
// _queue = queue;
// _index = 0;
// _current = default;
// }
//
// public bool MoveNext()
// {
// PriorityQueue<TElement, TPriority> queue = _queue;
//
// if (queue._version == _version && _index < queue._count)
// {
// ref HeapEntry entry = ref queue._heap[_index];
// _current = (entry.Element, entry.Priority);
// _index++;
// return true;
// }
//
// if (queue._version != _version)
// {
// throw new InvalidOperationException("collection was modified");
// }
//
// return false;
// }
//
// public (TElement Element, TPriority Priority) Current => _current;
// object IEnumerator.Current => _current;
//
// public void Reset()
// {
// if (_queue._version != _version)
// {
// throw new InvalidOperationException("collection was modified");
// }
//
// _index = 0;
// _current = default;
// }
//
// public void Dispose()
// {
// }
// }
// }
//
// #region Private Methods
// private void Heapify()
// {
// HeapEntry[] heap = _heap;
//
// for (int i = (_count - 1) >> 2; i >= 0; i--)
// {
// HeapEntry entry = heap[i]; // ensure struct is copied before sifting
// SiftDown(i, in entry.Element, in entry.Priority);
// }
// }
//
// private void AppendRaw(IEnumerable<(TElement Element, TPriority Priority)> values)
// {
// // TODO: specialize on ICollection types
// var heap = _heap;
// int count = _count;
//
// foreach ((TElement element, TPriority priority) in values)
// {
// if (count == heap.Length)
// {
// Resize(ref heap);
// }
//
// ref HeapEntry entry = ref heap[count];
// entry.Element = element;
// entry.Priority = priority;
// count++;
// }
//
// _heap = heap;
// _count = count;
// }
//
// private void RemoveIndex(int index, out TElement element, out TPriority priority)
// {
// Debug.Assert(index < _count);
//
// (element, priority) = _heap[index];
//
// int lastElementPos = --_count;
// ref HeapEntry lastElement = ref _heap[lastElementPos];
//
// if (lastElementPos > 0)
// {
// #if SIFTDOWN_EMPTY_NODES
// SiftDownHeapPropertyRequired(index, in lastElement.Element, in lastElement.Priority);
// #else
// SiftDown(index, in lastElement.Element, in lastElement.Priority);
// #endif
// }
//
// //if (RuntimeHelpers.IsReferenceOrContainsReferences<HeapEntry>())
// {
// lastElement = default;
// }
// }
//
// private void SiftUp(int index, in TElement element, in TPriority priority)
// {
// while (index > 0)
// {
// int parentIndex = (index - 1) >> 2;
// ref HeapEntry parent = ref _heap[parentIndex];
//
// if (_priorityComparer.Compare(parent.Priority, priority) <= 0)
// {
// // parentPriority <= priority, heap property is satisfed
// break;
// }
//
// _heap[index] = parent;
// index = parentIndex;
// }
//
// ref HeapEntry entry = ref _heap[index];
// entry.Element = element;
// entry.Priority = priority;
// }
//
// private void SiftDown(int index, in TElement element, in TPriority priority)
// {
// int minChildIndex;
// int count = _count;
// HeapEntry[] heap = _heap;
//
// while ((minChildIndex = (index << 2) + 1) < count)
// {
// // find the child with the minimal priority
// ref HeapEntry minChild = ref heap[minChildIndex];
// int childUpperBound = Math.Min(count, minChildIndex + 4);
//
// for (int nextChildIndex = minChildIndex + 1; nextChildIndex < childUpperBound; nextChildIndex++)
// {
// ref HeapEntry nextChild = ref heap[nextChildIndex];
// if (_priorityComparer.Compare(nextChild.Priority, minChild.Priority) < 0)
// {
// minChildIndex = nextChildIndex;
// minChild = ref nextChild;
// }
// }
//
// // compare with inserted priority
// if (_priorityComparer.Compare(priority, minChild.Priority) <= 0)
// {
// // priority <= minChild, heap property is satisfied
// break;
// }
//
// heap[index] = minChild;
// index = minChildIndex;
// }
//
// ref HeapEntry entry = ref heap[index];
// entry.Element = element;
// entry.Priority = priority;
// }
//
// #if SIFTDOWN_EMPTY_NODES
// private void SiftDownHeapPropertyRequired(int index, in TElement element, in TPriority priority)
// {
// int emptyNodeIndex = SiftDownEmptyNode(index);
// SiftUp(emptyNodeIndex, in element, in priority);
// }
//
// private int SiftDownEmptyNode(int emptyNodeIndex)
// {
// int count = _count;
// int minChildIndex;
// HeapEntry[] heap = _heap;
//
// while ((minChildIndex = (emptyNodeIndex << 2) + 1) < count)
// {
// // find the child with the minimal priority
// ref HeapEntry minChild = ref heap[minChildIndex];
// int childUpperBound = Math.Min(count, minChildIndex + 4);
//
// for (int nextChildIndex = minChildIndex + 1; nextChildIndex < childUpperBound; nextChildIndex++)
// {
// ref HeapEntry nextChild = ref heap[nextChildIndex];
// if (_priorityComparer.Compare(nextChild.Priority, minChild.Priority) < 0)
// {
// minChildIndex = nextChildIndex;
// minChild = ref nextChild;
// }
// }
//
// heap[emptyNodeIndex] = minChild;
// emptyNodeIndex = minChildIndex;
// }
//
// return emptyNodeIndex;
// }
// #endif
//
// private void Resize(ref HeapEntry[] heap)
// {
// int newSize = heap.Length == 0 ? DefaultCapacity : 2 * heap.Length;
// Array.Resize(ref heap, newSize);
// }
//
// private struct HeapEntry
// {
// public TElement Element;
// public TPriority Priority;
//
// public void Deconstruct(out TElement element, out TPriority priority)
// {
// element = Element;
// priority = Priority;
// }
// }
//
// #if DEBUG
// public void ValidateInternalState()
// {
// if (_heap.Length < _count)
// {
// throw new Exception("invalid elements array length");
// }
//
// foreach ((var element, var idx) in _heap.Select((x, i) => (x.Element, i)).Skip(_count))
// {
// if (!IsDefault(element))
// {
// throw new Exception($"Non-zero element '{element}' at index {idx}.");
// }
// }
//
// foreach ((var priority, var idx) in _heap.Select((x, i) => (x.Priority, i)).Skip(_count))
// {
// if (!IsDefault(priority))
// {
// throw new Exception($"Non-zero priority '{priority}' at index {idx}.");
// }
// }
//
// static bool IsDefault<T>(T value)
// {
// T defaultVal = default;
//
// if (defaultVal is null)
// {
// return value is null;
// }
//
// return value!.Equals(defaultVal);
// }
// }
// #endif
// #endregion
// }
// }
// #endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 1e7e9292b9c27384d9aed58ea3d5098d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public sealed class ReuseList<T> : List<T>, IDisposable
{
private bool _isDispose;
public static ReuseList<T> Create()
{
var list = Pool<ReuseList<T>>.Rent();
list._isDispose = false;
return list;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<ReuseList<T>>.Return(this);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: fe34244ec94ec0240a92d11c66f9d94e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Linq;
#pragma warning disable CS8603
namespace TEngine.DataStructure
{
public class SortedConcurrentOneToManyListPool<TKey, TValue> : SortedConcurrentOneToManyList<TKey, TValue>,
IDisposable where TKey : notnull
{
private bool _isDispose;
public static SortedConcurrentOneToManyListPool<TKey, TValue> Create()
{
var a = Pool<SortedConcurrentOneToManyListPool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<SortedConcurrentOneToManyListPool<TKey, TValue>>.Return(this);
}
}
public class SortedConcurrentOneToManyList<TKey, TValue> : SortedDictionary<TKey, List<TValue>> where TKey : notnull
{
private readonly object _lockObject = new object();
private readonly Queue<List<TValue>> _queue = new Queue<List<TValue>>();
private readonly int _recyclingLimit;
public SortedConcurrentOneToManyList()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public SortedConcurrentOneToManyList(int recyclingLimit = 0)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
lock (_lockObject)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
}
public void Add(TKey key, TValue value)
{
lock (_lockObject)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Add(value);
base[key] = list;
return;
}
list.Add(value);
}
}
public TValue First(TKey key)
{
lock (_lockObject)
{
return !TryGetValue(key, out var list) ? default : list.FirstOrDefault();
}
}
public void RemoveValue(TKey key, TValue value)
{
lock (_lockObject)
{
if (!TryGetValue(key, out var list)) return;
list.Remove(value);
if (list.Count == 0) RemoveKey(key);
}
}
public void RemoveKey(TKey key)
{
lock (_lockObject)
{
if (!TryGetValue(key, out var list)) return;
Remove(key);
Recycle(list);
}
}
private List<TValue> Fetch()
{
lock (_lockObject)
{
return _queue.Count <= 0 ? new List<TValue>() : _queue.Dequeue();
}
}
private void Recycle(List<TValue> list)
{
lock (_lockObject)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(list);
}
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 24aa8bc8ac4d8584cb9ceb23a51c78fa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,111 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public class SortedOneToManyHashSetPool<TKey, TValue> : SortedOneToManyHashSet<TKey, TValue>, IDisposable
where TKey : notnull
{
private bool _isDispose;
public static SortedOneToManyHashSetPool<TKey, TValue> Create()
{
var a = Pool<SortedOneToManyHashSetPool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<SortedOneToManyHashSetPool<TKey, TValue>>.Return(this);
}
}
public class SortedOneToManyHashSet<TKey, TValue> : SortedDictionary<TKey, HashSet<TValue>> where TKey : notnull
{
private readonly Queue<HashSet<TValue>> _queue = new Queue<HashSet<TValue>>();
private readonly int _recyclingLimit = 120;
public SortedOneToManyHashSet()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public SortedOneToManyHashSet(int recyclingLimit)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
public void Add(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Add(value);
Add(key, list);
return;
}
list.Add(value);
}
public void RemoveValue(TKey key, TValue value)
{
if (!TryGetValue(key, out var list)) return;
list.Remove(value);
if (list.Count == 0) RemoveKey(key);
}
public void RemoveKey(TKey key)
{
if (!TryGetValue(key, out var list)) return;
Remove(key);
Recycle(list);
}
private HashSet<TValue> Fetch()
{
return _queue.Count <= 0 ? new HashSet<TValue>() : _queue.Dequeue();
}
private void Recycle(HashSet<TValue> list)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(list);
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ddf73601eadf11349b77b78a4d91f35a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
#pragma warning disable CS8603
namespace TEngine.DataStructure
{
public class SortedOneToManyListPool<TKey, TValue> : SortedOneToManyList<TKey, TValue>, IDisposable
where TKey : notnull
{
private bool _isDispose;
public static SortedOneToManyListPool<TKey, TValue> Create()
{
var a = Pool<SortedOneToManyListPool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<SortedOneToManyListPool<TKey, TValue>>.Return(this);
}
}
public class SortedOneToManyList<TKey, TValue> : SortedDictionary<TKey, List<TValue>> where TKey : notnull
{
private readonly Queue<List<TValue>> _queue = new Queue<List<TValue>>();
private readonly int _recyclingLimit;
public SortedOneToManyList()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public SortedOneToManyList(int recyclingLimit = 0)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValue value)
{
TryGetValue(key, out var list);
return list != null && list.Contains(value);
}
public void Add(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
list = Fetch();
list.Add(value);
base[key] = list;
return;
}
list.Add(value);
}
public TValue First(TKey key)
{
return !TryGetValue(key, out var list) ? default : list.FirstOrDefault();
}
public void RemoveValue(TKey key, TValue value)
{
if (!TryGetValue(key, out var list))
{
return;
}
list.Remove(value);
if (list.Count == 0)
{
RemoveKey(key);
}
}
public void RemoveKey(TKey key)
{
if (!TryGetValue(key, out var list))
{
return;
}
Remove(key);
Recycle(list);
}
private List<TValue> Fetch()
{
return _queue.Count <= 0 ? new List<TValue>() : _queue.Dequeue();
}
private void Recycle(List<TValue> list)
{
list.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit)
{
return;
}
_queue.Enqueue(list);
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: db62c1998de4d5048bde5d5e09d6f284
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3217a5df0b4fae04cad4694ae07948b8
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,19 @@
using System.Collections.Generic;
#pragma warning disable CS8601
namespace TEngine.DataStructure
{
public static class DictionaryExtensions
{
public static bool TryRemove<T, TV>(this IDictionary<T, TV> self, T key, out TV value)
{
if (!self.TryGetValue(key, out value))
{
return false;
}
self.Remove(key);
return true;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2906692879c7825458790e8cb128e7f2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public sealed class DictionaryPool<TM, TN> : Dictionary<TM, TN>, IDisposable where TM : notnull
{
private bool _isDispose;
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<DictionaryPool<TM, TN>>.Return(this);
}
public static DictionaryPool<TM, TN> Create()
{
var dictionary = Pool<DictionaryPool<TM, TN>>.Rent();
dictionary._isDispose = false;
return dictionary;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 45285f160861f9c4bb96634e744e2c74
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,174 @@
using System;
using System.Collections.Generic;
#pragma warning disable CS8601
#pragma warning disable CS8604
#pragma warning disable CS8603
namespace TEngine.DataStructure
{
public class DoubleMapDictionaryPool<TKey, TValue> : DoubleMapDictionary<TKey, TValue>, IDisposable
where TKey : notnull where TValue : notnull
{
private bool _isDispose;
public static DoubleMapDictionaryPool<TKey, TValue> Create()
{
var a = Pool<DoubleMapDictionaryPool<TKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<DoubleMapDictionaryPool<TKey, TValue>>.Return(this);
}
}
public class DoubleMapDictionary<TK, TV> where TK : notnull where TV : notnull
{
private readonly Dictionary<TK, TV> _kv = new Dictionary<TK, TV>();
private readonly Dictionary<TV, TK> _vk = new Dictionary<TV, TK>();
public DoubleMapDictionary()
{
}
public DoubleMapDictionary(int capacity)
{
_kv = new Dictionary<TK, TV>(capacity);
_vk = new Dictionary<TV, TK>(capacity);
}
public List<TK> Keys => new List<TK>(_kv.Keys);
public List<TV> Values => new List<TV>(_vk.Keys);
public void ForEach(Action<TK, TV> action)
{
if (action == null)
{
return;
}
var keys = _kv.Keys;
foreach (var key in keys)
{
action(key, _kv[key]);
}
}
public void Add(TK key, TV value)
{
if (key == null || value == null || _kv.ContainsKey(key) || _vk.ContainsKey(value))
{
return;
}
_kv.Add(key, value);
_vk.Add(value, key);
}
public TV GetValueByKey(TK key)
{
if (key != null && _kv.ContainsKey(key))
{
return _kv[key];
}
return default;
}
public bool TryGetValueByKey(TK key, out TV value)
{
var result = key != null && _kv.ContainsKey(key);
value = result ? _kv[key] : default;
return result;
}
public TK GetKeyByValue(TV value)
{
if (value != null && _vk.ContainsKey(value))
{
return _vk[value];
}
return default;
}
public bool TryGetKeyByValue(TV value, out TK key)
{
var result = value != null && _vk.ContainsKey(value);
key = result ? _vk[value] : default;
return result;
}
public void RemoveByKey(TK key)
{
if (key == null)
{
return;
}
if (!_kv.TryGetValue(key, out var value))
{
return;
}
_kv.Remove(key);
_vk.Remove(value);
}
public void RemoveByValue(TV value)
{
if (value == null)
{
return;
}
if (!_vk.TryGetValue(value, out var key))
{
return;
}
_kv.Remove(key);
_vk.Remove(value);
}
public void Clear()
{
_kv.Clear();
_vk.Clear();
}
public bool ContainsKey(TK key)
{
return key != null && _kv.ContainsKey(key);
}
public bool ContainsValue(TV value)
{
return value != null && _vk.ContainsKey(value);
}
public bool Contains(TK key, TV value)
{
if (key == null || value == null)
{
return false;
}
return _kv.ContainsKey(key) && _vk.ContainsKey(value);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e9b1abd0ffc450842bea6ec96e84425f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public sealed class EntityDictionary<TM, TN> : Dictionary<TM, TN>, IDisposable where TN : IDisposable where TM : notnull
{
private bool _isDispose;
public static EntityDictionary<TM, TN> Create()
{
var entityDictionary = Pool<EntityDictionary<TM, TN>>.Rent();
entityDictionary._isDispose = false;
return entityDictionary;
}
public new void Clear()
{
foreach (var keyValuePair in this)
{
keyValuePair.Value.Dispose();
}
base.Clear();
}
public void ClearNotDispose()
{
base.Clear();
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<EntityDictionary<TM, TN>>.Return(this);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3e08b2b0077e40143a1f772a0f7b1ad5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,147 @@
using System;
using System.Collections.Generic;
using System.Linq;
#pragma warning disable CS8603
#pragma warning disable CS8601
namespace TEngine.DataStructure
{
public class OneToManyDictionaryPool<TKey, TValueKey, TValue> : OneToManyDictionary<TKey, TValueKey, TValue>,
IDisposable where TKey : notnull where TValueKey : notnull
{
private bool _isDispose;
public static OneToManyDictionaryPool<TKey, TValueKey, TValue> Create()
{
var a = Pool<OneToManyDictionaryPool<TKey, TValueKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<OneToManyDictionaryPool<TKey, TValueKey, TValue>>.Return(this);
}
}
public class OneToManyDictionary<TKey, TValueKey, TValue> : Dictionary<TKey, Dictionary<TValueKey, TValue>>
where TKey : notnull where TValueKey : notnull
{
private readonly Queue<Dictionary<TValueKey, TValue>> _queue = new Queue<Dictionary<TValueKey, TValue>>();
private readonly int _recyclingLimit = 120;
public OneToManyDictionary()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public OneToManyDictionary(int recyclingLimit = 0)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key, TValueKey valueKey)
{
TryGetValue(key, out var dic);
return dic != null && dic.ContainsKey(valueKey);
}
public bool TryGetValue(TKey key, TValueKey valueKey, out TValue value)
{
value = default;
return TryGetValue(key, out var dic) && dic.TryGetValue(valueKey, out value);
}
public TValue First(TKey key)
{
return !TryGetValue(key, out var dic) ? default : dic.First().Value;
}
public void Add(TKey key, TValueKey valueKey, TValue value)
{
if (!TryGetValue(key, out var dic))
{
dic = Fetch();
dic[valueKey] = value;
// dic.Add(valueKey, value);
Add(key, dic);
return;
}
dic[valueKey] = value;
// dic.Add(valueKey, value);
}
public bool Remove(TKey key, TValueKey valueKey)
{
if (!TryGetValue(key, out var dic)) return false;
var result = dic.Remove(valueKey);
if (dic.Count == 0) RemoveKey(key);
return result;
}
public bool Remove(TKey key, TValueKey valueKey, out TValue value)
{
if (!TryGetValue(key, out var dic))
{
value = default;
return false;
}
var result = dic.TryGetValue(valueKey, out value);
if (result) dic.Remove(valueKey);
if (dic.Count == 0) RemoveKey(key);
return result;
}
public void RemoveKey(TKey key)
{
if (!TryGetValue(key, out var dic)) return;
Remove(key);
Recycle(dic);
}
private Dictionary<TValueKey, TValue> Fetch()
{
return _queue.Count <= 0 ? new Dictionary<TValueKey, TValue>() : _queue.Dequeue();
}
private void Recycle(Dictionary<TValueKey, TValue> dic)
{
dic.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit) return;
_queue.Enqueue(dic);
}
public new void Clear()
{
foreach (var keyValuePair in this) Recycle(keyValuePair.Value);
base.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5db437bfa1f9b8b438b7260196e01934
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,150 @@
using System;
using System.Collections.Generic;
#pragma warning disable CS8601
namespace TEngine.DataStructure
{
public class
OneToManySortedDictionaryPool<TKey, TSortedKey, TValue> : OneToManySortedDictionary<TKey, TSortedKey, TValue>,
IDisposable where TKey : notnull where TSortedKey : notnull
{
private bool _isDispose;
public static OneToManySortedDictionaryPool<TKey, TSortedKey, TValue> Create()
{
var a = Pool<OneToManySortedDictionaryPool<TKey, TSortedKey, TValue>>.Rent();
a._isDispose = false;
return a;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<OneToManySortedDictionaryPool<TKey, TSortedKey, TValue>>.Return(this);
}
}
public class
OneToManySortedDictionary<TKey, TSortedKey, TValue> : Dictionary<TKey, SortedDictionary<TSortedKey, TValue>>
where TSortedKey : notnull where TKey : notnull
{
private readonly int _recyclingLimit = 120;
private readonly Queue<SortedDictionary<TSortedKey, TValue>> _queue =
new Queue<SortedDictionary<TSortedKey, TValue>>();
protected OneToManySortedDictionary()
{
}
/// <summary>
/// 设置最大缓存数量
/// </summary>
/// <param name="recyclingLimit">
/// 1:防止数据量过大、所以超过recyclingLimit的数据还是走GC.
/// 2:设置成0不控制数量全部缓存
/// </param>
public OneToManySortedDictionary(int recyclingLimit)
{
_recyclingLimit = recyclingLimit;
}
public bool Contains(TKey key)
{
return this.ContainsKey(key);
}
public bool Contains(TKey key, TSortedKey sortedKey)
{
return TryGetValue(key, out var dic) && dic.ContainsKey(sortedKey);
}
public new bool TryGetValue(TKey key, out SortedDictionary<TSortedKey, TValue> dic)
{
return base.TryGetValue(key, out dic);
}
public bool TryGetValueBySortedKey(TKey key, TSortedKey sortedKey, out TValue value)
{
if (base.TryGetValue(key, out var dic))
{
return dic.TryGetValue(sortedKey, out value);
}
value = default;
return false;
}
public void Add(TKey key, TSortedKey sortedKey, TValue value)
{
if (!TryGetValue(key, out var dic))
{
dic = Fetch();
dic.Add(sortedKey, value);
Add(key, dic);
return;
}
dic.Add(sortedKey, value);
}
public bool RemoveSortedKey(TKey key, TSortedKey sortedKey)
{
if (!TryGetValue(key, out var dic))
{
return false;
}
var isRemove = dic.Remove(sortedKey);
if (dic.Count == 0)
{
isRemove = RemoveKey(key);
}
return isRemove;
}
public bool RemoveKey(TKey key)
{
if (!TryGetValue(key, out var list))
{
return false;
}
Remove(key);
Recycle(list);
return true;
}
private SortedDictionary<TSortedKey, TValue> Fetch()
{
return _queue.Count <= 0 ? new SortedDictionary<TSortedKey, TValue>() : _queue.Dequeue();
}
private void Recycle(SortedDictionary<TSortedKey, TValue> dic)
{
dic.Clear();
if (_recyclingLimit != 0 && _queue.Count > _recyclingLimit)
{
return;
}
_queue.Enqueue(dic);
}
protected new void Clear()
{
base.Clear();
_queue.Clear();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c0ac9ed7abbf4e4408901274ee344642
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
namespace TEngine.DataStructure
{
public sealed class ReuseDictionary<TM, TN> : Dictionary<TM, TN>, IDisposable where TM : notnull
{
private bool _isDispose;
public static ReuseDictionary<TM, TN> Create()
{
var entityDictionary = Pool<ReuseDictionary<TM, TN>>.Rent();
entityDictionary._isDispose = false;
return entityDictionary;
}
public void Dispose()
{
if (_isDispose)
{
return;
}
_isDispose = true;
Clear();
Pool<ReuseDictionary<TM, TN>>.Return(this);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bf01018dd07bd2243b8636a1321f50c5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: a7e0baed1cdd57b4e916270c39382710
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,166 @@
#pragma warning disable CS8602
#pragma warning disable CS8601
#pragma warning disable CS8625
#pragma warning disable CS8604
#pragma warning disable CS8600
namespace TEngine.DataStructure
{
/// <summary>
/// 跳表升序版
/// </summary>
/// <typeparam name="TValue"></typeparam>
public class SkipTable<TValue> : SkipTableBase<TValue>
{
public SkipTable(int maxLayer = 8) : base(maxLayer) { }
public override void Add(long sortKey, long viceKey, long key, TValue value)
{
var rLevel = 1;
while (rLevel <= MaxLayer && Random.Next(3) == 0)
{
++rLevel;
}
SkipTableNode<TValue> cur = TopHeader, last = null;
for (var layer = MaxLayer; layer >= 1; --layer)
{
// 节点有next节点next主键 < 插入主键) 或 next主键 == 插入主键 且 next副键 < 插入副键)
while (cur.Right != null && ((cur.Right.SortKey < sortKey) ||
(cur.Right.SortKey == sortKey && cur.Right.ViceKey < viceKey)))
{
cur = cur.Right;
}
if (layer <= rLevel)
{
var currentRight = cur.Right;
cur.Right = new SkipTableNode<TValue>(sortKey, viceKey, key, value, layer == 1 ? cur.Index + 1 : 0, cur, cur.Right, null);
if (currentRight != null)
{
currentRight.Left = cur.Right;
}
if (last != null)
{
last.Down = cur.Right;
}
if (layer == 1)
{
cur.Right.Index = cur.Index + 1;
Node.Add(key, cur.Right);
SkipTableNode<TValue> v = cur.Right.Right;
while (v != null)
{
v.Index++;
v = v.Right;
}
}
last = cur.Right;
}
cur = cur.Down;
}
}
public override bool Remove(long sortKey, long viceKey, long key, out TValue value)
{
value = default;
var seen = false;
var cur = TopHeader;
for (var layer = MaxLayer; layer >= 1; --layer)
{
// 先按照主键查找 再 按副键查找
while (cur.Right != null && cur.Right.SortKey < sortKey && cur.Right.Key != key) cur = cur.Right;
while (cur.Right != null && (cur.Right.SortKey == sortKey && cur.Right.ViceKey <= viceKey) &&
cur.Right.Key != key) cur = cur.Right;
var isFind = false;
var currentCur = cur;
SkipTableNode<TValue> removeCur = null;
// 如果当前不是要删除的节点、但主键和副键都一样、需要特殊处理下。
if (cur.Right != null && cur.Right.Key == key)
{
isFind = true;
removeCur = cur.Right;
currentCur = cur;
}
else
{
// 先向左查找下
var currentNode = cur.Left;
while (currentNode != null && currentNode.SortKey == sortKey && currentNode.ViceKey == viceKey)
{
if (currentNode.Key == key)
{
isFind = true;
removeCur = currentNode;
currentCur = currentNode.Left;
break;
}
currentNode = currentNode.Left;
}
// 再向右查找下
if (!isFind)
{
currentNode = cur.Right;
while (currentNode != null && currentNode.SortKey == sortKey && currentNode.ViceKey == viceKey)
{
if (currentNode.Key == key)
{
isFind = true;
removeCur = currentNode;
currentCur = currentNode.Left;
break;
}
currentNode = currentNode.Right;
}
}
}
if (isFind && currentCur != null)
{
value = removeCur.Value;
currentCur.Right = removeCur.Right;
if (removeCur.Right != null)
{
removeCur.Right.Left = currentCur;
removeCur.Right = null;
}
removeCur.Left = null;
removeCur.Down = null;
removeCur.Value = default;
if (layer == 1)
{
var tempCur = currentCur.Right;
while (tempCur != null)
{
tempCur.Index--;
tempCur = tempCur.Right;
}
Node.Remove(removeCur.Key);
}
seen = true;
}
cur = cur.Down;
}
return seen;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 94af5a03823f82246a24b4e5dfdc2be5
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,181 @@
using System;
using System.Collections;
using System.Collections.Generic;
#pragma warning disable CS8601
#pragma warning disable CS8603
#pragma warning disable CS8625
#pragma warning disable CS8604
namespace TEngine.DataStructure
{
public abstract class SkipTableBase<TValue> : IEnumerable<SkipTableNode<TValue>>
{
public readonly int MaxLayer;
public readonly SkipTableNode<TValue> TopHeader;
public SkipTableNode<TValue> BottomHeader;
public int Count => Node.Count;
protected readonly Random Random = new Random();
protected readonly Dictionary<long, SkipTableNode<TValue>> Node = new();
protected readonly Stack<SkipTableNode<TValue>> AntiFindStack = new Stack<SkipTableNode<TValue>>();
protected SkipTableBase(int maxLayer = 8)
{
MaxLayer = maxLayer;
var cur = TopHeader = new SkipTableNode<TValue>(long.MinValue, 0, 0, default, 0, null, null, null);
for (var layer = MaxLayer - 1; layer >= 1; --layer)
{
cur.Down = new SkipTableNode<TValue>(long.MinValue, 0, 0, default, 0, null, null, null);
cur = cur.Down;
}
BottomHeader = cur;
}
public TValue this[long key] => !TryGetValueByKey(key, out TValue value) ? default : value;
public int GetRanking(long key)
{
if (!Node.TryGetValue(key, out var node))
{
return 0;
}
return node.Index;
}
public int GetAntiRanking(long key)
{
var ranking = GetRanking(key);
if (ranking == 0)
{
return 0;
}
return Count + 1 - ranking;
}
public bool TryGetValueByKey(long key, out TValue value)
{
if (!Node.TryGetValue(key, out var node))
{
value = default;
return false;
}
value = node.Value;
return true;
}
public bool TryGetNodeByKey(long key, out SkipTableNode<TValue> node)
{
if (Node.TryGetValue(key, out node))
{
return true;
}
return false;
}
public void Find(int start, int end, ListPool<SkipTableNode<TValue>> list)
{
var cur = BottomHeader;
var count = end - start;
for (var i = 0; i < start; i++)
{
cur = cur.Right;
}
for (var i = 0; i <= count; i++)
{
if (cur == null)
{
break;
}
list.Add(cur);
cur = cur.Right;
}
}
public void AntiFind(int start, int end, ListPool<SkipTableNode<TValue>> list)
{
var cur = BottomHeader;
start = Count + 1 - start;
end = start - end;
for (var i = 0; i < start; i++)
{
cur = cur.Right;
if (cur == null)
{
break;
}
if (i < end)
{
continue;
}
AntiFindStack.Push(cur);
}
while (AntiFindStack.TryPop(out var node))
{
list.Add(node);
}
}
public TValue GetLastValue()
{
var cur = TopHeader;
while (cur.Right != null || cur.Down != null)
{
while (cur.Right != null)
{
cur = cur.Right;
}
if (cur.Down != null)
{
cur = cur.Down;
}
}
return cur.Value;
}
public bool Remove(long key)
{
if (!Node.TryGetValue(key, out var node))
{
return false;
}
return Remove(node.SortKey, node.ViceKey, key, out _);
}
public abstract void Add(long sortKey, long viceKey, long key, TValue value);
public abstract bool Remove(long sortKey, long viceKey, long key, out TValue value);
public IEnumerator<SkipTableNode<TValue>> GetEnumerator()
{
var cur = BottomHeader.Right;
while (cur != null)
{
yield return cur;
cur = cur.Right;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c3dacacc147f7c8499e328284a0542ef
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,166 @@
#pragma warning disable CS8602
#pragma warning disable CS8601
#pragma warning disable CS8625
#pragma warning disable CS8604
#pragma warning disable CS8600
namespace TEngine.DataStructure
{
/// <summary>
/// 跳表降序版
/// </summary>
/// <typeparam name="TValue"></typeparam>
public class SkipTableDesc<TValue> : SkipTableBase<TValue>
{
public SkipTableDesc(int maxLayer = 8) : base(maxLayer) { }
public override void Add(long sortKey, long viceKey, long key, TValue value)
{
var rLevel = 1;
while (rLevel <= MaxLayer && Random.Next(3) == 0)
{
++rLevel;
}
SkipTableNode<TValue> cur = TopHeader, last = null;
for (var layer = MaxLayer; layer >= 1; --layer)
{
// 节点有next节点next主键 > 插入主键) 或 next主键 == 插入主键 且 next副键 > 插入副键)
while (cur.Right != null && ((cur.Right.SortKey > sortKey) ||
(cur.Right.SortKey == sortKey && cur.Right.ViceKey > viceKey)))
{
cur = cur.Right;
}
if (layer <= rLevel)
{
var currentRight = cur.Right;
cur.Right = new SkipTableNode<TValue>(sortKey, viceKey, key, value,
layer == 1 ? cur.Index + 1 : 0, cur, cur.Right, null);
if (currentRight != null)
{
currentRight.Left = cur.Right;
}
if (last != null)
{
last.Down = cur.Right;
}
if (layer == 1)
{
cur.Right.Index = cur.Index + 1;
Node.Add(key, cur.Right);
SkipTableNode<TValue> v = cur.Right.Right;
while (v != null)
{
v.Index++;
v = v.Right;
}
}
last = cur.Right;
}
cur = cur.Down;
}
}
public override bool Remove(long sortKey, long viceKey, long key, out TValue value)
{
value = default;
var seen = false;
var cur = TopHeader;
for (var layer = MaxLayer; layer >= 1; --layer)
{
// 先按照主键查找 再 按副键查找
while (cur.Right != null && cur.Right.SortKey > sortKey && cur.Right.Key != key) cur = cur.Right;
while (cur.Right != null && (cur.Right.SortKey == sortKey && cur.Right.ViceKey >= viceKey) &&
cur.Right.Key != key) cur = cur.Right;
var isFind = false;
var currentCur = cur;
SkipTableNode<TValue> removeCur = null;
// 如果当前不是要删除的节点、但主键和副键都一样、需要特殊处理下。
if (cur.Right != null && cur.Right.Key == key)
{
isFind = true;
removeCur = cur.Right;
currentCur = cur;
}
else
{
// 先向左查找下
var currentNode = cur.Left;
while (currentNode != null && currentNode.SortKey == sortKey && currentNode.ViceKey == viceKey)
{
if (currentNode.Key == key)
{
isFind = true;
removeCur = currentNode;
currentCur = currentNode.Left;
break;
}
currentNode = currentNode.Left;
}
// 再向右查找下
if (!isFind)
{
currentNode = cur.Right;
while (currentNode != null && currentNode.SortKey == sortKey && currentNode.ViceKey == viceKey)
{
if (currentNode.Key == key)
{
isFind = true;
removeCur = currentNode;
currentCur = currentNode.Left;
break;
}
currentNode = currentNode.Right;
}
}
}
if (isFind && currentCur != null)
{
value = removeCur.Value;
currentCur.Right = removeCur.Right;
if (removeCur.Right != null)
{
removeCur.Right.Left = currentCur;
removeCur.Right = null;
}
removeCur.Left = null;
removeCur.Down = null;
removeCur.Value = default;
if (layer == 1)
{
var tempCur = currentCur.Right;
while (tempCur != null)
{
tempCur.Index--;
tempCur = tempCur.Right;
}
Node.Remove(removeCur.Key);
}
seen = true;
}
cur = cur.Down;
}
return seen;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5dd291e3d391b8842a15ea93f72e4771
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,29 @@
namespace TEngine.DataStructure
{
public class SkipTableNode<TValue>
{
public int Index;
public long Key;
public long SortKey;
public long ViceKey;
public TValue Value;
public SkipTableNode<TValue> Left;
public SkipTableNode<TValue> Right;
public SkipTableNode<TValue> Down;
public SkipTableNode(long sortKey, long viceKey, long key, TValue value, int index,
SkipTableNode<TValue> l,
SkipTableNode<TValue> r,
SkipTableNode<TValue> d)
{
Left = l;
Right = r;
Down = d;
Value = value;
Key = key;
Index = index;
SortKey = sortKey;
ViceKey = viceKey;
}
}
}

Some files were not shown because too many files have changed in this diff Show More