From a973652c959454115721eed406bd58cfd5cc5ca7 Mon Sep 17 00:00:00 2001 From: Say Cheong Date: Tue, 4 Jul 2023 10:54:15 +0900 Subject: [PATCH] Rewrite attribute checking logic --- .../Factory/ClaimStakeRewardFactoryTest.cs | 6 ++++-- .../Action/Scenario/MeadScenarioTest.cs | 19 ++++++------------- .Lib9c.Tests/Action/Snapshot/ActionUtils.cs | 5 ++--- .Lib9c.Tools/SubCommand/Action.cs | 18 +++++------------- Lib9c/Action/ActionObsoleteAttribute.cs | 1 + 5 files changed, 18 insertions(+), 31 deletions(-) diff --git a/.Lib9c.Tests/Action/Factory/ClaimStakeRewardFactoryTest.cs b/.Lib9c.Tests/Action/Factory/ClaimStakeRewardFactoryTest.cs index 9cf3b364f6..15bdc79cde 100644 --- a/.Lib9c.Tests/Action/Factory/ClaimStakeRewardFactoryTest.cs +++ b/.Lib9c.Tests/Action/Factory/ClaimStakeRewardFactoryTest.cs @@ -63,8 +63,10 @@ public void Create_ByVersion_Success(string expectActionType, int version) { var addr = new PrivateKey().ToAddress(); var action = ClaimStakeRewardFactory.CreateByVersion(version, addr); - var actualActionType = (string)(Text)ActionTypeAttribute.ValueOf(action.GetType())!; - Assert.Equal(expectActionType, actualActionType); + var actualActionType = action + .GetType() + .GetCustomAttribute()?.TypeIdentifier; + Assert.Equal(new Text(expectActionType), actualActionType); } } } diff --git a/.Lib9c.Tests/Action/Scenario/MeadScenarioTest.cs b/.Lib9c.Tests/Action/Scenario/MeadScenarioTest.cs index c06a66dc68..04933ee60d 100644 --- a/.Lib9c.Tests/Action/Scenario/MeadScenarioTest.cs +++ b/.Lib9c.Tests/Action/Scenario/MeadScenarioTest.cs @@ -64,23 +64,16 @@ public void Contract() public void UseGas() { Type baseType = typeof(Nekoyume.Action.ActionBase); - Type attrType = typeof(ActionTypeAttribute); - Type obsoleteType = typeof(ActionObsoleteAttribute); bool IsTarget(Type type) { return baseType.IsAssignableFrom(type) && - type.IsDefined(attrType) && - type != typeof(InitializeStates) && - ActionTypeAttribute.ValueOf(type) is { } && - ( - !type.IsDefined(obsoleteType) || - type - .GetCustomAttributes() - .OfType() - .Select(attr => attr.ObsoleteIndex) - .FirstOrDefault() > ActionObsoleteConfig.V200030ObsoleteIndex - ); + type != typeof(InitializeStates) && + type.GetCustomAttribute() is { } && + ( + !(type.GetCustomAttribute()?.ObsoleteIndex is { } obsoleteIndex) || + obsoleteIndex > ActionObsoleteConfig.V200030ObsoleteIndex + ); } var assembly = baseType.Assembly; diff --git a/.Lib9c.Tests/Action/Snapshot/ActionUtils.cs b/.Lib9c.Tests/Action/Snapshot/ActionUtils.cs index 066290acdd..ee11c2ad6a 100644 --- a/.Lib9c.Tests/Action/Snapshot/ActionUtils.cs +++ b/.Lib9c.Tests/Action/Snapshot/ActionUtils.cs @@ -12,9 +12,8 @@ public static IValue GetActionTypeId() { Type attrType = typeof(ActionTypeAttribute); Type actionType = typeof(T); - return actionType.IsDefined(attrType) && - ActionTypeAttribute.ValueOf(actionType) is { } tid - ? tid + return actionType.GetCustomAttribute() is { } attr + ? attr.TypeIdentifier : throw new ArgumentException( $"The action type attribute is missing for {typeof(T)}."); } diff --git a/.Lib9c.Tools/SubCommand/Action.cs b/.Lib9c.Tools/SubCommand/Action.cs index e56ab4c869..2384172811 100644 --- a/.Lib9c.Tools/SubCommand/Action.cs +++ b/.Lib9c.Tools/SubCommand/Action.cs @@ -23,31 +23,23 @@ public void List( ) { Type baseType = typeof(Nekoyume.Action.ActionBase); - Type attrType = typeof(ActionTypeAttribute); - Type obsoleteType = typeof(ActionObsoleteAttribute); bool IsTarget(Type type) { return baseType.IsAssignableFrom(type) && - type.IsDefined(attrType) && - ActionTypeAttribute.ValueOf(type) is { } && + type.GetCustomAttribute() is { } && ( !excludeObsolete || - !type.IsDefined(obsoleteType) || - type - .GetCustomAttributes() - .OfType() - .Select(attr => attr.ObsoleteIndex) - .FirstOrDefault() > blockIndex + !(type.GetCustomAttribute() is { } aoAttr) || + aoAttr.ObsoleteIndex > blockIndex ); } var assembly = baseType.Assembly; var typeIds = assembly.GetTypes() .Where(IsTarget) - .Select(type => ActionTypeAttribute.ValueOf(type)) - .Where(type => type is Text) - .Cast() + .Select(type => type.GetCustomAttribute()?.TypeIdentifier) + .OfType() .OrderBy(type => type); foreach (Text typeId in typeIds) { diff --git a/Lib9c/Action/ActionObsoleteAttribute.cs b/Lib9c/Action/ActionObsoleteAttribute.cs index 5a0ff08d55..53990e9dd7 100644 --- a/Lib9c/Action/ActionObsoleteAttribute.cs +++ b/Lib9c/Action/ActionObsoleteAttribute.cs @@ -14,6 +14,7 @@ namespace Nekoyume.Action /// starting from + 2. /// /// + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] public class ActionObsoleteAttribute : Attribute { public ActionObsoleteAttribute(long obsoleteIndex)