-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.m
196 lines (155 loc) · 5.26 KB
/
Game.m
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Copyright (c) 2010 Per Johansson, per at morth.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#import "Game.h"
@implementation Game
static Game *sharedGame = nil;
+ (Game*)sharedGame
{
if (sharedGame)
return sharedGame;
return [[self alloc] init];
}
- (id)init
{
if (sharedGame)
return sharedGame;
self = [super init];
if (self)
{
self.gameAppImage = [NSImage imageNamed:@"dragon_4"];
[self updateLaunchButtonImage];
}
sharedGame = self;
return self;
}
@synthesize gameAppImage;
@synthesize spotlightGameItem;
- (void)updateLaunchButtonImage
{
NSURL *url = [self gameURL];
if (!url)
{
[self searchSpotlightForGame];
return;
}
NSBundle *gameBundle = [NSBundle bundleWithURL:url];
NSDictionary *gameInfo = [gameBundle infoDictionary];
NSString *imageName = [gameInfo objectForKey:@"CFBundleIconFile"];
NSURL *imageURL = [gameBundle URLForImageResource:imageName];
NSImage *img = [[NSImage alloc] initByReferencingURL:imageURL];
if (img)
self.gameAppImage = img;
}
- (BOOL)searchSpotlightForGame
{
NSAssert(spotlightGameItem == nil, @"Already have spotlightGameItem!");
if (gameSpotlightQuery)
return [gameSpotlightQuery isGathering];
gameSpotlightQuery = [[NSMetadataQuery alloc] init];
[gameSpotlightQuery setPredicate:[NSPredicate predicateWithFormat:@"kMDItemContentType == 'com.apple.application-bundle' AND (kMDItemCFBundleIdentifier == 'com.transgaming.cider.dragonageorigins' OR kMDItemFSName == 'DragonAgeOrigins.app')"]];
[gameSpotlightQuery setSearchScopes:[NSArray arrayWithObject:NSMetadataQueryLocalComputerScope]];
[gameSpotlightQuery addObserver:self forKeyPath:@"results" options:0 context:nil];
if (![gameSpotlightQuery startQuery])
{
gameSpotlightQuery = nil;
return NO;
}
return YES;
}
- (void)gameSpotlightQueryChanged
{
[gameSpotlightQuery disableUpdates];
if ([gameSpotlightQuery resultCount] > 0)
{
self.spotlightGameItem = [gameSpotlightQuery resultAtIndex:0];
[self updateLaunchButtonImage];
}
[gameSpotlightQuery enableUpdates];
return;
}
- (NSURL*)gameURL
{
NSURL *url;
NSArray *maybeRunning = [NSRunningApplication runningApplicationsWithBundleIdentifier:@"com.transgaming.cider.dragonageorigins"];
NSRunningApplication *running = nil;
NSString *path;
if ([maybeRunning count])
{
running = [maybeRunning objectAtIndex:0];
return [running bundleURL];
}
if ((url = [[NSUserDefaults standardUserDefaults] URLForKey:@"game url"]))
{
if ([url checkResourceIsReachableAndReturnError:nil])
return url;
}
if ((url = [[NSWorkspace sharedWorkspace] URLForApplicationWithBundleIdentifier:@"com.transgaming.cider.dragonageorigins"]))
return url;
for (running in [[NSWorkspace sharedWorkspace] runningApplications])
{
url = [running bundleURL];
if ([[url lastPathComponent] isEqualToString:@"DragonAgeOrigins.app"])
return url;
}
if ((path = [[NSWorkspace sharedWorkspace] fullPathForApplication:@"DragonAgeOrigins.app"]))
return [NSURL fileURLWithPath:path];
if (spotlightGameItem)
return [NSURL fileURLWithPath:[spotlightGameItem valueForAttribute:@"kMDItemPath"]];
/* Phew, exhausted */
return nil;
}
- (IBAction)launchGame:(id)sender
{
NSURL *url;
if ((url = [self gameURL]))
{
NSError *err;
NSRunningApplication *running;
url = [url URLByAppendingPathComponent:@"Contents/MacOS/cider"];
running = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:url options:NSWorkspaceLaunchDefault configuration:nil error:&err];
if (running)
{
[[NSUserDefaults standardUserDefaults] setURL:[running bundleURL] forKey:@"game url"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"quitOnGameLaunch"])
[NSApp terminate:self];
return;
}
}
/* -gameURL works very hard to find the URL, so assume it is not there if it fails. */
}
- (NSString*)gameVersion;
{
NSURL *url = [self gameURL];
if (!url)
{
[self searchSpotlightForGame];
return nil;
}
NSBundle *gameBundle = [NSBundle bundleWithURL:url];
NSDictionary *gameInfo = [gameBundle infoDictionary];
return [gameInfo objectForKey:@"CFBundleShortVersionString"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == gameSpotlightQuery)
[self gameSpotlightQueryChanged];
}
@end