Skip to content

Instantly share code, notes, and snippets.

@neilpopham
Created September 2, 2019 10:33
Show Gist options
  • Save neilpopham/baae794e22251c83afe7520c621246d8 to your computer and use it in GitHub Desktop.
Save neilpopham/baae794e22251c83afe7520c621246d8 to your computer and use it in GitHub Desktop.
PHP script to parse a TIC 80 lua file and replace requires() with the contents of the files required. Use before saving as .tic.
<?php
$strRoot = "C:\\Users\\Neil\\AppData\\Roaming\\com.nesbox.tic\\TIC-80\\";
$strMain = file_get_contents($argv[1]);
if (preg_match_all('/^require \"(.+?)\"$/m', $strMain, $arrMatches)) {
foreach ($arrMatches[1] as $i => $strFile) {
$strPath = $strRoot . '\\' . str_replace(["carts/", "\/"], ["", "\\"], $strFile) . ".lua";
$strRequire = file_get_contents($strPath);
$strRequire = trim($strRequire) . "\n";
$strRequire = "-- {$strFile}.lua\n\n" . $strRequire;
$strMain = str_replace(
$arrMatches[0][$i],
$strRequire,
$strMain
);
}
file_put_contents(str_replace(".lua", ".merged.lua", $argv[1]), $strMain);
}
@neilpopham
Copy link
Author

To clarify:

  • Executes using > php merge-requires.php foo.lua
  • Finds any require "bar" lines in foo.lua, and replaces them with the contents of "bar.lua".
  • I have a symbolic link called "carts" in the folder that holds tic80.exe that targets "C:\Users\Neil\AppData\Roaming\com.nesbox.tic\TIC-80", where all my lua files are (in various subfolders). This allow the require() to work. Line 8 may need some changing if yours is different.
  • Will create foo.meged.lua in the same folder as foo.lua

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment