public void Save() { try { if (metaStorage != null && metaStorage.NativeInstance != IntPtr.Zero) { metaStorage.SetMeta(WorkTimerStringPrefix + nameof(CurrentWorkTime), CurrentWorkTime.Ticks.ToString()); metaStorage.SetMeta(WorkTimerStringPrefix + nameof(IsCounterTempDisabled), ((int)IsCounterTempDisabled).ToString()); metaStorage.SetMeta(WorkTimerStringPrefix + nameof(TempDisableStartTime), TempDisableStartTime.Ticks.ToString()); } var file = new File(); var dir = new Directory(); if (file.FileExists(saveFile)) { if (file.FileExists(saveFileBak)) { if (dir.Remove(saveFileBak) != Error.Ok) { GD.PrintErr($"{LogPrefix}Can't remove backup file: {saveFileBak}"); return; } } if (dir.Rename(saveFile, saveFileBak) != Error.Ok) { GD.PrintErr($"{LogPrefix}Can't rename backup file: {saveFileBak}"); return; } } else { if (dir.MakeDirRecursive(saveFile.GetBaseDir()) != Error.Ok) { GD.PrintErr($"{LogPrefix}Can't make directory for save file: {saveFile.GetBaseDir()}"); return; } } var dict = new Godot.Collections.Dictionary { // Store data ["fist_launch"] = FirstLaunch.Ticks.ToString(), ["time"] = TotalWorkTime.Ticks.ToString(), ["plays"] = TotalPlayTimes.ToString(), ["builds"] = TotalBuildTimes.ToString(), ["exports"] = TotalExportTimes.ToString(), ["temp_disable_delay"] = TempDisableDelayMinutes.ToString(), ["enabled"] = IsCounterEnabled.ToString(), ["discord"] = IsDiscordEnabled.ToString(), }; var text = JSON.Print(dict, "", true); if (file.Open(saveFile, File.ModeFlags.Write) == Error.Ok) { file.StorePascalString(text); file.Close(); } else { GD.PrintErr($"{LogPrefix}Can't open save file for writing: {saveFile}"); return; } } catch (Exception e) { GD.PrintErr($"{LogPrefix}Exception when saving: {e.Message}\n{e.StackTrace}"); return; } }
void Load() { try { if (metaStorage != null && metaStorage.NativeInstance != IntPtr.Zero) { if (metaStorage.HasMeta(WorkTimerStringPrefix + nameof(CurrentWorkTime))) { CurrentWorkTime = new TimeSpan(long.Parse((string)metaStorage.GetMeta(WorkTimerStringPrefix + nameof(CurrentWorkTime)))); } // Temporary disabling if (metaStorage.HasMeta(WorkTimerStringPrefix + nameof(IsCounterTempDisabled))) { IsCounterTempDisabled = (TempDisabledState)int.Parse((string)metaStorage.GetMeta(WorkTimerStringPrefix + nameof(IsCounterTempDisabled))); } if (metaStorage.HasMeta(WorkTimerStringPrefix + nameof(TempDisableStartTime))) { TempDisableStartTime = new DateTime(long.Parse((string)metaStorage.GetMeta(WorkTimerStringPrefix + nameof(TempDisableStartTime)))); } } var file = new File(); if (file.FileExists(saveFile)) { if (file.Open(saveFile, File.ModeFlags.Read) == Error.Ok) { var text = file.GetPascalString(); file.Close(); var res = JSON.Parse(text); if (res.Error == Error.Ok && res.Result is Godot.Collections.Dictionary dict) { if (dict != null) { string getValue(string key, string def) { if (dict.Contains(key)) { return((string)(dict[key])); } return(def); }; // Loading data FirstLaunch = new DateTime(long.Parse(getValue("fist_launch", FirstLaunch.Ticks.ToString()))); TotalWorkTime = new TimeSpan(long.Parse(getValue("time", TotalWorkTime.Ticks.ToString()))); TotalBuildTimes = int.Parse(getValue("builds", TotalBuildTimes.ToString())); TotalPlayTimes = int.Parse(getValue("plays", TotalPlayTimes.ToString())); TotalExportTimes = int.Parse(getValue("exports", TotalExportTimes.ToString())); TempDisableDelayMinutes = int.Parse(getValue("temp_disable_delay", TempDisableDelayMinutes.ToString())); IsCounterEnabled = bool.Parse(getValue("enabled", IsCounterEnabled.ToString())); IsDiscordEnabled = bool.Parse(getValue("discord", IsDiscordEnabled.ToString())); } } } } } catch (Exception e) { GD.PrintErr($"{LogPrefix}{e.Message}"); } }