Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Platform.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 using System;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Linq;
7 using System.Reflection;
8 using SiliconStudio.Core.IO;
9 
10 namespace SiliconStudio.Core
11 {
12  /// <summary>
13  /// Platform specific queries and functions.
14  /// </summary>
15  public static class Platform
16  {
17 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP
18  /// <summary>
19  /// The current running <see cref="PlatformType"/>.
20  /// </summary>
21  public static readonly PlatformType Type = PlatformType.Windows;
22 #elif SILICONSTUDIO_PLATFORM_WINDOWS_PHONE
23  /// <summary>
24  /// The current running <see cref="PlatformType"/>.
25  /// </summary>
26  public static readonly PlatformType Type = PlatformType.WindowsPhone;
27 #elif SILICONSTUDIO_PLATFORM_WINDOWS_STORE
28  /// <summary>
29  /// The current running <see cref="PlatformType"/>.
30  /// </summary>
31  public static readonly PlatformType Type = PlatformType.WindowsStore;
32 #elif SILICONSTUDIO_PLATFORM_ANDROID
33  /// <summary>
34  /// The current running <see cref="PlatformType"/>.
35  /// </summary>
36  public static readonly PlatformType Type = PlatformType.Android;
37 #elif SILICONSTUDIO_PLATFORM_IOS
38  /// <summary>
39  /// The current running <see cref="PlatformType"/>.
40  /// </summary>
41  public static readonly PlatformType Type = PlatformType.iOS;
42 #endif
43 
44  /// <summary>
45  /// Gets a value indicating whether the running platform is windows desktop.
46  /// </summary>
47  /// <value><c>true</c> if this instance is windows desktop; otherwise, <c>false</c>.</value>
48  public static readonly bool IsWindowsDesktop = Type == PlatformType.Windows;
49 
50  /// <summary>
51  /// Gets a value indicating whether the running assembly is a debug assembly.
52  /// </summary>
53  public static readonly bool IsRunningDebugAssembly = GetIsRunningDebugAssembly();
54 
55  /// <summary>
56  /// The system temporary directory.
57  /// </summary>
58  public static string TemporaryDirectory = GetTemporaryDirectory();
59 
60  /// <summary>
61  /// The Application temporary directory.
62  /// </summary>
63  public static string ApplicationTemporaryDirectory = GetApplicationTemporaryDirectory();
64 
65  /// <summary>
66  /// The application local directory, where user can write local data (included in backup).
67  /// </summary>
68  public static readonly string ApplicationLocalDirectory = GetApplicationLocalDirectory();
69 
70  /// <summary>
71  /// The application roaming directory, where user can write roaming data (included in backup).
72  /// </summary>
73  public static readonly string ApplicationRoamingDirectory = GetApplicationRoamingDirectory();
74 
75  /// <summary>
76  /// The application cache directory, where user can write data that won't be backup.
77  /// </summary>
78  public static readonly string ApplicationCacheDirectory = GetApplicationCacheDirectory();
79 
80  /// <summary>
81  /// The application data directory, where data is deployed.
82  /// It could be read-only on some platforms.
83  /// </summary>
84  public static readonly string ApplicationDataDirectory = GetApplicationDataDirectory();
85 
86  /// <summary>
87  /// The (optional) application data subdirectory. If not null or empty, /data will be mounted on <see cref="ApplicationDataDirectory"/>/<see cref="ApplicationDataSubDirectory"/>
88  /// </summary>
89  /// <remarks>This property should not be written after the VirtualFileSystem static initialization. If so, an InvalidOperationExeception will be thrown.</remarks>
90  public static string ApplicationDataSubDirectory
91  {
92  get { return applicationDataSubDirectory; }
93  set
94  {
95  if (virtualFileSystemInitialized)
96  throw new InvalidOperationException("ApplicationDataSubDirectory cannot be modified after the VirtualFileSystem has been initialized.");
97 
98  applicationDataSubDirectory = value;
99  }
100  }
101 
102  /// <summary>
103  /// The application directory, where assemblies are deployed.
104  /// It could be read-only on some platforms.
105  /// </summary>
106  public static readonly string ApplicationBinaryDirectory = GetApplicationBinaryDirectory();
107 
108  public static readonly string ApplicationExecutablePath = GetApplicationExecutablePath();
109 
110  private static string applicationDataSubDirectory = "";
111 
112  private static bool virtualFileSystemInitialized;
113  public static bool IsVirtualFileSystemInitialized
114  {
115  get
116  {
117  return virtualFileSystemInitialized;
118  }
119  internal set
120  {
121  virtualFileSystemInitialized = value;
122  }
123  }
124 
125  private static string GetApplicationLocalDirectory()
126  {
127 #if SILICONSTUDIO_PLATFORM_ANDROID
128  var directory = Path.Combine(PlatformAndroid.Context.FilesDir.AbsolutePath, "local");
129  Directory.CreateDirectory(directory);
130  return directory;
131 #elif SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
132  return Windows.Storage.ApplicationData.Current.LocalFolder.Path;
133 #elif SILICONSTUDIO_PLATFORM_IOS
134  var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", "Local");
135  Directory.CreateDirectory(directory);
136  return directory;
137 #else
138  // TODO: Should we add "local" ?
139  var directory = Path.Combine(GetApplicationBinaryDirectory(), "local");
140  Directory.CreateDirectory(directory);
141  return directory;
142 #endif
143  }
144 
145  private static string GetApplicationRoamingDirectory()
146  {
147 #if SILICONSTUDIO_PLATFORM_ANDROID
148  var directory = Path.Combine(PlatformAndroid.Context.FilesDir.AbsolutePath, "roaming");
149  Directory.CreateDirectory(directory);
150  return directory;
151 #elif SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
152  return Windows.Storage.ApplicationData.Current.RoamingFolder.Path;
153 #elif SILICONSTUDIO_PLATFORM_IOS
154  var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", "Roaming");
155  Directory.CreateDirectory(directory);
156  return directory;
157 #else
158  // TODO: Should we add "local" ?
159  var directory = Path.Combine(GetApplicationBinaryDirectory(), "roaming");
160  Directory.CreateDirectory(directory);
161  return directory;
162 #endif
163  }
164 
165  private static string GetApplicationCacheDirectory()
166  {
167 #if SILICONSTUDIO_PLATFORM_ANDROID
168  var directory = Path.Combine(PlatformAndroid.Context.FilesDir.AbsolutePath, "cache");
169  Directory.CreateDirectory(directory);
170  return directory;
171 #elif SILICONSTUDIO_PLATFORM_WINDOWS_STORE
172  var directory = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "cache");
173  NativeFile.DirectoryCreate(directory);
174  return directory;
175 #elif SILICONSTUDIO_PLATFORM_WINDOWS_PHONE
176  return Windows.Storage.ApplicationData.Current.LocalCacheFolder.Path;
177 #elif SILICONSTUDIO_PLATFORM_IOS
178  var directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "Library", "Caches");
179  Directory.CreateDirectory(directory);
180  return directory;
181 #else
182  // TODO: Should we add "local" ?
183  var directory = Path.Combine(GetApplicationBinaryDirectory(), "cache");
184  Directory.CreateDirectory(directory);
185  return directory;
186 #endif
187  }
188 
189  private static string GetApplicationExecutablePath()
190  {
191 #if SILICONSTUDIO_PLATFORM_WINDOWS_DESKTOP || SILICONSTUDIO_PLATFORM_MONO_MOBILE
192  return (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location;
193 #elif SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
194  return Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, "ParadoxGame.exe"); // Use generic name workaround
195 #else
196  throw new NotImplementedException();
197 #endif
198  }
199 
200  private static string GetTemporaryDirectory()
201  {
202  return GetApplicationTemporaryDirectory();
203  }
204 
205  private static string GetApplicationTemporaryDirectory()
206  {
207 #if SILICONSTUDIO_PLATFORM_ANDROID
208  return PlatformAndroid.Context.CacheDir.AbsolutePath;
209 #elif SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
210  return Windows.Storage.ApplicationData.Current.TemporaryFolder.Path;
211 #elif SILICONSTUDIO_PLATFORM_IOS
212  return Path.Combine (Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "..", "tmp");
213 #else
214  return Path.GetTempPath();
215 #endif
216  }
217 
218  private static string GetApplicationBinaryDirectory()
219  {
220  var result = Path.GetDirectoryName(GetApplicationExecutablePath());
221  if (result == string.Empty)
222  result = ".";
223 
224  return result;
225  }
226 
227  private static string GetApplicationDataDirectory()
228  {
229 #if SILICONSTUDIO_PLATFORM_ANDROID
230  return Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Android/data/" + PlatformAndroid.Context.PackageName + "/data";
231 #elif SILICONSTUDIO_PLATFORM_IOS
232  return MonoTouch.Foundation.NSBundle.MainBundle.BundlePath + "/data";
233 #elif SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
234  return Windows.ApplicationModel.Package.Current.InstalledLocation.Path + @"\data";
235 #else
236  return Path.Combine(Directory.GetCurrentDirectory(), "data");
237 #endif
238  }
239 
240  private static bool GetIsRunningDebugAssembly()
241  {
242 #if SILICONSTUDIO_PLATFORM_WINDOWS_RUNTIME
243  return false;
244 #else
245  var entryAssembly = Assembly.GetEntryAssembly();
246  if (entryAssembly != null)
247  {
248  var debuggableAttribute = entryAssembly.GetCustomAttributes(typeof(DebuggableAttribute)).OfType<DebuggableAttribute>().FirstOrDefault();
249  if (debuggableAttribute != null)
250  {
251  return (debuggableAttribute.DebuggingFlags & DebuggableAttribute.DebuggingModes.DisableOptimizations) != 0;
252  }
253  }
254  return false;
255 #endif
256  }
257  }
258 }
PlatformType
Describes the platform operating system.
Definition: PlatformType.cs:9
Platform specific queries and functions.
Definition: Platform.cs:15