godot_ios.mm 3.9 KB

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