Skip to content

Commit

Permalink
Allow AREngine to be used independently of ARCodeFile (#2108)
Browse files Browse the repository at this point in the history
* Make `EmuInstance::cheatFile` use a `unique_ptr`

- Fixes a memory leak, as the cheat file wasn't cleaned up in the destructor

* Split `AREngine` and `ARCodeFile` apart

- Suitable for frontends that have their own way of storing cheats
- Store the cheats in `AREngine` in a `std::vector`
- Apparently cheats are _supposed_ to be executed each frame; I didn't understand this until recently
  • Loading branch information
JesseTG authored Aug 1, 2024
1 parent f3f6a6a commit c6bf5d5
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
21 changes: 15 additions & 6 deletions src/ARCodeFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,26 @@ ARCodeFile::ARCodeFile(const std::string& filename)
{
Filename = filename;

Error = false;

Categories.clear();

if (!Load())
Error = true;
}

ARCodeFile::~ARCodeFile()
std::vector<ARCode> ARCodeFile::GetCodes() const noexcept
{
Categories.clear();
if (Error)
return {};

std::vector<ARCode> codes;

for (const ARCodeCat& cat : Categories)
{
for (const ARCode& code : cat.Codes)
{
codes.push_back(code);
}
}

return codes;
}

bool ARCodeFile::Load()
Expand Down
8 changes: 5 additions & 3 deletions src/ARCodeFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ class ARCodeFile
{
public:
ARCodeFile(const std::string& filename);
~ARCodeFile();
~ARCodeFile() noexcept = default;

bool Error;
[[nodiscard]] std::vector<ARCode> GetCodes() const noexcept;

bool Error = false;

bool Load();
bool Save();

ARCodeCatList Categories;
ARCodeCatList Categories {};

private:
std::string Filename;
Expand Down
16 changes: 4 additions & 12 deletions src/AREngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ using Platform::LogLevel;

AREngine::AREngine(melonDS::NDS& nds) : NDS(nds)
{
CodeFile = nullptr;
}

#define case16(x) \
Expand Down Expand Up @@ -388,19 +387,12 @@ void AREngine::RunCheat(const ARCode& arcode)

void AREngine::RunCheats()
{
if (!CodeFile) return;
if (Cheats.empty()) return;

for (ARCodeCatList::iterator i = CodeFile->Categories.begin(); i != CodeFile->Categories.end(); i++)
for (const ARCode& code : Cheats)
{
ARCodeCat& cat = *i;

for (ARCodeList::iterator j = cat.Codes.begin(); j != cat.Codes.end(); j++)
{
ARCode& code = *j;

if (code.Enabled)
RunCheat(code);
}
if (code.Enabled)
RunCheat(code);
}
}
}
10 changes: 5 additions & 5 deletions src/AREngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef ARENGINE_H
#define ARENGINE_H

#include <vector>
#include "ARCodeFile.h"

namespace melonDS
Expand All @@ -29,14 +30,13 @@ class AREngine
public:
AREngine(melonDS::NDS& nds);

ARCodeFile* GetCodeFile() { return CodeFile; }
void SetCodeFile(ARCodeFile* file) { CodeFile = file; }

std::vector<ARCode> Cheats {};
private:
friend class ARM;
void RunCheats();
void RunCheat(const ARCode& arcode);
private:

melonDS::NDS& NDS;
ARCodeFile* CodeFile; // AR code file - frontend is responsible for managing this
};

}
Expand Down
25 changes: 15 additions & 10 deletions src/frontend/qt_sdl/EmuInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,12 +686,8 @@ void EmuInstance::undoStateLoad()

void EmuInstance::unloadCheats()
{
if (cheatFile)
{
delete cheatFile;
cheatFile = nullptr;
nds->AREngine.SetCodeFile(nullptr);
}
cheatFile = nullptr; // cleaned up by unique_ptr
nds->AREngine.Cheats.clear();
}

void EmuInstance::loadCheats()
Expand All @@ -701,9 +697,16 @@ void EmuInstance::loadCheats()
std::string filename = getAssetPath(false, globalCfg.GetString("CheatFilePath"), ".mch");

// TODO: check for error (malformed cheat file, ...)
cheatFile = new ARCodeFile(filename);
cheatFile = std::make_unique<ARCodeFile>(filename);

nds->AREngine.SetCodeFile(cheatsOn ? cheatFile : nullptr);
if (cheatsOn)
{
nds->AREngine.Cheats = cheatFile->GetCodes();
}
else
{
nds->AREngine.Cheats.clear();
}
}

std::unique_ptr<ARM9BIOSImage> EmuInstance::loadARM9BIOS() noexcept
Expand Down Expand Up @@ -1013,12 +1016,14 @@ void EmuInstance::enableCheats(bool enable)
{
cheatsOn = enable;
if (cheatFile)
nds->AREngine.SetCodeFile(cheatsOn ? cheatFile : nullptr);
nds->AREngine.Cheats = cheatFile->GetCodes();
else
nds->AREngine.Cheats.clear();
}

ARCodeFile* EmuInstance::getCheatFile()
{
return cheatFile;
return cheatFile.get();
}

void EmuInstance::setBatteryLevels()
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/qt_sdl/EmuInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ class EmuInstance
bool savestateLoaded;
std::string previousSaveFile;

melonDS::ARCodeFile* cheatFile;
std::unique_ptr<melonDS::ARCodeFile> cheatFile;
bool cheatsOn;

SDL_AudioDeviceID audioDevice;
Expand Down

0 comments on commit c6bf5d5

Please sign in to comment.