OSDN Git Service

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