]> Dogcows Code - chaz/thecheat/blob - DocCheaterDelegate.m
The Cheat 1.2.1
[chaz/thecheat] / DocCheaterDelegate.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
9 // any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20
21
22 #import "CheatDocument.h"
23
24
25 @implementation CheatDocument ( DocCheaterDelegate )
26
27
28 - (void)cheaterDidConnect:(Cheater *)cheater
29 {
30 ChazLog( @"cheaterDidConnect:" );
31 [ibStatusText setDefaultStatus:[self defaultStatusString]];
32 // update the interface
33 [self updateInterface];
34 }
35
36 - (void)cheaterDidDisconnect:(Cheater *)cheater
37 {
38 ChazLog( @"cheaterDidDisonnect:" );
39 [self disconnectFromCheater];
40 // update status
41 [self showError:@"Disconnected by server."];
42 [ibStatusBar setIndeterminate:NO];
43 [ibStatusBar setDoubleValue:0.0];
44 [ibStatusBar stopAnimation:self];
45 // update the interface
46 [self updateInterface];
47 }
48
49
50 - (void)cheaterRequiresAuthentication:(Cheater *)cheater
51 {
52 ChazLog( @"cheaterRequiresAuthentication:" );
53
54 [self ibRunPasswordSheet:nil];
55 }
56
57 - (void)cheaterRejectedPassword:(Cheater *)cheater
58 {
59 ChazLog( @"cheaterRejectedPassword" );
60
61
62 }
63
64 - (void)cheaterAcceptedPassword:(Cheater *)cheater
65 {
66 ChazLog( @"cheaterAcceptedPassword" );
67
68 [ibStatusText setTemporaryStatus:@"Password Accepted"];
69 }
70
71
72 - (void)cheater:(Cheater *)cheater didFindProcesses:(NSArray *)processes
73 {
74 NSMenu *processMenu;
75 NSMenuItem *menuItem;
76
77 unsigned i, len;
78
79 Process *selectThis = nil;
80
81 ChazLog( @"cheater:didFindProcesses:" );
82
83 // create and set the server popup menu
84 processMenu = [[NSMenu alloc] init];
85 [processMenu setAutoenablesItems:YES];
86
87 // add menu items
88 // processes returned
89 len = [processes count];
90 for ( i = 0; i < len; i++ ) {
91 Process *item = [processes objectAtIndex:i];
92
93 menuItem = [[NSMenuItem alloc] init];
94 [menuItem setTarget:self];
95 [menuItem setAction:@selector(ibSetProcess:)];
96 [menuItem setTitle:[item name]];
97 [menuItem setImage:[item icon]];
98 [menuItem setRepresentedObject:item];
99 [processMenu addItem:menuItem];
100 [menuItem release];
101
102 // check and see if our document uses this application
103 if ( [self isLoadedFromFile] && [[_cheatData process] sameApplicationAs:item] ) {
104 selectThis = item;
105 }
106 }
107 // set the menu
108 [ibProcessPopup setMenu:processMenu];
109 [processMenu release];
110
111 // if we're loaded from a file, select the process matching
112 // the one saved, if it is launched.
113 if ( selectThis ) {
114 [_cheater setTarget:selectThis];
115 }
116 // otherwise, select the global target
117 else if ( selectThis = [CheatDocument globalTarget] ) {
118 ChazLog( @"setting global target" );
119 [_cheater setTarget:selectThis];
120 }
121 // otherwise, select the first process in this list
122 else if ( len > 0 ) {
123 [_cheater setTarget:[processes objectAtIndex:0]];
124 }
125 }
126
127 - (void)cheater:(Cheater *)cheater didAddProcess:(Process *)process
128 {
129 NSMenu *processMenu = [ibProcessPopup menu];
130 NSMenuItem *menuItem;
131
132 Process *savedTarget = [_cheatData process];
133
134 // add the newly found process to the process popup
135 menuItem = [[NSMenuItem alloc] init];
136 [menuItem setTarget:self];
137 [menuItem setAction:@selector(ibSetProcess:)];
138 [menuItem setTitle:[process name]];
139 [menuItem setImage:[process icon]];
140 [menuItem setRepresentedObject:process];
141 [processMenu addItem:menuItem];
142 [menuItem release];
143
144 // make this the target if appropiate
145 if ( _status == TCIdleStatus &&
146 ![_searchData hasSearchedOnce] &&
147 [savedTarget sameApplicationAs:process] &&
148 ![savedTarget sameApplicationAs:_process] ) {
149 [_cheater setTarget:process];
150 }
151 }
152
153 - (void)cheater:(Cheater *)cheater didRemoveProcess:(Process *)process
154 {
155 NSMenu *processes = [ibProcessPopup menu];
156 // remove the service from the menu
157 [processes removeItemWithRepresentedObject:process];
158
159 // if this is the current process, select the first one
160 if ( [_process isEqual:process] ) {
161 [_process release];
162 _process = nil;
163
164 if ( [processes numberOfItems] > 0 ) {
165 [_cheater setTarget:[[processes itemAtIndex:0] representedObject]];
166 }
167 }
168 }
169
170
171 - (void)cheater:(Cheater *)cheater didSetTarget:(Process *)target
172 {
173 ChazLog( @"cheater:setTarget:" );
174
175 // save a reference to the process
176 [_process release];
177 _process = [target retain];
178
179 [CheatDocument setGlobalTarget:_process];
180
181 // make sure the correct item is selected
182 [ibProcessPopup selectItemAtIndex:[ibProcessPopup indexOfItemWithRepresentedObject:target]];
183 //[ibProcessPopup selectItemWithTitle:[target name]];
184
185 // apply the name and version to the document if the doc isn't saved
186 if ( ![self isLoadedFromFile] ) {
187 [_cheatData setProcess:target];
188 }
189
190 [ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Target is %@.", [target name]] duration:2.0];
191 [self updateInterface];
192 }
193
194 - (void)cheaterPausedTarget:(Cheater *)cheater
195 {
196 _isTargetPaused = YES;
197 [ibStatusText setTemporaryStatus:@"Target Paused"];
198 }
199
200 - (void)cheaterResumedTarget:(Cheater *)cheater
201 {
202 _isTargetPaused = NO;
203 [ibStatusText setTemporaryStatus:@"Target Resumed"];
204 }
205
206
207 - (void)cheater:(Cheater *)cheater didFindVariables:(TCArray)variables actualAmount:(unsigned)count
208 {
209 if ( _status == TCSearchingStatus ) {
210 _status = TCIdleStatus;
211
212 // do something with the variables
213 [_searchData setAddresses:variables];
214 [_searchData didAddResults];
215 [ibSearchVariableTable reloadData];
216
217 [self setActualResults:count];
218
219 [ibStatusText setDefaultStatus:[self defaultStatusString]];
220 if ( count == 1 ) {
221 NSColor *green = [NSColor colorWithCalibratedRed:0.0 green:0.7 blue:0.0 alpha:1.0];
222 [ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Search found one result.", count] color:green duration:6.0];
223 }
224 else if ( count == 0 ) {
225 NSColor *red = [NSColor colorWithCalibratedRed:0.7 green:0.0 blue:0.0 alpha:1.0];
226 [ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Search found no results.", count] color:red duration:6.0];
227 }
228 else {
229 [ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Search found %i results.", count] duration:6.0];
230 }
231 [ibStatusBar setIndeterminate:NO];
232 [ibStatusBar setDoubleValue:0.0];
233 [ibStatusBar stopAnimation:self];
234
235 [self updateInterface];
236 if ( _mode == TCSearchMode ) {
237 [ibWindow makeFirstResponder:ibSearchValueField];
238 }
239 }
240 }
241
242 - (void)cheater:(Cheater *)cheater didFindValues:(TCArray)values
243 {
244 [_searchData setValues:values];
245 [ibSearchVariableTable reloadData];
246
247 [self watchVariables];
248 }
249
250 - (void)cheaterDidCancelSearch:(Cheater *)cheater
251 {
252 ChazLog( @"cheaterDidCancelSearch:" );
253
254 if ( _isCancelingTask ) {
255 _status = TCIdleStatus;
256 _isCancelingTask = NO;
257
258 [ibStatusText setDefaultStatus:[self defaultStatusString]];
259 [ibStatusText setTemporaryStatus:@"Search cancelled." duration:2.0];
260 [ibStatusBar setIndeterminate:NO];
261 [ibStatusBar setDoubleValue:0.0];
262 [ibStatusBar stopAnimation:self];
263
264 [self updateInterface];
265 if ( _mode == TCSearchMode ) {
266 [ibWindow makeFirstResponder:ibSearchValueField];
267 }
268 }
269 }
270
271 - (void)cheaterDidClearSearch:(Cheater *)cheater
272 {
273 [_searchData clearResults];
274 [ibSearchVariableTable reloadData];
275
276 [self setActualResults:0];
277
278 [ibStatusText setTemporaryStatus:@"The search was cleared." duration:2.0];
279 [self updateInterface];
280 }
281
282
283 - (void)cheater:(Cheater *)cheater didDumpMemory:(NSData *)memoryDump
284 {
285 NSSavePanel *panel;
286
287 ChazLog( @"cheater:didDumpMemory:" );
288
289 ChazLog( @"status: %i", _status );
290 if ( _status == TCDumpingStatus ) {
291 _status = TCIdleStatus;
292
293 panel = [NSSavePanel savePanel];
294 [panel setRequiredFileType:@"dump"];
295 [panel setExtensionHidden:NO];
296 [panel setCanSelectHiddenExtension:YES];
297 if ( MacOSXVersion() >= 0x1030 ) {
298 [panel setMessage:@"Dump files are huge! Exercise patience while saving."];
299 }
300
301 [panel beginSheetForDirectory:nil
302 file:[NSString stringWithFormat:[NSString stringWithFormat:@"%@.dump", [_process name]]]
303 modalForWindow:ibWindow
304 modalDelegate:self
305 didEndSelector:@selector(saveMemoryDumpDidEnd:returnCode:contextInfo:)
306 contextInfo:memoryDump];
307 [memoryDump retain];
308 }
309 }
310
311 - (void)saveMemoryDumpDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
312 {
313 NSData *dump = (NSData *)contextInfo;
314
315 [ibStatusText setDefaultStatus:[self defaultStatusString]];
316
317 if ( returnCode == NSOKButton ) {
318 // write the file
319 [dump writeToFile:[sheet filename] atomically:YES];
320
321 [ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Successfully dumped %@'s memory.", [_process name]]];
322 }
323 else {
324 [ibStatusText setTemporaryStatus:@"Dumping memory cancelled."];
325 }
326
327 [dump release];
328
329 [ibStatusBar stopAnimation:self];
330 [ibStatusBar setIndeterminate:NO];
331
332 [self updateInterface];
333 if ( _mode == TCSearchMode ) {
334 [ibWindow makeFirstResponder:ibSearchValueField];
335 }
336 }
337
338 - (void)cheaterDidCancelMemoryDump:(Cheater *)cheater
339 {
340 ChazLog( @"cheaterDidCancelMemoryDump:" );
341
342 if ( _isCancelingTask ) {
343 _status = TCIdleStatus;
344 _isCancelingTask = NO;
345
346 [ibStatusText setDefaultStatus:[self defaultStatusString]];
347 [ibStatusText setTemporaryStatus:@"Dumping memory cancelled."];
348 [ibStatusBar stopAnimation:self];
349 [ibStatusBar setIndeterminate:NO];
350
351 [self updateInterface];
352 if ( _mode == TCSearchMode ) {
353 [ibWindow makeFirstResponder:ibSearchValueField];
354 }
355 }
356 }
357
358
359 - (void)cheater:(Cheater *)cheater didChangeVariables:(unsigned)changeCount
360 {
361 ChazLog( @"CHEATER changed %u variables.", changeCount );
362 if ( ![_cheatData repeats] ) {
363 _status = TCIdleStatus;
364 }
365
366 if ( changeCount == 0 ) {
367 [ibStatusText setTemporaryStatus:@"No variables were changed." duration:2.0];
368 }
369 else if ( changeCount == 1 ) {
370 [ibStatusText setTemporaryStatus:@"Changed one variable." duration:2.0];
371 }
372 else {
373 [ibStatusText setTemporaryStatus:[NSString stringWithFormat:@"Changed %i variables.", changeCount] duration:2.0];
374 }
375
376 [self updateInterface];
377 }
378
379 - (void)cheaterDidStopChangingVariables:(Cheater *)cheater
380 {
381 _status = TCIdleStatus;
382 _isCancelingTask = NO;
383
384 [ibStatusText setDefaultStatus:[self defaultStatusString]];
385 [ibStatusBar stopAnimation:self];
386 [ibStatusBar setIndeterminate:NO];
387
388 [self updateInterface];
389 if ( _mode == TCSearchMode ) {
390 [ibWindow makeFirstResponder:ibSearchValueField];
391 }
392 }
393
394
395 - (void)cheater:(Cheater *)cheater didReportProgress:(int)progress
396 {
397 if ( _status == TCSearchingStatus && !_isCancelingTask ) {
398 [ibStatusBar setDoubleValue:(double)progress];
399 }
400 }
401
402
403 - (void)cheater:(Cheater *)cheater didRevertToVariables:(TCArray)variables actualAmount:(unsigned)count
404 {
405 // do something with the variables
406 [_searchData setAddresses:variables];
407 [ibSearchVariableTable reloadData];
408
409 [self setActualResults:count];
410
411 [self updateInterface];
412 }
413
414
415 - (void)cheaterDidUndo:(Cheater *)cheater
416 {
417 [_searchData didUndo];
418
419 [ibStatusText setTemporaryStatus:@"Reverted to previous results." duration:2.0];
420 }
421
422 - (void)cheaterDidRedo:(Cheater *)cheater
423 {
424 [_searchData didRedo];
425
426 [ibStatusText setTemporaryStatus:@"Reverted to saved results." duration:2.0];
427 }
428
429
430 - (void)cheater:(Cheater *)cheater variableAtIndex:(unsigned)index didChangeTo:(Variable *)variable
431 {
432 [_searchData setValue:variable atIndex:index];
433 [ibSearchVariableTable reloadData];
434 }
435
436
437 - (void)cheater:(Cheater *)cheater didFailLastRequest:(NSString *)reason
438 {
439 _status = TCIdleStatus;
440
441 // echo the reason to the status
442 [self showError:reason];
443 [ibStatusBar setIndeterminate:NO];
444 [ibStatusBar setDoubleValue:0.0];
445 [ibStatusBar stopAnimation:self];
446
447 [self updateInterface];
448 }
449
450 - (void)cheater:(Cheater *)cheater echo:(NSString *)message
451 {
452 // echo the message to the status
453 [ibStatusText setTemporaryStatus:message duration:7.0];
454 }
455
456
457 @end
This page took 0.05084 seconds and 4 git commands to generate.