import getpass, re, time, os
import dearpygui.dearpygui as dpg
from python_samp import SAMP, API
from pathlib import Path
from os.path import isfile
samp = SAMP()
api = API(samp)
def getUserPath():
return ('C:\\Users\\'+ getUserNameD() + '\\Documents\\GTA San Andreas User Files\\SAMP\\chatlog.txt')
def getUserNameD():
return getpass.getuser()
def checkUserFilePathD(filepath): #debug
if isfile(filepath): return True
else: return False
def read_lines(path):
chatlog_lines = []
with open(path, 'rb') as chatlog_obj:
chatlog_obj.seek(0, os.SEEK_END)
buffer = bytearray()
p_location = chatlog_obj.tell()
while p_location >= 0:
chatlog_obj.seek(p_location)
p_location = p_location -1
l_byte = chatlog_obj.read(1)
if l_byte == b'\n':
chatlog_lines.append(buffer.decode()[::-1])
if len(chatlog_lines) == 2:
return list(reversed(chatlog_lines))
buffer = bytearray()
else:
buffer.extend(l_byte)
if len(buffer) > 0:
list_of_lines.append(buffer.decode()[::-1])
return list(reversed(chatlog_lines))
path = getUserPath()
def chatevent():
dpg.delete_item(1)
ug = r'"([^"]*)"'
dpg.set_value(103, "Wait 3 seconds.")
time.sleep (1)
dpg.set_value(103, "Wait 2 seconds..")
time.sleep (1)
dpg.set_value(103, "Wait 1 seconds...")
time.sleep (1)
dpg.set_value(103, "Found, waiting for word")
stat_result = os.stat(path)
current_size = stat_result.st_size
current_mtime = stat_result.st_mtime
while True:
time.sleep(0.25)
stat_result = os.stat(path)
new_size = stat_result.st_size
new_mtime = stat_result.st_mtime
if new_size != current_size:
print(f'The size of {path} has changed to {new_size} bytes')
last_line = read_lines(path)[0][11:].rstrip()
last_line = re.sub(r"(\s?\{[A-F0-9]{6}\}\s?)", "", last_line)
match = re.search(ug, last_line)
if match:
api.send_chat(match.group(1))
dpg.set_value(104, "Detected word: " + match.group(1))
print(last_line)
dpg.set_value(103, read_lines(path)[0][11:].rstrip())
current_size = new_size
dpg.create_context()
dpg.create_viewport(title="Chatscanner", resizable=False,
width=550, height=150, x_pos=0, y_pos=0,
min_width=300, min_height=300)
dpg.setup_dearpygui()
with dpg.viewport_menu_bar():
with dpg.menu(label= "Stats"):
dpg.add_menu_item(tag = 100, label = "isValidChat: " + str(checkUserFilePathD(path)))
dpg.add_menu_item(tag = 101, label = "Path: " + path, callback = lambda: os.system('cmd /c start %windir%\\explorer.exe "C:\\Users\\Claudiu\\Documents\\GTA San Andreas User Files\\SAMP\\chatlog.txt"'))
chatlog_word = "<None>"
with dpg.window(label="Chatlog", pos=(0,0), width=600, height=300,
no_move = True, no_close = True, no_collapse = True, no_background = True, menubar = False):
dpg.add_text(tag = 103, default_value = "Press Start to load chat...")
dpg.add_text(tag = 104, default_value = "Last detected word " + chatlog_word)
dpg.add_button(tag = 1, label = "START", width = 270, height = 150, callback = chatevent)
dpg.show_viewport()
dpg.start_dearpygui()
dpg.destroy_context()