-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mm
118 lines (112 loc) · 5.98 KB
/
main.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#import <AppKit/AppKit.h>
#import "ApplicationDelegate.h"
#import "MainWindow.h"
#import "MainWindowDelegate.h"
#import "Preference.h"
#include "bx/commandline.h"
#include "emapp/Allocator.h"
#include "emapp/emapp.h"
#include "CocoaThreadedApplicationService.h"
#if defined(NANOEM_ENABLE_LOGGING)
#include "spdlog/async.h"
#include "spdlog/cfg/env.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#endif /* NANOEM_ENABLE_LOGGING */
using namespace nanoem;
int
main(int argc, char *argv[])
{
NSBundle *mainBundle = [NSBundle mainBundle];
NSString *executablePath = mainBundle.executableURL.path;
if ([executablePath hasPrefix:@"/private/var/folders"] && [executablePath containsString:@"AppTranslocation"]) {
NSAlert *alert = [[NSAlert alloc] init];
alert.alertStyle = NSAlertStyleCritical;
alert.messageText = NSLocalizedString(@"Cannot execute from here", @"");
alert.informativeText = NSLocalizedString(@"Move nanoem.app to Application folder and retry again.", @"");
[alert runModal];
return 1;
}
Allocator::initialize();
ThreadedApplicationService::setup();
@autoreleasepool {
JSON_Value *config = json_value_init_object();
NSLocale *locale = [NSLocale currentLocale];
JSON_Object *root = json_object(config);
NSURL *url = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:YES
error:nil];
NSString *domainString = [[NSString alloc] initWithUTF8String:BaseApplicationService::kOrganizationDomain];
NSURL *applicationDirectoryURL = [url URLByAppendingPathComponent:domainString isDirectory:YES];
NSURL *redoDirectoryURL = [applicationDirectoryURL URLByAppendingPathComponent:@"redo" isDirectory:YES];
NSURL *tempDirectoryURL = [applicationDirectoryURL URLByAppendingPathComponent:@"tmp" isDirectory:YES];
NSURL *sentryDatabaseURL = [applicationDirectoryURL URLByAppendingPathComponent:@"sentry-db" isDirectory:NO];
NSURL *sentryHandlerURL = [mainBundle.builtInPlugInsURL URLByAppendingPathComponent:@"crashpad_handler"];
NSError *error = nil;
NSDictionary *attributes =
[[NSDictionary alloc] initWithObjectsAndKeys:[NSNumber numberWithShort:0700], NSFilePosixPermissions, nil];
[[NSFileManager defaultManager] createDirectoryAtURL:tempDirectoryURL
withIntermediateDirectories:YES
attributes:attributes
error:&error];
[[NSFileManager defaultManager] createDirectoryAtURL:redoDirectoryURL
withIntermediateDirectories:YES
attributes:attributes
error:&error];
json_object_dotset_string(root, "project.home", NSHomeDirectory().UTF8String);
json_object_dotset_string(root, "project.locale", locale.localeIdentifier.UTF8String);
json_object_dotset_string(root, "project.tmp.path", tempDirectoryURL.path.UTF8String);
json_object_dotset_string(root, "macos.redo.path", redoDirectoryURL.path.UTF8String);
json_object_dotset_string(root, "macos.sentry.handler.path", sentryHandlerURL.path.UTF8String);
json_object_dotset_string(root, "macos.sentry.database.path", sentryDatabaseURL.path.UTF8String);
{
const bx::CommandLine command(argc, argv);
NSApplication *app = macos::CocoaThreadedApplicationService::createApplication();
if (@available(macOS 10.12.1, *)) {
if (NSClassFromString(@"NSTouchBar") != nil) {
app.automaticCustomizeTouchBarMenuItemEnabled = YES;
}
}
if (command.hasArg("locale")) {
json_object_dotset_string(root, "project.locale", command.findOption("locale"));
}
macos::CocoaThreadedApplicationService service(config);
macos::Preference preference([NSUserDefaults standardUserDefaults], &service, config);
ThreadedApplicationClient client;
service.start();
client.connect();
#if defined(NANOEM_ENABLE_LOGGING)
spdlog::init_thread_pool(1024, 1);
tinystl::vector<spdlog::sink_ptr, TinySTLAllocator> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
auto logger = std::make_shared<spdlog::async_logger>(
"emapp", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block);
spdlog::register_logger(logger);
spdlog::cfg::load_env_levels();
#endif /* NANOEM_ENABLE_LOGGING */
{
NSWindow *nativeWindow = macos::CocoaThreadedApplicationService::createMainWindow();
macos::MainWindow *window =
new macos::MainWindow(&command, &service, &client, nativeWindow, &preference);
window->initialize();
ApplicationDelegate *applicationDelegate = [[ApplicationDelegate alloc] initWithWindow:window];
app.delegate = applicationDelegate;
MainWindowDelegate *windowDelegate = [[MainWindowDelegate alloc] initWithWindow:window];
nativeWindow.delegate = windowDelegate;
[app run];
while (window->isRunning()) {
window->processMessage(app);
}
window->terminate();
delete window;
}
client.close();
service.stop();
}
json_value_free(config);
}
ThreadedApplicationService::terminate();
Allocator::destroy();
return 0;
}