feat: Dynamically resolve configuration file paths to support frozen executables.

This commit is contained in:
2026-01-30 16:02:58 +08:00
parent 994b25fc82
commit b86408e7e6

View File

@@ -3,7 +3,7 @@ import os
import uuid
import base64
CONFIG_FILE = "config.dat" # Obfuscated file
import sys
class ConfigManager:
DEFAULT_JOB = {
@@ -30,6 +30,16 @@ class ConfigManager:
"schedule_unit": "Menit"
}
@staticmethod
def _get_base_dir():
if getattr(sys, 'frozen', False):
return os.path.dirname(sys.executable)
return os.path.dirname(os.path.abspath(__file__))
@staticmethod
def _get_config_path(filename="config.dat"):
return os.path.join(ConfigManager._get_base_dir(), filename)
@staticmethod
def create_new_job():
job = ConfigManager.DEFAULT_JOB.copy()
@@ -38,24 +48,27 @@ class ConfigManager:
@staticmethod
def load_config():
config_dat = ConfigManager._get_config_path("config.dat")
config_json = ConfigManager._get_config_path("config.json")
# Check for old unencrypted config.json and migrate
if os.path.exists("config.json"):
if os.path.exists(config_json):
try:
with open("config.json", 'r') as f:
with open(config_json, 'r') as f:
data = json.load(f)
print("Migrasi config.json...")
config = ConfigManager._migrate_old_config(data)
ConfigManager.save_config(config)
os.rename("config.json", "config.json.bak")
os.rename(config_json, config_json + ".bak")
return config
except Exception as e:
print("Error migrasi config: {}".format(e))
if not os.path.exists(CONFIG_FILE):
if not os.path.exists(config_dat):
return ConfigManager.DEFAULT_CONFIG.copy()
try:
with open(CONFIG_FILE, 'rb') as f:
with open(config_dat, 'rb') as f:
encoded_data = f.read()
# Simple Base64 Decode
@@ -113,7 +126,8 @@ class ConfigManager:
# Simple Base64 Encode
encoded_data = base64.b64encode(json_data.encode('utf-8'))
with open(CONFIG_FILE, 'wb') as f:
config_path = ConfigManager._get_config_path("config.dat")
with open(config_path, 'wb') as f:
f.write(encoded_data)
except Exception as e:
print("Error saving config: {}".format(e))