OSDN Git Service

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