Page 1 of 1

Script to replace character names in .story/.character files

Posted: Thu Oct 25, 2018 2:12 am
by snippy64
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.

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);
    }
}


Re: Script to replace character names in .story/.character files

Posted: Mon Dec 10, 2018 7:05 pm
by BlazR
Nicely done. Since most people don't have php installed (I'm assuming) I could whip up something as an AutoIt script (or executable), in C#, or even something in Java with a simple little interface if enough people were interested in it. It's pretty simple to accomplish code-wise as it's just opening a file, replacing text, and then writing the file out.

Re: Script to replace character names in .story/.character files

Posted: Tue Dec 11, 2018 3:55 am
by peter980
Wouldn't notepad++, with some regex in find/replace, do the same?

Re: Script to replace character names in .story/.character files

Posted: Tue Dec 11, 2018 7:33 am
by BlazR
peter980 wrote:
Tue Dec 11, 2018 3:55 am
Wouldn't notepad++, with some regex in find/replace, do the same?
Sure. There's plenty of ways to go about doing it.

Re: Script to replace character names in .story/.character files

Posted: Wed Dec 26, 2018 8:37 pm
by Ihop23
As above, not sure how I would run the PHP file. So a python script (or others) or find/replace regex instructions would be great!