private void updateTextFile(int buttonId, String url, String path, String filename, String description, Button button, PictureBox pic, ref String newContents)
{
enableButtons(false);
shouldBeEnabled[buttonId] = false;
if (readyToGo[buttonId])
{
readyToGo[buttonId] = false;
if (newContents == null)
{
MessageBox.Show(this, "Sorry, no data found. Please close the update window and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
enableButtons(true);
pic.Image = Properties.Resources.x;
return;
}
button.Text = String.Format("Writing updates to {0}.", filename);
button.Refresh();
using (StreamWriter sw = new StreamWriter(path, false))
{
sw.Write(newContents);
sw.Close();
}
button.Text = String.Format("You now have the latest {0}.", description);
pic.Image = Properties.Resources.check;
if (buttonId == Blocks)
{
ColorPalette.Reset();
ColorPalette.Preload();
}
else if (buttonId == Biomes)
{
BiomeType.Reset();
((Form1)Owner).FillLists();
}
enableButtons(true);
return;
}
enableButtons(false);
shouldBeEnabled[buttonId] = false;
button.Text = String.Format("Checking for updates to {0}.", filename);
button.Refresh();
String json = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Headers["Accept-Encoding"] = "gzip,deflate";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.UserAgent = userAgent;
request.Proxy = null;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader responseStream = new StreamReader(response.GetResponseStream());
json = responseStream.ReadToEnd();
response.Close();
responseStream.Close();
}
catch (Exception)
{
button.Text = "Unable to connect to GitHub. Please try again later.";
shouldBeEnabled[buttonId] = true;
readyToGo[buttonId] = false;
enableButtons(true);
pic.Image = Properties.Resources.x;
return;
}
json = json.Replace("\\n", "");
String serverSha = new Regex("[\"']sha[\"']: ?\"([^\"']+)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline).Match(json).Groups[1].Value;
byte[] raw = File.ReadAllBytes(path);
byte[] head = Encoding.UTF8.GetBytes("blob " + raw.Length.ToString() + "\0");
byte[] combined = new byte[head.Length + raw.Length];
head.CopyTo(combined, 0);
raw.CopyTo(combined, head.Length);
SHA1 sha1 = new SHA1CryptoServiceProvider();
String localSha = BitConverter.ToString(sha1.ComputeHash(combined)).Replace("-", "").ToLower();
if (serverSha == localSha)
{
button.Text = String.Format("You have the latest {0}. No action is necessary.", description);
shouldBeEnabled[buttonId] = false;
readyToGo[buttonId] = false;
enableButtons(true);
pic.Image = Properties.Resources.check;
}
else
{
newContents = new Regex("[\"']content[\"']: ?\"([^\"']+)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline).Match(json).Groups[1].Value;
newContents = Encoding.UTF8.GetString(Convert.FromBase64String(newContents));
button.Text = String.Format("Updated {0} available! Click to save a new copy of {1}.", description, filename);
shouldBeEnabled[buttonId] = true;
readyToGo[buttonId] = true;
enableButtons(true);
pic.Image = Properties.Resources.bang;
}
}