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

In this forum, you can ask questions and get help with the House Party Custom Story Creator. Not for Bug Reports. See the forum below for that.
Forum rules
Forum rules
No racism, sexism, anti-semitism, brigading, doxxing, or ANY form of abusive behavior. No discussion of hacking or piracy or the buying, trading, or selling of ANY type of product or service (digital or otherwise). No advertising of any product or service (digital or otherwise, and this includes streams or any service involving 'subscribers'). No discussion of religion or politics. Do not link to other adult sites, with the exception being discussion of other adult games in Offtopic/allowed subforums; i.e. no pornography.

Do not spam, do not try to circumvent any automated tools that are part of these forums or any linking to these forums. Do not judge others based on their preferences for custom stories, their opinions, sexuality, etc., and most certainly do not bring it to the moderators.

Respect the moderators and each other and all will be well. Report any violations of the above rules. Do not violate local or international laws (including, but not limited to the laws of your country and/or the laws of the United States) while making use of these forums. The IP address of all posts are recorded to aid in enforcing these conditions.

We will use username, e-mail, AND IP bans without reservation, and mods hold the final word in such decisions.
Post Reply
snippy64
Posts: 11
Joined: Sat Oct 13, 2018 4:51 am

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

Post by snippy64 » 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.

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


Post Reply