Advertisement
Guest User

Untitled

a guest
Dec 2nd, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.IO;
  9. using System.Net;
  10.  
  11. namespace Zelda_Oracles_Suite
  12. {
  13.     [DefaultEvent("StatusChanged")]
  14.     public partial class DownloadableListBox : ComboBox
  15.     {
  16.         private string listName;
  17.         [Browsable(true), DefaultValue("")]
  18.         public string ListName { get { return listName; } set { listName = value; } }
  19.  
  20.         public delegate void StatusChangedEventHandler(object sender, StatusEventArgs e);
  21.         public event StatusChangedEventHandler StatusChanged;
  22.  
  23.         private int forceIndexCount = 256;
  24.         [Browsable(true), DefaultValue(256)]
  25.         public int ForceIndexCount { get { return forceIndexCount; } set { forceIndexCount = Math.Max(1, value); ParseListText(); } }
  26.  
  27.         private string displayedCategory = "";
  28.         [Browsable(true), DefaultValue("")]
  29.         public string DisplayedCategory { get { return displayedCategory; } set { displayedCategory = value; ParseListText(); } }
  30.  
  31.         private string listText = "";
  32.  
  33.         public DownloadableListBox()
  34.             : base()
  35.         {
  36.             InitializeComponent();
  37.             this.DropDownStyle = ComboBoxStyle.DropDownList;
  38.         }
  39.  
  40.         public void DownloadList()
  41.         {
  42.             try
  43.             {
  44.                 if (!Directory.Exists("./lists"))
  45.                     Directory.CreateDirectory("./lists");
  46.                 WebClient wb = new WebClient();
  47.                 wb.DownloadFileCompleted += new AsyncCompletedEventHandler(wb_DownloadFileCompleted);
  48.                 wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wb_DownloadProgressChanged);
  49.                 wb.DownloadFileAsync(new Uri(Settings.ListsLocation + listName + ".ini"), "./lists/" + listName + ".ini");
  50.             }
  51.             catch (Exception ex)
  52.             {
  53.                 if (StatusChanged != null)
  54.                     StatusChanged(this, new StatusEventArgs(ex, "Failed to retrieve " + listName + ".ini"));
  55.             }
  56.         }
  57.  
  58.         private void wb_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
  59.         {
  60.             if (e.Cancelled && StatusChanged != null)
  61.                 StatusChanged(this, new StatusEventArgs(e.Error, "Download cancelled for " + listName + ".ini"));
  62.             else if (e.Error != null && StatusChanged != null)
  63.                 StatusChanged(this, new StatusEventArgs(e.Error, "Failed to retrieve " + listName + ".ini"));
  64.             else if (StatusChanged != null)
  65.                 StatusChanged(this, new StatusEventArgs(null, "Successfully retrieved " + listName + ".ini"));
  66.             listText = "";
  67.             ParseListText();
  68.         }
  69.  
  70.         private void wb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  71.         {
  72.             if (StatusChanged != null)
  73.                 StatusChanged(this, new StatusEventArgs(null, "Downloading list " + listName + ".ini - " + (int)((double)e.BytesReceived / (double)e.TotalBytesToReceive * 100) + "%"));
  74.         }
  75.  
  76.         public void ParseListText()
  77.         {
  78.             if (listText.Length == 0)
  79.             {
  80.                 try
  81.                 {
  82.                 Load:
  83.                     if (File.Exists("./lists/" + listName + ".ini"))
  84.                     {
  85.                         StreamReader sr = new StreamReader(File.OpenRead("./lists/" + listName + ".ini"));
  86.                         listText = sr.ReadToEnd();
  87.                         sr.Close();
  88.                     }
  89.                     else if (Settings.AutoDownloadLists)
  90.                     {
  91.                         DownloadList();
  92.                         goto Load;
  93.                     }
  94.                 }
  95.                 catch (Exception)
  96.                 {
  97.  
  98.                 }
  99.             }
  100.  
  101.             DataSource = null;
  102.             Items.Clear();
  103.  
  104.             string[] cboLines = new string[forceIndexCount];
  105.             if (listText.Length > 0)
  106.             {
  107.                 string[] lines = listText.Replace("\r", "").Split('\n');
  108.                 string currentCategory = "";
  109.                 foreach (string line in lines)
  110.                 {
  111.                     if (line.Length == 0)
  112.                         continue;
  113.                     if (line.StartsWith("["))
  114.                     {
  115.                         currentCategory = line.Substring(1, line.Length - 2);
  116.                         continue;
  117.                     }
  118.                     if (displayedCategory != "" && currentCategory.ToLower() != displayedCategory.ToLower())
  119.                         continue;
  120.                     if (!line.Contains("="))
  121.                         continue;
  122.                     string[] parts = line.Split('=');
  123.                     if (parts.Length < 1)
  124.                         continue;
  125.  
  126.                     int index = -1;
  127.                     int.TryParse(parts[0], System.Globalization.NumberStyles.HexNumber, null, out index);
  128.                     if (index > -1 && index < forceIndexCount)
  129.                     {
  130.                         string hex = index.ToString("X");
  131.                         if (hex.Length == 1)
  132.                             hex = "0" + hex;
  133.                         if (parts.Length > 1)
  134.                             cboLines[index] = hex + " = " + parts[1];
  135.                         else
  136.                             cboLines[index] = hex;
  137.                     }
  138.                 }
  139.             }
  140.  
  141.             for (int i = 0; i < forceIndexCount; i++)
  142.             {
  143.                 if (cboLines[i] == null || cboLines[i].Length == 0)
  144.                 {
  145.                     string hex = i.ToString("X");
  146.                     if (hex.Length == 1)
  147.                         hex = "0" + hex;
  148.                     cboLines[i] = hex;
  149.                 }
  150.             }
  151.  
  152.             this.DataSource = cboLines;
  153.         }
  154.     }
  155.  
  156.     public class StatusEventArgs : EventArgs
  157.     {
  158.         public Exception Exception { get; set; }
  159.         public string Status { get; set; }
  160.         public StatusEventArgs()
  161.             : base()
  162.         {
  163.  
  164.         }
  165.  
  166.         public StatusEventArgs(Exception ex, string s)
  167.             : this()
  168.         {
  169.             this.Exception = ex;
  170.             this.Status = s;
  171.         }
  172.     }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement