public override void OnLoad(ConfigNode gameNode) { try { CNCLog.Verbose("Scenario content to be read:\n{0}", gameNode); //Other variables for (int i = 0; i < gameNode.values.Count; i++) { ConfigNode.Value value = gameNode.values[i]; string name = value.name; switch (name) { case "DisplayModeTracking": CNCCommNetUI.CustomModeTrackingStation = (CNCCommNetUI.CustomDisplayMode)((int)Enum.Parse(typeof(CNCCommNetUI.CustomDisplayMode), value.value)); break; case "DisplayModeFlight": CNCCommNetUI.CustomModeFlightMap = (CNCCommNetUI.CustomDisplayMode)((int)Enum.Parse(typeof(CNCCommNetUI.CustomDisplayMode), value.value)); break; case "HideGroundStations": this.hideGroundStations = Boolean.Parse(value.value); break; } } //Constellations if (gameNode.HasNode("Constellations")) { ConfigNode constellationNode = gameNode.GetNode("Constellations"); ConfigNode[] constellationNodes = constellationNode.GetNodes(); if (constellationNodes.Length < 1) // missing constellation list { CNCLog.Error("The 'Constellations' node is malformed! Reverted to the default constellation list."); constellations = CNCSettings.Instance.Constellations; } else { constellations.Clear(); for (int i = 0; i < constellationNodes.Length; i++) { Constellation newConstellation = new Constellation(); ConfigNode.LoadObjectFromConfig(newConstellation, constellationNodes[i]); constellations.Add(newConstellation); } } } else { CNCLog.Verbose("The 'Constellations' node is not found. The default constellation list is loaded."); constellations = CNCSettings.Instance.Constellations; } constellations.Sort(); //Ground stations if (gameNode.HasNode("GroundStations")) { ConfigNode stationNode = gameNode.GetNode("GroundStations"); ConfigNode[] stationNodes = stationNode.GetNodes(); if (stationNodes.Length < 1) // missing ground-station list { CNCLog.Error("The 'GroundStations' node is malformed! Reverted to the default list of ground stations."); persistentGroundStations = CNCSettings.Instance.GroundStations; } else { persistentGroundStations.Clear(); for (int i = 0; i < stationNodes.Length; i++) { CNCCommNetHome dummyGroundStation = new CNCCommNetHome(); ConfigNode.LoadObjectFromConfig(dummyGroundStation, stationNodes[i]); if (!stationNodes[i].HasNode("Frequencies")) // empty list is not saved as empty node in persistent.sfs { dummyGroundStation.deleteFrequencies(); // clear the default frequency list } persistentGroundStations.Add(dummyGroundStation); } } } else { CNCLog.Verbose("The 'GroundStations' node is not found. The default list of ground stations is loaded from KSP's data."); persistentGroundStations = CNCSettings.Instance.GroundStations; } } catch (Exception e) { CNCLog.Error("Error when loading CNCCommNetScenario: {0}", e.Message); } }
protected override void Start() { CNCCommNetScenario.Instance = this; this.commVessels = new List <CNCCommNetVessel>(); this.dirtyCommNetVesselList = true; CNCLog.Verbose("CommNet Scenario loading ..."); //Issue #13: Commnet behaves like vanilla when joining a DMP server for the second time. //if stock CommNet logic somehow runs (such as the order of CNCCommNetScenario and CommNetScenario in persisten.sfs) if (CommNetScenario.Instance != null) { UnityEngine.Object.DestroyImmediate(CommNetScenario.Instance); CNCCommNetScenario.Instance = this; } //Replace the CommNet user interface CommNetUI ui = FindObjectOfType <CommNetUI>(); // the order of the three lines is important CustomCommNetUI = gameObject.AddComponent <CNCCommNetUI>(); // gameObject.AddComponent<>() is "new" keyword for Monohebaviour class UnityEngine.Object.Destroy(ui); //Replace the CommNet network CommNetNetwork net = FindObjectOfType <CommNetNetwork>(); CustomCommNetNetwork = gameObject.AddComponent <CNCCommNetNetwork>(); UnityEngine.Object.Destroy(net); //CommNetNetwork.Instance.GetType().GetMethod("set_Instance").Invoke(CustomCommNetNetwork, null); // reflection to bypass Instance's protected set // don't seem to work //Replace the TelemetryUpdate TelemetryUpdate tel = TelemetryUpdate.Instance; //only appear in flight CommNetUIModeButton cnmodeUI = FindObjectOfType <CommNetUIModeButton>(); //only appear in tracking station; initialised separately by TelemetryUpdate in flight if (tel != null && HighLogic.LoadedSceneIsFlight) { TelemetryUpdateData tempData = new TelemetryUpdateData(tel); UnityEngine.Object.DestroyImmediate(tel); //seem like UE won't initialise CNCTelemetryUpdate instance in presence of TelemetryUpdate instance CustomCommNetTelemetry = gameObject.AddComponent <CNCTelemetryUpdate>(); CustomCommNetTelemetry.copyOf(tempData); } else if (cnmodeUI != null && HighLogic.LoadedScene == GameScenes.TRACKSTATION) { CustomCommNetModeButton = cnmodeUI.gameObject.AddComponent <CNCCommNetUIModeButton>(); CustomCommNetModeButton.copyOf(cnmodeUI); UnityEngine.Object.DestroyImmediate(cnmodeUI); } //Replace the CommNet ground stations groundStations.Clear(); CommNetHome[] homes = FindObjectsOfType <CommNetHome>(); for (int i = 0; i < homes.Length; i++) { CNCCommNetHome customHome = homes[i].gameObject.AddComponent(typeof(CNCCommNetHome)) as CNCCommNetHome; customHome.copyOf(homes[i]); UnityEngine.Object.Destroy(homes[i]); groundStations.Add(customHome); } groundStations.Sort(); //Apply the ground-station changes from persistent.sfs for (int i = 0; i < persistentGroundStations.Count; i++) { if (groundStations.Exists(x => x.ID.Equals(persistentGroundStations[i].ID))) { groundStations.Find(x => x.ID.Equals(persistentGroundStations[i].ID)).applySavedChanges(persistentGroundStations[i]); } } persistentGroundStations.Clear();//dont need anymore //Replace the CommNet celestial bodies CommNetBody[] bodies = FindObjectsOfType <CommNetBody>(); for (int i = 0; i < bodies.Length; i++) { CNCCommNetBody customBody = bodies[i].gameObject.AddComponent(typeof(CNCCommNetBody)) as CNCCommNetBody; customBody.copyOf(bodies[i]); UnityEngine.Object.Destroy(bodies[i]); } //Imitate stock CommNetScenario.Instance in order to run certain stock functionalities //Comment: Vessel.GetControlLevel() has the check on CommNetScenario.Instance != null before calling vessel.connection.GetControlLevel() PropertyInfo property = typeof(CommNetScenario).GetProperty("Instance"); property.DeclaringType.GetProperty("Instance"); property.SetValue(CommNetScenario.Instance, this, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null); CNCLog.Verbose("CommNet Scenario loading done!"); }
protected override void Start() { CNCCommNetScenario.Instance = this; this.commVessels = new List <CNCCommNetVessel>(); this.dirtyCommNetVesselList = true; CNCLog.Verbose("CommNet Scenario loading ..."); //Replace the CommNet user interface CommNetUI ui = FindObjectOfType <CommNetUI>(); // the order of the three lines is important CustomCommNetUI = gameObject.AddComponent <CNCCommNetUI>(); // gameObject.AddComponent<>() is "new" keyword for Monohebaviour class UnityEngine.Object.Destroy(ui); //Replace the CommNet network CommNetNetwork net = FindObjectOfType <CommNetNetwork>(); CustomCommNetNetwork = gameObject.AddComponent <CNCCommNetNetwork>(); UnityEngine.Object.Destroy(net); //CommNetNetwork.Instance.GetType().GetMethod("set_Instance").Invoke(CustomCommNetNetwork, null); // reflection to bypass Instance's protected set // don't seem to work //Replace the TelemetryUpdate TelemetryUpdate tel = TelemetryUpdate.Instance; //only appear in flight CommNetUIModeButton cnmodeUI = FindObjectOfType <CommNetUIModeButton>(); //only appear in tracking station; initialised separately by TelemetryUpdate in flight if (tel != null && HighLogic.LoadedSceneIsFlight) { TelemetryUpdateData tempData = new TelemetryUpdateData(tel); UnityEngine.Object.DestroyImmediate(tel); //seem like UE won't initialise CNCTelemetryUpdate instance in presence of TelemetryUpdate instance CustomCommNetTelemetry = gameObject.AddComponent <CNCTelemetryUpdate>(); CustomCommNetTelemetry.copyOf(tempData); } else if (cnmodeUI != null && HighLogic.LoadedScene == GameScenes.TRACKSTATION) { CustomCommNetModeButton = cnmodeUI.gameObject.AddComponent <CNCCommNetUIModeButton>(); CustomCommNetModeButton.copyOf(cnmodeUI); UnityEngine.Object.DestroyImmediate(cnmodeUI); } //Replace the CommNet ground stations groundStations.Clear(); CommNetHome[] homes = FindObjectsOfType <CommNetHome>(); for (int i = 0; i < homes.Length; i++) { CNCCommNetHome customHome = homes[i].gameObject.AddComponent(typeof(CNCCommNetHome)) as CNCCommNetHome; customHome.copyOf(homes[i]); UnityEngine.Object.Destroy(homes[i]); groundStations.Add(customHome); } groundStations.Sort(); //Apply the ground-station changes from persistent.sfs for (int i = 0; i < persistentGroundStations.Count; i++) { if (groundStations.Exists(x => x.ID.Equals(persistentGroundStations[i].ID))) { groundStations.Find(x => x.ID.Equals(persistentGroundStations[i].ID)).applySavedChanges(persistentGroundStations[i]); } } persistentGroundStations.Clear();//dont need anymore //Replace the CommNet celestial bodies CommNetBody[] bodies = FindObjectsOfType <CommNetBody>(); for (int i = 0; i < bodies.Length; i++) { CNCCommNetBody customBody = bodies[i].gameObject.AddComponent(typeof(CNCCommNetBody)) as CNCCommNetBody; customBody.copyOf(bodies[i]); UnityEngine.Object.Destroy(bodies[i]); } CNCLog.Verbose("CommNet Scenario loading done!"); }