Skip to content

Commit

Permalink
Mame is now executed with the cwd set to the mame folder, which great…
Browse files Browse the repository at this point in the history
…ly improve support of relative paths + support for relative rompaths too
  • Loading branch information
TiBeN committed Apr 6, 2016
1 parent 0d4c21c commit 24cf104
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ CHANGELOG
Current
-------

- Now support paths relatives to mame binary on the mame configuration
(which is the case on unmodified MAME installations) and set the cwd to
the folder containing the MAME binary when executing it.
- Added batch launcher for Windows

0.2
Expand Down
3 changes: 1 addition & 2 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ TODO
- Create deb/rpm/msi distributions packages
- Remove the configuration file and define mame.binary using environment
variable instead.
- Change the cwd to the Mame binary folder while executing Mame from Java
to better handle mame.ini default paths
- Handle the configuration parameters (ie rompath and so one) set from
the command line too.
- Try to create the rompath if it doesnt exist on the disk
- Create simple screencast to demonstrate usage
- Reduce the line size of the INFO traces
32 changes: 31 additions & 1 deletion src/main/java/org/tibennetwork/iamame/mame/MameRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,34 @@
import java.util.regex.Pattern;

import org.apache.commons.cli.ParseException;
import org.tibennetwork.iamame.IaMame;
import org.tibennetwork.iamame.internetarchive.NoWritableRomPathException;

/**
* Mame binary facade
*/
public class MameRuntime {

/**
* The path of the Mame binary executable
*/
private String binPath;

/**
* Directory containing the Mame
* executable
*/
private String binDirectory;

private List<File> romsPaths;

public MameRuntime (String binPath)
throws IOException, InterruptedException, ParseException {
this.binPath = binPath;
this.binDirectory = new File(binPath).getParent();
IaMame.debug(String.format(
"Mame binary directory: %s",
this.binDirectory));
this.getRomsPathsFromBinary();
}

Expand All @@ -47,6 +61,7 @@ public void execute (String[] rawArgs)
args.addAll(Arrays.asList(rawArgs));

ProcessBuilder builder = new ProcessBuilder(args);
builder.directory(new File(this.binDirectory));
builder.inheritIO();
Process mameProcess = builder.start();
mameProcess.waitFor();
Expand All @@ -60,6 +75,7 @@ public InputStream executeAndReturnStdoutAsInputStream (String[] rawArgs)
args.addAll(Arrays.asList(rawArgs));

ProcessBuilder builder = new ProcessBuilder(args);
builder.directory(new File(this.binDirectory));
Process mameProcess = builder.start();

return mameProcess.getInputStream();
Expand All @@ -78,6 +94,7 @@ public List<String> executeAndReturnStdout (String[] rawArgs)
args.addAll(Arrays.asList(rawArgs));

ProcessBuilder builder = new ProcessBuilder(args);
builder.directory(new File(this.binDirectory));
Process mameProcess = builder.start();

BufferedReader mameRuntimeStdout = new BufferedReader(
Expand Down Expand Up @@ -139,18 +156,31 @@ private void getRomsPathsFromBinary ()

Matcher m = p.matcher(s);
if (m.matches()) {

// Change temporary the current working dir
// to mame binary directory to find paths relative to
// it
String originalCwd = System.getProperty("user.dir");
System.setProperty("user.dir", this.binDirectory);

for(String path : m.group(1).split(";")) {

// Handle $HOME syntax
path = path.replace("$HOME",
System.getProperty("user.home"));

File dir = new File(path);


dir = new File(dir.getAbsolutePath());
if(dir.isDirectory()) {
IaMame.debug("rompath found: " + dir);
this.romsPaths.add(dir);
}
}

// Revert back the original cwd
System.setProperty("user.dir", originalCwd);

break;
}
}
Expand Down

0 comments on commit 24cf104

Please sign in to comment.