Files
SaveWizard_rewritten/UpdateForm.cs

81 lines
2.9 KiB
C#

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace SaveWizard_rewritten
{
public partial class UpdateForm: Form
{
private static readonly string githubLink = "https://raw.githubusercontent.com/JDM170/SaveWizard/configs/";
public UpdateForm()
{
InitializeComponent();
}
private static void CheckPath(string path)
{
string currentDirectory = Environment.CurrentDirectory;
foreach (string unit in Regex.Split(path, "/"))
{
currentDirectory = Path.Combine(currentDirectory, unit);
if (!unit.Contains(".json"))
{
if (!Directory.Exists(currentDirectory))
Directory.CreateDirectory(currentDirectory);
}
else
{
if (!File.Exists(currentDirectory))
File.WriteAllText(currentDirectory, "");
}
}
}
private void ProgressWindow_Load(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
using (WebClient wb = new WebClient())
{
wb.Encoding = Encoding.UTF8;
string responseInString = wb.DownloadString($"{githubLink}configs/version.cfg");
Dictionary<string, string> result = JsonConvert.DeserializeObject<Dictionary<string, string>>(responseInString);
string[] splitted;
string path;
foreach (KeyValuePair<string, string> unit in result)
{
splitted = Regex.Split(unit.Key, "_");
path = $"configs/{splitted.GetValue(0)}/{splitted.GetValue(1)}.json";
CheckPath(path);
if (Utils.GenerateMD5(path) != unit.Value)
{
string newConfigText = wb.DownloadString($"{githubLink}{path}");
File.WriteAllText(Path.Combine(Environment.CurrentDirectory, path), newConfigText);
}
backgroundWorker1.ReportProgress(progressBar1.Value + 1);
}
}
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lbl_progress.Text = $"{e.ProgressPercentage}/6";
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
this.Close();
}
}
}