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
126 /// 現在のマシンのアーキテクチャを得る
\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
136 return thisOs != null && Array.BinarySearch(OsType, (PlatformOSType) thisOs) >= 0;
\r
142 /// <returns>現在のマシンのOS</returns>
\r
143 public static PlatformOSType? GetOSType()
\r
145 OperatingSystem os = Environment.OSVersion;
\r
146 Version osVer = os.Version;
\r
148 switch (os.Platform) {
\r
149 case PlatformID.Win32Windows:
\r
150 if (osVer.Major == 4) {
\r
151 switch (osVer.Minor) {
\r
153 return PlatformOSType.WIN95;
\r
155 return PlatformOSType.WIN98;
\r
157 return PlatformOSType.WINME;
\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
167 return PlatformOSType.WIN2K;
\r
169 return PlatformOSType.WINXP;
\r
171 return PlatformOSType.WIN2003;
\r
173 } else if (osVer.Major == 6) {
\r
174 switch (osVer.Minor) {
\r
176 return PlatformOSType.VISTA;
\r
178 return PlatformOSType.WIN7;
\r
182 // case PlatformID.WinCE:
\r
184 // case PlatformID.Unix:
\r