1、Scene.Create接口增加了一个泛型方法接口。 2、SceneConfig增加了SceneTypeConfig和SceneSubType

1、Scene.Create接口增加了一个泛型方法接口。 2、SceneConfig增加了SceneTypeConfig和SceneSubType
This commit is contained in:
ALEXTANGXIAO
2023-07-29 01:03:35 +08:00
parent f7c95d8216
commit 60a5caebae
21 changed files with 263 additions and 160 deletions

View File

@@ -6,5 +6,6 @@ namespace TEngine.Core
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
}
}

View File

@@ -9,7 +9,7 @@ using TEngine.Core.DataBase;
#pragma warning disable CS8618
namespace TEngine
{
public sealed class Scene : Entity, INotSupportedPool
public class Scene : Entity, INotSupportedPool
{
public string Name { get; private set; }
public uint LocationId { get; private set; }
@@ -18,7 +18,8 @@ namespace TEngine
public SceneConfigInfo SceneInfo { get; private set; }
#endif
#if TENGINE_NET
public string SceneType { get; private set; }
public int SceneType { get; private set; }
public int SceneSubType { get; private set; }
public World World { get; private set; }
public Server Server { get; private set; }
#endif
@@ -36,7 +37,8 @@ namespace TEngine
#if TENGINE_NET
World = null;
Server = null;
SceneType = null;
SceneType = 0;
SceneSubType = 0;
#endif
#if TENGINE_UNITY
SceneInfo = null;
@@ -58,6 +60,16 @@ namespace TEngine
}
}
#if TENGINE_UNITY
public static Scene Create()
{
var sceneId = IdFactory.NextRunTimeId();
var scene = Create<Scene>(sceneId, sceneId);
scene.Scene = scene;
scene.Parent = scene;
Scenes.Add(scene);
return scene;
}
public static Scene Create(string name)
{
var sceneId = IdFactory.NextRunTimeId();
@@ -68,6 +80,7 @@ namespace TEngine
Scenes.Add(scene);
return scene;
}
public void CreateSession(string remoteAddress, NetworkProtocolType networkProtocolType, Action onConnectComplete, Action onConnectFail,Action onConnectDisconnect, int connectTimeout = 5000)
{
var address = NetworkHelper.ToIPEndPoint(remoteAddress);
@@ -78,19 +91,20 @@ namespace TEngine
}
#else
/// <summary>
/// 创建一个Scene、但这个Scene是在某个Scene下面的Scene
/// 创建一个Scene、但这个Scene是在某个Scene下面的Scene
/// </summary>
/// <param name="scene"></param>
/// <param name="sceneName"></param>
/// <param name="sceneType"></param>
/// <param name="sceneSubType"></param>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public static async FTask<Scene> Create(Scene scene, string sceneType, string sceneName)
public static async FTask<T> Create<T>(Scene scene, int sceneType, int sceneSubType) where T : Scene, new()
{
var newScene = Create<Scene>(scene);
var newScene = Create<T>(scene);
newScene.Scene = scene;
newScene.Parent = scene;
newScene.Name = sceneName;
newScene.SceneType = sceneType;
newScene.SceneSubType = sceneSubType;
newScene.Server = scene.Server;
newScene.LocationId = scene.Server.Id;
@@ -99,13 +113,13 @@ namespace TEngine
newScene.World = scene.World;
}
if (!string.IsNullOrEmpty(sceneType))
if (sceneType > 0)
{
await EventSystem.Instance.PublishAsync(new OnCreateScene(scene));
}
Scenes.Add(scene);
return scene;
return newScene;
}
/// <summary>
@@ -113,14 +127,14 @@ namespace TEngine
/// </summary>
/// <param name="server"></param>
/// <param name="sceneType"></param>
/// <param name="sceneName"></param>
/// <param name="sceneSubType"></param>
/// <param name="sceneId"></param>
/// <param name="worldId"></param>
/// <param name="networkProtocol"></param>
/// <param name="outerBindIp"></param>
/// <param name="outerPort"></param>
/// <returns></returns>
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)
public static async FTask<Scene> Create(Server server, int sceneType = 0, int sceneSubType = 0, long sceneId = 0, uint worldId = 0, string networkProtocol = null, string outerBindIp = null, int outerPort = 0)
{
if (sceneId == 0)
{
@@ -130,8 +144,8 @@ namespace TEngine
var scene = Create<Scene>(sceneId, sceneId);
scene.Scene = scene;
scene.Parent = scene;
scene.Name = sceneName;
scene.SceneType = sceneType;
scene.SceneSubType = sceneSubType;
scene.Server = server;
scene.LocationId = server.Id;
@@ -150,22 +164,9 @@ namespace TEngine
serverNetworkComponent.Initialize(networkProtocolType, NetworkTarget.Outer, address);
}
if (!string.IsNullOrEmpty(sceneType))
if (sceneType > 0)
{
switch (sceneType)
{
case "Addressable":
{
scene.AddComponent<AddressableManageComponent>();
break;
}
default:
{
// 没有SceneType目前只有代码创建的Scene才会这样、目前只有Server的Scene是这样
await EventSystem.Instance.PublishAsync(new OnCreateScene(scene));
break;
}
}
await EventSystem.Instance.PublishAsync(new OnCreateScene(scene));
}
Scenes.Add(scene);

View File

@@ -0,0 +1,89 @@
#if TENGINE_NET
using System.Text;
using TEngine.Core;
using TEngine.Helper;
namespace TEngine.CustomExport;
public sealed class SceneTypeConfigToEnum : ACustomExport
{
public override void Run()
{
var fullPath = FileHelper.GetFullPath("../../../Config/Excel/Server/SceneConfig.xlsx");
using var excelPackage = ExcelHelper.LoadExcel(fullPath);
var sceneType = new Dictionary<string, string>();
var sceneSubType = new Dictionary<string, string>();
var sceneTypeConfig = excelPackage.Workbook.Worksheets["SceneTypeConfig"];
for (var row = 3; row <= sceneTypeConfig.Dimension.Rows; row++)
{
var sceneTypeId = sceneTypeConfig.GetCellValue(row, 1);
var sceneTypeStr = sceneTypeConfig.GetCellValue(row, 2);
if (string.IsNullOrEmpty(sceneTypeId) || string.IsNullOrEmpty(sceneTypeStr))
{
continue;
}
sceneType.Add(sceneTypeId, sceneTypeStr);
}
var sceneSubTypeConfig = excelPackage.Workbook.Worksheets["SceneSubTypeConfig"];
for (var row = 3; row <= sceneSubTypeConfig.Dimension.Rows; row++)
{
var sceneSubTypeId = sceneSubTypeConfig.GetCellValue(row, 1);
var sceneSubTypeStr = sceneSubTypeConfig.GetCellValue(row, 2);
if (string.IsNullOrEmpty(sceneSubTypeId) || string.IsNullOrEmpty(sceneSubTypeStr))
{
continue;
}
sceneSubType.Add(sceneSubTypeId, sceneSubTypeStr);
}
if (sceneType.Count > 0 || sceneSubType.Count > 0)
{
Write(CustomExportType.Server, sceneType, sceneSubType);
}
}
private void Write(CustomExportType customExportType, Dictionary<string, string> sceneTypes, Dictionary<string, string> sceneSubType)
{
var strBuilder = new StringBuilder();
var dicBuilder = new StringBuilder();
strBuilder.AppendLine("namespace TEngine\n{");
strBuilder.AppendLine("\t// 生成器自动生成,请不要手动编辑。");
strBuilder.AppendLine("\tpublic static class SceneType\n\t{");
dicBuilder.AppendLine("\n\t\tpublic static readonly Dictionary<string, int> SceneTypeDic = new Dictionary<string, int>()\n\t\t{");
foreach (var (sceneTypeId, sceneTypeStr) in sceneTypes)
{
dicBuilder.AppendLine($"\t\t\t{{ \"{sceneTypeStr}\", {sceneTypeId} }},");
strBuilder.AppendLine($"\t\tpublic const int {sceneTypeStr} = {sceneTypeId};");
}
dicBuilder.AppendLine("\t\t};");
strBuilder.Append(dicBuilder);
strBuilder.AppendLine("\t}\n");
strBuilder.AppendLine("\t// 生成器自动生成,请不要手动编辑。");
strBuilder.AppendLine("\tpublic static class SceneSubType\n\t{");
dicBuilder.Clear();
dicBuilder.AppendLine("\n\t\tpublic static readonly Dictionary<string, int> SceneSubTypeDic = new Dictionary<string, int>()\n\t\t{");
foreach (var (sceneSubTypeId, sceneSubTypeStr) in sceneSubType)
{
dicBuilder.AppendLine($"\t\t\t{{ \"{sceneSubTypeStr}\", {sceneSubTypeId} }},");
strBuilder.AppendLine($"\t\tpublic const int {sceneSubTypeStr} = {sceneSubTypeId};");
}
dicBuilder.AppendLine("\t\t};");
strBuilder.Append(dicBuilder);
strBuilder.AppendLine("\t}\n}");
Write("SceneType.cs", strBuilder.ToString(), customExportType);
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: d254c1aefb624046ab545bd8cebbe57c
timeCreated: 1690561569

View File

@@ -4,17 +4,12 @@ using System.Reflection;
using System.Runtime.Loader;
using System.Text;
using System.Text.RegularExpressions;
using TEngine.CustomExport;
using TEngine.DataStructure;
using TEngine.Core;
using Newtonsoft.Json;
using OfficeOpenXml;
using TEngine.Helper;
using static System.String;
#pragma warning disable CS8625
#pragma warning disable CS8604
#pragma warning disable CS8602
#pragma warning disable CS8601
#pragma warning disable CS8600
#pragma warning disable CS8618
namespace TEngine.Core;
@@ -28,10 +23,14 @@ public sealed class ExcelExporter
private readonly OneToManyList<string, ExportInfo> _tables = new OneToManyList<string, ExportInfo>();
private readonly ConcurrentDictionary<string, ExcelTable> _excelTables = new ConcurrentDictionary<string, ExcelTable>();
private readonly ConcurrentDictionary<string, ExcelWorksheet> _worksheets = new ConcurrentDictionary<string, ExcelWorksheet>();
static ExcelExporter()
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
}
public ExcelExporter(ExportType exportType)
{
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
var versionFilePath = Define.ExcelVersionFile;
switch (exportType)
@@ -86,6 +85,9 @@ public sealed class ExcelExporter
task.Add(Task.Run(customExport.Run));
}
}
// 添加生成SceneType的自定义导出
task.Add(Task.Run(new SceneTypeConfigToEnum().Run));
Task.WaitAll(task.ToArray());
}
@@ -226,7 +228,7 @@ public sealed class ExcelExporter
{
// 列名字第一个字符是#不参与导出
var colName = GetCellValue(worksheet, 5, col);
var colName = worksheet.GetCellValue(5, col);
if (colName.StartsWith("#", StringComparison.Ordinal))
{
continue;
@@ -234,14 +236,14 @@ public sealed class ExcelExporter
// 数值列不参与导出
var numericalCol = GetCellValue(worksheet, 3, col);
var numericalCol = worksheet.GetCellValue(3, col);
if (numericalCol != "" && numericalCol != "0")
{
continue;
}
var serverType = GetCellValue(worksheet, 1, col);
var clientType = GetCellValue(worksheet, 2, col);
var serverType = worksheet.GetCellValue(1, col);
var clientType = worksheet.GetCellValue(2, col);
var isExportServer = !IsNullOrEmpty(serverType) && serverType != "0";
var isExportClient = !IsNullOrEmpty(clientType) && clientType != "0";
@@ -350,7 +352,7 @@ public sealed class ExcelExporter
foreach (var colIndex in cols)
{
var colName = GetCellValue(excelWorksheet, 5, colIndex);
var colName = excelWorksheet.GetCellValue(5, colIndex);
if (colNameSet.Contains(colName))
{
@@ -363,19 +365,19 @@ public sealed class ExcelExporter
if (isServer)
{
colType = GetCellValue(excelWorksheet, 1, colIndex);
colType = excelWorksheet.GetCellValue(1, colIndex);
if (IsNullOrEmpty(colType) || colType == "0")
{
colType = GetCellValue(excelWorksheet, 2, colIndex);
colType = excelWorksheet.GetCellValue(2, colIndex);
}
}
else
{
colType = GetCellValue(excelWorksheet, 2, colIndex);
colType = excelWorksheet.GetCellValue(2, colIndex);
}
var remarks = GetCellValue(excelWorksheet, 4, colIndex);
var remarks = excelWorksheet.GetCellValue(4, colIndex);
fileBuilder.Append($"\n\t\t[ProtoMember({++index}, IsRequired = true)]\n");
fileBuilder.Append(
@@ -436,12 +438,12 @@ public sealed class ExcelExporter
for (var row = 7; row <= rows; row++)
{
if (GetCellValue(excelWorksheet, row, 1).StartsWith("#", StringComparison.Ordinal))
if (excelWorksheet.GetCellValue(row, 1).StartsWith("#", StringComparison.Ordinal))
{
continue;
}
var id = GetCellValue(excelWorksheet, row, 3);
var id = excelWorksheet.GetCellValue(row, 3);
if (idCheck.Contains(id))
{
@@ -525,21 +527,21 @@ public sealed class ExcelExporter
{
string colType;
var colIndex = cols[i];
var colName = GetCellValue(excelWorksheet, 5, colIndex);
var value = GetCellValue(excelWorksheet, row, colIndex);
var colName = excelWorksheet.GetCellValue(5, colIndex);
var value = excelWorksheet.GetCellValue(row, colIndex);
if (isServer)
{
colType = GetCellValue(excelWorksheet, 1, colIndex);
colType = excelWorksheet.GetCellValue(1, colIndex);
if (IsNullOrEmpty(colType) || colType == "0")
{
colType = GetCellValue(excelWorksheet, 2, colIndex);
colType = excelWorksheet.GetCellValue(2, colIndex);
}
}
else
{
colType = GetCellValue(excelWorksheet, 2, colIndex);
colType = excelWorksheet.GetCellValue(2, colIndex);
}
try
@@ -566,7 +568,7 @@ public sealed class ExcelExporter
dynamicInfo.Json.AppendLine($"{json},");
}
}
public ExcelWorksheet LoadExcel(string name, bool isAddToDic)
{
if (_worksheets.TryGetValue(name, out var worksheet))
@@ -574,7 +576,7 @@ public sealed class ExcelExporter
return worksheet;
}
worksheet = new ExcelPackage(name).Workbook.Worksheets[0];
worksheet = ExcelHelper.LoadExcel(name).Workbook.Worksheets[0];
if (isAddToDic)
{
@@ -585,26 +587,6 @@ public sealed class ExcelExporter
return worksheet;
}
private string GetCellValue(ExcelWorksheet sheet, int row, int column)
{
var cell = sheet.Cells[row, column];
try
{
if (cell.Value == null)
{
return "";
}
var s = cell.GetValue<string>();
return s.Trim();
}
catch (Exception e)
{
throw new Exception($"Rows {row} Columns {column} Content {cell.Text} {e}");
}
}
private void SetNewValue(PropertyInfo propertyInfo, AProto config, string type, string value)
{
if (IsNullOrWhiteSpace(value))
@@ -753,20 +735,6 @@ public sealed class ExcelExporter
return;
}
// case "AttrConfig":
// {
// if (value.Trim() == "" || value.Trim() == "{}")
// {
// propertyInfo.SetValue(config, null);
// return;
// }
//
// var attr = new AttrConfig {KV = JsonConvert.DeserializeObject<Dictionary<int, int>>(value)};
//
// propertyInfo.SetValue(config, attr);
//
// return;
// }
default:
throw new NotSupportedException($"不支持此类型: {type}");
}

View File

@@ -0,0 +1,34 @@
#if TENGINE_NET
using OfficeOpenXml;
namespace TEngine.Helper;
public static class ExcelHelper
{
public static ExcelPackage LoadExcel(string name)
{
return new ExcelPackage(name);
}
public static string GetCellValue(this ExcelWorksheet sheet, int row, int column)
{
ExcelRange cell = sheet.Cells[row, column];
try
{
if (cell.Value == null)
{
return "";
}
string s = cell.GetValue<string>();
return s.Trim();
}
catch (Exception e)
{
throw new Exception($"Rows {row} Columns {column} Content {cell.Text} {e}");
}
}
}
#endif

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 2ea3356f30cb40949949691a0e258a05
timeCreated: 1690561972

View File

@@ -5,6 +5,16 @@ namespace TEngine.Core
{
public static class FileHelper
{
/// <summary>
/// 获取文件全路径。
/// </summary>
/// <param name="relativePath"></param>
/// <returns></returns>
public static string GetFullPath(string relativePath)
{
return Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePath));
}
/// <summary>
/// 拷贝文件到目标路径、如果目标目录不存在会自动创建目录
/// </summary>

View File

@@ -3,9 +3,11 @@ namespace TEngine
public class SceneConfigInfo
{
public uint Id;
public string Name;
public long EntityId;
public string SceneType;
public int SceneType;
public int SceneSubType;
public string SceneTypeStr;
public string SceneSubTypeStr;
public uint ServerConfigId;
public uint WorldId;
public int OuterPort;

View File

@@ -140,7 +140,7 @@ namespace TEngine
Id = serverConfigId
};
server.Scene = await Scene.Create(server,null,$"ServerScene{serverConfigId}");
server.Scene = await Scene.Create(server);
// 创建网络、Server下的网络只能是内部网络、外部网络是在Scene中定义
@@ -155,7 +155,7 @@ namespace TEngine
foreach (var sceneConfig in sceneInfos)
{
await Scene.Create(server, sceneConfig.SceneType, sceneConfig.Name, sceneConfig.EntityId,
await Scene.Create(server, sceneConfig.SceneType, sceneConfig.SceneSubType, sceneConfig.EntityId,
sceneConfig.WorldId, sceneConfig.NetworkProtocol, outerBindIp, sceneConfig.OuterPort);
}

View File

@@ -12,7 +12,7 @@ namespace TEngine.Core.Network
foreach (var sceneConfigInfo in sceneConfigInfos)
{
if (sceneConfigInfo.SceneType == "Addressable")
if (sceneConfigInfo.SceneTypeStr == "Addressable")
{
AddressableScenes.Add(sceneConfigInfo);
}

View File

@@ -36,46 +36,41 @@ public static class TEngineSettingsHelper
private static void LoadProtoConfig(IConfigurationRoot root)
{
// ProtoBuf文件所在的位置文件夹位置
Define.ProtoBufDirectory = root["Export:ProtoBufDirectory:Value"].GetFullPath();
Define.ProtoBufDirectory = FileHelper.GetFullPath(root["Export:ProtoBufDirectory:Value"]);
// ProtoBuf生成到服务端的文件夹位置
Define.ProtoBufServerDirectory = root["Export:ProtoBufServerDirectory:Value"].GetFullPath();
Define.ProtoBufServerDirectory = FileHelper.GetFullPath(root["Export:ProtoBufServerDirectory:Value"]);
// ProtoBuf生成到客户端的文件夹位置
Define.ProtoBufClientDirectory = root["Export:ProtoBufClientDirectory:Value"].GetFullPath();
Define.ProtoBufClientDirectory = FileHelper.GetFullPath(root["Export:ProtoBufClientDirectory:Value"]);
// ProtoBuf生成代码模板的位置
Define.ProtoBufTemplatePath = root["Export:ProtoBufTemplatePath:Value"].GetFullPath();
Define.ProtoBufTemplatePath = FileHelper.GetFullPath(root["Export:ProtoBufTemplatePath:Value"]);
}
private static void LoadExcelConfig(IConfigurationRoot root)
{
// Excel配置文件根目录
Define.ExcelProgramPath = root["Export:ExcelProgramPath:Value"].GetFullPath();
Define.ExcelProgramPath = FileHelper.GetFullPath(root["Export:ExcelProgramPath:Value"]);
// Excel版本文件的位置
Define.ExcelVersionFile = root["Export:ExcelVersionFile:Value"].GetFullPath();
Define.ExcelVersionFile = FileHelper.GetFullPath(root["Export:ExcelVersionFile:Value"]);
// Excel生成服务器代码的文件夹位置
Define.ExcelServerFileDirectory = root["Export:ExcelServerFileDirectory:Value"].GetFullPath();
Define.ExcelServerFileDirectory = FileHelper.GetFullPath(root["Export:ExcelServerFileDirectory:Value"]);
// Excel生成客户端代码文件夹位置
Define.ExcelClientFileDirectory = root["Export:ExcelClientFileDirectory:Value"].GetFullPath();
Define.ExcelClientFileDirectory = FileHelper.GetFullPath(root["Export:ExcelClientFileDirectory:Value"]);
// Excel生成服务器二进制数据文件夹位置
Define.ExcelServerBinaryDirectory = root["Export:ExcelServerBinaryDirectory:Value"].GetFullPath();
Define.ExcelServerBinaryDirectory = FileHelper.GetFullPath(root["Export:ExcelServerBinaryDirectory:Value"]);
// Excel生成客户端二进制数据文件夹位置
Define.ExcelClientBinaryDirectory = root["Export:ExcelClientBinaryDirectory:Value"].GetFullPath();
Define.ExcelClientBinaryDirectory = FileHelper.GetFullPath(root["Export:ExcelClientBinaryDirectory:Value"]);
// Excel生成服务器Json数据文件夹位置
Define.ExcelServerJsonDirectory = root["Export:ExcelServerJsonDirectory:Value"].GetFullPath();
Define.ExcelServerJsonDirectory = FileHelper.GetFullPath(root["Export:ExcelServerJsonDirectory:Value"]);
// Excel生成客户端Json数据文件夹位置
Define.ExcelClientJsonDirectory = root["Export:ExcelClientJsonDirectory:Value"].GetFullPath();
Define.ExcelClientJsonDirectory = FileHelper.GetFullPath(root["Export:ExcelClientJsonDirectory:Value"]);
// Excel生成代码模板的位置
Define.ExcelTemplatePath = root["Export:ExcelTemplatePath:Value"].GetFullPath();
Define.ExcelTemplatePath = FileHelper.GetFullPath(root["Export:ExcelTemplatePath:Value"]);
// 服务器自定义导出代码文件夹位置
Define.ServerCustomExportDirectory = root["Export:ServerCustomExportDirectory:Value"].GetFullPath();
Define.ServerCustomExportDirectory = FileHelper.GetFullPath(root["Export:ServerCustomExportDirectory:Value"]);
// 客户端自定义导出代码
Define.ClientCustomExportDirectory = root["Export:ClientCustomExportDirectory:Value"].GetFullPath();
Define.ClientCustomExportDirectory = FileHelper.GetFullPath(root["Export:ClientCustomExportDirectory:Value"]);
// 自定义导出代码存放的程序集
Define.CustomExportAssembly = root["Export:CustomExportAssembly:Value"].GetFullPath();
}
private static string GetFullPath(this string relativePath)
{
return Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), relativePath));
Define.CustomExportAssembly = FileHelper.GetFullPath(root["Export:CustomExportAssembly:Value"]);
}
}
#endif