Skip to content

Instantly share code, notes, and snippets.

@Ellpeck
Created January 4, 2021 23:59
Show Gist options
  • Save Ellpeck/73e156fcf3f214d2faa567c58b41784b to your computer and use it in GitHub Desktop.
Save Ellpeck/73e156fcf3f214d2faa567c58b41784b to your computer and use it in GitHub Desktop.
A simple script to shuffle pk3DS text dumps with a lower chance of the game crashing (by only shuffling strings with similar lengths)
# shuffles a pokemon 3ds language document based on string length with a given leeway
# needs storytext.txt and gametext.txt in the same directory
from random import randrange
# amount of characters that two string lengths can be different by to allow shuffling
leeway = 3
def is_allowed_line(line):
return not line.startswith("Text File :") and not "~~~" in line and not "[" in line
def is_fitting_line(line, other):
return is_allowed_line(other) and abs(len(other) - len(line)) <= leeway
def shuffle(file):
print(f"Shuffling {file}")
with open(file + ".txt", "r", encoding="utf-16-le") as f:
lines = f.readlines()
# iterate through all lines and find a swap partner
for i in range(len(lines)):
line = lines[i]
if not is_allowed_line(line):
continue
other = find_shuffle_line(lines, line)
if other >= 0:
# swap
lines[i] = lines[other]
lines[other] = line
print(f"Shuffled {i} and {other}")
# write to file
with open(file + "_out.txt", "w", encoding="utf-16-le") as f:
f.writelines(lines)
print(f"Finished shuffling {file}")
def find_shuffle_line(lines, line):
# find all line indices with a similar length
possible = list(filter(lambda i: is_fitting_line(line, lines[i]), range(len(lines))))
if len(possible) <= 0:
return -1
# return a random line out of the ones with similar length
return possible[randrange(0, len(possible))]
shuffle("storytext")
shuffle("gametext")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment