From 0bb80b15b1f956ee9f63edba1eef452637d85b5b Mon Sep 17 00:00:00 2001 From: ZenithVal <88603991+ZenithVal@users.noreply.github.com> Date: Mon, 26 Feb 2024 21:48:12 -0500 Subject: [PATCH] Invalid Json management Past Zeni, why in the world did you even let this past? --- Controllers/DataController.py | 9 ++++++--- OSCLeash.py | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Controllers/DataController.py b/Controllers/DataController.py index f5b01fa..fb3e66a 100644 --- a/Controllers/DataController.py +++ b/Controllers/DataController.py @@ -1,4 +1,5 @@ import time +import os import ctypes #Required for colored error messages. DefaultConfig = { @@ -61,8 +62,11 @@ def setSettings(self, configJson): self.XboxJoystickMovement = configJson["XboxJoystickMovement"] self.Leashes = configJson["PhysboneParameters"] except Exception as e: - print('\x1b[1;31;40m' + 'Malformed config file. Loading default values.' + '\x1b[0m') - print(e,"was the exception\n") + print('\x1b[1;31;40m' + 'Malformed Config.json contents. Was something missing?' + '\x1b[0m') + print(f"Exception: {e}\nDefault Config will be loaded.\n") + os.system("pause") + print("") + self.IP = DefaultConfig["IP"] self.ListeningPort = DefaultConfig["ListeningPort"] self.SendingPort = DefaultConfig["SendingPort"] @@ -80,7 +84,6 @@ def setSettings(self, configJson): self.Logging = DefaultConfig["Logging"] self.XboxJoystickMovement = DefaultConfig["XboxJoystickMovement"] self.Leashes = DefaultConfig["PhysboneParameters"] - time.sleep(3) def addGamepadControls(self, gamepad, runButton): self.gamepad = gamepad diff --git a/OSCLeash.py b/OSCLeash.py index 9ce9ac6..130a032 100644 --- a/OSCLeash.py +++ b/OSCLeash.py @@ -30,7 +30,7 @@ def createDefaultConfigFile(configPath): # Creates a default config program.cls() # Make sure to change this to the correct version number on releases. - print('\x1b[1;32;40m' + "OSCLeash v2.1.1" + '\x1b[0m') + print('\x1b[1;32;40m' + "OSCLeash v2.1.2" + '\x1b[0m') # Test if Config file exists. Create the default if it does not. configRelativePath = "./Config.json" @@ -41,8 +41,17 @@ def createDefaultConfigFile(configPath): # Creates a default config else: print("Config file found\n") - configData = json.load(open(configRelativePath)) # Config file should be prepared at this point. - settings = ConfigSettings(configData) # Get settings from config file + # load settings + try: + configData = json.load(open(configRelativePath)) + except Exception as e: + print('\x1b[1;31;40m' + 'Malformed Config.json file. Fix or delete it to generate a new one.' + '\x1b[0m') + print(f"{e}\nDefault Config will be loaded.\n") + + configData = DefaultConfig + os.system("pause") + + settings = ConfigSettings(configData) time.sleep(1)