]> Dogcows Code - chaz/thecheat/blob - Variable.m
The Cheat 1.2.3
[chaz/thecheat] / Variable.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 "Variable.h"
22
23
24 @interface Variable ( PrivateAPI )
25
26 - (void)_setType:(TCVariableType)type;
27 - (void)_setIntegerSign:(TCIntegerSign)sign;
28
29 @end
30
31
32 @implementation Variable
33
34
35 - (id)init
36 {
37 return [self initWithType:TCInt32 integerSign:TCSigned];
38 }
39
40 - (id)initWithType:(TCVariableType)type
41 {
42 return [self initWithType:type integerSign:TCSigned];
43 }
44
45 - (id)initWithType:(TCVariableType)type integerSign:(TCIntegerSign)sign // DESIGNATED
46 {
47 if ( self = [super init] ) {
48 _isValueValid = YES;
49 _enabled = YES;
50 [self _setType:type];
51 [self _setIntegerSign:sign];
52 }
53 return self;
54 }
55
56
57 - (void)dealloc
58 {
59 if ( _value ) {
60 free( _value );
61 }
62 [super dealloc];
63 }
64
65
66 // #############################################################################
67 #pragma mark NSCoding
68 // #############################################################################
69
70 - (id)initWithCoder:(NSCoder *)coder
71 {
72 if ( self = [super init] ) {
73 [coder decodeValueOfObjCType:@encode(TCVariableType) at:&_type];
74 [coder decodeValueOfObjCType:@encode(TCIntegerSign) at:&_integerSign];
75 [coder decodeValueOfObjCType:@encode(TCAddress) at:&_address];
76 [self setValue:[coder decodeBytesWithReturnedLength:&_size]];
77 [coder decodeValueOfObjCType:@encode(BOOL) at:&_isValueValid];
78 [coder decodeValueOfObjCType:@encode(BOOL) at:&_enabled];
79 [coder decodeValueOfObjCType:@encode(int) at:&_tag];
80 }
81 return self;
82 }
83
84 - (void)encodeWithCoder:(NSCoder *)coder
85 {
86 [coder encodeValueOfObjCType:@encode(TCVariableType) at:&_type];
87 [coder encodeValueOfObjCType:@encode(TCIntegerSign) at:&_integerSign];
88 [coder encodeValueOfObjCType:@encode(TCAddress) at:&_address];
89 [coder encodeBytes:_value length:_size];
90 [coder encodeValueOfObjCType:@encode(BOOL) at:&_isValueValid];
91 [coder encodeValueOfObjCType:@encode(BOOL) at:&_enabled];
92 [coder encodeValueOfObjCType:@encode(int) at:&_tag];
93 }
94
95
96 // #############################################################################
97 #pragma mark Accessors
98 // #############################################################################
99
100 - (TCVariableType)type
101 {
102 return _type;
103 }
104
105 - (void)_setType:(TCVariableType)type
106 {
107 _type = type;
108 // set the size of the value
109 switch ( _type ) {
110 case TCInt64:
111 case TCDouble: _size = 8;
112 break;
113 case TCInt32:
114 case TCFloat: _size = 4;
115 break;
116 case TCInt16: _size = 2;
117 break;
118 case TCInt8: _size = 1;
119 break;
120 }
121
122 if ( !_value ) {
123 _value = calloc( 1, _size );
124 }
125 }
126
127 - (TCIntegerSign)integerSign
128 {
129 return _integerSign;
130 }
131
132 - (void)_setIntegerSign:(TCIntegerSign)sign
133 {
134 _integerSign = sign;
135 }
136
137
138 - (NSString *)typeString
139 {
140 switch ( _type ) {
141 case TCDouble: return @"Double";
142 case TCFloat: return @"Float";
143 case TCString: return @"ASCII String";
144 }
145 if ( _integerSign == TCUnsigned ) {
146 switch ( _type ) {
147 case TCInt64: return @"64-bit Unsigned Integer";
148 case TCInt32: return @"32-bit Unsigned Integer";
149 case TCInt16: return @"16-bit Unsigned Integer";
150 case TCInt8: return @"08-bit Unsigned Integer";
151 }
152 }
153 else {
154 switch ( _type ) {
155 case TCInt64: return @"64-bit Integer";
156 case TCInt32: return @"32-bit Integer";
157 case TCInt16: return @"16-bit Integer";
158 case TCInt8: return @"08-bit Integer";
159 }
160 }
161 return @"";
162 }
163
164
165 - (TCAddress)address
166 {
167 return _address;
168 }
169
170 - (void)setAddress:(TCAddress)addr
171 {
172 _address = addr;
173 }
174
175
176 - (NSString *)addressString
177 {
178 return [NSString stringWithFormat:@"%0.8X", _address];
179 }
180
181 - (BOOL)setAddressString:(NSString *)string
182 {
183 NSScanner *scanner = [NSScanner scannerWithString:string];
184 TCAddress address;
185
186 if ( [scanner scanHexInt:(unsigned *)(&address)] ) {
187 [self setAddress:address];
188 return YES;
189 }
190 return NO;
191 }
192
193
194 - (void const *)value
195 {
196 return _value;
197 }
198
199 - (void)setValue:(void const *)value
200 {
201 if ( !_value ) {
202 _value = malloc( _size );
203 }
204
205 _isValueValid = YES;
206 memcpy( _value, value, _size );
207 }
208
209 - (void)setValue:(void const *)value size:(unsigned)size
210 {
211 // make sure the size doesn't exceed the maximum
212 size = MIN( size, TC_MAX_VAR_SIZE );
213 // only string variables can have the value size changed
214 if ( (_type == TCString) && (_size != size) && _value ) {
215 void *newValue = realloc( _value, size );
216 if ( newValue ) {
217 _value = newValue;
218 _size = size;
219 }
220 }
221 _size = MIN( _size, size );
222
223 [self setValue:value];
224 }
225
226
227 - (NSString *)stringValue
228 {
229 switch ( _type ) {
230 case TCDouble: return [NSString stringWithFormat:@"%.1lf", *(double *)[self value]];
231 case TCFloat: return [NSString stringWithFormat:@"%.1f", *(float *)[self value]];
232 case TCString: return [NSString stringWithCString:[self value] length:[self valueSize]];
233 }
234 if ( _integerSign == TCUnsigned ) {
235 switch ( _type ) {
236 case TCInt64: return [NSString stringWithFormat:@"%llu", *(UInt64 *)[self value]];
237 case TCInt32: return [NSString stringWithFormat:@"%u", *(UInt32 *)[self value]];
238 case TCInt16: return [NSString stringWithFormat:@"%u", *(UInt16 *)[self value]];
239 case TCInt8: return [NSString stringWithFormat:@"%u", *(UInt8 *)[self value]];
240 }
241 }
242 else {
243 switch ( _type ) {
244 case TCInt64: return [NSString stringWithFormat:@"%lli", *(SInt64 *)[self value]];
245 case TCInt32: return [NSString stringWithFormat:@"%i", *(SInt32 *)[self value]];
246 case TCInt16: return [NSString stringWithFormat:@"%i", *(SInt16 *)[self value]];
247 case TCInt8: return [NSString stringWithFormat:@"%i", *(SInt8 *)[self value]];
248 }
249 }
250 return @"foobar";
251 }
252
253 - (BOOL)setStringValue:(NSString *)string
254 {
255 NSScanner *scanner = [NSScanner scannerWithString:string];
256
257 // invalid until proven valid
258 _isValueValid = NO;
259
260 switch ( _type ) {
261 case TCInt64:
262 {
263 SInt64 value;
264 if ( [scanner scanLongLong:(long long *)(&value)] ) {
265 [self setValue:&value];
266 }
267 break;
268 }
269 case TCInt32:
270 {
271 SInt32 value;
272 if ( [scanner scanInt:(int *)(&value)] ) {
273 [self setValue:&value];
274 }
275 break;
276 }
277 case TCInt16:
278 {
279 int integer;
280 SInt16 value;
281 if ( [scanner scanInt:&integer] ) {
282 value = integer;
283 [self setValue:&value];
284 }
285 break;
286 }
287 case TCInt8:
288 {
289 int integer;
290 SInt8 value;
291 if ( [scanner scanInt:&integer] ) {
292 value = integer;
293 [self setValue:&value];
294 }
295 break;
296 }
297 case TCString:
298 {
299 char *str = (char *)[string lossyCString];
300 unsigned len = strlen( str );
301 [self setValue:str size:len];
302 break;
303 }
304 case TCFloat:
305 {
306 float value;
307 if ( [scanner scanFloat:&value] ) {
308 [self setValue:&value];
309 }
310 break;
311 }
312 case TCDouble:
313 {
314 double value;
315 if ( [scanner scanDouble:&value] ) {
316 [self setValue:&value];
317 }
318 break;
319 }
320 }
321 return [self isValueValid];
322 }
323
324
325 - (unsigned)valueSize
326 {
327 return _size;
328 }
329
330 - (BOOL)isValueValid
331 {
332 return _isValueValid;
333 }
334
335
336 - (BOOL)isEnabled
337 {
338 return _enabled;
339 }
340
341 - (void)setEnabled:(BOOL)enabled
342 {
343 _enabled = enabled;
344 }
345
346
347 - (int)tag
348 {
349 return _tag;
350 }
351
352 - (void)setTag:(int)tag
353 {
354 _tag = tag;
355 }
356
357
358 @end
This page took 0.049414 seconds and 4 git commands to generate.