OSDN Git Service

na-get-lib,Fujitsu Scand Allがインストールされている環境下で例外が発生する問題を修正(レジストリのキーのタイプが想定しているのと異なるとき例外...
[applistation/AppliStation.git] / AppliStation / Program.cs
index 9b007df..466944d 100644 (file)
@@ -1,4 +1,4 @@
-using System;\r
+using System;\r
 using System.Collections.Generic;\r
 using System.Windows.Forms;\r
 using System.IO;\r
@@ -12,24 +12,22 @@ namespace AppliStation
                PackageListViewForm form;\r
                \r
                /// <summary>\r
-               /// \83A\83v\83\8a\83P\81[\83V\83\87\83\93\82Ì\83I\83v\83V\83\87\83\93\r
+               /// アプリケーションのオプション\r
                /// </summary>\r
                Dictionary<string, object> appArgs;\r
                \r
-               string[] restAppArgs;\r
-               \r
                public Program()\r
                {\r
                        appArgs = new Dictionary<string, object>();\r
                        appArgs["noupdate"] = false;\r
                        appArgs["cmd"] = string.Empty;\r
                        appArgs["pkgsref"] = string.Empty;\r
-                       \r
-                       form = new PackageListViewForm();\r
+                       appArgs["instsref"] = string.Empty;\r
                }\r
                \r
                void RunNormal()\r
                {\r
+                       form = new PackageListViewForm();\r
                        form.Load += delegate(object sender, EventArgs e) {\r
                                hideSplashScreen();\r
                                form.updateActionInvoke(((bool)appArgs["noupdate"]) != true);\r
@@ -42,15 +40,17 @@ namespace AppliStation
                void RunInstall()\r
                {\r
                        try {\r
-                               NaGet.Packages.Package[] pkgs = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Package[]>((string) appArgs["pkgsref"]);\r
+                               NaGet.Packages.Install.Installation[] insts = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Install.Installation[]>((string) appArgs["instsref"]);\r
                                \r
+                               form = new PackageListViewForm();\r
+                               form.UpdatePackageList();\r
                                hideSplashScreen();\r
-                               form.installActionInvoke(pkgs);\r
+                               form.installActionInvoke(insts);\r
                        } catch (UnauthorizedAccessException e) {\r
-                               MessageBox.Show(string.Format("\8aÇ\97\9d\8eÒ\8c \8cÀ\82É\8f¸\8ai\82µ\82Ä\82¢\82È\82¢\82©\81A\8eÀ\8ds\8c \8cÀ\82É\96â\91è\82ª\82 \82è\82Ü\82·\81B\n(\8fÚ\8d×:{0})", e.Message),\r
+                               MessageBox.Show(string.Format("管理者権限に昇格していないか、実行権限に問題があります。\n(詳細:{0})", e.Message),\r
                                                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
                        } catch (FileNotFoundException e) {\r
-                               MessageBox.Show(string.Format("\83\\83t\83g\8ew\92è\83t\83@\83C\83\8b{0}\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ", e.FileName),\r
+                               MessageBox.Show(string.Format("ソフト指定ファイル{0}が見つかりません", e.FileName),\r
                                                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
                        }\r
                }\r
@@ -60,17 +60,84 @@ namespace AppliStation
                        try {\r
                                NaGet.Packages.Install.InstalledPackage[] pkgs = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Install.InstalledPackage[]>((string) appArgs["pkgsref"]);\r
                                \r
+                               form = new PackageListViewForm();\r
+                               form.UpdatePackageList();\r
                                hideSplashScreen();\r
                                form.uninstallActionInvoke(pkgs);\r
                        } catch (UnauthorizedAccessException e) {\r
-                               MessageBox.Show(string.Format("\8aÇ\97\9d\8eÒ\8c \8cÀ\82É\8f¸\8ai\82µ\82Ä\82¢\82È\82¢\82©\81A\8eÀ\8ds\8c \8cÀ\82É\96â\91è\82ª\82 \82è\82Ü\82·\81B\n(\8fÚ\8d×:{0})", e.Message),\r
+                               MessageBox.Show(string.Format("管理者権限に昇格していないか、実行権限に問題があります。\n(詳細:{0})", e.Message),\r
                                                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
                        } catch (FileNotFoundException e) {\r
-                               MessageBox.Show(string.Format("\83\\83t\83g\8ew\92è\83t\83@\83C\83\8b{0}\82ª\8c©\82Â\82©\82è\82Ü\82¹\82ñ", e.FileName),\r
+                               MessageBox.Show(string.Format("ソフト指定ファイル{0}が見つかりません", e.FileName),\r
                                                Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
                        }\r
                }\r
                \r
+               void RunUpdateAppliStation()\r
+               {\r
+                       if ( splashScreen == null ) {\r
+                               splashScreen = createAndOpenSplashScreen();\r
+                       }\r
+                       \r
+                       System.Threading.Thread.Sleep(5000); /* 5sec待って呼び出しもとの終了を待つ */\r
+                       \r
+                       string newAppliStationDir = Application.StartupPath;\r
+                       string targetDir = Environment.CurrentDirectory;\r
+                       \r
+                       foreach (string file in Directory.GetFiles(newAppliStationDir)) {\r
+                               string ext = Path.GetExtension(file).ToLower();\r
+                               if ((ext == ".exe") || (ext == ".dll") ||\r
+                                   (ext == ".pdb") || (ext == ".png")) {\r
+                                       while (true) { /* loop for retry */\r
+                                               try {           \r
+                                                       File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);\r
+                                                       break;\r
+                                               } catch (IOException ex) {\r
+                                                       DialogResult result = MessageBox.Show(string.Format("AppliStationの更新に失敗しました。\r\n\r\n{0}", ex.Message),\r
+                                                                                             "AppliStation", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);\r
+                                                       if (result == DialogResult.Retry) {\r
+                                                               continue; /* retry */\r
+                                                       } else {\r
+                                                               MessageBox.Show("AppliStationの更新は中断されました。", "AppliStation", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                                                               hideSplashScreen();\r
+                                                               return;\r
+                                                       }\r
+                                               }\r
+                                       }\r
+                               }\r
+                       }\r
+                       System.Threading.Thread.Sleep(10);\r
+                       System.Diagnostics.Process.Start(Path.Combine(targetDir, Path.GetFileName(Application.ExecutablePath)));\r
+                       hideSplashScreen();\r
+                       //Application.Exit(); // Do nothing = exit\r
+               }\r
+               \r
+               /// <summary>\r
+               /// AppliStationの自己更新を行う。アーカイブインストーラーによってあらかじめインストールされたファイルを、\r
+               /// カレントディレクトリにコピーするよう新しいAppliStation.exeを呼ぶ。\r
+               /// コピー元がないまたはそれが新しくないならば何もしない。\r
+               /// </summary>\r
+               /// <returns>更新をしたときtrueをかえす。</returns>\r
+               private bool autoUpdateAppliStation()\r
+               {\r
+                       if (string.IsNullOrEmpty(appArgs["cmd"].ToString())) {\r
+                               string newAppliStationDir = Path.Combine(NaGet.Env.ArchiveProgramFiles, "AppliStation");\r
+                               string thisAppliStation = Application.ExecutablePath;\r
+                               \r
+                               if (Directory.Exists(newAppliStationDir)) {\r
+                                       string newAppliStation = Path.Combine(newAppliStationDir, Path.GetFileName(thisAppliStation));\r
+                                                                               \r
+                                       if ( File.Exists(newAppliStation) &&\r
+                                           (File.GetLastWriteTime(thisAppliStation) != File.GetLastWriteTime(newAppliStation)) ) {\r
+                                               \r
+                                               System.Diagnostics.Process.Start(newAppliStation, "--cmd=updateAppliStation");\r
+                                               return true;\r
+                                       }\r
+                               }\r
+                       }\r
+                       return false;\r
+               }\r
+               \r
                private void hideSplashScreen()\r
                {\r
                        if (splashScreen != null && splashScreen.Visible) {\r
@@ -81,7 +148,22 @@ namespace AppliStation
                \r
                public void Run(string[] args)\r
                {\r
-                       parseArgs(args);\r
+                       try {\r
+                               parseArgs(args);\r
+                       } catch (ApplicationException e) {\r
+                               MessageBox.Show(e.Message, "AppliStation 起動引数エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                               return;\r
+                       }\r
+                       \r
+                       // cmd引数がないときに更新処理を試みる。\r
+                       // cmd引数があるときに更新しないのは、AppliStationから「管理者で実行」などで呼び出された場合に\r
+                       // 更新処理にならないようにするため。\r
+                       if ( string.IsNullOrEmpty((string) appArgs["cmd"]) &&\r
+                           autoUpdateAppliStation() ) {\r
+                               // アップデートしたときは起動しない\r
+                               return;\r
+                       }\r
+                       \r
                        \r
                        switch ((string) appArgs["cmd"]) {\r
                                case "install":\r
@@ -90,6 +172,9 @@ namespace AppliStation
                                case "uninstall":\r
                                        RunUninstall();\r
                                        break;\r
+                               case "updateAppliStation":\r
+                                       RunUpdateAppliStation();\r
+                                       break;\r
                                default:\r
                                        RunNormal();\r
                                        break;\r
@@ -99,7 +184,21 @@ namespace AppliStation
                private void parseArgs(string[] args)\r
                {\r
                        NaGet.ArgParser parser = new NaGet.ArgParser(appArgs);\r
-                       restAppArgs = parser.Parse(args);\r
+                       string[] restAppArgs = parser.Parse(args);\r
+               }\r
+               \r
+               private static Form createAndOpenSplashScreen()\r
+               {\r
+                       System.Drawing.Bitmap bitmap;\r
+                       \r
+                       bitmap = new System.Drawing.Bitmap(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "SplashScreen.png"));\r
+                       Form splashScreen = AppliStation.Util.SprashScreenLayered.CreateSprashScreenLayered(bitmap, System.Drawing.Color.Black);\r
+                       \r
+                       splashScreen.Text = "AppliStation";\r
+                       splashScreen.Icon = System.Drawing.Icon.ExtractAssociatedIcon(Application.ExecutablePath);\r
+                       splashScreen.Show();\r
+                       \r
+                       return splashScreen;\r
                }\r
                \r
                [STAThread]\r
@@ -108,22 +207,21 @@ namespace AppliStation
                        Form splashScreen = null;\r
                        \r
                        try {\r
-                               if (args.Length <= 0) { // HACK \88ø\90\94\83p\81[\83X\82Ì\8e\9e\8aÔ\82³\82¦\91Ò\82Ä\82È\82¢\82Ì\82Å\88ø\90\94\82Ì\97L\96³\82Å\95\\8e¦\82ð\94»\92f\r
-                                       splashScreen = new Form();\r
-                                       splashScreen.FormBorderStyle = FormBorderStyle.None;\r
-                                       splashScreen.BackgroundImage = System.Drawing.Bitmap.FromFile(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "SplashScreen.png"));\r
-                                       splashScreen.Size = splashScreen.BackgroundImage.Size;\r
-                                       splashScreen.ShowIcon = false;\r
-                                       splashScreen.ShowInTaskbar = false;\r
-                                       splashScreen.StartPosition = FormStartPosition.CenterScreen;\r
-                                       splashScreen.Show();\r
+                               if (args.Length <= 0) { // HACK 引数パースの時間さえ待てないので引数の有無で表示を判断\r
+                                       splashScreen = createAndOpenSplashScreen();\r
                                }\r
                                \r
-                               ToolStripManager.VisualStylesEnabled = false; // ToolStrip\82ªLuna\82Å\90Â\82­\82È\82ç\82È\82¢\82æ\82¤\82É\r
-                               Application.EnableVisualStyles(); // Luna\82âVista\82Ì\83f\83U\83C\83\93\82ð\97L\8cø\82É\r
+                               // アーカイブSYSTEM32をパスに足す\r
+                               NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);\r
                                \r
-                               Application.ThreadException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
-                               System.Threading.Thread.GetDomain().UnhandledException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
+                               ToolStripManager.VisualStylesEnabled = false; // ToolStripがLunaで青くならないように\r
+                               Application.EnableVisualStyles(); // LunaやVistaのデザインを有効に\r
+                               \r
+                               // デバッガがアタッチしていないなら、Exceptionを自力でハンドルする\r
+                               if (! System.Diagnostics.Debugger.IsAttached) {\r
+                                       Application.ThreadException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
+                                       System.Threading.Thread.GetDomain().UnhandledException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
+                               }\r
                                \r
                                Program prog = new Program();\r
                                prog.splashScreen = splashScreen;\r