OSDN Git Service

pbs.twimg.com の画像URLのフォーマット変更に対応
[opentween/open-tween.git] / OpenTween / ApplicationEvents.cs
index 628cea8..3f2f680 100644 (file)
@@ -25,6 +25,8 @@
 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 // Boston, MA 02110-1301, USA.
 
+#nullable enable
+
 using System;
 using System.Collections.Generic;
 using System.IO;
@@ -53,7 +55,7 @@ namespace OpenTween
         /// <summary>
         /// 起動時に指定されたオプションを取得します
         /// </summary>
-        public static IDictionary<string, string> StartupOptions { get; private set; }
+        public static IDictionary<string, string> StartupOptions { get; private set; } = null!;
 
         /// <summary>
         /// アプリケーションのメイン エントリ ポイントです。
@@ -65,8 +67,8 @@ namespace OpenTween
 
             if (!CheckRuntimeVersion())
             {
-                var message = string.Format(Properties.Resources.CheckRuntimeVersion_Error, ".NET Framework 4.5.1");
-                MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
+                var message = string.Format(Properties.Resources.CheckRuntimeVersion_Error, ".NET Framework 4.7.2");
+                MessageBox.Show(message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                 return 1;
             }
 
@@ -79,13 +81,14 @@ namespace OpenTween
 
             InitCulture();
 
-            // 同じ設定ファイルを使用する OpenTween プロセスの二重起動を防止する
-            string pt = MyCommon.settingPath.Replace("\\", "/") + "/" + Application.ProductName;
-            using (Mutex mt = new Mutex(false, pt))
             {
+                // 同じ設定ファイルを使用する OpenTween プロセスの二重起動を防止する
+                var pt = MyCommon.settingPath.Replace("\\", "/") + "/" + ApplicationSettings.AssemblyName;
+                using var mt = new Mutex(false, pt);
+
                 if (!mt.WaitOne(0, false))
                 {
-                    var text = string.Format(MyCommon.ReplaceAppName(Properties.Resources.StartupText1), MyCommon.GetAssemblyName());
+                    var text = string.Format(MyCommon.ReplaceAppName(Properties.Resources.StartupText1), ApplicationSettings.AssemblyName);
                     MessageBox.Show(text, MyCommon.ReplaceAppName(Properties.Resources.StartupText2), MessageBoxButtons.OK, MessageBoxIcon.Information);
 
                     TryActivatePreviousWindow();
@@ -99,15 +102,16 @@ namespace OpenTween
                 };
                 Application.ThreadException += (s, e) => OnUnhandledException(e.Exception);
                 AppDomain.CurrentDomain.UnhandledException += (s, e) => OnUnhandledException((Exception)e.ExceptionObject);
+                AsyncTimer.UnhandledException += (s, e) => OnUnhandledException(e.Exception);
 
                 Application.EnableVisualStyles();
                 Application.SetCompatibleTextRenderingDefault(false);
                 Application.Run(new TweenMain());
 
                 mt.ReleaseMutex();
-
-                return 0;
             }
+
+            return 0;
         }
 
         /// <summary>
@@ -116,22 +120,19 @@ namespace OpenTween
         private static void WarnIfRunAsAdministrator()
         {
             // UAC が無効なシステムでは警告を表示しない
-            using (var lmKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
-            using (var systemKey = lmKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\"))
-            {
-                var enableLUA = (int?)systemKey?.GetValue("EnableLUA");
-                if (enableLUA != 1)
-                    return;
-            }
+            using var lmKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
+            using var systemKey = lmKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System\");
 
-            using (var currentIdentity = WindowsIdentity.GetCurrent())
+            var enableLUA = (int?)systemKey?.GetValue("EnableLUA");
+            if (enableLUA != 1)
+                return;
+
+            using var currentIdentity = WindowsIdentity.GetCurrent();
+            var principal = new WindowsPrincipal(currentIdentity);
+            if (principal.IsInRole(WindowsBuiltInRole.Administrator))
             {
-                var principal = new WindowsPrincipal(currentIdentity);
-                if (principal.IsInRole(WindowsBuiltInRole.Administrator))
-                {
-                    var message = string.Format(Properties.Resources.WarnIfRunAsAdministrator_Message, Application.ProductName);
-                    MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
-                }
+                var message = string.Format(Properties.Resources.WarnIfRunAsAdministrator_Message, ApplicationSettings.ApplicationName);
+                MessageBox.Show(message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
             }
         }
 
@@ -144,15 +145,14 @@ namespace OpenTween
             if (Type.GetType("Mono.Runtime", false) != null)
                 return true;
 
-            // .NET Framework 4.5.1 以降で動作しているかチェックする
-            // 参照: http://msdn.microsoft.com/en-us/library/hh925568%28v=vs.110%29.aspx
+            // .NET Framework 4.7.2 以降で動作しているかチェックする
+            // 参照: https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed
 
-            using (var lmKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32))
-            using (var ndpKey = lmKey.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\"))
-            {
-                var releaseKey = (int)ndpKey.GetValue("Release");
-                return releaseKey >= 378675;
-            }
+            using var lmKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32);
+            using var ndpKey = lmKey.OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\");
+
+            var releaseKey = (int)ndpKey.GetValue("Release");
+            return releaseKey >= 461808;
         }
 
         /// <summary>
@@ -181,14 +181,14 @@ namespace OpenTween
                 return;
             }
 
-            IntPtr windowHandle = NativeMethods.GetWindowHandle((uint)prevProcess.Id, Application.ProductName);
+            var windowHandle = NativeMethods.GetWindowHandle((uint)prevProcess.Id, ApplicationSettings.ApplicationName);
             if (windowHandle != IntPtr.Zero)
             {
                 NativeMethods.SetActiveWindow(windowHandle);
             }
         }
 
-        private static Process GetPreviousProcess()
+        private static Process? GetPreviousProcess()
         {
             var currentProcess = Process.GetCurrentProcess();
             try
@@ -285,13 +285,13 @@ namespace OpenTween
 
         private static bool SetConfigDirectoryPath()
         {
-            if (StartupOptions.TryGetValue("configDir", out var configDir) && !string.IsNullOrEmpty(configDir))
+            if (StartupOptions.TryGetValue("configDir", out var configDir) && !MyCommon.IsNullOrEmpty(configDir))
             {
                 // 起動オプション /configDir で設定ファイルの参照先を変更できます
                 if (!Directory.Exists(configDir))
                 {
                     var text = string.Format(Properties.Resources.ConfigDirectoryNotExist, configDir);
-                    MessageBox.Show(text, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
+                    MessageBox.Show(text, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                     return false;
                 }
 
@@ -317,7 +317,7 @@ namespace OpenTween
                     var roamingDir = Path.Combine(new[]
                     {
                         Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
-                        Application.ProductName,
+                        ApplicationSettings.ApplicationName,
                     });
                     Directory.CreateDirectory(roamingDir);
 
@@ -359,7 +359,7 @@ namespace OpenTween
                         {
                             // StartupPath に設定ファイルが存在し、Roaming 内のファイルよりも新しい場合のみ警告を表示する
                             var message = string.Format(Properties.Resources.SettingPath_Relocation, Application.StartupPath, MyCommon.settingPath);
-                            MessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
+                            MessageBox.Show(message, ApplicationSettings.ApplicationName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                         }
 
                         // Roaming に設定ファイルを作成 (StartupPath に読み込みに成功した設定ファイルがあれば内容がコピーされる)