Skip to content
This repository has been archived by the owner on Aug 3, 2022. It is now read-only.

Commit

Permalink
Refactoring and 0.2.8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiiks committed May 6, 2016
1 parent 2689708 commit 71131ef
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 195 deletions.
62 changes: 22 additions & 40 deletions WindowsInstaller/BetterDiscordWI/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,66 @@
using System.Xml;
using BetterDiscordWI.panels;

namespace BetterDiscordWI
{
public partial class FormMain : Form
{
namespace BetterDiscordWI {
public partial class FormMain : Form {

private readonly IPanel[] _panels = { new Panel0(), new Panel1(), new Panel2() };
private int _index;

public String DiscordPath;
public Boolean RestartDiscord = false;
public String Sha;
public Boolean finished = false;
public string DiscordPath;
public bool RestartDiscord = false;
public string Sha;
public bool Finished = false;

public XmlNodeList ResourceList;

public FormMain()
{
public FormMain() {
InitializeComponent();

Sha = Utils.GetHash();

if (Sha.Length < 1)
{
MessageBox.Show("Failed to get sha", "Error", MessageBoxButtons.OK);
if (Sha.Length < 1) {
MessageBox.Show(@"Failed to get sha", @"Error", MessageBoxButtons.OK);
Environment.Exit(0);
}

foreach (IPanel ipanel in _panels)
{

foreach (IPanel ipanel in _panels) {
panelContainer.Controls.Add((UserControl)ipanel);
((UserControl)ipanel).Dock = DockStyle.Fill;
((UserControl)ipanel).Hide();
}
((UserControl)_panels[_index]).Show();
_panels[_index].SetVisible();



btnCancel.Click += (sender, args) => Close();
btnNext.Click += (sender, args) => _panels[_index].BtnNext();
btnBack.Click += (sender, args) => _panels[_index].BtnPrev();
}

public void SwitchPanel(int index)
{
public void SwitchPanel(int index) {
((UserControl)_panels[_index]).Hide();
_index = index;
((UserControl)_panels[_index]).Show();
_panels[_index].SetVisible();
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
if (!finished)
{
DialogResult dr =
MessageBox.Show(
"Setup is not complete. If you exit now, BetterDiscord will not be installed.\n\nExit Setup?",
"Exit Setup?", MessageBoxButtons.YesNo);

if (dr == DialogResult.No)
{
e.Cancel = true;
}
protected override void OnFormClosing(FormClosingEventArgs e) {
if (Finished) return;
DialogResult dr = MessageBox.Show(@"Setup is not complete. If you exit now, BetterDiscord will not be installed. Exit Setup?", @"Exit Setup?", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) {
e.Cancel = true;
}
}

readonly Pen borderPen = new Pen(Color.FromArgb(160,160,160));
protected override void OnPaint(PaintEventArgs e)
{
readonly Pen _borderPen = new Pen(Color.FromArgb(160, 160, 160));
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
g.FillRectangle(SystemBrushes.Window, new Rectangle(0,0, Width, 50) );
g.DrawLine(borderPen, 0, 50, Width, 50);
g.FillRectangle(SystemBrushes.Window, new Rectangle(0, 0, Width, 50));
g.DrawLine(_borderPen, 0, 50, Width, 50);
g.DrawLine(SystemPens.Window, 0, 51, Width, 51);

g.DrawLine(borderPen, 0, 310, Width, 310);
g.DrawLine(_borderPen, 0, 310, Width, 310);
g.DrawLine(SystemPens.Window, 0, 311, Width, 311);


base.OnPaint(e);
}
Expand Down
3 changes: 0 additions & 3 deletions WindowsInstaller/BetterDiscordWI/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BetterDiscordWI
Expand Down
26 changes: 6 additions & 20 deletions WindowsInstaller/BetterDiscordWI/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BetterDiscordWI
{
class Utils
{


public void StartDownload(ProgressBar pb, String url, String name)
public void StartDownload(ProgressBar pb, string url, string name)
{



Thread t = new Thread(() =>
{
WebClient webClient = new WebClient();
webClient.Headers["User-Agent"] = "Mozilla/5.0";
WebClient webClient = new WebClient {Headers = {["User-Agent"] = "Mozilla/5.0"}};
webClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs args)
{
double percentage = (double.Parse(args.BytesReceived.ToString()) /double.Parse(args.TotalBytesToReceive.ToString())) * 100;
Expand All @@ -43,18 +32,15 @@ public void StartDownload(ProgressBar pb, String url, String name)
t.Start();
}

public static String GetHash()
public static string GetHash()
{
WebClient wc = new WebClient();
wc.Headers["User-Agent"] = "Mozilla/5.0";
String result = wc.DownloadString("https://api.github.com/repos/Jiiks/BetterDiscordApp/commits/master");
WebClient wc = new WebClient {Headers = {["User-Agent"] = "Mozilla/5.0"}};
string result = wc.DownloadString(@"https://api.github.com/repos/Jiiks/BetterDiscordApp/commits/master");

int start = result.IndexOf("{\"sha\":");
int end = result.IndexOf("\",\"");

return result.Substring(start + 8, end - 8);
}


}
}
}
17 changes: 3 additions & 14 deletions WindowsInstaller/BetterDiscordWI/panels/IPanel.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace BetterDiscordWI.panels
{
interface IPanel
{

namespace BetterDiscordWI.panels {
interface IPanel {
void SetVisible();
FormMain GetParent();
void BtnNext();
void BtnPrev();

}
}
}
32 changes: 11 additions & 21 deletions WindowsInstaller/BetterDiscordWI/panels/Panel0.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
using System.Windows.Forms;

namespace BetterDiscordWI.panels
{
public partial class Panel0 : UserControl, IPanel
{
public Panel0()
{
namespace BetterDiscordWI.panels {
public partial class Panel0 : UserControl, IPanel {
public Panel0() {
InitializeComponent();

radioAcceptLicense.CheckedChanged += (sender, args) =>
{
radioAcceptLicense.CheckedChanged += (sender, args) => {
GetParent().btnNext.Enabled = radioAcceptLicense.Checked;
};
}

public void SetVisible()
{
public void SetVisible() {
GetParent().btnBack.Visible = false;
GetParent().btnNext.Enabled = false;
GetParent().btnNext.Text = "Next >";
GetParent().lblPanelTitle.Text = "BetterDiscord License Agreement";
GetParent().btnNext.Text = @"Next >";
GetParent().lblPanelTitle.Text = @"BetterDiscord License Agreement";
GetParent().btnNext.Enabled = radioAcceptLicense.Checked;
}

public FormMain GetParent()
{
return (FormMain) ParentForm;
public FormMain GetParent() {
return (FormMain)ParentForm;
}

public void BtnNext()
{
public void BtnNext() {
GetParent().SwitchPanel(1);
}

public void BtnPrev()
{
throw new System.NotImplementedException();
}
public void BtnPrev() {}
}
}
58 changes: 28 additions & 30 deletions WindowsInstaller/BetterDiscordWI/panels/Panel1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ public void SetVisible() {
GetParent().btnBack.Visible = true;
GetParent().btnNext.Enabled = true;
GetParent().btnBack.Enabled = true;
GetParent().btnNext.Text = "Install";
GetParent().lblPanelTitle.Text = "BetterDiscord Installation";
GetParent().btnNext.Text = @"Install";
GetParent().lblPanelTitle.Text = @"BetterDiscord Installation";

pickVersion();
PickVersion();
}

public FormMain GetParent() {
Expand All @@ -42,51 +42,49 @@ private void btnBrowser_Click(object sender, EventArgs e) {
}

private void checkBox1_CheckedChanged(object sender, EventArgs e) {
pickVersion();
PickVersion();
}

private void checkBox2_CheckedChanged(object sender, EventArgs e) {
pickVersion();
PickVersion();
}

private void pickVersion() {
string dirPath = null;
if(checkBox1.Checked == true) {
dirPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\DiscordCanary";
private void PickVersion() {
string dirPath;
if(checkBox1.Checked) {
dirPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\DiscordCanary";
if(!Directory.Exists(dirPath)) checkBox1.Checked = false;

checkBox2.Checked = false;
} else if(checkBox2.Checked == true) {
dirPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\DiscordPTB";
} else if(checkBox2.Checked) {
dirPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\DiscordPTB";
if(!Directory.Exists(dirPath)) checkBox2.Checked = false;

checkBox1.Checked = false;
} else {
dirPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Discord";
dirPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)}\\Discord";
}

if(Directory.Exists(dirPath)) {
String[] directories = Directory.GetDirectories(dirPath);
if (!Directory.Exists(dirPath)) return;
string[] directories = Directory.GetDirectories(dirPath);

String highestVersion = null;
string highestVersion = null;

foreach(String s in directories) {
Debug.Print(s);
if(!s.Contains("app-"))
continue;
if(String.IsNullOrEmpty(highestVersion)) {
highestVersion = s;
continue;
}

if(String.CompareOrdinal(s, highestVersion) > 0) {
highestVersion = s;
}
foreach(string s in directories) {
Debug.Print(s);
if(!s.Contains("app-"))
continue;
if(string.IsNullOrEmpty(highestVersion)) {
highestVersion = s;
continue;
}


tbPath.Text = highestVersion;
if(string.CompareOrdinal(s, highestVersion) > 0) {
highestVersion = s;
}
}

tbPath.Text = highestVersion;
}
}
}
}
Loading

0 comments on commit 71131ef

Please sign in to comment.