mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
style(csharp): 格式化代码样式和优化代码结构
- 统一调整代码注释的缩进格式,保持文档注释的一致性 - 简化对象初始化语法,移除不必要的参数名称指定 - 优化条件语句结构,移除多余的花括号 - 调整方法实现格式,使用表达式主体语法简化代码 - 标准化代码缩进和空格使用,提升代码可读性 - [skip ci]
This commit is contained in:
parent
18267e7c14
commit
f87c9cf421
@ -30,7 +30,10 @@ public abstract class TestArchitectureBase : Architecture
|
||||
/// 添加注册后钩子函数
|
||||
/// </summary>
|
||||
/// <param name="hook">要添加的钩子函数</param>
|
||||
public void AddPostRegistrationHook(Action<TestArchitectureBase> hook) => _postRegistrationHook = hook;
|
||||
public void AddPostRegistrationHook(Action<TestArchitectureBase> hook)
|
||||
{
|
||||
_postRegistrationHook = hook;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 初始化架构组件,注册模型、系统并设置事件监听器
|
||||
|
||||
@ -49,7 +49,10 @@ public sealed class AsyncTestModel : IModel, IAsyncInitializable
|
||||
/// 获取架构上下文
|
||||
/// </summary>
|
||||
/// <returns>架构上下文对象</returns>
|
||||
public IArchitectureContext GetContext() => _context;
|
||||
public IArchitectureContext GetContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理架构阶段事件
|
||||
|
||||
@ -19,8 +19,15 @@ public sealed class AsyncTestSystem : ISystem, IAsyncInitializable
|
||||
Initialized = true;
|
||||
}
|
||||
|
||||
public void SetContext(IArchitectureContext context) => _context = context;
|
||||
public IArchitectureContext GetContext() => _context;
|
||||
public void SetContext(IArchitectureContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public IArchitectureContext GetContext()
|
||||
{
|
||||
return _context;
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
@ -28,7 +35,10 @@ public sealed class AsyncTestSystem : ISystem, IAsyncInitializable
|
||||
throw new InvalidOperationException("Sync Init should not be called");
|
||||
}
|
||||
|
||||
public void Destroy() => DestroyCalled = true;
|
||||
public void Destroy()
|
||||
{
|
||||
DestroyCalled = true;
|
||||
}
|
||||
|
||||
public void OnArchitecturePhase(ArchitecturePhase phase)
|
||||
{
|
||||
|
||||
@ -21,7 +21,10 @@ public class AsyncArchitectureTests : ArchitectureTestsBase<AsyncTestArchitectur
|
||||
/// 创建异步测试架构实例
|
||||
/// </summary>
|
||||
/// <returns>AsyncTestArchitecture实例</returns>
|
||||
protected override AsyncTestArchitecture CreateArchitecture() => new();
|
||||
protected override AsyncTestArchitecture CreateArchitecture()
|
||||
{
|
||||
return new AsyncTestArchitecture();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试架构是否正确初始化所有组件
|
||||
|
||||
@ -15,7 +15,10 @@ namespace GFramework.Core.Tests.tests;
|
||||
[NonParallelizable]
|
||||
public class SyncArchitectureTests : ArchitectureTestsBase<SyncTestArchitecture>
|
||||
{
|
||||
protected override SyncTestArchitecture CreateArchitecture() => new();
|
||||
protected override SyncTestArchitecture CreateArchitecture()
|
||||
{
|
||||
return new SyncTestArchitecture();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试架构是否正确初始化所有组件
|
||||
|
||||
@ -249,7 +249,7 @@ public abstract class Architecture(
|
||||
{
|
||||
try
|
||||
{
|
||||
InitializeInternalAsync(asyncMode: false).GetAwaiter().GetResult();
|
||||
InitializeInternalAsync(false).GetAwaiter().GetResult();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@ -268,7 +268,7 @@ public abstract class Architecture(
|
||||
{
|
||||
try
|
||||
{
|
||||
await InitializeInternalAsync(asyncMode: true);
|
||||
await InitializeInternalAsync(true);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
@ -289,9 +289,7 @@ public abstract class Architecture(
|
||||
{
|
||||
// 根据组件类型和异步模式选择相应的初始化方法
|
||||
if (asyncMode && component is IAsyncInitializable asyncInit)
|
||||
{
|
||||
await asyncInit.InitializeAsync();
|
||||
}
|
||||
else
|
||||
switch (component)
|
||||
{
|
||||
|
||||
@ -17,7 +17,7 @@ public sealed class ArchitectureConfiguration : IArchitectureConfiguration
|
||||
/// </summary>
|
||||
public LoggerProperties LoggerProperties { get; set; } = new()
|
||||
{
|
||||
LoggerFactoryProvider = new ConsoleLoggerFactoryProvider()
|
||||
LoggerFactoryProvider = new ConsoleLoggerFactoryProvider
|
||||
{
|
||||
MinLevel = LogLevel.Info
|
||||
}
|
||||
|
||||
@ -27,11 +27,9 @@ public static class GameContext
|
||||
public static void Bind(Type architectureType, IArchitectureContext context)
|
||||
{
|
||||
if (!ArchitectureDictionary.TryAdd(architectureType, context))
|
||||
{
|
||||
throw new InvalidOperationException(
|
||||
$"Architecture context for '{architectureType.Name}' already exists");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取字典中的第一个架构上下文
|
||||
|
||||
@ -132,7 +132,9 @@ public static class ObjectExtensions
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static T? As<T>(this object obj) where T : class
|
||||
=> obj as T;
|
||||
{
|
||||
return obj as T;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 对对象执行指定操作后返回对象本身,
|
||||
|
||||
@ -114,10 +114,7 @@ public class IocContainer : ContextAwareBase, IIocContainer
|
||||
RegisterInternal(concreteType, instance);
|
||||
|
||||
// 注册所有接口类型
|
||||
foreach (var itf in interfaces)
|
||||
{
|
||||
RegisterInternal(itf, instance);
|
||||
}
|
||||
foreach (var itf in interfaces) RegisterInternal(itf, instance);
|
||||
}
|
||||
finally
|
||||
{
|
||||
|
||||
@ -18,5 +18,7 @@ public sealed class ConsoleLoggerFactoryProvider : ILoggerFactoryProvider
|
||||
/// <param name="name">日志记录器的名称,用于标识特定的日志源</param>
|
||||
/// <returns>配置了指定名称和最小日志级别的ILogger实例</returns>
|
||||
public ILogger CreateLogger(string name)
|
||||
=> new ConsoleLoggerFactory().GetLogger(name, MinLevel);
|
||||
{
|
||||
return new ConsoleLoggerFactory().GetLogger(name, MinLevel);
|
||||
}
|
||||
}
|
||||
@ -28,10 +28,8 @@ public class BindablePropertyUnRegister<T>(BindableProperty<T> bindableProperty,
|
||||
{
|
||||
// 检查两个引用都不为null时才执行注销操作
|
||||
if (BindableProperty != null && OnValueChanged != null)
|
||||
{
|
||||
// 调用可绑定属性的注销方法,传入值变化回调函数
|
||||
BindableProperty.UnRegister(OnValueChanged);
|
||||
}
|
||||
|
||||
// 清理属性引用
|
||||
BindableProperty = null;
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
namespace GFramework.Game.Abstractions.assets
|
||||
{
|
||||
namespace GFramework.Game.Abstractions.assets;
|
||||
|
||||
/// <summary>
|
||||
/// 资源目录类,用于定义和管理游戏中的场景和资源标识符
|
||||
/// </summary>
|
||||
@ -43,4 +43,3 @@
|
||||
/// <param name="Path">资源路径</param>
|
||||
public readonly record struct AssetId(string Path) : IAssetId;
|
||||
}
|
||||
}
|
||||
@ -3,10 +3,9 @@
|
||||
// when targeting netstandard2.0 or older frameworks.
|
||||
|
||||
#pragma warning disable S2094 // Remove this empty class
|
||||
namespace System.Runtime.CompilerServices
|
||||
{
|
||||
namespace System.Runtime.CompilerServices;
|
||||
|
||||
internal static class IsExternalInit
|
||||
{
|
||||
}
|
||||
}
|
||||
#pragma warning restore S2094
|
||||
@ -1,7 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using GFramework.Core.Abstractions.architecture;
|
||||
using GFramework.Core.Abstractions.architecture;
|
||||
using GFramework.Core.Abstractions.environment;
|
||||
using GFramework.Core.architecture;
|
||||
using GFramework.Core.constants;
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Godot;
|
||||
using Godot;
|
||||
|
||||
namespace GFramework.Godot.architecture;
|
||||
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
using System;
|
||||
using GFramework.Core.Abstractions.enums;
|
||||
using GFramework.Core.extensions;
|
||||
using GFramework.Core.system;
|
||||
|
||||
@ -1,6 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using GFramework.Core.Abstractions.system;
|
||||
using GFramework.Core.Abstractions.system;
|
||||
using GFramework.Game.Abstractions.assets;
|
||||
using Godot;
|
||||
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
using System.Collections.Generic;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Game.Abstractions.assets;
|
||||
using Godot;
|
||||
|
||||
|
||||
@ -58,5 +58,8 @@ public sealed class SignalBuilder(GodotObject target, StringName signal)
|
||||
/// 显式结束,返回 Node
|
||||
/// </summary>
|
||||
/// <returns>目标节点</returns>
|
||||
public GodotObject End() => target;
|
||||
public GodotObject End()
|
||||
{
|
||||
return target;
|
||||
}
|
||||
}
|
||||
@ -16,5 +16,7 @@ public static class SignalFluentExtensions
|
||||
public static SignalBuilder Signal(
|
||||
this GodotObject @object,
|
||||
StringName signal)
|
||||
=> new(@object, signal);
|
||||
{
|
||||
return new SignalBuilder(@object, signal);
|
||||
}
|
||||
}
|
||||
@ -18,10 +18,7 @@ public sealed class GodotLogger(
|
||||
var logPrefix = $"[{timestamp}] {levelStr} [{Name()}]";
|
||||
|
||||
// 添加异常信息
|
||||
if (exception != null)
|
||||
{
|
||||
message += "\n" + exception;
|
||||
}
|
||||
if (exception != null) message += "\n" + exception;
|
||||
|
||||
var logMessage = $"{logPrefix} {message}";
|
||||
|
||||
|
||||
@ -17,5 +17,8 @@ public sealed class GodotLoggerFactoryProvider : ILoggerFactoryProvider
|
||||
/// </summary>
|
||||
/// <param name="name">日志记录器的名称</param>
|
||||
/// <returns>返回配置了最小日志级别的Godot日志记录器实例</returns>
|
||||
public ILogger CreateLogger(string name) => new GodotLoggerFactory().GetLogger(name, MinLevel);
|
||||
public ILogger CreateLogger(string name)
|
||||
{
|
||||
return new GodotLoggerFactory().GetLogger(name, MinLevel);
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,3 @@
|
||||
using System.Collections.Generic;
|
||||
using GFramework.Core.extensions;
|
||||
using GFramework.Core.system;
|
||||
using GFramework.Game.Abstractions.assets;
|
||||
|
||||
@ -24,7 +24,7 @@ public static class CommonDiagnostics
|
||||
"Class '{0}' must be declared partial for code generation",
|
||||
"GFramework.Common",
|
||||
DiagnosticSeverity.Error,
|
||||
isEnabledByDefault: true
|
||||
true
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
@ -44,7 +44,7 @@ public static class CommonDiagnostics
|
||||
"{0}",
|
||||
"GFramework.Trace",
|
||||
DiagnosticSeverity.Info,
|
||||
isEnabledByDefault: true
|
||||
true
|
||||
);
|
||||
|
||||
/// <summary>
|
||||
|
||||
@ -128,7 +128,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
|
||||
ClassDeclarationSyntax syntax,
|
||||
INamedTypeSymbol symbol,
|
||||
AttributeData attr)
|
||||
=> true;
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成源代码
|
||||
@ -150,7 +152,9 @@ public abstract class AttributeClassGeneratorBase : IIncrementalGenerator
|
||||
/// <param name="symbol">命名类型符号</param>
|
||||
/// <returns>生成文件的提示名称</returns>
|
||||
protected virtual string GetHintName(INamedTypeSymbol symbol)
|
||||
=> $"{symbol.Name}.g.cs";
|
||||
{
|
||||
return $"{symbol.Name}.g.cs";
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 报告类必须是部分类的错误
|
||||
|
||||
@ -31,7 +31,7 @@ public abstract class AttributeEnumGeneratorBase : IIncrementalGenerator
|
||||
(ctx, _) =>
|
||||
{
|
||||
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);
|
||||
})
|
||||
.Where(x => x.symbol is not null);
|
||||
@ -98,5 +98,7 @@ public abstract class AttributeEnumGeneratorBase : IIncrementalGenerator
|
||||
/// <param name="symbol">命名类型符号</param>
|
||||
/// <returns>生成文件的提示名称</returns>
|
||||
protected virtual string GetHintName(INamedTypeSymbol symbol)
|
||||
=> $"{symbol.Name}.g.cs";
|
||||
{
|
||||
return $"{symbol.Name}.g.cs";
|
||||
}
|
||||
}
|
||||
@ -66,5 +66,7 @@ public static class GeneratorSnapshotTest<TGenerator>
|
||||
/// <param name="text">要标准化的文本</param>
|
||||
/// <returns>标准化后的文本</returns>
|
||||
private static string Normalize(string text)
|
||||
=> text.Replace("\r\n", "\n").Trim();
|
||||
{
|
||||
return text.Replace("\r\n", "\n").Trim();
|
||||
}
|
||||
}
|
||||
@ -75,7 +75,7 @@ public class ContextAwareGeneratorSnapshotTests
|
||||
// 执行生成器快照测试,将生成的代码与预期快照进行比较
|
||||
await GeneratorSnapshotTest<ContextAwareGenerator>.RunAsync(
|
||||
source,
|
||||
snapshotFolder: Path.Combine(
|
||||
Path.Combine(
|
||||
TestContext.CurrentContext.TestDirectory,
|
||||
"rule",
|
||||
"snapshots",
|
||||
|
||||
@ -108,5 +108,7 @@ public sealed class EnumExtensionsGenerator : AttributeEnumGeneratorBase
|
||||
/// <param name="symbol">命名类型符号</param>
|
||||
/// <returns>生成文件的提示名称</returns>
|
||||
protected override string GetHintName(INamedTypeSymbol symbol)
|
||||
=> $"{symbol.Name}.EnumExtensions.g.cs";
|
||||
{
|
||||
return $"{symbol.Name}.EnumExtensions.g.cs";
|
||||
}
|
||||
}
|
||||
@ -102,8 +102,12 @@ public sealed class LoggerGenerator : TypeAttributeClassGeneratorBase
|
||||
/// 可以自定义生成文件名
|
||||
/// </summary>
|
||||
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)
|
||||
=> attr.NamedArguments.FirstOrDefault(kv => kv.Key == name).Value.Value;
|
||||
{
|
||||
return attr.NamedArguments.FirstOrDefault(kv => kv.Key == name).Value.Value;
|
||||
}
|
||||
}
|
||||
@ -117,7 +117,9 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
|
||||
/// <param name="symbol">命名类型符号</param>
|
||||
/// <returns>生成文件的提示名称</returns>
|
||||
protected override string GetHintName(INamedTypeSymbol symbol)
|
||||
=> $"{symbol.Name}.ContextAware.g.cs";
|
||||
{
|
||||
return $"{symbol.Name}.ContextAware.g.cs";
|
||||
}
|
||||
|
||||
// =========================
|
||||
// Context 属性(无 global::,与测试一致)
|
||||
@ -223,10 +225,8 @@ public sealed class ContextAwareGenerator : MetadataAttributeClassGeneratorBase
|
||||
|
||||
default:
|
||||
if (!method.ReturnsVoid)
|
||||
{
|
||||
sb.AppendLine(
|
||||
$" throw new System.NotImplementedException(\"Method '{method.Name}' is not supported.\");");
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user