Skip to content

Commit

Permalink
Only save changed files
Browse files Browse the repository at this point in the history
  • Loading branch information
Mc-muffin committed Nov 21, 2024
1 parent 1234383 commit 509aed4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
6 changes: 3 additions & 3 deletions TranslationApp/fMain.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions TranslationApp/fMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using TranslationLib;
using PackingLib;
using System.Reflection;
using System.Threading.Tasks;

namespace TranslationApp
{
Expand Down Expand Up @@ -575,8 +576,12 @@ private void LoadEntryData(ListBox lb)

private void bSave_Click(object sender, EventArgs e)
{
Project.XmlFolders.ForEach(f => f.XMLFiles.ForEach(x => x.SaveToDisk()));
MessageBox.Show("Text has been written to the XML files");
int count = 0;
foreach (var folder in Project.XmlFolders)
{
count += folder.SaveChanged();
}
MessageBox.Show($"{count} XML files has been written to disk");

UpdateDisplayedEntries();
UpdateStatusData();
Expand Down Expand Up @@ -939,6 +944,7 @@ private void tbEnglishText_TextChanged(object sender, EventArgs e)

cbStatus.Text = status;
textPreview1.ReDraw(tbEnglishText.Text);
Project.CurrentFolder.CurrentFile.needsSave = true;
}

private void cbStatus_TextChanged(object sender, EventArgs e)
Expand All @@ -950,6 +956,7 @@ private void tbNoteText_TextChanged(object sender, EventArgs e)
if (lbEntries.SelectedIndex > -1 && lbEntries.SelectedIndex < CurrentTextList.Count)
{
CurrentTextList[lbEntries.SelectedIndex].Notes = tbNoteText.Text;
Project.CurrentFolder.CurrentFile.needsSave = true;
}
}

Expand Down Expand Up @@ -1243,6 +1250,7 @@ private void btnRefresh_Click(object sender, EventArgs e)
{
DisableEventHandlers();
Project.CurrentFolder.XMLFiles[cbFileList.SelectedIndex] = Project.CurrentFolder.LoadXML(Project.CurrentFolder.CurrentFile.FilePath);
Project.CurrentFolder.CurrentFile = Project.CurrentFolder.XMLFiles[cbFileList.SelectedIndex];
Project.CurrentFolder.InvalidateTranslations();
EnableEventHandlers();

Expand Down Expand Up @@ -1309,7 +1317,7 @@ private void cbEmpty_CheckedChanged(object sender, EventArgs e)
{
setEmpty(lbSpeaker);
}

Project.CurrentFolder.CurrentFile.needsSave = true;
}

private void setEmpty(ListBox lb)
Expand Down Expand Up @@ -1359,6 +1367,7 @@ private void cbStatus_SelectionChangeCommitted(object sender, EventArgs e)
{
entry.Status = cbStatus.Text;
}
Project.CurrentFolder.CurrentFile.needsSave = true;
UpdateStatusData();
}

Expand All @@ -1384,7 +1393,7 @@ private void reloadCurrentFileToolStripMenuItem_Click(object sender, EventArgs e
}
private void saveAllToolStripMenuItem_Click(object sender, EventArgs e)
{
Project.XmlFolders.ForEach(f => f.XMLFiles.ForEach(x => x.SaveToDisk()));
Project.XmlFolders.ForEach(f => f.XMLFiles.AsParallel().ForAll(x => x.SaveToDisk()));
MessageBox.Show("Text has been written to the XML files");
UpdateDisplayedEntries();
UpdateStatusData();
Expand Down Expand Up @@ -1431,6 +1440,7 @@ private void tbFriendlyName_KeyDown(object sender, KeyEventArgs e)
lbEntries.SelectedIndex = tindex;
e.Handled = true;
e.SuppressKeyPress = true;
Project.CurrentFolder.CurrentFile.needsSave = true;
}
}
private void tbSectionName_KeyDown(object sender, KeyEventArgs e)
Expand All @@ -1446,6 +1456,7 @@ private void tbSectionName_KeyDown(object sender, KeyEventArgs e)
lbEntries.SelectedIndex = tindex;
e.Handled = true;
e.SuppressKeyPress = true;
Project.CurrentFolder.CurrentFile.needsSave = true;
}
}

Expand Down
1 change: 1 addition & 0 deletions TranslationLib/XMLFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class XMLFile
public List<XMLEntry> Speakers = null;
public XMLSection CurrentSection { get; set; }
public bool isLegacy { get; set; }
public bool needsSave { get; set; }

public XMLFile()
{
Expand Down
19 changes: 19 additions & 0 deletions TranslationLib/XMLFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace TranslationLib
Expand Down Expand Up @@ -101,6 +103,23 @@ public XMLFile LoadXML(string xmlpath)
return XMLFile;
}

public int SaveChanged()
{
int count = 0;
Parallel.For(0, XMLFiles.Count(), () => 0, (i, loop, subtotal) =>
{
if (XMLFiles[i].needsSave)
{
XMLFiles[i].SaveToDisk();
XMLFiles[i].needsSave = false;
subtotal++;
}
return subtotal;
},
(x) => Interlocked.Add(ref count, x));
return count;
}

public void InvalidateTranslations()
{
Translations = new Dictionary<string, TranslationEntry>();
Expand Down

0 comments on commit 509aed4

Please sign in to comment.