Jump to content
LaunchBox Community Forums

Run Python script before rom launch...


Andy911

Recommended Posts

I feel like this must have been discussed in the past, but I've been searching and cannot find an answer.

Before a MAME Rom is launched, I want to run a python script. I need to pass the filename for the selected rom to the script. I cant figure out how to get Launchbox to pass the filename. 

I've created a Python script, and put it in a batch file. 

Using Additional Apps feature, I launch the batch file which successfully runs the py script.

I've tried using variationsof  %romlocation% in the cmd line parameters. It literally passes "%romlocation%" to the batch file, but does not substitute the filename as expected. 

I'd appreciate any help. Thanks. 

Edited by Andy911
Link to comment
Share on other sites

  • Andy911 changed the title to Run Python script before rom launch...
1 hour ago, Andy911 said:

I feel like this must have been discussed in the past, but I've been searching and cannot find an answer.

Before a MAME Rom is launched, I want to run a python script. I need to pass the filename for the selected rom to the script. I cant figure out how to get Launchbox to pass the filename. 

I've created a Python script, and put it in a batch file. 

Using Additional Apps feature, I launch the batch file which successfully runs the py script.

I've tried using variationsof  %romlocation% in the cmd line parameters. It literally passes "%romlocation%" to the batch file, but does not substitute the filename as expected. 

I'd appreciate any help. Thanks. 

These are the variables that I am aware of (no good place documenting so just picked up via change logs, etc):

%romfilename% - Filename of the ROM WITHOUT extension
%romextension% - Extension of the ROM file (ie, .rom)
%romlocation_noquotes% - Path to the ROM without quotes
%noromfile% - Replaced with empty string, suppresses rom file being added to end of the command line 
%romfile% - Rom name with extension

Have you tried %romfilename% or %romfile%  as this should work for what you are trying to do.

Edited by sundogak
edit error
Link to comment
Share on other sites

37 minutes ago, Andy911 said:

I tried the ones you listed. No luck. Launchbox doesn't substitute as expected. 

Parameters cannot be passed to Additional Apps or Running Scripts.

The quick and dirty, yet effective way to do what you need is to create a new emulator.  This 'emulator' can be anything you can execute, like a batch file, AutoHotkey script, Python script etc.

LaunchBox will pass the parameters (mentioned above) to the 'emulator'.  From there, have it do whatever pre-launch things need to be done and then have it launch MAME with its required parameters.

You can also have your 'emulator' wait until MAME exits and run something else before exiting itself [if needed].

If you don't need to run anything after MAME exits, you may still want to have your 'emulator' wait for it (MAME) to exit before closing the script so that the LaunchBox 'closing' screen will show ("Thank you for Playing").

 

Link to comment
Share on other sites

Wrapping my head around this one. 

I set up an "emulator", which actually runs my batch file with the python script. LB adds the full path to the rom as a parameter, even though I left the default command-line parameters blank. I tried using the other parameters, but LB still is not substituting the info.

I need the filename, so my py script can look up info in an xml file. 

Although I can probably make this work by having my py script parse the filename from the full path, I feel like I'm missing something here. Shouldn't LB be substituting the info when %romfile% is in the command line parameters?

Edited by Andy911
Link to comment
Share on other sites

1 hour ago, Andy911 said:

Shouldn't LB be substituting the info when %romfile% is in the command line parameters?

It does. Having %romfile% be the only thing in the command-line parameters is the same as leaving it blank.  

Quote

if %romfile% is not in the custom command-line parameters, it will be added at the end.
Otherwise it's placed in the order you put it.

With the command-line parameters blank, you capture that [%romfile%] in the batch file as %1.  What's all included in %romfile% depends on the check boxes you have checked [in the emulator].

image.thumb.png.4efabfad5fcebbe1cdbc5d9dd52878f5.png

Say the MAME game is "1942".  Per the above set checkboxes, that's what %1 will be.  "1942" (without quotes).  To run this game from a batch file which is the 'emulator', the batch file would look like: (again, using the above check boxes)

set game=%1

cd "D:\LaunchBox\Emulators\MAME\"
"D:\LaunchBox\Emulators\MAME\mame.exe" %game%

 

And to confuse matters more... you can put %romfilename% as the only parameter, and %1 will be "1942" (without quotes) regardless of the checkboxes.  Note: %romfile% will still be added to the end of the parameters that are passed to the 'emulator' and can be captured in the batch file as %2.

Link to comment
Share on other sites

Ok. The checkboxes are working for me. 

And to confuse matters more... you can put %romfilename% as the only parameter, and %1 will be "1942" (without quotes) regardless of the checkboxes.  Note: %romfile% will still be added to the end of the parameters that are passed to the 'emulator' and can be captured in the batch file as %2.

However, I tried %romfilename% as the only parameter. I tried capturing both parameters %1 and %2. It still does not substitute the date for %romfilename%. 

Link to comment
Share on other sites

22 minutes ago, Andy911 said:

However, I tried %romfilename% as the only parameter. I tried capturing both parameters %1 and %2. It still does not substitute the date for %romfilename%.

Not really sure what "date" you're referring to.

Edit the 'emulator' you created that points to the batch file.  Share a screenshot of the Details section so we can see what you have.

Link to comment
Share on other sites

Data (not date).

 

image.thumb.png.4f8906bb89fb0948bd3c480158c439ea.png

Batch File:

@echo off
REM Pass the first and second arguments (ROM filename and other parameter) to the Python script
python "G:\My Drive\Projects\Projects\Arduino RasPi and Robotics\Python to Arduino Led Control\Test LB to PY pass filename.py" %1 %2
pause

Py Script

import sys

def main():
# Capture the two parameters
    if len(sys.argv) > 1:
        rom_filename = sys.argv[1]
        second_param = sys.argv[2]      
        print(f"ROM filename: {rom_filename}")
        print(f"Second parameter: {second_param}")
    else:
        print("No filename passed.")

if __name__ == "__main__":
    main()
    input("Press Enter to close...")

 

Output:
 

image.thumb.png.dc46cd052299dfd150b4fbcb3aa834db.png

Link to comment
Share on other sites

Ok. Thanks for your help. I'll probably update anyway. 

In the latest updates, is there a way to pass the rom metadata (date of release, controls, etc...) upon launch? I have an xml file that I can parse from, but it sure would be simpler if I could get LB to just send it to my custom scripts. 

Link to comment
Share on other sites

12 minutes ago, Andy911 said:

In the latest updates, is there a way to pass the rom metadata (date of release, controls, etc...) upon launch? I have an xml file that I can parse from, but it sure would be simpler if I could get LB to just send it to my custom scripts. 

No. The only parameters that can be passed are the ones @sundogak mentioned and a couple more found here:

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...