using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using BetterPatchFileCreator.Code;
using Differ;
using PatcherShared;
namespace BetterPatchFileCreator.XAML.Pages
{
///
/// Interaction logic for CreatingPatch.xaml
///
public partial class CreatingPatch : Page {
private PatchConfig config;
private TaskScheduler uiScheduler;
private TaskFactory uiFactory;
private FileSystem oldFileSys;
private FileSystem newFileSys;
private IEnumerable diffs;
private PatchFile patchFile;
public CreatingPatch(PatchConfig config) {
uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
uiFactory = new TaskFactory(uiScheduler);
this.config = config;
InitializeComponent();
Task.Factory.StartNew(CreatePatch);
}
#region Create-Patch Methods
public void CreatePatch()
{
ReadIndexOfOldClient();
IndexCurrentClient();
CalculatedDiffs();
if(!CheckForDiffs()) return; // No diffs.
EncodePatchFile();
SavePatchFile();
SaveIndex();
SetStatus("Finished.");
Finish();
}
private void ReadIndexOfOldClient() {
SetStatus("Reading index...");
string oldXmlPath = GetXmlPath();
oldFileSys = new FileSystem();
oldFileSys.ParseFromXml(oldXmlPath);
SetProgress(0.1);
}
private void IndexCurrentClient() {
SetStatus("Indexing client...");
newFileSys = new FileSystem();
newFileSys.Parse(config.ClientPath);
SetProgress(1.3);
}
private void CalculatedDiffs() {
SetStatus("Calculating differences...");
diffs = newFileSys.GetDiffs(oldFileSys);
SetProgress(1.6);
}
private bool CheckForDiffs() {
if (diffs.Count() == 0)
{
SetStatus("Finished. No changes detected.");
SetProgress(2.0); // complete
Finish();
return false;
}
return true;
}
private void EncodePatchFile() {
SetStatus("Encoding to patchfile...");
DiffToPatchfileEncoder encoder = new DiffToPatchfileEncoder();
patchFile = encoder.Encode(diffs);
SetProgress(1.8);
}
private void SavePatchFile() {
SetStatus("Saving patchfile...");
if (MessageBox.Show("Patcherpatch?", "Patcherpatch", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes) {
patchFile.SelfPatch = true;
} else {
patchFile.SelfPatch = false;
}
patchFile.Write(GetPatchFilePath());
SetProgress(1.9);
}
private void SaveIndex() {
SetStatus("Saving Index...");
Interlocked.Increment(ref config._v);
newFileSys.SaveToXmlFile(GetXmlPath());
SetProgress(2.0);
}
private string GetXmlPath() {
StringBuilder builder = new StringBuilder();
builder.Append(config.XmlPath);
builder.Append('\\');
builder.Append(config.Name);
builder.Append('-');
builder.Append(config.Version);
builder.Append(".xml");
return builder.ToString();
}
private string GetPatchFilePath() {
StringBuilder builder = new StringBuilder();
builder.Append(config.PatchFilePath);
builder.Append('\\');
builder.Append(config.Name);
builder.Append('-');
builder.Append(config.Version);
builder.Append(".spf");
return builder.ToString();
}
#endregion
#region Methods
#endregion
#region UI-ThreadSafe Methods
private void Finish()
{
uiFactory.StartNew(() => btFinish.Visibility = Visibility.Visible);
}
private void SetProgress(double progress)
{
uiFactory.StartNew(() => Progress.Value = progress);
}
private void SetStatus(string status)
{
uiFactory.StartNew(() => State.Text = status);
}
#endregion
}
}