style(csharp): 格式化代码样式和优化代码结构

- 统一调整代码注释的缩进格式,保持文档注释的一致性
- 简化对象初始化语法,移除不必要的参数名称指定
- 优化条件语句结构,移除多余的花括号
- 调整方法实现格式,使用表达式主体语法简化代码
- 标准化代码缩进和空格使用,提升代码可读性
- [skip ci]
This commit is contained in:
GwWuYou 2026-01-04 22:14:34 +08:00
parent 18267e7c14
commit f87c9cf421
61 changed files with 364 additions and 339 deletions

View File

@ -30,7 +30,10 @@ public abstract class TestArchitectureBase : Architecture
/// 添加注册后钩子函数 /// 添加注册后钩子函数
/// </summary> /// </summary>
/// <param name="hook">要添加的钩子函数</param> /// <param name="hook">要添加的钩子函数</param>
public void AddPostRegistrationHook(Action<TestArchitectureBase> hook) => _postRegistrationHook = hook; public void AddPostRegistrationHook(Action<TestArchitectureBase> hook)
{
_postRegistrationHook = hook;
}
/// <summary> /// <summary>
/// 初始化架构组件,注册模型、系统并设置事件监听器 /// 初始化架构组件,注册模型、系统并设置事件监听器

View File

@ -49,7 +49,10 @@ public sealed class AsyncTestModel : IModel, IAsyncInitializable
/// 获取架构上下文 /// 获取架构上下文
/// </summary> /// </summary>
/// <returns>架构上下文对象</returns> /// <returns>架构上下文对象</returns>
public IArchitectureContext GetContext() => _context; public IArchitectureContext GetContext()
{
return _context;
}
/// <summary> /// <summary>
/// 处理架构阶段事件 /// 处理架构阶段事件

View File

@ -19,8 +19,15 @@ public sealed class AsyncTestSystem : ISystem, IAsyncInitializable
Initialized = true; Initialized = true;
} }
public void SetContext(IArchitectureContext context) => _context = context; public void SetContext(IArchitectureContext context)
public IArchitectureContext GetContext() => _context; {
_context = context;
}
public IArchitectureContext GetContext()
{
return _context;
}
public void Init() public void Init()
{ {
@ -28,7 +35,10 @@ public sealed class AsyncTestSystem : ISystem, IAsyncInitializable
throw new InvalidOperationException("Sync Init should not be called"); throw new InvalidOperationException("Sync Init should not be called");
} }
public void Destroy() => DestroyCalled = true; public void Destroy()
{
DestroyCalled = true;
}
public void OnArchitecturePhase(ArchitecturePhase phase) public void OnArchitecturePhase(ArchitecturePhase phase)
{ {

View File

@ -21,7 +21,10 @@ public class AsyncArchitectureTests : ArchitectureTestsBase<AsyncTestArchitectur
/// 创建异步测试架构实例 /// 创建异步测试架构实例
/// </summary> /// </summary>
/// <returns>AsyncTestArchitecture实例</returns> /// <returns>AsyncTestArchitecture实例</returns>
protected override AsyncTestArchitecture CreateArchitecture() => new(); protected override AsyncTestArchitecture CreateArchitecture()
{
return new AsyncTestArchitecture();
}
/// <summary> /// <summary>
/// 测试架构是否正确初始化所有组件 /// 测试架构是否正确初始化所有组件

View File

@ -15,7 +15,10 @@ namespace GFramework.Core.Tests.tests;
[NonParallelizable] [NonParallelizable]
public class SyncArchitectureTests : ArchitectureTestsBase<SyncTestArchitecture> public class SyncArchitectureTests : ArchitectureTestsBase<SyncTestArchitecture>
{ {
protected override SyncTestArchitecture CreateArchitecture() => new(); protected override SyncTestArchitecture CreateArchitecture()
{
return new SyncTestArchitecture();
}
/// <summary> /// <summary>
/// 测试架构是否正确初始化所有组件 /// 测试架构是否正确初始化所有组件

View File

@ -249,7 +249,7 @@ public abstract class Architecture(
{ {
try try
{ {
InitializeInternalAsync(asyncMode: false).GetAwaiter().GetResult(); InitializeInternalAsync(false).GetAwaiter().GetResult();
} }
catch (Exception e) catch (Exception e)
{ {
@ -268,7 +268,7 @@ public abstract class Architecture(
{ {
try try
{ {
await InitializeInternalAsync(asyncMode: true); await InitializeInternalAsync(true);
} }
catch (Exception e) catch (Exception e)
{ {
@ -289,9 +289,7 @@ public abstract class Architecture(
{ {
// 根据组件类型和异步模式选择相应的初始化方法 // 根据组件类型和异步模式选择相应的初始化方法
if (asyncMode && component is IAsyncInitializable asyncInit) if (asyncMode && component is IAsyncInitializable asyncInit)
{
await asyncInit.InitializeAsync(); await asyncInit.InitializeAsync();
}
else else
switch (component) switch (component)
{ {

View File

@ -17,7 +17,7 @@ public sealed class ArchitectureConfiguration : IArchitectureConfiguration
/// </summary> /// </summary>
public LoggerProperties LoggerProperties { get; set; } = new() public LoggerProperties LoggerProperties { get; set; } = new()
{ {
LoggerFactoryProvider = new ConsoleLoggerFactoryProvider() LoggerFactoryProvider = new ConsoleLoggerFactoryProvider
{ {
MinLevel = LogLevel.Info MinLevel = LogLevel.Info
} }

View File

@ -27,11 +27,9 @@ public static class GameContext
public static void Bind(Type architectureType, IArchitectureContext context) public static void Bind(Type architectureType, IArchitectureContext context)
{ {
if (!ArchitectureDictionary.TryAdd(architectureType, context)) if (!ArchitectureDictionary.TryAdd(architectureType, context))
{
throw new InvalidOperationException( throw new InvalidOperationException(
$"Architecture context for '{architectureType.Name}' already exists"); $"Architecture context for '{architectureType.Name}' already exists");
} }
}
/// <summary> /// <summary>
/// 获取字典中的第一个架构上下文 /// 获取字典中的第一个架构上下文

View File

@ -132,7 +132,9 @@ public static class ObjectExtensions
/// </code> /// </code>
/// </example> /// </example>
public static T? As<T>(this object obj) where T : class public static T? As<T>(this object obj) where T : class
=> obj as T; {
return obj as T;
}
/// <summary> /// <summary>
/// 对对象执行指定操作后返回对象本身, /// 对对象执行指定操作后返回对象本身,

View File

@ -114,10 +114,7 @@ public class IocContainer : ContextAwareBase, IIocContainer
RegisterInternal(concreteType, instance); RegisterInternal(concreteType, instance);
// 注册所有接口类型 // 注册所有接口类型
foreach (var itf in interfaces) foreach (var itf in interfaces) RegisterInternal(itf, instance);
{
RegisterInternal(itf, instance);
}
} }
finally finally
{ {

View File

@ -18,5 +18,7 @@ public sealed class ConsoleLoggerFactoryProvider : ILoggerFactoryProvider
/// <param name="name">日志记录器的名称,用于标识特定的日志源</param> /// <param name="name">日志记录器的名称,用于标识特定的日志源</param>
/// <returns>配置了指定名称和最小日志级别的ILogger实例</returns> /// <returns>配置了指定名称和最小日志级别的ILogger实例</returns>
public ILogger CreateLogger(string name) public ILogger CreateLogger(string name)
=> new ConsoleLoggerFactory().GetLogger(name, MinLevel); {
return new ConsoleLoggerFactory().GetLogger(name, MinLevel);
}
} }

View File

@ -28,10 +28,8 @@ public class BindablePropertyUnRegister<T>(BindableProperty<T> bindableProperty,
{ {
// 检查两个引用都不为null时才执行注销操作 // 检查两个引用都不为null时才执行注销操作
if (BindableProperty != null && OnValueChanged != null) if (BindableProperty != null && OnValueChanged != null)
{
// 调用可绑定属性的注销方法,传入值变化回调函数 // 调用可绑定属性的注销方法,传入值变化回调函数
BindableProperty.UnRegister(OnValueChanged); BindableProperty.UnRegister(OnValueChanged);
}
// 清理属性引用 // 清理属性引用
BindableProperty = null; BindableProperty = null;

View File

@ -1,5 +1,5 @@
namespace GFramework.Game.Abstractions.assets namespace GFramework.Game.Abstractions.assets;
{
/// <summary> /// <summary>
/// 资源目录类,用于定义和管理游戏中的场景和资源标识符 /// 资源目录类,用于定义和管理游戏中的场景和资源标识符
/// </summary> /// </summary>
@ -43,4 +43,3 @@
/// <param name="Path">资源路径</param> /// <param name="Path">资源路径</param>
public readonly record struct AssetId(string Path) : IAssetId; public readonly record struct AssetId(string Path) : IAssetId;
} }
}

View File

@ -3,10 +3,9 @@
// when targeting netstandard2.0 or older frameworks. // when targeting netstandard2.0 or older frameworks.
#pragma warning disable S2094 // Remove this empty class #pragma warning disable S2094 // Remove this empty class
namespace System.Runtime.CompilerServices namespace System.Runtime.CompilerServices;
{
internal static class IsExternalInit internal static class IsExternalInit
{ {
} }
}
#pragma warning restore S2094 #pragma warning restore S2094

View File

@ -1,7 +1,4 @@
using System; using GFramework.Core.Abstractions.architecture;
using System.Collections.Generic;
using System.Threading.Tasks;
using GFramework.Core.Abstractions.architecture;
using GFramework.Core.Abstractions.environment; using GFramework.Core.Abstractions.environment;
using GFramework.Core.architecture; using GFramework.Core.architecture;
using GFramework.Core.constants; using GFramework.Core.constants;

View File

@ -1,5 +1,4 @@
using System; using Godot;
using Godot;
namespace GFramework.Godot.architecture; namespace GFramework.Godot.architecture;

View File

@ -1,4 +1,3 @@
using System;
using GFramework.Core.Abstractions.enums; using GFramework.Core.Abstractions.enums;
using GFramework.Core.extensions; using GFramework.Core.extensions;
using GFramework.Core.system; using GFramework.Core.system;

View File

@ -1,6 +1,4 @@
using System; using GFramework.Core.Abstractions.system;
using System.Collections.Generic;
using GFramework.Core.Abstractions.system;
using GFramework.Game.Abstractions.assets; using GFramework.Game.Abstractions.assets;
using Godot; using Godot;

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using GFramework.Core.system;
using GFramework.Core.system;
using GFramework.Game.Abstractions.assets; using GFramework.Game.Abstractions.assets;
using Godot; using Godot;

View File

@ -58,5 +58,8 @@ public sealed class SignalBuilder(GodotObject target, StringName signal)
/// 显式结束,返回 Node /// 显式结束,返回 Node
/// </summary> /// </summary>
/// <returns>目标节点</returns> /// <returns>目标节点</returns>
public GodotObject End() => target; public GodotObject End()
{
return target;
}
} }

View File

@ -16,5 +16,7 @@ public static class SignalFluentExtensions
public static SignalBuilder Signal( public static SignalBuilder Signal(
this GodotObject @object, this GodotObject @object,
StringName signal) StringName signal)
=> new(@object, signal); {
return new SignalBuilder(@object, signal);
}
} }

View File

@ -18,10 +18,7 @@ public sealed class GodotLogger(
var logPrefix = $"[{timestamp}] {levelStr} [{Name()}]"; var logPrefix = $"[{timestamp}] {levelStr} [{Name()}]";
// 添加异常信息 // 添加异常信息
if (exception != null) if (exception != null) message += "\n" + exception;
{
message += "\n" + exception;
}
var logMessage = $"{logPrefix} {message}"; var logMessage = $"{logPrefix} {message}";

View File

@ -17,5 +17,8 @@ public sealed class GodotLoggerFactoryProvider : ILoggerFactoryProvider
/// </summary> /// </summary>
/// <param name="name">日志记录器的名称</param> /// <param name="name">日志记录器的名称</param>
/// <returns>返回配置了最小日志级别的Godot日志记录器实例</returns> /// <returns>返回配置了最小日志级别的Godot日志记录器实例</returns>
public ILogger CreateLogger(string name) => new GodotLoggerFactory().GetLogger(name, MinLevel); public ILogger CreateLogger(string name)
{
return new GodotLoggerFactory().GetLogger(name, MinLevel);
}
} }

View File

@ -1,4 +1,3 @@
using System.Collections.Generic;
using GFramework.Core.extensions; using GFramework.Core.extensions;
using GFramework.Core.system; using GFramework.Core.system;
using GFramework.Game.Abstractions.assets; using GFramework.Game.Abstractions.assets;

View File

@ -24,7 +24,7 @@ public static class CommonDiagnostics
"Class '{0}' must be declared partial for code generation", "Class '{0}' must be declared partial for code generation",
"GFramework.Common", "GFramework.Common",
DiagnosticSeverity.Error, DiagnosticSeverity.Error,
isEnabledByDefault: true true
); );
/// <summary> /// <summary>
@ -44,7 +44,7 @@ public static class CommonDiagnostics
"{0}", "{0}",
"GFramework.Trace", "GFramework.Trace",
DiagnosticSeverity.Info, DiagnosticSeverity.Info,
isEnabledByDefault: true true
); );
/// <summary> /// <summary>

View File

@ -128,7 +128,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
ClassDeclarationSyntax syntax, ClassDeclarationSyntax syntax,
INamedTypeSymbol symbol, INamedTypeSymbol symbol,
AttributeData attr) AttributeData attr)
=> true; {
return true;
}
/// <summary> /// <summary>
/// 生成源代码 /// 生成源代码
@ -150,7 +152,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
/// <param name="symbol">命名类型符号</param> /// <param name="symbol">命名类型符号</param>
/// <returns>生成文件的提示名称</returns> /// <returns>生成文件的提示名称</returns>
protected virtual string GetHintName(INamedTypeSymbol symbol) protected virtual string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.g.cs"; {
return $"{symbol.Name}.g.cs";
}
/// <summary> /// <summary>
/// 报告类必须是部分类的错误 /// 报告类必须是部分类的错误

View File

@ -31,7 +31,7 @@ public abstract class AttributeEnumGeneratorBase : IIncrementalGenerator
(ctx, _) => (ctx, _) =>
{ {
var syntax = (EnumDeclarationSyntax)ctx.Node; var syntax = (EnumDeclarationSyntax)ctx.Node;
var symbol = ctx.SemanticModel.GetDeclaredSymbol(syntax, cancellationToken: _) as INamedTypeSymbol; var symbol = ctx.SemanticModel.GetDeclaredSymbol(syntax, _) as INamedTypeSymbol;
return (syntax, symbol); return (syntax, symbol);
}) })
.Where(x => x.symbol is not null); .Where(x => x.symbol is not null);
@ -98,5 +98,7 @@ public abstract class AttributeEnumGeneratorBase : IIncrementalGenerator
/// <param name="symbol">命名类型符号</param> /// <param name="symbol">命名类型符号</param>
/// <returns>生成文件的提示名称</returns> /// <returns>生成文件的提示名称</returns>
protected virtual string GetHintName(INamedTypeSymbol symbol) protected virtual string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.g.cs"; {
return $"{symbol.Name}.g.cs";
}
} }

View File

@ -66,5 +66,7 @@ public static class GeneratorSnapshotTest<TGenerator>
/// <param name="text">要标准化的文本</param> /// <param name="text">要标准化的文本</param>
/// <returns>标准化后的文本</returns> /// <returns>标准化后的文本</returns>
private static string Normalize(string text) private static string Normalize(string text)
=> text.Replace("\r\n", "\n").Trim(); {
return text.Replace("\r\n", "\n").Trim();
}
} }

View File

@ -75,7 +75,7 @@ public class ContextAwareGeneratorSnapshotTests
// 执行生成器快照测试,将生成的代码与预期快照进行比较 // 执行生成器快照测试,将生成的代码与预期快照进行比较
await GeneratorSnapshotTest<ContextAwareGenerator>.RunAsync( await GeneratorSnapshotTest<ContextAwareGenerator>.RunAsync(
source, source,
snapshotFolder: Path.Combine( Path.Combine(
TestContext.CurrentContext.TestDirectory, TestContext.CurrentContext.TestDirectory,
"rule", "rule",
"snapshots", "snapshots",

View File

@ -108,5 +108,7 @@ public sealed class EnumExtensionsGenerator : AttributeEnumGeneratorBase
/// <param name="symbol">命名类型符号</param> /// <param name="symbol">命名类型符号</param>
/// <returns>生成文件的提示名称</returns> /// <returns>生成文件的提示名称</returns>
protected override string GetHintName(INamedTypeSymbol symbol) protected override string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.EnumExtensions.g.cs"; {
return $"{symbol.Name}.EnumExtensions.g.cs";
}
} }

View File

@ -102,8 +102,12 @@ public sealed class LoggerGenerator : TypeAttributeClassGeneratorBase
/// 可以自定义生成文件名 /// 可以自定义生成文件名
/// </summary> /// </summary>
protected override string GetHintName(INamedTypeSymbol symbol) protected override string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.Logger.g.cs"; {
return $"{symbol.Name}.Logger.g.cs";
}
private static object? GetNamedArg(AttributeData attr, string name) private static object? GetNamedArg(AttributeData attr, string name)
=> attr.NamedArguments.FirstOrDefault(kv => kv.Key == name).Value.Value; {
return attr.NamedArguments.FirstOrDefault(kv => kv.Key == name).Value.Value;
}
} }

View File

@ -117,7 +117,9 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
/// <param name="symbol">命名类型符号</param> /// <param name="symbol">命名类型符号</param>
/// <returns>生成文件的提示名称</returns> /// <returns>生成文件的提示名称</returns>
protected override string GetHintName(INamedTypeSymbol symbol) protected override string GetHintName(INamedTypeSymbol symbol)
=> $"{symbol.Name}.ContextAware.g.cs"; {
return $"{symbol.Name}.ContextAware.g.cs";
}
// ========================= // =========================
// Context 属性(无 global::,与测试一致) // Context 属性(无 global::,与测试一致)
@ -223,10 +225,8 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
default: default:
if (!method.ReturnsVoid) if (!method.ReturnsVoid)
{
sb.AppendLine( sb.AppendLine(
$" throw new System.NotImplementedException(\"Method '{method.Name}' is not supported.\");"); $" throw new System.NotImplementedException(\"Method '{method.Name}' is not supported.\");");
}
break; break;
} }