-
Notifications
You must be signed in to change notification settings - Fork 13
/
DocCheaterDelegate.m
448 lines (347 loc) · 11.4 KB
/
DocCheaterDelegate.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* The Cheat - The legendary universal game trainer for Mac OS X.
* http://www.brokenzipper.com/trac/wiki/TheCheat
*
* Copyright (c) 2003-2011, Charles McGarvey et al.
*
* Distributable under the terms and conditions of the 2-clause BSD
* license; see the file COPYING for the legal text of the license.
*/
#import "CheatDocument.h"
@implementation CheatDocument ( DocCheaterDelegate )
- (void)cheaterDidConnect:(Cheater *)cheater
{
ChazLog( @"cheaterDidConnect:" );
[ibStatusText setDefaultStatus:[self defaultStatusString]];
// update the interface
[self updateInterface];
}
- (void)cheaterDidDisconnect:(Cheater *)cheater
{
ChazLog( @"cheaterDidDisonnect:" );
[self disconnectFromCheater];
// update status
[self showError:@"Disconnected by server."];
[ibStatusBar setIndeterminate:NO];
[ibStatusBar setDoubleValue:0.0];
[ibStatusBar stopAnimation:self];
// update the interface
[self updateInterface];
}
- (void)cheaterRequiresAuthentication:(Cheater *)cheater
{
ChazLog( @"cheaterRequiresAuthentication:" );
[self ibRunPasswordSheet:nil];
}
- (void)cheaterRejectedPassword:(Cheater *)cheater
{
ChazLog( @"cheaterRejectedPassword" );
}
- (void)cheaterAcceptedPassword:(Cheater *)cheater
{
ChazLog( @"cheaterAcceptedPassword" );
[ibStatusText setTemporaryStatus:@"Password Accepted"];
}
- (void)cheater:(Cheater *)cheater didFindProcesses:(NSArray *)processes
{
NSMenu *processMenu;
NSMenuItem *menuItem;
unsigned i, len;
Process *selectThis = nil;
ChazLog( @"cheater:didFindProcesses:" );
// create and set the server popup menu
processMenu = [[NSMenu alloc] init];
[processMenu setAutoenablesItems:YES];
// add menu items
// processes returned
len = [processes count];
for ( i = 0; i < len; i++ ) {
Process *item = [processes objectAtIndex:i];
menuItem = [[NSMenuItem alloc] init];
[menuItem setTarget:self];
[menuItem setAction:@selector(ibSetProcess:)];
[menuItem setTitle:[item name]];
[menuItem setImage:[item icon]];
[menuItem setRepresentedObject:item];
[processMenu addItem:menuItem];
[menuItem release];
// check and see if our document uses this application
if ( [self isLoadedFromFile] && [[_cheatData process] sameApplicationAs:item] ) {
selectThis = item;
}
}
// set the menu
[ibProcessPopup setMenu:processMenu];
[processMenu release];
// if we're loaded from a file, select the process matching
// the one saved, if it is launched.
if ( selectThis ) {
[_cheater setTarget:selectThis];
}
// otherwise, select the global target
else if ( selectThis = [CheatDocument globalTarget] ) {
ChazLog( @"setting global target" );
[_cheater setTarget:selectThis];
}
// otherwise, select the first process in this list
else if ( len > 0 ) {
[_cheater setTarget:[processes objectAtIndex:0]];
}
}
- (void)cheater:(Cheater *)cheater didAddProcess:(Process *)process
{
NSMenu *processMenu = [ibProcessPopup menu];
NSMenuItem *menuItem;
Process *savedTarget = [_cheatData process];
// add the newly found process to the process popup
menuItem = [[NSMenuItem alloc] init];
[menuItem setTarget:self];
[menuItem setAction:@selector(ibSetProcess:)];
[menuItem setTitle:[process name]];
[menuItem setImage:[process icon]];
[menuItem setRepresentedObject:process];
[processMenu addItem:menuItem];
[menuItem release];
// make this the target if appropiate
if ( _status == TCIdleStatus &&
![_searchData hasSearchedOnce] &&
[savedTarget sameApplicationAs:process] &&
![savedTarget sameApplicationAs:_process] ) {
[_cheater setTarget:process];
}
}
- (void)cheater:(Cheater *)cheater didRemoveProcess:(Process *)process
{
NSMenu *processes = [ibProcessPopup menu];
// remove the service from the menu
[processes removeItemWithRepresentedObject:process];
// if this is the current process, select the first one
if ( [_process isEqual:process] ) {
[_process release];
_process = nil;
if ( [processes numberOfItems] > 0 ) {
[_cheater setTarget:[[processes itemAtIndex:0] representedObject]];
}
}
}
- (void)cheater:(Cheater *)cheater didSetTarget:(Process *)target
{
ChazLog( @"cheater:setTarget:" );
// save a reference to the process
[_process release];
_process = [target retain];
[CheatDocument setGlobalTarget:_process];
// make sure the correct item is selected
[ibProcessPopup selectItemAtIndex:[ibProcessPopup indexOfItemWithRepresentedObject:target]];
//[ibProcessPopup selectItemWithTitle:[target name]];
// apply the name and version to the document if the doc isn't saved
if ( ![self isLoadedFromFile] ) {
[_cheatData setProcess:target];
}
[ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Target is %@.", [target name]] duration:2.0];
[self updateInterface];
}
- (void)cheaterPausedTarget:(Cheater *)cheater
{
_isTargetPaused = YES;
[ibStatusText setTemporaryStatus:@"Target Paused"];
}
- (void)cheaterResumedTarget:(Cheater *)cheater
{
_isTargetPaused = NO;
[ibStatusText setTemporaryStatus:@"Target Resumed"];
}
- (void)cheater:(Cheater *)cheater didFindVariables:(TCArray)variables actualAmount:(unsigned)count
{
if ( _status == TCSearchingStatus ) {
_status = TCIdleStatus;
// do something with the variables
[_searchData setAddresses:variables];
[_searchData didAddResults];
[ibSearchVariableTable reloadData];
[self setActualResults:count];
[ibStatusText setDefaultStatus:[self defaultStatusString]];
if ( count == 1 ) {
NSColor *green = [NSColor colorWithCalibratedRed:0.0 green:0.7 blue:0.0 alpha:1.0];
[ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Search found one result.", count] color:green duration:6.0];
}
else if ( count == 0 ) {
NSColor *red = [NSColor colorWithCalibratedRed:0.7 green:0.0 blue:0.0 alpha:1.0];
[ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Search found no results.", count] color:red duration:6.0];
}
else {
[ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Search found %i results.", count] duration:6.0];
}
[ibStatusBar setIndeterminate:NO];
[ibStatusBar setDoubleValue:0.0];
[ibStatusBar stopAnimation:self];
[self updateInterface];
if ( _mode == TCSearchMode ) {
[ibWindow makeFirstResponder:ibSearchValueField];
}
}
}
- (void)cheater:(Cheater *)cheater didFindValues:(TCArray)values
{
[_searchData setValues:values];
[ibSearchVariableTable reloadData];
[self watchVariables];
}
- (void)cheaterDidCancelSearch:(Cheater *)cheater
{
ChazLog( @"cheaterDidCancelSearch:" );
if ( _isCancelingTask ) {
_status = TCIdleStatus;
_isCancelingTask = NO;
[ibStatusText setDefaultStatus:[self defaultStatusString]];
[ibStatusText setTemporaryStatus:@"Search cancelled." duration:2.0];
[ibStatusBar setIndeterminate:NO];
[ibStatusBar setDoubleValue:0.0];
[ibStatusBar stopAnimation:self];
[self updateInterface];
if ( _mode == TCSearchMode ) {
[ibWindow makeFirstResponder:ibSearchValueField];
}
}
}
- (void)cheaterDidClearSearch:(Cheater *)cheater
{
[_searchData clearResults];
[ibSearchVariableTable reloadData];
[self setActualResults:0];
[ibStatusText setTemporaryStatus:@"The search was cleared." duration:2.0];
[self updateInterface];
}
- (void)cheater:(Cheater *)cheater didDumpMemory:(NSData *)memoryDump
{
NSSavePanel *panel;
ChazLog( @"cheater:didDumpMemory:" );
ChazLog( @"status: %i", _status );
if ( _status == TCDumpingStatus ) {
_status = TCIdleStatus;
panel = [NSSavePanel savePanel];
[panel setRequiredFileType:@"dump"];
[panel setExtensionHidden:NO];
[panel setCanSelectHiddenExtension:YES];
if ( MacOSXVersion() >= 0x1030 ) {
[panel setMessage:@"Dump files are huge! Exercise patience while saving."];
}
[panel beginSheetForDirectory:nil
file:[NSString stringWithFormat:[NSString stringWithFormat:@"%@.dump", [_process name]]]
modalForWindow:ibWindow
modalDelegate:self
didEndSelector:@selector(saveMemoryDumpDidEnd:returnCode:contextInfo:)
contextInfo:memoryDump];
[memoryDump retain];
}
}
- (void)saveMemoryDumpDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
NSData *dump = (NSData *)contextInfo;
[ibStatusText setDefaultStatus:[self defaultStatusString]];
if ( returnCode == NSOKButton ) {
// write the file
[dump writeToFile:[sheet filename] atomically:YES];
[ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Successfully dumped %@'s memory.", [_process name]]];
}
else {
[ibStatusText setTemporaryStatus:@"Dumping memory cancelled."];
}
[dump release];
[ibStatusBar stopAnimation:self];
[ibStatusBar setIndeterminate:NO];
[self updateInterface];
if ( _mode == TCSearchMode ) {
[ibWindow makeFirstResponder:ibSearchValueField];
}
}
- (void)cheaterDidCancelMemoryDump:(Cheater *)cheater
{
ChazLog( @"cheaterDidCancelMemoryDump:" );
if ( _isCancelingTask ) {
_status = TCIdleStatus;
_isCancelingTask = NO;
[ibStatusText setDefaultStatus:[self defaultStatusString]];
[ibStatusText setTemporaryStatus:@"Dumping memory cancelled."];
[ibStatusBar stopAnimation:self];
[ibStatusBar setIndeterminate:NO];
[self updateInterface];
if ( _mode == TCSearchMode ) {
[ibWindow makeFirstResponder:ibSearchValueField];
}
}
}
- (void)cheater:(Cheater *)cheater didChangeVariables:(unsigned)changeCount
{
ChazLog( @"CHEATER changed %u variables.", changeCount );
if ( ![_cheatData repeats] ) {
_status = TCIdleStatus;
}
if ( changeCount == 0 ) {
[ibStatusText setTemporaryStatus:@"No variables were changed." duration:2.0];
}
else if ( changeCount == 1 ) {
[ibStatusText setTemporaryStatus:@"Changed one variable." duration:2.0];
}
else {
[ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Changed %i variables.", changeCount] duration:2.0];
}
[self updateInterface];
}
- (void)cheaterDidStopChangingVariables:(Cheater *)cheater
{
_status = TCIdleStatus;
_isCancelingTask = NO;
[ibStatusText setDefaultStatus:[self defaultStatusString]];
[ibStatusBar stopAnimation:self];
[ibStatusBar setIndeterminate:NO];
[self updateInterface];
if ( _mode == TCSearchMode ) {
[ibWindow makeFirstResponder:ibSearchValueField];
}
}
- (void)cheater:(Cheater *)cheater didReportProgress:(int)progress
{
if ( _status == TCSearchingStatus && !_isCancelingTask ) {
[ibStatusBar setDoubleValue:(double)progress];
}
}
- (void)cheater:(Cheater *)cheater didRevertToVariables:(TCArray)variables actualAmount:(unsigned)count
{
// do something with the variables
[_searchData setAddresses:variables];
[ibSearchVariableTable reloadData];
[self setActualResults:count];
[self updateInterface];
}
- (void)cheaterDidUndo:(Cheater *)cheater
{
[_searchData didUndo];
[ibStatusText setTemporaryStatus:@"Reverted to previous results." duration:2.0];
}
- (void)cheaterDidRedo:(Cheater *)cheater
{
[_searchData didRedo];
[ibStatusText setTemporaryStatus:@"Reverted to saved results." duration:2.0];
}
- (void)cheater:(Cheater *)cheater variableAtIndex:(unsigned)index didChangeTo:(Variable *)variable
{
[_searchData setValue:variable atIndex:index];
[ibSearchVariableTable reloadData];
}
- (void)cheater:(Cheater *)cheater didFailLastRequest:(NSString *)reason
{
_status = TCIdleStatus;
// echo the reason to the status
[self showError:reason];
[ibStatusBar setIndeterminate:NO];
[ibStatusBar setDoubleValue:0.0];
[ibStatusBar stopAnimation:self];
[self updateInterface];
}
- (void)cheater:(Cheater *)cheater echo:(NSString *)message
{
// echo the message to the status
[ibStatusText setTemporaryStatus:message duration:7.0];
}
@end