-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (92 loc) · 3.83 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import tkinter as tk
from tkinter import filedialog, ttk, messagebox
import json
import os
def extract_json_from_rofl(file_name, keys):
with open(file_name, 'rb') as file:
content = file.read()
brace_stack = []
start_idx = None
end_idx = None
for idx, byte in enumerate(content):
if byte == ord('{'):
brace_stack.append(idx)
if start_idx is None:
start_idx = idx
elif byte == ord('}'):
if brace_stack:
brace_stack.pop()
if not brace_stack:
end_idx = idx
break
if start_idx is not None and end_idx is not None:
json_bytes = content[start_idx:end_idx + 1]
try:
json_str = json_bytes.decode('utf-8', 'ignore')
json_object = json.loads(json_str)
return {key: json_object[key] for key in keys if key in json_object}
except json.JSONDecodeError as e:
print(f"Error decoding JSON: {e}")
return None
def extract_stats_json_from_rofl(file_name):
full_json = extract_json_from_rofl(file_name)
if full_json and 'statsJson' in full_json:
return full_json['statsJson']
else:
return None
def open_file():
file_path = filedialog.askopenfilename(filetypes=[("ROFL files", "*.rofl"), ("All files", "*.*")])
if file_path:
json_data = extract_json_from_rofl(file_path, keys)
if json_data:
formatted_json = json.dumps(json_data, indent=4)
text_widget.delete(1.0, tk.END)
text_widget.insert(tk.END, formatted_json)
else:
messagebox.showerror("Error", "The file could not be processed.")
def save_json(data):
file_path = filedialog.asksaveasfilename(
defaultextension='.json',
filetypes=[("JSON files", "*.json"), ("All files", "*.*")]
)
if file_path:
with open(file_path, 'w') as json_file:
json.dump(data, json_file, indent=4)
print(f"File saved successfully at {file_path}")
def handle_save():
current_text = text_widget.get("1.0", tk.END)
try:
data = json.loads(current_text)
save_json(data)
except json.JSONDecodeError:
messagebox.showerror("Error", "The current text is not valid JSON.")
keys = ['NAME', 'ASSISTS', 'BARON_KILLS', 'DOUBLE_KILLS', 'DRAGON_KILLS', 'HORDE_KILLS', 'PENTA_KILLS', 'QUADRA_KILLS', 'RIFT_HERALD_KILLS', 'TRIPLE_KILLS', 'UNREAL_KILLS', 'WIN']
root = tk.Tk()
root.title("RoflToJson")
root.geometry("800x600")
root.iconbitmap('rofltojson.ico')
style = ttk.Style()
style.theme_use("clam")
large_font = ("Verdna", 12)
small_font = ("Verdana", 10)
padding = (5,5)
button_frame = ttk.Frame(root, padding=padding)
button_frame.pack(side=tk.TOP, fill=tk.X)
text_frame = ttk.Frame(root, padding=padding)
text_frame.pack(side=tk.TOP, fill=tk.BOTH, expand=True)
open_button = ttk.Button(button_frame, text="Open ROFL File", command=open_file)
open_button.pack(side=tk.LEFT, padx=10, pady=10)
save_button = ttk.Button(button_frame, text="Save JSON", command=handle_save)
save_button.pack(side=tk.RIGHT, padx=10, pady=10)
text_widget = tk.Text(text_frame, font=large_font, wrap=tk.WORD)
text_widget.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar = ttk.Scrollbar(text_frame, orient='vertical', command=text_widget.yview)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
text_widget['yscrollcommand'] = scrollbar.set
root.configure(background='#f0f0f0')
text_widget.configure(bg='#ffffff', fg='#333333')
footer_frame = ttk.Frame(root)
footer_frame.pack(side=tk.BOTTOM, fill=tk.X)
footer_label = ttk.Label(footer_frame, text="[email protected] - v1.1.0", background="#ddd", anchor="center")
footer_label.pack(side=tk.BOTTOM, fill=tk.X)
root.mainloop()