app.cpp 20 KB

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