# needs classes etc
# must run as admin
'''
https://pypi.org/project/game-overlay-sdk/
54470 is steam app id
the following code uses start_monitor rather than run_process method
command line is:
python examples\monitor.py --name SOTTR.exe
https://github.com/Andrey1994/game_overlay_sdk
'''
import time
import game_overlay_sdk
import game_overlay_sdk.injector
import threading
import logging
import subprocess

with open('Data/read.txt', 'w') as initial_file:
    initial_file.write('Full Screen Overlay Enabled')

subprocess.Popen('python GUI_Main.py')

# create the file in the directory that (game_overlay_sdk) requires this:
path = 'C:\Program Files (x86)\Steam\steamapps\common\SoulcaliburVI'
with open('%s\steam_appid.txt' % path, 'w') as appid_file:
    appid_file.write('544750')

class MessageThread (threading.Thread):

    def __init__ (self):
        super (MessageThread, self).__init__ ()
        self.need_quit = False

    def run (self):
        # checks if previous_line variable exists and if not creates it
        try:
            previous_line
        except NameError:
            previous_line = 'placeholder'
        while not self.need_quit:
            with open('Data/read.txt', 'r') as read_file:
                # last_line = read_file.read().splitlines()[-1]
                # last_line = read_file.readlines(1000)[-1]
                last_line = read_file.read()
            '''
            example of lines generated by scuffle:
            FDO:p1:281|  5B   | 14 |mid_4b| -8  |   +2  |       |14|7 |3|43 |29 | 0 |�
            FDO:p1:283|  5BB  | 20 |mid_4b| -6  |   +4  |       |14|7 |2|51 |31 |-1 |�
            FDO:p1:284| 5BBB  | 36 |mid_6f| -4  |LNC +2 |       |20|30|2|69 |33 |11 |GRND �
            '''
            if '|' in last_line and last_line != previous_line:
                columns = last_line.split('|')
                print(columns)
                # remove whitespace
                for column in columns:
                    column.replace(' ', '')
                player = columns[0].split(':')[1].upper()
                move_notation = columns[1]
                frames_on_block = columns[4]
                frames_on_hit = columns[5]
                message = '%s %s: %s OB, %s OH' % (player, move_notation, frames_on_block, frames_on_hit)
                try:
                    '''
                    game_overlay_sdk.injector.send_message ('Hi from python %d' % i)
                    i = i + 1
                    time.sleep (1)
                    '''
                    game_overlay_sdk.injector.send_message (message)
                    previous_line = last_line
                except game_overlay_sdk.injector.InjectionError as err:
                    if err.exit_code == game_overlay_sdk.injector.CustomExitCodes.TARGET_PROCESS_IS_NOT_CREATED_ERROR.value:
                        logging.warning ('target process is not created')
                        time.sleep (5)
                    elif err.exit_code == game_overlay_sdk.injector.CustomExitCodes.TARGET_PROCESS_WAS_TERMINATED_ERROR.value:
                        logging.warning ('target process was stopped')
                        # in monitor mode we can run process several times so dont need to stop this thread here
                        i = 0
                        time.sleep (5)
                    else:
                        raise err
            time.sleep(0.01666)


def main ():
    logging.basicConfig (level = logging.DEBUG)

    game_overlay_sdk.injector.enable_monitor_logger ()
    game_overlay_sdk.injector.start_monitor ('SoulcaliburVI.exe')

    # start sending messages to overlay
    thread = MessageThread ()
    thread.start ()
    input ("Press Enter to stop...")
    thread.need_quit = True
    thread.join ()

    game_overlay_sdk.injector.release_resources ()


if __name__ == "__main__":
    main ()
# needs classes etc
# must run as admin
'''
https://pypi.org/project/game-overlay-sdk/
54470 is steam app id
the following code uses start_monitor rather than run_process method
command line is:
python examples\monitor.py --name SOTTR.exe
https://github.com/Andrey1994/game_overlay_sdk
'''
import time
import game_overlay_sdk
import game_overlay_sdk.injector
import threading
import logging
import subprocess

with open('Data/read.txt', 'w') as initial_file:
    initial_file.write('Full Screen Overlay Enabled')

subprocess.Popen('python GUI_Main.py')

# create the file in the directory that (game_overlay_sdk) requires this:
path = 'C:\Program Files (x86)\Steam\steamapps\common\SoulcaliburVI'
with open('%s\steam_appid.txt' % path, 'w') as appid_file:
    appid_file.write('544750')

class MessageThread (threading.Thread):

    def __init__ (self):
        super (MessageThread, self).__init__ ()
        self.need_quit = False

    def run (self):
        # checks if previous_line variable exists and if not creates it
        try:
            previous_line
        except NameError:
            previous_line = 'placeholder'
        while not self.need_quit:
            with open('Data/read.txt', 'r') as read_file:
                # last_line = read_file.read().splitlines()[-1]
                # last_line = read_file.readlines(1000)[-1]
                last_line = read_file.read()
            '''
            example of lines generated by scuffle:
            FDO:p1:281|  5B   | 14 |mid_4b| -8  |   +2  |       |14|7 |3|43 |29 | 0 |�
            FDO:p1:283|  5BB  | 20 |mid_4b| -6  |   +4  |       |14|7 |2|51 |31 |-1 |�
            FDO:p1:284| 5BBB  | 36 |mid_6f| -4  |LNC +2 |       |20|30|2|69 |33 |11 |GRND �
            '''
            if '|' in last_line and last_line != previous_line:
                columns = last_line.split('|')
                print(columns)
                # remove whitespace
                for column in columns:
                    column.replace(' ', '')
                player = columns[0].split(':')[1].upper()
                move_notation = columns[1]
                frames_on_block = columns[4]
                frames_on_hit = columns[5]
                message = '%s %s: %s OB, %s OH' % (player, move_notation, frames_on_block, frames_on_hit)
                try:
                    '''
                    game_overlay_sdk.injector.send_message ('Hi from python %d' % i)
                    i = i + 1
                    time.sleep (1)
                    '''
                    game_overlay_sdk.injector.send_message (message)
                    previous_line = last_line
                except game_overlay_sdk.injector.InjectionError as err:
                    if err.exit_code == game_overlay_sdk.injector.CustomExitCodes.TARGET_PROCESS_IS_NOT_CREATED_ERROR.value:
                        logging.warning ('target process is not created')
                        time.sleep (5)
                    elif err.exit_code == game_overlay_sdk.injector.CustomExitCodes.TARGET_PROCESS_WAS_TERMINATED_ERROR.value:
                        logging.warning ('target process was stopped')
                        # in monitor mode we can run process several times so dont need to stop this thread here
                        i = 0
                        time.sleep (5)
                    else:
                        raise err
            time.sleep(0.01666)


def main ():
    logging.basicConfig (level = logging.DEBUG)

    game_overlay_sdk.injector.enable_monitor_logger ()
    game_overlay_sdk.injector.start_monitor ('SoulcaliburVI.exe')

    # start sending messages to overlay
    thread = MessageThread ()
    thread.start ()
    input ("Press Enter to stop...")
    thread.need_quit = True
    thread.join ()

    game_overlay_sdk.injector.release_resources ()


if __name__ == "__main__":
    main ()
# needs classes etc # must run as admin https://pypi.org/project/game-overlay-sdk/ 54470 is steam app id the following code uses start_monitor rather than run_process method command line is: python examples\monitor.py --name SOTTR.exe https://github.com/Andrey1994/game_overlay_sdk # create the file in the directory that (game_overlay_sdk) requires this: # checks if previous_line variable exists and if not creates it # last_line = read_file.read().splitlines()[-1] # last_line = read_file.readlines(1000)[-1] example of lines generated by scuffle:             FDO:p1:281|  5B   | 14 |mid_4b| -8  |   +2  |       |14|7 |3|43 |29 | 0 |�             FDO:p1:283|  5BB  | 20 |mid_4b| -6  |   +4  |       |14|7 |2|51 |31 |-1 |�             FDO:p1:284| 5BBB  | 36 |mid_6f| -4  |LNC +2 |       |20|30|2|69 |33 |11 |GRND � # remove whitespace game_overlay_sdk.injector.send_message ('Hi from python %d' % i)                     i = i + 1                     time.sleep (1) # in monitor mode we can run process several times so dont need to stop this thread here # start sending messages to overlay