Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions core/hw/maple/maple_jvs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include <algorithm>
#include <array>
#include <memory>
#include <filesystem>
#include <fstream>

#define LOGJVS(...) DEBUG_LOG(JVS, __VA_ARGS__)

Expand Down Expand Up @@ -1355,13 +1357,14 @@ maple_naomi_jamma::maple_naomi_jamma()
}
}

std::string eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom";
FILE* f = nowide::fopen(eeprom_file.c_str(), "rb");
if (f)
std::filesystem::path eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom";
std::ifstream file(eeprom_file, std::ios::in | std::ios::binary);
if (file.is_open())
{
if (std::fread(eeprom, 1, 0x80, f) != 0x80)
file.read(reinterpret_cast<char *>(eeprom), sizeof(eeprom));
if (file.gcount() != sizeof(eeprom))
WARN_LOG(MAPLE, "Failed or truncated read of EEPROM '%s'", eeprom_file.c_str());
std::fclose(f);
file.close();
DEBUG_LOG(MAPLE, "Loaded EEPROM from %s", eeprom_file.c_str());
}
else if (naomi_default_eeprom != NULL)
Expand Down Expand Up @@ -1667,12 +1670,12 @@ void maple_naomi_jamma::handle_86_subcommand()
size = std::min((int)sizeof(eeprom) - address, size);
memcpy(eeprom + address, dma_buffer_in + 4, size);

std::string eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom";
FILE* f = nowide::fopen(eeprom_file.c_str(), "wb");
if (f)
std::filesystem::path eeprom_file = hostfs::getArcadeFlashPath() + ".eeprom";
std::ofstream f(eeprom_file, std::ios::out | std::ios::binary);
if (f.is_open())
{
std::fwrite(eeprom, 1, sizeof(eeprom), f);
std::fclose(f);
f.write(reinterpret_cast<char *>(eeprom), sizeof(eeprom));
f.close();
INFO_LOG(MAPLE, "Saved EEPROM to %s", eeprom_file.c_str());
}
else
Expand Down
46 changes: 23 additions & 23 deletions core/ui/boxart/boxart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "oslib/oslib.h"
#include "cfg/option.h"
#include <chrono>
#include <filesystem>
#include <fstream>

GameBoxart Boxart::getBoxart(const GameMedia& media)
{
Expand Down Expand Up @@ -154,10 +156,10 @@ void Boxart::saveDatabase()
{
if (!databaseDirty)
return;
std::string basePath = getSaveDirectory();
std::string db_name = basePath + DB_NAME;
FILE *file = nowide::fopen(db_name.c_str(), "wt");
if (file == nullptr)
std::filesystem::path basePath = getSaveDirectory();
std::filesystem::path db_name = basePath / DB_NAME;
std::ofstream file(db_name, std::ios::out | std::ios::trunc);
if (!file.is_open())
{
WARN_LOG(COMMON, "Can't save boxart database to %s: error %d", db_name.c_str(), errno);
return;
Expand All @@ -172,9 +174,13 @@ void Boxart::saveDatabase()
array.push_back(game.second.to_json(basePath));
}
std::string serialized = array.dump(4);
fwrite(serialized.c_str(), 1, serialized.size(), file);
fclose(file);
databaseDirty = false;
file.write(serialized.c_str(), serialized.size());
if (!file.good())
{
WARN_LOG(COMMON, "Error writing to boxart database %s",
db_name.string().c_str());
}
databaseDirty = false;
}

void Boxart::loadDatabase()
Expand All @@ -183,25 +189,19 @@ void Boxart::loadDatabase()
return;
databaseLoaded = true;
databaseDirty = false;
std::string save_dir = getSaveDirectory();
if (!file_exists(save_dir))
make_directory(save_dir);
std::string db_name = save_dir + DB_NAME;
FILE *f = nowide::fopen(db_name.c_str(), "rt");
if (f == nullptr)
std::filesystem::path save_dir = getSaveDirectory();
if (!std::filesystem::exists(save_dir))
std::filesystem::create_directory(save_dir);
std::filesystem::path db_name = save_dir / DB_NAME;
std::ifstream file(db_name, std::ios::in);
if (!file.is_open())
return;

DEBUG_LOG(COMMON, "Loading boxart database from %s", db_name.c_str());
std::string all_data;
char buf[4096];
while (true)
{
int s = fread(buf, 1, sizeof(buf), f);
if (s <= 0)
break;
all_data.append(buf, s);
}
fclose(f);
std::string all_data((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();

try {
std::lock_guard<std::mutex> guard(mutex);

Expand Down
35 changes: 18 additions & 17 deletions core/ui/boxart/gamesdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
#include "stdclass.h"
#include "emulator.h"

#include <filesystem>
#include <fstream>

#define APIKEY "3fcc5e726a129924972be97abfd577ac5311f8f12398a9d9bcb5a377d4656fa8"

std::string TheGamesDb::makeUrl(const std::string& endpoint)
Expand All @@ -46,29 +49,27 @@ TheGamesDb::~TheGamesDb()

void TheGamesDb::copyFile(const std::string& from, const std::string& to)
{
FILE *ffrom = nowide::fopen(from.c_str(), "rb");
if (ffrom == nullptr)
std::ifstream ffrom(from, std::ios::in | std::ios::binary);
if (!ffrom.is_open())
{
WARN_LOG(COMMON, "Can't open %s: error %d", from.c_str(), errno);
WARN_LOG(COMMON, "Can't open %s:", from.c_str());
return;
}
FILE *fto = nowide::fopen(to.c_str(), "wb");
if (fto == nullptr)
std::ofstream fto(to, std::ios::out | std::ios::binary);
if (!fto.is_open())
{
WARN_LOG(COMMON, "Can't open %s: error %d", to.c_str(), errno);
fclose(ffrom);
WARN_LOG(COMMON, "Can't open %s:", to.c_str());
ffrom.close();
return;
}
u8 buffer[4096];
while (true)
{
int l = fread(buffer, 1, sizeof(buffer), ffrom);
if (l == 0)
break;
fwrite(buffer, 1, l, fto);
}
fclose(ffrom);
fclose(fto);

u8 buffer[4096];
while (ffrom.read(reinterpret_cast<char*>(buffer), sizeof(buffer)) || ffrom.gcount() > 0)
{
fto.write(reinterpret_cast<char*>(buffer), ffrom.gcount());
}
ffrom.close();
fto.close();
}

json TheGamesDb::httpGet(const std::string& url)
Expand Down
Loading