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!"); }