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