Attempt to fix traceback on game exit with some older games.

This commit is contained in:
Storm Dragon
2025-03-14 23:10:14 -04:00
parent 2ad22ff1ae
commit be6dfdf53a
9 changed files with 483 additions and 457 deletions

View File

@ -14,24 +14,24 @@ from xdg import BaseDirectory
class Config:
"""Configuration management class for Storm Games."""
def __init__(self, game_title):
def __init__(self, gameTitle):
"""Initialize configuration system for a game.
Args:
game_title (str): Title of the game
gameTitle (str): Title of the game
"""
self.game_title = game_title
self.global_path = os.path.join(BaseDirectory.xdg_config_home, "storm-games")
self.game_path = os.path.join(self.global_path,
str.lower(str.replace(game_title, " ", "-")))
self.gameTitle = gameTitle
self.globalPath = os.path.join(BaseDirectory.xdg_config_home, "storm-games")
self.gamePath = os.path.join(self.globalPath,
str.lower(str.replace(gameTitle, " ", "-")))
# Create game directory if it doesn't exist
if not os.path.exists(self.game_path):
os.makedirs(self.game_path)
if not os.path.exists(self.gamePath):
os.makedirs(self.gamePath)
# Initialize config parsers
self.local_config = configparser.ConfigParser()
self.global_config = configparser.ConfigParser()
self.localConfig = configparser.ConfigParser()
self.globalConfig = configparser.ConfigParser()
# Load existing configurations
self.read_local_config()
@ -40,28 +40,28 @@ class Config:
def read_local_config(self):
"""Read local configuration from file."""
try:
with open(os.path.join(self.game_path, "config.ini"), 'r') as configfile:
self.local_config.read_file(configfile)
with open(os.path.join(self.gamePath, "config.ini"), 'r') as configFile:
self.localConfig.read_file(configFile)
except:
pass
def read_global_config(self):
"""Read global configuration from file."""
try:
with open(os.path.join(self.global_path, "config.ini"), 'r') as configfile:
self.global_config.read_file(configfile)
with open(os.path.join(self.globalPath, "config.ini"), 'r') as configFile:
self.globalConfig.read_file(configFile)
except:
pass
def write_local_config(self):
"""Write local configuration to file."""
with open(os.path.join(self.game_path, "config.ini"), 'w') as configfile:
self.local_config.write(configfile)
with open(os.path.join(self.gamePath, "config.ini"), 'w') as configFile:
self.localConfig.write(configFile)
def write_global_config(self):
"""Write global configuration to file."""
with open(os.path.join(self.global_path, "config.ini"), 'w') as configfile:
self.global_config.write(configfile)
with open(os.path.join(self.globalPath, "config.ini"), 'w') as configFile:
self.globalConfig.write(configFile)
# Global variables for backward compatibility
localConfig = configparser.ConfigParser()
@ -69,34 +69,34 @@ globalConfig = configparser.ConfigParser()
gamePath = ""
globalPath = ""
def write_config(write_global=False):
def write_config(writeGlobal=False):
"""Write configuration to file.
Args:
write_global (bool): If True, write to global config, otherwise local (default: False)
writeGlobal (bool): If True, write to global config, otherwise local (default: False)
"""
if not write_global:
with open(gamePath + "/config.ini", 'w') as configfile:
localConfig.write(configfile)
if not writeGlobal:
with open(gamePath + "/config.ini", 'w') as configFile:
localConfig.write(configFile)
else:
with open(globalPath + "/config.ini", 'w') as configfile:
globalConfig.write(configfile)
with open(globalPath + "/config.ini", 'w') as configFile:
globalConfig.write(configFile)
def read_config(read_global=False):
def read_config(readGlobal=False):
"""Read configuration from file.
Args:
read_global (bool): If True, read global config, otherwise local (default: False)
readGlobal (bool): If True, read global config, otherwise local (default: False)
"""
if not read_global:
if not readGlobal:
try:
with open(gamePath + "/config.ini", 'r') as configfile:
localConfig.read_file(configfile)
with open(gamePath + "/config.ini", 'r') as configFile:
localConfig.read_file(configFile)
except:
pass
else:
try:
with open(globalPath + "/config.ini", 'r') as configfile:
globalConfig.read_file(configfile)
with open(globalPath + "/config.ini", 'r') as configFile:
globalConfig.read_file(configFile)
except:
pass