-
-
Save barkingdoginteractive/7b9fa886989fcff8596c9457be296a6f to your computer and use it in GitHub Desktop.
PHP script to assist with making PICO-8 games with multiple .lua files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This is a PHP script that allows you to | |
// edit a directory full of .lua files to | |
// code PICO-8 games. | |
// To use: | |
// | |
// Replace your game name below. If your game | |
// is knights.p8, use knights as your game name. | |
DEFINE('GAME_NAME', 'game'); | |
// Then, place this PHP script in the game folder | |
// (you can type 'folder' in PICO-8 to open it). | |
// Create a subfolder called src/. Place your | |
// .lua files in there. Create a backups/ folder; | |
// backups of your game will go in there. | |
// Whenever you want to inject your codebase into | |
// the .p8 file, run this script. From the | |
// command line, it will be something like: | |
// php build.php | |
// This script will concatenate the .lua files | |
// found in the src/ folder (in alpha order), | |
// massage them (see below), and inject them into the .p8 | |
// file. You can then load and run the .p8 file. | |
// You can use #LEFT# #RIGHT# #UP# #DOWN# #X# #O# | |
// as shorthands for those symbols in strings, and | |
// you can use tabs instead of spaces for indentation. | |
date_default_timezone_set( 'America/Denver' ); | |
DEFINE( 'BEFORE_LUA', 0 ); | |
DEFINE( 'DURING_LUA', 1 ); | |
DEFINE( 'AFTER_LUA', 2 ); | |
function include_lua( $results ) { | |
$files = glob( "src/*.lua" ); | |
sort( $files ); | |
foreach ($files as $filename) { | |
// $results .= '-- INCLUDED: ' . $filename . "\n\n"; | |
// $results .= "------------------------------------- " . $filename . "\n\n"; | |
// strip out comments | |
$code = file_get_contents( $filename ); | |
$lines = explode( "\n", $code ); | |
$cleanlines = array(); | |
foreach( $lines as $line ) { | |
$line = trim( $line ); | |
if (substr( $line, 0, 2 ) == "--") { continue; } | |
$cleanlines[] = $line; | |
} | |
$code = implode( "\n", $cleanlines ); | |
$code = str_replace( | |
array('#LEFT#','#RIGHT#','#UP#','#DOWN#','#X#','#O#',"\t"), | |
array('ã', 'ë', 'î', 'É', 'ó', 'é'," "), | |
$code); | |
$results .= $code . "\n"; | |
} | |
return $results; | |
} | |
$contents = file_get_contents( GAME_NAME . ".p8" ); | |
file_put_contents( "backups/" . GAME_NAME . "-" . date("y-m-d-G-i") . ".p8", $contents ); | |
$lines = explode( "\n", $contents ); | |
$result = ""; | |
$stream_stage = BEFORE_LUA; | |
foreach( $lines as $line ) { | |
switch( $stream_stage ) { | |
case BEFORE_LUA: | |
$result = $result . $line . "\n"; | |
if ($line == "__lua__") { | |
$result = include_lua( $result ); | |
$stream_stage = DURING_LUA; | |
} | |
break; | |
case DURING_LUA: | |
if ($line == "__gfx__") { | |
$stream_stage = AFTER_LUA; | |
$result = $result . $line . "\n"; | |
} | |
break; | |
case AFTER_LUA: | |
$result = $result . $line . "\n"; | |
} | |
} | |
file_put_contents( GAME_NAME . ".p8", $result ); | |
fputs(STDOUT, "Done.\n"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment