Skip to content

Commit

Permalink
feat: More commands for string manipulation
Browse files Browse the repository at this point in the history
  • Loading branch information
N3rdL0rd committed Dec 28, 2024
1 parent d1be7b5 commit c5f9286
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion crashlink/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def save(self, args: List[str]) -> None:
if len(args) == 0:
print("Usage: save <path>")
return
print("Serialising...")
print("Serialising... (don't panic if it looks stuck!)")
ser = self.code.serialise()
print("Saving...")
with open(args[0], "wb") as f:
Expand Down Expand Up @@ -259,6 +259,63 @@ def pseudo(self, args: List[str]) -> None:
return
print("Function not found.")

def savestrings(self, args: List[str]) -> None:
"""Save all strings in the bytecode to a given path. `savestrings <path>`"""
if len(args) == 0:
print("Usage: savestrings <path>")
return
with open(args[0], "wb") as f:
for string in self.code.strings.value:
f.write(string.encode("utf-8", errors="surrogateescape") + b"\n")
print("Strings saved.")

def ss(self, args: List[str]) -> None:
"""
Search for a string in the bytecode by substring. `ss <query>`
"""
if len(args) == 0:
print("Usage: ss <query>")
return
query = " ".join(args)
for i, string in enumerate(self.code.strings.value):
if query.lower() in string.lower():
print(f"String {i}: {string}")

def string(self, args: List[str]) -> None:
"""
Print a string by index. `string <index>`
"""
if len(args) == 0:
print("Usage: string <index>")
return
try:
index = int(args[0])
except ValueError:
print("Invalid index.")
return
try:
print(self.code.strings.value[index])
except IndexError:
print("String not found.")

def setstring(self, args: List[str]) -> None:
"""
Set a string by index. `setstring <index> <string>`
"""
if len(args) < 2:
print("Usage: setstring <index> <string>")
return
try:
index = int(args[0])
except ValueError:
print("Invalid index.")
return
try:
self.code.strings.value[index] = " ".join(args[1:])
except IndexError:
print("String not found.")
print("String set.")

def _get_commands(self) -> Dict[str, Callable[[List[str]], None]]:
"""Get all command methods using reflection"""
return {
Expand Down

0 comments on commit c5f9286

Please sign in to comment.