forked from Sappharad/MultiPatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPPatchWindow.mm
228 lines (208 loc) · 6.98 KB
/
MPPatchWindow.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
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
#import "MPPatchWindow.h"
#import "XDeltaAdapter.h"
#import "IPSAdapter.h"
#import "PPFAdapter.h"
#import "BSdiffAdapter.h"
#import "BPSAdapter.h"
#import "UPSAdapter.h"
#import "RUPAdapter.h"
#import "MPPatchResult.h"
@implementation MPPatchWindow
static mbFlipWindow* _flipper;
-(void)awakeFromNib{
[super awakeFromNib];
_flipper = [mbFlipWindow new];
}
-(void)close{
[super close];
[[NSApplication sharedApplication] terminate:nil];
}
-(void)makeKeyAndOrderFront:(id)sender{
[super makeKeyAndOrderFront:sender]; //This one gets called when mbFlipWindow flips back to this window
[self onOrderFront];
}
-(void)orderFront:(id)sender{
[super orderFront:sender]; //This one gets called when the app starts
[self onOrderFront];
}
-(void)onOrderFront{
txtRomPath.acceptFileDrop = ^BOOL(NSURL * target) {
[self setTargetFile:target];
return YES;
};
txtPatchPath.acceptFileDrop = ^BOOL(NSURL * target) {
[self setPatchFile:target];
return YES;
};
}
-(void)orderOut:(id)sender{
[super orderOut:sender];
txtRomPath.acceptFileDrop = nil;
txtPatchPath.acceptFileDrop = nil;
}
- (IBAction)btnApply:(id)sender {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *romPath = [txtRomPath stringValue];
NSString *outputPath = [txtOutputPath stringValue];
NSString *patchPath = [txtPatchPath stringValue];
if([fileManager fileExistsAtPath:patchPath]){
if([romPath length] > 0 && [outputPath length] > 0 && [patchPath length] > 0){
[lblStatus setStringValue:@"Now patching..."];
[NSApp beginSheet:pnlPatching modalForWindow:self modalDelegate:nil didEndSelector:nil contextInfo:nil]; //Make a sheet
[barProgress setUsesThreadedAnimation:YES]; //Make sure it animates.
[barProgress startAnimation:self];
MPPatchResult* errMsg = [self ApplyPatch:patchPath :romPath :outputPath];
[barProgress stopAnimation:self];
[NSApp endSheet:pnlPatching]; //Tell the sheet we're done.
[pnlPatching orderOut:self]; //Lets hide the sheet.
if(errMsg == nil){
NSRunAlertPanel(@"Finished!", @"The file was patched successfully.", @"Okay", nil, nil);
}
else if(errMsg.IsWarning){
NSRunAlertPanel(@"Patching finished with warning", errMsg.Message, @"Okay", nil, nil);
[errMsg release];
errMsg = nil;
}
else{
NSRunAlertPanel(@"Patching failed", errMsg.Message, @"Okay", nil, nil);
[errMsg release];
errMsg = nil;
}
}
else{
NSRunAlertPanel(@"Not ready yet",@"All of the files above must be chosen before patching is possible.",@"Okay",nil,nil);
}
}
else{
NSRunAlertPanel(@"Patch not found", @"The patch file selected does not exist anymore.\nWhy did you do that?", @"Okay", nil, nil);
}
}
-(void)setPatchFile:(NSURL*)patch{
NSString* selfile = [patch path];
[txtPatchPath setStringValue:selfile];
currentFormat = [MPPatchWindow detectPatchFormat:selfile];
[btnApply setEnabled:currentFormat!=UNKNOWNPAT];
switch (currentFormat) {
case UPSPAT:
[lblPatchFormat setStringValue:@"UPS"];
break;
case XDELTAPAT:
[lblPatchFormat setStringValue:@"XDelta"];
break;
case IPSPAT:
[lblPatchFormat setStringValue:@"IPS"];
break;
case PPFPAT:
[lblPatchFormat setStringValue:@"PPF"];
break;
case BSDIFFPAT:
[lblPatchFormat setStringValue:@"BSDiff"];
break;
case BPSPAT:
[lblPatchFormat setStringValue:@"BPS"];
break;
case RUPPAT:
[lblPatchFormat setStringValue:@"Ninja (RUP)"];
break;
default:
[lblPatchFormat setStringValue:@"Not supported"];
break;
}
}
- (IBAction)btnSelectPatch:(id)sender{
NSOpenPanel *fbox = [NSOpenPanel openPanel];
[fbox beginSheetModalForWindow:self completionHandler:^(NSInteger result) {
if(result == NSOKButton){
[self setPatchFile:[[fbox URLs] objectAtIndex:0]];
}
}];
}
-(void)setTargetFile:(NSURL*)target{
NSString* selfile = [target path];
[txtRomPath setStringValue:selfile];
if(romFormat != nil){
[romFormat release];
}
romFormat = [selfile pathExtension];
[romFormat retain];
}
- (IBAction)btnSelectOriginal:(id)sender {
NSOpenPanel *fbox = [NSOpenPanel openPanel];
[fbox beginSheetModalForWindow:self completionHandler:^(NSInteger result) {
if(result == NSOKButton){
[self setTargetFile:[[fbox URLs] objectAtIndex:0]];
}
}];
}
- (IBAction)btnSelectOutput:(id)sender{
NSSavePanel *fbox = [NSSavePanel savePanel];
if(romFormat != nil && [romFormat length]>0){
[fbox setAllowedFileTypes:[NSArray arrayWithObject:romFormat]];
}
[fbox beginSheetModalForWindow:self completionHandler:^(NSInteger result) {
if(result == NSOKButton){
NSString* selfile = [[fbox URL] path];
[txtOutputPath setStringValue:selfile];
}
}];
}
+ (PatchFormat)detectPatchFormat:(NSString*)patchPath{
//I'm just going to look at the file extensions for now.
//In the future, I might wish to actually look at the contents of the file.
NSString* lowerPath = [patchPath lowercaseString];
if([lowerPath hasSuffix:@".ups"]){
return UPSPAT;
}
else if([lowerPath hasSuffix:@".ips"]){
return IPSPAT;
}
else if([lowerPath hasSuffix:@".ppf"]){
return PPFPAT;
}
else if([lowerPath hasSuffix:@".dat"] || [lowerPath hasSuffix:@"delta"]){
return XDELTAPAT;
}
else if([lowerPath hasSuffix:@".bdf"] || [lowerPath hasSuffix:@".bsdiff"]){
return BSDIFFPAT;
}
else if([lowerPath hasSuffix:@".bps"]){
return BPSPAT;
}
else if([lowerPath hasSuffix:@".rup"]){
return RUPPAT;
}
return UNKNOWNPAT;
}
- (MPPatchResult*)ApplyPatch:(NSString*)patchPath :(NSString*)sourceFile :(NSString*)destFile{
MPPatchResult* retval = nil;
if(currentFormat == UPSPAT){
retval = [UPSAdapter ApplyPatch:patchPath toFile:sourceFile andCreate:destFile];
}
else if(currentFormat == IPSPAT){
retval = [IPSAdapter ApplyPatch:patchPath toFile:sourceFile andCreate:destFile];
}
else if(currentFormat == XDELTAPAT){
retval = [XDeltaAdapter ApplyPatch:patchPath toFile:sourceFile andCreate:destFile];
}
else if(currentFormat == PPFPAT){
retval = [PPFAdapter ApplyPatch:patchPath toFile:sourceFile andCreate:destFile];
}
else if(currentFormat == BSDIFFPAT){
retval = [BSdiffAdapter ApplyPatch:patchPath toFile:sourceFile andCreate:destFile];
}
else if(currentFormat == BPSPAT){
retval = [BPSAdapter ApplyPatch:patchPath toFile:sourceFile andCreate:destFile];
}
else if(currentFormat == RUPPAT){
retval = [RUPAdapter ApplyPatch:patchPath toFile:sourceFile andCreate:destFile];
}
return retval;
}
- (IBAction)btnCreatePatch:(id)sender {
_flipper.flipRight = YES;
[_flipper flip:self to:wndCreator];
}
+ (mbFlipWindow*)flipper{
return _flipper;
}
@end