OSDN Git Service

AppliStation,全部のバージョン表記を1.2.1にしたりした。また、win7がwin2008と間違って認識するバグの修正
[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                 WIN2008 = 160,\r
22                 WIN7 = 161,\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                         return thisOs != null && Array.BinarySearch(OsType, (PlatformOSType) thisOs) >= 0;\r
137                 }\r
138                 \r
139                 /// <summary>\r
140                 /// 現在のマシンのOSを得る\r
141                 /// </summary>\r
142                 /// <returns>現在のマシンのOS</returns>\r
143                 public static PlatformOSType? GetOSType()\r
144                 {\r
145                         OperatingSystem os = Environment.OSVersion;\r
146                         Version osVer = os.Version;\r
147                         \r
148                         switch (os.Platform) {\r
149                                 case PlatformID.Win32Windows:\r
150                                         if (osVer.Major == 4) {\r
151                                                 switch (osVer.Minor) {\r
152                                                         case 4:\r
153                                                                 return PlatformOSType.WIN95;\r
154                                                         case 10:\r
155                                                                 return PlatformOSType.WIN98;\r
156                                                         case 99:\r
157                                                                 return PlatformOSType.WINME;\r
158                                                 }\r
159                                         }\r
160                                         break;\r
161                                 case PlatformID.Win32NT:\r
162                                         if (osVer.Major == 4) {\r
163                                                 return PlatformOSType.WINNT4;\r
164                                         } else if (osVer.Major == 5) {\r
165                                                 switch (osVer.Minor) {\r
166                                                         case 0:\r
167                                                                 return PlatformOSType.WIN2K;\r
168                                                         case 1:\r
169                                                                 return PlatformOSType.WINXP;\r
170                                                         case 2:\r
171                                                                 return PlatformOSType.WIN2003;\r
172                                                 }\r
173                                         } else if (osVer.Major == 6) {\r
174                                                 switch (osVer.Minor) {\r
175                                                         case 0:\r
176                                                                 return PlatformOSType.VISTA;\r
177                                                         case 1:\r
178                                                                 return PlatformOSType.WIN7;\r
179                                                 }\r
180                                         }\r
181                                         break;\r
182 //                              case PlatformID.WinCE:\r
183 //                                      return null;\r
184 //                              case PlatformID.Unix:\r
185 //                                      return null;\r
186                         }\r
187                         \r
188                         return null;\r
189                 }\r
190         }\r
191 }\r