app.cpp 20 KB

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