Fix to windows OpenFile dialog

Now it actually says something about what exactly it tries to open, also on "cancel" it exits gracefully.

Proposal: instead of checking one thousand return codes, which are easily ignored, I suggest using an exceptions (this part of emulator doesn't require execution speed, so exceptions here would be just fine).
This commit is contained in:
Pavel Dmytriiev (Github) 2018-07-26 18:57:44 +03:00 committed by Ender's Games
parent 14e1e4ded4
commit a4e6dbadb5
10 changed files with 113 additions and 135 deletions

View File

@ -125,18 +125,17 @@ s32 cfgExists(const wchar * Section, const wchar * Key)
return (cfgdb.has_section(string(Section)) ? 1 : 0);
}
}
void cfgLoadStr(const wchar * Section, const wchar * Key, wchar * Return,const wchar* Default)
void cfgLoadStr(const std::string& Section, const std::string& Key, std::string& Return, const std::string& Default)
{
string value = cfgdb.get(Section, Key, Default);
// FIXME: Buffer overflow possible
strcpy(Return, value.c_str());
Return = cfgdb.get(Section, Key, Default);
}
string cfgLoadStr(const wchar * Section, const wchar * Key, const wchar* Default)
string cfgLoadStr(const std::string& Section, const std::string& Key, const std::string& Default)
{
if(!cfgdb.has_entry(string(Section), string(Key)))
if(!cfgdb.has_entry(Section, Key))
{
cfgSaveStr(Section, Key, Default);
cfgSaveStr(Section.c_str(), Key.c_str(), Default.c_str());
}
return cfgdb.get(string(Section), string(Key), string(Default));
}

View File

@ -13,8 +13,8 @@ bool cfgOpen();
s32 cfgLoadInt(const wchar * lpSection, const wchar * lpKey,s32 Default);
s32 cfgGameInt(const wchar * lpSection, const wchar * lpKey,s32 Default);
void cfgSaveInt(const wchar * lpSection, const wchar * lpKey, s32 Int);
void cfgLoadStr(const wchar * lpSection, const wchar * lpKey, wchar * lpReturn,const wchar* lpDefault);
string cfgLoadStr(const wchar * Section, const wchar * Key, const wchar* Default);
void cfgLoadStr(const std::string& Section, const std::string& Key, std::string& Return, const std::string& Default);
std::string cfgLoadStr(const std::string& Section, const std::string& Key, const std::string& Default);
void cfgSaveStr(const wchar * lpSection, const wchar * lpKey, const wchar * lpString);
void cfgSaveBool(const wchar * Section, const wchar * Key, bool BoolValue);
bool cfgLoadBool(const wchar * Section, const wchar * Key,bool Default);

View File

@ -125,17 +125,10 @@ ConfigEntry* ConfigFile::get_entry(string section_name, string entry_name)
}
string ConfigFile::get(string section_name, string entry_name, string default_value)
std::string ConfigFile::get(const string& section_name, const string& entry_name, const string& default_value /*= ""*/)
{
ConfigEntry* entry = this->get_entry(section_name, entry_name);
if (entry == NULL)
{
return default_value;
}
else
{
return entry->get_string();
}
return (!entry) ? default_value : entry->get_string();
}
int ConfigFile::get_int(string section_name, string entry_name, int default_value)

View File

@ -33,7 +33,7 @@ struct ConfigFile {
void save(FILE* fd);
/* getting values */
string get(string section_name, string entry_name, string default_value = "");
std::string get(const string& section_name, const string& entry_name, const string& default_value = "");
int get_int(string section_name, string entry_name, int default_value = 0);
bool get_bool(string section_name, string entry_name, bool default_value = false);
/* setting values */

View File

@ -19,9 +19,9 @@ u32 RomSize;
fd_t* RomCacheMap;
u32 RomCacheMapCount;
char SelectedFile[512];
std::string SelectedFile;
bool naomi_cart_LoadRom(char* file)
bool naomi_cart_LoadRom(const char* file)
{
printf("\nnullDC-Naomi rom loader v1.2\n");
@ -50,7 +50,7 @@ bool naomi_cart_LoadRom(char* file)
char* eon = strstr(line, "\n");
if (!eon)
printf("+Loading naomi rom that has no name\n");
printf("+Loading naomi rom that has no name %s\n", line);
else
*eon = 0;
@ -204,13 +204,14 @@ bool naomi_cart_LoadRom(char* file)
bool naomi_cart_SelectFile(void* handle)
{
cfgLoadStr("config", "image", SelectedFile, "null");
#if HOST_OS == OS_WINDOWS
if (strcmp(SelectedFile, "null") == 0) {
if (SelectedFile == "null")
{
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hInstance = (HINSTANCE)GetModuleHandle(0);
ofn.lpstrFile = SelectedFile;
ofn.lpstrFile = &SelectedFile[0];
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = "*.lst\0*.lst\0\0";
ofn.nFilterIndex = 0;
@ -221,17 +222,17 @@ bool naomi_cart_SelectFile(void* handle)
return true;
}
#endif
if (!naomi_cart_LoadRom(SelectedFile))
if (!naomi_cart_LoadRom(SelectedFile.c_str()))
{
cfgSaveStr("emu", "gamefile", "naomi_bios");
}
else
{
cfgSaveStr("emu", "gamefile", SelectedFile);
cfgSaveStr("emu", "gamefile", SelectedFile.c_str());
}
printf("EEPROM file : %s.eeprom\n", SelectedFile);
printf("EEPROM file : %s.eeprom\n", SelectedFile.c_str());
return true;
}

View File

@ -23,6 +23,13 @@ Disc*(*drivers[])(const wchar* path)=
u8 q_subchannel[96];
void SetSnsCodes(const u32 asc, const u32 ascq, const u32 key)
{
sns_asc = asc;
sns_ascq = ascq;
sns_key = key;
}
void PatchRegion_0(u8* sector,int size)
{
#ifndef NOT_REICAST
@ -150,7 +157,7 @@ Disc* OpenDisc(const wchar* fn)
return rv;
}
bool InitDrive_(wchar* fn)
bool InitDrive_(const wchar* const fn)
{
TermDrive();
@ -176,132 +183,99 @@ bool InitDrive_(wchar* fn)
}
#ifndef NOT_REICAST
bool InitDrive(u32 fileflags)
bool InitDrive(u32 fileflags/*=0*/)
{
if (settings.imgread.LoadDefaultImage)
{
printf("Loading default image \"%s\"\n",settings.imgread.DefaultImage);
if (!InitDrive_(settings.imgread.DefaultImage))
printf("Loading default image \"%s\"\n", settings.imgread.DefaultImage.c_str());
if (!InitDrive_(settings.imgread.DefaultImage.c_str()))
{
msgboxf("Default image \"%s\" failed to load",MBX_ICONERROR,settings.imgread.DefaultImage);
msgboxf("Default image \"%s\" failed to load",MBX_ICONERROR,settings.imgread.DefaultImage.c_str());
return false;
}
else
return true;
}
// FIXME: Data loss if buffer is too small
wchar fn[512];
strncpy(fn,settings.imgread.LastImage, sizeof(fn));
fn[sizeof(fn) - 1] = '\0';
std::string diskImageFileName{ settings.imgread.LastImage };
#ifdef BUILD_DREAMCAST
int gfrv=GetFile(fn,0,fileflags);
int gfrv = GetFile(diskImageFileName);
#else
int gfrv=0;
int gfrv = rv_error;
#endif
if (gfrv == 0)
{
NullDriveDiscType=NoDisk;
gd_setdisc();
sns_asc=0x29;
sns_ascq=0x00;
sns_key=0x6;
return true;
}
else if (gfrv == -1)
if (gfrv != rv_ok)
{
return false;
}
// FIXME: Data loss if buffer is too small
strncpy(settings.imgread.LastImage, fn, sizeof(settings.imgread.LastImage));
settings.imgread.LastImage[sizeof(settings.imgread.LastImage) - 1] = '\0';
settings.imgread.LastImage = diskImageFileName;
SaveSettings();
if (!InitDrive_(fn))
if (!InitDrive_(diskImageFileName.c_str()))
{
//msgboxf("Selected image failed to load",MBX_ICONERROR);
NullDriveDiscType=NoDisk;
gd_setdisc();
sns_asc=0x29;
sns_ascq=0x00;
sns_key=0x6;
return true;
}
else
{
return true;
NullDriveDiscType = NoDisk;
gd_setdisc();
SetSnsCodes(0x29, 0x00, 0x06);
return false;
}
return true;
}
bool DiscSwap(u32 fileflags)
{
if (settings.imgread.LoadDefaultImage)
{
printf("Loading default image \"%s\"\n",settings.imgread.DefaultImage);
if (!InitDrive_(settings.imgread.DefaultImage))
printf("Loading default image \"%s\"\n", settings.imgread.DefaultImage.c_str());
if (!InitDrive_(settings.imgread.DefaultImage.c_str()))
{
msgboxf("Default image \"%s\" failed to load",MBX_ICONERROR,settings.imgread.DefaultImage);
msgboxf("Default image \"%s\" failed to load",MBX_ICONERROR,settings.imgread.DefaultImage.c_str());
return false;
}
else
return true;
}
// FIXME: Data loss if buffer is too small
wchar fn[512];
strncpy(fn, settings.imgread.LastImage, sizeof(fn));
fn[sizeof(fn) - 1] = '\0';
std::string diskImageFileName{ settings.imgread.LastImage };
#ifdef BUILD_DREAMCAST
int gfrv=GetFile(fn,0,fileflags);
int gfrv = GetFile(diskImageFileName);
#else
int gfrv=0;
int gfrv = rv_error;
#endif
if (gfrv == 0)
if (gfrv == rv_ok)
{
NullDriveDiscType=Open;
NullDriveDiscType = Open;
gd_setdisc();
sns_asc=0x28;
sns_ascq=0x00;
sns_key=0x6;
return true;
}
else if (gfrv == -1)
{
sns_asc=0x28;
sns_ascq=0x00;
sns_key=0x6;
return false;
}
// FIXME: Data loss if buffer is too small
strncpy(settings.imgread.LastImage, fn, sizeof(settings.imgread.LastImage));
settings.imgread.LastImage[sizeof(settings.imgread.LastImage) - 1] = '\0';
SaveSettings();
if (!InitDrive_(fn))
{
//msgboxf("Selected image failed to load",MBX_ICONERROR);
NullDriveDiscType=Open;
gd_setdisc();
sns_asc=0x28;
sns_ascq=0x00;
sns_key=0x6;
SetSnsCodes(0x28, 0x00, 0x06);
return true;
}
else
{
sns_asc=0x28;
sns_ascq=0x00;
sns_key=0x6;
SetSnsCodes(0x28, 0x00, 0x06); // to fix: we have same return codes as previous branch, looks suspicious
return false;
}
settings.imgread.LastImage = diskImageFileName;
SaveSettings();
if (!InitDrive_(diskImageFileName.c_str()))
{
//msgboxf("Selected image failed to load",MBX_ICONERROR);
NullDriveDiscType = Open;
gd_setdisc();
SetSnsCodes(0x28, 0x00, 0x06);
return true;
}
else
{
SetSnsCodes(0x28, 0x00, 0x06);
return false;
}
}
#endif

View File

@ -97,7 +97,7 @@ void GetDriveToc(u32* to,DiskArea area);
void GetDriveSector(u8 * buff,u32 StartSector,u32 SectorCount,u32 secsz);
void GetDriveSessionInfo(u8* to,u8 session);
int GetFile(char *szFileName, char *szParse=0,u32 flags=0);
int GetFile(std::string& diskFileName);
int msgboxf(wchar* text,unsigned int type,...);
void printtoc(TocInfo* toc,SessionInfo* ses);
extern u8 q_subchannel[96];

View File

@ -40,35 +40,38 @@ settings_t settings;
#include <windows.h>
#endif
int GetFile(char *szFileName, char *szParse=0, u32 flags=0)
int GetFile(std::string& diskFileName)
{
cfgLoadStr("config","image",szFileName,"null");
if (strcmp(szFileName,"null")==0)
cfgLoadStr("config", "image", diskFileName, "");
#if HOST_OS==OS_WINDOWS
if (diskFileName.empty())
{
#if HOST_OS==OS_WINDOWS
diskFileName.resize(MAX_PATH);
OPENFILENAME ofn;
ZeroMemory( &ofn , sizeof( ofn));
ofn.lStructSize = sizeof ( ofn );
ofn.hwndOwner = NULL ;
ofn.lpstrFile = szFileName ;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = "All\0*.*\0\0";
ofn.nFilterIndex =1;
ofn.lpstrFileTitle = NULL ;
ofn.nMaxFileTitle = 0 ;
ofn.lpstrInitialDir=NULL ;
ofn.Flags = OFN_PATHMUSTEXIST|OFN_FILEMUSTEXIST ;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = &diskFileName[0];
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = "Supported Disk Image Files(.CDI, .GDI)\0*.CDI;*.GDI\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrTitle = "Open disk image file";
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileNameA(&ofn))
while (!GetOpenFileName(&ofn))
{
//already there
//strcpy(szFileName,ofn.lpstrFile);
const DWORD errorCode = CommDlgExtendedError();
if (errorCode == FNERR_BUFFERTOOSMALL)
{
//The first two bytes of the lpstrFile buffer contain an integer value specifying the size required to receive the full name, in characters.
const size_t newBufferSize = ((size_t)ofn.lpstrFile[0] | (size_t)ofn.lpstrFile[1] << 8);
diskFileName.resize(newBufferSize);
//try again
}
else return (errorCode == ERROR_SUCCESS) ? rv_ok : rv_error;
}
#endif
}
return 1;
#endif
return rv_ok;
}
@ -94,8 +97,6 @@ s32 plugins_Init()
//if (s32 rv = libExtDevice_Init())
// return rv;
return rv_ok;
}
@ -204,7 +205,14 @@ int dc_init(int argc,wchar* argv[])
}
}
plugins_Init();
if (plugins_Init() != rv_ok) {
#if defined(_ANDROID)
reios_init_value = rv_error;
return;
#else
return rv_error;
#endif
}
#if defined(_ANDROID)
}

View File

@ -677,8 +677,8 @@ struct settings_t
{
bool PatchRegion;
bool LoadDefaultImage;
char DefaultImage[512];
char LastImage[512];
std::string DefaultImage;
std::string LastImage;
} imgread;
struct

View File

@ -695,7 +695,10 @@ int CALLBACK WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine
int dc_init(int argc,wchar* argv[]);
void dc_run();
void dc_term();
dc_init(argc,argv);
if(dc_init(argc,argv)!=rv_ok)
{
return 0;
}
#ifdef _WIN64
setup_seh();