protected override void OnLoad(ConfigNode node) { contractType = ContractType.contractTypes[node.GetValue("subtype")]; foreach (ConfigNode child in node.GetNodes("BEHAVIOUR")) { ContractBehaviour behaviour = ContractBehaviour.LoadBehaviour(child, this); behaviours.Add(behaviour); } }
/* * Generates all the ContractBehaviour objects required for the array of ConfigNodes, and * adds them to the host object. */ public static void GenerateBehaviours(ConfiguredContract contract, List <BehaviourFactory> behaviourNodes) { foreach (BehaviourFactory behaviourFactory in behaviourNodes) { ContractBehaviour behaviour = behaviourFactory.Generate(contract); if (behaviour == null) { throw new Exception(behaviourFactory.GetType().FullName + ".Generate() returned a null ContractBehaviour!"); } // Add ContractBehaviour to the host contract.AddBehaviour(behaviour); } }
protected override void OnLoad(ConfigNode node) { try { subType = node.GetValue("subtype"); contractType = string.IsNullOrEmpty(subType) ? null : ContractType.GetContractType(subType); title = ConfigNodeUtil.ParseValue <string>(node, "title", contractType != null ? contractType.title : subType); description = ConfigNodeUtil.ParseValue <string>(node, "description", contractType != null ? contractType.description : ""); synopsis = ConfigNodeUtil.ParseValue <string>(node, "synopsis", contractType != null ? contractType.synopsis : ""); completedMessage = ConfigNodeUtil.ParseValue <string>(node, "completedMessage", contractType != null ? contractType.completedMessage : ""); notes = ConfigNodeUtil.ParseValue <string>(node, "notes", contractType != null ? contractType.notes : ""); hash = ConfigNodeUtil.ParseValue <int>(node, "hash", contractType != null ? contractType.hash : 0); foreach (ConfigNode child in node.GetNodes("BEHAVIOUR")) { ContractBehaviour behaviour = ContractBehaviour.LoadBehaviour(child, this); behaviours.Add(behaviour); } // If the contract type is null, then it likely means that it was uninstalled if (contractType == null) { LoggingUtil.LogWarning(this, "Error loading contract for contract type '" + subType + "'. The contract type either failed to load or was uninstalled."); try { SetState(State.Failed); } catch { } return; } } catch (Exception e) { LoggingUtil.LogError(this, "Error loading contract from persistance file!"); LoggingUtil.LogException(e); ExceptionLogWindow.DisplayFatalException(ExceptionLogWindow.ExceptionSituation.CONTRACT_LOAD, e, this); try { SetState(State.Failed); } catch { } } }
/// <summary> /// Loads a behaviour from a ConfigNode. /// </summary> /// <param name="configNode"></param> /// <param name="contract"></param> /// <returns></returns> public static ContractBehaviour LoadBehaviour(ConfigNode configNode, ConfiguredContract contract) { // Determine the type string typeName = configNode.GetValue("type"); Type type = ContractConfigurator.GetAllTypes <ContractBehaviour>().Where(t => t.FullName == typeName).FirstOrDefault(); if (type == null) { throw new Exception("No ContractBehaviour with type = '" + typeName + "'."); } // Instantiate and load ContractBehaviour behaviour = (ContractBehaviour)Activator.CreateInstance(type); behaviour.contract = contract; behaviour.Load(configNode); return(behaviour); }
/// <summary> /// Loads a behaviour from a ConfigNode. /// </summary> /// <param name="configNode"></param> /// <param name="contract"></param> /// <returns></returns> public static ContractBehaviour LoadBehaviour(ConfigNode configNode, ConfiguredContract contract) { // Determine the type string typeName = configNode.GetValue("type"); Type type = null; typeMap.TryGetValue(typeName, out type); if (type == null) { throw new Exception("No ContractBehaviour with type = '" + typeName + "'."); } // Instantiate and load ContractBehaviour behaviour = (ContractBehaviour)Activator.CreateInstance(type); behaviour.contract = contract; behaviour.Load(configNode); return(behaviour); }
/* * Loads a behaviour from a ConfigNode. */ public static ContractBehaviour LoadBehaviour(ConfigNode configNode, ConfiguredContract contract) { // Determine the type string typeName = configNode.GetValue("type"); Type type = Type.GetType(typeName); if (type == null) { LoggingUtil.LogError(typeof(ContractBehaviour), "No ContractBehaviour with type = '" + typeName + "'."); return(null); } // Instantiate and load ContractBehaviour behaviour = (ContractBehaviour)Activator.CreateInstance(type); behaviour.contract = contract; behaviour.Load(configNode); return(behaviour); }
/* * Adds a new behaviour to our list. */ public void AddBehaviour(ContractBehaviour behaviour) { behaviour.contract = this; behaviours.Add(behaviour); }
/// <summary> /// Adds a new behaviour to our list. /// </summary> /// <param name="behaviour">The behaviour to add</param> public void AddBehaviour(ContractBehaviour behaviour) { behaviour.contract = this; behaviours.Add(behaviour); }
protected override void OnLoad(ConfigNode node) { try { subType = node.GetValue("subtype"); contractType = ContractType.GetContractType(subType); title = ConfigNodeUtil.ParseValue <string>(node, "title", contractType != null ? contractType.title : subType); description = ConfigNodeUtil.ParseValue <string>(node, "description", contractType != null ? contractType.description : ""); synopsis = ConfigNodeUtil.ParseValue <string>(node, "synopsis", contractType != null ? contractType.synopsis : ""); completedMessage = ConfigNodeUtil.ParseValue <string>(node, "completedMessage", contractType != null ? contractType.completedMessage : ""); notes = ConfigNodeUtil.ParseValue <string>(node, "notes", contractType != null ? contractType.notes : ""); hash = ConfigNodeUtil.ParseValue <int>(node, "hash", contractType != null ? contractType.hash : 0); targetBody = ConfigNodeUtil.ParseValue <CelestialBody>(node, "targetBody", null); // Load the unique data ConfigNode dataNode = node.GetNode("UNIQUE_DATA"); if (dataNode != null) { // Handle individual values foreach (ConfigNode.Value pair in dataNode.values) { string typeName = pair.value.Remove(pair.value.IndexOf(":")); string value = pair.value.Substring(typeName.Length + 1); Type type = ConfigNodeUtil.ParseTypeValue(typeName); // Prevents issues with vessels not getting loaded in some scenes (ie. VAB) if (type == typeof(Vessel)) { type = typeof(Guid); } if (type == typeof(string)) { uniqueData[pair.name] = value; } else { // Get the ParseValue method MethodInfo parseValueMethod = typeof(ConfigNodeUtil).GetMethods().Where(m => m.Name == "ParseSingleValue").Single(); parseValueMethod = parseValueMethod.MakeGenericMethod(new Type[] { type }); // Invoke the ParseValue method uniqueData[pair.name] = parseValueMethod.Invoke(null, new object[] { pair.name, value, false }); } } } foreach (ConfigNode child in node.GetNodes("BEHAVIOUR")) { ContractBehaviour behaviour = ContractBehaviour.LoadBehaviour(child, this); behaviours.Add(behaviour); } foreach (ConfigNode child in node.GetNodes("REQUIREMENT")) { ContractRequirement requirement = ContractRequirement.LoadRequirement(child); requirements.Add(requirement); } // If the contract type is null, then it likely means that it was uninstalled if (contractType == null) { LoggingUtil.LogWarning(this, "Error loading contract for contract type '" + subType + "'. The contract type either failed to load or was uninstalled."); try { if (ContractState == State.Active || ContractState == State.Offered) { SetState(ContractState == State.Active ? State.Failed : State.Withdrawn); } } catch { } return; } OnContractLoaded.Fire(this); } catch (Exception e) { LoggingUtil.LogError(this, "Error loading contract from persistance file!"); LoggingUtil.LogException(e); ExceptionLogWindow.DisplayFatalException(ExceptionLogWindow.ExceptionSituation.CONTRACT_LOAD, e, this); try { SetState(State.Failed); } catch { } } }