OSDN Git Service

na-get-lib,FunctionalSubTaskのジェネリック化
[applistation/AppliStation.git] / na-get-lib / NaGet.Packages / Platform.cs
1 using System;\r
2 using System.Reflection;\r
3 using System.Xml.Serialization;\r
4 \r
5 namespace NaGet.Packages\r
6 {\r
7         /// <summary>\r
8         /// PlatformのOSの種類をあらわす\r
9         /// </summary>\r
10         public enum PlatformOSType : ushort\r
11         {\r
12                 WIN95 = 40,\r
13                 WIN98 = 41,\r
14                 WINME = 42,\r
15                 \r
16                 WINNT4 = 140,\r
17                 WIN2K = 150,\r
18                 WINXP = 151,\r
19                 WIN2003 = 152,\r
20                 VISTA = 160,\r
21                 WIN7 = 161,\r
22         }\r
23         \r
24         public class Platform\r
25         {\r
26                 /// <summary>\r
27                 /// ソフトの動作するアーキテクチャ\r
28                 /// </summary>\r
29                 [XmlAttribute]\r
30                 public ProcessorArchitecture Arch = ProcessorArchitecture.X86;\r
31                 \r
32                 /// <summary>\r
33                 /// 動作するOSの種類の配列\r
34                 /// </summary>\r
35                 [XmlIgnore]\r
36                 public PlatformOSType[] OsType;\r
37                 \r
38                 /// <summary>\r
39                 /// OsTypeの文字列表現\r
40                 /// </summary>\r
41                 [XmlAttribute]\r
42                 public string Os {\r
43                         get {\r
44                                 if (OsType == null) return null;\r
45                                 string[] strs = new string[OsType.Length];\r
46                                 for (int i = 0; i < OsType.Length; i++) {\r
47                                         strs[i] = OsType[i].ToString("G");\r
48                                 }\r
49                                 return string.Join(",", strs);\r
50                         }\r
51                         set {\r
52                                 string[] strs = (value ?? string.Empty).Split(',');\r
53                                 System.Collections.Generic.List<PlatformOSType> list = new System.Collections.Generic.List<PlatformOSType>();\r
54                                 for (int i = 0; i < strs.Length; i++) {\r
55                                         try {\r
56                                                 list.Add((PlatformOSType) Enum.Parse(typeof(PlatformOSType), strs[i], true));\r
57                                         } catch (ArgumentException) {\r
58                                         }\r
59                                 }\r
60                                 list.Sort();\r
61                                 OsType = list.ToArray();\r
62                         }\r
63                 }\r
64                 \r
65                 /// <summary>\r
66                 /// 現在のプラットホームで動くか否か?\r
67                 /// </summary>\r
68                 /// <returns>動く場合はtrue</returns>\r
69                 public bool IsRunnable()\r
70                 {\r
71                         return IsRunnableArch() && IsRunnableOS();\r
72                 }\r
73                 \r
74                 /// <summary>\r
75                 /// 現在のマシンのアーキテクチャで動くか否か?\r
76                 /// </summary>\r
77                 /// <returns>動く場合はtrue</returns>\r
78                 public bool IsRunnableArch()\r
79                 {\r
80                         return Arch == GetArch() ||\r
81                                 Arch == ProcessorArchitecture.None ||\r
82                                 Arch == ProcessorArchitecture.MSIL;\r
83                 }\r
84                 \r
85                 /// <summary>\r
86                 /// 現在のマシンのアーキテクチャで動かないが、Wow64で動くか否か?\r
87                 /// 64ビット環境でない場合は常にfalse\r
88                 /// </summary>\r
89                 /// <returns></returns>\r
90                 public bool IsRunnableArchOnWow64()\r
91                 {\r
92                         if (IntPtr.Size == 8) {\r
93                                 return Arch == ProcessorArchitecture.X86;\r
94                         } else {\r
95                                 return false;\r
96                         }\r
97                 }\r
98                 \r
99                 /// <summary>\r
100                 /// 現在のマシンのアーキテクチャを得る\r
101                 /// </summary>\r
102                 /// <returns>現在のマシンのアーキテクチャ</returns>\r
103                 public static ProcessorArchitecture GetArch()\r
104                 {\r
105                         Module[] moduleArray = Assembly.GetExecutingAssembly().GetModules();\r
106                         Module md = moduleArray[0];\r
107 \r
108                         PortableExecutableKinds pekinds;\r
109                         ImageFileMachine ifm;\r
110                         md.GetPEKind(out pekinds, out ifm);\r
111                         \r
112                         switch (ifm) {\r
113                                 case ImageFileMachine.AMD64:\r
114                                         return ProcessorArchitecture.Amd64;\r
115                                 case ImageFileMachine.I386:\r
116                                         return (IntPtr.Size == 4)? ProcessorArchitecture.X86 : ProcessorArchitecture.Amd64;\r
117                                 case ImageFileMachine.IA64:\r
118                                         return ProcessorArchitecture.IA64;\r
119                                 default:\r
120                                         return ProcessorArchitecture.None;\r
121                         }\r
122                 }\r
123                 \r
124                 /// <summary>\r
125                 /// 現在のマシンで動くかを返す\r
126                 /// </summary>\r
127                 /// <returns>現在のマシンで動くか</returns>\r
128                 public bool IsRunnableOS()\r
129                 {\r
130                         if (OsType == null || OsType.Length <= 0) {\r
131                                 return true; // 記述なしはOS全部で動く扱い\r
132                         }\r
133                         \r
134                         PlatformOSType? thisOs = GetOSType();\r
135                         return thisOs != null && Array.BinarySearch(OsType, (PlatformOSType) thisOs) >= 0;\r
136                 }\r
137                 \r
138                 /// <summary>\r
139                 /// 現在のマシンのOSを得る\r
140                 /// </summary>\r
141                 /// <returns>現在のマシンのOS</returns>\r
142                 public static PlatformOSType? GetOSType()\r
143                 {\r
144                         OperatingSystem os = Environment.OSVersion;\r
145                         Version osVer = os.Version;\r
146                         \r
147                         switch (os.Platform) {\r
148                                 case PlatformID.Win32Windows:\r
149                                         if (osVer.Major == 4) {\r
150                                                 switch (osVer.Minor) {\r
151                                                         case 4:\r
152                                                                 return PlatformOSType.WIN95;\r
153                                                         case 10:\r
154                                                                 return PlatformOSType.WIN98;\r
155                                                         case 99:\r
156                                                                 return PlatformOSType.WINME;\r
157                                                 }\r
158                                         }\r
159                                         break;\r
160                                 case PlatformID.Win32NT:\r
161                                         if (osVer.Major == 4) {\r
162                                                 return PlatformOSType.WINNT4;\r
163                                         } else if (osVer.Major == 5) {\r
164                                                 switch (osVer.Minor) {\r
165                                                         case 0:\r
166                                                                 return PlatformOSType.WIN2K;\r
167                                                         case 1:\r
168                                                                 return PlatformOSType.WINXP;\r
169                                                         case 2:\r
170                                                                 return PlatformOSType.WIN2003;\r
171                                                 }\r
172                                         } else if (osVer.Major == 6) {\r
173                                                 switch (osVer.Minor) {\r
174                                                         case 0:\r
175                                                                 return PlatformOSType.VISTA;\r
176                                                         case 1:\r
177                                                                 return PlatformOSType.WIN7;\r
178                                                 }\r
179                                         }\r
180                                         break;\r
181 //                              case PlatformID.WinCE:\r
182 //                                      return null;\r
183 //                              case PlatformID.Unix:\r
184 //                                      return null;\r
185                         }\r
186                         \r
187                         return null;\r
188                 }\r
189         }\r
190 }\r