app.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*************************************************************************/
  2. /* app.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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_windows.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 GodotUWP;
  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 GodotUWPViewSource 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 GodotUWPViewSource();
  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. }
  79. // The first method called when the IFrameworkView is being created.
  80. void App::Initialize(CoreApplicationView ^ applicationView) {
  81. // Register event handlers for app lifecycle. This example includes Activated, so that we
  82. // can make the CoreWindow active and start rendering on the window.
  83. applicationView->Activated +=
  84. ref new TypedEventHandler<CoreApplicationView ^, IActivatedEventArgs ^>(this, &App::OnActivated);
  85. // Logic for other event handlers could go here.
  86. // Information about the Suspending and Resuming event handlers can be found here:
  87. // http://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh994930.aspx
  88. os = new OS_UWP;
  89. }
  90. // Called when the CoreWindow object is created (or re-created).
  91. void App::SetWindow(CoreWindow ^ p_window) {
  92. window = p_window;
  93. window->VisibilityChanged +=
  94. ref new TypedEventHandler<CoreWindow ^, VisibilityChangedEventArgs ^>(this, &App::OnVisibilityChanged);
  95. window->Closed +=
  96. ref new TypedEventHandler<CoreWindow ^, CoreWindowEventArgs ^>(this, &App::OnWindowClosed);
  97. window->SizeChanged +=
  98. ref new TypedEventHandler<CoreWindow ^, WindowSizeChangedEventArgs ^>(this, &App::OnWindowSizeChanged);
  99. #if !(WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  100. // Disable all pointer visual feedback for better performance when touching.
  101. // This is not supported on Windows Phone applications.
  102. auto pointerVisualizationSettings = PointerVisualizationSettings::GetForCurrentView();
  103. pointerVisualizationSettings->IsContactFeedbackEnabled = false;
  104. pointerVisualizationSettings->IsBarrelButtonFeedbackEnabled = false;
  105. #endif
  106. window->PointerPressed +=
  107. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerPressed);
  108. window->PointerMoved +=
  109. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerMoved);
  110. window->PointerReleased +=
  111. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerReleased);
  112. window->PointerWheelChanged +=
  113. ref new TypedEventHandler<CoreWindow ^, PointerEventArgs ^>(this, &App::OnPointerWheelChanged);
  114. mouseChangedNotifier = SignalNotifier::AttachToEvent(L"os_mouse_mode_changed", ref new SignalHandler(this, &App::OnMouseModeChanged));
  115. mouseChangedNotifier->Enable();
  116. window->CharacterReceived +=
  117. ref new TypedEventHandler<CoreWindow ^, CharacterReceivedEventArgs ^>(this, &App::OnCharacterReceived);
  118. window->KeyDown +=
  119. ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &App::OnKeyDown);
  120. window->KeyUp +=
  121. ref new TypedEventHandler<CoreWindow ^, KeyEventArgs ^>(this, &App::OnKeyUp);
  122. os->set_window(window);
  123. unsigned int argc;
  124. char **argv = get_command_line(&argc);
  125. Main::setup("uwp", argc, argv, false);
  126. UpdateWindowSize(Size(window->Bounds.Width, window->Bounds.Height));
  127. Main::setup2();
  128. }
  129. static int _get_button(Windows::UI::Input::PointerPoint ^ pt) {
  130. using namespace Windows::UI::Input;
  131. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  132. return BUTTON_LEFT;
  133. #else
  134. switch (pt->Properties->PointerUpdateKind) {
  135. case PointerUpdateKind::LeftButtonPressed:
  136. case PointerUpdateKind::LeftButtonReleased:
  137. return BUTTON_LEFT;
  138. case PointerUpdateKind::RightButtonPressed:
  139. case PointerUpdateKind::RightButtonReleased:
  140. return BUTTON_RIGHT;
  141. case PointerUpdateKind::MiddleButtonPressed:
  142. case PointerUpdateKind::MiddleButtonReleased:
  143. return BUTTON_MIDDLE;
  144. case PointerUpdateKind::XButton1Pressed:
  145. case PointerUpdateKind::XButton1Released:
  146. return BUTTON_WHEEL_UP;
  147. case PointerUpdateKind::XButton2Pressed:
  148. case PointerUpdateKind::XButton2Released:
  149. return BUTTON_WHEEL_DOWN;
  150. default:
  151. break;
  152. }
  153. #endif
  154. return 0;
  155. };
  156. static bool _is_touch(Windows::UI::Input::PointerPoint ^ pointerPoint) {
  157. #if WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP
  158. return true;
  159. #else
  160. using namespace Windows::Devices::Input;
  161. switch (pointerPoint->PointerDevice->PointerDeviceType) {
  162. case PointerDeviceType::Touch:
  163. case PointerDeviceType::Pen:
  164. return true;
  165. default:
  166. return false;
  167. }
  168. #endif
  169. }
  170. static Windows::Foundation::Point _get_pixel_position(CoreWindow ^ window, Windows::Foundation::Point rawPosition, OS *os) {
  171. Windows::Foundation::Point outputPosition;
  172. // Compute coordinates normalized from 0..1.
  173. // If the coordinates need to be sized to the SDL window,
  174. // we'll do that after.
  175. #if 1 || WINAPI_FAMILY != WINAPI_FAMILY_PHONE_APP
  176. outputPosition.X = rawPosition.X / window->Bounds.Width;
  177. outputPosition.Y = rawPosition.Y / window->Bounds.Height;
  178. #else
  179. switch (DisplayProperties::CurrentOrientation) {
  180. case DisplayOrientations::Portrait:
  181. outputPosition.X = rawPosition.X / window->Bounds.Width;
  182. outputPosition.Y = rawPosition.Y / window->Bounds.Height;
  183. break;
  184. case DisplayOrientations::PortraitFlipped:
  185. outputPosition.X = 1.0f - (rawPosition.X / window->Bounds.Width);
  186. outputPosition.Y = 1.0f - (rawPosition.Y / window->Bounds.Height);
  187. break;
  188. case DisplayOrientations::Landscape:
  189. outputPosition.X = rawPosition.Y / window->Bounds.Height;
  190. outputPosition.Y = 1.0f - (rawPosition.X / window->Bounds.Width);
  191. break;
  192. case DisplayOrientations::LandscapeFlipped:
  193. outputPosition.X = 1.0f - (rawPosition.Y / window->Bounds.Height);
  194. outputPosition.Y = rawPosition.X / window->Bounds.Width;
  195. break;
  196. default:
  197. break;
  198. }
  199. #endif
  200. OS::VideoMode vm = os->get_video_mode();
  201. outputPosition.X *= vm.width;
  202. outputPosition.Y *= vm.height;
  203. return outputPosition;
  204. };
  205. static int _get_finger(uint32_t p_touch_id) {
  206. return p_touch_id % 31; // for now
  207. };
  208. void App::pointer_event(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args, bool p_pressed, bool p_is_wheel) {
  209. Windows::UI::Input::PointerPoint ^ point = args->CurrentPoint;
  210. Windows::Foundation::Point pos = _get_pixel_position(window, point->Position, os);
  211. int but = _get_button(point);
  212. if (_is_touch(point)) {
  213. Ref<InputEventScreenTouch> screen_touch;
  214. screen_touch.instance();
  215. screen_touch->set_device(0);
  216. screen_touch->set_pressed(p_pressed);
  217. screen_touch->set_position(Vector2(pos.X, pos.Y));
  218. screen_touch->set_index(_get_finger(point->PointerId));
  219. last_touch_x[screen_touch->get_index()] = pos.X;
  220. last_touch_y[screen_touch->get_index()] = pos.Y;
  221. os->input_event(screen_touch);
  222. } else {
  223. Ref<InputEventMouseButton> mouse_button;
  224. mouse_button.instance();
  225. mouse_button->set_device(0);
  226. mouse_button->set_pressed(p_pressed);
  227. mouse_button->set_button_index(but);
  228. mouse_button->set_position(Vector2(pos.X, pos.Y));
  229. mouse_button->set_global_position(Vector2(pos.X, pos.Y));
  230. if (p_is_wheel) {
  231. if (point->Properties->MouseWheelDelta > 0) {
  232. mouse_button->set_button_index(point->Properties->IsHorizontalMouseWheel ? BUTTON_WHEEL_RIGHT : BUTTON_WHEEL_UP);
  233. } else if (point->Properties->MouseWheelDelta < 0) {
  234. mouse_button->set_button_index(point->Properties->IsHorizontalMouseWheel ? BUTTON_WHEEL_LEFT : BUTTON_WHEEL_DOWN);
  235. }
  236. }
  237. last_touch_x[31] = pos.X;
  238. last_touch_y[31] = pos.Y;
  239. os->input_event(mouse_button);
  240. if (p_is_wheel) {
  241. // Send release for mouse wheel
  242. mouse_button->set_pressed(false);
  243. os->input_event(mouse_button);
  244. }
  245. }
  246. };
  247. void App::OnPointerPressed(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  248. pointer_event(sender, args, true);
  249. };
  250. void App::OnPointerReleased(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  251. pointer_event(sender, args, false);
  252. };
  253. void App::OnPointerWheelChanged(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  254. pointer_event(sender, args, true, true);
  255. }
  256. void App::OnMouseModeChanged(Windows::System::Threading::Core::SignalNotifier ^ signalNotifier, bool timedOut) {
  257. OS::MouseMode mode = os->get_mouse_mode();
  258. SignalNotifier ^ notifier = mouseChangedNotifier;
  259. window->Dispatcher->RunAsync(
  260. CoreDispatcherPriority::High,
  261. ref new DispatchedHandler(
  262. [mode, notifier, this]() {
  263. if (mode == OS::MOUSE_MODE_CAPTURED) {
  264. this->MouseMovedToken = MouseDevice::GetForCurrentView()->MouseMoved +=
  265. ref new TypedEventHandler<MouseDevice ^, MouseEventArgs ^>(this, &App::OnMouseMoved);
  266. } else {
  267. MouseDevice::GetForCurrentView()->MouseMoved -= MouseMovedToken;
  268. }
  269. notifier->Enable();
  270. }));
  271. ResetEvent(os->mouse_mode_changed);
  272. }
  273. void App::OnPointerMoved(Windows::UI::Core::CoreWindow ^ sender, Windows::UI::Core::PointerEventArgs ^ args) {
  274. Windows::UI::Input::PointerPoint ^ point = args->CurrentPoint;
  275. Windows::Foundation::Point pos = _get_pixel_position(window, point->Position, os);
  276. if (_is_touch(point)) {
  277. Ref<InputEventScreenDrag> screen_drag;
  278. screen_drag.instance();
  279. screen_drag->set_device(0);
  280. screen_drag->set_position(Vector2(pos.X, pos.Y));
  281. screen_drag->set_index(_get_finger(point->PointerId));
  282. screen_drag->set_relative(Vector2(screen_drag->get_position().x - last_touch_x[screen_drag->get_index()], screen_drag->get_position().y - last_touch_y[screen_drag->get_index()]));
  283. os->input_event(screen_drag);
  284. } else {
  285. // In case the mouse grabbed, MouseMoved will handle this
  286. if (os->get_mouse_mode() == OS::MouseMode::MOUSE_MODE_CAPTURED)
  287. return;
  288. Ref<InputEventMouseMotion> mouse_motion;
  289. mouse_motion.instance();
  290. mouse_motion->set_device(0);
  291. mouse_motion->set_position(Vector2(pos.X, pos.Y));
  292. mouse_motion->set_global_position(Vector2(pos.X, pos.Y));
  293. mouse_motion->set_relative(Vector2(pos.X - last_touch_x[31], pos.Y - last_touch_y[31]));
  294. last_mouse_pos = pos;
  295. os->input_event(mouse_motion);
  296. }
  297. }
  298. void App::OnMouseMoved(MouseDevice ^ mouse_device, MouseEventArgs ^ args) {
  299. // In case the mouse isn't grabbed, PointerMoved will handle this
  300. if (os->get_mouse_mode() != OS::MouseMode::MOUSE_MODE_CAPTURED)
  301. return;
  302. Windows::Foundation::Point pos;
  303. pos.X = last_mouse_pos.X + args->MouseDelta.X;
  304. pos.Y = last_mouse_pos.Y + args->MouseDelta.Y;
  305. Ref<InputEventMouseMotion> mouse_motion;
  306. mouse_motion.instance();
  307. mouse_motion->set_device(0);
  308. mouse_motion->set_position(Vector2(pos.X, pos.Y));
  309. mouse_motion->set_global_position(Vector2(pos.X, pos.Y));
  310. mouse_motion->set_relative(Vector2(args->MouseDelta.X, args->MouseDelta.Y));
  311. last_mouse_pos = pos;
  312. os->input_event(mouse_motion);
  313. }
  314. void App::key_event(Windows::UI::Core::CoreWindow ^ sender, bool p_pressed, Windows::UI::Core::KeyEventArgs ^ key_args, Windows::UI::Core::CharacterReceivedEventArgs ^ char_args) {
  315. OS_UWP::KeyEvent ke;
  316. ke.control = sender->GetAsyncKeyState(VirtualKey::Control) == CoreVirtualKeyStates::Down;
  317. ke.alt = sender->GetAsyncKeyState(VirtualKey::Menu) == CoreVirtualKeyStates::Down;
  318. ke.shift = sender->GetAsyncKeyState(VirtualKey::Shift) == CoreVirtualKeyStates::Down;
  319. ke.pressed = p_pressed;
  320. if (key_args != nullptr) {
  321. ke.type = OS_UWP::KeyEvent::MessageType::KEY_EVENT_MESSAGE;
  322. ke.unicode = 0;
  323. ke.scancode = KeyMappingWindows::get_keysym((unsigned int)key_args->VirtualKey);
  324. ke.physical_scancode = KeyMappingWindows::get_scansym((unsigned int)key_args->KeyStatus.ScanCode, key_args->KeyStatus.IsExtendedKey);
  325. ke.echo = (!p_pressed && !key_args->KeyStatus.IsKeyReleased) || (p_pressed && key_args->KeyStatus.WasKeyDown);
  326. } else {
  327. ke.type = OS_UWP::KeyEvent::MessageType::CHAR_EVENT_MESSAGE;
  328. ke.unicode = char_args->KeyCode;
  329. ke.scancode = 0;
  330. ke.physical_scancode = 0;
  331. ke.echo = (!p_pressed && !char_args->KeyStatus.IsKeyReleased) || (p_pressed && char_args->KeyStatus.WasKeyDown);
  332. }
  333. os->queue_key_event(ke);
  334. }
  335. void App::OnKeyDown(CoreWindow ^ sender, KeyEventArgs ^ args) {
  336. key_event(sender, true, args);
  337. }
  338. void App::OnKeyUp(CoreWindow ^ sender, KeyEventArgs ^ args) {
  339. key_event(sender, false, args);
  340. }
  341. void App::OnCharacterReceived(CoreWindow ^ sender, CharacterReceivedEventArgs ^ args) {
  342. key_event(sender, true, nullptr, args);
  343. }
  344. // Initializes scene resources
  345. void App::Load(Platform::String ^ entryPoint) {
  346. }
  347. // This method is called after the window becomes active.
  348. void App::Run() {
  349. if (Main::start())
  350. os->run();
  351. }
  352. // Terminate events do not cause Uninitialize to be called. It will be called if your IFrameworkView
  353. // class is torn down while the app is in the foreground.
  354. void App::Uninitialize() {
  355. Main::cleanup();
  356. delete os;
  357. }
  358. // Application lifecycle event handler.
  359. void App::OnActivated(CoreApplicationView ^ applicationView, IActivatedEventArgs ^ args) {
  360. // Run() won't start until the CoreWindow is activated.
  361. CoreWindow::GetForCurrentThread()->Activate();
  362. }
  363. // Window event handlers.
  364. void App::OnVisibilityChanged(CoreWindow ^ sender, VisibilityChangedEventArgs ^ args) {
  365. mWindowVisible = args->Visible;
  366. }
  367. void App::OnWindowClosed(CoreWindow ^ sender, CoreWindowEventArgs ^ args) {
  368. mWindowClosed = true;
  369. }
  370. void App::OnWindowSizeChanged(CoreWindow ^ sender, WindowSizeChangedEventArgs ^ args) {
  371. #if (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)
  372. // On Windows 8.1, apps are resized when they are snapped alongside other apps, or when the device is rotated.
  373. // The default framebuffer will be automatically resized when either of these occur.
  374. // In particular, on a 90 degree rotation, the default framebuffer's width and height will switch.
  375. UpdateWindowSize(args->Size);
  376. #else if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  377. // On Windows Phone 8.1, the window size changes when the device is rotated.
  378. // The default framebuffer will not be automatically resized when this occurs.
  379. // It is therefore up to the app to handle rotation-specific logic in its rendering code.
  380. //os->screen_size_changed();
  381. UpdateWindowSize(args->Size);
  382. #endif
  383. }
  384. void App::UpdateWindowSize(Size size) {
  385. float dpi;
  386. #if (WINAPI_FAMILY == WINAPI_FAMILY_PC_APP)
  387. DisplayInformation ^ currentDisplayInformation = DisplayInformation::GetForCurrentView();
  388. dpi = currentDisplayInformation->LogicalDpi;
  389. #else if (WINAPI_FAMILY == WINAPI_FAMILY_PHONE_APP)
  390. dpi = DisplayProperties::LogicalDpi;
  391. #endif
  392. Size pixelSize(ConvertDipsToPixels(size.Width, dpi), ConvertDipsToPixels(size.Height, dpi));
  393. mWindowWidth = static_cast<GLsizei>(pixelSize.Width);
  394. mWindowHeight = static_cast<GLsizei>(pixelSize.Height);
  395. OS::VideoMode vm;
  396. vm.width = mWindowWidth;
  397. vm.height = mWindowHeight;
  398. vm.fullscreen = true;
  399. vm.resizable = false;
  400. os->set_video_mode(vm);
  401. }
  402. char **App::get_command_line(unsigned int *out_argc) {
  403. static char *fail_cl[] = { "--path", "game", NULL };
  404. *out_argc = 2;
  405. FILE *f = _wfopen(L"__cl__.cl", L"rb");
  406. if (f == NULL) {
  407. wprintf(L"Couldn't open command line file.\n");
  408. return fail_cl;
  409. }
  410. #define READ_LE_4(v) ((int)(##v[3] & 0xFF) << 24) | ((int)(##v[2] & 0xFF) << 16) | ((int)(##v[1] & 0xFF) << 8) | ((int)(##v[0] & 0xFF))
  411. #define CMD_MAX_LEN 65535
  412. uint8_t len[4];
  413. int r = fread(len, sizeof(uint8_t), 4, f);
  414. Platform::Collections::Vector<Platform::String ^> cl;
  415. if (r < 4) {
  416. fclose(f);
  417. wprintf(L"Wrong cmdline length.\n");
  418. return (fail_cl);
  419. }
  420. int argc = READ_LE_4(len);
  421. for (int i = 0; i < argc; i++) {
  422. r = fread(len, sizeof(uint8_t), 4, f);
  423. if (r < 4) {
  424. fclose(f);
  425. wprintf(L"Wrong cmdline param length.\n");
  426. return (fail_cl);
  427. }
  428. int strlen = READ_LE_4(len);
  429. if (strlen > CMD_MAX_LEN) {
  430. fclose(f);
  431. wprintf(L"Wrong command length.\n");
  432. return (fail_cl);
  433. }
  434. char *arg = new char[strlen + 1];
  435. r = fread(arg, sizeof(char), strlen, f);
  436. arg[strlen] = '\0';
  437. if (r == strlen) {
  438. int warg_size = MultiByteToWideChar(CP_UTF8, 0, arg, -1, NULL, 0);
  439. wchar_t *warg = new wchar_t[warg_size];
  440. MultiByteToWideChar(CP_UTF8, 0, arg, -1, warg, warg_size);
  441. cl.Append(ref new Platform::String(warg, warg_size));
  442. } else {
  443. delete[] arg;
  444. fclose(f);
  445. wprintf(L"Error reading command.\n");
  446. return (fail_cl);
  447. }
  448. }
  449. #undef READ_LE_4
  450. #undef CMD_MAX_LEN
  451. fclose(f);
  452. char **ret = new char *[cl.Size + 1];
  453. for (int i = 0; i < cl.Size; i++) {
  454. int arg_size = WideCharToMultiByte(CP_UTF8, 0, cl.GetAt(i)->Data(), -1, NULL, 0, NULL, NULL);
  455. char *arg = new char[arg_size];
  456. WideCharToMultiByte(CP_UTF8, 0, cl.GetAt(i)->Data(), -1, arg, arg_size, NULL, NULL);
  457. ret[i] = arg;
  458. }
  459. ret[cl.Size] = NULL;
  460. *out_argc = cl.Size;
  461. return ret;
  462. }