1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 15:02:34 +02: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); l.loadHandling(syspath, vehicleInfo);
} }
SCMFile* GameData::loadSCM(const std::string& path) { SCMFile GameData::loadSCM(const std::string& path) {
auto scm_h = index.openFileRaw(path); auto scm_h = index.openFileRaw(path);
SCMFile* scm = new SCMFile; SCMFile scm{};
scm->loadFile(scm_h.data.get(), scm_h.length); scm.loadFile(scm_h.data.get(), scm_h.length);
return scm; return scm;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -32,7 +32,7 @@ int ScriptArguments::getModel(unsigned int arg) const {
/// @todo verify this behaviour /// @todo verify this behaviour
if (id < 0) { if (id < 0) {
id = -id; id = -id;
const auto& model = getVM()->getFile()->getModels()[id]; const auto& model = getVM()->getFile().getModels()[id];
id = getWorld()->data->findModelObject(model); 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 @arg visible Boolean true/false
*/ */
void opcode_0363(const ScriptArguments& args, ScriptVec3 coord, const ScriptFloat radius, const ScriptModel model, const ScriptBoolean visible) { 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]; auto& modelName = models[-model];
// Attempt to find the closest object // 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 name0 = std::abs(model0);
int name1 = std::abs(model1); int name1 = std::abs(model1);
auto oldmodel = args.getVM()->getFile()->getModels()[name0]; auto oldmodel = args.getVM()->getFile().getModels()[name0];
auto newmodel = args.getVM()->getFile()->getModels()[name1]; auto newmodel = args.getVM()->getFile().getModels()[name1];
std::transform(newmodel.begin(), newmodel.end(), newmodel.begin(), ::tolower); std::transform(newmodel.begin(), newmodel.end(), newmodel.begin(), ::tolower);
std::transform(oldmodel.begin(), oldmodel.end(), oldmodel.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 @arg arg1
*/ */
void opcode_0417(const ScriptArguments& args, const ScriptInt 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); 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) { void RWGame::startScript(const std::string& name) {
script.reset(data.loadSCM(name)); script = data.loadSCM(name);
if (script) { if (script) {
vm = std::make_unique<ScriptMachine>(&state, script.get(), &opcodes); vm = std::make_unique<ScriptMachine>(&state, script, &opcodes);
state.script = vm.get(); state.script = vm.get();
} else { } else {
log.error("Game", "Failed to load SCM: " + name); log.error("Game", "Failed to load SCM: " + name);

View File

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

View File

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