-
Notifications
You must be signed in to change notification settings - Fork 0
/
general.py
60 lines (53 loc) · 1.5 KB
/
general.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from random import randint, randrange
from structures import *
from ast import literal_eval
import struct
import time
from queue import Queue
#print a string in the gui, don't call more than once per frame
def print_to_gui(text, queue):
if(queue.empty()):
queue.put(text)
queue.join()
#pick between two things
def choose(option1, option2):
choice = randint(0, 1)
if(choice == 0):
return option1
return option2
#select randomly from a list
def choose_list(choice_list):
random_index = randrange(0, len(choice_list))
return choice_list[random_index]
def flatten(x, min, max):
return ((x-min) / (max-min))
#constructs hex string from list of bytes
#returns int conversion
def hex_to_int(hexlist):
final = "0x"
for value in hexlist:
if(len(value[2:]) == 1):
final += "0"
final += value[2:]
return int(literal_eval(final))
#constructs hex string from list of bytes
#returns float conversion
def hex_to_float(hexlist):
final = ""
for value in hexlist:
if(len(value[2:]) == 1):
final += "0"
final += value[2:]
return struct.unpack('!f', bytes.fromhex(final))[0]
#read a given length of bytes from replay
def read_frame(replay, length):
data = []
while(len(data) < length):
position = replay.tell()
byte = replay.read(1)
if(not byte):
time.sleep(1/120)
replay.seek(position)
else:
data.append(hex(ord(byte)))
return data