Advertisement
mjshi

Common Event Manager v1.0

Jan 13th, 2016
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. #----------------------------------------
  2. # Common Events Manager v1.0 by mjshi
  3. # Requires Python 3 to run.
  4. #----------------------------------------
  5.  
  6. import os #import os operations
  7. import re #import regex
  8.  
  9. def printHeader():
  10.   print("\n-------------------------------------------------------\n    RPG Maker MV Utility: Common Events Manager v1.0\n-------------------------------------------------------")
  11.  
  12. def pause(): #pauses and clears the screen
  13.   input("\n (Press ENTER to continue...)")
  14.   clear()
  15.  
  16. def clear():
  17.   os.system('cls' if os.name=='nt' else 'clear')
  18.   printHeader()
  19.  
  20. def move(files, folder):
  21.   for file in files:
  22.     try:
  23.       os.rename(file, folder + "\\" + file)
  24.     except FileExistsError:
  25.       os.remove(folder + "\\" + file)
  26.       os.rename(file, folder + "\\" + file)
  27.      
  28. def getFiles(directory): #gets all files in current directory
  29.   print("\n Searching for files in\n " + directory + "...\n Please wait.")
  30.   found = []
  31.   arbitrary = ["Actors.json", "Animations.json", "Armors.json", "Classes.json",
  32.                "Enemies.json", "MapInfos.json", "States.json", "System.json",
  33.                "Tilesets.json", "Weapons.json"]
  34.   for file in os.listdir(directory):
  35.     if file.endswith('.json') and not(file in arbitrary):
  36.       found.append(file)
  37.        
  38.   if len(found) > 0:
  39.     print("\n Found", len(found), "relevant .json files.")
  40.     print(found)
  41.   else:
  42.     print("\n Error: Could not find any relevant .json files.",
  43.           "\n Please make sure that this utility is placed in the project's data folder.")
  44.     pause()
  45.     exit()
  46.    
  47.   pause()
  48.   print("\n Original files will be put into a folder named 'backup' to ensure that",
  49.         "\n this program will not permanently overwrite any data.",
  50.         "\n\n Please make sure to keep the folder as backup until you have ensured",
  51.         "\n that nothing has been broken.")
  52.   try:
  53.     os.mkdir("backup")
  54.     pause()
  55.   except FileExistsError:
  56.     clear()
  57.     print("\n Backup directory already exists.",
  58.           "\n Continuing to run this program may overwrite existing backups.")
  59.     cont = input("\n  Continue? (y/n) ")
  60.     if cont != "y":
  61.       exit()
  62.     clear()
  63.   return found
  64.  
  65. def reorderCE(start, step, directory):
  66.   backup = directory + "\\backup"
  67.   common = re.compile(r'({"id":)([0-9]+?)(.*)')
  68.   rmv = re.compile(r'\{"id":[0-9]+?,"list":\[\{"code":0,"indent":0,"parameters":\[\]\}\],"name":"","switchId":1,"trigger":0\},')
  69.   rmvstr = '\{"id":[0-9]+?,"list":\[\{"code":0,"indent":0,"parameters":\[\]\}\],"name":"","switchId":1,"trigger":0\},'
  70.   rmvcount = 0
  71.  
  72.   file = open(backup + "\\CommonEvents.json", "r")
  73.   output = open("CommonEvents.json", "w")
  74.   output.truncate()
  75.  
  76.   for line in file:
  77.  
  78.     if step > 0:
  79.       if line.startswith("{\"id\":" + str(start)):
  80.         for i in range(step):
  81.           output.write("{\"id\":" + str(start + i) + ",\"list\":[{\"code\":0,\"indent\":0,\"parameters\":[]}],\"name\":\"\",\"switchId\":1,\"trigger\":0},")
  82.  
  83.       if re.search(common, line):
  84.         match = re.split(common, line)
  85.         for i in range(len(match)):
  86.           if match[i].isdigit() and int(match[i]) >= start:
  87.             match[i] = str(int(match[i]) + step)
  88.         line = ""
  89.         for i in match:
  90.           line += i
  91.     else:
  92.       while re.search(rmv, line) and rmvcount < abs(step):
  93.         line = re.sub(rmvstr, '', line, count = 1)
  94.         rmvcount += 1
  95.      
  96.     output.write(line)
  97.    
  98.   file.close()
  99.   output.close()
  100.  
  101.   #Move the file to the appropriate area
  102.   try:
  103.     os.rename(backup + "\\CommonEvents.json", backup + "\\CommonEvents.BAK")
  104.     os.rename(directory + "\\CommonEvents.json", backup + "\\CommonEvents.json")
  105.   except FileExistsError:
  106.     os.remove(backup + "\\CommonEvents.BAK")
  107.     os.rename(backup + "\\CommonEvents.json", backup + "\\CommonEvents.BAK")
  108.     os.rename(directory + "\\CommonEvents.json", backup + "\\CommonEvents.json")  
  109.  
  110. #initialize regex for event and effect types
  111. event = re.compile(r'(\{"code":117,"indent":[0-9]+?,"parameters":\[)([0-9]+?)(\]\})')
  112. effect = re.compile(r'(\{"code":44,"dataId":)([0-9]+?)(,"value1":[0-9]+?,"value2":[0-9]+?\})')
  113.  
  114. directory = os.getcwd() #sets the directory
  115. backup = directory + "\\backup"
  116.  
  117. printHeader()
  118.  
  119. found = getFiles(directory) #creates list of relevant files
  120.  
  121. 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.",
  122.       "\n\n Both positive and negative increments are supported.",)
  123. start = int(input("\n Starting event number: "))
  124. step = int(input(" Increment by: "))
  125.  
  126. move(found, backup)
  127. reorderCE(start, step, directory)
  128.  
  129. done = 1
  130. clear()
  131.  
  132. for item in found:
  133.   file = open(backup + "\\" + item, "r")
  134.   output = open(item, "w")
  135.   output.truncate()
  136.   for line in file:
  137.     if re.search(event, line):
  138.       match = re.split(event, line)
  139.       for i in range(len(match)):
  140.         if match[i].isdigit() and int(match[i]) >= start:
  141.           match[i] = str(int(match[i]) + step)
  142.       line = ""
  143.       for i in match:
  144.         line += i
  145.        
  146.     if re.search(effect, line):
  147.       match = re.split(effect, line)
  148.       for i in range(len(match)):
  149.         if match[i].isdigit() and int(match[i]) >= start:
  150.           match[i] = str(int(match[i]) + step)
  151.       line = ""
  152.       for i in match:
  153.         line += i
  154.  
  155.     output.write(line)
  156.    
  157.   #Incremental variables to track progress
  158.   clear()
  159.   print("\n ", done, "out of", len(found), "processed.")
  160.   file.close()
  161.   output.close()
  162.   done += 1
  163.  
  164. os.remove(backup + "\\CommonEvents.json")
  165. os.rename(backup + "\\CommonEvents.BAK", backup + "\\CommonEvents.json")
  166.  
  167. print("\n  Operation complete.")
  168. pause()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement