Seven is the number.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.1 KiB

4 years ago
  1. using System;
  2. using System.Linq;
  3. using System.Runtime.InteropServices;
  4. public class BoltDebugStartSettings
  5. {
  6. #if UNITY_EDITOR
  7. public static bool DebugStartIsServer
  8. {
  9. get { return BoltRuntimeSettings.instance.debugEditorMode == BoltEditorStartMode.Server; }
  10. }
  11. public static bool DebugStartIsClient
  12. {
  13. get { return BoltRuntimeSettings.instance.debugEditorMode == BoltEditorStartMode.Client; }
  14. }
  15. public static int WindowIndex
  16. {
  17. get { return -1; }
  18. }
  19. #elif UNITY_STANDALONE
  20. public static bool DebugStartIsServer
  21. {
  22. get { return Environment.GetCommandLineArgs().Contains("--bolt-debugstart-server"); }
  23. }
  24. public static bool DebugStartIsClient
  25. {
  26. get { return Environment.GetCommandLineArgs().Contains("--bolt-debugstart-client"); }
  27. }
  28. public static int WindowIndex
  29. {
  30. get
  31. {
  32. foreach (string arg in Environment.GetCommandLineArgs())
  33. {
  34. if (arg.StartsWith("--bolt-window-index-"))
  35. {
  36. return int.Parse(arg.Replace("--bolt-window-index-", ""));
  37. }
  38. }
  39. return 0;
  40. }
  41. }
  42. #else
  43. public static bool DebugStartIsServer
  44. {
  45. get { return false; }
  46. }
  47. public static bool DebugStartIsClient
  48. {
  49. get { return false; }
  50. }
  51. public static int WindowIndex
  52. {
  53. get { return -1; }
  54. }
  55. #endif
  56. #if UNITY_STANDALONE_WIN && !UNITY_EDITOR
  57. static readonly object handle = new object();
  58. static HandleRef unityHandle = new HandleRef();
  59. static class HWND {
  60. public static IntPtr
  61. NoTopMost = new IntPtr(-2),
  62. TopMost = new IntPtr(-1),
  63. Top = new IntPtr(0),
  64. Bottom = new IntPtr(1);
  65. }
  66. static class SWP {
  67. public static readonly int
  68. NOSIZE = 0x0001,
  69. NOMOVE = 0x0002,
  70. NOZORDER = 0x0004,
  71. NOREDRAW = 0x0008,
  72. NOACTIVATE = 0x0010,
  73. DRAWFRAME = 0x0020,
  74. FRAMECHANGED = 0x0020,
  75. SHOWWINDOW = 0x0040,
  76. HIDEWINDOW = 0x0080,
  77. NOCOPYBITS = 0x0100,
  78. NOOWNERZORDER = 0x0200,
  79. NOREPOSITION = 0x0200,
  80. NOSENDCHANGING = 0x0400,
  81. DEFERERASE = 0x2000,
  82. ASYNCWINDOWPOS = 0x4000;
  83. }
  84. delegate bool EnumWindowsProc (IntPtr hWnd, IntPtr lParam);
  85. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  86. private static extern int GetWindowThreadProcessId (HandleRef handle, out int processId);
  87. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  88. private static extern bool EnumWindows (EnumWindowsProc callback, IntPtr extraData);
  89. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  90. private static extern int GetSystemMetrics (int index);
  91. [DllImport("user32.dll")]
  92. [return: MarshalAs(UnmanagedType.Bool)]
  93. static extern bool SetWindowPos (IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
  94. static bool Window (IntPtr hWnd, IntPtr lParam) {
  95. int pid = -1;
  96. int unity_pid = System.Diagnostics.Process.GetCurrentProcess().Id;
  97. GetWindowThreadProcessId(new HandleRef(handle, hWnd), out pid);
  98. if (pid == unity_pid) {
  99. unityHandle = new HandleRef(handle, hWnd);
  100. return false;
  101. }
  102. return true;
  103. }
  104. public static void PositionWindow () {
  105. if (DebugStartIsClient || DebugStartIsServer) {
  106. EnumWindows(Window, IntPtr.Zero);
  107. if (unityHandle.Wrapper != null) {
  108. int ww = UnityEngine.Screen.width;
  109. int wh = UnityEngine.Screen.height;
  110. int x = 0;
  111. int y = 0;
  112. int w = GetSystemMetrics(0);
  113. int h = GetSystemMetrics(1);
  114. if (DebugStartIsServer) {
  115. x = w / 2 - (ww / 2);
  116. y = h / 2 - (wh / 2);
  117. } else {
  118. switch (WindowIndex % 4) {
  119. case 1: x = w - ww; break;
  120. case 2: y = h - wh; break;
  121. case 3:
  122. x = w - ww;
  123. y = h - wh;
  124. break;
  125. }
  126. }
  127. SetWindowPos(unityHandle.Handle, HWND.Top, x, y, ww, wh, SWP.NOSIZE);
  128. }
  129. }
  130. }
  131. #else
  132. public static void PositionWindow()
  133. {
  134. }
  135. #endif
  136. }