瀏覽代碼

support enums in config

Scrublord1336 6 年之前
父節點
當前提交
2bfc3430ee
共有 1 個文件被更改,包括 32 次插入4 次删除
  1. 32 4
      src/XUnity.AutoTranslator.Plugin.Core/Configuration/IniKeyExtensions.cs

+ 32 - 4
src/XUnity.AutoTranslator.Plugin.Core/Configuration/IniKeyExtensions.cs

@@ -16,12 +16,26 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
             var value = that.Value;
             if( string.IsNullOrEmpty( value ) )
             {
-               that.Value = Convert.ToString( defaultValue, CultureInfo.InvariantCulture );
+               if( typeof( T ).IsEnum )
+               {
+                  that.Value = Enum.GetName( typeof( T ), defaultValue );
+               }
+               else
+               {
+                  that.Value = Convert.ToString( defaultValue, CultureInfo.InvariantCulture );
+               }
                return defaultValue;
             }
             else
             {
-               return (T)Convert.ChangeType( that.Value, typeof( T ), CultureInfo.InvariantCulture );
+               if( typeof( T ).IsEnum )
+               {
+                  return (T)Enum.Parse( typeof( T ), that.Value, true );
+               }
+               else
+               {
+                  return (T)Convert.ChangeType( that.Value, typeof( T ), CultureInfo.InvariantCulture );
+               }
             }
          }
          else
@@ -29,12 +43,26 @@ namespace XUnity.AutoTranslator.Plugin.Core.Configuration
             var value = that.Value;
             if( value == null )
             {
-               that.Value = Convert.ToString( defaultValue, CultureInfo.InvariantCulture );
+               if( typeof( T ).IsEnum )
+               {
+                  that.Value = Enum.GetName( typeof( T ), defaultValue );
+               }
+               else
+               {
+                  that.Value = Convert.ToString( defaultValue, CultureInfo.InvariantCulture );
+               }
                return defaultValue;
             }
             else
             {
-               return (T)Convert.ChangeType( that.Value, typeof( T ), CultureInfo.InvariantCulture );
+               if( typeof( T ).IsEnum )
+               {
+                  return (T)Enum.Parse( typeof( T ), that.Value, true );
+               }
+               else
+               {
+                  return (T)Convert.ChangeType( that.Value, typeof( T ), CultureInfo.InvariantCulture );
+               }
             }
          }
       }