# if rl.disable_readline: # def dummy(completer=""): # pass # for funk in __all__: # globals()[funk] = dummy # sys.exit(0) # else: try: from pyreadline.rlmain import Readline from pyreadline import console # create a Readline object to contain the state rl = Readline() def GetOutputFile(rl): """Return the console object used by readline so that it can be used for printing in color.""" return rl.console __all__.append("GetOutputFile") # make these available so this looks like the python readline module read_init_file = rl.read_init_file parse_and_bind = rl.parse_and_bind clear_history = rl.clear_history add_history = rl.add_history insert_text = rl.insert_text write_history_file = rl.write_history_file
for v in reversed(self._values): readline.add_history(v) def clear_values(self): self._values = [] readline.clear_history() class NoCompleter(object): def set_values(self, values): pass def clear_values(self): pass # try: if platform.system().lower() == 'windows': from pyreadline.rlmain import Readline import pyreadline.console as console readline = Readline() console.install_readline(readline.readline) else: import readline except: completer = NoCompleter() readline = None else: completer = PromptCompleter() readline.set_completer(completer.complete) # if 'libedit' in readline.__doc__: readline.parse_and_bind('bind ^I rl_complete') readline.parse_and_bind('bind ^F em-inc-search-prev') readline.parse_and_bind('bind ^R em-inc-search-next') # hack for the equivalent of set_completer_delims():
''' Created on Mar 1, 2014 @author: sgeen ''' import os, copy, textwrap, time, inspect import LevelData, Verbs, Eat, Talk, Use, Get, Animals, Examine import CmdVars, antigravity, Level hasrl = True if os.name != "posix": try: import pyreadline from pyreadline.rlmain import Readline readline = Readline() except: hasrl = False # readline bugged out; turn off else: import readline import cPickle as pik from CmdVars import cmdvars debug = False speech = r"'''" titletext = ''' HACK AND SLASH: An Adventure in Cybercity Created on Mar 01, 2014 @author: Sam Geen
def main(input_args): path = os.path.join(os.getcwd(), "complete_particle_list.txt") rl = Readline() rl.parse_and_bind("control-v: paste") if "--help" in input_args or "-h" in input_args: print("\nPossible Parameters:") print("-h|--help a list of all possible parameters") print( "-b|--build retrieve all particles from the content folder" ) print("-p|--path set the path for the content folder") print("--force_parse force to parse built particles again") print("-l_[max]|--limit_[max] set a maximum of particles to process") print( "--dump writes all particle information in particle_dump.json" ) print("--count print the count of all particles") print( "--operator write a list of all operators in result.txt" ) print( "--operator_count write a list of all operators with their total number of occurances in result.txt" ) print( "--operator_example write a list of all operators with an random example in result.txt" ) print( "--operator_fields write a list of all operators with all their fields and types in result_fields.txt" ) print( "--field_types write a list of all fields types with count in result_types.txt" ) print("--search_field_value Searches for particles with tath field") print("\nValve Resource Format support:") print( "--vrf_path set the folder to search for 'Decompiler.exe'" ) print("-g|--game_path set the path for the game folder") print( "--vrf_build include compiled particles in search and decompile them" ) print( "--vrf use decompiled particles (doesn't decompile them)" ) sys.exit() if len(input_args) == 0: print("\nNo arguments given. Abort...") print("Refer to help (-h|--help) for more information about arguments") sys.exit() if "--path" in input_args or "-p" in input_args: cPath = "" while cPath == "": cPath = input("Content Path: ") if not os.path.exists(cPath): print("This path does not exist. Exit...") sys.exit() else: set_content_path(cPath) if "--game_path" in input_args or "-g" in input_args: gPath = "" while gPath == "": gPath = input("Game Path: ") if not os.path.exists(gPath): print("This path does not exist. Exit...") sys.exit() else: set_game_path(gPath) useVrf = False buildVrf = False if "--vrf_build" in input_args: useVrf = True buildVrf = True vrfPath = get_vrf_path() if not vrfPath: if get_user_input("No path for vrf decompiler given.", useYN=True): if get_user_input("Use current directory?", useYN=True, preset=False): set_vrf_path(os.getcwd()) else: newPath = get_user_input("Enter path to search: ") set_vrf_path(newPath) vrfPath = get_vrf_path() if not vrfPath: print("No Decompiler.exe found. Exit...") sys.exit() if not useVrf: if "--vrf" in input_args: useVrf = True if "--build" in input_args or "-b" in input_args: build_main_file(useVrf, buildVrf) if not os.path.isfile(path): print("WARNING! complete_particle_list.txt does not exist!") answer = None while answer is None: userInput = input("Do you want to build it now? (y|n) ") if userInput.lower() == "y" or userInput.lower() == "yes": answer = True elif userInput.lower() == "n" or userInput.lower() == "no": answer = False if answer: build_main_file(useVrf, buildVrf) else: print("Exit...") sys.exit() limit = -1 for arg in input_args: match = re.search(r'(--limit|-l)_(\d+)', arg) if match: limit = int(match.group(2)) if "-p" in input_args: input_args.remove("-p") if "--path" in input_args: input_args.remove("--path") if len(input_args) == 0: sys.exit() forceParse = False if "--force_parse" in input_args: forceParse = True particles = parse_main_file(limit, forceParse) text = "" resDict = {} if "--count" in input_args: print("\n\nCOUNT: " + str(len(particles))) if "--dump" in input_args: text = "" newDict = {} for k, v in particles.items(): newDict[k] = v["content"].to_json() text += json.dumps(newDict, indent=4) with open(os.path.join(os.getcwd(), "particle_dump.txt"), "w") as file: file.write(text) if "--operator" in input_args: for pName, particle in particles.items(): pItem = particle["content"] for item in pItem.get_objects(): opName = item.get_name() if opName != "": if not opName in resDict: resDict[opName] = {} if "--operator_count" in input_args: for pName, particle in particles.items(): pItem = particle["content"] for item in pItem.get_objects(): opName = item.get_name() if opName != "": if not opName in resDict: resDict[opName] = {} if not "count" in resDict[opName]: resDict[opName]["count"] = 1 else: resDict[opName]["count"] += 1 if "--operator_example" in input_args: for pName, particle in particles.items(): pItem = particle["content"] for item in pItem.get_objects(): opName = item.get_name() if opName != "": if not opName in resDict: resDict[opName] = {} resDict[opName]["example"] = particle["path"] if not "example" in resDict[opName]: resDict[opName]["example"] = particle["path"] else: if randrange(0, 100) < 2000 / len(particles): resDict[opName]["example"] = particle["path"] if "--operator_count" in input_args: resDict = { k: v for k, v in sorted( resDict.items(), key=lambda x: x[1]["count"], reverse=True) } for opName, opDict in resDict.items(): countText = "" exampleText = "" if "count" in opDict: opCount = opDict["count"] tabCount = (1 if opCount >= 1000 else 2) + 1 countText = str(opCount) + "\t" * tabCount if "example" in opDict: nameLen = len(opName.replace("\"", "")) nameTabs = "\t" * (12 - int(nameLen / 4)) exampleText = nameTabs + opDict["example"] text += countText + opName.replace("\"", "") + exampleText + "\n" if text != "": with open(os.path.join(os.getcwd(), "result.txt"), "w") as file: file.write(text) if "--operator_fields" in input_args: opDict = {} for pName, particle in particles.items(): pItem = particle["content"] for item in pItem.get_objects(): opName = item.get_name() newDict = item.to_json("type") if len(newDict) > 0: if not opName in opDict: opDict[opName] = {} opDict[opName].update(newDict) text = json.dumps(opDict, indent=4) if text != "": with open(os.path.join(os.getcwd(), "result_fields.txt"), "w") as file: file.write(text) if "--field_types" in input_args: fDict = {} for pName, particle in particles.items(): pItem = particle["content"] for item in pItem.get_objects(): for field in item.get_properties(): fType = field.get_type() if not fType in fDict: fDict[fType] = 1 else: fDict[fType] += 1 fDict = { k: v for k, v in sorted(fDict.items(), key=lambda x: x[1], reverse=True) } text = "" for typeName, count in fDict.items(): countLen = len(str(count)) countTabs = "\t" * (2 - int(countLen / 4)) text += str(count) + countTabs + typeName + "\n" with open(os.path.join(os.getcwd(), "result_types.txt"), "w") as file: if text != "": file.write(text) if "--search_field_value" in input_args: name = "_" value = "" if len(input_args) > 1: name = input_args[1] if len(input_args) > 2: value = input_args[2] valString = "any value" if value != "": valString = " the value " + value print("Searching for " + name + " with " + valString) results = {} for pName, particle in particles.items(): pItem = particle["content"] for item in pItem.get_objects(): for fName, field in item.get_properties_with_name(): if fName == name: if value == "" or field.get_val() == value: # print(pName, fName, field.get_type()) results[pName] = field.get_val() if len(results) == 0: print("Nothing found") else: index = 0 maxIndex = 25 print("First " + str(min(maxIndex, len(results))) + " results:\n") for pName, fVal in results.items(): if index >= maxIndex: break index += 1 print(" - " + pName + "; with value \"" + fVal + "\"") print("\nDone.")