更新最新框架

This commit is contained in:
Bob.Song
2025-11-11 17:43:11 +08:00
parent 7b10d4cb31
commit fd3c6ec38d
1048 changed files with 11041 additions and 87002 deletions

View File

@@ -1,187 +1,224 @@
using System.Diagnostics;
using Fantasy;
using Fantasy.Async;
using Fantasy.Entitas;
using Fantasy.Event;
using Fantasy.Helper;
using Fantasy.Serialize;
using NB;
using NB.Authentication;
using NB.Chat;
using NB.Game;
using NB.Gate;
using NB.Map;
using NBF;
using ProtoBuf;
namespace NB;
public sealed class SubSceneTestComponent : Entity
{
public override void Dispose()
{
Log.Debug("销毁SubScene下的SubSceneTestComponent");
base.Dispose();
}
}
namespace NBF;
public sealed class OnCreateSceneEvent : AsyncEventSystem<OnCreateScene>
{
private static long _addressableSceneRunTimeId;
private static long _addressableSceneRuntimeId;
/// <summary>
/// Handles the OnCreateScene event.
/// </summary>
/// <param name="self">The OnCreateScene object.</param>
/// <returns>A task representing the asynchronous operation.</returns>
protected override async FTask Handler(OnCreateScene self)
{
// var epoch1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000;
//
// {
// var now = TimeHelper.Transition(new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc));
// var epochThisYear = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000 - epoch1970;
// var time = (uint)((now - epochThisYear) / 1000);
// Log.Debug($"time = {time} now = {now} epochThisYear = {epochThisYear}");
// }
//
// {
// var now = TimeHelper.Transition(new DateTime(2025, 1, 1, 0, 0, 0, DateTimeKind.Utc));
// var epochThisYear = new DateTime(2024, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000 - epoch1970;
// var time = (uint)((now - epochThisYear) / 1000);
// Log.Debug($"time = {time} now = {now} epochThisYear = {epochThisYear}");
// }
var scene = self.Scene;
// 在这里执行你的初始化逻辑
Log.Info($"Scene created: SceneType={scene.SceneType}, SceneId={scene.Id} SceneConfigId={scene.SceneConfigId}");
switch (scene.SceneType)
{
case 6666:
{
var subSceneTestComponent = scene.AddComponent<SubSceneTestComponent>();
Log.Debug("增加了SubSceneTestComponent");
scene.EntityComponent.CustomSystem(subSceneTestComponent, CustomSystemType.RunSystem);
break;
}
case SceneType.Addressable:
{
// scene.AddComponent<AddressableManageComponent>();
_addressableSceneRunTimeId = scene.RuntimeId;
// 保存 Addressable 场景的 RuntimeId,供其他场景使用
_addressableSceneRuntimeId = scene.RuntimeId;
await InitializeAddressableScene(scene);
break;
}
case SceneType.Map:
case SceneType.Authentication:
{
Log.Debug($"Map Scene SceneRuntimeId:{scene.RuntimeId}");
var roomCode = RoomHelper.GenerateCode(scene.SceneConfigId, 1);
Log.Info($"测试 roomCode{roomCode}");
// uint serverId = 25255;
// for (int i = 1; i < 65535; i++)
// {
// var roomId = RoomHelper.GenerateCode(serverId, i);
// RoomHelper.ParseCode(roomId, out var pId, out var rId);
// Log.Info($"生成id测试房间id={roomId} 原始服务id{serverId} 原始房间id={i} 解析={pId} {rId}");
// }
break;
}
case SceneType.Social:
{
break;
}
case SceneType.Game:
{
var rod = RodConfig.Get(30001);
Log.Info("rod config id="+rod.Id);
// // Begins transaction
// using (var session = mongoClient.StartSession())
// {
// session.StartTransaction();
// try
// {
// // Creates sample data
// var book = new Book
// {
// Title = "Beloved",
// Author = "Toni Morrison",
// InStock = true
// };
// var film = new Film
// {
// Title = "Star Wars",
// Director = "George Lucas",
// InStock = true
// };
// // Inserts sample data
// books.InsertOne(session, book);
// films.InsertOne(session, film);
// // Commits our transaction
// session.CommitTransaction();
// }
// catch (Exception e)
// {
// Console.WriteLine("Error writing to MongoDB: " + e.Message);
// return;
// }
// // Prints a success message if no error thrown
// Console.WriteLine("Successfully committed transaction!");
// }
// var client = self.Scene.World.DataBase;
// List<FTask> tasks = new List<FTask>();
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();
// for (int i = 0; i < 100; i++)
// {
// var accountId = scene.EntityIdFactory.Create;
// var account = PlayerFactory.Create(scene, accountId);
//
// account.Level = 99;
// account.NickName = $"测试号{i + 1}";
// account.Country = "cn";
// account.Exp = 999;
// account.Head = "xxx.png";
// tasks.Add(account.Save());
// }
// await FTask.WaitAll(tasks);
// // self.Scene.World.DataBase.InsertBatch()
//
// stopwatch.Stop();
// Log.Info($"创建100个号入库耗时={stopwatch.ElapsedMilliseconds}ms");
await InitializeAuthenticationScene(scene);
break;
}
case SceneType.Gate:
{
// var tasks = new List<FTask>(2000);
// var session = scene.GetSession(_addressableSceneRunTimeId);
// var sceneNetworkMessagingComponent = scene.NetworkMessagingComponent;
// var g2ATestRequest = new G2A_TestRequest();
//
// async FTask Call()
// {
// await sceneNetworkMessagingComponent.CallInnerRouteBySession(session,_addressableSceneRunTimeId,g2ATestRequest);
// }
//
// for (int i = 0; i < 100000000000; i++)
// {
// tasks.Clear();
// for (int j = 0; j < tasks.Capacity; ++j)
// {
// tasks.Add(Call());
// }
// await FTask.WaitAll(tasks);
// }
// 执行自定义系统
var testCustomSystemComponent = scene.AddComponent<TestCustomSystemComponent>();
scene.EntityComponent.CustomSystem(testCustomSystemComponent, CustomSystemType.RunSystem);
// // 测试配置表
// var instanceList = UnitConfigData.Instance.List;
// var unitConfig = instanceList[0];
// Log.Debug(instanceList[0].Dic[1]);
// Gate 场景初始化
await InitializeGateScene(scene);
break;
}
case SceneType.Map:
{
await InitializeMapScene(scene);
break;
}
case SceneType.Social:
{
// Gate 场景初始化
await InitializeSocialScene(scene);
break;
}
case SceneType.Game:
{
await InitializeGameScene(scene);
break;
}
}
await FTask.CompletedTask;
}
private async FTask InitializeAuthenticationScene(Scene scene)
{
// 用于鉴权服务器注册和登录相关逻辑的组件
scene.AddComponent<AuthenticationComponent>().UpdatePosition();
// 用于颁发ToKen证书相关的逻辑。
scene.AddComponent<AuthenticationJwtComponent>();
await FTask.CompletedTask;
}
private async FTask InitializeGateScene(Scene scene)
{
// Gate 场景特定的初始化逻辑
Log.Info($"初始化 Gate 场景: {scene.Id}");
// 用于管理网关所有连接的组件
scene.AddComponent<GateUnitManageComponent>();
// 用于验证JWT是否合法的组件
scene.AddComponent<GateJWTComponent>();
await FTask.CompletedTask;
// var roomCode = RoomHelper.GenerateCode(scene.SceneConfigId, 1);
// Log.Info($"测试 roomCode{roomCode}");
// uint serverId = 25255;
// for (int i = 1; i < 65535; i++)
// {
// var roomId = RoomHelper.GenerateCode(serverId, i);
// RoomHelper.ParseCode(roomId, out var pId, out var rId);
// Log.Info($"生成id测试房间id={roomId} 原始服务id{serverId} 原始房间id={i} 解析={pId} {rId}");
// }
// var tasks = new List<FTask>(2000);
// var session = scene.GetSession(_addressableSceneRunTimeId);
// var sceneNetworkMessagingComponent = scene.NetworkMessagingComponent;
// var g2ATestRequest = new G2A_TestRequest();
//
// async FTask Call()
// {
// await sceneNetworkMessagingComponent.CallInnerRouteBySession(session,_addressableSceneRunTimeId,g2ATestRequest);
// }
//
// for (int i = 0; i < 100000000000; i++)
// {
// tasks.Clear();
// for (int j = 0; j < tasks.Capacity; ++j)
// {
// tasks.Add(Call());
// }
// await FTask.WaitAll(tasks);
// }
// // 测试配置表
// var instanceList = UnitConfigData.Instance.List;
// var unitConfig = instanceList[0];
// Log.Debug(instanceList[0].Dic[1]);
}
private async FTask InitializeMapScene(Scene scene)
{
// Map 场景特定的初始化逻辑
Log.Info($"初始化 Map 场景: {scene.Id}");
Log.Info("创建地图场景===");
scene.AddComponent<MapUnitManageComponent>();
var roomManageComponent = scene.AddComponent<RoomManageComponent>();
scene.AddComponent<MapManageComponent>();
var room = roomManageComponent.Create(361499030775398402);
if (room != null)
{
roomManageComponent.TestRoomCode = room.Code;
Log.Info($"测试房间代码 = {room.Code}");
}
await FTask.CompletedTask;
}
private async FTask InitializeSocialScene(Scene scene)
{
Log.Info($"初始化 Social 场景: {scene.Id}");
//用于管理玩家的组件
scene.AddComponent<SocialUnitManageComponent>();
scene.AddComponent<ChatChannelCenterComponent>();
scene.AddComponent<MailManageComponent>();
await FTask.CompletedTask;
}
private async FTask InitializeGameScene(Scene scene)
{
// Chat 场景特定的初始化逻辑
Log.Info($"初始化 Game 场景: {scene.Id}");
//用于管理玩家的组件
scene.AddComponent<PlayerManageComponent>();
scene.AddComponent<PlayerBasicCacheManageComponent>();
await FTask.CompletedTask;
// var rod = RodConfig.Get(30001);
// Log.Info("rod config id=" + rod.Id);
// // Begins transaction
// using (var session = mongoClient.StartSession())
// {
// session.StartTransaction();
// try
// {
// // Creates sample data
// var book = new Book
// {
// Title = "Beloved",
// Author = "Toni Morrison",
// InStock = true
// };
// var film = new Film
// {
// Title = "Star Wars",
// Director = "George Lucas",
// InStock = true
// };
// // Inserts sample data
// books.InsertOne(session, book);
// films.InsertOne(session, film);
// // Commits our transaction
// session.CommitTransaction();
// }
// catch (Exception e)
// {
// Console.WriteLine("Error writing to MongoDB: " + e.Message);
// return;
// }
// // Prints a success message if no error thrown
// Console.WriteLine("Successfully committed transaction!");
// }
// var client = self.Scene.World.Database;
// List<FTask> tasks = new List<FTask>();
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();
// for (int i = 0; i < 100; i++)
// {
// var accountId = scene.EntityIdFactory.Create;
// var account = PlayerFactory.Create(scene, accountId);
//
// account.Level = 99;
// account.NickName = $"测试号{i + 1}";
// account.Country = "cn";
// account.Exp = 999;
// account.Head = "xxx.png";
// tasks.Add(account.Save());
// }
// await FTask.WaitAll(tasks);
// // self.Scene.World.Database.InsertBatch()
//
// stopwatch.Stop();
// Log.Info($"创建100个号入库耗时={stopwatch.ElapsedMilliseconds}ms");
}
private async FTask InitializeAddressableScene(Scene scene)
{
// Addressable 场景特定的初始化逻辑
Log.Info($"初始化 Addressable 场景: {scene.Id}");
await FTask.CompletedTask;
}
}