LIJI32 / SameBoy

Game Boy and Game Boy Color emulator written in C
https://sameboy.github.io/
Other
1.58k stars 206 forks source link

Associate ROM files with SameBoy on Windows #643

Open Rangi42 opened 1 week ago

Rangi42 commented 1 week ago

This could be done by having the user run a .reg or .bat file; that's what I do for Polished Map. (See also)

I believe this C code will be the equivalent:

bool set_registry_string(HKEY hive, const char *folder, const char *name, const char *value) {
    HKEY hkey;
    LONG status = RegCreateKeyExA(hive, folder, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL);
    if (status != ERROR_SUCCESS || hkey == NULL) {
        return false;
    }
    status = RegSetValueExA(hkey, name, 0, REG_SZ, value, strlen(value) + 1);
    RegCloseKey(hkey);
    return status == ERROR_SUCCESS;
}

    set_registry_string(HKEY_CURRENT_USER, "Software\\Classes\\.gbc", "", "SameBoy");
    set_registry_string(HKEY_CURRENT_USER, "Software\\Classes\\SameBoy", "", "Game Boy Color ROM");
    set_registry_string(HKEY_CURRENT_USER, "Software\\Classes\\SameBoy\\shell\\open\\command", "", "\"C:\Programs\SameBoy\sameboy.exe\" \"%%1\"");

(Of course SameBoy should figure out the right .exe path to pass for the open command. And you might use the W or A or neither functions, not sure if SameBoy is used to using wide strings or not.)

LIJI32 commented 1 week ago

I'll give it a look, if I get it working I'll also add the cartridge icons in Windows .ico format. (SameBoy internally wraps fopen and co. to use UTF-8 strings for source compatibility with every other OS)

LIJI32 commented 1 week ago

More thoughts:

  1. Is there a way to do this less aggressively? i.e. don't overwrite existing associations.
  2. How would I associate a specific icon resource with these types?
  3. Can I add myself as a "valid open handler" for a file without "stealing" the default handler for a file type?
Rangi42 commented 1 week ago
  1. Check if "Software\\Classes\\.gbc" already exists with RegOpenKeyEx, and if so, ask the user whether to continue?
  2. Create HKCR\SameBoy\DefaultIcon and set it to the .ico path. (Or to use e.g. icon -42 within an EXE or DLL, you can do sameboy.exe,-42.)
  3. Add sameboy.exe to HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.gbc\OpenWithList.