2 using System.Reflection;
\r
3 using System.Xml.Serialization;
\r
5 namespace NaGet.Packages
\r
8 /// PlatformのOSの種類をあらわす
\r
10 public enum PlatformOSType : ushort
\r
25 public class Platform
\r
31 public ProcessorArchitecture Arch = ProcessorArchitecture.X86;
\r
37 public PlatformOSType[] OsType;
\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
50 return string.Join(",", strs);
\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
57 list.Add((PlatformOSType) Enum.Parse(typeof(PlatformOSType), strs[i], true));
\r
58 } catch (ArgumentException) {
\r
62 OsType = list.ToArray();
\r
67 /// 現在のプラットホームで動くか否か?
\r
69 /// <returns>動く場合はtrue</returns>
\r
70 public bool IsRunnable()
\r
72 return IsRunnableArch() && IsRunnableOS();
\r
76 /// 現在のマシンのアーキテクチャで動くか否か?
\r
78 /// <returns>動く場合はtrue</returns>
\r
79 public bool IsRunnableArch()
\r
81 return Arch == GetArch() ||
\r
82 Arch == ProcessorArchitecture.None ||
\r
83 Arch == ProcessorArchitecture.MSIL;
\r
87 /// 現在のマシンのアーキテクチャで動かないが、Wow64で動くか否か?
\r
88 /// 64ビット環境でない場合は常にfalse
\r
90 /// <returns></returns>
\r
91 public bool IsRunnableArchOnWow64()
\r
93 if (IntPtr.Size == 8) {
\r
94 return Arch == ProcessorArchitecture.X86;
\r
101 /// 現在のマシンのアーキテクチャを得る
\r
103 /// <returns>現在のマシンのアーキテクチャ</returns>
\r
104 public static ProcessorArchitecture GetArch()
\r
106 Module[] moduleArray = Assembly.GetExecutingAssembly().GetModules();
\r
107 Module md = moduleArray[0];
\r
109 PortableExecutableKinds pekinds;
\r
110 ImageFileMachine ifm;
\r
111 md.GetPEKind(out pekinds, out 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
121 return ProcessorArchitecture.None;
\r
128 /// <returns>現在のマシンで動くか</returns>
\r
129 public bool IsRunnableOS()
\r
131 if (OsType == null || OsType.Length <= 0) {
\r
132 return true; // 記述なしはOS全部で動く扱い
\r
135 PlatformOSType? thisOs = GetOSType();
\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
143 return thisOs != null && Array.BinarySearch(OsType, (PlatformOSType) thisOs) >= 0;
\r
149 /// <returns>現在のマシンのOS</returns>
\r
150 public static PlatformOSType? GetOSType()
\r
152 OperatingSystem os = Environment.OSVersion;
\r
153 Version osVer = os.Version;
\r
155 switch (os.Platform) {
\r
156 case PlatformID.Win32Windows:
\r
157 if (osVer.Major == 4) {
\r
158 switch (osVer.Minor) {
\r
160 return PlatformOSType.WIN95;
\r
162 return PlatformOSType.WIN98;
\r
164 return PlatformOSType.WINME;
\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
174 return PlatformOSType.WIN2K;
\r
176 return PlatformOSType.WINXP;
\r
178 return PlatformOSType.WIN2003;
\r
180 } else if (osVer.Major == 6) {
\r
181 switch (osVer.Minor) {
\r
183 return PlatformOSType.VISTA;
\r
185 return PlatformOSType.WIN7;
\r
187 return PlatformOSType.WIN8;
\r
191 // case PlatformID.WinCE:
\r
193 // case PlatformID.Unix:
\r