public override void Initialize() { base.Initialize(); //Show only the virtual controllers supported on this platform VirtualControllerTypes[] vcTypes = EnumUtility.GetValues <VirtualControllerTypes> .EnumValues; for (int i = 0; i < vcTypes.Length; i++) { VirtualControllerTypes vControllerType = vcTypes[i]; //Continue if not supported if (VControllerHelper.IsVControllerSupported(vControllerType, TRBotOSPlatform.CurrentOS) == false) { continue; } CachedVCTypesStr += vControllerType.ToString(); if (i < (vcTypes.Length - 1)) { CachedVCTypesStr += ", "; } } }
private void HandleReloadBoth() { //Check if the periodic input value changed, and enable/disable the routine if we should long periodicEnabled = DataHelper.GetSettingInt(SettingsConstants.PERIODIC_INPUT_ENABLED, 0L); if (periodicEnabled == 0) { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Remove the routine if it exists if (rIndex >= 0) { RoutineHandler.RemoveRoutine(rIndex); } } else { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Add the routine if it doesn't exist if (rIndex < 0) { RoutineHandler.AddRoutine(new PeriodicInputRoutine()); } } //Check if the virtual controller type was changed if (LastVControllerTypeChanged() == true) { VirtualControllerTypes lastVControllerType = (VirtualControllerTypes)DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes supportedVCType = VControllerHelper.ValidateVirtualControllerType(lastVControllerType, TRBotOSPlatform.CurrentOS); //Show a message saying the previous value wasn't supported if (VControllerHelper.IsVControllerSupported(lastVControllerType, TRBotOSPlatform.CurrentOS) == false) { MsgHandler.QueueMessage($"Current virtual controller {lastVControllerType} is not supported by the {TRBotOSPlatform.CurrentOS} platform. Switched it to the default of {supportedVCType} for this platform."); using (BotDBContext context = DatabaseManager.OpenContext()) { Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)supportedVCType; context.SaveChanges(); } } ChangeVControllerType(supportedVCType); return; } ReinitVControllerCount(); }
public override void ExecuteCommand(EvtChatCommandArgs args) { List <string> arguments = args.Command.ArgumentsAsList; long lastVControllerType = DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes curVCType = (VirtualControllerTypes)lastVControllerType; //See the virtual controller if (arguments.Count == 0) { QueueMessage($"The current virtual controller is {(VirtualControllerTypes)lastVControllerType}. To set the virtual controller, add one as an argument: {CachedVCTypesStr}"); return; } //Invalid number of arguments if (arguments.Count > 1) { QueueMessage(UsageMessage); return; } using (BotDBContext context = DatabaseManager.OpenContext()) { //Check if the user has the ability to set the type User user = DataHelper.GetUserNoOpen(args.Command.ChatMessage.Username, context); if (user != null && user.HasEnabledAbility(PermissionConstants.SET_VCONTROLLER_TYPE_ABILITY) == false) { QueueMessage("You don't have the ability to set the virtual controller type!"); return; } } string vControllerStr = arguments[0]; //Parse if (EnumUtility.TryParseEnumValue(vControllerStr, out VirtualControllerTypes parsedVCType) == false) { QueueMessage($"Please enter a valid virtual controller: {CachedVCTypesStr}"); return; } //Same type if (parsedVCType == curVCType && DataContainer.ControllerMngr.Initialized == true) { QueueMessage($"The current virtual controller is already {curVCType}!"); return; } //Make sure this virtual controller is supported on this platform if (VControllerHelper.IsVControllerSupported(parsedVCType, TRBotOSPlatform.CurrentOS) == false) { QueueMessage($"{parsedVCType} virtual controllers are not supported on {TRBotOSPlatform.CurrentOS} platforms."); return; } using (BotDBContext context = DatabaseManager.OpenContext()) { //Set the value and save Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)parsedVCType; context.SaveChanges(); } //Stop and halt all inputs InputHandler.StopAndHaltAllInputs(); try { //Assign the new controller manager IVirtualControllerManager controllerMngr = VControllerHelper.GetVControllerMngrForType(parsedVCType); if (controllerMngr == null) { QueueMessage($"Virtual controller manager of new type {parsedVCType} failed to initialize. This indicates an invalid {SettingsConstants.LAST_VCONTROLLER_TYPE} setting in the database or an unimplemented platform."); return; } //Dispose the controller manager DataContainer.ControllerMngr.Dispose(); DataContainer.SetCurVControllerType(parsedVCType); DataContainer.SetControllerManager(controllerMngr); DataContainer.ControllerMngr.Initialize(); //Ensure we clamp the controller count to the correct value for this virtual controller manager int minControllerCount = DataContainer.ControllerMngr.MinControllers; int maxControllerCount = DataContainer.ControllerMngr.MaxControllers; int joystickCount = (int)DataHelper.GetSettingInt(SettingsConstants.JOYSTICK_COUNT, 0L); int newJoystickCount = joystickCount; if (joystickCount < minControllerCount) { QueueMessage($"Controller count of {joystickCount} is invalid. Clamping to the min of {minControllerCount}."); newJoystickCount = minControllerCount; } else if (joystickCount > maxControllerCount) { QueueMessage($"Controller count of {joystickCount} is invalid. Clamping to the max of {maxControllerCount}."); newJoystickCount = maxControllerCount; } if (joystickCount != newJoystickCount) { using (BotDBContext context = DatabaseManager.OpenContext()) { //Adjust the joystick count setting Settings joystickCountSetting = DataHelper.GetSettingNoOpen(SettingsConstants.JOYSTICK_COUNT, context); joystickCountSetting.ValueInt = newJoystickCount; context.SaveChanges(); } } int acquiredCount = DataContainer.ControllerMngr.InitControllers(newJoystickCount); QueueMessage($"Set virtual controller to {parsedVCType} with {acquiredCount} controller(s) and reset all running inputs!"); } catch (Exception e) { DataContainer.MessageHandler.QueueMessage($"Error changing virtual controller type: {e.Message}"); return; } finally { //Resume inputs InputHandler.ResumeRunningInputs(); } }
public override void ExecuteCommand(EvtChatCommandArgs args) { List <string> arguments = args.Command.ArgumentsAsList; long lastVControllerType = DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes vcType = (VirtualControllerTypes)lastVControllerType; //Use the controller manager's count here //The setting can be changed in the database, but it won't reflect the actual number of controllers in use until a reload int curJoystickCount = DataContainer.ControllerMngr.ControllerCount; //See the number of controllers if (arguments.Count == 0) { QueueMessage($"There are {curJoystickCount} controller(s) plugged in! To set the controller count, please provide a number as an argument."); return; } //Invalid number of arguments if (arguments.Count > 1) { QueueMessage(UsageMessage); return; } using (BotDBContext context = DatabaseManager.OpenContext()) { //Check if the user has the ability to set the controller count User user = DataHelper.GetUserNoOpen(args.Command.ChatMessage.Username, context); if (user != null && user.HasEnabledAbility(PermissionConstants.SET_VCONTROLLER_COUNT_ABILITY) == false) { QueueMessage("You don't have the ability to change the number of controllers!"); return; } } string countStr = arguments[0]; //Parse if (int.TryParse(countStr, out int newJoystickCount) == false) { QueueMessage("Invalid controller count."); return; } //Same count if (newJoystickCount == curJoystickCount) { QueueMessage($"There are already {newJoystickCount} controller(s) plugged in."); return; } int minControllers = DataContainer.ControllerMngr.MinControllers; int maxControllers = DataContainer.ControllerMngr.MaxControllers; if (newJoystickCount < minControllers) { QueueMessage($"New controller count of {newJoystickCount} is invalid. The minimum number of controllers for {vcType} is {minControllers}."); return; } else if (newJoystickCount > maxControllers) { QueueMessage($"New controller count of {newJoystickCount} is invalid. The maximum number of controllers for {vcType} is {maxControllers}."); return; } using (BotDBContext context = DatabaseManager.OpenContext()) { Settings joystickCountSetting = DataHelper.GetSettingNoOpen(SettingsConstants.JOYSTICK_COUNT, context); //Set the value and save joystickCountSetting.ValueInt = newJoystickCount; context.SaveChanges(); } //Stop and halt all inputs InputHandler.StopAndHaltAllInputs(); try { //Dispose the controller manager DataContainer.ControllerMngr.Dispose(); //Re-initialize and initialize controllers DataContainer.ControllerMngr.Initialize(); int acquiredCount = DataContainer.ControllerMngr.InitControllers(newJoystickCount); QueueMessage($"Changed controller count from {curJoystickCount} to {newJoystickCount}, acquired {acquiredCount} controllers, and reset all running inputs!"); } catch (Exception e) { DataContainer.MessageHandler.QueueMessage($"Error changing virtual controller count: {e.Message}"); return; } finally { //Resume inputs InputHandler.ResumeRunningInputs(); } }
private bool LastVControllerTypeChanged() { VirtualControllerTypes vContType = (VirtualControllerTypes)DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); return(vContType != DataContainer.CurVControllerType); }
private void HandleReloadBoth() { //Check for changes in the log level Serilog.Events.LogEventLevel logLevel = (Serilog.Events.LogEventLevel)DataHelper.GetSettingInt(SettingsConstants.LOG_LEVEL, (long)Serilog.Events.LogEventLevel.Information); if (logLevel != TRBotLogger.MinLoggingLevel) { TRBotLogger.Logger.Information($"Detected change in logging level - changing the logging level from {TRBotLogger.MinLoggingLevel} to {logLevel}"); TRBotLogger.SetLogLevel(logLevel); } //Check if the periodic input value changed, and enable/disable the routine if we should long periodicEnabled = DataHelper.GetSettingInt(SettingsConstants.PERIODIC_INPUT_ENABLED, 0L); if (periodicEnabled == 0) { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Remove the routine if it exists if (rIndex >= 0) { RoutineHandler.RemoveRoutine(rIndex); } } else { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Add the routine if it doesn't exist if (rIndex < 0) { RoutineHandler.AddRoutine(new PeriodicInputRoutine()); } } //Check if the virtual controller type was changed if (LastVControllerTypeChanged() == true) { VirtualControllerTypes lastVControllerType = (VirtualControllerTypes)DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes supportedVCType = VControllerHelper.ValidateVirtualControllerType(lastVControllerType, TRBotOSPlatform.CurrentOS); //Show a message saying the previous value wasn't supported if (VControllerHelper.IsVControllerSupported(lastVControllerType, TRBotOSPlatform.CurrentOS) == false) { MsgHandler.QueueMessage($"Current virtual controller {lastVControllerType} is not supported by the {TRBotOSPlatform.CurrentOS} platform. Switched it to the default of {supportedVCType} for this platform."); using (BotDBContext context = DatabaseManager.OpenContext()) { Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)supportedVCType; context.SaveChanges(); } } ChangeVControllerType(supportedVCType); } else { ReinitVControllerCount(); } //Handle message throttling changes MessageThrottlingOptions msgThrottle = (MessageThrottlingOptions)DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_TYPE, 0L); long msgTime = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_COOLDOWN, 30000L); long msgThrottleCount = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_COUNT, 20L); if (msgThrottle != MsgHandler.CurThrottleOption) { TRBotLogger.Logger.Information("Detected change in message throttling type - changing message throttler."); } MsgHandler.SetMessageThrottling(msgThrottle, new MessageThrottleData(msgTime, msgThrottleCount)); }
public void Initialize() { if (Initialized == true) { return; } //Use invariant culture Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; //Set up the logger string logPath = Path.Combine(LoggingConstants.LogFolderPath, LoggingConstants.LOG_FILE_NAME); if (Utilities.FileHelpers.ValidatePathForFile(logPath) == false) { Console.WriteLine("Logger path cannot be validated. This is a problem! Double check the path is correct."); } //Set up the logger //Cap the size at 10 MB TRBotLogger.SetupLogger(logPath, Serilog.Events.LogEventLevel.Verbose, Serilog.RollingInterval.Day, 1024L * 1024L * 10L, TimeSpan.FromSeconds(60d)); //Initialize database string databasePath = Path.Combine(DataConstants.DataFolderPath, DataConstants.DATABASE_FILE_NAME); TRBotLogger.Logger.Information($"Validating database at: {databasePath}"); if (Utilities.FileHelpers.ValidatePathForFile(databasePath) == false) { TRBotLogger.Logger.Error($"Cannot create database path at {databasePath}. Check if you have permission to write to this directory. Aborting."); return; } TRBotLogger.Logger.Information("Database path validated! Initializing database and importing migrations."); DatabaseManager.SetDatabasePath(databasePath); DatabaseManager.InitAndMigrateContext(); TRBotLogger.Logger.Information("Checking to initialize default values for missing database entries."); //Check for and initialize default values if the database was newly created or needs updating int addedDefaultEntries = DataHelper.InitDefaultData(); if (addedDefaultEntries > 0) { TRBotLogger.Logger.Information($"Added {addedDefaultEntries} additional entries to the database."); } //Set the logger's log level long logLevel = DataHelper.GetSettingInt(SettingsConstants.LOG_LEVEL, (long)Serilog.Events.LogEventLevel.Information); TRBotLogger.SetLogLevel((Serilog.Events.LogEventLevel)logLevel); TRBotLogger.Logger.Information("Initializing client service"); //Initialize client service InitClientService(); //If the client service doesn't exist, we can't continue if (ClientService == null) { TRBotLogger.Logger.Error("Client service failed to initialize; please check your settings. Aborting."); return; } //Set client service and message cooldown MsgHandler.SetClientService(ClientService); MessageThrottlingOptions msgThrottleOption = (MessageThrottlingOptions)DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_TYPE, 0L); long msgCooldown = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_COOLDOWN, 30000L); long msgThrottleCount = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_COUNT, 20L); MsgHandler.SetMessageThrottling(msgThrottleOption, new MessageThrottleData(msgCooldown, msgThrottleCount)); //Subscribe to events UnsubscribeEvents(); SubscribeEvents(); DataContainer.SetMessageHandler(MsgHandler); DataContainer.SetDataReloader(DataReloader); TRBotLogger.Logger.Information("Setting up virtual controller manager."); VirtualControllerTypes lastVControllerType = (VirtualControllerTypes)DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes curVControllerType = VControllerHelper.ValidateVirtualControllerType(lastVControllerType, TRBotOSPlatform.CurrentOS); //Show a message saying the previous value wasn't supported and save the changes if (VControllerHelper.IsVControllerSupported(lastVControllerType, TRBotOSPlatform.CurrentOS) == false) { MsgHandler.QueueMessage($"Current virtual controller {lastVControllerType} is not supported by the {TRBotOSPlatform.CurrentOS} platform. Switched it to the default of {curVControllerType} for this platform."); using (BotDBContext context = DatabaseManager.OpenContext()) { Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)curVControllerType; context.SaveChanges(); } } DataContainer.SetCurVControllerType(curVControllerType); IVirtualControllerManager controllerMngr = VControllerHelper.GetVControllerMngrForType(curVControllerType); DataContainer.SetControllerManager(controllerMngr); int controllerCount = 0; //Clamp the controller count to the min and max allowed by the virtual controller manager using (BotDBContext context = DatabaseManager.OpenContext()) { Settings joystickCountSetting = DataHelper.GetSettingNoOpen(SettingsConstants.JOYSTICK_COUNT, context); int minCount = DataContainer.ControllerMngr.MinControllers; int maxCount = DataContainer.ControllerMngr.MaxControllers; //Validate controller count if (joystickCountSetting.ValueInt < minCount) { MsgHandler.QueueMessage($"Controller count of {joystickCountSetting.ValueInt} in database is invalid. Clamping to the min of {minCount}."); joystickCountSetting.ValueInt = minCount; context.SaveChanges(); } else if (joystickCountSetting.ValueInt > maxCount) { MsgHandler.QueueMessage($"Controller count of {joystickCountSetting.ValueInt} in database is invalid. Clamping to the max of {maxCount}."); joystickCountSetting.ValueInt = maxCount; context.SaveChanges(); } controllerCount = (int)joystickCountSetting.ValueInt; } DataContainer.ControllerMngr.Initialize(); int acquiredCount = DataContainer.ControllerMngr.InitControllers(controllerCount); TRBotLogger.Logger.Information($"Setting up virtual controller {curVControllerType} and acquired {acquiredCount} controllers!"); CmdHandler = new CommandHandler(); CmdHandler.Initialize(DataContainer, RoutineHandler); DataReloader.SoftDataReloadedEvent -= OnSoftReload; DataReloader.SoftDataReloadedEvent += OnSoftReload; DataReloader.HardDataReloadedEvent -= OnHardReload; DataReloader.HardDataReloadedEvent += OnHardReload; //Initialize routines InitRoutines(); //Cache our parser InputParser = new Parser(); Initialized = true; }