OpenRCT2/src/openrct2/platform/macos.mm

165 lines
4.8 KiB
Plaintext
Raw Normal View History

2014-10-06 18:36:58 +02:00
/*****************************************************************************
2020-07-21 15:04:34 +02:00
* Copyright (c) 2014-2020 OpenRCT2 developers
2015-10-20 20:16:30 +02:00
*
* For a complete list of all authors, please refer to contributors.md
* Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2
2015-10-20 20:16:30 +02:00
*
* OpenRCT2 is licensed under the GNU General Public License version 3.
2014-10-06 18:36:58 +02:00
*****************************************************************************/
2015-11-27 18:01:22 +01:00
#if defined(__APPLE__) && defined(__MACH__)
2014-10-06 18:36:58 +02:00
2018-07-21 16:17:06 +02:00
# include "../config/Config.h"
# include "../localisation/Language.h"
# include "../util/Util.h"
# include "platform.h"
2018-06-21 12:48:12 +02:00
// undefine `interface` and `abstract`, because it's causing conflicts with Objective-C's keywords
2018-07-21 16:17:06 +02:00
# undef interface
# undef abstract
2018-06-21 12:48:12 +02:00
2018-07-21 16:17:06 +02:00
# import <AppKit/AppKit.h>
# import <Foundation/Foundation.h>
# include <mach-o/dyld.h>
# include <pwd.h>
2014-10-06 18:36:58 +02:00
2018-07-21 16:17:06 +02:00
# ifndef NO_TTF
2018-06-22 23:04:38 +02:00
bool platform_get_font_path(TTFFontDescriptor* font, utf8* buffer, size_t size)
{
@autoreleasepool
{
CTFontDescriptorRef fontRef = CTFontDescriptorCreateWithNameAndSize(
(CFStringRef)[NSString stringWithUTF8String:font->font_name], 0.0);
CFURLRef url = (CFURLRef)CTFontDescriptorCopyAttribute(fontRef, kCTFontURLAttribute);
2018-06-22 23:04:38 +02:00
if (url)
{
NSString* fontPath = [NSString stringWithString:[(NSURL*)CFBridgingRelease(url) path]];
safe_strcpy(buffer, fontPath.UTF8String, size);
return true;
2018-06-22 23:04:38 +02:00
}
else
{
return false;
}
}
}
2018-07-21 16:17:06 +02:00
# endif // NO_TTF
2018-06-22 23:04:38 +02:00
bool platform_has_matching_language(NSString* preferredLocale, uint16_t* languageIdentifier)
2016-08-21 16:28:24 +02:00
{
@autoreleasepool
{
2018-06-22 23:04:38 +02:00
if ([preferredLocale isEqualToString:@"en"] || [preferredLocale isEqualToString:@"en-CA"])
{
*languageIdentifier = LANGUAGE_ENGLISH_US;
return YES;
}
2018-06-22 23:04:38 +02:00
if ([preferredLocale isEqualToString:@"zh-CN"])
{
*languageIdentifier = LANGUAGE_CHINESE_SIMPLIFIED;
return YES;
}
2018-06-22 23:04:38 +02:00
if ([preferredLocale isEqualToString:@"zh-TW"])
{
*languageIdentifier = LANGUAGE_CHINESE_TRADITIONAL;
return YES;
}
// Find an exact match (language and region)
2018-06-22 23:04:38 +02:00
for (int i = 1; i < LANGUAGE_COUNT; i++)
{
if ([preferredLocale isEqualToString:[NSString stringWithUTF8String:LanguagesDescriptors[i].locale]])
{
*languageIdentifier = i;
return YES;
}
}
// Only check for a matching language
2018-06-22 23:04:38 +02:00
NSString* languageCode = [[preferredLocale componentsSeparatedByString:@"-"] firstObject];
for (int i = 1; i < LANGUAGE_COUNT; i++)
{
NSString* optionLanguageCode = [[[NSString stringWithUTF8String:LanguagesDescriptors[i].locale]
componentsSeparatedByString:@"-"] firstObject];
2018-06-22 23:04:38 +02:00
if ([languageCode isEqualToString:optionLanguageCode])
{
*languageIdentifier = i;
return YES;
}
}
return NO;
}
2016-08-21 16:28:24 +02:00
}
uint16_t platform_get_locale_language()
2016-08-21 16:28:24 +02:00
{
@autoreleasepool
{
2018-06-22 23:04:38 +02:00
NSArray<NSString*>* preferredLanguages = [NSLocale preferredLanguages];
for (NSString* preferredLanguage in preferredLanguages)
{
uint16_t languageIdentifier;
2018-06-22 23:04:38 +02:00
if (platform_has_matching_language(preferredLanguage, &languageIdentifier))
{
return languageIdentifier;
}
}
// Fallback
return LANGUAGE_ENGLISH_UK;
}
2016-08-21 16:28:24 +02:00
}
uint8_t platform_get_locale_currency()
2016-08-21 15:14:54 +02:00
{
@autoreleasepool
{
2018-06-22 23:04:38 +02:00
NSString* currencyCode = [[NSLocale currentLocale] objectForKey:NSLocaleCurrencyCode];
return platform_get_currency_value(currencyCode.UTF8String);
}
2016-08-21 15:14:54 +02:00
}
MeasurementFormat platform_get_locale_measurement_format()
2016-08-21 15:14:54 +02:00
{
@autoreleasepool
{
2018-06-22 23:04:38 +02:00
NSNumber* metricSystem = [[NSLocale currentLocale] objectForKey:NSLocaleUsesMetricSystem];
2016-08-21 15:14:54 +02:00
2018-06-22 23:04:38 +02:00
if (metricSystem.boolValue)
{
return MeasurementFormat::Metric;
}
2016-11-13 20:17:49 +01:00
return MeasurementFormat::Imperial;
}
2016-08-21 15:14:54 +02:00
}
2018-06-22 23:04:38 +02:00
void platform_get_changelog_path(utf8* outPath, size_t outSize)
2017-08-07 14:00:28 +02:00
{
platform_get_openrct2_data_path(outPath, outSize);
2017-08-07 14:00:28 +02:00
safe_strcat_path(outPath, "changelog.txt", outSize);
}
2018-06-22 23:04:38 +02:00
bool platform_get_steam_path(utf8* outPath, size_t outSize)
{
char steamPath[1024] = { 0 };
2018-06-22 23:04:38 +02:00
const char* homeDir = getpwuid(getuid())->pw_dir;
if (homeDir != NULL)
{
safe_strcpy(steamPath, homeDir, sizeof(steamPath));
safe_strcat_path(
steamPath, "Library/Application Support/Steam/Steam.AppBundle/Steam/Contents/MacOS/steamapps", sizeof(steamPath));
if (platform_directory_exists(steamPath))
{
safe_strcpy(outPath, steamPath, outSize);
return true;
}
}
return false;
}
2015-11-27 18:01:22 +01:00
#endif