合理化服务器命名,使开发者更容易理解。

Scene的routeId更名为locationId
This commit is contained in:
ALEXTANGXIAO
2023-07-27 00:33:39 +08:00
parent d1c93f15d6
commit b7b2262d53
19 changed files with 134 additions and 150 deletions

View File

@@ -194,14 +194,14 @@ namespace TEngine
public static T Create<T>(Scene scene, bool isRunEvent = true) where T : Entity, new()
{
var entity = Create<T>(scene.RouteId, isRunEvent);
var entity = Create<T>(scene.LocationId, isRunEvent);
entity.Scene = scene;
return entity;
}
public static T Create<T>(Scene scene, long id, bool isRunEvent = true) where T : Entity, new()
{
var entity = Create<T>(id, scene.RouteId, isRunEvent);
var entity = Create<T>(id, scene.LocationId, isRunEvent);
entity.Scene = scene;
return entity;
}
@@ -235,12 +235,12 @@ namespace TEngine
return entity;
}
private static T Create<T>(long id, uint routeId, bool isRunEvent = true) where T : Entity, new()
protected static T Create<T>(long id, uint locationId, bool isRunEvent = true) where T : Entity, new()
{
return Create<T>(id, IdFactory.NextEntityId(routeId), isRunEvent);
return Create<T>(id, IdFactory.NextEntityId(locationId), isRunEvent);
}
private static T Create<T>(long id, long runtimeId, bool isRunEvent = true) where T : Entity, new()
protected static T Create<T>(long id, long runtimeId, bool isRunEvent = true) where T : Entity, new()
{
var entity = Rent<T>(typeof(T));
entity.Id = id;
@@ -264,13 +264,6 @@ namespace TEngine
return entity;
}
protected static Scene CreateScene(long id, bool isRunEvent = true)
{
var entity = Create<Scene>(id, id, isRunEvent);
entity.Scene = entity;
return entity;
}
#endregion
#region Members
@@ -293,12 +286,12 @@ namespace TEngine
[BsonIgnore]
[JsonIgnore]
[IgnoreDataMember]
public Scene Scene { get; private set; }
public Scene Scene { get; protected set; }
[BsonIgnore]
[JsonIgnore]
[IgnoreDataMember]
public Entity Parent { get; private set; }
public Entity Parent { get; protected set; }
[BsonElement("t")]
[BsonIgnoreIfNull]
@@ -321,7 +314,7 @@ namespace TEngine
public T AddComponent<T>() where T : Entity, new()
{
var entity = Create<T>(Id, Scene.RouteId, false);
var entity = Create<T>(Id, Scene.LocationId, false);
AddComponent(entity);
EntitiesSystem.Instance.Awake(entity);
EntitiesSystem.Instance.StartUpdate(entity);
@@ -330,7 +323,7 @@ namespace TEngine
public T AddComponent<T>(long id) where T : Entity, new()
{
var entity = Create<T>(id, Scene.RouteId, false);
var entity = Create<T>(id, Scene.LocationId, false);
AddComponent(entity);
EntitiesSystem.Instance.Awake(entity);
EntitiesSystem.Instance.StartUpdate(entity);
@@ -538,7 +531,7 @@ namespace TEngine
{
Scene = scene;
#if TENGINE_NET
RuntimeId = IdFactory.NextEntityId(scene.RouteId);
RuntimeId = IdFactory.NextEntityId(scene.LocationId);
#else
RuntimeId = IdFactory.NextRunTimeId();
#endif

View File

@@ -12,16 +12,15 @@ namespace TEngine
public sealed class Scene : Entity, INotSupportedPool
{
public string Name { get; private set; }
public uint RouteId { get; private set; }
public uint LocationId { get; private set; }
#if TENGINE_UNITY
public Session Session { get; private set; }
public SceneConfigInfo SceneInfo { get; private set; }
#endif
#if TENGINE_NET
public string SceneType { get; private set; }
public World World { get; private set; }
public Server Server { get; private set; }
public uint SceneConfigId { get; private set; }
public SceneConfigInfo SceneInfo => ConfigTableManage.SceneConfig(SceneConfigId);
#endif
public static readonly List<Scene> Scenes = new List<Scene>();
@@ -33,11 +32,11 @@ namespace TEngine
}
Name = null;
RouteId = 0;
this.LocationId = 0;
#if TENGINE_NET
World = null;
Server = null;
SceneConfigId = 0;
SceneType = null;
#endif
#if TENGINE_UNITY
SceneInfo = null;
@@ -61,8 +60,10 @@ namespace TEngine
#if TENGINE_UNITY
public static Scene Create(string name)
{
var runTimeId = IdFactory.NextRunTimeId();
var scene = CreateScene(runTimeId);
var sceneId = IdFactory.NextRunTimeId();
var scene = Create<Scene>(sceneId, sceneId);
scene.Scene = scene;
scene.Parent = scene;
scene.Name = name;
Scenes.Add(scene);
return scene;
@@ -77,41 +78,81 @@ namespace TEngine
}
#else
/// <summary>
/// 创建一个Scene
/// 创建一个Scene、但这个Scene是在某个Scene下面的Scene
/// </summary>
/// <param name="scene"></param>
/// <param name="sceneName"></param>
/// <param name="sceneType"></param>
/// <returns></returns>
public static async FTask<Scene> Create(Scene scene, string sceneType, string sceneName)
{
var newScene = Create<Scene>(scene);
newScene.Scene = scene;
newScene.Parent = scene;
newScene.Name = sceneName;
newScene.SceneType = sceneType;
newScene.Server = scene.Server;
newScene.LocationId = scene.Server.Id;
if (scene.World !=null)
{
newScene.World = scene.World;
}
if (!string.IsNullOrEmpty(sceneType))
{
await EventSystem.Instance.PublishAsync(new OnCreateScene(scene));
}
Scenes.Add(scene);
return scene;
}
/// <summary>
/// 创建一个Scene。
/// </summary>
/// <param name="server"></param>
/// <param name="sceneType"></param>
/// <param name="sceneName"></param>
/// <param name="sceneId"></param>
/// <param name="worldId"></param>
/// <param name="networkProtocol"></param>
/// <param name="outerBindIp"></param>
/// <param name="sceneInfo"></param>
/// <param name="runEvent"></param>
/// <param name="onSetNetworkComplete"></param>
/// <param name="outerPort"></param>
/// <returns></returns>
public static async FTask<Scene> Create(Server server, string outerBindIp, SceneConfigInfo sceneInfo, Action<Session> onSetNetworkComplete = null, bool runEvent = true)
public static async FTask<Scene> Create(Server server, string sceneType, string sceneName, long sceneId =0, uint worldId =0, string networkProtocol = null, string outerBindIp = null, int outerPort = 0)
{
var scene = CreateScene(sceneInfo.EntityId);
sceneInfo.Scene = scene;
scene.Name = sceneInfo.Name;
scene.RouteId = sceneInfo.RouteId;
if (sceneId == 0)
{
sceneId = new EntityIdStruct(server.Id, 0, 0);
}
var scene = Create<Scene>(sceneId, sceneId);
scene.Scene = scene;
scene.Parent = scene;
scene.Name = sceneName;
scene.SceneType = sceneType;
scene.Server = server;
scene.SceneConfigId = sceneInfo.Id;
scene.LocationId = server.Id;
if (sceneInfo.WorldId != 0)
if (worldId != 0)
{
// 有可能不需要数据库、所以这里默认0的情况下就不创建数据库了
scene.World = World.Create(sceneInfo.WorldId);
scene.World = World.Create(worldId);
}
if (!string.IsNullOrEmpty(sceneInfo.NetworkProtocol) && !string.IsNullOrEmpty(outerBindIp) && sceneInfo.OuterPort != 0)
if (!string.IsNullOrEmpty(networkProtocol) && !string.IsNullOrEmpty(outerBindIp) && outerPort != 0)
{
// 设置Scene的网络、目前只支持KCP和TCP
var networkProtocolType = Enum.Parse<NetworkProtocolType>(sceneInfo.NetworkProtocol);
var networkProtocolType = Enum.Parse<NetworkProtocolType>(networkProtocol);
var serverNetworkComponent = scene.AddComponent<ServerNetworkComponent>();
var address = NetworkHelper.ToIPEndPoint($"{outerBindIp}:{sceneInfo.OuterPort}");
var address = NetworkHelper.ToIPEndPoint($"{outerBindIp}:{outerPort}");
serverNetworkComponent.Initialize(networkProtocolType, NetworkTarget.Outer, address);
}
if (runEvent && sceneInfo.SceneType != null)
if (!string.IsNullOrEmpty(sceneType))
{
switch (sceneInfo.SceneType)
switch (sceneType)
{
case "Addressable":
{
@@ -121,7 +162,7 @@ namespace TEngine
default:
{
// 没有SceneType目前只有代码创建的Scene才会这样、目前只有Server的Scene是这样
await EventSystem.Instance.PublishAsync(new OnCreateScene(sceneInfo, onSetNetworkComplete));
await EventSystem.Instance.PublishAsync(new OnCreateScene(scene));
break;
}
}
@@ -131,42 +172,14 @@ namespace TEngine
return scene;
}
/// <summary>
/// 一般用于创建临时Scene、如果不是必要不建议使用这个接口
/// </summary>
/// <param name="name"></param>
/// <param name="server"></param>
/// <param name="entityId"></param>
/// <param name="sceneConfigId"></param>
/// <param name="networkProtocol"></param>
/// <param name="outerBindIp"></param>
/// <param name="outerPort"></param>
/// <param name="runEvent"></param>
/// <param name="onSetNetworkComplete"></param>
/// <returns></returns>
public static async FTask<Scene> Create(string name, Server server, long entityId, uint sceneConfigId = 0, string networkProtocol = null, string outerBindIp = null, int outerPort = 0, Action<Session> onSetNetworkComplete = null, bool runEvent = true)
{
var sceneInfo = new SceneConfigInfo()
{
Name = name,
EntityId = entityId,
Id = sceneConfigId,
NetworkProtocol = networkProtocol,
OuterPort = outerPort,
WorldId = ((EntityIdStruct)entityId).WordId
};
return await Create(server, outerBindIp, sceneInfo, onSetNetworkComplete, runEvent);
}
public static List<SceneConfigInfo> GetSceneInfoByRouteId(uint routeId)
public static List<SceneConfigInfo> GetSceneInfoByServerConfigId(uint serverConfigId)
{
var list = new List<SceneConfigInfo>();
var allSceneConfig = ConfigTableManage.AllSceneConfig();
foreach (var sceneConfigInfo in allSceneConfig)
{
if (sceneConfigInfo.RouteId != routeId)
if (sceneConfigInfo.ServerConfigId != serverConfigId)
{
continue;
}

View File

@@ -5,13 +5,11 @@ namespace TEngine
{
public struct OnCreateScene
{
public readonly SceneConfigInfo SceneInfo;
public readonly Action<Session> OnSetNetworkComplete;
public readonly Scene Scene;
public OnCreateScene(SceneConfigInfo sceneInfo, Action<Session> onSetNetworkComplete)
public OnCreateScene(Scene scene)
{
SceneInfo = sceneInfo;
OnSetNetworkComplete = onSetNetworkComplete;
Scene = scene;
}
}
}