OSDN Git Service

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