-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFNaturalExtensionProject.m
180 lines (137 loc) · 5.16 KB
/
IFNaturalExtensionProject.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
//
// IFNaturalExtensionProject.m
// Inform
//
// Created by Andrew Hunter on 18/11/2004.
// Copyright 2004 Andrew Hunter. All rights reserved.
//
#import "IFNaturalExtensionProject.h"
#import "IFSingleFile.h"
#import "IFExtensionsManager.h"
#import "IFPreferences.h"
#import "IFAppDelegate.h"
@implementation IFNaturalExtensionProject
- (void) dealloc {
[vw release];
[super dealloc];
}
- (NSString*) projectName {
return [[NSBundle mainBundle] localizedStringForKey: @"Natural Inform extension"
value: @"Extension"
table: nil];
}
- (NSString*) projectHeading {
return [[NSBundle mainBundle] localizedStringForKey: @"Natural Inform"
value: @"Natural Inform"
table: nil];
}
- (NSAttributedString*) projectDescription {
return [[[NSAttributedString alloc] initWithString:
[[NSBundle mainBundle] localizedStringForKey: @"Natural Inform extension description"
value: @"Creates a Natural Inform extension directory."
table: nil]] autorelease];
}
- (NSObject<IFProjectSetupView>*) configView {
if (!vw) {
vw = [[IFNaturalExtensionView alloc] init];
[NSBundle loadNibNamed: @"NaturalExtensionOptions"
owner: vw];
}
[vw setupControls];
return vw;
}
- (void) setupFile: (IFProjectFile*) file
fromView: (NSObject<IFProjectSetupView>*) view {
}
- (BOOL) showFinalPage {
return NO;
}
- (NSString*) errorMessage {
if (![vw authorName] || [[vw authorName] isEqualToString: @""]) {
return [[NSBundle mainBundle] localizedStringForKey: @"BadExtensionAuthor"
value: @"Bad extension author"
table: nil];
}
return nil;
}
- (NSString*) confirmationMessage {
if ([[NSFileManager defaultManager] fileExistsAtPath: [self saveFilename]]) {
return [[NSBundle mainBundle] localizedStringForKey: @"Extension already exists"
value: @"Extension already exists"
table: nil];
}
return nil;
}
- (NSString*) saveFilename {
NSString* extnDir = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex: 0];
extnDir = [[extnDir stringByAppendingPathComponent: @"Inform"] stringByAppendingPathComponent: @"Extensions"];
return [[extnDir stringByAppendingPathComponent: [vw authorName]] stringByAppendingPathComponent: [vw extensionName]];
}
- (NSString*) openAsType {
return @"Inform Extension Directory";
}
- (void) createDeepDirectory: (NSString*) deepDirectory {
// Creates a directory and any parent directories as required
if (deepDirectory == nil || [deepDirectory length] < 2) return;
if (![[NSFileManager defaultManager] fileExistsAtPath: [deepDirectory stringByDeletingLastPathComponent]]) {
[self createDeepDirectory: [deepDirectory stringByDeletingLastPathComponent]];
}
if (![[NSFileManager defaultManager] fileExistsAtPath: deepDirectory]) {
[[NSFileManager defaultManager] createDirectoryAtPath: deepDirectory
attributes: nil];
}
}
- (BOOL) createAndOpenDocument: (NSString*) filename {
NSString* contents = [NSString stringWithFormat: @"%@ by %@ begins here.\n\n%@ ends here.\n", [vw extensionName], [vw authorName], [vw extensionName]];
NSDictionary* fileAttr = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedLong: 'INfm'], NSFileHFSCreatorCode,
[NSNumber numberWithUnsignedLong: 'INex'], NSFileHFSTypeCode,
nil];
NSData* contentData = [contents dataUsingEncoding: NSUTF8StringEncoding];
// Try to create the extension directory, if necessary
if (![[NSFileManager defaultManager] fileExistsAtPath: [filename stringByDeletingLastPathComponent]]) {
[self createDeepDirectory: [filename stringByDeletingLastPathComponent]];
}
// Try to create the file
if (![[NSFileManager defaultManager] createFileAtPath: filename
contents: contentData
attributes: fileAttr]) {
return NO;
}
// Open the file
NSDocument* newDoc = [[IFSingleFile alloc] initWithContentsOfFile: filename
ofType: @"Inform 7 extension"];
[[NSDocumentController sharedDocumentController] addDocument: [newDoc autorelease]];
[newDoc makeWindowControllers];
[newDoc showWindows];
// Get the delegate to update the list of extensions
[[IFExtensionsManager sharedNaturalInformExtensionsManager] updateTableData];
[[NSApp delegate] updateExtensions];
return YES;
}
@end
@implementation IFNaturalExtensionView
- (NSView*) view {
return view;
}
- (void) setupControls {
NSString* longuserName = [[IFPreferences sharedPreferences] newGameAuthorName];
// If longuserName contains a '.', then we have to enclose it in quotes
BOOL needQuotes = NO;
int x;
for (x=0; x<[longuserName length]; x++) {
if ([longuserName characterAtIndex: x] == '.') needQuotes = YES;
}
if (needQuotes) longuserName = [NSString stringWithFormat: @"\"%@\"", longuserName];
[name setStringValue: longuserName];
[extensionName setStringValue: [[NSBundle mainBundle] localizedStringForKey: @"New extension"
value: @"New extension"
table: nil]];
}
- (NSString*) authorName {
return [name stringValue];
}
- (NSString*) extensionName {
return [extensionName stringValue];
}
@end