1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 02:12:45 +01:00

Remove unneeded dynamic alocation of SCMFile

This commit is contained in:
Filip Gawin 2018-08-25 17:51:48 +02:00
parent d4061540d0
commit 711fa70950
12 changed files with 38 additions and 34 deletions

View File

@ -289,10 +289,10 @@ void GameData::loadHandling(const std::string& path) {
l.loadHandling(syspath, vehicleInfo);
}
SCMFile* GameData::loadSCM(const std::string& path) {
SCMFile GameData::loadSCM(const std::string& path) {
auto scm_h = index.openFileRaw(path);
SCMFile* scm = new SCMFile;
scm->loadFile(scm_h.data.get(), scm_h.length);
SCMFile scm{};
scm.loadFile(scm_h.data.get(), scm_h.length);
return scm;
}

View File

@ -104,7 +104,7 @@ public:
void loadHandling(const std::string& path);
SCMFile* loadSCM(const std::string& path);
SCMFile loadSCM(const std::string& path);
void loadGXT(const std::string& name);

View File

@ -560,7 +560,7 @@ bool SaveGame::loadGame(GameState& state, const std::string& file) {
BlockDword scriptVarCount;
READ_SIZE(scriptVarCount)
RW_ASSERT(scriptVarCount == state.script->getFile()->getGlobalsSize());
RW_ASSERT(scriptVarCount == state.script->getFile().getGlobalsSize());
if (fread(state.script->getGlobals(), sizeof(SCMByte), scriptVarCount,
loadFile) != scriptVarCount) {

View File

@ -22,6 +22,10 @@ public:
delete[] _data;
}
operator bool() {
return _data;
}
void loadFile(char* data, unsigned int size);
SCMByte* data() const {

View File

@ -35,7 +35,7 @@ const char* script::getBlipSprite(ScriptRadarSprite sprite) {
ScriptModel script::getModel(const ScriptArguments& args, ScriptModel model) {
if (model < 0) {
/// @todo verify that this is how the game uses negative models
const auto& m = args.getVM()->getFile()->getModels()[-model];
const auto& m = args.getVM()->getFile().getModels()[-model];
return args.getWorld()->data->findModelObject(m);
}
return model;

View File

@ -36,7 +36,7 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
while (t.wakeCounter == 0) {
auto pc = t.programCounter;
auto opcode = file->read<SCMOpcode>(pc);
auto opcode = file.read<SCMOpcode>(pc);
bool isNegatedConditional = ((opcode & SCM_NEGATE_CONDITIONAL_MASK) ==
SCM_NEGATE_CONDITIONAL_MASK);
@ -56,7 +56,7 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
auto requiredParams = std::abs(code.arguments);
for (int p = 0; p < requiredParams || hasExtraParameters; ++p) {
auto type_r = file->read<SCMByte>(pc);
auto type_r = file.read<SCMByte>(pc);
auto type = static_cast<SCMType>(type_r);
if (type_r > 42) {
@ -72,27 +72,27 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
hasExtraParameters = false;
break;
case TInt8:
parameters.back().integer = file->read<std::int8_t>(pc);
parameters.back().integer = file.read<std::int8_t>(pc);
pc += sizeof(SCMByte);
break;
case TInt16:
parameters.back().integer = file->read<std::int16_t>(pc);
parameters.back().integer = file.read<std::int16_t>(pc);
pc += sizeof(SCMByte) * 2;
break;
case TGlobal: {
auto v = file->read<std::uint16_t>(pc);
auto v = file.read<std::uint16_t>(pc);
parameters.back().globalPtr =
globalData.data() + v; //* SCM_VARIABLE_SIZE;
if (v >= file->getGlobalsSize()) {
if (v >= file.getGlobalsSize()) {
state->world->logger->error(
"SCM", "Global Out of bounds! " +
std::to_string(v) + " " +
std::to_string(file->getGlobalsSize()));
std::to_string(file.getGlobalsSize()));
}
pc += sizeof(SCMByte) * 2;
} break;
case TLocal: {
auto v = file->read<std::uint16_t>(pc);
auto v = file.read<std::uint16_t>(pc);
parameters.back().globalPtr =
t.locals.data() + v * SCM_VARIABLE_SIZE;
if (v >= SCM_THREAD_LOCAL_SIZE) {
@ -102,17 +102,17 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
pc += sizeof(SCMByte) * 2;
} break;
case TInt32:
parameters.back().integer = file->read<std::int32_t>(pc);
parameters.back().integer = file.read<std::int32_t>(pc);
pc += sizeof(SCMByte) * 4;
break;
case TString:
std::copy(file->data() + pc, file->data() + pc + 8,
std::copy(file.data() + pc, file.data() + pc + 8,
parameters.back().string);
pc += sizeof(SCMByte) * 8;
break;
case TFloat16:
parameters.back().real =
file->read<std::int16_t>(pc) / 16.f;
file.read<std::int16_t>(pc) / 16.f;
pc += sizeof(SCMByte) * 2;
break;
default:
@ -185,7 +185,7 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) {
}
}
ScriptMachine::ScriptMachine(GameState* _state, SCMFile* file,
ScriptMachine::ScriptMachine(GameState* _state, SCMFile& file,
ScriptModule* ops)
: file(file)
, module(ops)
@ -193,10 +193,10 @@ ScriptMachine::ScriptMachine(GameState* _state, SCMFile* file,
, debugFlag(false)
, randomNumberGen(std::random_device()()) {
// Copy globals
auto size = file->getGlobalsSize();
auto size = file.getGlobalsSize();
globalData.resize(size);
auto offset = file->getGlobalSection();
std::copy(file->data() + offset, file->data() + offset + size,
auto offset = file.getGlobalSection();
std::copy(file.data() + offset, file.data() + offset + size,
globalData.begin());
}

View File

@ -129,10 +129,10 @@ struct SCMThread {
*/
class ScriptMachine {
public:
ScriptMachine(GameState* state, SCMFile* file, ScriptModule* ops);
ScriptMachine(GameState* state, SCMFile& file, ScriptModule* ops);
~ScriptMachine() = default;
SCMFile* getFile() const {
SCMFile& getFile() const {
return file;
}
@ -185,7 +185,7 @@ public:
void execute(float dt);
private:
SCMFile* file = nullptr;
SCMFile& file;
ScriptModule* module = nullptr;
GameState* state = nullptr;
bool debugFlag;

View File

@ -32,7 +32,7 @@ int ScriptArguments::getModel(unsigned int arg) const {
/// @todo verify this behaviour
if (id < 0) {
id = -id;
const auto& model = getVM()->getFile()->getModels()[id];
const auto& model = getVM()->getFile().getModels()[id];
id = getWorld()->data->findModelObject(model);
}

View File

@ -9611,7 +9611,7 @@ void opcode_0362(const ScriptArguments& args, const ScriptCharacter character,
@arg visible Boolean true/false
*/
void opcode_0363(const ScriptArguments& args, ScriptVec3 coord, const ScriptFloat radius, const ScriptModel model, const ScriptBoolean visible) {
auto& models = args.getVM()->getFile()->getModels();
auto& models = args.getVM()->getFile().getModels();
auto& modelName = models[-model];
// Attempt to find the closest object
@ -10874,8 +10874,8 @@ void opcode_03b6(const ScriptArguments& args, ScriptVec3 coord, const ScriptFloa
int name0 = std::abs(model0);
int name1 = std::abs(model1);
auto oldmodel = args.getVM()->getFile()->getModels()[name0];
auto newmodel = args.getVM()->getFile()->getModels()[name1];
auto oldmodel = args.getVM()->getFile().getModels()[name0];
auto newmodel = args.getVM()->getFile().getModels()[name1];
std::transform(newmodel.begin(), newmodel.end(), newmodel.begin(), ::tolower);
std::transform(oldmodel.begin(), oldmodel.end(), oldmodel.begin(), ::tolower);
@ -12030,7 +12030,7 @@ void opcode_0415(const ScriptArguments& args, const ScriptVehicle vehicle, const
@arg arg1
*/
void opcode_0417(const ScriptArguments& args, const ScriptInt arg1) {
auto offset = args.getVM()->getFile()->getMissionOffsets()[arg1];
auto offset = args.getVM()->getFile().getMissionOffsets()[arg1];
args.getVM()->startThread(offset, true);
}

View File

@ -152,10 +152,9 @@ void RWGame::loadGame(const std::string& savename) {
}
void RWGame::startScript(const std::string& name) {
script.reset(data.loadSCM(name));
script = data.loadSCM(name);
if (script) {
vm = std::make_unique<ScriptMachine>(&state, script.get(), &opcodes);
vm = std::make_unique<ScriptMachine>(&state, script, &opcodes);
state.script = vm.get();
} else {
log.error("Game", "Failed to load SCM: " + name);

View File

@ -11,6 +11,7 @@
#include <engine/GameWorld.hpp>
#include <render/DebugDraw.hpp>
#include <render/GameRenderer.hpp>
#include <script/SCMFile.hpp>
#include <script/ScriptMachine.hpp>
#include <script/modules/GTA3Module.hpp>
#include "game.hpp"
@ -29,7 +30,7 @@ class RWGame final : public GameBase {
GTA3Module opcodes;
std::unique_ptr<ScriptMachine> vm;
std::unique_ptr<SCMFile> script;
SCMFile script;
bool inFocus = true;
ViewCamera currentCam;

View File

@ -319,7 +319,7 @@ std::shared_ptr<Menu> DebugState::createMissionsMenu() {
if (vm) {
std::list<SCMThread>& threads = vm->getThreads();
const auto& offsets = vm->getFile()->getMissionOffsets();
const auto& offsets = vm->getFile().getMissionOffsets();
RW_ASSERT(!offsets.empty());