app_uwp.cpp 20 KB

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