From 711fa70950e3930efd4030d18a5f8d394b08afd5 Mon Sep 17 00:00:00 2001 From: Filip Gawin Date: Sat, 25 Aug 2018 17:51:48 +0200 Subject: [PATCH] Remove unneeded dynamic alocation of SCMFile --- rwengine/src/engine/GameData.cpp | 6 ++-- rwengine/src/engine/GameData.hpp | 2 +- rwengine/src/engine/SaveGame.cpp | 2 +- rwengine/src/script/SCMFile.hpp | 4 +++ rwengine/src/script/ScriptFunctions.cpp | 2 +- rwengine/src/script/ScriptMachine.cpp | 30 +++++++++---------- rwengine/src/script/ScriptMachine.hpp | 6 ++-- rwengine/src/script/ScriptTypes.cpp | 2 +- .../src/script/modules/GTA3ModuleImpl.inl | 8 ++--- rwgame/RWGame.cpp | 5 ++-- rwgame/RWGame.hpp | 3 +- rwgame/states/DebugState.cpp | 2 +- 12 files changed, 38 insertions(+), 34 deletions(-) diff --git a/rwengine/src/engine/GameData.cpp b/rwengine/src/engine/GameData.cpp index b6d5779e..a913fe29 100644 --- a/rwengine/src/engine/GameData.cpp +++ b/rwengine/src/engine/GameData.cpp @@ -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; } diff --git a/rwengine/src/engine/GameData.hpp b/rwengine/src/engine/GameData.hpp index 2a8727fc..9f55d06b 100644 --- a/rwengine/src/engine/GameData.hpp +++ b/rwengine/src/engine/GameData.hpp @@ -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); diff --git a/rwengine/src/engine/SaveGame.cpp b/rwengine/src/engine/SaveGame.cpp index 4b8bcce1..d815aaf3 100644 --- a/rwengine/src/engine/SaveGame.cpp +++ b/rwengine/src/engine/SaveGame.cpp @@ -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) { diff --git a/rwengine/src/script/SCMFile.hpp b/rwengine/src/script/SCMFile.hpp index 92fa7875..ea443a30 100644 --- a/rwengine/src/script/SCMFile.hpp +++ b/rwengine/src/script/SCMFile.hpp @@ -22,6 +22,10 @@ public: delete[] _data; } + operator bool() { + return _data; + } + void loadFile(char* data, unsigned int size); SCMByte* data() const { diff --git a/rwengine/src/script/ScriptFunctions.cpp b/rwengine/src/script/ScriptFunctions.cpp index 1c747576..c3527b7b 100644 --- a/rwengine/src/script/ScriptFunctions.cpp +++ b/rwengine/src/script/ScriptFunctions.cpp @@ -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; diff --git a/rwengine/src/script/ScriptMachine.cpp b/rwengine/src/script/ScriptMachine.cpp index bd6fcb20..874d066b 100644 --- a/rwengine/src/script/ScriptMachine.cpp +++ b/rwengine/src/script/ScriptMachine.cpp @@ -36,7 +36,7 @@ void ScriptMachine::executeThread(SCMThread& t, int msPassed) { while (t.wakeCounter == 0) { auto pc = t.programCounter; - auto opcode = file->read(pc); + auto opcode = file.read(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(pc); + auto type_r = file.read(pc); auto type = static_cast(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(pc); + parameters.back().integer = file.read(pc); pc += sizeof(SCMByte); break; case TInt16: - parameters.back().integer = file->read(pc); + parameters.back().integer = file.read(pc); pc += sizeof(SCMByte) * 2; break; case TGlobal: { - auto v = file->read(pc); + auto v = file.read(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(pc); + auto v = file.read(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(pc); + parameters.back().integer = file.read(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(pc) / 16.f; + file.read(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()); } diff --git a/rwengine/src/script/ScriptMachine.hpp b/rwengine/src/script/ScriptMachine.hpp index 586520eb..91027d10 100644 --- a/rwengine/src/script/ScriptMachine.hpp +++ b/rwengine/src/script/ScriptMachine.hpp @@ -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; diff --git a/rwengine/src/script/ScriptTypes.cpp b/rwengine/src/script/ScriptTypes.cpp index 4ad43f75..af7ade52 100644 --- a/rwengine/src/script/ScriptTypes.cpp +++ b/rwengine/src/script/ScriptTypes.cpp @@ -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); } diff --git a/rwengine/src/script/modules/GTA3ModuleImpl.inl b/rwengine/src/script/modules/GTA3ModuleImpl.inl index 7fd35734..8855628e 100644 --- a/rwengine/src/script/modules/GTA3ModuleImpl.inl +++ b/rwengine/src/script/modules/GTA3ModuleImpl.inl @@ -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); } diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index 8c7b60d8..8aed7a41 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -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(&state, script.get(), &opcodes); - + vm = std::make_unique(&state, script, &opcodes); state.script = vm.get(); } else { log.error("Game", "Failed to load SCM: " + name); diff --git a/rwgame/RWGame.hpp b/rwgame/RWGame.hpp index e60865ff..6127f4f4 100644 --- a/rwgame/RWGame.hpp +++ b/rwgame/RWGame.hpp @@ -11,6 +11,7 @@ #include #include #include +#include