Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENGINES: Avoid using static objects with destructors #3314

Merged
merged 4 commits into from Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion engines/cine/cine.cpp
Expand Up @@ -126,6 +126,7 @@ Common::Error CineEngine::run() {

delete renderer;
delete[] collisionPage;
delete _scriptInfo;
} while (_restartRequested);

delete g_sound;
Expand Down Expand Up @@ -176,7 +177,7 @@ void CineEngine::initialize() {
Common::fill(g_cine->_zoneQuery.begin(), g_cine->_zoneQuery.end(), 0);

setDefaultGameSpeed();
setupOpcodes();
_scriptInfo = setupOpcodes();

initLanguage(getLanguage());

Expand Down
1 change: 1 addition & 0 deletions engines/cine/cine.h
Expand Up @@ -184,6 +184,7 @@ class CineEngine : public Engine {
*/
ScriptVars _globalVars;
RawScriptArray _scriptTable; ///< Table of script bytecode
FWScriptInfo *_scriptInfo;

Common::Array<int16> _zoneData;
Common::Array<uint16> _zoneQuery; ///< Only exists in Operation Stealth
Expand Down
2 changes: 1 addition & 1 deletion engines/cine/prc.cpp
Expand Up @@ -85,7 +85,7 @@ bool loadPrc(const char *pPrcName) {
uint16 size = g_cine->_scriptTable[i]->_size;
// TODO: delete the test?
if (size) {
g_cine->_scriptTable[i]->setData(*scriptInfo, scriptPtr);
g_cine->_scriptTable[i]->setData(*g_cine->_scriptInfo, scriptPtr);
scriptPtr += size;
}
}
Expand Down
2 changes: 1 addition & 1 deletion engines/cine/rel.cpp
Expand Up @@ -63,7 +63,7 @@ void loadRel(char *pRelName) {
size = g_cine->_relTable[i]->_size;
// TODO: delete the test?
if (size) {
g_cine->_relTable[i]->setData(*scriptInfo, ptr);
g_cine->_relTable[i]->setData(*g_cine->_scriptInfo, ptr);
ptr += size;
}
}
Expand Down
4 changes: 2 additions & 2 deletions engines/cine/saveload.cpp
Expand Up @@ -210,11 +210,11 @@ void loadScriptFromSave(Common::SeekableReadStream &fHandle, bool isGlobal) {
// original code loaded everything into globalScripts, this should be
// the correct behavior
if (isGlobal) {
ScriptPtr tmp(scriptInfo->create(*g_cine->_scriptTable[idx], idx, labels, localVars, compare, pos));
ScriptPtr tmp(g_cine->_scriptInfo->create(*g_cine->_scriptTable[idx], idx, labels, localVars, compare, pos));
assert(tmp);
g_cine->_globalScripts.push_back(tmp);
} else {
ScriptPtr tmp(scriptInfo->create(*g_cine->_relTable[idx], idx, labels, localVars, compare, pos));
ScriptPtr tmp(g_cine->_scriptInfo->create(*g_cine->_relTable[idx], idx, labels, localVars, compare, pos));
assert(tmp);
g_cine->_objectScripts.push_back(tmp);
}
Expand Down
4 changes: 1 addition & 3 deletions engines/cine/script.h
Expand Up @@ -367,9 +367,7 @@ typedef Common::Array<RawObjectScriptPtr> RawObjectScriptArray;

#define NUM_MAX_SCRIPT 50

extern FWScriptInfo *scriptInfo;

void setupOpcodes();
FWScriptInfo *setupOpcodes();

void decompileScript(const byte *scriptPtr, uint16 scriptSize, uint16 scriptIdx);
void dumpScript(char *dumpName);
Expand Down
12 changes: 4 additions & 8 deletions engines/cine/script_fw.cpp
Expand Up @@ -206,20 +206,16 @@ void FWScript::setupTable() {
FWScript::_numOpcodes = ARRAYSIZE(opcodeTable);
}

FWScriptInfo *scriptInfo; ///< Script factory

/**
* @todo replace with script subsystem
*/
void setupOpcodes() {
static FWScriptInfo fw;
static OSScriptInfo os;
FWScriptInfo *setupOpcodes() {
if (g_cine->getGameType() == Cine::GType_FW) {
FWScript::setupTable();
scriptInfo = &fw;
return new FWScriptInfo();
} else {
OSScript::setupTable();
scriptInfo = &os;
return new OSScriptInfo();
}
}

Expand Down Expand Up @@ -2015,7 +2011,7 @@ int FWScript::o1_unloadMask5() {
//-----------------------------------------------------------------------

void addScriptToGlobalScripts(uint16 idx) {
ScriptPtr tmp(scriptInfo->create(*g_cine->_scriptTable[idx], idx));
ScriptPtr tmp(g_cine->_scriptInfo->create(*g_cine->_scriptTable[idx], idx));
assert(tmp);
g_cine->_globalScripts.push_back(tmp);
}
Expand Down
2 changes: 1 addition & 1 deletion engines/cine/various.cpp
Expand Up @@ -177,7 +177,7 @@ uint safeControlAccessMinMs() {
}

void runObjectScript(int16 entryIdx) {
ScriptPtr tmp(scriptInfo->create(*g_cine->_relTable[entryIdx], entryIdx));
ScriptPtr tmp(g_cine->_scriptInfo->create(*g_cine->_relTable[entryIdx], entryIdx));
assert(tmp);
g_cine->_objectScripts.push_back(tmp);
}
Expand Down
8 changes: 4 additions & 4 deletions engines/illusions/bbdou/illusions_bbdou.cpp
Expand Up @@ -668,14 +668,14 @@ void IllusionsEngine_BBDOU::reset() {

void IllusionsEngine_BBDOU::loadSavegameFromScript(int16 slotNum, uint32 callingThreadId) {
// NOTE Just loads the savegame, doesn't activate it yet
const char *fileName = getSavegameFilename(_savegameSlotNum);
_loadGameResult = loadgame(fileName);
Common::String fileName = getSavegameFilename(_targetName, _savegameSlotNum);
_loadGameResult = loadgame(fileName.c_str());
}

void IllusionsEngine_BBDOU::saveSavegameFromScript(int16 slotNum, uint32 callingThreadId) {
// TODO
// const char *fileName = getSavegameFilename(slotNum);
_saveGameResult = false;//savegame(fileName, _savegameDescription.c_str());
// Common::String fileName = getSavegameFilename(_targetName, slotNum);
_saveGameResult = false;//savegame(fileName.c_str(), _savegameDescription.c_str());
}

void IllusionsEngine_BBDOU::activateSavegame(uint32 callingThreadId) {
Expand Down
8 changes: 4 additions & 4 deletions engines/illusions/duckman/illusions_duckman.cpp
Expand Up @@ -1278,17 +1278,17 @@ bool IllusionsEngine_Duckman::loadSavegameFromScript(int16 slotNum, uint32 calli
return false; // TODO need to handle reset from new game (without exising savegame).
}

const char *fileName = getSavegameFilename(_savegameSlotNum);
bool success = loadgame(fileName);
Common::String fileName = getSavegameFilename(_targetName, _savegameSlotNum);
bool success = loadgame(fileName.c_str());
if (success)
activateSavegame(callingThreadId);
_gameState->deleteReadStream();
return success;
}

bool IllusionsEngine_Duckman::saveSavegameFromScript(int16 slotNum, uint32 callingThreadId) {
const char *fileName = getSavegameFilename(_savegameSlotNum);
return savegame(fileName, _savegameDescription.c_str());
Common::String fileName = getSavegameFilename(_targetName, _savegameSlotNum);
return savegame(fileName.c_str(), _savegameDescription.c_str());
}

void IllusionsEngine_Duckman::activateSavegame(uint32 callingThreadId) {
Expand Down
1 change: 0 additions & 1 deletion engines/illusions/illusions.h
Expand Up @@ -229,7 +229,6 @@ class IllusionsEngine : public Engine {
Common::Error removeGameState(int slot);
bool savegame(const char *filename, const char *description);
bool loadgame(const char *filename);
const char *getSavegameFilename(int num);
bool existsSavegame(int num);
static Common::String getSavegameFilename(const Common::String &target, int num);
static kReadSaveHeaderError readSaveHeader(Common::SeekableReadStream *in, SaveHeader &header, bool skipThumbnail = true);
Expand Down
16 changes: 5 additions & 11 deletions engines/illusions/saveload.cpp
Expand Up @@ -126,34 +126,28 @@ bool IllusionsEngine::loadgame(const char *filename) {

Common::Error IllusionsEngine::loadGameState(int slot) {
_resumeFromSavegameRequested = false;
const char *fileName = getSavegameFilename(slot);
if (!loadgame(fileName))
Common::String fileName = getSavegameFilename(_targetName, slot);
if (!loadgame(fileName.c_str()))
return Common::kReadingFailed;
_resumeFromSavegameRequested = true;
_savegameSlotNum = slot;
return Common::kNoError;
}

Common::Error IllusionsEngine::saveGameState(int slot, const Common::String &description, bool isAutosave) {
const char *fileName = getSavegameFilename(slot);
if (!savegame(fileName, description.c_str()))
Common::String fileName = getSavegameFilename(_targetName, slot);
if (!savegame(fileName.c_str(), description.c_str()))
return Common::kWritingFailed;
return Common::kNoError;
}

Common::Error IllusionsEngine::removeGameState(int slot) {
Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
Common::String filename = Illusions::IllusionsEngine::getSavegameFilename(_targetName, slot);
Common::String filename = getSavegameFilename(_targetName, slot);
saveFileMan->removeSavefile(filename.c_str());
return Common::kNoError;
}

const char *IllusionsEngine::getSavegameFilename(int num) {
static Common::String filename;
filename = getSavegameFilename(_targetName, num);
return filename.c_str();
}

Common::String IllusionsEngine::getSavegameFilename(const Common::String &target, int num) {
assert(num >= 0 && num <= 999);
return Common::String::format("%s.%03d", target.c_str(), num);
Expand Down
8 changes: 2 additions & 6 deletions engines/lab/interface.cpp
Expand Up @@ -191,9 +191,7 @@ void Interface::mayShowCrumbIndicator() {
return;

if (_vm->_droppingCrumbs && _vm->isMainDisplay()) {
static byte *imgData = new byte[CRUMBSWIDTH * CRUMBSHEIGHT];
memcpy(imgData, dropCrumbsImageData, CRUMBSWIDTH * CRUMBSHEIGHT);
static Image dropCrumbsImage(CRUMBSWIDTH, CRUMBSHEIGHT, imgData, _vm);
Image dropCrumbsImage(CRUMBSWIDTH, CRUMBSHEIGHT, dropCrumbsImageData, _vm, false);

dropCrumbsImage.drawMaskImage(612, 4);
}
Expand Down Expand Up @@ -231,9 +229,7 @@ void Interface::mayShowCrumbIndicatorOff() {
return;

if (_vm->isMainDisplay()) {
static byte *imgData = new byte[CRUMBSWIDTH * CRUMBSHEIGHT];
memcpy(imgData, dropCrumbsOffImageData, CRUMBSWIDTH * CRUMBSHEIGHT);
static Image dropCrumbsOffImage(CRUMBSWIDTH, CRUMBSHEIGHT, imgData, _vm);
Image dropCrumbsOffImage(CRUMBSWIDTH, CRUMBSHEIGHT, dropCrumbsOffImageData, _vm, false);

dropCrumbsOffImage.drawMaskImage(612, 4);
}
Expand Down
6 changes: 3 additions & 3 deletions engines/supernova/supernova1/state.cpp
Expand Up @@ -405,7 +405,7 @@ void GameManager1::corridorOnEntrance() {
}

void GameManager1::telomat(int nr) {
static Common::String name[8] = {
static const char *const name[8] = {
"DR. ALAB HANSI",
"ALAB HANSI",
"SAVAL LUN",
Expand All @@ -416,7 +416,7 @@ void GameManager1::telomat(int nr) {
"x"
};

static Common::String name2[4] = {
static const char *const name2[4] = {
"Alab Hansi",
"Saval Lun",
"Ugnul Tschabb",
Expand Down Expand Up @@ -506,7 +506,7 @@ void GameManager1::telomat(int nr) {
_vm->removeMessage();
if (_state._nameSeen[nr]) {
Common::String string = _vm->getGameString(kStringTelomat2);
_vm->setGameString(kStringPlaceholder1, Common::String::format(string.c_str(), name2[nr].c_str()));
_vm->setGameString(kStringPlaceholder1, Common::String::format(string.c_str(), name2[nr]));
dial1[1] = kStringPlaceholder1;
_currentRoom->addSentence(1, 1);
} else
Expand Down