ios_support.mm 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**************************************************************************/
  2. /* ios_support.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 "ios_support.h"
  31. #if defined(IPHONE_ENABLED)
  32. #import <Foundation/Foundation.h>
  33. #include <os/log.h>
  34. #include "core/ustring.h"
  35. #include "../gd_mono_marshal.h"
  36. // Implemented mostly following: https://github.com/mono/mono/blob/master/sdks/ios/app/runtime.m
  37. // Definition generated by the Godot exporter
  38. extern "C" void gd_mono_setup_aot();
  39. namespace gdmono {
  40. namespace ios {
  41. namespace support {
  42. void ios_mono_log_callback(const char *log_domain, const char *log_level, const char *message, mono_bool fatal, void *user_data) {
  43. os_log_info(OS_LOG_DEFAULT, "(%s %s) %s", log_domain, log_level, message);
  44. if (fatal) {
  45. os_log_info(OS_LOG_DEFAULT, "Exit code: %d.", 1);
  46. exit(1);
  47. }
  48. }
  49. void initialize() {
  50. mono_dllmap_insert(NULL, "System.Native", NULL, "__Internal", NULL);
  51. mono_dllmap_insert(NULL, "System.IO.Compression.Native", NULL, "__Internal", NULL);
  52. mono_dllmap_insert(NULL, "System.Security.Cryptography.Native.Apple", NULL, "__Internal", NULL);
  53. #ifdef IOS_DEVICE
  54. // This function is defined in an auto-generated source file
  55. gd_mono_setup_aot();
  56. #endif
  57. mono_set_signal_chaining(true);
  58. mono_set_crash_chaining(true);
  59. }
  60. void cleanup() {
  61. }
  62. } // namespace support
  63. } // namespace ios
  64. } // namespace gdmono
  65. // The following are P/Invoke functions required by the monotouch profile of the BCL.
  66. // These are P/Invoke functions and not internal calls, hence why they use
  67. // 'mono_bool' and 'const char*' instead of 'MonoBoolean' and 'MonoString*'.
  68. #define GD_PINVOKE_EXPORT extern "C" __attribute__((visibility("default")))
  69. GD_PINVOKE_EXPORT const char *xamarin_get_locale_country_code() {
  70. NSLocale *locale = [NSLocale currentLocale];
  71. NSString *countryCode = [locale objectForKey:NSLocaleCountryCode];
  72. if (countryCode == NULL) {
  73. return strdup("US");
  74. }
  75. return strdup([countryCode UTF8String]);
  76. }
  77. GD_PINVOKE_EXPORT void xamarin_log(const uint16_t *p_unicode_message) {
  78. int length = 0;
  79. const uint16_t *ptr = p_unicode_message;
  80. while (*ptr++)
  81. length += sizeof(uint16_t);
  82. NSString *msg = [[NSString alloc] initWithBytes:p_unicode_message length:length encoding:NSUTF16LittleEndianStringEncoding];
  83. os_log_info(OS_LOG_DEFAULT, "%{public}@", msg);
  84. }
  85. GD_PINVOKE_EXPORT const char *xamarin_GetFolderPath(int p_folder) {
  86. NSSearchPathDirectory dd = (NSSearchPathDirectory)p_folder;
  87. NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:dd inDomains:NSUserDomainMask] lastObject];
  88. NSString *path = [url path];
  89. return strdup([path UTF8String]);
  90. }
  91. GD_PINVOKE_EXPORT char *xamarin_timezone_get_local_name() {
  92. NSTimeZone *tz = nil;
  93. tz = [NSTimeZone localTimeZone];
  94. NSString *name = [tz name];
  95. return (name != nil) ? strdup([name UTF8String]) : strdup("Local");
  96. }
  97. GD_PINVOKE_EXPORT char **xamarin_timezone_get_names(uint32_t *p_count) {
  98. NSArray *array = [NSTimeZone knownTimeZoneNames];
  99. *p_count = array.count;
  100. char **result = (char **)malloc(sizeof(char *) * (*p_count));
  101. for (uint32_t i = 0; i < *p_count; i++) {
  102. NSString *s = [array objectAtIndex:i];
  103. result[i] = strdup(s.UTF8String);
  104. }
  105. return result;
  106. }
  107. GD_PINVOKE_EXPORT void *xamarin_timezone_get_data(const char *p_name, uint32_t *p_size) { // FIXME: uint32_t since Dec 2019, unsigned long before
  108. NSTimeZone *tz = nil;
  109. if (p_name) {
  110. NSString *n = [[NSString alloc] initWithUTF8String:p_name];
  111. tz = [[NSTimeZone alloc] initWithName:n];
  112. } else {
  113. tz = [NSTimeZone localTimeZone];
  114. }
  115. NSData *data = [tz data];
  116. *p_size = [data length];
  117. void *result = malloc(*p_size);
  118. memcpy(result, data.bytes, *p_size);
  119. return result;
  120. }
  121. GD_PINVOKE_EXPORT void xamarin_start_wwan(const char *p_uri) {
  122. // FIXME: What's this for? No idea how to implement.
  123. os_log_error(OS_LOG_DEFAULT, "Not implemented: 'xamarin_start_wwan'");
  124. }
  125. #endif // IPHONE_ENABLED