/// <summary> /// Registers all the ContractRequirement classes. /// </summary> void RegisterContractRequirements() { LoggingUtil.LogDebug(this, "Start Registering ContractRequirements"); // Register each type foreach (Type subclass in GetAllTypes <ContractRequirement>().Where(t => !t.IsAbstract)) { string name = subclass.Name; if (name.EndsWith("Requirement")) { name = name.Remove(name.Length - 11, 11); } try { ContractRequirement.Register(subclass, name); } catch (Exception e) { LoggingUtil.LogError(this, "Error registering contract requirement " + subclass.Name); LoggingUtil.LogException(e); } } LoggingUtil.LogInfo(this, "Finished Registering ContractRequirements"); }
void PSystemReady() { // Log version info var ainfoV = Attribute.GetCustomAttribute(typeof(ContractConfigurator).Assembly, typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; LoggingUtil.LogInfo(this, "Contract Configurator {0} loading...", ainfoV.InformationalVersion); LoggingUtil.LoadDebuggingConfig(); RegisterParameterFactories(); RegisterBehaviourFactories(); RegisterContractRequirements(); LoadTypeInfo(); IEnumerator <YieldInstruction> iterator = LoadGroupConfig(); while (iterator.MoveNext()) { } DebugWindow.LoadTextures(); StartCoroutine(FinalizeContractTypeLoad()); }
/// <summary> /// Registers all the BehaviourFactory classes. /// </summary> void RegisterBehaviourFactories() { LoggingUtil.LogDebug(this, "Start Registering BehaviourFactories"); // Register each type with the behaviour factory foreach (Type subclass in GetAllTypes <BehaviourFactory>().Where(t => !t.IsAbstract)) { string name = subclass.Name; if (name.EndsWith("Factory")) { name = name.Remove(name.Length - 7, 7); } try { BehaviourFactory.Register(subclass, name); } catch (Exception e) { LoggingUtil.LogError(this, "Error registering behaviour factory " + subclass.Name); LoggingUtil.LogException(e); } } LoggingUtil.LogInfo(this, "Finished Registering BehaviourFactories"); }
/// <summary> /// Loads all the contact configuration group nodes. /// </summary> private IEnumerator <YieldInstruction> LoadGroupConfig() { // Load all the contract groups LoggingUtil.LogDebug(this, "Loading CONTRACT_GROUP nodes."); ConfigNode[] contractGroups = GameDatabase.Instance.GetConfigNodes("CONTRACT_GROUP"); foreach (ConfigNode groupConfig in contractGroups) { // Create the group string name = groupConfig.GetValue("name"); LoggingUtil.LogInfo(this, "Loading CONTRACT_GROUP: '" + name + "'"); ContractGroup contractGroup = null; try { contractGroup = new ContractGroup(name); } catch (ArgumentException) { LoggingUtil.LogError(this, "Couldn't load CONTRACT_GROUP '" + name + "' due to a duplicate name."); } // Peform the actual load if (contractGroup != null) { bool success = false; try { ConfigNodeUtil.ClearCache(true); success = contractGroup.Load(groupConfig); } catch (Exception e) { Exception wrapper = new Exception("Error loading CONTRACT_GROUP '" + name + "'", e); LoggingUtil.LogException(wrapper); } finally { if (!success) { ContractGroup.contractGroups.Remove(name); } } } } if (!reloading) { yield return(new WaitForEndOfFrame()); } // Emit settings for the menu SettingsBuilder.EmitSettings(); yield break; }
/// <summary> /// Disables standard contract types as requested by contract packs. /// </summary> /// <returns>True if the disabling is done.</returns> public static bool DisableContracts() { if (contractsDisabled) { return(true); } // Don't do anything if the contract system has not yet loaded if (ContractSystem.ContractTypes == null) { return(false); } LoggingUtil.LogDebug(typeof(ContractDisabler), "Disabling contract types..."); ConfigNode[] nodes = GameDatabase.Instance.GetConfigNodes("CONTRACT_CONFIGURATOR"); int disabledCounter = 0; // Start disabling via legacy method Dictionary <string, Type> contractsToDisable = new Dictionary <string, Type>(); foreach (ConfigNode node in nodes) { foreach (string contractType in node.GetValues("disabledContractType")) { LoggingUtil.LogWarning(typeof(ContractDisabler), "Disabling contract " + contractType + " via legacy method. Recommend using the disableContractType attribute of the CONTRACT_GROUP node instead."); if (SetContractToDisabled(contractType, null)) { disabledCounter++; } } } // Disable via new method foreach (ContractGroup contractGroup in ContractGroup.AllGroups.Where(g => g != null && g.parent == null)) { foreach (string contractType in contractGroup.disabledContractType) { if (SetContractToDisabled(contractType, contractGroup)) { disabledCounter++; } } } LoggingUtil.LogInfo(typeof(ContractDisabler), "Disabled " + disabledCounter + " ContractTypes."); contractsDisabled = true; return(true); }
/// <summary> /// Registers all the ContractRequirement classes. /// </summary> void RegisterContractRequirements() { LoggingUtil.LogDebug(this.GetType(), "Start Registering ContractRequirements"); // Register each type with the parameter factory foreach (Type subclass in GetAllTypes <ContractRequirement>()) { string name = subclass.Name; if (name.EndsWith("Requirement")) { name = name.Remove(name.Length - 11, 11); } ContractRequirement.Register(subclass, name); } LoggingUtil.LogInfo(this.GetType(), "Finished Registering ContractRequirements"); }
/// <summary> /// Registers all the BehaviourFactory classes. /// </summary> void RegisterBehaviourFactories() { LoggingUtil.LogDebug(this.GetType(), "Start Registering BehaviourFactories"); // Register each type with the behaviour factory foreach (Type subclass in GetAllTypes <BehaviourFactory>()) { string name = subclass.Name; if (name.EndsWith("Factory")) { name = name.Remove(name.Length - 7, 7); } BehaviourFactory.Register(subclass, name); } LoggingUtil.LogInfo(this.GetType(), "Finished Registering BehaviourFactories"); }
public IEnumerator <YieldInstruction> FinalizeContractTypeLoad() { YieldInstruction eof = new WaitForEndOfFrame(); while (HighLogic.LoadedScene != GameScenes.MAINMENU) { yield return(eof); } yield return(eof); IEnumerator <YieldInstruction> iterator = LoadContractTypeConfig(); while (iterator.MoveNext()) { yield return(iterator.Current); } var ainfoV = Attribute.GetCustomAttribute(typeof(ContractConfigurator).Assembly, typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; LoggingUtil.LogInfo(this, "Contract Configurator " + ainfoV.InformationalVersion + " finished loading."); }
void PSystemReady() { // Log version info var ainfoV = Attribute.GetCustomAttribute(typeof(ContractConfigurator).Assembly, typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; LoggingUtil.LogInfo(this, "Contract Configurator " + ainfoV.InformationalVersion + " loading..."); LoggingUtil.LoadDebuggingConfig(); RegisterParameterFactories(); RegisterBehaviourFactories(); RegisterContractRequirements(); IEnumerator <YieldInstruction> iterator = LoadContractConfig(); while (iterator.MoveNext()) { } DebugWindow.LoadTextures(); LoggingUtil.LogInfo(this, "Contract Configurator " + ainfoV.InformationalVersion + " finished loading."); }
public static void SetContractState(Type contractType, bool enabled) { if (ContractSystem.ContractTypes == null || ContractSystem.Instance == null) { Instance.StartCoroutine(Instance.SetContractStateDeferred(contractType, enabled)); } else { if (!enabled && ContractSystem.ContractTypes.Contains(contractType)) { LoggingUtil.LogInfo(typeof(ContractDisabler), "Disabling ContractType: {0} ({1})", contractType.FullName, contractType.Module); do { ContractSystem.ContractTypes.Remove(contractType); } while (ContractSystem.ContractTypes.Contains(contractType)); while (ContractSystem.MandatoryTypes.Contains(contractType)) { ContractSystem.MandatoryTypes.Remove(contractType); } // Remove Offered and active contracts foreach (Contract contract in ContractSystem.Instance.Contracts.Where(c => c != null && c.GetType() == contractType && (c.ContractState == Contract.State.Offered || c.ContractState == Contract.State.Active))) { contract.Withdraw(); } } else if (enabled && !ContractSystem.ContractTypes.Contains(contractType)) { LoggingUtil.LogInfo(typeof(ContractDisabler), "Enabling ContractType: {0} ({1})", contractType.FullName, contractType.Module); ContractSystem.ContractTypes.Add(contractType); if (contractType == typeof(FinePrint.Contracts.ExplorationContract)) { ContractSystem.MandatoryTypes.Add(contractType); } } } }
void Update() { // Load all the contract configurator configuration if (HighLogic.LoadedScene == GameScenes.MAINMENU && !loading) { LoggingUtil.LoadDebuggingConfig(); // Log version info var ainfoV = Attribute.GetCustomAttribute(typeof(ExceptionLogWindow).Assembly, typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; LoggingUtil.LogInfo(this, "Contract Configurator " + ainfoV.InformationalVersion + " loading..."); RegisterParameterFactories(); RegisterBehaviourFactories(); RegisterContractRequirements(); loading = true; IEnumerator <YieldInstruction> iterator = LoadContractConfig(); while (iterator.MoveNext()) { } DebugWindow.LoadTextures(); LoggingUtil.LogInfo(this, "Contract Configurator " + ainfoV.InformationalVersion + " finished loading."); } // Try to disable the contract types else if ((HighLogic.LoadedScene == GameScenes.SPACECENTER) && !contractTypesAdjusted) { if (AdjustContractTypes()) { contractTypesAdjusted = true; } } // Alt-F9 shows the contract configurator window if (GameSettings.MODIFIER_KEY.GetKey() && Input.GetKeyDown(KeyCode.F10)) { DebugWindow.showGUI = !DebugWindow.showGUI; } // Check if the ContractsApp has just become visible if (!contractsAppVisible && ContractsApp.Instance != null && ContractsApp.Instance.appLauncherButton != null) // TODO - fix for 1.0 // ContractsApp.Instance.cascadingList.cascadingList != null && // ContractsApp.Instance.cascadingList.cascadingList.gameObject.activeInHierarchy) { contractsAppVisible = true; } // Display reloading message if (reloading) { if (lastMessage != null) { ScreenMessages.RemoveMessage(lastMessage); lastMessage = null; } switch (reloadStep) { case ReloadStep.GAME_DATABASE: lastMessage = ScreenMessages.PostScreenMessage("Reloading game database...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; case ReloadStep.MODULE_MANAGER: lastMessage = ScreenMessages.PostScreenMessage("Reloading module manager...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; case ReloadStep.CLEAR_CONFIG: lastMessage = ScreenMessages.PostScreenMessage("Clearing previously loaded contract configuration...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; case ReloadStep.LOAD_CONFIG: lastMessage = ScreenMessages.PostScreenMessage("Loading contract configuration (" + attemptedContracts + "/" + totalContracts + ")...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; case ReloadStep.ADJUST_TYPES: lastMessage = ScreenMessages.PostScreenMessage("Adjusting contract types...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; } } // Fire update events if (contractsAppVisible) { foreach (Contract contract in contractsToUpdate) { if (contract.ContractState == Contract.State.Active && contract.GetType() == typeof(ConfiguredContract)) { GameEvents.Contract.onParameterChange.Fire(contract, contract.GetParameter(0)); } } contractsToUpdate.Clear(); } }
/// <summary> /// Performs adjustments to the contract type list. Specifically, disables contract types /// as per configuration files and adds addtional ConfiguredContract instances based on the /// number on contract types. /// </summary> /// <returns>Whether the changes took place</returns> bool AdjustContractTypes() { // Don't do anything if the contract system has not yet loaded if (ContractSystem.ContractTypes == null) { return(false); } LoggingUtil.LogDebug(this.GetType(), "Loading CONTRACT_CONFIGURATOR nodes."); ConfigNode[] nodes = GameDatabase.Instance.GetConfigNodes("CONTRACT_CONFIGURATOR"); // Build a unique list of contract types to disable, in case multiple mods try to // disable the same ones. Dictionary <string, Type> contractsToDisable = new Dictionary <string, Type>(); foreach (ConfigNode node in nodes) { foreach (string contractType in node.GetValues("disabledContractType")) { // No type for now contractsToDisable[contractType] = null; } } // Map the string to a type foreach (Type subclass in GetAllTypes <Contract>()) { string name = subclass.Name; if (contractsToDisable.ContainsKey(name)) { contractsToDisable[name] = subclass; } } // Start disabling! int disabledCounter = 0; foreach (KeyValuePair <string, Type> p in contractsToDisable) { // Didn't find a type if (p.Value == null) { LoggingUtil.LogWarning(this.GetType(), "Couldn't find ContractType '" + p.Key + "' to disable."); } else { LoggingUtil.LogDebug(this.GetType(), "Disabling ContractType: " + p.Value.FullName + " (" + p.Value.Module + ")"); ContractSystem.ContractTypes.Remove(p.Value); disabledCounter++; } } LoggingUtil.LogInfo(this.GetType(), "Disabled " + disabledCounter + " ContractTypes."); // Now add the ConfiguredContract type int count = (int)(ContractType.AllValidContractTypes.Count() / 3.0 + 0.5); for (int i = 0; i < count; i++) { ContractSystem.ContractTypes.Add(typeof(ConfiguredContract)); } LoggingUtil.LogInfo(this.GetType(), "Finished Adjusting ContractTypes"); return(true); }
/// <summary> /// Loads all the contact configuration nodes and creates ContractType objects. /// </summary> private IEnumerator <YieldInstruction> LoadContractConfig() { // Load all the contract groups LoggingUtil.LogDebug(this.GetType(), "Loading CONTRACT_GROUP nodes."); ConfigNode[] contractGroups = GameDatabase.Instance.GetConfigNodes("CONTRACT_GROUP"); foreach (ConfigNode groupConfig in contractGroups) { // Create the group string name = groupConfig.GetValue("name"); LoggingUtil.LogInfo(this.GetType(), "Loading CONTRACT_GROUP: '" + name + "'"); ContractGroup contractGroup = null; try { contractGroup = new ContractGroup(name); } catch (ArgumentException) { LoggingUtil.LogError(this.GetType(), "Couldn't load CONTRACT_GROUP '" + name + "' due to a duplicate name."); } // Peform the actual load if (contractGroup != null) { bool success = false; try { success = contractGroup.Load(groupConfig); } catch (Exception e) { Exception wrapper = new Exception("Error loading CONTRACT_GROUP '" + name + "'", e); LoggingUtil.LogException(wrapper); } finally { if (!success) { ContractGroup.contractGroups.Remove(name); } } } } LoggingUtil.LogDebug(this.GetType(), "Loading CONTRACT_TYPE nodes."); ConfigNode[] contractConfigs = GameDatabase.Instance.GetConfigNodes("CONTRACT_TYPE"); totalContracts = contractConfigs.Count(); // First pass - create all the ContractType objects foreach (ConfigNode contractConfig in contractConfigs) { // Create the initial contract type LoggingUtil.LogVerbose(this.GetType(), "Pre-load for node: '" + contractConfig.GetValue("name") + "'"); try { ContractType contractType = new ContractType(contractConfig.GetValue("name")); } catch (ArgumentException) { LoggingUtil.LogError(this.GetType(), "Couldn't load CONTRACT_TYPE '" + contractConfig.GetValue("name") + "' due to a duplicate name."); } } // Second pass - do the actual loading of details foreach (ConfigNode contractConfig in contractConfigs) { attemptedContracts++; yield return(new WaitForEndOfFrame()); // Fetch the contractType string name = contractConfig.GetValue("name"); ContractType contractType = ContractType.GetContractType(name); if (contractType != null) { LoggingUtil.LogDebug(this.GetType(), "Loading CONTRACT_TYPE: '" + name + "'"); // Perform the load try { contractType.Load(contractConfig); if (contractType.enabled) { successContracts++; } } catch (Exception e) { Exception wrapper = new Exception("Error loading CONTRACT_TYPE '" + name + "'", e); LoggingUtil.LogException(wrapper); } } } LoggingUtil.LogInfo(this.GetType(), "Loaded " + successContracts + " out of " + totalContracts + " CONTRACT_TYPE nodes."); if (!reloading && LoggingUtil.logLevel == LoggingUtil.LogLevel.DEBUG || LoggingUtil.logLevel == LoggingUtil.LogLevel.VERBOSE) { ScreenMessages.PostScreenMessage("Contract Configurator: Loaded " + successContracts + " out of " + totalContracts + " contracts successfully.", 5, ScreenMessageStyle.UPPER_CENTER); } }
/// <summary> /// Loads all the contact type nodes and creates ContractType objects. /// </summary> private IEnumerator <YieldInstruction> LoadContractTypeConfig() { LoggingUtil.LogDebug(this, "Loading CONTRACT_TYPE nodes."); ConfigNode[] contractConfigs = GameDatabase.Instance.GetConfigNodes("CONTRACT_TYPE"); totalContracts = contractConfigs.Count(); // First pass - create all the ContractType objects foreach (ConfigNode contractConfig in contractConfigs) { // Create the initial contract type LoggingUtil.LogVerbose(this, "Pre-load for node: '" + contractConfig.GetValue("name") + "'"); try { ContractType contractType = new ContractType(contractConfig.GetValue("name")); } catch (ArgumentException) { LoggingUtil.LogError(this, "Couldn't load CONTRACT_TYPE '" + contractConfig.GetValue("name") + "' due to a duplicate name."); } } // Second pass - do the actual loading of details foreach (ConfigNode contractConfig in contractConfigs) { attemptedContracts++; if (reloading) { yield return(new WaitForEndOfFrame()); } // Fetch the contractType string name = contractConfig.GetValue("name"); ContractType contractType = ContractType.GetContractType(name); if (contractType != null && !contractType.loaded) { // Perform the load try { contractType.Load(contractConfig); if (contractType.enabled) { successContracts++; } } catch (Exception e) { LoggingUtil.LogException(e); } } } LoggingUtil.LogInfo(this, "Loaded " + successContracts + " out of " + totalContracts + " CONTRACT_TYPE nodes."); // Check for empty groups and warn foreach (ContractGroup group in ContractGroup.contractGroups.Values.Where(g => g != null)) { group.CheckEmpty(); } // Load other things MissionControlUI.GroupContainer.LoadConfig(); if (!reloading && LoggingUtil.logLevel == LoggingUtil.LogLevel.DEBUG || LoggingUtil.logLevel == LoggingUtil.LogLevel.VERBOSE) { ScreenMessages.PostScreenMessage("Contract Configurator: Loaded " + successContracts + " out of " + totalContracts + " contracts successfully.", 5, ScreenMessageStyle.UPPER_CENTER); } }
void Update() { // Load all the contract configurator configuration if (HighLogic.LoadedScene == GameScenes.MAINMENU && !loading) { // Log version info var ainfoV = Attribute.GetCustomAttribute(typeof(ContractConfigurator).Assembly, typeof(AssemblyInformationalVersionAttribute)) as AssemblyInformationalVersionAttribute; LoggingUtil.LogInfo(this, "Contract Configurator " + ainfoV.InformationalVersion + " loading..."); LoggingUtil.LoadDebuggingConfig(); RegisterParameterFactories(); RegisterBehaviourFactories(); RegisterContractRequirements(); loading = true; IEnumerator <YieldInstruction> iterator = LoadContractConfig(); while (iterator.MoveNext()) { } DebugWindow.LoadTextures(); LoggingUtil.LogInfo(this, "Contract Configurator " + ainfoV.InformationalVersion + " finished loading."); } // Alt-F10 shows the contract configurator window if (GameSettings.MODIFIER_KEY.GetKey() && Input.GetKeyDown(KeyCode.F10)) { DebugWindow.showGUI = !DebugWindow.showGUI; } // Display reloading message if (reloading) { if (lastMessage != null) { ScreenMessages.RemoveMessage(lastMessage); lastMessage = null; } switch (reloadStep) { case ReloadStep.GAME_DATABASE: lastMessage = ScreenMessages.PostScreenMessage("Reloading game database...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; case ReloadStep.MODULE_MANAGER: lastMessage = ScreenMessages.PostScreenMessage("Reloading module manager...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; case ReloadStep.CLEAR_CONFIG: lastMessage = ScreenMessages.PostScreenMessage("Clearing previously loaded contract configuration...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; case ReloadStep.LOAD_CONFIG: lastMessage = ScreenMessages.PostScreenMessage("Loading contract configuration (" + attemptedContracts + "/" + totalContracts + ")...", Time.deltaTime, ScreenMessageStyle.UPPER_CENTER); break; } } }
/* * Loads all the contact configuration nodes and creates ContractType objects. */ void LoadContractConfig() { LoggingUtil.LogDebug(this.GetType(), "Loading CONTRACT_TYPE nodes."); ConfigNode[] contractConfigs = GameDatabase.Instance.GetConfigNodes("CONTRACT_TYPE"); // First pass - create all the ContractType objects foreach (ConfigNode contractConfig in contractConfigs) { totalContracts++; LoggingUtil.LogVerbose(this.GetType(), "Pre-load for node: '" + contractConfig.GetValue("name") + "'"); // Create the initial contract type try { ContractType contractType = new ContractType(contractConfig.GetValue("name")); } catch (ArgumentException) { LoggingUtil.LogError(this.GetType(), "Couldn't load CONTRACT_TYPE '" + contractConfig.GetValue("name") + "' due to a duplicate name."); // BUG: The same contract will get loaded twice, but just decrement the success counter so one shows as failed successContracts--; } } // Second pass - do the actual loading of details foreach (ConfigNode contractConfig in contractConfigs) { // Fetch the contractType string name = contractConfig.GetValue("name"); ContractType contractType = ContractType.contractTypes[name]; bool success = false; if (contractType != null) { LoggingUtil.LogDebug(this.GetType(), "Loading CONTRACT_TYPE: '" + name + "'"); // Perform the load try { if (contractType.Load(contractConfig)) { successContracts++; success = true; } } catch (Exception e) { Exception wrapper = new Exception("Error loading CONTRACT_TYPE '" + name + "'", e); Debug.LogException(wrapper); } finally { if (!success) { ContractType.contractTypes.Remove(name); } } } } LoggingUtil.LogInfo(this.GetType(), "Loaded " + successContracts + " out of " + totalContracts + " CONTRACT_TYPE nodes."); if (!reloading && LoggingUtil.logLevel == LoggingUtil.LogLevel.DEBUG || LoggingUtil.logLevel == LoggingUtil.LogLevel.VERBOSE) { ScreenMessages.PostScreenMessage("Contract Configurator: Loaded " + successContracts + " out of " + totalContracts + " contracts successfully.", 5, ScreenMessageStyle.UPPER_CENTER); } }