OSDN Git Service

na-get-lib,ひとまず依存関係解決関数群を作った。
[applistation/AppliStation.git] / AppliStation / Program.cs
1 using System;\r
2 using System.Collections.Generic;\r
3 using System.Windows.Forms;\r
4 using System.IO;\r
5 \r
6 namespace AppliStation\r
7 {\r
8         class Program\r
9         {\r
10                 Form splashScreen;\r
11                 \r
12                 PackageListViewForm form;\r
13                 \r
14                 /// <summary>\r
15                 /// アプリケーションのオプション\r
16                 /// </summary>\r
17                 Dictionary<string, object> appArgs;\r
18                 \r
19                 string[] restAppArgs;\r
20                 \r
21                 public Program()\r
22                 {\r
23                         appArgs = new Dictionary<string, object>();\r
24                         appArgs["noupdate"] = false;\r
25                         appArgs["cmd"] = string.Empty;\r
26                         appArgs["pkgsref"] = string.Empty;\r
27                         appArgs["instsref"] = string.Empty;\r
28                         \r
29                         form = new PackageListViewForm();\r
30                 }\r
31                 \r
32                 void RunNormal()\r
33                 {\r
34                         form.Load += delegate(object sender, EventArgs e) {\r
35                                 hideSplashScreen();\r
36                                 form.updateActionInvoke(((bool)appArgs["noupdate"]) != true);\r
37                                 \r
38                                 form.UpdatePackageList();\r
39                         };\r
40                         Application.Run(form);\r
41                 }\r
42                 \r
43                 void RunInstall()\r
44                 {\r
45                         try {\r
46                                 NaGet.Packages.Install.Installation[] insts = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Install.Installation[]>((string) appArgs["instsref"]);\r
47                                 \r
48                                 hideSplashScreen();\r
49                                 form.installActionInvoke(insts);\r
50                         } catch (UnauthorizedAccessException e) {\r
51                                 MessageBox.Show(string.Format("管理者権限に昇格していないか、実行権限に問題があります。\n(詳細:{0})", e.Message),\r
52                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
53                         } catch (FileNotFoundException e) {\r
54                                 MessageBox.Show(string.Format("ソフト指定ファイル{0}が見つかりません", e.FileName),\r
55                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
56                         }\r
57                 }\r
58                 \r
59                 void RunUninstall()\r
60                 {\r
61                         try {\r
62                                 NaGet.Packages.Install.InstalledPackage[] pkgs = NaGet.Utils.GetDeserializedObject<NaGet.Packages.Install.InstalledPackage[]>((string) appArgs["pkgsref"]);\r
63                                 \r
64                                 hideSplashScreen();\r
65                                 form.uninstallActionInvoke(pkgs);\r
66                         } catch (UnauthorizedAccessException e) {\r
67                                 MessageBox.Show(string.Format("管理者権限に昇格していないか、実行権限に問題があります。\n(詳細:{0})", e.Message),\r
68                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
69                         } catch (FileNotFoundException e) {\r
70                                 MessageBox.Show(string.Format("ソフト指定ファイル{0}が見つかりません", e.FileName),\r
71                                                 Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);\r
72                         }\r
73                 }\r
74                 \r
75                 private void hideSplashScreen()\r
76                 {\r
77                         if (splashScreen != null && splashScreen.Visible) {\r
78                                 splashScreen.Hide();\r
79                                 splashScreen.Dispose();\r
80                         }\r
81                 }\r
82                 \r
83                 public void Run(string[] args)\r
84                 {\r
85                         try {\r
86                                 parseArgs(args);\r
87                         } catch (ApplicationException e) {\r
88                                 MessageBox.Show(e.Message, "AppliStation 起動引数エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
89                                 return;\r
90                         }\r
91                         \r
92                         switch ((string) appArgs["cmd"]) {\r
93                                 case "install":\r
94                                         RunInstall();\r
95                                         break;\r
96                                 case "uninstall":\r
97                                         RunUninstall();\r
98                                         break;\r
99                                 default:\r
100                                         RunNormal();\r
101                                         break;\r
102                         }\r
103                 }\r
104                 \r
105                 private void parseArgs(string[] args)\r
106                 {\r
107                         NaGet.ArgParser parser = new NaGet.ArgParser(appArgs);\r
108                         restAppArgs = parser.Parse(args);\r
109                 }\r
110                 \r
111                 [STAThread]\r
112                 public static void Main(string[] args)\r
113                 {\r
114                         Form splashScreen = null;\r
115                         \r
116                         try {\r
117                                 if (args.Length <= 0) { // HACK 引数パースの時間さえ待てないので引数の有無で表示を判断\r
118                                         splashScreen = new Form();\r
119                                         splashScreen.FormBorderStyle = FormBorderStyle.None;\r
120                                         splashScreen.BackgroundImage = System.Drawing.Bitmap.FromFile(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "SplashScreen.png"));\r
121                                         splashScreen.Size = splashScreen.BackgroundImage.Size;\r
122                                         splashScreen.ShowIcon = false;\r
123                                         splashScreen.ShowInTaskbar = false;\r
124                                         splashScreen.StartPosition = FormStartPosition.CenterScreen;\r
125                                         splashScreen.Show();\r
126                                 }\r
127                                 \r
128                                 // アーカイブSYSTEM32をパスに足す\r
129                                 NaGet.Utils.AddDirectoryToPath(NaGet.Env.ArchiveSystem32);\r
130                                 \r
131                                 ToolStripManager.VisualStylesEnabled = false; // ToolStripがLunaで青くならないように\r
132                                 Application.EnableVisualStyles(); // LunaやVistaのデザインを有効に\r
133                                 \r
134                                 Application.ThreadException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
135                                 System.Threading.Thread.GetDomain().UnhandledException += AppliStation.Util.ExceptionDialogForm.Application_ThrowException;\r
136                                 \r
137                                 Program prog = new Program();\r
138                                 prog.splashScreen = splashScreen;\r
139                                 prog.Run(args);\r
140                         } catch (Exception e) {\r
141                                 AppliStation.Util.ExceptionDialogForm.Application_ThrowException(null, new System.Threading.ThreadExceptionEventArgs(e));\r
142                         } finally {\r
143                                 if (splashScreen != null) {\r
144                                         splashScreen.Dispose();\r
145                                 }\r
146                         }\r
147                 }\r
148         }\r
149 }\r