Assorted portability enhancements (#1800)

* Introduce some Platform calls for managing dynamic libraries

* Add Platform::WriteFATSectors

* Introduce some Platform calls for managing dynamic libraries

* Add Platform::WriteFATSectors

* Change includes of "../types.h" to "types.h"

- Makes it easier to directly include these headers in downstream projects

* Change an include of "../Wifi.h" to "Wifi.h"

* Allow CommonFuncs.cpp to compile on Android

* Tidy up some logging calls

- Use Platform::Log in LAN_Socket.cpp
- Soften some warnings to Debug logs (since they don't necessarily represent problems)

* Add Platform::EnterGBAMode

- Gracefully stop the emulator if trying to enter GBA mode

* Soften some logs that most players won't care about

* Soften some more logs

* Introduce Platform wrappers for file operations

* Fix pointer spacing

* Fix more style nits

* Log the errno when ftruncate fails

* Fix FileSeek offset argument

- With an s32 offset, we couldn't access files larger than 2GB

* Revise Platform::StopEmu to address feedback

- Remove Platform::EnterGBAMode in favor of adding a reason to Platform::StopEmu
- Also rename Platform::StopEmu to Platform::SignalStop
- Add an optional argument to NDS::Stop
- Use the new argument everywhere that the console stops itself

* Rename FileGetString to FileReadLine

- It conveys the meaning better

* Rename FileSeekOrigin::Set to Start

- It conveys the meaning better

* Change definition of FileGetString to FileReadLine

- Oops, almost forgot it

* Rename FlushFile to FileFlush

- To remain consistent with the other File functions

* Add a FileType usage

* Fix line break in FileSeekOrigin

* Document Platform::DeInit

* Clarify that StopReason::Unknown doesn't always mean an error

* Move and document FileType::HostFile

* Remove Platform::OpenDataFile

- Nothing currently uses it

* Refactor Platform::OpenFile and Platform::OpenLocalFile to accept a FileMode enum instead of a string

- The enum is converted to fopen flags under the hood
- The file type is used to decide whether to add the "b" flag
- Some helper functions are exposed for the benefit of consistent behavior among frontends
- Equivalent behavior is maintained

* Fix a tab that should be spaces

* Use Windows' 64-bit implementations of fseek/ftell

* Move Platform::IsBinaryFile to Platform.cpp

- It could vary by frontend

* Remove an unused FileType

* Rename an enum constant

* Document various Platform items

* Use Platform::DynamicLibrary to load libandroid

- And clean it up at the end

* Fix a typo

* Pass the correct filetype to FATStorage

- Since it can be used for DSI NAND images or for SD cards

* Remove Platform::FileType
This commit is contained in:
Jesse Talavera-Greenberg
2023-08-18 16:50:57 -04:00
committed by GitHub
parent f454eba3c3
commit ee55677086
36 changed files with 787 additions and 443 deletions

View File

@ -36,6 +36,7 @@
#include "SPI.h"
#include "DSi_I2C.h"
using namespace Platform;
namespace ROMManager
{
@ -119,137 +120,131 @@ std::string GetAssetPath(bool gba, const std::string& configpath, const std::str
QString VerifyDSBIOS()
{
FILE* f;
FileHandle* f;
long len;
f = Platform::OpenLocalFile(Config::BIOS9Path, "rb");
f = Platform::OpenLocalFile(Config::BIOS9Path, FileMode::Read);
if (!f) return "DS ARM9 BIOS was not found or could not be accessed. Check your emu settings.";
fseek(f, 0, SEEK_END);
len = ftell(f);
len = FileLength(f);
if (len != 0x1000)
{
fclose(f);
CloseFile(f);
return "DS ARM9 BIOS is not a valid BIOS dump.";
}
fclose(f);
CloseFile(f);
f = Platform::OpenLocalFile(Config::BIOS7Path, "rb");
f = Platform::OpenLocalFile(Config::BIOS7Path, FileMode::Read);
if (!f) return "DS ARM7 BIOS was not found or could not be accessed. Check your emu settings.";
fseek(f, 0, SEEK_END);
len = ftell(f);
len = FileLength(f);
if (len != 0x4000)
{
fclose(f);
CloseFile(f);
return "DS ARM7 BIOS is not a valid BIOS dump.";
}
fclose(f);
CloseFile(f);
return "";
}
QString VerifyDSiBIOS()
{
FILE* f;
FileHandle* f;
long len;
// TODO: check the first 32 bytes
f = Platform::OpenLocalFile(Config::DSiBIOS9Path, "rb");
f = Platform::OpenLocalFile(Config::DSiBIOS9Path, FileMode::Read);
if (!f) return "DSi ARM9 BIOS was not found or could not be accessed. Check your emu settings.";
fseek(f, 0, SEEK_END);
len = ftell(f);
len = FileLength(f);
if (len != 0x10000)
{
fclose(f);
CloseFile(f);
return "DSi ARM9 BIOS is not a valid BIOS dump.";
}
fclose(f);
CloseFile(f);
f = Platform::OpenLocalFile(Config::DSiBIOS7Path, "rb");
f = Platform::OpenLocalFile(Config::DSiBIOS7Path, FileMode::Read);
if (!f) return "DSi ARM7 BIOS was not found or could not be accessed. Check your emu settings.";
fseek(f, 0, SEEK_END);
len = ftell(f);
len = FileLength(f);
if (len != 0x10000)
{
fclose(f);
CloseFile(f);
return "DSi ARM7 BIOS is not a valid BIOS dump.";
}
fclose(f);
CloseFile(f);
return "";
}
QString VerifyDSFirmware()
{
FILE* f;
FileHandle* f;
long len;
f = Platform::OpenLocalFile(Config::FirmwarePath, "rb");
f = Platform::OpenLocalFile(Config::FirmwarePath, FileMode::Read);
if (!f) return "DS firmware was not found or could not be accessed. Check your emu settings.";
fseek(f, 0, SEEK_END);
len = ftell(f);
len = FileLength(f);
if (len == 0x20000)
{
// 128KB firmware, not bootable
fclose(f);
CloseFile(f);
// TODO report it somehow? detect in core?
return "";
}
else if (len != 0x40000 && len != 0x80000)
{
fclose(f);
CloseFile(f);
return "DS firmware is not a valid firmware dump.";
}
fclose(f);
CloseFile(f);
return "";
}
QString VerifyDSiFirmware()
{
FILE* f;
FileHandle* f;
long len;
f = Platform::OpenLocalFile(Config::DSiFirmwarePath, "rb");
f = Platform::OpenLocalFile(Config::DSiFirmwarePath, FileMode::Read);
if (!f) return "DSi firmware was not found or could not be accessed. Check your emu settings.";
fseek(f, 0, SEEK_END);
len = ftell(f);
len = FileLength(f);
if (len != 0x20000)
{
// not 128KB
// TODO: check whether those work
fclose(f);
CloseFile(f);
return "DSi firmware is not a valid firmware dump.";
}
fclose(f);
CloseFile(f);
return "";
}
QString VerifyDSiNAND()
{
FILE* f;
FileHandle* f;
long len;
f = Platform::OpenLocalFile(Config::DSiNANDPath, "r+b");
f = Platform::OpenLocalFile(Config::DSiNANDPath, FileMode::ReadWriteExisting);
if (!f) return "DSi NAND was not found or could not be accessed. Check your emu settings.";
// TODO: some basic checks
// check that it has the nocash footer, and all
fclose(f);
CloseFile(f);
return "";
}
@ -659,29 +654,28 @@ bool LoadROM(QStringList filepath, bool reset)
// regular file
std::string filename = filepath.at(0).toStdString();
FILE* f = Platform::OpenFile(filename, "rb", true);
Platform::FileHandle* f = Platform::OpenFile(filename, FileMode::Read);
if (!f) return false;
fseek(f, 0, SEEK_END);
long len = ftell(f);
long len = Platform::FileLength(f);
if (len > 0x40000000)
{
fclose(f);
Platform::CloseFile(f);
delete[] filedata;
return false;
}
fseek(f, 0, SEEK_SET);
Platform::FileRewind(f);
filedata = new u8[len];
size_t nread = fread(filedata, (size_t)len, 1, f);
size_t nread = Platform::FileRead(filedata, (size_t)len, 1, f);
if (nread != 1)
{
fclose(f);
Platform::CloseFile(f);
delete[] filedata;
return false;
}
fclose(f);
Platform::CloseFile(f);
filelen = (u32)len;
if (filename.length() > 4 && filename.substr(filename.length() - 4) == ".zst")
@ -754,17 +748,16 @@ bool LoadROM(QStringList filepath, bool reset)
std::string origsav = savname;
savname += Platform::InstanceFileSuffix();
FILE* sav = Platform::OpenFile(savname, "rb", true);
if (!sav) sav = Platform::OpenFile(origsav, "rb", true);
FileHandle* sav = Platform::OpenFile(savname, FileMode::Read);
if (!sav) sav = Platform::OpenFile(origsav, FileMode::Read);
if (sav)
{
fseek(sav, 0, SEEK_END);
savelen = (u32)ftell(sav);
savelen = (u32)Platform::FileLength(sav);
fseek(sav, 0, SEEK_SET);
FileRewind(sav);
savedata = new u8[savelen];
fread(savedata, savelen, 1, sav);
fclose(sav);
FileRead(savedata, savelen, 1, sav);
CloseFile(sav);
}
bool res = NDS::LoadCart(filedata, filelen, savedata, savelen);
@ -841,28 +834,27 @@ bool LoadGBAROM(QStringList filepath)
// regular file
std::string filename = filepath.at(0).toStdString();
FILE* f = Platform::OpenFile(filename, "rb", true);
FileHandle* f = Platform::OpenFile(filename, FileMode::Read);
if (!f) return false;
fseek(f, 0, SEEK_END);
long len = ftell(f);
long len = FileLength(f);
if (len > 0x40000000)
{
fclose(f);
CloseFile(f);
return false;
}
fseek(f, 0, SEEK_SET);
FileRewind(f);
filedata = new u8[len];
size_t nread = fread(filedata, (size_t)len, 1, f);
size_t nread = FileRead(filedata, (size_t)len, 1, f);
if (nread != 1)
{
fclose(f);
CloseFile(f);
delete[] filedata;
return false;
}
fclose(f);
CloseFile(f);
filelen = (u32)len;
if (filename.length() > 4 && filename.substr(filename.length() - 4) == ".zst")
@ -926,17 +918,16 @@ bool LoadGBAROM(QStringList filepath)
std::string origsav = savname;
savname += Platform::InstanceFileSuffix();
FILE* sav = Platform::OpenFile(savname, "rb", true);
if (!sav) sav = Platform::OpenFile(origsav, "rb", true);
FileHandle* sav = Platform::OpenFile(savname, FileMode::Read);
if (!sav) sav = Platform::OpenFile(origsav, FileMode::Read);
if (sav)
{
fseek(sav, 0, SEEK_END);
savelen = (u32)ftell(sav);
savelen = (u32)FileLength(sav);
fseek(sav, 0, SEEK_SET);
FileRewind(sav);
savedata = new u8[savelen];
fread(savedata, savelen, 1, sav);
fclose(sav);
FileRead(savedata, savelen, 1, sav);
CloseFile(sav);
}
bool res = NDS::LoadGBACart(filedata, filelen, savedata, savelen);