/// <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."); }
private void SaveConfig() { try { if (!Directory.Exists(ModDataDirectory)) { Directory.CreateDirectory(ModDataDirectory); } if (Config == null) { Config = new CModConfig(); } File.WriteAllText( ModConfigFilePath, JsonConvert.SerializeObject( Config, Formatting.Indented, new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Include }) ?? throw new System.Exception("Failed to serialize")); } catch (System.Exception _e) { CUtil.LogW("Failed to save mod configuration: " + _e.Message); } }
private void LoadConfig() { try { if (!File.Exists(ModConfigFilePath)) { SaveConfig(); return; } Config = JsonConvert.DeserializeObject <CModConfig>(File.ReadAllText(ModConfigFilePath)) ?? throw new System.Exception("Deserialisation failed."); } catch (System.Exception _e) { CUtil.LogW("Failed to load mod configuration: " + _e.Message + ". Check your configuration file."); } }
private void LoadLightData() { try { if (!File.Exists(ModDataFilePath)) { return; } CLightData[] data = JsonConvert.DeserializeObject <CLightData[]>(File.ReadAllText(ModDataFilePath)) ?? throw new System.Exception("Deserialisation failed."); SavedLightData = data .GroupBy(_o => _o.SaveName) .Select(_o => new KeyValuePair <string, CLightData[]>(_o.Key, _o.ToArray())) .ToDictionary(_o => _o.Key, _o => _o.Value); } catch (System.Exception _e) { CUtil.LogW("Failed to load saved mod data: " + _e.Message + ". Light data wont be loaded."); SavedLightData = new Dictionary <string, CLightData[]>(); } }
private void SaveLightData() { try { if (File.Exists(ModDataFilePath)) { File.Delete(ModDataFilePath); } if (SavedLightData.ContainsKey(SaveAndLoad.CurrentGameFileName)) { SavedLightData.Remove(SaveAndLoad.CurrentGameFileName); } SavedLightData.Add( SaveAndLoad.CurrentGameFileName, mi_sceneLights .Where(_o => _o.LightSwitch.UserControlsState) .Select(_o => new CLightData(SaveAndLoad.CurrentGameFileName, _o.LightSwitch.ObjectIndex, _o.LightSwitch.IsOn)) .ToArray()); File.WriteAllText( ModDataFilePath, JsonConvert.SerializeObject( SavedLightData.SelectMany(_o => _o.Value).ToArray(), Formatting.None, new JsonSerializerSettings() { DefaultValueHandling = DefaultValueHandling.Ignore }) ?? throw new System.Exception("Failed to serialize")); } catch (System.Exception _e) { CUtil.LogW("Failed to save mod data: " + _e.Message + ". Light data wont be saved."); } }