Build Your Own Arcade Controls Forum

Main => Software Forum => Topic started by: Justin on January 26, 2023, 08:20:01 pm

Title: how to parse MAME.XML to obtain 4 or 8 way info
Post by: Justin on January 26, 2023, 08:20:01 pm
Im trying to write a BAT file that will parse the MAME.XML file and obtain the "way" of the joystick (4 or 8 way) for a given rom that I will pass on as an argument to the BAT file.
This will be used to set the motorized restrictor plate of my joystick automatically.

I have tried the well known XPATH.BAT code out there to use a simple XPATH command but it does NOT work except to match the first line of the XML file for whatever reason.  I think the XPATH.BAT is not robust as I keep getting odd results.

the XPATH command needed would be:
//machine[@name="pacman"]/control[@player="1"]/@ways

Anyone have any other ideas how to parse the XML file and obtain the joystick (4 or 8) way data for a given rom?  I need it to be scriptable, and preferably in the BAT format for compatibility with my other scripts...

Title: Re: how to parse MAME.XML to obtain 4 or 8 way info
Post by: Justin on January 26, 2023, 08:57:16 pm
And here is a sample of the simplistic XML file I am parsing (it is a simplified version of mame.xml with just the 4/8 way info)

Code: [Select]
<mame build="0.251 (mame0251)" debug="no" mameconfig="10">
<machine name="005" sourcefile="sega/segag80r.cpp" sampleof="005">
<control type="joy" player="1" buttons="1" ways="4"/>
<control type="joy" player="2" buttons="1" ways="4"/>
</machine>
<machine name="10yard" sourcefile="irem/m58.cpp">
<control type="joy" player="1" buttons="2" ways="8"/>
<control type="joy" player="2" buttons="2" ways="8"/>
</machine>
<machine name="10yard85" sourcefile="irem/m58.cpp" cloneof="10yard" romof="10yard">
<control type="joy" player="1" buttons="2" ways="8"/>
<control type="joy" player="2" buttons="2" ways="8"/>
</machine>
<machine name="10yardj" sourcefile="irem/m58.cpp" cloneof="10yard" romof="10yard">
<control type="joy" player="1" buttons="2" ways="8"/>
<control type="joy" player="2" buttons="2" ways="8"/>
</machine>
</mame>
Title: Re: how to parse MAME.XML to obtain 4 or 8 way info
Post by: 10yard on January 27, 2023, 07:22:06 am
You can use powershell to query the XML.

This command line will search your mame.xml for a machine named "10yard" and then return the control ways attribute.  This example will return the value "8" - the first of the 2 occurrences i.e. player 1 controls.

Code: [Select]
powershell "Select-Xml -Path mame.xml -XPath '/mame/machine' | ForEach-Object { if($_.Node.name -eq '10yard') {$_.Node.control.ways[0] }"}
You can pipe the result to a file or save the result to a powershell variable for use in your batch script.
Hopefully gives you a solution to work with.
Title: Re: how to parse MAME.XML to obtain 4 or 8 way info
Post by: Justin on January 27, 2023, 08:19:22 am
Thanks, I was looking into powershell last night but seemed complicated.

I tried your code and get an error:


D:\mame>powershell "Select-Xml -Path mame-compact.xml -XPath '/mame/machine' | F
orEach-Object { if($_.Node.name -eq '10yard') {$_.Node.control.ways[0] }"}
Cannot index into a null array.
At line:1 char:129
+ Select-Xml -Path mame-compact.xml -XPath '/mame/machine' | ForEach-Object { i
f($_.Node.name -eq '10yard') {$_.Node.control.ways[ <<<< 0] }}
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray


Any ideas what this may be?
Title: Re: how to parse MAME.XML to obtain 4 or 8 way info
Post by: 10yard on January 27, 2023, 08:30:24 am
Thanks, I was looking into powershell last night but seemed complicated.

I tried your code and get an error:


D:\mame>powershell "Select-Xml -Path mame-compact.xml -XPath '/mame/machine' | F
orEach-Object { if($_.Node.name -eq '10yard') {$_.Node.control.ways[0] }"}
Cannot index into a null array.
At line:1 char:129
+ Select-Xml -Path mame-compact.xml -XPath '/mame/machine' | ForEach-Object { i
f($_.Node.name -eq '10yard') {$_.Node.control.ways[ <<<< 0] }}
    + CategoryInfo          : InvalidOperation: (0:Int32) [], RuntimeException
    + FullyQualifiedErrorId : NullArray


Any ideas what this may be?

This worked for me when using your originally posted mame.xml.  Can you attach your mame-compact.xml?  maybe the XML structure is slightly different. 
Title: Re: how to parse MAME.XML to obtain 4 or 8 way info
Post by: Justin on January 27, 2023, 09:41:27 pm
I think you are right.  The file seems to have some odd formatting.

I did end up going a different route after discovering I can request the xml file from mame for a SPECIFIC ROM.

I've finished my script now and it works flawlessly.  Thanks!

I'll post a separate thread to share the auto switching script. For those with motorized Sanwa TOS GRS and Hyperspin.
Title: Re: how to parse MAME.XML to obtain 4 or 8 way info
Post by: Justin on January 27, 2023, 11:38:20 pm
SOLVED!

http://forum.arcadecontrols.com/index.php/topic,167355.msg1762688.html#msg1762688