2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
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)
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.
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.
24 @interface Variable ( PrivateAPI
)
26 - (void)_setType
:(TCVariableType
)type
;
27 - (void)_setIntegerSign
:(TCIntegerSign
)sign
;
32 @implementation Variable
37 return [self initWithType
:TCInt32 integerSign
:TCSigned
];
40 - (id)initWithType
:(TCVariableType
)type
42 return [self initWithType
:type integerSign
:TCSigned
];
45 - (id)initWithType
:(TCVariableType
)type integerSign
:(TCIntegerSign
)sign
// DESIGNATED
47 if ( self = [super init
] ) {
51 [self _setIntegerSign
:sign
];
66 // #############################################################################
68 // #############################################################################
70 - (id)initWithCoder
:(NSCoder
*)coder
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
];
84 - (void)encodeWithCoder
:(NSCoder
*)coder
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
];
96 // #############################################################################
97 #pragma mark Accessors
98 // #############################################################################
100 - (TCVariableType
)type
105 - (void)_setType
:(TCVariableType
)type
108 // set the size of the value
111 case TCDouble
: _size
= 8;
114 case TCFloat
: _size
= 4;
116 case TCInt16
: _size
= 2;
118 case TCInt8
: _size
= 1;
123 _value
= calloc( 1, _size
);
127 - (TCIntegerSign
)integerSign
132 - (void)_setIntegerSign
:(TCIntegerSign
)sign
138 - (NSString
*)typeString
141 case TCDouble
: return @
"Double";
142 case TCFloat
: return @
"Float";
143 case TCString
: return @
"ASCII String";
145 if ( _integerSign
== TCUnsigned
) {
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";
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";
170 - (void)setAddress
:(TCAddress
)addr
176 - (NSString
*)addressString
178 return [NSString stringWithFormat
:@
"%0.8X", _address
];
181 - (BOOL)setAddressString
:(NSString
*)string
183 NSScanner
*scanner
= [NSScanner scannerWithString
:string
];
186 if ( [scanner scanHexInt
:(unsigned *)(&address
)] ) {
187 [self setAddress
:address
];
194 - (void const *)value
199 - (void)setValue
:(void const *)value
202 _value
= malloc( _size
);
206 memcpy( _value
, value
, _size
);
209 - (void)setValue
:(void const *)value size
:(unsigned)size
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
);
221 _size
= MIN( _size
, size
);
223 [self setValue
:value
];
227 - (NSString
*)stringValue
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
]];
234 if ( _integerSign
== TCUnsigned
) {
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
]];
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
]];
253 - (BOOL)setStringValue
:(NSString
*)string
255 NSScanner
*scanner
= [NSScanner scannerWithString
:string
];
257 // invalid until proven valid
264 if ( [scanner scanLongLong
:(long long *)(&value
)] ) {
265 [self setValue
:&value
];
272 if ( [scanner scanInt
:(int *)(&value
)] ) {
273 [self setValue
:&value
];
281 if ( [scanner scanInt
:&integer
] ) {
283 [self setValue
:&value
];
291 if ( [scanner scanInt
:&integer
] ) {
293 [self setValue
:&value
];
299 char *str
= (char *)[string lossyCString
];
300 unsigned len
= strlen( str
);
301 [self setValue
:str size
:len
];
307 if ( [scanner scanFloat
:&value
] ) {
308 [self setValue
:&value
];
315 if ( [scanner scanDouble
:&value
] ) {
316 [self setValue
:&value
];
321 return [self isValueValid
];
325 - (unsigned)valueSize
332 return _isValueValid
;
341 - (void)setEnabled
:(BOOL)enabled
352 - (void)setTag
:(int)tag