Page 1 of 1

Edit Train Schedules via PHP

Posted: Sat Jan 21, 2023 8:32 pm
by ptx0
What is it
I have grown weary of manually updating complicated train schedules because we can't merely point a conditional to a new station name. Who wants to have to delete all the entries to recreate the whole schedule? Not I.

Blueprint strings are just base64-encoded zlib-compressed JSON strings with a version byte prepended (0).

This script allows finding & replacing strings in the station name as well as item conditional.

Easily change a station from "[item=logistic-science-pack] Green Science Load [item=logistic-science-pack]", to "[item=chemical-science-pack] Blue Science Load [item=chemical-science-pack]" just by exporting and importing a blueprint!

To use it:
1. Place a file called "bp.txt" with the blueprint string of a locomotive with a train schedule you wish to update, into the same directory as this script
2. Update and/or add entries to the to_find and to_replace array with a piece of, or, the entire station name you wish to replace.
In the example below, it finds "Green" in "Green Science" and replaces with "Blue", for "Blue Science"
3. Update and/or add entries to the to_find and to_replace array with an in-game item name, if you wish to update "Cargo count" conditionals to reference a different item.
In the example below, it finds "logistic-science-pack" and replaces it with "chemical-science-pack"

Code: Select all

<?php

$path = __DIR__ . '/bp.txt';
$j = get_blueprint($path);

$to_find = [
	'Green',
	'logistic-science-pack'
];
$to_replace = [
	'Blue',
	'chemical-science-pack'
];

$schedules = &$j['blueprint']['schedules'];
foreach ($schedules as $id => &$sched) {
	$s = &$sched['schedule'];
	foreach ($s as $order => &$stop) {
		$station_name = &$stop['station'];
		echo 'Station: ' . $station_name . PHP_EOL;
		foreach ($to_find as $find_key => $find_string) {
			if (strpos($find_string, $station_name) !== 0) {
				$new_station = str_replace($find_string, $to_replace[$find_key], $station_name);
				$stop['station'] = $new_station;
				echo 'Changed station name to ' . $new_station . PHP_EOL;
			}
			foreach ($stop['wait_conditions'] as $condition_key => &$condition) {
				
				if (isset($condition['condition']['first_signal']['type']) && $condition['condition']['first_signal']['type'] === 'item' && $condition['condition']['first_signal']['name'] === $find_string) {
					$condition['condition']['first_signal']['name'] = str_replace($find_string, $to_replace[$find_key], $condition['condition']['first_signal']['name']);
					echo 'Changed condition to ' . $condition['condition']['first_signal']['name'] . PHP_EOL;
				}
			}
		}
	}
	echo PHP_EOL;
}

$new_path = __DIR__ . '/new-bp.txt';
create_blueprint($new_path, $j);

echo PHP_EOL;

function get_blueprint($path) {
	$txt = file_get_contents($path);
	$no_version = substr($txt, 1);
	$b64 = base64_decode($no_version);
	$d = zlib_decode($b64);
	file_put_contents(__DIR__ . '/bp.json', $d);
	$j = json_decode($d, true);

	return $j;
}

function create_blueprint($path, $data) {
	$j = json_encode($data);
	file_put_contents(__DIR__ . '/new-bp.json', $j);
	$d = zlib_encode($j, ZLIB_ENCODING_DEFLATE, 9);
	$b64 = base64_encode($d);
	$added_version = '0' . $b64;

	return file_put_contents($path, $added_version);
}