(GTK) Fix some improper nulls, and let's save the rom database to config

This commit is contained in:
Alcaro
2019-01-05 21:49:44 +01:00
parent 3acf91df82
commit 8eb78a9b15
4 changed files with 66 additions and 3 deletions

4
.gitignore vendored
View File

@@ -1,3 +1,7 @@
profile/choice
profile/firefox-10.0esr.tar
profile/firefox-17.0esr.tar
obj/
flips
flips.exe

View File

@@ -683,7 +683,7 @@ static void a_ApplyRun(GtkButton* widget, gpointer user_data)
GPid pid;
GError* error=NULL;
if (!g_spawn_async(patchname, (gchar**)argv, NULL, G_SPAWN_DEFAULT, NULL, NULL, &pid, &error))
if (!g_spawn_async(*patchname ? patchname : NULL, (gchar**)argv, NULL, G_SPAWN_DEFAULT, NULL, NULL, &pid, &error))
{
//g_unlink(tempname);//apparently this one isn't in the headers.
ShowMessage((struct errorinfo){ el_broken, error->message });
@@ -775,7 +775,8 @@ static void a_SetEmulatorFor(GtkButton* widget, gpointer user_data)
gtk_file_filter_add_custom(filter, GTK_FILE_FILTER_URI, filterExecOnly, NULL, NULL);
gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter);
char* emu_uri = g_filename_to_uri(GetEmuFor((const char*)user_data), NULL, NULL);
const char * emu_path = GetEmuFor((const char*)user_data);
char* emu_uri = emu_path ? g_filename_to_uri(emu_path, NULL, NULL) : NULL;
setoutpath(GTK_FILE_CHOOSER(dialog), emu_uri, false);
g_free(emu_uri);

View File

@@ -337,6 +337,11 @@ void config::init_raw(LPWSTR contents)
contents = nextline+1;
while (contents && iswspace(*contents)) contents++;
}
for (size_t i=0;i<numentries;i++)
{
AddConfigToRomList(names[i], values[i]);
}
}
void config::init_file(LPCWSTR filename)
@@ -419,7 +424,7 @@ LPCWSTR config::get(LPCWSTR name, LPCWSTR def)
LPWSTR config::flatten()
{
LPCWSTR header = TEXT("[Flips]\n#Changing this file voids your warranty. Do not report any bugs if you do.\n");
LPCWSTR header = TEXT("[Flips]\n#Changing this file may void your warranty. Do not report any bugs if you do.\n");
size_t len = wcslen(header);
for (size_t i=0;i<this->numentries;i++)
@@ -497,6 +502,9 @@ enum {
ch_crc32,
ch_last
};
static LPCWSTR checkmap_typenames[] = { TEXT("rom.crc32.") };
// sizeof rather than strlen to ensure compile-time evaluation; -1 for NUL
static const int checkmap_typenames_maxlen = sizeof("rom.crc32.")-1;
struct checkmap {
uint8_t* sum;
LPWSTR name;
@@ -506,6 +514,43 @@ static uint32_t checkmap_len[ch_last]={0};
static const uint8_t checkmap_sum_size[]={ 4 };
static const uint8_t checkmap_sum_size_max = 4;
static const int CfgSumNameMaxLen = checkmap_typenames_maxlen + checkmap_sum_size_max*2 + 1;
static void CfgSumName(WCHAR* out, int type, const void* sum)
{
const uint8_t* sum8 = (uint8_t*)sum;
wcscpy(out, checkmap_typenames[type]);
WCHAR* end = out + wcslen(checkmap_typenames[type]);
for (int i=0;i<checkmap_sum_size[type];i++)
wsprintf(end+i*2, TEXT("%.2X"), sum8[i]);
}
static bool CfgSumParseName(int* type, void* sum, LPCWSTR in)
{
if (!!wcsncmp(in, TEXT("rom."), strlen("rom.")))
return false;
uint8_t* out = (uint8_t*)sum;
for (int t=0;t<ch_last;t++)
{
if (!wcsncmp(in, checkmap_typenames[t], wcslen(checkmap_typenames[t])))
{
*type = t;
LPCWSTR hex = in + wcslen(checkmap_typenames[t]);
if (wcslen(hex) != checkmap_sum_size[t]*2) return false;
WCHAR tmp[3];
unsigned tmpout;
tmp[2] = '\0';
for (int i=0;i<checkmap_sum_size[t];i++)
{
tmp[0] = hex[i*2+0];
tmp[1] = hex[i*2+1];
sscanf(tmp, "%x", &tmpout);
out[i] = tmpout; // not %hhx because XP doesn't trust c99
}
return true;
}
}
return false;
}
static LPCWSTR FindRomForSum(int type, void* sum)
{
for (unsigned int i=0;i<checkmap_len[type];i++)
@@ -533,6 +578,10 @@ static void AddRomForSum(int type, void* sum, LPCWSTR filename)
item->sum=(uint8_t*)malloc(checkmap_sum_size[type]);
memcpy(item->sum, sum, checkmap_sum_size[type]);
item->name=wcsdup(filename);
WCHAR cfgname[CfgSumNameMaxLen];
CfgSumName(cfgname, type, sum);
cfg.set(cfgname, filename);
}
struct mem GetRomList()
@@ -648,6 +697,14 @@ void AddToRomList(file* patch, LPCWSTR path)
}
}
void AddConfigToRomList(LPCWSTR key, LPCWSTR value)
{
int type;
uint8_t sum[checkmap_sum_size_max];
if (CfgSumParseName(&type, sum, key))
AddRomForSum(type, sum, value);
}
void DeleteRomFromList(LPCWSTR path)
{
for (unsigned int type=0;type<ch_last;type++)

View File

@@ -230,6 +230,7 @@ struct mem GetRomList();
void SetRomList(struct mem data);
LPCWSTR FindRomForPatch(file* patch, bool * possibleToFind);
void AddToRomList(file* patch, LPCWSTR path);
void AddConfigToRomList(LPCWSTR key, LPCWSTR value);
void DeleteRomFromList(LPCWSTR path);
LPCWSTR GetEmuFor(LPCWSTR filename); // NULL if none