-
Notifications
You must be signed in to change notification settings - Fork 1
/
AthenaDocument.m
292 lines (257 loc) · 11.3 KB
/
AthenaDocument.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//
// AthenaDocument.m
// Athena
//
// Created by Scott McClaugherty on 1/20/11.
// Copyright 2011 Scott McClaugherty. All rights reserved.
//
#import "AthenaDocument.h"
#import "MainData.h"
#import "StringTable.h"
#import "Archivers.h"
#import "ApplicationDelegate.h"
#import "ObjectEditor.h"
#import "ScenarioEditor.h"
#import "RaceEditor.h"
#import "ImageEditor.h"
#import "SpriteEditor.h"
#import "SoundEditor.h"
#import "NSStringExtensions.h"
#import <sys/stat.h>
NSString *XSAntaresPluginUTI = @"org.arescentral.antares.plugin";
NSString *XSAresPluginUTI = @"com.biggerplanet.AresData";
NSString *XSXseraPluginUTI = @"org.brainpen.XseraPlugin";
NSString *XSAthenaMayCleanAntaresData = @"AntaresDidInstallScenarioFromPath";
NSFileWrapper *generateFileWrapperFromDictionary(NSDictionary *dictionary) {
NSFileWrapper *wrapper = [[NSFileWrapper alloc] initDirectoryWithFileWrappers:[NSDictionary dictionary]];
for (NSString *key in [dictionary keyEnumerator]) {
id obj = [dictionary objectForKey:key];
NSFileWrapper *file = nil;
if ([obj isKindOfClass:[NSData class]]) {
file = [[[NSFileWrapper alloc] initRegularFileWithContents:(NSData *)obj] autorelease];
} else if ([obj isKindOfClass:[NSDictionary class]]) {
file = generateFileWrapperFromDictionary(obj);
}
[file setPreferredFilename:key];
[wrapper addFileWrapper:file];
}
return [wrapper autorelease];
}
@interface AthenaDocument (Private)
- (void)deleteAntaresTemp:(NSNotification *)notification;
@end
@implementation AthenaDocument
@synthesize data;
- (id) init {
self = [super init];
if (self) {
// Add your subclass-specific initialization here.
// If an error occurs here, send a [self release] message and return nil.
data = [[MainData alloc] init];
}
return self;
}
- (NSString *) windowNibName {
// Override returning the nib file name of the document
// If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
return @"AthenaDocument";
}
- (void) windowControllerDidLoadNib:(NSWindowController *) aController {
[super windowControllerDidLoadNib:aController];
[aController setShouldCloseDocument:YES];
}
- (void)awakeFromNib {
[[identifierField cell] bind:@"placeholderString" toObject:self withKeyPath:@"data.computedIdentifier" options:nil];
}
- (BOOL) writeToURL:(NSURL *)absoluteURL ofType:(NSString *)type error:(NSError **)outError {
NSLog(@"Saving data of type: %@", type);
NSString *fileName = [absoluteURL path];
if ([type isEqualToCaseInsensitiveString:XSAresPluginUTI]) {
ResArchiver *coder = [[ResArchiver alloc] init];
[coder setSaveType:DataBasisAres];
[coder encodeObject:data atIndex:128];
BOOL success = [coder writeToResourceFile:fileName];
[coder release];
NSLog(@"Save complete");
return success;
} else if ([type isEqualToCaseInsensitiveString:XSXseraPluginUTI]) {
return [super writeToURL:absoluteURL ofType:type error:outError];
} else if ([type isEqualToCaseInsensitiveString:XSAntaresPluginUTI]) {
ResArchiver *coder = [[ResArchiver alloc] init];
[coder setSaveType:DataBasisAntares];
[coder encodeObject:data atIndex:128];
BOOL success = [coder writeToZipFile:fileName];
[coder release];
NSLog(@"Save complete");
return success;
} else {
//BAD!!!
return NO;
}
return NO;//This should never be reached.
}
- (NSFileWrapper *)fileWrapperOfType:(NSString *)typeName error:(NSError **)outError {
LuaArchiver *arch = [[LuaArchiver alloc] init];
[arch encodeObject:data forKey:@"data"];
[arch sync];
[arch saveFile:arch.data named:@"data.lua" inDirectory:@"Scripts/Modules"];
NSMutableDictionary *files = [arch files];
NSFileWrapper *wrapper = generateFileWrapperFromDictionary(files);
[arch release];
NSLog(@"Save complete");
return wrapper;
}
- (BOOL)readFromURL:(NSURL *)url ofType:(NSString *)type error:(NSError **)outError {
NSString *fileName = [url path];
NSLog(@"Reading Data of type (%@)", type);
[data release];
data = nil;
@try {
if ([type isEqualToCaseInsensitiveString:XSXseraPluginUTI]){
return [super readFromURL:url ofType:type error:outError];
} else if ([type isEqualToCaseInsensitiveString:XSAresPluginUTI]) {
ResUnarchiver *coder = [[ResUnarchiver alloc] init];
NSString *dataDir = [[ApplicationDelegate supportDir] stringByAppendingPathComponent:@"Ares Data"];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Sprites"] ofType:DataBasisAres];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Sounds"] ofType:DataBasisAres];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Interfaces"] ofType:DataBasisAres];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Scenarios"] ofType:DataBasisAres];
if (![[fileName lastPathComponent] isEqualTo:@"Ares Scenarios"]) {
[coder addFile:fileName ofType:DataBasisAres];
}
data = [[coder decodeObjectOfClass:[MainData class] atIndex:128] retain];
[coder release];
} else if ([type isEqualToCaseInsensitiveString:XSAntaresPluginUTI]) {
ResUnarchiver *coder = [[ResUnarchiver alloc] init];
NSString *dataDir = [[ApplicationDelegate supportDir] stringByAppendingPathComponent:@"Ares Data"];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Sprites"] ofType:DataBasisAres];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Sounds"] ofType:DataBasisAres];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Interfaces"] ofType:DataBasisAres];
[coder addFile:[dataDir stringByAppendingPathComponent:@"Ares Scenarios"] ofType:DataBasisAres];
[coder addFile:fileName ofType:DataBasisAntares];
data = [[coder decodeObjectOfClass:[MainData class] atIndex:128] retain];
[coder release];
return YES;
} else {
NSLog(@"ERROR: Type not found. aborting");
return NO;
}
}
@catch (id exception) {
NSLog(@"Error while opening:\n%@", exception);
return NO;
}
return YES;
}
- (BOOL)loadFileWrapperRepresentation:(NSFileWrapper *)wrapper ofType:(NSString *)type {
LuaUnarchiver *arch = [[LuaUnarchiver alloc] init];
[arch setBaseDir:wrapper];
[arch loadData:[arch fileNamed:@"data.lua" inDirectory:@"Scripts/Modules"]];
[data release];
data = [[arch decodeObjectOfClass:[MainData class] forKey:@"data"] retain];
[arch release];
return YES;
}
- (void) dealloc {
[data release];
[super dealloc];
}
- (IBAction) openObjectEditor:(id)sender {
ObjectEditor *objectEditor = [[ObjectEditor alloc] initWithMainData:data];
[self addWindowController:objectEditor];
[objectEditor showWindow:self];
[objectEditor release];
}
- (IBAction) openScenarioEditor:(id)sender {
ScenarioEditor *scenarioEditor = [[ScenarioEditor alloc] initWithMainData:data];
[self addWindowController:scenarioEditor];
[scenarioEditor showWindow:self];
[scenarioEditor release];
}
- (IBAction) openRaceEditor:(id)sender {
RaceEditor *raceEditor = [[RaceEditor alloc] initWithMainData:data];
[self addWindowController:raceEditor];
[raceEditor showWindow:self];
[raceEditor release];
}
- (IBAction)openImageEditor:(id)sender {
ImageEditor *imageEditor = [[ImageEditor alloc] initWithMainData:data];
[self addWindowController:imageEditor];
[imageEditor showWindow:self];
[imageEditor release];
}
- (IBAction) openSpriteEditor:(id)sender {
SpriteEditor *spriteEditor = [[SpriteEditor alloc] initWithMainData:data];
[self addWindowController:spriteEditor];
[spriteEditor showWindow:self];
[spriteEditor release];
}
- (IBAction)openSoundEditor:(id)sender {
SoundEditor *soundEditor = [[SoundEditor alloc] initWithMainData:data];
[self addWindowController:soundEditor];
[soundEditor showWindow:self];
[soundEditor release];
}
- (IBAction) displayEasterWindow:(id)sender {
[easter makeKeyAndOrderFront:self];
}
- (IBAction) launchInAntares:(id)sender {
NSString *fileName = nil;
BOOL isAntaresFileType = [[self fileType] isEqualToCaseInsensitiveString:XSAntaresPluginUTI];
if ([self isDocumentEdited] || !isAntaresFileType) {
fileName = NSTemporaryDirectory();
fileName = [fileName stringByAppendingPathComponent:[[[self data] computedIdentifier] stringByReplacingOccurrencesOfString:@"." withString:@"_" ]];
fileName = [fileName stringByAppendingPathExtension:[self fileNameExtensionForType:XSAntaresPluginUTI saveOperation:NSSaveToOperation]];
NSError *error = nil;
[self saveToURL:[NSURL fileURLWithPath:fileName] ofType:XSAntaresPluginUTI forSaveOperation:NSSaveToOperation error:&error];
if (error != nil) {
[self presentError:error];
return;
}
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(deleteAntaresTemp:) name:XSAthenaMayCleanAntaresData object:[fileName stringByStandardizingPath]];
} else {
fileName = [[self fileURL] path];
}
[[NSWorkspace sharedWorkspace] openFile:fileName withApplication:@"/Users/scott/dev/antares/build/antares/Antares.app"];
}
- (void)deleteAntaresTemp:(NSNotification *)notification {
NSString *file = [notification object];
NSLog(@"Deleting: %@", file);
unlink([file fileSystemRepresentation]);
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:XSAthenaMayCleanAntaresData object:file];
}
@end
@implementation AthenaDocumentWindow
- (void)keyDown:(NSEvent *)event {
if ([[event charactersIgnoringModifiers] isEqualToString:@"x"]) {
[NSApp sendAction:@selector(displayEasterWindow:) to:nil from:self];
} else {
[super keyDown:event];
}
}
- (void)sendEvent:(NSEvent *)event {
if ([event type] == NSKeyDown && ([event modifierFlags] & NSAlternateKeyMask) == NSAlternateKeyMask && [[event charactersIgnoringModifiers] isEqualToString:@"x"]) {
[NSApp sendAction:@selector(displayEasterWindow:) to:nil from:self];
} else {
[super sendEvent:event];
}
}
@end
@implementation EasterWindow
- (void)makeKeyAndOrderFront:(id)sender {
NSURL *path = [[NSBundle mainBundle] URLForResource:@"Easter" withExtension:@"txt"];
NSError *error = nil;
NSArray *lines = [[NSString stringWithContentsOfURL:path encoding:NSUTF8StringEncoding error:&error] componentsSeparatedByString:@"\n"];
NSString *msg = [lines objectAtIndex:(arc4random() % [lines count])];
NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:24.0f]
forKey:NSFontAttributeName];
NSAttributedString *message = [[[NSAttributedString alloc] initWithString:msg
attributes:attributes] autorelease];
[label setAttributedStringValue:message];
[super makeKeyAndOrderFront:sender];
}
- (void)keyDown:(NSEvent *)theEvent {
[self performClose:theEvent];
}
@end