app.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*************************************************************************/
  2. /* app.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. //
  31. // This file demonstrates how to initialize EGL in a Windows Store app, using ICoreWindow.
  32. //
  33. #include "app.h"
  34. #include "core/os/dir_access.h"
  35. #include "core/os/file_access.h"
  36. #include "core/os/keyboard.h"
  37. #include "main/main.h"
  38. #include "platform/windows/key_mapping_win.h"
  39. #include <collection.h>
  40. using namespace Windows::ApplicationModel::Core;
  41. using namespace Windows::ApplicationModel::Activation;
  42. using namespace Windows::UI::Core;
  43. using namespace Windows::UI::Input;
  44. using namespace Windows::Devices::Input;
  45. using namespace Windows::UI::Xaml::Input;
  46. using namespace Windows::Foundation;
  47. using namespace Windows::Graphics::Display;
  48. using namespace Windows::System;
  49. using namespace Windows::System::Threading::Core;
  50. using namespace Microsoft::WRL;
  51. using namespace GodotWinRT;
  52. // Helper to convert a length in device-independent pixels (DIPs) to a length in physical pixels.
  53. inline float ConvertDipsToPixels(float dips, float dpi) {
  54. static const float dipsPerInch = 96.0f;
  55. return floor(dips * dpi / dipsPerInch + 0.5f); // Round to nearest integer.
  56. }
  57. // Implementation of the IFrameworkViewSource interface, necessary to run our app.
  58. ref class GodotWinrtViewSource sealed : Windows::ApplicationModel::Core::IFrameworkViewSource {
  59. public:
  60. virtual Windows::ApplicationModel::Core::IFrameworkView ^ CreateView() {
  61. return ref new App();
  62. }
  63. };
  64. // The main function creates an IFrameworkViewSource for our app, and runs the app.
  65. [Platform::MTAThread] int main(Platform::Array<Platform::String ^> ^) {
  66. auto godotApplicationSource = ref new GodotWinrtViewSource();
  67. CoreApplication::Run(godotApplicationSource);
  68. return 0;
  69. }
  70. App::App() :
  71. mWindowClosed(false),
  72. mWindowVisible(true),
  73. mWindowWidth(0),
  74. mWindowHeight(0),
  75. mEglDisplay(EGL_NO_DISPLAY),
  76. mEglContext(EGL_NO_CONTEXT),
  77. mEglSurface(EGL_NO_SURFACE),
  78. number_of_contacts(0) {
  79. }
  80. // The first method called when the IFrameworkView is being created.
  81. void App::Initialize(CoreApplicationView ^ applicationView) {
  82. // Register event handlers for app lifecycle. This example includes Activated, so that we
  83. // can make the CoreWindow active and start rendering on the window.
  84. applicationView->Activated +=
  85. ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(this, &App::OnActivated);
  86. // Logic for other event handlers could go here.
  87. // Information about the Suspending and Resuming event handlers can be found here:
  88. // http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh994930.aspx
  89. os = new OSWinrt;
  90. }
  91. // Called when the CoreWindow object is created (or re-created).
  92. void App::SetWindow(CoreWindow ^ p_window) {
  93. window = p_window;
  94. window->VisibilityChanged +=
  95. ref new TypedEventHandler<CoreWindow ^, VisibilityChangedEventArgs ^>(this, &App::OnVisibilityChanged);
  96. window->Closed +=
  97. ref new TypedEventHandler<CoreWindow ^, CoreWindowEventArgs ^>(this, &App::OnWindowClosed);
  98. window->SizeChanged +=
  99. ref new TypedEventHandler<CoreWindow ^, WindowSizeChangedEventArgs ^>(this, &App::OnWindowSizeChanged);
  100. #if !(WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  101. // Disable all pointer visual feedback for better performance when touching.
  102. // This is not supported on Windows Phone applications.
  103. auto pointerVisualizationSettings = PointerVisualizationSettings::GetForCurrentView();
  104. pointerVisualizationSettings->IsContactFeedbackEnabled = false;
  105. pointerVisualizationSettings->IsBarrelButtonFeedbackEnabled = false;
  106. #endif
  107. window->PointerPressed +=
  108. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerPressed);
  109. window->PointerMoved +=
  110. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerMoved);
  111. window->PointerReleased +=
  112. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerReleased);
  113. window->PointerWheelChanged +=
  114. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerWheelChanged);
  115. mouseChangedNotifier = SignalNotifier::AttachToEvent(L"os_mouse_mode_changed", ref new SignalHandler(
  116. this, &App::OnMouseModeChanged));
  117. mouseChangedNotifier->Enable();
  118. window->CharacterReceived +=
  119. ref new TypedEventHandler<CoreWindow ^, CharacterReceivedEventArgs ^>(this, &App::OnCharacterReceived);
  120. window->KeyDown +=
  121. ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &App::OnKeyDown);
  122. window->KeyUp +=
  123. ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &App::OnKeyUp);
  124. unsigned int argc;
  125. char **argv = get_command_line(&argc);
  126. Main::setup("winrt", argc, argv, false);
  127. // The CoreWindow has been created, so EGL can be initialized.
  128. ContextEGL *context = memnew(ContextEGL(window));
  129. os->set_gl_context(context);
  130. UpdateWindowSize(Size(window->Bounds.Width, window->Bounds.Height));
  131. Main::setup2();
  132. }
  133. static int _get_button(Windows::UI::Input::PointerPoint ^ pt) {
  134. using namespace Windows::UI::Input;
  135. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  136. return BUTTON_LEFT;
  137. #else
  138. switch (pt->Properties->PointerUpdateKind) {
  139. case PointerUpdateKind::LeftButtonPressed:
  140. case PointerUpdateKind::LeftButtonReleased:
  141. return BUTTON_LEFT;
  142. case PointerUpdateKind::RightButtonPressed:
  143. case PointerUpdateKind::RightButtonReleased:
  144. return BUTTON_RIGHT;
  145. case PointerUpdateKind::MiddleButtonPressed:
  146. case PointerUpdateKind::MiddleButtonReleased:
  147. return BUTTON_MIDDLE;
  148. case PointerUpdateKind::XButton1Pressed:
  149. case PointerUpdateKind::XButton1Released:
  150. return BUTTON_WHEEL_UP;
  151. case PointerUpdateKind::XButton2Pressed:
  152. case PointerUpdateKind::XButton2Released:
  153. return BUTTON_WHEEL_DOWN;
  154. default:
  155. break;
  156. }
  157. #endif
  158. return 0;
  159. };
  160. static bool _is_touch(Windows::UI::Input::PointerPoint ^ pointerPoint) {
  161. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  162. return true;
  163. #else
  164. using namespace Windows::Devices::Input;
  165. switch (pointerPoint->PointerDevice->PointerDeviceType) {
  166. case PointerDeviceType::Touch:
  167. case PointerDeviceType::Pen:
  168. return true;
  169. default:
  170. return false;
  171. }
  172. #endif
  173. }
  174. static Windows::Foundation::Point _get_pixel_position(CoreWindow ^ window, Windows::Foundation::Point rawPosition, OS *os) {
  175. Windows::Foundation::Point outputPosition;
  176. // Compute coordinates normalized from 0..1.
  177. // If the coordinates need to be sized to the SDL window,
  178. // we'll do that after.
  179. #if 1 || WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  180. outputPosition.X = rawPosition.X / window->Bounds.Width;
  181. outputPosition.Y = rawPosition.Y / window->Bounds.Height;
  182. #else
  183. switch (DisplayProperties::CurrentOrientation) {
  184. case DisplayOrientations::Portrait:
  185. outputPosition.X = rawPosition.X / window->Bounds.Width;
  186. outputPosition.Y = rawPosition.Y / window->Bounds.Height;
  187. break;
  188. case DisplayOrientations::PortraitFlipped:
  189. outputPosition.X = 1.0f - (rawPosition.X / window->Bounds.Width);
  190. outputPosition.Y = 1.0f - (rawPosition.Y / window->Bounds.Height);
  191. break;
  192. case DisplayOrientations::Landscape:
  193. outputPosition.X = rawPosition.Y / window->Bounds.Height;
  194. outputPosition.Y = 1.0f - (rawPosition.X / window->Bounds.Width);
  195. break;
  196. case DisplayOrientations::LandscapeFlipped:
  197. outputPosition.X = 1.0f - (rawPosition.Y / window->Bounds.Height);
  198. outputPosition.Y = rawPosition.X / window->Bounds.Width;
  199. break;
  200. default:
  201. break;
  202. }
  203. #endif
  204. OS::VideoMode vm = os->get_video_mode();
  205. outputPosition.X *= vm.width;
  206. outputPosition.Y *= vm.height;
  207. return outputPosition;
  208. };
  209. static int _get_finger(uint32_t p_touch_id) {
  210. return p_touch_id % 31; // for now
  211. };
  212. void App::pointer_event(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args, bool p_pressed, bool p_is_wheel) {
  213. Windows::UI::Input::PointerPoint ^ point = args->CurrentPoint;
  214. Windows::Foundation::Point pos = _get_pixel_position(window, point->Position, os);
  215. int but = _get_button(point);
  216. if (_is_touch(point)) {
  217. InputEvent event;
  218. event.type = InputEvent::SCREEN_TOUCH;
  219. event.device = 0;
  220. event.screen_touch.pressed = p_pressed;
  221. event.screen_touch.x = pos.X;
  222. event.screen_touch.y = pos.Y;
  223. event.screen_touch.index = _get_finger(point->PointerId);
  224. last_touch_x[event.screen_touch.index] = pos.X;
  225. last_touch_y[event.screen_touch.index] = pos.Y;
  226. os->input_event(event);
  227. if (number_of_contacts > 1)
  228. return;
  229. }; // fallthrought of sorts
  230. InputEvent event;
  231. event.type = InputEvent::MOUSE_BUTTON;
  232. event.device = 0;
  233. event.mouse_button.pressed = p_pressed;
  234. event.mouse_button.button_index = but;
  235. event.mouse_button.x = pos.X;
  236. event.mouse_button.y = pos.Y;
  237. event.mouse_button.global_x = pos.X;
  238. event.mouse_button.global_y = pos.Y;
  239. if (p_is_wheel) {
  240. if (point->Properties->MouseWheelDelta > 0) {
  241. event.mouse_button.button_index = point->Properties->IsHorizontalMouseWheel ? BUTTON_WHEEL_RIGHT : BUTTON_WHEEL_UP;
  242. } else if (point->Properties->MouseWheelDelta < 0) {
  243. event.mouse_button.button_index = point->Properties->IsHorizontalMouseWheel ? BUTTON_WHEEL_LEFT : BUTTON_WHEEL_DOWN;
  244. }
  245. }
  246. last_touch_x[31] = pos.X;
  247. last_touch_y[31] = pos.Y;
  248. os->input_event(event);
  249. };
  250. void App::OnPointerPressed(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  251. number_of_contacts++;
  252. pointer_event(sender, args, true);
  253. };
  254. void App::OnPointerReleased(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  255. number_of_contacts--;
  256. pointer_event(sender, args, false);
  257. };
  258. void App::OnPointerWheelChanged(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  259. pointer_event(sender, args, true, true);
  260. }
  261. void App::OnMouseModeChanged(Windows::System::Threading::Core::SignalNotifier ^ signalNotifier, bool timedOut) {
  262. OS::MouseMode mode = os->get_mouse_mode();
  263. SignalNotifier ^ notifier = mouseChangedNotifier;
  264. window->Dispatcher->RunAsync(
  265. CoreDispatcherPriority::High,
  266. ref new DispatchedHandler(
  267. [mode, notifier, this]() {
  268. if (mode == OS::MOUSE_MODE_CAPTURED) {
  269. this->MouseMovedToken = MouseDevice::GetForCurrentView()->MouseMoved +=
  270. ref new TypedEventHandler<MouseDevice ^, MouseEventArgs ^>(this, &App::OnMouseMoved);
  271. } else {
  272. MouseDevice::GetForCurrentView()->MouseMoved -= MouseMovedToken;
  273. }
  274. notifier->Enable();
  275. }));
  276. ResetEvent(os->mouse_mode_changed);
  277. }
  278. void App::OnPointerMoved(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  279. Windows::UI::Input::PointerPoint ^ point = args->CurrentPoint;
  280. Windows::Foundation::Point pos = _get_pixel_position(window, point->Position, os);
  281. if (point->IsInContact && _is_touch(point)) {
  282. InputEvent event;
  283. event.type = InputEvent::SCREEN_DRAG;
  284. event.device = 0;
  285. event.screen_drag.x = pos.X;
  286. event.screen_drag.y = pos.Y;
  287. event.screen_drag.index = _get_finger(point->PointerId);
  288. event.screen_drag.relative_x = event.screen_drag.x - last_touch_x[event.screen_drag.index];
  289. event.screen_drag.relative_y = event.screen_drag.y - last_touch_y[event.screen_drag.index];
  290. os->input_event(event);
  291. if (number_of_contacts > 1)
  292. return;
  293. }; // fallthrought of sorts
  294. // In case the mouse grabbed, MouseMoved will handle this
  295. if (os->get_mouse_mode() == OS::MouseMode::MOUSE_MODE_CAPTURED)
  296. return;
  297. InputEvent event;
  298. event.type = InputEvent::MOUSE_MOTION;
  299. event.device = 0;
  300. event.mouse_motion.x = pos.X;
  301. event.mouse_motion.y = pos.Y;
  302. event.mouse_motion.global_x = pos.X;
  303. event.mouse_motion.global_y = pos.Y;
  304. event.mouse_motion.relative_x = pos.X - last_touch_x[31];
  305. event.mouse_motion.relative_y = pos.Y - last_touch_y[31];
  306. last_mouse_pos = pos;
  307. os->input_event(event);
  308. }
  309. void App::OnMouseMoved(MouseDevice ^ mouse_device, MouseEventArgs ^ args) {
  310. // In case the mouse isn't grabbed, PointerMoved will handle this
  311. if (os->get_mouse_mode() != OS::MouseMode::MOUSE_MODE_CAPTURED)
  312. return;
  313. Windows::Foundation::Point pos;
  314. pos.X = last_mouse_pos.X + args->MouseDelta.X;
  315. pos.Y = last_mouse_pos.Y + args->MouseDelta.Y;
  316. InputEvent event;
  317. event.type = InputEvent::MOUSE_MOTION;
  318. event.device = 0;
  319. event.mouse_motion.x = pos.X;
  320. event.mouse_motion.y = pos.Y;
  321. event.mouse_motion.global_x = pos.X;
  322. event.mouse_motion.global_y = pos.Y;
  323. event.mouse_motion.relative_x = args->MouseDelta.X;
  324. event.mouse_motion.relative_y = args->MouseDelta.Y;
  325. last_mouse_pos = pos;
  326. os->input_event(event);
  327. }
  328. void App::key_event(Windows::UI::Core::CoreWindow ^ sender, bool p_pressed, Windows::UI::Core::KeyEventArgs ^ key_args, Windows::UI::Core::CharacterReceivedEventArgs ^ char_args) {
  329. OSWinrt::KeyEvent ke;
  330. InputModifierState mod;
  331. mod.meta = false;
  332. mod.command = false;
  333. mod.control = sender->GetAsyncKeyState(VirtualKey::Control) == CoreVirtualKeyStates::Down;
  334. mod.alt = sender->GetAsyncKeyState(VirtualKey::Menu) == CoreVirtualKeyStates::Down;
  335. mod.shift = sender->GetAsyncKeyState(VirtualKey::Shift) == CoreVirtualKeyStates::Down;
  336. ke.mod_state = mod;
  337. ke.pressed = p_pressed;
  338. if (key_args != nullptr) {
  339. ke.type = OSWinrt::KeyEvent::MessageType::KEY_EVENT_MESSAGE;
  340. ke.unicode = 0;
  341. ke.scancode = KeyMappingWindows::get_keysym((unsigned int)key_args->VirtualKey);
  342. ke.echo = (!p_pressed && !key_args->KeyStatus.IsKeyReleased) || (p_pressed && key_args->KeyStatus.WasKeyDown);
  343. } else {
  344. ke.type = OSWinrt::KeyEvent::MessageType::CHAR_EVENT_MESSAGE;
  345. ke.unicode = char_args->KeyCode;
  346. ke.scancode = 0;
  347. ke.echo = (!p_pressed && !char_args->KeyStatus.IsKeyReleased) || (p_pressed && char_args->KeyStatus.WasKeyDown);
  348. }
  349. os->queue_key_event(ke);
  350. }
  351. void App::OnKeyDown(CoreWindow ^ sender, KeyEventArgs ^ args) {
  352. key_event(sender, true, args);
  353. }
  354. void App::OnKeyUp(CoreWindow ^ sender, KeyEventArgs ^ args) {
  355. key_event(sender, false, args);
  356. }
  357. void App::OnCharacterReceived(CoreWindow ^ sender, CharacterReceivedEventArgs ^ args) {
  358. key_event(sender, true, nullptr, args);
  359. }
  360. // Initializes scene resources
  361. void App::Load(Platform::String ^ entryPoint) {
  362. }
  363. // This method is called after the window becomes active.
  364. void App::Run() {
  365. if (Main::start())
  366. os->run();
  367. }
  368. // Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
  369. // class is torn down while the app is in the foreground.
  370. void App::Uninitialize() {
  371. Main::cleanup();
  372. delete os;
  373. }
  374. // Application lifecycle event handler.
  375. void App::OnActivated(CoreApplicationView ^ applicationView, IActivatedEventArgs ^ args) {
  376. // Run() won't start until the CoreWindow is activated.
  377. CoreWindow::GetForCurrentThread()->Activate();
  378. }
  379. // Window event handlers.
  380. void App::OnVisibilityChanged(CoreWindow ^ sender, VisibilityChangedEventArgs ^ args) {
  381. mWindowVisible = args->Visible;
  382. }
  383. void App::OnWindowClosed(CoreWindow ^ sender, CoreWindowEventArgs ^ args) {
  384. mWindowClosed = true;
  385. }
  386. void App::OnWindowSizeChanged(CoreWindow ^ sender, WindowSizeChangedEventArgs ^ args) {
  387. #if (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)
  388. // On Windows 8.1, apps are resized when they are snapped alongside other apps, or when the device is rotated.
  389. // The default framebuffer will be automatically resized when either of these occur.
  390. // In particular, on a 90 degree rotation, the default framebuffer's width and height will switch.
  391. UpdateWindowSize(args->Size);
  392. #else if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  393. // On Windows Phone 8.1, the window size changes when the device is rotated.
  394. // The default framebuffer will not be automatically resized when this occurs.
  395. // It is therefore up to the app to handle rotation-specific logic in its rendering code.
  396. //os->screen_size_changed();
  397. UpdateWindowSize(args->Size);
  398. #endif
  399. }
  400. void App::UpdateWindowSize(Size size) {
  401. float dpi;
  402. #if (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)
  403. DisplayInformation ^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
  404. dpi = currentDisplayInformation->LogicalDpi;
  405. #else if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  406. dpi = DisplayProperties::LogicalDpi;
  407. #endif
  408. Size pixelSize(ConvertDipsToPixels(size.Width, dpi), ConvertDipsToPixels(size.Height, dpi));
  409. mWindowWidth = static_cast<GLsizei>(pixelSize.Width);
  410. mWindowHeight = static_cast<GLsizei>(pixelSize.Height);
  411. OS::VideoMode vm;
  412. vm.width = mWindowWidth;
  413. vm.height = mWindowHeight;
  414. vm.fullscreen = true;
  415. vm.resizable = false;
  416. os->set_video_mode(vm);
  417. }
  418. char **App::get_command_line(unsigned int *out_argc) {
  419. static char *fail_cl[] = { "-path", "game", NULL };
  420. *out_argc = 2;
  421. FILE *f = _wfopen(L"__cl__.cl", L"rb");
  422. if (f == NULL) {
  423. wprintf(L"Couldn't open command line file.");
  424. return fail_cl;
  425. }
  426. #define READ_LE_4(v) ((int)(##v[3] & 0xFF) << 24) | ((int)(##v[2] & 0xFF) << 16) | ((int)(##v[1] & 0xFF) << 8) | ((int)(##v[0] & 0xFF))
  427. #define CMD_MAX_LEN 65535
  428. uint8_t len[4];
  429. int r = fread(len, sizeof(uint8_t), 4, f);
  430. Platform::Collections::Vector<Platform::String ^> cl;
  431. if (r < 4) {
  432. fclose(f);
  433. wprintf(L"Wrong cmdline length.");
  434. return (fail_cl);
  435. }
  436. int argc = READ_LE_4(len);
  437. for (int i = 0; i < argc; i++) {
  438. r = fread(len, sizeof(uint8_t), 4, f);
  439. if (r < 4) {
  440. fclose(f);
  441. wprintf(L"Wrong cmdline param length.");
  442. return (fail_cl);
  443. }
  444. int strlen = READ_LE_4(len);
  445. if (strlen > CMD_MAX_LEN) {
  446. fclose(f);
  447. wprintf(L"Wrong command length.");
  448. return (fail_cl);
  449. }
  450. char *arg = new char[strlen + 1];
  451. r = fread(arg, sizeof(char), strlen, f);
  452. arg[strlen] = '\0';
  453. if (r == strlen) {
  454. int warg_size = MultiByteToWideChar(CP_UTF8, 0, arg, -1, NULL, 0);
  455. wchar_t *warg = new wchar_t[warg_size];
  456. MultiByteToWideChar(CP_UTF8, 0, arg, -1, warg, warg_size);
  457. cl.Append(ref new Platform::String(warg, warg_size));
  458. } else {
  459. delete[] arg;
  460. fclose(f);
  461. wprintf(L"Error reading command.");
  462. return (fail_cl);
  463. }
  464. }
  465. #undef READ_LE_4
  466. #undef CMD_MAX_LEN
  467. fclose(f);
  468. char **ret = new char *[cl.Size + 1];
  469. for (int i = 0; i < cl.Size; i++) {
  470. int arg_size = WideCharToMultiByte(CP_UTF8, 0, cl.GetAt(i)->Data(), -1, NULL, 0, NULL, NULL);
  471. char *arg = new char[arg_size];
  472. WideCharToMultiByte(CP_UTF8, 0, cl.GetAt(i)->Data(), -1, arg, arg_size, NULL, NULL);
  473. ret[i] = arg;
  474. }
  475. ret[cl.Size] = NULL;
  476. *out_argc = cl.Size;
  477. return ret;
  478. }