Advertisement
feasel

Porting SGL's Lua script to other emulators

May 16th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.64 KB | None | 0 0
  1. Here is the existing Lua script for FCEUX and Snes9x:
  2. http://pastebin.com/w2VkNcAj
  3.  
  4. It would all be straightforward except that the API does not contain a movie.start() function. So there is no way to programmatically begin a movie recording. There is a movie.stop() function, and you can access the filename of the movie that is currently playing/recording or toggle its read/write flag, etc, you just can't start a new movie. This is true for FCEUX, Snes9x, Bizhawk, and all other emulators I've seen so far. While the most elegant solution might be to extend the source code of Bizhawk (or whatever emulator) to add movie.start() to its API, in practice this would be a very difficult undertaking. So we're stuck with trying to create some sort of workaround.
  5.  
  6. Here's the way the script works now (in FCEUX and Snes9x, which have APIs that are nearly identical): At the beginning of every match the player must manually load the savestate for the goal, begin a movie recording, and then run the Lua script. When the Lua script begins, it creates a savestate (that exists in memory but not in a file) of the game's current state at frame 0 of the movie. Whenever the player hits the Reset key, it reloads that initial state and re-records the movie from frame 0.
  7.  
  8. The problem is that when it comes time for a player to upload a completed run, we can't close/finalize the movie file since we'd be unable to start the movie again for the next run without forcing the player to start the movie manually. So we have to make a copy of the movie file while that file is still open in the emulator. Which means you have to trust that the emulator has actually flushed all its data to disk recently. FCEUX and Snes9x will flush to disk regularly but not every single frame -- which is why the current version has a 5-second countdown timer after you hit the submit button. The other issue is that when you copy a movie file that's still open, the header has not been finalized to include stuff like the length of the movie. So this needs to be done by the Lua script.
  9.  
  10. Snes9x has one more oddity: after you close a file it adds one extra frame to the beginning of the movie. Perhaps this is done so that code that looks for changes in button-state between the previous frame and the current frame will function correctly on the first frame of the movie. But the point is that this extra frame isn't there while the file is still open. So when we fix the header on the copied file we also need to add a dummy frame to the beginning of the movie or else it will desync.
  11.  
  12. So for whatever emulators we'd like to port this Lua script to, we'll need to investigate what happens when you try to copy a movie file that is still open. And we'll need to know something about the file-format for the movie and its header, since we may need to alter it.
  13.  
  14. Freeball1 has pointed out that in Bizhawk there is a "Save Movie" option in the menu. So we could simply instruct the players to bind "Y" as the hotkey for Save Movie, which is the same key that players press to submit a run. So whenever a player hits the submit button they are also saving the movie, which finalizes the movie's header and flushes it to disk.
  15.  
  16. This is a quick and easy solution. Freeball1 has already made a version of the SGL Lua script that works with Bizhawk, and he has adjusted the timer HUD so that it fits the Gameboy screen. It seems to work well enough that we could try it out in an upcoming episode. With this solution, all that would be involved in porting the script to other consoles (within Bizhawk) is adjusting the timer HUD so that it fits the screen.
  17.  
  18. Omnigamer has proposed a different solution, also using Bizhawk: instead of using the emulator's built-in movie feature, we handle the input recording ourselves within the Lua script. We keep all the input data in our own buffer, and whenever the player submits a run we generate a Bizhawk movie file with the appropriate format. Omnigamer has done some research into the file format, and this seems like something we can do. Though we might move the actual task of generating that file into the sgl-upload.exe tool instead of doing it within Lua, since that would let us write that part of the code in a "real" language instead of Lua.
  19.  
  20. The reason I like this solution in the longer term -- handling the movie recording ourselves -- is that it eliminates all the setup steps that the player has to go through each time they use it. They'd only have to run the script and everything would be taken care of.
  21.  
  22. That said though, if anyone wants to port the current version of the SGL script to any other emulator such as VBA, Gens, MAME, etc, I would be happy to use it on the show.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement