Settings.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using XUnity.AutoTranslator.Plugin.Core.Constants;
  8. using XUnity.AutoTranslator.Plugin.Core.Debugging;
  9. using XUnity.AutoTranslator.Plugin.Core.Extensions;
  10. using XUnity.AutoTranslator.Plugin.Core.Utilities;
  11. namespace XUnity.AutoTranslator.Plugin.Core.Configuration
  12. {
  13. internal static class Settings
  14. {
  15. // cannot be changed
  16. public static readonly string PluginFolder = "Translators";
  17. public static readonly int MaxMaxCharactersPerTranslation = 500;
  18. public static readonly string DefaultLanguage = "en";
  19. public static readonly string DefaultFromLanguage = "ja";
  20. public static readonly string EnglishLanguage = "en";
  21. public static readonly int MaxErrors = 5;
  22. public static readonly float ClipboardDebounceTime = 1f;
  23. public static readonly int MaxTranslationsBeforeShutdown = 8000;
  24. public static readonly int MaxUnstartedJobs = 3500;
  25. public static readonly float IncreaseBatchOperationsEvery = 30;
  26. public static readonly int MaximumStaggers = 6;
  27. public static readonly int PreviousTextStaggerCount = 3;
  28. public static readonly int MaximumConsecutiveFramesTranslated = 90;
  29. public static readonly int MaximumConsecutiveSecondsTranslated = 60;
  30. public static bool UsesWhitespaceBetweenWords = false;
  31. public static string ApplicationName;
  32. public static bool IsShutdown = false;
  33. public static bool IsShutdownFatal = false;
  34. public static int TranslationCount = 0;
  35. public static int MaxAvailableBatchOperations = 40;
  36. public static readonly float MaxTranslationsQueuedPerSecond = 5;
  37. public static readonly int MaxSecondsAboveTranslationThreshold = 30;
  38. public static readonly int TranslationQueueWatchWindow = 6;
  39. public static readonly int BatchSize = 10;
  40. // can be changed
  41. public static string ServiceEndpoint;
  42. public static string Language;
  43. public static string FromLanguage;
  44. public static string OutputFile;
  45. public static string TranslationDirectory;
  46. public static float Delay;
  47. public static int MaxCharactersPerTranslation;
  48. public static bool EnablePrintHierarchy;
  49. public static bool EnableConsole;
  50. public static bool EnableDebugLogs;
  51. public static string AutoTranslationsFilePath;
  52. public static bool EnableIMGUI;
  53. public static bool EnableUGUI;
  54. public static bool EnableNGUI;
  55. public static bool EnableTextMeshPro;
  56. public static bool EnableUtage;
  57. public static bool AllowPluginHookOverride;
  58. public static bool IgnoreWhitespaceInDialogue;
  59. public static bool IgnoreWhitespaceInNGUI;
  60. public static int MinDialogueChars;
  61. public static int ForceSplitTextAfterCharacters;
  62. public static bool EnableMigrations;
  63. public static string MigrationsTag;
  64. public static bool EnableBatching;
  65. public static bool TrimAllText;
  66. public static bool EnableUIResizing;
  67. public static bool UseStaticTranslations;
  68. public static string OverrideFont;
  69. public static string UserAgent;
  70. public static WhitespaceHandlingStrategy WhitespaceRemovalStrategy;
  71. public static float? ResizeUILineSpacingScale;
  72. public static bool ForceUIResizing;
  73. public static string[] IgnoreTextStartingWith;
  74. public static HashSet<string> GameLogTextPaths;
  75. public static bool TextGetterCompatibilityMode;
  76. public static string TextureDirectory;
  77. public static bool EnableTextureTranslation;
  78. public static bool EnableTextureDumping;
  79. public static bool EnableTextureToggling;
  80. public static bool EnableTextureScanOnSceneLoad;
  81. public static bool EnableSpriteRendererHooking;
  82. public static bool LoadUnmodifiedTextures;
  83. public static TextureHashGenerationStrategy TextureHashGenerationStrategy;
  84. public static bool CopyToClipboard;
  85. public static int MaxClipboardCopyCharacters;
  86. public static void SetEndpoint( string id )
  87. {
  88. ServiceEndpoint = id;
  89. Config.Current.Preferences[ "Service" ][ "Endpoint" ].Value = id;
  90. Config.Current.SaveConfig();
  91. }
  92. public static void Configure()
  93. {
  94. try
  95. {
  96. ApplicationName = Path.GetFileNameWithoutExtension( ApplicationInformation.StartupPath );
  97. }
  98. catch( Exception e )
  99. {
  100. ApplicationName = "Unknown";
  101. XuaLogger.Current.Error( e, "An error occurred while getting application name." );
  102. }
  103. ServiceEndpoint = Config.Current.Preferences.GetOrDefault( "Service", "Endpoint", KnownEndpointNames.GoogleTranslate );
  104. Language = Config.Current.Preferences.GetOrDefault( "General", "Language", DefaultLanguage );
  105. FromLanguage = Config.Current.Preferences.GetOrDefault( "General", "FromLanguage", DefaultFromLanguage );
  106. TranslationDirectory = Config.Current.Preferences.GetOrDefault( "Files", "Directory", "Translation" );
  107. OutputFile = Config.Current.Preferences.GetOrDefault( "Files", "OutputFile", @"Translation\_AutoGeneratedTranslations.{lang}.txt" );
  108. EnableIMGUI = Config.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableIMGUI", false );
  109. EnableUGUI = Config.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableUGUI", true );
  110. EnableNGUI = Config.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableNGUI", true );
  111. EnableTextMeshPro = Config.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableTextMeshPro", true );
  112. EnableUtage = Config.Current.Preferences.GetOrDefault( "TextFrameworks", "EnableUtage", true );
  113. AllowPluginHookOverride = Config.Current.Preferences.GetOrDefault( "TextFrameworks", "AllowPluginHookOverride", true );
  114. Delay = Config.Current.Preferences.GetOrDefault( "Behaviour", "Delay", 0f );
  115. MaxCharactersPerTranslation = Config.Current.Preferences.GetOrDefault( "Behaviour", "MaxCharactersPerTranslation", 200 );
  116. IgnoreWhitespaceInDialogue = Config.Current.Preferences.GetOrDefault( "Behaviour", "IgnoreWhitespaceInDialogue", true );
  117. IgnoreWhitespaceInNGUI = Config.Current.Preferences.GetOrDefault( "Behaviour", "IgnoreWhitespaceInNGUI", true );
  118. MinDialogueChars = Config.Current.Preferences.GetOrDefault( "Behaviour", "MinDialogueChars", 20 );
  119. ForceSplitTextAfterCharacters = Config.Current.Preferences.GetOrDefault( "Behaviour", "ForceSplitTextAfterCharacters", 0 );
  120. CopyToClipboard = Config.Current.Preferences.GetOrDefault( "Behaviour", "CopyToClipboard", false );
  121. MaxClipboardCopyCharacters = Config.Current.Preferences.GetOrDefault( "Behaviour", "MaxClipboardCopyCharacters", 450 );
  122. EnableUIResizing = Config.Current.Preferences.GetOrDefault( "Behaviour", "EnableUIResizing", true );
  123. EnableBatching = Config.Current.Preferences.GetOrDefault( "Behaviour", "EnableBatching", true );
  124. TrimAllText = Config.Current.Preferences.GetOrDefault( "Behaviour", "TrimAllText", ClrTypes.AdvEngine == null );
  125. UseStaticTranslations = Config.Current.Preferences.GetOrDefault( "Behaviour", "UseStaticTranslations", true );
  126. OverrideFont = Config.Current.Preferences.GetOrDefault( "Behaviour", "OverrideFont", string.Empty );
  127. ResizeUILineSpacingScale = Config.Current.Preferences.GetOrDefault<float?>( "Behaviour", "ResizeUILineSpacingScale", null );
  128. ForceUIResizing = Config.Current.Preferences.GetOrDefault( "Behaviour", "ForceUIResizing", false );
  129. IgnoreTextStartingWith = Config.Current.Preferences.GetOrDefault( "Behaviour", "IgnoreTextStartingWith", "\\u180e;" )
  130. ?.Split( new[] { ';' }, StringSplitOptions.RemoveEmptyEntries ).Select( x => x.UnescapeJson() ).ToArray() ?? new string[ 0 ];
  131. TextGetterCompatibilityMode = Config.Current.Preferences.GetOrDefault( "Behaviour", "TextGetterCompatibilityMode", false );
  132. GameLogTextPaths = Config.Current.Preferences.GetOrDefault( "Behaviour", "GameLogTextPaths", string.Empty )
  133. ?.Split( new[] { ';' }, StringSplitOptions.RemoveEmptyEntries ).ToHashSet() ?? new HashSet<string>();
  134. GameLogTextPaths.RemoveWhere( x => !x.StartsWith( "/" ) ); // clean up to ensure no 'empty' entries
  135. WhitespaceRemovalStrategy = Config.Current.Preferences.GetOrDefault( "Behaviour", "WhitespaceRemovalStrategy", WhitespaceHandlingStrategy.TrimPerNewline );
  136. TextureDirectory = Config.Current.Preferences.GetOrDefault( "Texture", "TextureDirectory", @"Translation\Texture" );
  137. EnableTextureTranslation = Config.Current.Preferences.GetOrDefault( "Texture", "EnableTextureTranslation", false );
  138. EnableTextureDumping = Config.Current.Preferences.GetOrDefault( "Texture", "EnableTextureDumping", false );
  139. EnableTextureToggling = Config.Current.Preferences.GetOrDefault( "Texture", "EnableTextureToggling", false );
  140. EnableTextureScanOnSceneLoad = Config.Current.Preferences.GetOrDefault( "Texture", "EnableTextureScanOnSceneLoad", false );
  141. EnableSpriteRendererHooking = Config.Current.Preferences.GetOrDefault( "Texture", "EnableSpriteRendererHooking", false );
  142. LoadUnmodifiedTextures = Config.Current.Preferences.GetOrDefault( "Texture", "LoadUnmodifiedTextures", false );
  143. TextureHashGenerationStrategy = Config.Current.Preferences.GetOrDefault( "Texture", "TextureHashGenerationStrategy", TextureHashGenerationStrategy.FromImageName );
  144. if( MaxCharactersPerTranslation > MaxMaxCharactersPerTranslation )
  145. {
  146. Config.Current.Preferences[ "Behaviour" ][ "MaxCharactersPerTranslation" ].Value = MaxMaxCharactersPerTranslation.ToString( CultureInfo.InvariantCulture );
  147. MaxCharactersPerTranslation = MaxMaxCharactersPerTranslation;
  148. }
  149. UserAgent = Config.Current.Preferences.GetOrDefault( "Http", "UserAgent", string.Empty );
  150. EnablePrintHierarchy = Config.Current.Preferences.GetOrDefault( "Debug", "EnablePrintHierarchy", false );
  151. EnableConsole = Config.Current.Preferences.GetOrDefault( "Debug", "EnableConsole", false );
  152. EnableDebugLogs = Config.Current.Preferences.GetOrDefault( "Debug", "EnableLog", false );
  153. EnableMigrations = Config.Current.Preferences.GetOrDefault( "Migrations", "Enable", true );
  154. MigrationsTag = Config.Current.Preferences.GetOrDefault( "Migrations", "Tag", string.Empty );
  155. AutoTranslationsFilePath = Path.Combine( Config.Current.DataPath, OutputFile.Replace( "{lang}", Language ) ).Replace( "/", "\\" ).Parameterize();
  156. UsesWhitespaceBetweenWords = LanguageHelper.RequiresWhitespaceUponLineMerging( FromLanguage );
  157. if( EnableMigrations )
  158. {
  159. Migrate();
  160. }
  161. // update tag
  162. MigrationsTag = Config.Current.Preferences[ "Migrations" ][ "Tag" ].Value = PluginData.Version;
  163. Config.Current.SaveConfig();
  164. }
  165. private static void Migrate()
  166. {
  167. }
  168. }
  169. }