2020-11-15 11:56:40 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-05-11 10:41:48 +01:00
|
|
|
|
2022-07-29 14:58:15 +01:00
|
|
|
def _on_error(e):
|
|
|
|
"""Code to handle errors"""
|
2021-07-06 13:52:47 +01:00
|
|
|
try:
|
2022-07-29 14:58:15 +01:00
|
|
|
import traceback
|
2023-07-03 17:02:37 +01:00
|
|
|
import sys
|
|
|
|
import os
|
2020-11-15 12:00:23 +00:00
|
|
|
|
2023-07-03 17:02:37 +01:00
|
|
|
except ImportError as e:
|
|
|
|
# Something has gone seriously wrong
|
|
|
|
print(e)
|
|
|
|
print("Failed to import requirements. Check that you extracted correctly.")
|
|
|
|
input("Press ENTER to continue.")
|
|
|
|
else:
|
|
|
|
err = "\n".join(
|
|
|
|
[traceback.format_exc()]
|
|
|
|
+ ["Failed to import requirements. Check that you extracted correctly."]
|
|
|
|
* isinstance(e, ImportError)
|
|
|
|
+ [str(e)]
|
2022-07-29 14:58:15 +01:00
|
|
|
)
|
2023-07-03 17:02:37 +01:00
|
|
|
print(err)
|
|
|
|
try:
|
|
|
|
with open("crash.log", "w") as f:
|
|
|
|
f.write(err)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
input("Press ENTER to continue.")
|
|
|
|
sys.exit(1)
|
2022-07-29 14:58:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
import sys
|
|
|
|
|
|
|
|
if sys.version_info[:2] < (3, 7):
|
|
|
|
raise Exception("Must be using Python 3.7+")
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import traceback
|
|
|
|
import glob
|
|
|
|
import time
|
2022-08-06 08:49:46 +01:00
|
|
|
import wx
|
2023-07-03 17:02:37 +01:00
|
|
|
import platformdirs
|
2022-07-29 14:58:15 +01:00
|
|
|
|
|
|
|
if sys.platform == "linux" and wx.VERSION >= (4, 1, 1):
|
|
|
|
# bug 247
|
|
|
|
os.environ["PYOPENGL_PLATFORM"] = "egl"
|
2023-07-03 17:02:37 +01:00
|
|
|
except Exception as e_:
|
|
|
|
_on_error(e_)
|
2022-07-29 14:58:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
def _init_log():
|
2023-07-03 17:02:37 +01:00
|
|
|
logs_path = os.environ["LOG_DIR"]
|
2022-07-29 14:58:15 +01:00
|
|
|
# set up handlers
|
|
|
|
os.makedirs(logs_path, exist_ok=True)
|
|
|
|
# remove all log files older than a week
|
2022-12-21 14:10:20 +00:00
|
|
|
for path in glob.glob(os.path.join(glob.escape(logs_path), "*.log")):
|
2022-07-29 14:58:15 +01:00
|
|
|
if (
|
|
|
|
os.path.isfile(path)
|
|
|
|
and os.path.getmtime(path) < time.time() - 3600 * 24 * 7
|
|
|
|
):
|
|
|
|
os.remove(path)
|
|
|
|
|
|
|
|
log = logging.getLogger()
|
|
|
|
log.setLevel(logging.DEBUG if "amulet-debug" in sys.argv else logging.INFO)
|
|
|
|
|
|
|
|
file_handler = logging.FileHandler(
|
|
|
|
os.path.join(logs_path, f"amulet_{os.getpid()}.log"), "w", encoding="utf-8"
|
|
|
|
)
|
|
|
|
file_handler.setFormatter(
|
|
|
|
logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
|
|
|
|
)
|
|
|
|
log.addHandler(file_handler)
|
|
|
|
|
|
|
|
console_handler = logging.StreamHandler()
|
|
|
|
console_handler.setFormatter(logging.Formatter("%(levelname)s - %(message)s"))
|
|
|
|
log.addHandler(console_handler)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
try:
|
2023-07-03 17:02:37 +01:00
|
|
|
# Initialise default paths.
|
|
|
|
data_dir = platformdirs.user_data_dir("AmuletMapEditor", "AmuletTeam")
|
|
|
|
os.environ.setdefault("DATA_DIR", data_dir)
|
|
|
|
config_dir = platformdirs.user_config_dir("AmuletMapEditor", "AmuletTeam")
|
|
|
|
if config_dir == data_dir:
|
|
|
|
config_dir = os.path.join(data_dir, "Config")
|
|
|
|
os.environ.setdefault("CONFIG_DIR", config_dir)
|
|
|
|
os.environ.setdefault(
|
|
|
|
"CACHE_DIR", platformdirs.user_cache_dir("AmuletMapEditor", "AmuletTeam")
|
|
|
|
)
|
|
|
|
os.environ.setdefault(
|
|
|
|
"LOG_DIR", platformdirs.user_log_dir("AmuletMapEditor", "AmuletTeam")
|
|
|
|
)
|
|
|
|
|
2022-07-29 14:58:15 +01:00
|
|
|
_init_log()
|
2021-07-06 13:52:47 +01:00
|
|
|
from amulet_map_editor.api.framework import AmuletApp
|
2021-04-01 09:49:14 +01:00
|
|
|
|
2022-07-29 14:58:15 +01:00
|
|
|
except Exception as e:
|
|
|
|
_on_error(e)
|
2023-07-03 17:02:37 +01:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
app = AmuletApp(0)
|
|
|
|
app.MainLoop()
|
|
|
|
except Exception as e:
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.critical(
|
|
|
|
f"Amulet Crashed. Sorry about that. Please report it to a developer if you think this is an issue. \n{traceback.format_exc()}"
|
|
|
|
)
|
|
|
|
input("Press ENTER to continue.")
|
2020-03-21 14:03:24 +00:00
|
|
|
|
2022-07-29 14:58:15 +01:00
|
|
|
sys.exit(0)
|
2022-05-11 10:41:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|