Skip to content

Instantly share code, notes, and snippets.

@SirFlobo
Created March 18, 2024 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SirFlobo/23f4b04db1e26d71185f55aa40a78a5a to your computer and use it in GitHub Desktop.
Save SirFlobo/23f4b04db1e26d71185f55aa40a78a5a to your computer and use it in GitHub Desktop.
Looks recursively through a folder with funscripts and fixes the range value
import os
import json
dryRun = True #Looks trough all the files but does not change anything
logChanges = True #Write a line to output when a file with mismatching range is found | Always prints when dryRun == True
makeBackup = True #Make a copy of the original script with .orig extenstion
newRange = 100 #New range to set in script file, -1 means use the maximum pos found in the script
def fixRangeFolder(path):
for entry in os.scandir(path):
if(entry.is_dir()):
fixRangeFolder(entry.path)
elif(entry.is_file() and os.path.splitext(entry.name)[1] == ".funscript"):
og_funscriptFile = open(entry.path, 'r', encoding="utf-8")
og_funscriptData = json.load(og_funscriptFile)
og_funscriptFile.close()
if "range" in og_funscriptData and og_funscriptData["range"] < 100:
if not dryRun and makeBackup:
backupFile = open(entry.path+".orig", 'w')
json.dump(og_funscriptData, backupFile)
backupFile.close()
maxAction = 0
origRange =og_funscriptData["range"]
for action in og_funscriptData["actions"]:
if action["pos"] > maxAction:
maxAction = action["pos"]
if not dryRun:
new_funscriptFile = open(entry.path, 'w', encoding="utf-8")
og_funscriptData["range"] = maxAction if newRange == -1 else newRange
json.dump(og_funscriptData, new_funscriptFile)
new_funscriptFile.close()
if logChanges or dryRun:
print(f"Modified range in file {og_funscriptFile.name}\nRange in file({entry.path}): {origRange}, max pos in data {maxAction}, new range: {og_funscriptData['range'] if not dryRun else 'not set'}")
if __name__ == "__main__":
if dryRun:
print("Dry run enable, not changes will be made")
fixRangeFolder(os.path.curdir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment