Skip to content

Commit

Permalink
MAJOR SIG SCAN OPTIMISATION
Browse files Browse the repository at this point in the history
  • Loading branch information
RealistikDash committed Jun 10, 2023
1 parent 780e611 commit 0741c2b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 66 deletions.
2 changes: 1 addition & 1 deletion fps_bypass/genshin.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def get_memory_pointers(
estimate_metric = False

# Try the estimated offset first.
if memory.signature_match(
if False and memory.signature_match(
user_assembly_buffer[
ESTIMATE_FPS_OFFSET : ESTIMATE_FPS_OFFSET + len(FPS_SIGNATURE)
],
Expand Down
2 changes: 1 addition & 1 deletion fps_bypass/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import os

VERSION = (0, 1, 1)
VERSION = (0, 1, 2)

ERR_SUCCESS = 0
ERR_FAILURE = 1
Expand Down
113 changes: 49 additions & 64 deletions fps_bypass/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,83 +92,68 @@ def signature_match(buffer: bytes, signature: Signature) -> bool:
# No we have a JIT at home.
# The JIT at home:
SignatureFunction = Callable[[bytes], int | None]

BASE_FUNCTION = """
def _sig_func(buffer: bytes) -> int | None:
{pre_setup}
def _sig_scan(buffer: bytes) -> int | None:
byte_sequence = {byte_sequence}
sequence_offset = {sequence_offset}
signature_length = {signature_length}
offset = -signature_length
try:
while True:
initial_offset = buffer.index(byte_sequence, offset + signature_length)
offset = initial_offset - sequence_offset
if (
{conditions}
):
return offset
except ValueError:
return None
for i in range(len(buffer) - {pattern_len}):
if (
{conditions}
): return i
"""


def compile_signature(signature: Signature) -> SignatureFunction:
conditions = []
"""Compiles a signature into a Python function."""

# Find the largest sequence of constant bytes in the signature.
max_sequence = [-1, b""]
current_sequence = [-1, b""]

for i, byte in enumerate(signature.pattern):
if byte is None:
if len(current_sequence[1]) > len(max_sequence[1]):
max_sequence = current_sequence
current_sequence = [-1, b""]
continue

conditions.append(f"buffer[i + {i}] == {byte}")

condition_str = " and ".join(conditions)
function = BASE_FUNCTION.format(
conditions=condition_str,
pre_setup="",
pattern_len=len(signature),
)

exec(function, globals(), locals())
return locals()["_sig_func"]


# This was slower.
# def compile_signature(signature: Signature) -> SignatureFunction:
# """Compiles a signature into a Python function."""#
if current_sequence[0] == -1:
current_sequence[0] = i
current_sequence[1] = bytes([byte])

# # Find groups of bytes in the signature that are set.
# results = list[tuple[int, tuple[int, ...]]]()
# pat_len = len(signature)
# i = 0#
else:
current_sequence[1] += bytes([byte])

# while i < pat_len:
# if signature.pattern[i] is None:
# i += 1
# continue#

# j = i
# while j < pat_len and signature.pattern[j] is not None:
# j += 1#

# results.append((i, signature.pattern[i:j]))
# i = j#

# # Condition generation
# conditions = []
# pat_gen_tuples = list[tuple[str, tuple[int, ...]]]()#

# for offset, group in results:
# if len(group) == 1:
# conditions.append(f"buffer[i + {offset}] == {group[0]}")
# continue#
# Conditions
conditions = []
for i, byte in enumerate(signature.pattern):
if byte is None:
continue

# # Generate a tuple of bytes.
# pat_gen_tuples.append((f"_PAT_PART_{offset}", group))
# conditions.append(f"buffer[i + {offset}:i + {offset + len(group)}] == _PAT_PART_{offset}")#
conditions.append(f"buffer[offset + {i}] == {byte}")

# condition_str = " and ".join(conditions)
# pre_setup_str = "\n ".join(
# f"{name} = memoryview(bytes({bytes}))" for name, bytes in pat_gen_tuples
# )#
#
condition_str = " and ".join(conditions)

# # Function generation
# function = BASE_FUNCTION.format(
# conditions=condition_str,
# pre_setup=pre_setup_str,
# pattern_len=pat_len,
# )#
# Compile the function.
func_str = BASE_FUNCTION.format(
byte_sequence=max_sequence[1],
sequence_offset=max_sequence[0],
signature_length=len(signature),
conditions=condition_str,
)
out_vars = {}

# # Compile the function
# exec(function, globals(), locals())
# return locals()["_sig_func"]
exec(func_str, {}, out_vars)
return out_vars["_sig_scan"]

0 comments on commit 0741c2b

Please sign in to comment.