UrhoEngine.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.IO;
  3. namespace Urho
  4. {
  5. public static class UrhoEngine
  6. {
  7. /// <summary>
  8. /// Init engine
  9. /// </summary>
  10. /// <param name="pathToAssets">Path to a folder containing "Data" folder. CurrentDirectory if null</param>
  11. public static void Init(string pathToAssets = null)
  12. {
  13. if (!string.IsNullOrEmpty(pathToAssets))
  14. {
  15. if (!Directory.Exists(pathToAssets))
  16. {
  17. throw new InvalidDataException($"Directory {pathToAssets} not found");
  18. }
  19. const string coreDataFile = "CoreData.pak";
  20. System.IO.File.Copy(
  21. sourceFileName: Path.Combine(Environment.CurrentDirectory, coreDataFile),
  22. destFileName: Path.Combine(pathToAssets, coreDataFile),
  23. overwrite: true);
  24. Environment.CurrentDirectory = pathToAssets;
  25. }
  26. if (Environment.OSVersion.Platform == PlatformID.Win32NT &&
  27. !Environment.Is64BitProcess &&
  28. Is64Bit("mono-urho.dll"))
  29. {
  30. throw new NotSupportedException("mono-urho.dll is 64bit, but current process is x86 (change target platform from Any CPU/x86 to x64)");
  31. }
  32. Application.EngineInited = true;
  33. }
  34. static bool Is64Bit(string dllPath)
  35. {
  36. using (var fs = new FileStream(dllPath, FileMode.Open, FileAccess.Read))
  37. using (var br = new BinaryReader(fs))
  38. {
  39. fs.Seek(0x3c, SeekOrigin.Begin);
  40. var peOffset = br.ReadInt32();
  41. fs.Seek(peOffset, SeekOrigin.Begin);
  42. var value = br.ReadUInt16();
  43. const ushort IMAGE_FILE_MACHINE_AMD64 = 0x8664;
  44. const ushort IMAGE_FILE_MACHINE_IA64 = 0x200;
  45. return value == IMAGE_FILE_MACHINE_AMD64 ||
  46. value == IMAGE_FILE_MACHINE_IA64;
  47. }
  48. }
  49. }
  50. }