godot_main_osx.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*************************************************************************/
  2. /* godot_main_osx.mm */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. #include "main/main.h"
  31. #include "os_osx.h"
  32. #include <string.h>
  33. #include <unistd.h>
  34. int main(int argc, char **argv) {
  35. int first_arg = 1;
  36. const char *dbg_arg = "-NSDocumentRevisionsDebugMode";
  37. printf("arguments\n");
  38. for (int i = 0; i < argc; i++) {
  39. if (strcmp(dbg_arg, argv[i]) == 0)
  40. first_arg = i + 2;
  41. printf("%i: %s\n", i, argv[i]);
  42. };
  43. if (argc >= 1 && argv[0][0] == '/') {
  44. //potentially launched from finder
  45. int len = strlen(argv[0]);
  46. while (len--) {
  47. if (argv[0][len] == '/') break;
  48. }
  49. if (len >= 0) {
  50. char *path = (char *)malloc(len + 1);
  51. memcpy(path, argv[0], len);
  52. path[len] = 0;
  53. char *pathinfo = (char *)malloc(strlen(path) + strlen("/../Info.plist") + 1);
  54. //in real code you would check for errors in malloc here
  55. strcpy(pathinfo, path);
  56. strcat(pathinfo, "/../Info.plist");
  57. FILE *f = fopen(pathinfo, "rb");
  58. if (f) {
  59. //running from app bundle, as Info.plist was found
  60. fclose(f);
  61. chdir(path);
  62. chdir("../Resources"); //data.pck, or just the files are here
  63. }
  64. free(path);
  65. free(pathinfo);
  66. }
  67. }
  68. #ifdef DEBUG_ENABLED
  69. // lets report the path we made current after all that
  70. char cwd[4096];
  71. getcwd(cwd, 4096);
  72. printf("Current path: %s\n", cwd);
  73. #endif
  74. OS_OSX os;
  75. Error err;
  76. if (os.open_with_filename != "") {
  77. char *argv_c = (char *)malloc(os.open_with_filename.utf8().size());
  78. memcpy(argv_c, os.open_with_filename.utf8().get_data(), os.open_with_filename.utf8().size());
  79. err = Main::setup(argv[0], 1, &argv_c);
  80. free(argv_c);
  81. } else {
  82. err = Main::setup(argv[0], argc - first_arg, &argv[first_arg]);
  83. }
  84. if (err != OK)
  85. return 255;
  86. if (Main::start())
  87. os.run(); // it is actually the OS that decides how to run
  88. Main::cleanup();
  89. return os.get_exit_code();
  90. };