饭太稀

This commit is contained in:
bob
2025-06-30 10:51:37 +08:00
commit 8e45469c83
753 changed files with 87652 additions and 0 deletions

View File

@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.IO;
using Fantasy.Platform.Net;
using Fantasy.Serialize;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
// ReSharper disable SuspiciousTypeConversion.Global
namespace Fantasy.ConfigTable
{
/// <summary>
/// 配置表帮助类
/// </summary>
public static class ConfigTableHelper
{
private static string _configTableBinaryDirectory;
private static readonly object Lock = new object();
// 配置表数据缓存字典
private static readonly Dictionary<string, ASerialize> ConfigDic = new ();
/// <summary>
/// 初始化ConfigTableHelper
/// </summary>
/// <param name="configTableBinaryDirectory"></param>
public static void Initialize(string configTableBinaryDirectory)
{
_configTableBinaryDirectory = configTableBinaryDirectory;
}
/// <summary>
/// 加载配置表数据
/// </summary>
/// <typeparam name="T">配置表类型</typeparam>
/// <returns>配置表数据</returns>
public static T Load<T>() where T : ASerialize
{
lock (Lock)
{
try
{
var dataConfig = typeof(T).Name;
if (ConfigDic.TryGetValue(dataConfig, out var aProto))
{
return (T)aProto;
}
var configFile = GetConfigPath(dataConfig);
var bytes = File.ReadAllBytes(configFile);
// Log.Debug($"dataConfig:{dataConfig} {bytes.Length}");
var data = SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf).Deserialize<T>(bytes);
ConfigDic[dataConfig] = data;
return data;
}
catch (Exception ex)
{
throw new Exception($"ConfigTableManage:{typeof(T).Name} 数据表加载之后反序列化时出错:{ex}");
}
}
}
/// <summary>
/// 获取配置表文件路径
/// </summary>
/// <param name="name">配置表名称</param>
/// <returns>配置表文件路径</returns>
private static string GetConfigPath(string name)
{
var configFile = Path.Combine(_configTableBinaryDirectory, $"{name}.bytes");
if (File.Exists(configFile))
{
return configFile;
}
throw new FileNotFoundException($"{name}.byte not found: {configFile}");
}
/// <summary>
/// 重新加载配置表数据
/// </summary>
public static void ReLoadConfigTable()
{
lock (Lock)
{
foreach (var (_, aProto) in ConfigDic)
{
((IDisposable) aProto).Dispose();
}
ConfigDic.Clear();
}
}
}
}

View File

@@ -0,0 +1,33 @@
using System.Collections.Generic;
using ProtoBuf;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
// ReSharper disable CheckNamespace
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
namespace Fantasy.ConfigTable
{
[ProtoContract]
public partial class IntDictionaryConfig
{
[ProtoMember(1)]
public Dictionary<int, int> Dic;
public int this[int key] => GetValue(key);
public bool TryGetValue(int key, out int value)
{
value = default;
if (!Dic.ContainsKey(key))
{
return false;
}
value = Dic[key];
return true;
}
private int GetValue(int key)
{
return Dic.TryGetValue(key, out var value) ? value : 0;
}
}
}

View File

@@ -0,0 +1,35 @@
using System.Collections.Generic;
using ProtoBuf;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
// ReSharper disable CheckNamespace
// ReSharper disable CollectionNeverUpdated.Global
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
#pragma warning disable CS8603 // Possible null reference return.
namespace Fantasy.ConfigTable
{
[ProtoContract]
public sealed partial class StringDictionaryConfig
{
[ProtoMember(1)]
public Dictionary<int, string> Dic;
public string this[int key] => GetValue(key);
public bool TryGetValue(int key, out string value)
{
value = default;
if (!Dic.ContainsKey(key))
{
return false;
}
value = Dic[key];
return true;
}
private string GetValue(int key)
{
return Dic.TryGetValue(key, out var value) ? value : null;
}
}
}

View File

@@ -0,0 +1,34 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PackageId>Fantasy-Net.ConfigTable</PackageId>
<PackageVersion>2024.2.0</PackageVersion>
<Title>Fantasy-Net.ConfigTable</Title>
<Authors>qq362946</Authors>
<owners>qq362946</owners>
<PackageOutputPath>../../../nupkg</PackageOutputPath>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<Description>
Fantasy is a high-performance network development framework based on .NET, supporting mainstream protocols. It is designed for development teams or individuals needing a quick start, scalability, and a distributed, cross-platform solution at the commercial level. Fantasy aims to provide easy-to-use tools while ensuring high system performance and scalability.</Description>
<Copyright>Copyright 2026 qq362946</Copyright>
<PackageProjectUrl>https://www.code-fantasy.com/</PackageProjectUrl>
<RepositoryUrl>https://github.com/qq362946/Fantasy</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>Net, c#, Server, Game, GameServer, Fantasy , Network</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>Fantasy-Net.ConfigTable</AssemblyName>
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Fantasy-Net" Version="2024.2.22" />
</ItemGroup>
<ItemGroup>
<None Include="icon.png" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,7 @@
namespace Fantasy.ConfigTable
{
/// <summary>
/// 表示是一个配置文件
/// </summary>
public interface IConfigTable { }
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 246 B

View File

@@ -0,0 +1,74 @@
# Fantasy.ConfigTable
Fantasy配置表扩展包配合Tools里面的配置导出工具使用注意该包依赖Fantasy包。
- Unity : 需要先安装Fantasy.Unity
- Net : 需要先安装Fantasy.Net
## Unity
在Unity中使用Package Manager点击左上角选择add package from disk选择package.json安装。
Unity端采用assetbundle方式加载配置文件需要把配置文件放到一个包中例如config的ab包。
可以在导出工具中设置导出到前端二进制文件的路径设置到这个config包中。
用代码使用一个IConfigTableAssetBundle接口
```csharp
public sealed class AsserBundleManager : IConfigTableAssetBundle
{
public string Combine(string assetBundleDirectoryPath, string dataConfig)
{
// 该方法用于拼装ab包的路径因为不同的ab包资源管理工具路径都不一样
// 所以这里提供该方法自定义路径
return Path.Combine(assetBundleDirectoryPath, $"{dataConfig}.bytes");
}
public byte[] LoadConfigTable(string assetBundlePath)
{
// 这里是加载包里的二进制文件的逻辑。
// 正常的情况下需要分编辑器和打包环境下。
// 这里只做了编辑器下拿取ab包里的某个配置文件的逻辑。
// 正常情况下要把非编辑器环境的逻辑也要写好。
if (File.Exists(assetBundlePath))
{
return AssetDatabase.LoadAssetAtPath<TextAsset>(assetBundlePath).bytes;
}
UnityEngine.Debug.LogError($"assetBundlePath:{assetBundlePath} not exist");
return null;
}
}
```
上述工作完成够,可以在项目入口点执行初始化代码
```csharp
// 第一个参数是指定ab包的路径但不包含配置名字因为配置文件会在上面的AsserBundleManager里Combine方法拼接出的路径
// 同样这里因为是编辑环境下,所以我输入的是一个路径。
// 但如果打包后这个路径其实并不能使用要把这个路径改成ab包发布都得正常路径。
ConfigTableHelper.Initialize("Assets/Bundles/Config/", new AsserBundleManager());
```
## Net
```csharp
// 设置配置表的路径
// 导出工具会把配置表的二进制数据导出到一个目录中。
// 服务器启动的时候需要传入这个目录的路径。
// 我这里使用了相对路径,大家可以根据自己的环境更改目录
ConfigTableHelper.Initialize("../../../Config/Binary");
```
## 使用
初始化完成后,就可以在项目中使用配置文件了
```csharp
// 例如我配置了一个表名为UnitConfig那在导出在代码中使用只需要再这个表名后面加上Data
// 然后在使用下面的Instance就可以访问表里的数据了
// 获得表里的所有数据
var instanceList = UnitConfigData.Instance.List;
// 根据表的主键Id来获得数据这里的1就是表对应的Id
var unitConfig = UnitConfigData.Instance.Get(1);
```

View File

@@ -0,0 +1,16 @@
{
"name": "Fantasy.ConfigTable",
"rootNamespace": "",
"references": [
"GUID:0b7224b83ba514121aa026f3857f820a"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

View File

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

View File

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

View File

@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using Fantasy.Serialize;
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
// ReSharper disable SuspiciousTypeConversion.Global
namespace Fantasy.ConfigTable
{
/// <summary>
/// 配置表帮助类
/// </summary>
public static class ConfigTableHelper
{
private static string _assetBundleDirectoryPath;
private static IConfigTableAssetBundle _configTableAssetBundle;
private static readonly object Lock = new object();
// 配置表数据缓存字典
private static readonly Dictionary<string, ASerialize> ConfigDic = new ();
/// <summary>
/// 初始化ConfigTableHelper
/// </summary>
/// <param name="assetBundleDirectoryPath"></param>
/// <param name="configTableAssetBundle"></param>
public static void Initialize(string assetBundleDirectoryPath, IConfigTableAssetBundle configTableAssetBundle)
{
_assetBundleDirectoryPath = assetBundleDirectoryPath;
_configTableAssetBundle = configTableAssetBundle;
}
/// <summary>
/// 加载配置表数据
/// </summary>
/// <typeparam name="T">配置表类型</typeparam>
/// <returns>配置表数据</returns>
public static T Load<T>() where T : ASerialize
{
lock (Lock)
{
try
{
var dataConfig = typeof(T).Name;
if (ConfigDic.TryGetValue(dataConfig, out var aProto))
{
return (T)aProto;
}
var dataConfigPath = _configTableAssetBundle.Combine(_assetBundleDirectoryPath, dataConfig);
var bytes = _configTableAssetBundle.LoadConfigTable(dataConfigPath);
var data = SerializerManager.GetSerializer(FantasySerializerType.ProtoBuf).Deserialize<T>(bytes);
ConfigDic[dataConfig] = data;
return data;
}
catch (Exception ex)
{
throw new Exception($"ConfigTableManage:{typeof(T).Name} 数据表加载之后反序列化时出错:{ex}");
}
}
}
/// <summary>
/// 重新加载配置表数据
/// </summary>
public static void ReLoadConfigTable()
{
lock (Lock)
{
foreach (var (_, aProto) in ConfigDic)
{
((IDisposable) aProto).Dispose();
}
ConfigDic.Clear();
}
}
}
}

View File

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

View File

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

View File

@@ -0,0 +1,33 @@
using System.Collections.Generic;
using ProtoBuf;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
// ReSharper disable CheckNamespace
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
namespace Fantasy.ConfigTable
{
[ProtoContract]
public partial class IntDictionaryConfig
{
[ProtoMember(1)]
public Dictionary<int, int> Dic;
public int this[int key] => GetValue(key);
public bool TryGetValue(int key, out int value)
{
value = default;
if (!Dic.ContainsKey(key))
{
return false;
}
value = Dic[key];
return true;
}
private int GetValue(int key)
{
return Dic.TryGetValue(key, out var value) ? value : 0;
}
}
}

View File

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

View File

@@ -0,0 +1,35 @@
using System.Collections.Generic;
using ProtoBuf;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
// ReSharper disable CheckNamespace
// ReSharper disable CollectionNeverUpdated.Global
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
#pragma warning disable CS8603 // Possible null reference return.
namespace Fantasy.ConfigTable
{
[ProtoContract]
public sealed partial class StringDictionaryConfig
{
[ProtoMember(1)]
public Dictionary<int, string> Dic;
public string this[int key] => GetValue(key);
public bool TryGetValue(int key, out string value)
{
value = default;
if (!Dic.ContainsKey(key))
{
return false;
}
value = Dic[key];
return true;
}
private string GetValue(int key)
{
return Dic.TryGetValue(key, out var value) ? value : null;
}
}
}

View File

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

View File

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

View File

@@ -0,0 +1,7 @@
namespace Fantasy.ConfigTable
{
/// <summary>
/// 表示是一个配置文件
/// </summary>
public interface IConfigTable { }
}

View File

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

View File

@@ -0,0 +1,16 @@
namespace Fantasy.ConfigTable
{
public interface IConfigTableAssetBundle
{
/// <summary>
/// 不同的资源管理器有各自独特的包加载方式。
/// 为了满足不同的需求,我们提供了这个接口,允许用户自定义拼装包路径。
/// 通过这个接口,用户可以根据自己的特定格式和要求,灵活地加载包,从而提高资源管理的灵活性和效率。
/// </summary>
/// <param name="assetBundleDirectoryPath"></param>
/// <param name="dataConfig"></param>
/// <returns></returns>
public string Combine(string assetBundleDirectoryPath, string dataConfig);
public byte[] LoadConfigTable(string assetBundlePath);
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: a819f57dfada43d9955a9b402772cf6d
timeCreated: 1728549292

View File

@@ -0,0 +1,23 @@
{
"name": "com.fantasy.configtable",
"version": "2024.2.24",
"displayName": "Fantasy.ConfigTable",
"description": "Fantasy is a cross platform distributed server framework.",
"category": "Network Framework",
"documentationUrl": "https://www.code-fantasy.com/",
"changelogUrl": "https://www.code-fantasy.com/",
"licensesUrl": "https://www.code-fantasy.com/",
"keywords": [
"Fantasy",
"Framework",
"hotfix",
"Server",
"Network"
],
"author": {
"name": "Fantasy",
"email": "362946@qq.com",
"url": "https://www.code-fantasy.com/"
},
"dependencies": {}
}

View File

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