3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.dogcows.com/chaz/wiki/TheCheat
6 * Copyright (c) 2003-2010, Charles McGarvey et al.
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.
14 #if defined(__i386__) || defined(__x86_64__)
16 #import <sys/sysctl.h>
19 @interface Process ( PrivateAPI
)
21 - (void)_setName
:(NSString
*)name
;
22 - (void)_setVersion
:(NSString
*)version
;
23 - (void)_setIcon
:(NSImage
*)icon
;
24 - (void)_setPID
:(pid_t
)pid
;
29 @implementation Process
32 - (id)initWithName
:(NSString
*)name version
:(NSString
*)version icon
:(NSImage
*)icon pid
:(pid_t
)pid
34 if ( self = [super init
] ) {
36 [self _setVersion
:version
];
43 - (id)initWithName
:(NSString
*)name version
:(NSString
*)version icon
:(NSImage
*)icon
45 return [self initWithName
:name version
:version icon
:icon pid
:0];
48 - (id)initWithName
:(NSString
*)name version
:(NSString
*)version
50 return [self initWithName
:name version
:version icon
:nil pid
:0];
55 // release local objects
64 // #############################################################################
66 // #############################################################################
68 - (id)copyWithZone
:(NSZone
*)zone
70 return [[Process allocWithZone
:zone
] initWithName
:_name version
:_version icon
:_icon pid
:_pid
];
74 // #############################################################################
76 // #############################################################################
78 - (id)initWithCoder
:(NSCoder
*)coder
80 if ( self = [super init
] )
82 [self _setName
:[coder decodeObject
]];
83 [self _setVersion
:[coder decodeObject
]];
84 //[self setIcon:[coder decodeObject]];
85 [coder decodeValueOfObjCType
:@encode(pid_t
) at
:&_pid
];
90 - (void)encodeWithCoder
:(NSCoder
*)coder
92 [coder encodeObject
:_name
];
93 [coder encodeObject
:_version
];
94 //[coder encodeObject:_icon];
95 [coder encodeValueOfObjCType
:@encode(pid_t
) at
:&_pid
];
99 #pragma mark NSObject Override
101 - (BOOL)isEqual
:(id)anObject
103 if ( [_name isEqualToString
:[(Process
*)anObject name
]] &&
104 [_version isEqualToString
:[(Process
*)anObject version
]] &&
105 _pid
== [(Process
*)anObject pid
] ) {
106 // they are the same process
112 #if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED
118 return [[NSString stringWithFormat
:@
"%@%@%u", _name
, _version
, _pid
] hash
];
122 - (BOOL)sameApplicationAs
:(id)anObject
124 if ( [_name isEqualToString
:[(Process
*)anObject name
]] &&
125 [_version isEqualToString
:[(Process
*)anObject version
]] ) {
126 // they are the same application
132 #pragma mark Detecting Emulation
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
)
141 if (sysctlbyname(name
, oldp
, oldlenp
, newp
, newlen
) == -1) {
142 fprintf(stderr
, "sysctlbyname_with_pid(0): sysctlbyname failed:"
143 "%s\n", strerror(errno
));
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
));
156 if (sysctl(mib
, len
, oldp
, oldlenp
, newp
, newlen
) == -1) {
157 fprintf(stderr
, "sysctlbyname_with_pid: sysctl failed:"
158 "%s\n", strerror(errno
));
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
)
169 size_t sz
= sizeof(ret
);
171 if (sysctlbyname_with_pid("sysctl.proc_native", pid
,
172 &ret
, &sz
, NULL
, 0) == -1) {
173 if (errno
== ENOENT
) {
176 fprintf(stderr
, "is_pid_native: sysctlbyname_with_pid failed:"
177 "%s\n", strerror(errno
));
186 BOOL isEmulated
= NO
;
187 #if defined(__i386__) || defined(__x86_64__)
188 if (is_pid_native(_pid
) == 0)
197 #pragma mark Accessors
204 - (NSString
*)version
220 - (void)_setName
:(NSString
*)name
223 name
= [NSString stringWithString
:@
"Unknown Process"];
230 - (void)_setVersion
:(NSString
*)version
233 version
= [NSString stringWithString
:@
"Unknown Version"];
240 - (void)_setIcon
:(NSImage
*)icon
247 [_icon setScalesWhenResized
:YES
];
248 [_icon setSize
:NSMakeSize(16,16)];
251 - (void)_setPID
:(pid_t
)pid