godot_ios.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**************************************************************************/
  2. /* godot_ios.mm */
  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. #import "os_ios.h"
  31. #include "core/string/ustring.h"
  32. #include "main/main.h"
  33. #include <unistd.h>
  34. #include <cstdio>
  35. static OS_IOS *os = nullptr;
  36. int add_path(int p_argc, char **p_args) {
  37. NSString *str = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"godot_path"];
  38. if (!str) {
  39. return p_argc;
  40. }
  41. p_args[p_argc++] = (char *)"--path";
  42. p_args[p_argc++] = (char *)[str cStringUsingEncoding:NSUTF8StringEncoding];
  43. p_args[p_argc] = nullptr;
  44. return p_argc;
  45. }
  46. int add_cmdline(int p_argc, char **p_args) {
  47. NSArray *arr = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"godot_cmdline"];
  48. if (!arr) {
  49. return p_argc;
  50. }
  51. for (NSUInteger i = 0; i < [arr count]; i++) {
  52. NSString *str = [arr objectAtIndex:i];
  53. if (!str) {
  54. continue;
  55. }
  56. p_args[p_argc++] = (char *)[str cStringUsingEncoding:NSUTF8StringEncoding];
  57. }
  58. p_args[p_argc] = nullptr;
  59. return p_argc;
  60. }
  61. int ios_main(int argc, char **argv) {
  62. size_t len = strlen(argv[0]);
  63. while (len--) {
  64. if (argv[0][len] == '/') {
  65. break;
  66. }
  67. }
  68. if (len >= 0) {
  69. char path[512];
  70. memcpy(path, argv[0], len > sizeof(path) ? sizeof(path) : len);
  71. path[len] = 0;
  72. chdir(path);
  73. }
  74. os = new OS_IOS();
  75. // We must override main when testing is enabled
  76. TEST_MAIN_OVERRIDE
  77. char *fargv[64];
  78. for (int i = 0; i < argc; i++) {
  79. fargv[i] = argv[i];
  80. }
  81. fargv[argc] = nullptr;
  82. argc = add_path(argc, fargv);
  83. argc = add_cmdline(argc, fargv);
  84. Error err = Main::setup(fargv[0], argc - 1, &fargv[1], false);
  85. if (err != OK) {
  86. if (err == ERR_HELP) { // Returned by --help and --version, so success.
  87. return EXIT_SUCCESS;
  88. }
  89. return EXIT_FAILURE;
  90. }
  91. os->initialize_modules();
  92. return os->get_exit_code();
  93. }
  94. void ios_finish() {
  95. Main::cleanup();
  96. delete os;
  97. }