app_uwp.cpp 20 KB

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