From 382bf61271d633ccd48b69c1bc7de4a1393168fa Mon Sep 17 00:00:00 2001 From: wcko87 Date: Fri, 12 May 2017 19:52:45 +0800 Subject: [PATCH] Implement the file export. Now the Variable Export fully works. --- rabi_splitter_WPF/RabiRibiDisplay.cs | 13 +---------- rabi_splitter_WPF/VariableExportContext.cs | 3 ++- rabi_splitter_WPF/VariableExportSetting.cs | 25 ++++++++++++++++------ 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/rabi_splitter_WPF/RabiRibiDisplay.cs b/rabi_splitter_WPF/RabiRibiDisplay.cs index 8f482dc..b305d92 100644 --- a/rabi_splitter_WPF/RabiRibiDisplay.cs +++ b/rabi_splitter_WPF/RabiRibiDisplay.cs @@ -117,7 +117,6 @@ namespace rabi_splitter_WPF if (InGame()) { inGameState.nRestarts++; - UpdateTextFile(); } DebugLog("Reload Game! " + snapshot.playtime + " <- " + inGameState.lastNonZeroPlayTime); } @@ -197,7 +196,6 @@ namespace rabi_splitter_WPF if (InGame()) { inGameState.nDeaths++; - UpdateTextFile(); } DebugLog("Death!"); } @@ -296,16 +294,7 @@ namespace rabi_splitter_WPF mainContext.Text20 = bosstext; } } - - private void UpdateTextFile() - { - //return; - string text = $"Deaths: {inGameState.nDeaths}\nResets: {inGameState.nRestarts}"; - System.IO.StreamWriter file = new System.IO.StreamWriter("deaths_restarts.txt"); - file.WriteLine(text); - file.Close(); - } - + private void StartNewGame() { inGameState = new InGameState(); diff --git a/rabi_splitter_WPF/VariableExportContext.cs b/rabi_splitter_WPF/VariableExportContext.cs index 24efff7..8fc82fa 100644 --- a/rabi_splitter_WPF/VariableExportContext.cs +++ b/rabi_splitter_WPF/VariableExportContext.cs @@ -36,7 +36,8 @@ namespace rabi_splitter_WPF foreach (var ves in _variableExportSettings) { - ves.OutputUpdate(variableValues, updateFile); + ves.UpdateText(variableValues); + if (updateFile) ves.MaybeUpdateFile(); } } diff --git a/rabi_splitter_WPF/VariableExportSetting.cs b/rabi_splitter_WPF/VariableExportSetting.cs index d4d95c2..298dfc5 100644 --- a/rabi_splitter_WPF/VariableExportSetting.cs +++ b/rabi_splitter_WPF/VariableExportSetting.cs @@ -82,6 +82,7 @@ namespace rabi_splitter_WPF private string _formatPreview; private bool _isExporting; private bool _isPreviewingFormat; + private bool _hasChangedSinceLastFileOutput; public VariableExportSetting() { @@ -90,6 +91,7 @@ namespace rabi_splitter_WPF _outputFormat = ""; _isExporting = false; _isPreviewingFormat = false; + _hasChangedSinceLastFileOutput = true; } #region Logic @@ -105,17 +107,27 @@ namespace rabi_splitter_WPF } } - internal void OutputUpdate(Dictionary variableValues, bool updateFile) + internal void UpdateText(Dictionary variableValues) { var formattedOutput = FormatOutput(variableValues); - if (formattedOutput == FormatPreview) return; - FormatPreview = formattedOutput; - if (updateFile) + if (formattedOutput != FormatPreview) { - // TODO: Write to file - + FormatPreview = formattedOutput; + _hasChangedSinceLastFileOutput = true; } } + + internal void MaybeUpdateFile() + { + if (!_hasChangedSinceLastFileOutput || !IsExporting) return; + + System.IO.StreamWriter file = new System.IO.StreamWriter(OutputFileName); + file.WriteLine(FormatPreview); + file.Close(); + + _hasChangedSinceLastFileOutput = false; + } + #endregion #region Parameters @@ -128,6 +140,7 @@ namespace rabi_splitter_WPF if (value.Equals(_outputFileName)) return; _outputFileName = value; OnPropertyChanged(nameof(OutputFileName)); + _hasChangedSinceLastFileOutput = true; } }