Advertisement
Guest User

modified WRECKER

a guest
Mar 13th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.62 KB | Source Code | 0 0
  1. import sys
  2. sys.dont_write_bytecode = True
  3.  
  4. from os.path import split as path_split, exists as path_exists
  5. import re
  6. try:
  7.     import colorama
  8.     colorama.init()
  9.     COLORAMA = ('\x1b[0m', '\x1b[31m', '\x1b[32m', '\x1b[33m', '\x1b[34m', '\x1b[35m', '\x1b[36m', '\x1b[37m')
  10. except:
  11.     COLORAMA = ('', '', '', '', '', '', '', '')
  12.  
  13. headers = [
  14.     'header_animations.py',
  15.     'header_common.py',
  16.     'header_dialogs.py',
  17.     'header_factions.py',
  18.     'header_game_menus.py',
  19.     'header_ground_types.py',
  20.     'header_item_modifiers.py',
  21.     'header_items.py',
  22.     'header_map_icons.py',
  23.     'header_meshes.py',
  24.     'header_mission_templates.py',
  25.     'header_mission_types.py',
  26.     'header_music.py',
  27.     'header_operations.py',
  28.     'header_particle_systems.py',
  29.     'header_parties.py',
  30.     'header_postfx.py',
  31.     'header_presentations.py',
  32.     'header_quests.py',
  33.     'header_scene_props.py',
  34.     'header_scenes.py',
  35.     'header_skills.py',
  36.     'header_skins.py',
  37.     'header_sounds.py',
  38.     'header_strings.py',
  39.     'header_tableau_materials.py',
  40.     'header_terrain_types.py',
  41.     'header_triggers.py',
  42.     'header_troops.py'
  43. ]
  44. modules = [
  45.     'module_animations.py',
  46.     'module_constants.py',
  47.     'module_dialogs.py',
  48.     'module_factions.py',
  49.     'module_game_menus.py',
  50.     'module_info.py',
  51.     'module_info_pages.py',
  52.     'module_items.py',
  53.     'module_map_icons.py',
  54.     'module_meshes.py',
  55.     'module_mission_templates.py',
  56.     'module_music.py',
  57.     'module_particle_systems.py',
  58.     'module_parties.py',
  59.     'module_party_templates.py',
  60.     'module_postfx.py',
  61.     'module_presentations.py',
  62.     'module_quests.py',
  63.     'module_scene_props.py',
  64.     'module_scenes.py',
  65.     'module_scripts.py',
  66.     'module_simple_triggers.py',
  67.     'module_skills.py',
  68.     'module_skins.py',
  69.     'module_sounds.py',
  70.     'module_strings.py',
  71.     'module_tableau_materials.py',
  72.     'module_triggers.py',
  73.     'module_troops.py'
  74. ]
  75.  
  76. headers_package = path_exists('./headers')
  77.  
  78. ## unused: [, 'tableau_': 'tableau.' 'spr_': 'spr.', ] all quoted references
  79. identifiers = [
  80.   'icon',
  81.   'prsnt',
  82.   'p',
  83.   'psys',
  84.   'mesh',
  85.   'itm',
  86.   'qst',
  87.   'pt',
  88.   'trp',
  89.   'ip',
  90.   'mt',
  91.   'anim',
  92.   'pfx',
  93.   'snd',
  94.   'scn',
  95.   'mnu',
  96.   'imod',
  97.   'skl',
  98.   'script',
  99.   'track',
  100.   'fac'
  101. ]
  102. info = "\n\n\n\n###################################\n#   W.R.E.C.K. Compiler Options   #\n###################################\n\n\n\n# Change this line to select where compiler will generate ID_* files. Use None instead of the string to completely suppress generation of ID_* files.\n# ONLY DO THIS WHEN YOU HAVE COMPLETELY REMOVED ID_* FILE DEPENDENCIES IN MODULE SYSTEM!\n\nwrite_id_files = \"ID_%s.py\"     # default vanilla-compatible option\n#write_id_files = \"ID/ID_%s.py\" # will put ID_* files in ID/ subfolder of module system's folder\n#write_id_files = None          # will suppress generation of ID_*.py files\n\n\n\n# Set to True to display compiler performance information at the end of compilation. Set to False to suppress.\n\nshow_performance_data = False   # default: false\n\n\n\n##########################\n#   W.R.E.C.K. Plugins   #\n##########################\n\n\n\nimport plugin_ms_extension\nimport plugin_presentations\n\n#Plugins from `systems` subfolder are loaded automatically\nfrom systems import *"
  103.  
  104.  
  105.  
  106. quote = '"'
  107. score = "_"
  108.  
  109. def check_file(string1, string2):
  110.     is_found = string1.find(string2)
  111.     if is_found != -1:
  112.         return True
  113.     else:
  114.         return False
  115.  
  116. def find_old_ref(string):
  117.     if string.lstrip().startswith("#"):
  118.         return string
  119.  
  120.     for key in identifiers:
  121.         string = re.sub(r'([ ,\(\[]' + key + r')_([a-zA-Z_])', r'\1.\2', string)
  122.  
  123.     return string
  124.  
  125. ## Deprecated; All string references to remain quoted, as they contain special characters(=, +, etc.) which mess with the compiler otherwise.
  126. # def fix_string_refs(string):
  127. #   first = special_str.sub('s.1_', string)
  128. #   string = first
  129. #   find_str = re.compile(r'[\s|\S]' + 'str_')
  130. #   non_ref = re.compile(r'[\w.]' + 'str_' + r'[_]')
  131. #   found = find_str.findall(string)
  132. #   references = [f for f in found if not non_ref.match(f)]
  133. #   for ref in references:
  134. #     isquote = find_char(ref, quote)
  135. #     thing = string.find(ref)
  136. #     skip = not_str.findall(string)
  137. #     if isquote == -1:
  138. #       quoted = string.find('"str_')
  139. #       next_quote = string.find(quote, quoted+1)
  140. #       quoted_reference = string[quoted:next_quote+1]
  141. #       new_ref = quoted_reference.strip(quote).replace('str_', 's.', 1)
  142. #       new = string.replace(quoted_reference, new_ref)
  143. #       string = new
  144. #     elif not_str.match(string[thing+1:]):
  145. #       pass
  146. #     else:
  147. #       new = re.sub('str_', 's.', ref)
  148. #       final = string.replace(ref, new, 1)
  149. #       string = final
  150. #   return string
  151.  
  152.  
  153. def wrecker(filename):
  154.     with open(filename, "r", encoding='utf-8') as file:
  155.         lines = file.readlines()
  156.  
  157.     with open(filename, "w", encoding='utf-8') as file:
  158.         for line in lines:
  159.             start = find_old_ref(line)
  160.             line = start
  161.             # finish = fix_string_refs(line)
  162.             # line = finish
  163.             file.write("%s"%line)
  164.  
  165. def remove_imports(filename):
  166.     with open(filename, "r", encoding='utf-8') as file:
  167.         lines = file.readlines()
  168.     with open(filename,"w", encoding='utf-8') as file:
  169.         imports = []
  170.         for line in lines:
  171.             if not line.startswith("from"):
  172.                 # if not line.startswith("# -*- coding"):
  173.                 file.write("%s"%line)  
  174.             elif line.lower().find('bignum') != -1:
  175.                 file.write("%s"%line)
  176.  
  177. def add_import_compiler(filename):
  178.     with open(filename,"r", encoding='utf-8') as file:
  179.         lines = file.readlines()
  180.  
  181.     if lines[0].startswith("# -*- coding"):
  182.         i = 1
  183.     else:
  184.         i = 0
  185.  
  186.     lines.insert(i, "from compiler import *\n")
  187.     with open(filename,"w", encoding='utf-8') as file:
  188.         for line in lines:
  189.             file.write("%s"%line)
  190.  
  191. def add_wrecker_options(filename):
  192.  
  193.     test = "W.R.E.C.K. Compiler Options"
  194.  
  195.     with open(filename,"r") as file:
  196.         lines = file.readlines()
  197.  
  198.     if test not in lines:
  199.         print("expanding " + filename)
  200.         with open(filename, "a") as file:
  201.             file.write(info)
  202.  
  203.  
  204.  
  205. def print_menu():
  206.         print("")
  207.         print("{1}A note on syntax:{0}".format(*COLORAMA))
  208.         print("")
  209.         print("{2}    a) For files in the same directory, do not add any extra '.py'{0}".format(*COLORAMA))
  210.         print("{2}       Example: module_troops{0}".format(*COLORAMA))
  211.         print(" ")
  212.         print("{2}    b) For files in subdirectories, add the subdirectory with a '/' to{0}".format(*COLORAMA))
  213.         print("{2}       the beginning of the file name{0}".format(*COLORAMA))
  214.         print("{2}       Example: MDL/module_troops{0}".format(*COLORAMA))
  215.         print("")
  216.         print("{6}1) WRECK files{0}".format(*COLORAMA))
  217.         print("{6}2) Input a file to WRECK{0}".format(*COLORAMA))
  218.         print("{6}3) Expand module_info.py - unnecessary if (1) has been done.{0}".format(*COLORAMA))
  219.         print("{6}4) Print this menu again{0}".format(*COLORAMA))
  220.         print("{6}0) Exit{0}".format(*COLORAMA))
  221.         print("")
  222.  
  223. def process():
  224.  
  225.     legal_choices = set(["0","1","2","3","4"])
  226.     choice =""
  227.     errors = []
  228.     print_menu()
  229.     while choice != "0":
  230.         choice =""
  231.         while not choice in legal_choices:
  232.             if errors:
  233.                 for e in errors:
  234.                     print(e)
  235.                     print()
  236.                 errors = []
  237.  
  238.             choice = input("Make a choice (4 to display menu again) >").lower().strip()
  239.  
  240.             if choice == '1':
  241.                 print("processing...")
  242.                 for module in modules:
  243.                     try:
  244.                         f = open(module, encoding='utf-8')
  245.                         f.readlines()
  246.                         f.close()
  247.                     except IOError:
  248.                         errors.append("{2}Error: no such module {0}{1}".format(module, *COLORAMA))
  249.                     except UnicodeDecodeError:
  250.                         errors.append("{2}Error: wrong file ecnoding {0}, required 'Utf-8'{1}".format(module, *COLORAMA))
  251.                     else:
  252.                         print("Fixing references in " + module)
  253.                         wrecker(module)
  254.                         remove_imports(module)
  255.                         add_import_compiler(module)
  256.                        
  257.                 for header in headers:
  258.                     try:
  259.                         f = open(header, encoding='utf-8')
  260.                         f.readlines()
  261.                         f.close()
  262.                     except IOError:
  263.                         errors.append("{2}Error: no such header {0}{1}".format(header, *COLORAMA))
  264.                     except UnicodeDecodeError:
  265.                         errors.append("{2}Error: wrong file ecnoding {0}, required 'Utf-8'{1}".format(header, *COLORAMA))
  266.                     else:
  267.                         print("Fixing imports in " + header)
  268.                         remove_imports(header)
  269.  
  270.                 add_wrecker_options("module_info.py")
  271.                 print("")
  272.  
  273.             elif choice == '2':
  274.                 response = input('Input a file to process: ') + ".py"
  275.                 try:
  276.                     f = open(response)
  277.                     f.close()
  278.                 except IOError:
  279.                     print("No such file %s" % response)
  280.                 else:
  281.                     print("Fixing references in " + response)
  282.                     wrecker(response)
  283.                     remove_imports(response)
  284.                     add_import_compiler(response)
  285.                 print("")
  286.  
  287.             elif choice == '3':
  288.                 add_wrecker_options("module_info.py")
  289.                 print("")
  290.  
  291.             elif choice == '4':
  292.                 exit()
  293.  
  294. if __name__ == '__main__':
  295.     process()
  296.  
  297.  
Tags: WRECK Warband
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement