Pcsx2: [Question] API or Com object, library, that can be called to retrieve currently running game name?

Created on 2 Jan 2018  路  18Comments  路  Source: PCSX2/pcsx2

PCSX2 version:
1.4.0

Is there any available way to call for the name of the currently loaded game?

Would like to incorporate the ability for may application (InputMapper) to load profile automatically based on the current game loaded.

Mem search for string yielded only strings that in the game itself so the address is un-trustworthy.

Question / Discussion

All 18 comments

The games are listed by their serials, a PCSX2 dev team can better help you than me on this :).

Not a dev on the project, just a fan of PCSX2 and InputMapper, but I can give you some leads. PCSX2 exposes the name of the currently-running game in the title of the console window. For a hacky solution you could pop the console and read its title whenever a new emulation window appears. As for how it determines the title, I know it involves reading the game's serial number from the CD/DVD/ISO and looking it up in the database file "GameIndex.dbf". I haven't touched C code in nearly a decade so I can't tell how you'd actually _retrieve_ that information, but it's definitely in there somewhere if you poke around for references to serials and that specific database file. Good luck!

You could hook the window title.
I know for example spotifylyrics does something along that.

@mirh Unfortunately the only window that has the game title is the console, and that window is optional.

@nullripper YES! GameIndex may be the missing link, now I have to see if I can find the game serial in the applications memory!

So, just so it is documented for future use;

Memory address 0AF5D2EA-0AF5D2F5 contains the string of the game serial. This needs a tad of text conversion then GameIndex.dbf can be used to get the name.

Thanks guys!

Was gonna say solved then, but I wouldn't know about linux best practices then.

If you want to do it not at runtime from a plugin:

I'm doing this for ps1 in order not to care about dump format (read with libmirage, part of cdemu and connect the first data track to a iso parser pycdlib ):
https://gist.github.com/i30817/f58c7f904fe44c4599a6c9d48a60bce0

In the script i'm extracting the ps1 serial (normally exec name, also in system.cnf, but sometimes in the volume descriptor)

the same approach should be viable for ps2 if it follows ps1 practice. One thing i'm not sure of is if pycdlib can parse dvd data from libmirage the same as iso9660 data.

You should have a big collection to test this idea out even if it works because sometimes there are small gotchas (some games have the serial on volume descriptor and don't have system.cnf, some games don't have the '-' in the serial but have in databases etc).

Current code won't work on windows because libmirage has no windows port (it uses gnome gobject). It should be easier for you to do if you don't care about the 'any dump format' part, since there are pure iso parsing python libraries (but you need another than pycdlib - for now - because it doesn't parse dvds yet).

I decided not to go the memory route, too susceptible to change. Instead I parse the log file using the following:

    private static string getPCSX2GameName()
    {
        if (PCSX2LogFilePath == null)
        {
            string settingsFolder = Registry.CurrentUser.OpenSubKey(@"Software\PCSX2")?.GetValue(@"SettingsFolder").ToString();
            string settingsFile = Path.Combine(settingsFolder, "PCSX2_ui.ini");
            string logFolder = new IniFile(settingsFile).Read("Logs", "Folders").Replace(@"\\", @"\");
            PCSX2LogFilePath = Path.Combine(logFolder, "emuLog.txt");
        }
        PCSX2LogTS = File.GetLastWriteTime(PCSX2LogFilePath);
        string[] logFileContents;
        using (FileStream fs = new FileStream(PCSX2LogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        using (TextReader tr = new StreamReader(fs))
        {
            logFileContents = tr.ReadToEnd().Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToArray();
        }
        string[] cdLoads = logFileContents.Where(l => l.StartsWith(@"(SYSTEM.CNF) Detected PS2 Disc = cdrom0:\")).ToArray();
        string gameSerial = cdLoads.LastOrDefault()?.Replace(@"(SYSTEM.CNF) Detected PS2 Disc = cdrom0:\", string.Empty)?.Split(';')[0];
        if (gameSerial == null) return "PCSX2";
        gameSerial = gameSerial.Replace("_", "-").Replace(".",string.Empty);
        string gameIndexDBPath = Path.Combine(Registry.CurrentUser.OpenSubKey(@"Software\PCSX2")?.GetValue(@"Install_Dir").ToString(), "GameIndex.dbf");
        string[] gameIndexDB = File.ReadAllLines(gameIndexDBPath);
        int GameIndexLine = Array.IndexOf(gameIndexDB, "Serial = " + gameSerial);
        string gameName = (gameIndexDB[GameIndexLine + 1]).Replace("Name   = ",string.Empty);
        Console.WriteLine("PCSX2 Game Name: " + gameName);
        return gameName;
    }

I'm not following the parsing code too well, but it's not unusual for a game to place its executable in a subdirectory and i don't see the code to strip those out at a glance. Or have two leading backslashes as in 'cdrom0:\\'. Or no slashes as in 'cdrom0:'

This is the reason i wrote a regex, consistency isn't perfect here. It's best to test out any solution across as wide a range of games as possible.

@i30817 Do you have an example of a game that does not follow the normal convention?

I've only tested ps1 games.

Tekken 3 has a subdir
Mizzurna Falls doesn't have a '-'
King's Field (Japan) (there was a game released as 'King's Field (USA) that is actually game 2 in the series) has no system.cnf and its executable is named 'psx.exe' (it's in the volume descriptor). It's a very early game, maybe that's why.
I don't remember what games has two or no leading slash on the boot line only that they existed. I can check them out if you want, but notice i don't have even 10% of the ps1 library.

In the redump database all of these have the normal SLUS-XXXXX format so it's obvious they're 'preprocessed'.

Pycdlib doesn't work with ps2 isos otherwise i'd tell you the format what few isos of that console i have. I suspect they are more consistent. Since redump has quite a few names with a ' ' space instead of '-' on the database, so probably doesn't have many egregious errors if that small error passes without comment and they changed their policy. (not that that is very conclusive, sega consoles are full of obviously wrong 'serials' probably hardcoded parsed from positions on the Sega Header, badly).

It's probably best not to output redump 'standard' and always put out a consistent format anyway. They probably just eyeball it from the cd covers and errors and peculiarities were introduced that way. For example in this game:
http://redump.org/disc/43088/

Which i do have and is a 'cd iso' so i can test, the exec game is normal SLPM_623.62 and is in the database as 'SLPM 62362'. Maybe it's that way in the cover... maybe not.

I think most people would use PCSX not PCSX2 for PS1 games, I couldn't get either of those games to load in the bios I have in PCSX2, Every PS2 game I have tried the above code works for however.

Also neither of those games are in the GameIndex.dbf DB anyway, so it is a moot point.

Mhh.. I must confess the wonder if there shouldn't be a ps1 gameindex is worrying..

There are 0 ps1 games on the GameIndex.dbf list IIRC.
Anyway seems that the question/request in the OP have been answered so I'll be closing this one.

You can continue the discussion btw.

Guess like we might get more expert opinions on that here.

p.s. after homura and makishima, I'm just left wondering who will be next. Motoko? Re-l?

p.s. after homura and makishima, I'm just left wondering who will be next. Motoko? Re-l?

We shall see :smile:
Tho they weren't on my mind.

You can distinguish ps1 games from ps2 games from the 'boot' liine in system.cnf. PS2 games use a 'BOOT2' line.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RinMaru picture RinMaru  路  5Comments

vgturtle127 picture vgturtle127  路  4Comments

leyo96 picture leyo96  路  5Comments

Clarke2131 picture Clarke2131  路  3Comments

Levan7 picture Levan7  路  3Comments