]> Dogcows Code - chaz/thecheat/blob - Process.m
The Cheat 1.2.4
[chaz/thecheat] / Process.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 #import "Process.h"
22
23 #ifdef __i386__
24 #import <sys/types.h>
25 #import <sys/sysctl.h>
26 #endif
27
28 @interface Process ( PrivateAPI )
29
30 - (void)_setName:(NSString *)name;
31 - (void)_setVersion:(NSString *)version;
32 - (void)_setIcon:(NSImage *)icon;
33 - (void)_setPID:(pid_t)pid;
34
35 @end
36
37
38 @implementation Process
39
40
41 - (id)initWithName:(NSString *)name version:(NSString *)version icon:(NSImage *)icon pid:(pid_t)pid
42 {
43 if ( self = [super init] ) {
44 [self _setName:name];
45 [self _setVersion:version];
46 [self _setIcon:icon];
47 [self _setPID:pid];
48 }
49 return self;
50 }
51
52 - (id)initWithName:(NSString *)name version:(NSString *)version icon:(NSImage *)icon
53 {
54 return [self initWithName:name version:version icon:icon pid:0];
55 }
56
57 - (id)initWithName:(NSString *)name version:(NSString *)version
58 {
59 return [self initWithName:name version:version icon:nil pid:0];
60 }
61
62 - (void)dealloc
63 {
64 // release local objects
65 [_name release];
66 [_version release];
67 [_icon release];
68
69 [super dealloc];
70 }
71
72
73 // #############################################################################
74 #pragma mark NSCoding
75 // #############################################################################
76
77 - (id)copyWithZone:(NSZone *)zone
78 {
79 return [[Process allocWithZone:zone] initWithName:_name version:_version icon:_icon pid:_pid];
80 }
81
82
83 // #############################################################################
84 #pragma mark NSCoding
85 // #############################################################################
86
87 - (id)initWithCoder:(NSCoder *)coder
88 {
89 if ( self = [super init] )
90 {
91 [self _setName:[coder decodeObject]];
92 [self _setVersion:[coder decodeObject]];
93 //[self setIcon:[coder decodeObject]];
94 [coder decodeValueOfObjCType:@encode(pid_t) at:&_pid];
95 }
96 return self;
97 }
98
99 - (void)encodeWithCoder:(NSCoder *)coder
100 {
101 [coder encodeObject:_name];
102 [coder encodeObject:_version];
103 //[coder encodeObject:_icon];
104 [coder encodeValueOfObjCType:@encode(pid_t) at:&_pid];
105 }
106
107
108 #pragma mark NSObject Override
109
110 - (BOOL)isEqual:(id)anObject
111 {
112 if ( [_name isEqualToString:[(Process *)anObject name]] &&
113 [_version isEqualToString:[(Process *)anObject version]] &&
114 _pid == [(Process *)anObject pid] ) {
115 // they are the same process
116 return YES;
117 }
118 return NO;
119 }
120
121 - (unsigned)hash
122 {
123 return [[NSString stringWithFormat:@"%@%@%u", _name, _version, _pid] hash];
124 }
125
126
127 - (BOOL)sameApplicationAs:(id)anObject
128 {
129 if ( [_name isEqualToString:[(Process *)anObject name]] &&
130 [_version isEqualToString:[(Process *)anObject version]] ) {
131 // they are the same application
132 return YES;
133 }
134 return NO;
135 }
136
137 #pragma mark Detecting Emulation
138
139 #ifdef __i386__
140 // http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/universal_binary_exec_a/universal_binary_exec_a.html
141 static int sysctlbyname_with_pid (const char *name, pid_t pid,
142 void *oldp, size_t *oldlenp,
143 void *newp, size_t newlen)
144 {
145 if (pid == 0) {
146 if (sysctlbyname(name, oldp, oldlenp, newp, newlen) == -1) {
147 fprintf(stderr, "sysctlbyname_with_pid(0): sysctlbyname failed:"
148 "%s\n", strerror(errno));
149 return -1;
150 }
151 } else {
152 int mib[CTL_MAXNAME+1];
153 size_t len = CTL_MAXNAME;
154 if (sysctlnametomib(name, mib, &len) == -1) {
155 fprintf(stderr, "sysctlbyname_with_pid: sysctlnametomib failed:"
156 "%s\n", strerror(errno));
157 return -1;
158 }
159 mib[len] = pid;
160 len++;
161 if (sysctl(mib, len, oldp, oldlenp, newp, newlen) == -1) {
162 fprintf(stderr, "sysctlbyname_with_pid: sysctl failed:"
163 "%s\n", strerror(errno));
164 return -1;
165 }
166 }
167 return 0;
168 }
169
170 // http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/universal_binary_exec_a/universal_binary_exec_a.html
171 static int is_pid_native (pid_t pid)
172 {
173 int ret = 0;
174 size_t sz = sizeof(ret);
175
176 if (sysctlbyname_with_pid("sysctl.proc_native", pid,
177 &ret, &sz, NULL, 0) == -1) {
178 if (errno == ENOENT) {
179 return 1;
180 }
181 fprintf(stderr, "is_pid_native: sysctlbyname_with_pid failed:"
182 "%s\n", strerror(errno));
183 return -1;
184 }
185 return ret;
186 }
187 #endif
188
189 - (BOOL)isEmulated
190 {
191 BOOL isEmulated = NO;
192 #ifdef __i386__
193 if (is_pid_native(_pid) == 0)
194 {
195 isEmulated = YES;
196 }
197 #endif
198
199 return isEmulated;
200 }
201
202 #pragma mark Accessors
203
204 - (NSString *)name
205 {
206 return _name;
207 }
208
209 - (NSString *)version
210 {
211 return _version;
212 }
213
214 - (NSImage *)icon
215 {
216 return _icon;
217 }
218
219 - (pid_t)pid
220 {
221 return _pid;
222 }
223
224
225 - (void)_setName:(NSString *)name
226 {
227 if ( !name ) {
228 name = [NSString stringWithString:@"Unknown Process"];
229 }
230 [name retain];
231 [_name release];
232 _name = name;
233 }
234
235 - (void)_setVersion:(NSString *)version
236 {
237 if ( !version ) {
238 version = [NSString stringWithString:@"Unknown Version"];
239 }
240 [version retain];
241 [_version release];
242 _version = version;
243 }
244
245 - (void)_setIcon:(NSImage *)icon
246 {
247 [icon retain];
248 [_icon release];
249 _icon = icon;
250
251 // resize new image
252 [_icon setScalesWhenResized:YES];
253 [_icon setSize:NSMakeSize(16,16)];
254 }
255
256 - (void)_setPID:(pid_t)pid
257 {
258 _pid = pid;
259 }
260
261
262 @end
This page took 0.045014 seconds and 4 git commands to generate.