2 // DocCheaterDelegate.m
5 // Created by Chaz McGarvey on 12/26/04.
6 // Copyright 2004 Chaz McGarvey. All rights reserved.
9 #import "CheatDocument.h"
12 @implementation CheatDocument ( DocCheaterDelegate
)
15 - (void)cheaterDidConnect
:(Cheater
*)cheater
17 ChazLog( @
"cheaterDidConnect:" );
18 [ibStatusText setDefaultStatus
:[self defaultStatusString
]];
19 // update the interface
20 [self updateInterface
];
23 - (void)cheaterDidDisconnect
:(Cheater
*)cheater
25 ChazLog( @
"cheaterDidDisonnect:" );
26 [self disconnectFromCheater
];
28 [self showError
:@
"Disconnected by server."];
29 [ibStatusBar setIndeterminate
:NO
];
30 [ibStatusBar setDoubleValue
:0.0];
31 [ibStatusBar stopAnimation
:self];
32 // update the interface
33 [self updateInterface
];
37 - (void)cheaterRequiresAuthentication
:(Cheater
*)cheater
39 ChazLog( @
"cheaterRequiresAuthentication:" );
41 [self ibRunPasswordSheet
:nil];
44 - (void)cheaterRejectedPassword
:(Cheater
*)cheater
46 ChazLog( @
"cheaterRejectedPassword" );
51 - (void)cheaterAcceptedPassword
:(Cheater
*)cheater
53 ChazLog( @
"cheaterAcceptedPassword" );
55 [ibStatusText setTemporaryStatus
:@
"Password Accepted"];
59 - (void)cheater
:(Cheater
*)cheater didFindProcesses
:(NSArray
*)processes
66 Process
*selectThis
= nil;
68 ChazLog( @
"cheater:didFindProcesses:" );
70 // create and set the server popup menu
71 processMenu
= [[NSMenu alloc
] init
];
72 [processMenu setAutoenablesItems
:YES
];
76 len
= [processes count
];
77 for ( i
= 0; i
< len
; i
++ ) {
78 Process
*item
= [processes objectAtIndex
:i
];
80 menuItem
= [[NSMenuItem alloc
] init
];
81 [menuItem setTarget
:self];
82 [menuItem setAction
:@selector(ibSetProcess
:)];
83 [menuItem setTitle
:[item name
]];
84 [menuItem setImage
:[item icon
]];
85 [menuItem setRepresentedObject
:item
];
86 [processMenu addItem
:menuItem
];
89 // check and see if our document uses this application
90 if ( [self isLoadedFromFile
] && [[_cheatData process
] sameApplicationAs
:item
] ) {
95 [ibProcessPopup setMenu
:processMenu
];
96 [processMenu release
];
98 // if we're loaded from a file, select the process matching
99 // the one saved, if it is launched.
101 [_cheater setTarget
:selectThis
];
103 // otherwise, select the global target
104 else if ( selectThis
= [CheatDocument globalTarget
] ) {
105 ChazLog( @
"setting global target" );
106 [_cheater setTarget
:selectThis
];
108 // otherwise, select the first process in this list
109 else if ( len
> 0 ) {
110 [_cheater setTarget
:[processes objectAtIndex
:0]];
114 - (void)cheater
:(Cheater
*)cheater didAddProcess
:(Process
*)process
116 NSMenu
*processMenu
= [ibProcessPopup menu
];
117 NSMenuItem
*menuItem
;
119 Process
*savedTarget
= [_cheatData process
];
121 // add the newly found process to the process popup
122 menuItem
= [[NSMenuItem alloc
] init
];
123 [menuItem setTarget
:self];
124 [menuItem setAction
:@selector(ibSetProcess
:)];
125 [menuItem setTitle
:[process name
]];
126 [menuItem setImage
:[process icon
]];
127 [menuItem setRepresentedObject
:process
];
128 [processMenu addItem
:menuItem
];
131 // make this the target if appropiate
132 if ( _status
== TCIdleStatus
&&
133 ![_searchData hasSearchedOnce
] &&
134 [savedTarget sameApplicationAs
:process
] &&
135 ![savedTarget sameApplicationAs
:_process
] ) {
136 [_cheater setTarget
:process
];
140 - (void)cheater
:(Cheater
*)cheater didRemoveProcess
:(Process
*)process
142 NSMenu
*processes
= [ibProcessPopup menu
];
143 // remove the service from the menu
144 [processes removeItemWithRepresentedObject
:process
];
146 // if this is the current process, select the first one
147 if ( [_process isEqual
:process
] ) {
151 if ( [processes numberOfItems
] > 0 ) {
152 [_cheater setTarget
:[[processes itemAtIndex
:0] representedObject
]];
158 - (void)cheater
:(Cheater
*)cheater didSetTarget
:(Process
*)target
160 ChazLog( @
"cheater:setTarget:" );
162 // save a reference to the process
164 _process
= [target retain
];
166 [CheatDocument setGlobalTarget
:_process
];
168 // make sure the correct item is selected
169 [ibProcessPopup selectItemAtIndex
:[ibProcessPopup indexOfItemWithRepresentedObject
:target
]];
170 //[ibProcessPopup selectItemWithTitle:[target name]];
172 // apply the name and version to the document if the doc isn't saved
173 if ( ![self isLoadedFromFile
] ) {
174 [_cheatData setProcess
:target
];
177 [ibStatusText setTemporaryStatus
:[NSString stringWithFormat
:@
"Target is %@.", [target name
]] duration
:2.0];
178 [self updateInterface
];
181 - (void)cheaterPausedTarget
:(Cheater
*)cheater
183 _isTargetPaused
= YES
;
184 [ibStatusText setTemporaryStatus
:@
"Target Paused"];
187 - (void)cheaterResumedTarget
:(Cheater
*)cheater
189 _isTargetPaused
= NO
;
190 [ibStatusText setTemporaryStatus
:@
"Target Resumed"];
194 - (void)cheater
:(Cheater
*)cheater didFindVariables
:(TCArray
)variables actualAmount
:(unsigned)count
196 if ( _status
== TCSearchingStatus
) {
197 _status
= TCIdleStatus
;
199 // do something with the variables
200 [_searchData setAddresses
:variables
];
201 [_searchData didAddResults
];
202 [ibSearchVariableTable reloadData
];
204 [self setActualResults
:count
];
206 [ibStatusText setDefaultStatus
:[self defaultStatusString
]];
208 NSColor
*green
= [NSColor colorWithCalibratedRed
:0.0 green
:0.7 blue
:0.0 alpha
:1.0];
209 [ibStatusText setTemporaryStatus
:[NSString stringWithFormat
:@
"Search found one result.", count
] color
:green duration
:6.0];
211 else if ( count
== 0 ) {
212 NSColor
*red
= [NSColor colorWithCalibratedRed
:0.7 green
:0.0 blue
:0.0 alpha
:1.0];
213 [ibStatusText setTemporaryStatus
:[NSString stringWithFormat
:@
"Search found no results.", count
] color
:red duration
:6.0];
216 [ibStatusText setTemporaryStatus
:[NSString stringWithFormat
:@
"Search found %i results.", count
] duration
:6.0];
218 [ibStatusBar setIndeterminate
:NO
];
219 [ibStatusBar setDoubleValue
:0.0];
220 [ibStatusBar stopAnimation
:self];
222 [self updateInterface
];
223 if ( _mode
== TCSearchMode
) {
224 [ibWindow makeFirstResponder
:ibSearchValueField
];
229 - (void)cheater
:(Cheater
*)cheater didFindValues
:(TCArray
)values
231 [_searchData setValues
:values
];
232 [ibSearchVariableTable reloadData
];
234 [self watchVariables
];
237 - (void)cheaterDidCancelSearch
:(Cheater
*)cheater
239 ChazLog( @
"cheaterDidCancelSearch:" );
241 if ( _isCancelingTask
) {
242 _status
= TCIdleStatus
;
243 _isCancelingTask
= NO
;
245 [ibStatusText setDefaultStatus
:[self defaultStatusString
]];
246 [ibStatusText setTemporaryStatus
:@
"Search cancelled." duration
:2.0];
247 [ibStatusBar setIndeterminate
:NO
];
248 [ibStatusBar setDoubleValue
:0.0];
249 [ibStatusBar stopAnimation
:self];
251 [self updateInterface
];
252 if ( _mode
== TCSearchMode
) {
253 [ibWindow makeFirstResponder
:ibSearchValueField
];
258 - (void)cheaterDidClearSearch
:(Cheater
*)cheater
260 [_searchData clearResults
];
261 [ibSearchVariableTable reloadData
];
263 [self setActualResults
:0];
265 [ibStatusText setTemporaryStatus
:@
"The search was cleared." duration
:2.0];
266 [self updateInterface
];
270 - (void)cheater
:(Cheater
*)cheater didDumpMemory
:(NSData
*)memoryDump
274 ChazLog( @
"cheater:didDumpMemory:" );
276 ChazLog( @
"status: %i", _status
);
277 if ( _status
== TCDumpingStatus
) {
278 _status
= TCIdleStatus
;
280 panel
= [NSSavePanel savePanel
];
281 [panel setRequiredFileType
:@
"dump"];
282 [panel setExtensionHidden
:NO
];
283 [panel setCanSelectHiddenExtension
:YES
];
284 if ( MacOSXVersion() >= 0x1030 ) {
285 [panel setMessage
:@
"Dump files are huge! Exercise patience while saving."];
288 [panel beginSheetForDirectory
:nil
289 file
:[NSString stringWithFormat
:[NSString stringWithFormat
:@
"%@.dump", [_process name
]]]
290 modalForWindow
:ibWindow
292 didEndSelector
:@selector(saveMemoryDumpDidEnd
:returnCode
:contextInfo
:)
293 contextInfo
:memoryDump
];
298 - (void)saveMemoryDumpDidEnd
:(NSSavePanel
*)sheet returnCode
:(int)returnCode contextInfo
:(void *)contextInfo
300 NSData
*dump
= (NSData
*)contextInfo
;
302 [ibStatusText setDefaultStatus
:[self defaultStatusString
]];
304 if ( returnCode
== NSOKButton
) {
306 [dump writeToFile
:[sheet filename
] atomically
:YES
];
308 [ibStatusText setTemporaryStatus
:[NSString stringWithFormat
:@
"Successfully dumped %@'s memory.", [_process name
]]];
311 [ibStatusText setTemporaryStatus
:@
"Dumping memory cancelled."];
316 [ibStatusBar stopAnimation
:self];
317 [ibStatusBar setIndeterminate
:NO
];
319 [self updateInterface
];
320 if ( _mode
== TCSearchMode
) {
321 [ibWindow makeFirstResponder
:ibSearchValueField
];
325 - (void)cheaterDidCancelMemoryDump
:(Cheater
*)cheater
327 ChazLog( @
"cheaterDidCancelMemoryDump:" );
329 if ( _isCancelingTask
) {
330 _status
= TCIdleStatus
;
331 _isCancelingTask
= NO
;
333 [ibStatusText setDefaultStatus
:[self defaultStatusString
]];
334 [ibStatusText setTemporaryStatus
:@
"Dumping memory cancelled."];
335 [ibStatusBar stopAnimation
:self];
336 [ibStatusBar setIndeterminate
:NO
];
338 [self updateInterface
];
339 if ( _mode
== TCSearchMode
) {
340 [ibWindow makeFirstResponder
:ibSearchValueField
];
346 - (void)cheater
:(Cheater
*)cheater didChangeVariables
:(unsigned)changeCount
348 ChazLog( @
"CHEATER changed %u variables.", changeCount
);
349 if ( ![_cheatData repeats
] ) {
350 _status
= TCIdleStatus
;
353 if ( changeCount
== 0 ) {
354 [ibStatusText setTemporaryStatus
:@
"No variables were changed." duration
:2.0];
356 else if ( changeCount
== 1 ) {
357 [ibStatusText setTemporaryStatus
:@
"Changed one variable." duration
:2.0];
360 [ibStatusText setTemporaryStatus
:[NSString stringWithFormat
:@
"Changed %i variables.", changeCount
] duration
:2.0];
363 [self updateInterface
];
366 - (void)cheaterDidStopChangingVariables
:(Cheater
*)cheater
368 _status
= TCIdleStatus
;
369 _isCancelingTask
= NO
;
371 [ibStatusText setDefaultStatus
:[self defaultStatusString
]];
372 [ibStatusBar stopAnimation
:self];
373 [ibStatusBar setIndeterminate
:NO
];
375 [self updateInterface
];
376 if ( _mode
== TCSearchMode
) {
377 [ibWindow makeFirstResponder
:ibSearchValueField
];
382 - (void)cheater
:(Cheater
*)cheater didReportProgress
:(int)progress
384 if ( _status
== TCSearchingStatus
&& !_isCancelingTask
) {
385 [ibStatusBar setDoubleValue
:(double)progress
];
390 - (void)cheater
:(Cheater
*)cheater didRevertToVariables
:(TCArray
)variables actualAmount
:(unsigned)count
392 // do something with the variables
393 [_searchData setAddresses
:variables
];
394 [ibSearchVariableTable reloadData
];
396 [self setActualResults
:count
];
398 [self updateInterface
];
402 - (void)cheaterDidUndo
:(Cheater
*)cheater
404 [_searchData didUndo
];
406 [ibStatusText setTemporaryStatus
:@
"Reverted to previous results." duration
:2.0];
409 - (void)cheaterDidRedo
:(Cheater
*)cheater
411 [_searchData didRedo
];
413 [ibStatusText setTemporaryStatus
:@
"Reverted to saved results." duration
:2.0];
417 - (void)cheater
:(Cheater
*)cheater variableAtIndex
:(unsigned)index didChangeTo
:(Variable
*)variable
419 [_searchData setValue
:variable atIndex
:index
];
420 [ibSearchVariableTable reloadData
];
424 - (void)cheater
:(Cheater
*)cheater didFailLastRequest
:(NSString
*)reason
426 _status
= TCIdleStatus
;
428 // echo the reason to the status
429 [self showError
:reason
];
430 [ibStatusBar setIndeterminate
:NO
];
431 [ibStatusBar setDoubleValue
:0.0];
432 [ibStatusBar stopAnimation
:self];
434 [self updateInterface
];
437 - (void)cheater
:(Cheater
*)cheater echo
:(NSString
*)message
439 // echo the message to the status
440 [ibStatusText setTemporaryStatus
:message duration
:7.0];