godot_ios.mm 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #include "core/string/ustring.h"
  31. #include "main/main.h"
  32. #include "os_ios.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. printf("Path: %s\n", path);
  74. chdir(path);
  75. }
  76. printf("godot_ios %s\n", argv[0]);
  77. char cwd[512];
  78. getcwd(cwd, sizeof(cwd));
  79. printf("cwd %s\n", cwd);
  80. os = new OS_IOS();
  81. // We must override main when testing is enabled
  82. TEST_MAIN_OVERRIDE
  83. char *fargv[64];
  84. for (int i = 0; i < argc; i++) {
  85. fargv[i] = argv[i];
  86. }
  87. fargv[argc] = nullptr;
  88. argc = add_path(argc, fargv);
  89. argc = add_cmdline(argc, fargv);
  90. printf("os created\n");
  91. Error err = Main::setup(fargv[0], argc - 1, &fargv[1], false);
  92. printf("setup %i\n", err);
  93. if (err == ERR_HELP) { // Returned by --help and --version, so success.
  94. return 0;
  95. } else if (err != OK) {
  96. return 255;
  97. }
  98. os->initialize_modules();
  99. return 0;
  100. }
  101. void ios_finish() {
  102. printf("ios_finish\n");
  103. Main::cleanup();
  104. delete os;
  105. }