/// <summary> /// Called when the mod is unloaded /// </summary> private void OnDestroy() { Get = null; //undo all changes to the scene if (mi_sceneLights != null) { foreach (var light in mi_sceneLights) { RestoreLightSource(light); if (light.LightSwitch) { Component.DestroyImmediate(light.LightSwitch); } if (light.Raycastable) { Component.DestroyImmediate(light.Raycastable); } } } if (mi_harmony != null) { mi_harmony.UnpatchAll(APP_IDENT); } }
/// <summary> /// Called when the mod is loaded. /// </summary> private void Start() { if (Get != null) { DestroyImmediate(Get); CUtil.LogW("Mod has been loaded twice. Destroying old mod instance."); } Get = this; LoadConfig(); mi_harmony = new Harmony(APP_IDENT); mi_harmony.PatchAll(Assembly.GetExecutingAssembly()); mi_settings = ComponentManager <Settings> .Value; if (RAPI.IsCurrentSceneGame()) { LoadLights(); //mod was reloaded from game } LoadLightData(); CUtil.Log("LanternShadows v. " + VERSION + " loaded."); }
/// <summary> /// Initializes the lantern switch behaviour providing a mod handle and a scene light wrapper. /// </summary> /// <param name="_mod">A handle to the mod object initializing this switch.</param> /// <param name="_light">Contains references to the block and other components.</param> public void Load(CLanternShadows _mod, SSceneLight _light) { mi_mod = _mod; mi_sceneLight = _light; mi_nlCntrl = mi_sceneLight.LightComponent.GetComponent <NightLightController>(); mi_setLightIntensityInfo = typeof(NightLightController).GetMethod("SetLightIntensity", BindingFlags.NonPublic | BindingFlags.Instance); mi_network = ComponentManager <Semih_Network> .Value; //use our block objects index so we receive RPC calls //need to use an existing blockindex as clients/host need to be aware of it ObjectIndex = mi_sceneLight.BlockObject.ObjectIndex; NetworkIDManager.AddNetworkID(this); CheckLightState(true); mi_loaded = true; if (!Semih_Network.IsHost) //request lantern states from host after load { mi_network.SendP2P( mi_network.HostID, new Message_Battery_OnOff( //just use the battery message as it should never Messages.Battery_OnOff, mi_network.NetworkIDManager, mi_network.LocalSteamID, this.ObjectIndex, (int)ELanternRequestType.REQUEST_STATE, //we use the battery uses int to pass our custom command type IsOn), EP2PSend.k_EP2PSendReliable, NetworkChannel.Channel_Game); } else if (mi_mod.SavedLightData.ContainsKey(SaveAndLoad.CurrentGameFileName)) { var data = mi_mod.SavedLightData[SaveAndLoad.CurrentGameFileName].FirstOrDefault(_o => _o.ObjectID == ObjectIndex); if (data != null) { UserControlsState = true; SetLightOn(data.IsOn); } } }