mirror of
https://github.com/scummvm/scummvm.git
synced 2026-06-20 05:45:29 +00:00
This not only simplifies all the use of _t types to the ScummVM equivalents, it gets rid of the int32 usage in favor of just int, solving common type clashes with the Amiga, for which int doesn't match int32
242 lines
8.2 KiB
C++
242 lines
8.2 KiB
C++
/* ScummVM - Graphic Adventure Engine
|
|
*
|
|
* ScummVM is the legal property of its developers, whose names
|
|
* are too numerous to list here. Please refer to the COPYRIGHT
|
|
* file distributed with this source distribution.
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*
|
|
*/
|
|
|
|
#include "ags/shared/script/cc_error.h"
|
|
#include "ags/engine/script/runtimescriptvalue.h"
|
|
#include "ags/engine/ac/dynobj/cc_dynamicobject.h"
|
|
#include "ags/engine/ac/statobj/staticobject.h"
|
|
#include "ags/shared/util/memory.h"
|
|
|
|
//include <string.h> // for memcpy()
|
|
|
|
namespace AGS3 {
|
|
|
|
using namespace AGS::Shared;
|
|
|
|
//
|
|
// NOTE to future optimizers: I am using 'this' ptr here to better
|
|
// distinguish Runtime Values.
|
|
//
|
|
|
|
// TODO: test again if stack entry really can hold an offset itself
|
|
|
|
// TODO: use endian-agnostic method to access global vars
|
|
|
|
uint8 RuntimeScriptValue::ReadByte() {
|
|
if (this->Type == kScValStackPtr || this->Type == kScValGlobalVar) {
|
|
if (RValue->Type == kScValData) {
|
|
return *(uint8 *)(RValue->GetPtrWithOffset() + this->IValue);
|
|
} else {
|
|
return RValue->IValue; // get RValue as int
|
|
}
|
|
} else if (this->Type == kScValStaticObject || this->Type == kScValStaticArray) {
|
|
return this->StcMgr->ReadInt8(this->Ptr, this->IValue);
|
|
} else if (this->Type == kScValDynamicObject) {
|
|
return this->DynMgr->ReadInt8(this->Ptr, this->IValue);
|
|
}
|
|
return *((uint8 *)this->GetPtrWithOffset());
|
|
}
|
|
|
|
int16 RuntimeScriptValue::ReadInt16() {
|
|
if (this->Type == kScValStackPtr) {
|
|
if (RValue->Type == kScValData) {
|
|
return *(int16 *)(RValue->GetPtrWithOffset() + this->IValue);
|
|
} else {
|
|
return RValue->IValue; // get RValue as int
|
|
}
|
|
} else if (this->Type == kScValGlobalVar) {
|
|
if (RValue->Type == kScValData) {
|
|
return Memory::ReadInt16LE(RValue->GetPtrWithOffset() + this->IValue);
|
|
} else {
|
|
return RValue->IValue; // get RValue as int
|
|
}
|
|
} else if (this->Type == kScValStaticObject || this->Type == kScValStaticArray) {
|
|
return this->StcMgr->ReadInt16(this->Ptr, this->IValue);
|
|
} else if (this->Type == kScValDynamicObject) {
|
|
return this->DynMgr->ReadInt16(this->Ptr, this->IValue);
|
|
}
|
|
return *((int16 *)this->GetPtrWithOffset());
|
|
}
|
|
|
|
int RuntimeScriptValue::ReadInt32() {
|
|
if (this->Type == kScValStackPtr) {
|
|
if (RValue->Type == kScValData) {
|
|
return *(int *)(RValue->GetPtrWithOffset() + this->IValue);
|
|
} else {
|
|
return RValue->IValue; // get RValue as int
|
|
}
|
|
} else if (this->Type == kScValGlobalVar) {
|
|
if (RValue->Type == kScValData) {
|
|
return Memory::ReadInt32LE(RValue->GetPtrWithOffset() + this->IValue);
|
|
} else {
|
|
return RValue->IValue; // get RValue as int
|
|
}
|
|
} else if (this->Type == kScValStaticObject || this->Type == kScValStaticArray) {
|
|
return this->StcMgr->ReadInt32(this->Ptr, this->IValue);
|
|
} else if (this->Type == kScValDynamicObject) {
|
|
return this->DynMgr->ReadInt32(this->Ptr, this->IValue);
|
|
}
|
|
return *((int *)this->GetPtrWithOffset());
|
|
}
|
|
|
|
bool RuntimeScriptValue::WriteByte(uint8 val) {
|
|
if (this->Type == kScValStackPtr || this->Type == kScValGlobalVar) {
|
|
if (RValue->Type == kScValData) {
|
|
*(uint8 *)(RValue->GetPtrWithOffset() + this->IValue) = val;
|
|
} else {
|
|
RValue->SetUInt8(val); // set RValue as int
|
|
}
|
|
} else if (this->Type == kScValStaticObject || this->Type == kScValStaticArray) {
|
|
this->StcMgr->WriteInt8(this->Ptr, this->IValue, val);
|
|
} else if (this->Type == kScValDynamicObject) {
|
|
this->DynMgr->WriteInt8(this->Ptr, this->IValue, val);
|
|
} else {
|
|
*((uint8 *)this->GetPtrWithOffset()) = val;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool RuntimeScriptValue::WriteInt16(int16 val) {
|
|
if (this->Type == kScValStackPtr) {
|
|
if (RValue->Type == kScValData) {
|
|
*(int16 *)(RValue->GetPtrWithOffset() + this->IValue) = val;
|
|
} else {
|
|
RValue->SetInt16(val); // set RValue as int
|
|
}
|
|
} else if (this->Type == kScValGlobalVar) {
|
|
if (RValue->Type == kScValData) {
|
|
Memory::WriteInt16LE(RValue->GetPtrWithOffset() + this->IValue, val);
|
|
} else {
|
|
RValue->SetInt16(val); // set RValue as int
|
|
}
|
|
} else if (this->Type == kScValStaticObject || this->Type == kScValStaticArray) {
|
|
this->StcMgr->WriteInt16(this->Ptr, this->IValue, val);
|
|
} else if (this->Type == kScValDynamicObject) {
|
|
this->DynMgr->WriteInt16(this->Ptr, this->IValue, val);
|
|
} else {
|
|
*((int16 *)this->GetPtrWithOffset()) = val;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
bool RuntimeScriptValue::WriteInt32(int val) {
|
|
if (this->Type == kScValStackPtr) {
|
|
if (RValue->Type == kScValData) {
|
|
*(int *)(RValue->GetPtrWithOffset() + this->IValue) = val;
|
|
} else {
|
|
RValue->SetInt32(val); // set RValue as int
|
|
}
|
|
} else if (this->Type == kScValGlobalVar) {
|
|
if (RValue->Type == kScValData) {
|
|
Memory::WriteInt32LE(RValue->GetPtrWithOffset() + this->IValue, val);
|
|
} else {
|
|
RValue->SetInt32(val); // set RValue as int
|
|
}
|
|
} else if (this->Type == kScValStaticObject || this->Type == kScValStaticArray) {
|
|
this->StcMgr->WriteInt32(this->Ptr, this->IValue, val);
|
|
} else if (this->Type == kScValDynamicObject) {
|
|
this->DynMgr->WriteInt32(this->Ptr, this->IValue, val);
|
|
} else {
|
|
*((int *)this->GetPtrWithOffset()) = val;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// Notice, that there are only two valid cases when a pointer may be written:
|
|
// when the destination is a stack entry or global variable of free type
|
|
// (not kScValData type).
|
|
// In any other case, only the numeric value (integer/float) will be written.
|
|
bool RuntimeScriptValue::WriteValue(const RuntimeScriptValue &rval) {
|
|
if (this->Type == kScValStackPtr) {
|
|
if (RValue->Type == kScValData) {
|
|
*(int *)(RValue->GetPtrWithOffset() + this->IValue) = rval.IValue;
|
|
} else {
|
|
// NOTE: we cannot just WriteValue here because when an integer
|
|
// is pushed to the stack, script assumes that it is always 4
|
|
// bytes and uses that size when calculating offsets to local
|
|
// variables;
|
|
// Therefore if pushed value is of integer type, we should rather
|
|
// act as WriteInt32 (for int8, int16 and int).
|
|
if (rval.Type == kScValInteger) {
|
|
RValue->SetInt32(rval.IValue);
|
|
} else {
|
|
*RValue = rval;
|
|
}
|
|
}
|
|
} else if (this->Type == kScValGlobalVar) {
|
|
if (RValue->Type == kScValData) {
|
|
Memory::WriteInt32LE(RValue->GetPtrWithOffset() + this->IValue, rval.IValue);
|
|
} else {
|
|
*RValue = rval;
|
|
}
|
|
} else if (this->Type == kScValStaticObject || this->Type == kScValStaticArray) {
|
|
this->StcMgr->WriteInt32(this->Ptr, this->IValue, rval.IValue);
|
|
} else if (this->Type == kScValDynamicObject) {
|
|
this->DynMgr->WriteInt32(this->Ptr, this->IValue, rval.IValue);
|
|
} else {
|
|
*((int *)this->GetPtrWithOffset()) = rval.IValue;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
RuntimeScriptValue &RuntimeScriptValue::DirectPtr() {
|
|
if (Type == kScValGlobalVar || Type == kScValStackPtr) {
|
|
int ival = IValue;
|
|
*this = *RValue;
|
|
IValue += ival;
|
|
}
|
|
|
|
if (Ptr) {
|
|
if (Type == kScValDynamicObject)
|
|
Ptr = const_cast<char *>(DynMgr->GetFieldPtr(Ptr, IValue));
|
|
else if (Type == kScValStaticObject)
|
|
Ptr = const_cast<char *>(StcMgr->GetFieldPtr(Ptr, IValue));
|
|
else
|
|
Ptr += IValue;
|
|
IValue = 0;
|
|
}
|
|
return *this;
|
|
}
|
|
|
|
RuntimeScriptValue &RuntimeScriptValue::DirectPtrObj() {
|
|
if (Type == kScValGlobalVar || Type == kScValStackPtr)
|
|
*this = *RValue;
|
|
return *this;
|
|
}
|
|
|
|
intptr_t RuntimeScriptValue::GetDirectPtr() const {
|
|
const RuntimeScriptValue *temp_val = this;
|
|
int ival = temp_val->IValue;
|
|
if (temp_val->Type == kScValGlobalVar || temp_val->Type == kScValStackPtr) {
|
|
temp_val = temp_val->RValue;
|
|
ival += temp_val->IValue;
|
|
}
|
|
if (temp_val->Type == kScValDynamicObject)
|
|
return (intptr_t)temp_val->DynMgr->GetFieldPtr(temp_val->Ptr, ival);
|
|
else if (temp_val->Type == kScValStaticObject)
|
|
return (intptr_t)temp_val->StcMgr->GetFieldPtr(temp_val->Ptr, ival);
|
|
else
|
|
return (intptr_t)(temp_val->Ptr + ival);
|
|
}
|
|
|
|
} // namespace AGS3
|