Skip to content

Commit

Permalink
small round of fixes on missed stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
coornio committed Dec 6, 2024
1 parent 663b62e commit 40235d3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 33 deletions.
6 changes: 1 addition & 5 deletions src/Assistants/BasicLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ void BasicLogger::writeEntry(const BLOG type, const Str& message) noexcept {
std::ofstream logFile(mLogPath, std::ios::app);
if (logFile) {
logFile << output.str() << std::endl;

} else {
std::cerr << getSeverity(BLOG::ERROR)
<< "Unable to open log file: " << mLogPath << std::endl;
mLogPath.clear();
return;
newEntry(BLOG::ERROR, "Unable to write to Log file: \"{}\"", std::move(mLogPath).string());
}
} else {
std::cout << output.str() << std::endl;
Expand Down
10 changes: 5 additions & 5 deletions src/Assistants/HomeDirManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void HomeDirManager::clearCachedFileData() noexcept {
mFileData.resize(0);
}

bool HomeDirManager::validateGameFile(const Path gamePath) noexcept {
bool HomeDirManager::validateGameFile(const Path& gamePath) noexcept {

const auto fileExists{ fs::is_regular_file(gamePath) };
if (!fileExists || !fileExists.value()) {
Expand All @@ -91,14 +91,14 @@ bool HomeDirManager::validateGameFile(const Path gamePath) noexcept {
auto fileData{ ::readFileData(gamePath) };
if (!fileData) {
blog.newEntry(BLOG::WARN, "Path is ineligible: \"{}\" [{}]",
gamePath.string(), fileExists.error().message());
gamePath.string(), fileData.error().message());
return false;
} else {
mFileData = std::move(fileData.value());
}

mFileData = std::move(fileData.value());

const auto tempSHA1{ SHA1::from_data(mFileData) };
blog.newEntry(BLOG::INFO, "SHA1: {}", tempSHA1);
blog.newEntry(BLOG::INFO, "File SHA1: {}", tempSHA1);

if (checkGame(
std::data(mFileData), std::size(mFileData),
Expand Down
2 changes: 1 addition & 1 deletion src/Assistants/HomeDirManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class HomeDirManager final {
void setValidator(GameValidator func) noexcept { checkGame = func; }

void clearCachedFileData() noexcept;
bool validateGameFile(const Path) noexcept;
bool validateGameFile(const Path& gamePath) noexcept;
};

#pragma endregion
Expand Down
44 changes: 23 additions & 21 deletions src/Systems/CHIP8/Chip8_CoreInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
Chip8_CoreInterface::Chip8_CoreInterface() noexcept
: ASB{ std::make_unique<AudioSpecBlock>(SDL_AUDIO_S8, 1, 48'000, STREAM::COUNT) }
{
sSavestatePath = HDM->addSystemDir("savestate", "CHIP8");
if (sSavestatePath) { *sSavestatePath /= HDM->getFileSHA1(); }
sPermaRegsPath = HDM->addSystemDir("permaRegs", "CHIP8");
if (sPermaRegsPath) { *sPermaRegsPath /= HDM->getFileSHA1(); }

if (!checkFileValidity(sPermaRegsPath)) { sPermaRegsPath = nullptr; }
if ((sSavestatePath = HDM->addSystemDir("savestate", "CHIP8"))) {
*sSavestatePath /= HDM->getFileSHA1();
if (!checkFileValidity(*sSavestatePath)) { sSavestatePath = nullptr; }
}
if ((sPermaRegsPath = HDM->addSystemDir("permaRegs", "CHIP8"))) {
*sPermaRegsPath /= HDM->getFileSHA1();
if (!checkFileValidity(*sPermaRegsPath)) { sPermaRegsPath = nullptr; }
}

ASB->resumeStreams();
loadPresetBinds();
Expand Down Expand Up @@ -228,59 +230,59 @@ void Chip8_CoreInterface::triggerInterrupt(const Interrupt type) noexcept {

/*==================================================================*/

bool Chip8_CoreInterface::checkFileValidity(const Path* filePath) noexcept {
if (!filePath) { return false; }
bool Chip8_CoreInterface::checkFileValidity(const Path& filePath) noexcept {
if (filePath.empty()) { return false; }

const auto fileExists{ fs::exists(*filePath) };
const auto fileExists{ fs::exists(filePath) };
if (!fileExists) {
blog.newEntry(BLOG::ERROR, "\"{}\" [{}]",
filePath->string(), fileExists.error().message()
filePath.string(), fileExists.error().message()
);
return false;
}

if (fileExists.value()) {
const auto fileNormal{ fs::is_regular_file(*filePath) };
const auto fileNormal{ fs::is_regular_file(filePath) };
if (!fileNormal) {
blog.newEntry(BLOG::ERROR, "\"{}\" [{}]",
filePath->string(), fileExists.error().message()
filePath.string(), fileNormal.error().message()
);
return false;
}

if (fileNormal.value()) { return true; }
else {
const auto fileRemove{ fs::remove(*filePath) };
const auto fileRemove{ fs::remove(filePath) };
if (!fileRemove) {
blog.newEntry(BLOG::ERROR, "\"{}\" [{}]",
filePath->string(), fileExists.error().message()
filePath.string(), fileRemove.error().message()
);
return false;
}

if (fileRemove.value()) { return true; }
else {
blog.newEntry(BLOG::WARN, "{}: \"{}\"",
"Cannot remove irregular file", filePath->string()
"Cannot remove irregular file", filePath.string()
);
return false;
}
}
} else {
const char blankRegs[sPermRegsV.size()]{};
const auto fileWritten{ writeFileData(*filePath, blankRegs) };
const auto fileWritten{ ::writeFileData(filePath, blankRegs) };
if (fileWritten) { return true; }
else {
blog.newEntry(BLOG::WARN, "{}: \"{}\"",
"Cannot write new file", filePath->string()
"Cannot write new file", filePath.string()
);
return false;
}
}
}

void Chip8_CoreInterface::setFilePermaRegs(const u32 X) noexcept {
auto fileData{ writeFileData(*sPermaRegsPath, mRegisterV, X) };
auto fileData{ ::writeFileData(*sPermaRegsPath, mRegisterV, X) };
if (!fileData) {
blog.newEntry(BLOG::ERROR, "File IO error: \"{}\" [{}]",
sPermaRegsPath->string(), fileData.error().message()
Expand All @@ -289,7 +291,7 @@ void Chip8_CoreInterface::setFilePermaRegs(const u32 X) noexcept {
}

void Chip8_CoreInterface::getFilePermaRegs(const u32 X) noexcept {
auto fileData{ readFileData(*sPermaRegsPath, X) };
auto fileData{ ::readFileData(*sPermaRegsPath, X) };
if (!fileData) {
blog.newEntry(BLOG::ERROR, "File IO error: \"{}\" [{}]",
sPermaRegsPath->string(), fileData.error().message()
Expand All @@ -301,15 +303,15 @@ void Chip8_CoreInterface::getFilePermaRegs(const u32 X) noexcept {

void Chip8_CoreInterface::setPermaRegs(const u32 X) noexcept {
if (sPermaRegsPath) {
if (checkFileValidity(sPermaRegsPath)) { setFilePermaRegs(X); }
if (checkFileValidity(*sPermaRegsPath)) { setFilePermaRegs(X); }
else { sPermaRegsPath = nullptr; }
}
std::copy_n(mRegisterV.begin(), X, sPermRegsV.begin());
}

void Chip8_CoreInterface::getPermaRegs(const u32 X) noexcept {
if (sPermaRegsPath) {
if (checkFileValidity(sPermaRegsPath)) { getFilePermaRegs(X); }
if (checkFileValidity(*sPermaRegsPath)) { getFilePermaRegs(X); }
else { sPermaRegsPath = nullptr; }
}
std::copy_n(sPermRegsV.begin(), X, mRegisterV.begin());
Expand Down
2 changes: 1 addition & 1 deletion src/Systems/CHIP8/Chip8_CoreInterface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class Chip8_CoreInterface : public EmuInterface {
void triggerInterrupt(const Interrupt type) noexcept;

private:
bool checkFileValidity(const Path* filePath) noexcept;
bool checkFileValidity(const Path& filePath) noexcept;
void setFilePermaRegs(const u32 X) noexcept;
void getFilePermaRegs(const u32 X) noexcept;

Expand Down

0 comments on commit 40235d3

Please sign in to comment.