app.cpp 20 KB

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