-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
90 lines (72 loc) · 2.7 KB
/
Copy pathmain.py
File metadata and controls
90 lines (72 loc) · 2.7 KB
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
import ctypes
import faulthandler
import os
import sys
from datetime import datetime
from os.path import expandvars, join, split
class StreamMixer:
def __init__(self, std_file, added_stream):
self.std_file = std_file
self.added_stream = added_stream
def write(self, data: str):
self.std_file.write(data)
# 去除颜色转义符
if data.startswith("\033"): # 去除颜色符号
data = data.rstrip("\n")[5:-4] + "\n"
self.added_stream.write(data)
def flush(self):
self.std_file.flush()
self.added_stream.flush()
def fileno(self):
return self.std_file.fileno()
class MineCursorLauncher:
def __init__(self):
self.switch_work_dir()
self.handle_output()
self.program_prepare()
self.run_app()
@staticmethod
def switch_work_dir():
t = os.path.dirname(os.path.abspath(__file__))
os.chdir(t) # 进入当前目录
sys.path.append(t) # 添加模块导入路径
@staticmethod
def handle_output(): # 在无输出句柄的情况下替换标准输出为文件
data_dir = MineCursorLauncher.get_data_dir()
log_dir = join(data_dir, "Logs")
os.makedirs(log_dir, exist_ok=True)
output_file = open(join(log_dir, f"log_{datetime.now().strftime('%Y-%m-%d')}.log"), "a+", encoding="utf-8")
output_file.write(f"\n\nMineCursor Starting... ({datetime.now().strftime('%Y-%m-%d %H:%M:%S')})\n")
if sys.stdout is None or sys.stderr is None:
sys.stdout = output_file
sys.stderr = output_file
else:
sys.stdout = StreamMixer(sys.stdout, output_file)
sys.stderr = StreamMixer(sys.stderr, output_file)
@staticmethod
def get_data_dir():
data_dir = join(split(expandvars("%APPDATA%"))[0], "Mine Cursor")
if os.path.isfile("config.json"):
import json
config = json.load(open("config.json", encoding="utf-8"))
if isinstance(config, dict) and config.get("data_dir"):
data_dir = os.path.abspath(expandvars(config.get("data_dir")))
return data_dir
@staticmethod
def program_prepare():
faulthandler.enable()
import lib.dpi
lib.dpi.Y_SCALE = lib.dpi.Y_SCALE # 保持引用
ctypes.WinDLL('shell32').SetCurrentProcessExplicitAppUserModelID("MineCursor")
@staticmethod
def run_app():
from lib.log import logger
logger.info("导入库中...")
import wx
from ui_ctl.theme_editor import ThemeEditor
app = wx.App()
frame = ThemeEditor(None)
frame.Show()
app.MainLoop()
if __name__ == '__main__':
MineCursorLauncher()