Filesystem.mm 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <sys/xattr.h>
  2. #include "UnityInterface.h"
  3. #include <cstring>
  4. static NSString* bundleIdWithData = nil;
  5. extern "C" void UnitySetDataBundleDirWithBundleId(const char* bundleId)
  6. {
  7. if (bundleId) bundleIdWithData = [NSString stringWithUTF8String: bundleId];
  8. else bundleIdWithData = nil;
  9. }
  10. extern "C" const char* UnityDataBundleDir()
  11. {
  12. static const char* dir = NULL;
  13. if (dir == NULL)
  14. {
  15. if (bundleIdWithData == nil)
  16. dir = AllocCString([[NSBundle mainBundle] bundlePath]);
  17. else
  18. dir = AllocCString([[NSBundle bundleWithIdentifier: bundleIdWithData] bundlePath]);
  19. }
  20. return dir;
  21. }
  22. #define RETURN_SPECIAL_DIR(dir) \
  23. do { \
  24. static const char* var = NULL; \
  25. if (var == NULL) \
  26. var = AllocCString(NSSearchPathForDirectoriesInDomains(dir, NSUserDomainMask, YES)[0]); \
  27. return var; \
  28. } while (0)
  29. extern "C" const char* UnityDocumentsDir()
  30. {
  31. RETURN_SPECIAL_DIR(NSDocumentDirectory);
  32. }
  33. extern "C" const char* UnityLibraryDir()
  34. {
  35. RETURN_SPECIAL_DIR(NSLibraryDirectory);
  36. }
  37. extern "C" const char* UnityCachesDir()
  38. {
  39. RETURN_SPECIAL_DIR(NSCachesDirectory);
  40. }
  41. #undef RETURN_SPECIAL_DIR
  42. extern "C" int UnityUpdateNoBackupFlag(const char* path, int setFlag)
  43. {
  44. int result;
  45. if (setFlag)
  46. {
  47. u_int8_t b = 1;
  48. result = ::setxattr(path, "com.apple.MobileBackup", &b, 1, 0, 0);
  49. }
  50. else
  51. {
  52. result = ::removexattr(path, "com.apple.MobileBackup", 0);
  53. }
  54. return result == 0 ? 1 : 0;
  55. }
  56. extern "C" const char* const* UnityFontFallbacks()
  57. {
  58. /* The following is the family names of fonts that are used as fallbacks
  59. for characters that were not fount in user-specified fonts. Add more
  60. fonts and/or reorder the list to fit your needs. For certain character
  61. NOTE: Some similar Chinese, Japanese and Korean characters share the
  62. character number in Unicode, but are written differently. To display
  63. such characters properly, correct font must be selected. We reorder
  64. the fonts list on the first run of this function.
  65. */
  66. static const char** cachedFonts = NULL;
  67. if (cachedFonts == NULL)
  68. {
  69. const int fontsToReorderCount = 4;
  70. static const char* defaultFonts[] =
  71. {
  72. // first fontsToReorderCount items will be reordered if needed.
  73. "Hiragino Kaku Gothic ProN", // Japanese characters
  74. "Heiti TC", // Traditional Chinese characters (on 9.0 OS substitutes this with "PingFang TC")
  75. "Heiti SC", // Simplified Chinese characters (on 9.0 OS substitutes this with "PingFang SC")
  76. "Apple SD Gothic Neo", // Korean characters
  77. ".Sukhumvit Set UI", // Thai characters since 8.2 until 9.x
  78. "AppleGothic",
  79. "Noto Sans Yi", // Yi characters on 9.0 (not available on tvOS)
  80. "Helvetica",
  81. "Helvetica Neue",
  82. "Arial Hebrew", // Hebrew since 9.0
  83. "Kohinoor Devanagari", // Hindi since 9.0
  84. "Kohinoor Bangla", // Bengali since 9.0
  85. "Kohinoor Telugu", // Telugu since 9.0
  86. "Lao Sangam MN", // Lao
  87. "Geeza Pro", // Arabic
  88. "Kailasa", // Tibetan since iOS 10.0
  89. ".PhoneFallback", // Armenian, Braille, Georgian, Thai, various symbols since iOS 10.0
  90. // Note that iOS itself prefers Thonburi font for the Thai characters, but our font subsystem
  91. // can't display combining characters for some reason
  92. ".LastResort",
  93. NULL
  94. };
  95. // The default works for Japanese, we won't reorder in that case
  96. static const char* koFontOrder[] =
  97. {
  98. "Apple SD Gothic Neo", // Korean characters
  99. "Hiragino Kaku Gothic ProN", // Japanese characters
  100. "Heiti TC", // Traditional Chinese characters (on 9.0 OS substitutes this with "PingFang TC")
  101. "Heiti SC", // Simplified Chinese characters (on 9.0 OS substitutes this with "PingFang SC")
  102. };
  103. static const char* zhFontOrder[] =
  104. {
  105. "Heiti TC", // Traditional Chinese characters (on 9.0 OS substitutes this with "PingFang TC")
  106. "Heiti SC", // Simplified Chinese characters (on 9.0 OS substitutes this with "PingFang SC")
  107. "Hiragino Kaku Gothic ProN", // Japanese characters
  108. "Apple SD Gothic Neo", // Korean characters
  109. };
  110. const char* lang = UnitySystemLanguage();
  111. const char** fontOrderOverride = NULL;
  112. if (std::strncmp(lang, "ko", 2) == 0)
  113. fontOrderOverride = koFontOrder;
  114. if (std::strncmp(lang, "zh", 2) == 0)
  115. fontOrderOverride = zhFontOrder;
  116. if (fontOrderOverride)
  117. {
  118. for (int i = 0; i < fontsToReorderCount; ++i)
  119. defaultFonts[i] = fontOrderOverride[i];
  120. }
  121. cachedFonts = defaultFonts;
  122. }
  123. return cachedFonts;
  124. }