DesktopUrhoInitializer.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.IO;
  3. using System.Reflection;
  4. namespace Urho.Desktop
  5. {
  6. public static class DesktopUrhoInitializer
  7. {
  8. static string assetsDirectory;
  9. /// <summary>
  10. /// Path to a folder containing "Data" folder. CurrentDirectory if null
  11. /// </summary>
  12. public static string AssetsDirectory
  13. {
  14. get { return assetsDirectory; }
  15. set
  16. {
  17. assetsDirectory = value;
  18. if (!string.IsNullOrEmpty(assetsDirectory))
  19. {
  20. if (!Directory.Exists(assetsDirectory))
  21. {
  22. throw new InvalidDataException($"Directory {assetsDirectory} not found");
  23. }
  24. const string coreDataFile = "CoreData.pak";
  25. System.IO.File.Copy(
  26. sourceFileName: Path.Combine(Environment.CurrentDirectory, coreDataFile),
  27. destFileName: Path.Combine(assetsDirectory, coreDataFile),
  28. overwrite: true);
  29. Environment.CurrentDirectory = assetsDirectory;
  30. }
  31. }
  32. }
  33. internal static void OnInited()
  34. {
  35. var currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  36. //unlike the OS X, windows doesn't support FAT binaries
  37. if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
  38. !Environment.Is64BitProcess &&
  39. Is64Bit(Path.Combine(currentPath, "mono-urho.dll")))
  40. {
  41. throw new NotSupportedException("mono-urho.dll is 64bit, but current process is x86 (change target platform from Any CPU/x86 to x64)");
  42. }
  43. }
  44. static bool Is64Bit(string dllPath)
  45. {
  46. using (var fs = new FileStream(dllPath, FileMode.Open, FileAccess.Read))
  47. using (var br = new BinaryReader(fs))
  48. {
  49. fs.Seek(0x3c, SeekOrigin.Begin);
  50. var peOffset = br.ReadInt32();
  51. fs.Seek(peOffset, SeekOrigin.Begin);
  52. var value = br.ReadUInt16();
  53. const ushort IMAGE_FILE_MACHINE_AMD64 = 0x8664;
  54. const ushort IMAGE_FILE_MACHINE_IA64 = 0x200;
  55. return value == IMAGE_FILE_MACHINE_AMD64 ||
  56. value == IMAGE_FILE_MACHINE_IA64;
  57. }
  58. }
  59. }
  60. }