Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active August 16, 2018 16:06
Show Gist options
  • Save vijinho/d0e7a8b1dcfc8924ff384d7c602ee965 to your computer and use it in GitHub Desktop.
Save vijinho/d0e7a8b1dcfc8924ff384d7c602ee965 to your computer and use it in GitHub Desktop.
PHP script to generate FS-UAE .ini config files from folder/subfolders of Commodore Amiga .adf/.afz files
<?php
// script to generate fs_uae configs for amiga floppies on mac os x or linux (.fs-uae files) as a zip archive
//
// first organise games with more than one disk into a subfolder containing the disks
// this will auto-load each extra floppy drive drive with each floppy (up to 4)
// e.g. make a folder 'alien_breed' containing abreed1.adz and abreed2.adz, config created is: Alien_Breed.fs-uae
// run 'php 0make_uae_configs.php' in Floppies folder to generate .fs-uae configs in folder 0configs to manually copy/move
//
// place games with multiple floppies under a subfolder per game
// author: [email protected]
// gist: https://gist.github.com/vijinho/
// github: https://github.com/vijinho/
// config for uae to save
// see https://fs-uae.net/configuration-files
// https://fs-uae.net/performance
$zip = 'configs.zip'; // leave empty for no zipfile generation
$fs_uae_config_default = <<<EOT
[fs-uae]
amiga_model = A500
kickstart_file = kick13.rom
cpu = 68000
jit_compiler = 0
chip_memory = 1024
slow_memory = 512
video_sync = off
accuracy = 1
floppy_drive_speed = 800
floppy_drive_volume_empty = 15
EOT;
// get list of disks
$cmd = 'find . -type f -iname "*.ad*" | sort -i';
$disks = cmd_execute($cmd);
if (empty($disks)) {
die('No disks *.ad* found.');
}
$folders = []; // folders containing floppies
$floppies = []; // single floppies
$configs = []; // config files to write out $filename => $data
foreach ($disks as $disk) {
$disk = substr($disk, 2); // get rid of './' from find command at start of disk name
if (empty($disk)) {
continue;
}
$last_slash = strrpos($disk, '/');
if (false !== $last_slash) {
$folder = substr($disk, 0, $last_slash);
$floppy = substr($disk, 1 + $last_slash);
$folders[$folder][] = $floppy; // add floppy to folders array
} else {
$floppy = $disk;
$floppies[$disk] = $floppy;
}
}
// generate fs-uae single floppy configs
foreach ($floppies as $k => $floppy) {
$fs_uae_config = $fs_uae_config_default;
$fs_uae_config .= "\nfloppy_drive_0 = $floppy\nfloppy_drive_count = 1\n";
$config_filename = basename(strtolower($floppy), '.adf');
$config_filename = str_replace(' ' , '_', trim(ucwords(str_replace(['_adf', '_', '&'], ['', ' ', ' and '], $config_filename))));
$configs[$config_filename . '.fs-uae'] = $fs_uae_config;
}
// generate configs for multi-disk games
foreach ($folders as $folder => $floppies) {
$fs_uae_config = $fs_uae_config_default;
// count floppies required
$nfloppies = count($floppies);
if ($nfloppies > 4) {
echo "WARNING: $folder has over 4 floppies - $nfloppies\n";
}
$fs_uae_config .= "\nfloppy_drive_count = " . ($nfloppies > 4) ? 4 : $nfloppies;
foreach ($floppies as $i => $floppy) {
$fs_uae_config .= "\nfloppy_drive_$i = $folder/$floppy";
$fs_uae_config .= "\nfloppy_image_$i = $folder/$floppy";
}
// filename for fs_uae
$config_filename = basename(strtolower($folder));
$config_filename = str_replace(' ' , '_', trim(ucwords(str_replace(['_', '&'], [' ', ' and '], $config_filename))));
$configs[$config_filename . '.fs-uae'] = $fs_uae_config;
}
// generate configs
echo "Configs to write: " . count($configs) . "\n";
shell_execute('mkdir -p 0configs/special'); // special folder is custom configs
foreach ($configs as $filename => $data) {
echo "Writing config: $filename\n";
file_put_contents('0configs/' . $filename, $data);
}
if (!empty($zip)) {
echo "Compressing configs into fs-uae-configs.zip\n";
shell_execute("cd 0configs && zip -dc -m -R fs-uae-configs.zip *.fs-uae");
}
echo "\nDone!\n";
exit;
/**
* Execute a command and return streams as an array of
* stdin, stdout, stderr
*
* @param string $cmd command to execute
* @return mixed array $streams | boolean false if failure
* @see https://secure.php.net/manual/en/function.proc-open.php
*/
function shell_execute($cmd)
{
$process = proc_open(
$cmd,
[
['pipe', 'r'],
['pipe', 'w'],
['pipe', 'w']
],
$pipes
);
if (is_resource($process)) {
$streams = [];
foreach ($pipes as $p => $v) {
$streams[] = stream_get_contents($pipes[$p]);
}
proc_close($process);
return [
'stdin' => $streams[0],
'stdout' => $streams[1],
'stderr' => $streams[2]
];
}
return false;
}
/**
* Execute a command and return output of stdout or throw exception of stderr
*
* @param string $cmd command to execute
* @param boolean $split split returned results? default on newline
* @param string $exp regular expression to preg_split to split on
* @return mixed string $stdout | Exception if failure
* @see shell_execute($cmd)
*/
function cmd_execute($cmd, $split = true, $exp = "/\n/") {
$result = shell_execute($cmd);
if (!empty($result['stderr'])) {
throw new Exception($result['stderr']);
}
$data = $result['stdout'];
if (empty($split) || empty($exp) || empty($data)) {
return $data;
}
return preg_split($exp, $data);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment