T2FGame.Tools
1.0.5
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package T2FGame.Tools --version 1.0.5
NuGet\Install-Package T2FGame.Tools -Version 1.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="T2FGame.Tools" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="T2FGame.Tools" Version="1.0.5" />
<PackageReference Include="T2FGame.Tools" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add T2FGame.Tools --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: T2FGame.Tools, 1.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package T2FGame.Tools@1.0.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=T2FGame.Tools&version=1.0.5
#tool nuget:?package=T2FGame.Tools&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
T2FGame.Tools
T2FGame 开发工具库,提供 Proto 导出、客户端 SDK 代码生成和 protoc 执行功能。
功能
- ProtoExporter:从编译后的程序集中导出 .proto 文件
- ClientSdkGenerator:为 Unity/C# 客户端生成类型安全的 SDK 代码
- ProtocExecutor:调用 protoc 生成目标语言代码
安装
<PackageReference Include="T2FGame.Tools" />
CLI 工具
推荐使用配套的 CLI 工具进行命令行操作:
dotnet tool install -g T2FGame.Tools.Cli
# 导出 Proto 文件
t2f-proto export -a ./MyGame.dll -o ./protos
# 生成客户端 SDK
t2f-proto sdk -a ./MyGame.dll -o ./client-sdk -n MyGame.Client
详细用法请参阅 T2FGame.Tools.Cli README。
API 使用
ProtoExporter
从编译后的程序集中提取 Source Generator 生成的 .proto 文件:
using T2FGame.Tools;
var exporter = new ProtoExporter();
// 单程序集导出
var result = exporter.Export(
assemblyPath: "./bin/Debug/net9.0/MyGame.dll",
outputDirectory: "./protos",
cleanOutput: true, // 清理旧文件
includeCoreProto: true); // 包含通用协议文件
if (result.Success)
{
Console.WriteLine($"成功导出 {result.FileCount} 个文件");
foreach (var file in result.ExportedFiles)
{
Console.WriteLine($" - {file}");
}
}
// 多程序集导出
var multiResult = exporter.Export(
new[] { "Module1.dll", "Module2.dll", "SharedProto.dll" },
outputDirectory: "./protos");
// 导出所有嵌入资源(包括 .ts 等文件)
var allResult = exporter.ExportAll(
assemblyPath: "./MyGame.dll",
outputDirectory: "./output");
// 单独导出通用协议文件
var commonProtoPath = ProtoExporter.ExportCommonProto("./protos");
ExportResult 属性:
| 属性 | 类型 | 说明 |
|---|---|---|
| Success | bool | 是否成功 |
| FileCount | int | 导出的文件数量 |
| ExportedFiles | IReadOnlyList<string> | 导出的文件路径列表 |
| ErrorMessage | string? | 错误消息(失败时) |
ClientSdkGenerator
为 Unity 客户端生成类型安全的 SDK 代码:
using T2FGame.Tools.ClientSdk;
var generator = new ClientSdkGenerator();
// 配置选项
var options = new ClientSdkOptions
{
ClientNamespace = "MyGame.Client",
ProtoNamespace = "MyGame.Client.Proto"
};
// 单程序集生成
var result = generator.Generate(
assemblyPath: "./bin/Debug/net9.0/MyGame.dll",
outputDirectory: "./unity/Assets/Gen",
options: options);
if (result.Success)
{
Console.WriteLine($"成功生成 {result.GeneratedFiles.Count} 个文件");
}
// 多程序集生成
var multiResult = generator.Generate(
new[] { "GameServer.dll", "SharedModule.dll" },
outputDirectory: "./client-sdk",
options: options);
// 仅提取元数据(不生成文件)
var metadata = generator.ExtractMetadata("./MyGame.dll");
Console.WriteLine($"发现 {metadata.Controllers.Count} 个控制器");
生成的客户端文件:
*Action.cs- 每个控制器对应的 Action 类Listener.cs- 广播消息统一监听器GameCode.cs- 错误码常量定义
ProtocExecutor
调用系统 protoc 生成目标语言代码:
using T2FGame.Tools;
var executor = new ProtocExecutor();
// 检查 protoc 是否可用
if (!executor.IsAvailable())
{
Console.WriteLine("protoc 未安装");
return;
}
Console.WriteLine($"protoc 版本: {executor.GetVersion()}");
// 生成 C# 代码
var result = executor.Generate(
protoPath: "./protos",
outputPath: "./generated",
language: ProtocExecutor.Language.CSharp);
if (result.Success)
{
Console.WriteLine("生成成功");
}
else
{
Console.WriteLine($"错误: {result.ErrorMessage}");
}
支持的语言:
public enum Language
{
CSharp,
Java,
Python,
Go,
Cpp,
Ruby,
Objc,
Php,
Kotlin,
Js
}
客户端 SDK 使用示例
Action 类
每个服务器模块生成一个对应的 Action 类,支持回调和 async/await 两种调用方式:
// 回调方式
UserAction.OfLogin(request, result => {
if (result.IsSuccess)
{
var value = result.GetValue<LoginResponse>();
Debug.Log($"登录成功: {value.UserId}");
}
else
{
Debug.LogError($"登录失败: {result.ErrorMessage}");
}
});
// async/await 方式
var result = await UserAction.OfAwaitLogin(request);
if (result.IsSuccess)
{
var value = result.GetValue<LoginResponse>();
}
Listener 类
统一的广播消息监听器:
// 监听用户信息更新
Listener.ListenUserInfoUpdate(result => {
var userInfo = result.GetValue<UserInfo>();
UpdateUI(userInfo);
});
// 监听聊天消息
Listener.ListenChatMessage(result => {
var message = result.GetValue<ChatMessage>();
AddChatMessage(message);
});
GameCode 类
错误码常量和描述:
if (result.ErrorCode == GameCode.UserNotFound)
{
ShowError("用户不存在");
}
else if (result.ErrorCode == GameCode.InvalidPassword)
{
ShowError("密码错误");
}
目录结构
T2FGame.Tools/
├── ProtoExporter.cs # Proto 文件导出器
├── ProtocExecutor.cs # protoc 执行器
└── ClientSdk/
├── ClientSdkOptions.cs # 配置选项
├── ClientSdkModels.cs # 模型定义
├── ClientSdkMetadata.cs # 元数据结构
├── ClientSdkExtractor.cs # 元数据提取器
├── ClientSdkGenerator.cs # 主生成器
└── Emitters/
├── ActionEmitter.cs # Action 代码发射器
├── ListenerEmitter.cs # Listener 代码发射器
└── GameCodeEmitter.cs # GameCode 代码发射器
许可证
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net9.0
- Scriban (>= 5.10.0)
- System.Reflection.MetadataLoadContext (>= 9.0.0)
NuGet packages (2)
Showing the top 2 NuGet packages that depend on T2FGame.Tools:
| Package | Downloads |
|---|---|
|
T2FGame.Samples.ProtoExport
A high-performance game server framework |
|
|
T2FGame.Samples.ClientSdk
A high-performance game server framework |
GitHub repositories
This package is not used by any popular GitHub repositories.