Script to replace character names in .story/.character files
Posted: Thu Oct 25, 2018 2:12 am
So I'm working on a custom story that I might want to eventually release, but at the same time I wanted to personalize with my own character names/text.
Initially I tried using the translation files to do this but editing them was cumbersome with the mix of unix/windows line endings. What I ended up doing was just writing a PHP script that I can run in Mods/Stories/My Custom Story that will do all the replacements for me.
Figured I'd throw it up here in case anyone was interested.
This lets me write all my dialog/thought bubbles/narration/inspect text using the original character names, and then customize them after Exporting.
Initially I tried using the translation files to do this but editing them was cumbersome with the mix of unix/windows line endings. What I ended up doing was just writing a PHP script that I can run in Mods/Stories/My Custom Story that will do all the replacements for me.
Figured I'd throw it up here in case anyone was interested.
This lets me write all my dialog/thought bubbles/narration/inspect text using the original character names, and then customize them after Exporting.
Code: Select all
<?php
// Changes content in the character display names, quests, and all dialogues
$replacements = array(
"Ashley" => "Girl 1",
"Katherine" => "Girl 2",
"Frank" => "Guy 1",
"Madison" => "Girl 3",
"Rachael" => "Girl 4",
"Patrick" => "Guy 2"
);
function do_replacements($text, $r) {
$decoded_text = iconv("UTF-16LE", "UTF-8", $text);
foreach (explode(PHP_EOL, $decoded_text) as $line) {
if (preg_match('/("DisplayName"|"Text"|"Name")/', $line)) {
$line = str_replace(array_keys($r), array_values($r), $line);
} elseif (preg_match('/("Value"): "(.*)"/', $line, $m)) {
// Need to avoid Value's for actions like Walkto and Event Trigger names
// As well as item's like "Ashley's Panties" or "Madison's Phone"
// Will skip events like 'AshleySex' but break events named "Ashley's Sexy Time"
if ((!in_array($m[2], array_keys($r)) && !stristr($m[2], "'s ")) || str_word_count($m[2]) > 3) {
$line = str_replace(array_keys($r), array_values($r), $line);
}
}
$lines[] = $line;
}
$text = iconv("UTF-8", "UTF-16LE", implode(PHP_EOL, $lines));
return $text;
}
$dir = '.';
if (is_dir($dir)) {
if ($dh = opendir($dir)) {
while (($file = readdir($dh)) !== false) {
$f = pathinfo($file);
if (in_array($f['extension'], array('character', 'story'))) {
$file_contents = file_get_contents($file);
$file_contents = do_replacements($file_contents, $replacements);
file_put_contents($file,$file_contents);
}
}
closedir($dh);
}
}