Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #----------------------------------------
- # Common Events Manager v1.0 by mjshi
- # Requires Python 3 to run.
- #----------------------------------------
- import os #import os operations
- import re #import regex
- def printHeader():
- print("\n-------------------------------------------------------\n RPG Maker MV Utility: Common Events Manager v1.0\n-------------------------------------------------------")
- def pause(): #pauses and clears the screen
- input("\n (Press ENTER to continue...)")
- clear()
- def clear():
- os.system('cls' if os.name=='nt' else 'clear')
- printHeader()
- def move(files, folder):
- for file in files:
- try:
- os.rename(file, folder + "\\" + file)
- except FileExistsError:
- os.remove(folder + "\\" + file)
- os.rename(file, folder + "\\" + file)
- def getFiles(directory): #gets all files in current directory
- print("\n Searching for files in\n " + directory + "...\n Please wait.")
- found = []
- arbitrary = ["Actors.json", "Animations.json", "Armors.json", "Classes.json",
- "Enemies.json", "MapInfos.json", "States.json", "System.json",
- "Tilesets.json", "Weapons.json"]
- for file in os.listdir(directory):
- if file.endswith('.json') and not(file in arbitrary):
- found.append(file)
- if len(found) > 0:
- print("\n Found", len(found), "relevant .json files.")
- print(found)
- else:
- print("\n Error: Could not find any relevant .json files.",
- "\n Please make sure that this utility is placed in the project's data folder.")
- pause()
- exit()
- pause()
- print("\n Original files will be put into a folder named 'backup' to ensure that",
- "\n this program will not permanently overwrite any data.",
- "\n\n Please make sure to keep the folder as backup until you have ensured",
- "\n that nothing has been broken.")
- try:
- os.mkdir("backup")
- pause()
- except FileExistsError:
- clear()
- print("\n Backup directory already exists.",
- "\n Continuing to run this program may overwrite existing backups.")
- cont = input("\n Continue? (y/n) ")
- if cont != "y":
- exit()
- clear()
- return found
- def reorderCE(start, step, directory):
- backup = directory + "\\backup"
- common = re.compile(r'({"id":)([0-9]+?)(.*)')
- rmv = re.compile(r'\{"id":[0-9]+?,"list":\[\{"code":0,"indent":0,"parameters":\[\]\}\],"name":"","switchId":1,"trigger":0\},')
- rmvstr = '\{"id":[0-9]+?,"list":\[\{"code":0,"indent":0,"parameters":\[\]\}\],"name":"","switchId":1,"trigger":0\},'
- rmvcount = 0
- file = open(backup + "\\CommonEvents.json", "r")
- output = open("CommonEvents.json", "w")
- output.truncate()
- for line in file:
- if step > 0:
- if line.startswith("{\"id\":" + str(start)):
- for i in range(step):
- output.write("{\"id\":" + str(start + i) + ",\"list\":[{\"code\":0,\"indent\":0,\"parameters\":[]}],\"name\":\"\",\"switchId\":1,\"trigger\":0},")
- if re.search(common, line):
- match = re.split(common, line)
- for i in range(len(match)):
- if match[i].isdigit() and int(match[i]) >= start:
- match[i] = str(int(match[i]) + step)
- line = ""
- for i in match:
- line += i
- else:
- while re.search(rmv, line) and rmvcount < abs(step):
- line = re.sub(rmvstr, '', line, count = 1)
- rmvcount += 1
- output.write(line)
- file.close()
- output.close()
- #Move the file to the appropriate area
- try:
- os.rename(backup + "\\CommonEvents.json", backup + "\\CommonEvents.BAK")
- os.rename(directory + "\\CommonEvents.json", backup + "\\CommonEvents.json")
- except FileExistsError:
- os.remove(backup + "\\CommonEvents.BAK")
- os.rename(backup + "\\CommonEvents.json", backup + "\\CommonEvents.BAK")
- os.rename(directory + "\\CommonEvents.json", backup + "\\CommonEvents.json")
- #initialize regex for event and effect types
- event = re.compile(r'(\{"code":117,"indent":[0-9]+?,"parameters":\[)([0-9]+?)(\]\})')
- effect = re.compile(r'(\{"code":44,"dataId":)([0-9]+?)(,"value1":[0-9]+?,"value2":[0-9]+?\})')
- directory = os.getcwd() #sets the directory
- backup = directory + "\\backup"
- printHeader()
- found = getFiles(directory) #creates list of relevant files
- print("\n After running this utility, please open your project and press CTRL + S\n to ensure that RPG Maker MV will not reject the edits.",
- "\n\n Both positive and negative increments are supported.",)
- start = int(input("\n Starting event number: "))
- step = int(input(" Increment by: "))
- move(found, backup)
- reorderCE(start, step, directory)
- done = 1
- clear()
- for item in found:
- file = open(backup + "\\" + item, "r")
- output = open(item, "w")
- output.truncate()
- for line in file:
- if re.search(event, line):
- match = re.split(event, line)
- for i in range(len(match)):
- if match[i].isdigit() and int(match[i]) >= start:
- match[i] = str(int(match[i]) + step)
- line = ""
- for i in match:
- line += i
- if re.search(effect, line):
- match = re.split(effect, line)
- for i in range(len(match)):
- if match[i].isdigit() and int(match[i]) >= start:
- match[i] = str(int(match[i]) + step)
- line = ""
- for i in match:
- line += i
- output.write(line)
- #Incremental variables to track progress
- clear()
- print("\n ", done, "out of", len(found), "processed.")
- file.close()
- output.close()
- done += 1
- os.remove(backup + "\\CommonEvents.json")
- os.rename(backup + "\\CommonEvents.BAK", backup + "\\CommonEvents.json")
- print("\n Operation complete.")
- pause()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement