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; } }
private static void RenderChunkTerrain(Object state) { Object[] parameters = (Object[])state; Chunk c = (Chunk)parameters[0]; Bitmap b = (Bitmap)parameters[1]; int offsetX = (int)parameters[2]; int offsetY = (int)parameters[3]; TAG_Compound[] sections = new TAG_Compound[16]; int highest = -1; foreach (TAG t in (TAG[])c.Root["Level"]["Sections"]) { byte index = (byte)t["Y"]; if (index > highest) { highest = index; } sections[index] = (TAG_Compound)t; } if (c.ManualHeightmap == null) { c.ManualHeightmap = new int[256]; for (int i = 0; i < c.ManualHeightmap.Length; i++) { c.ManualHeightmap[i] = -1; } } //chunk exists but all blocks are air if (highest < 0) { if (Interlocked.Decrement(ref taskCount) == 0) { signal.Set(); } return; } Color[,] pixels = new Color[16, 16]; highest = ((highest + 1) * 16) - 1; TAG biomes = null; c.Root["Level"].TryGetValue("Biomes", out biomes); for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { int y; if (c.ManualHeightmap[x + z * 16] >= 0) { y = c.ManualHeightmap[x + z * 16]; } else { y = GetHeight(sections, x, z, highest); c.ManualHeightmap[x + z * 16] = y; } if (y < 0) { continue; } byte id, data; GetBlock(sections, x, y, z, out id, out data); byte biome = (byte)Biome.Unspecified; if (biomes != null && Settings.BiomeFoliage) { biome = ((byte[])biomes)[x + z * 16]; } Color color = ColorPalette.Lookup(id, data, biome); if (Settings.Transparency) { y--; while (color.A < 255 && y >= 0) { GetBlock(sections, x, y, z, out id, out data); Color c2 = ColorPalette.Lookup(id, data, biome); color = Blend(color, c2); y--; } } else { color = Color.FromArgb(255, color.R, color.G, color.B); } //brighten/darken by height; arbitrary value, but /seems/ to look okay color = AddtoColor(color, (int)(y / 1.7 - 42)); pixels[x, z] = color; } } mutex.WaitOne(); for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { b.SetPixel(offsetX + x, offsetY + z, pixels[x, z]); } } mutex.ReleaseMutex(); if (Interlocked.Decrement(ref taskCount) == 0) { signal.Set(); } }