Refactoring and 0.2.8 support

This commit is contained in:
Jiiks 2016-05-06 22:03:17 +03:00
parent 2689708b49
commit 71131ef335
7 changed files with 124 additions and 195 deletions

View File

@ -4,35 +4,30 @@ using System.Windows.Forms;
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();
@ -40,48 +35,35 @@ namespace BetterDiscordWI
((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);
}

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BetterDiscordWI

View File

@ -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;
@ -43,18 +32,15 @@ namespace BetterDiscordWI
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);
}
}
}
}

View File

@ -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();
}
}
}

View File

@ -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() {}
}
}

View File

@ -14,10 +14,10 @@ namespace BetterDiscordWI.panels {
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() {
@ -42,51 +42,49 @@ namespace BetterDiscordWI.panels {
}
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;
}
}
}
}

View File

@ -11,7 +11,7 @@ using asardotnet;
namespace BetterDiscordWI.panels {
public partial class Panel2: UserControl, IPanel {
private String _dataPath, _tempPath;
private string _dataPath, _tempPath;
private Utils _utils;
public Panel2() {
@ -35,41 +35,39 @@ namespace BetterDiscordWI.panels {
}
private void KillProcessIfInstalling(string app) {
if(GetParent().DiscordPath.Contains(app + "\\")) {
AppendLog("Killing " + app);
foreach(var process in Process.GetProcessesByName(app)) {
process.Kill();
}
if (!GetParent().DiscordPath.Contains(app + "\\")) return;
AppendLog("Killing " + app);
foreach(var process in Process.GetProcessesByName(app)) {
process.Kill();
}
}
private void CreateDirectories() {
Thread t = new Thread(() => {
_dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BetterDiscord";
_tempPath = _dataPath + "\\temp";
_dataPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord";
_tempPath = $"{_dataPath}\\temp";
AppendLog("Deleting old cached files");
try {
if(File.Exists(_dataPath + "\\emotes_bttv.json")) {
File.Delete(_dataPath + "\\emotes_bttv.json");
if(File.Exists($"{_dataPath}\\emotes_bttv.json")) {
File.Delete($"{_dataPath}\\emotes_bttv.json");
}
if(File.Exists(_dataPath + "\\emotes_bttv_2.json")) {
File.Delete(_dataPath + "\\emotes_bttv_2.json");
if(File.Exists($"{_dataPath}\\emotes_bttv_2.json")) {
File.Delete($"{_dataPath}\\emotes_bttv_2.json");
}
if(File.Exists(_dataPath + "\\emotes_ffz.json")) {
File.Delete(_dataPath + "\\emotes_ffz.json");
if(File.Exists($"{_dataPath}\\emotes_ffz.json")) {
File.Delete($"{_dataPath}\\emotes_ffz.json");
}
if(File.Exists(_dataPath + "\\emotes_twitch_global.json")) {
File.Delete(_dataPath + "\\emotes_twitch_global.json");
if(File.Exists($"{_dataPath}\\emotes_twitch_global.json")) {
File.Delete($"{_dataPath}\\emotes_twitch_global.json");
}
if(File.Exists(_dataPath + "\\emotes_twitch_subscriber.json")) {
File.Delete(_dataPath + "\\emotes_twitch_subscriber.json");
if(File.Exists($"{_dataPath}\\emotes_twitch_subscriber.json")) {
File.Delete($"{_dataPath}\\emotes_twitch_subscriber.json");
}
if(File.Exists(_dataPath + "\\user.json")) {
File.Delete(_dataPath + "\\user.json");
if(File.Exists($"{_dataPath}\\user.json")) {
File.Delete($"{_dataPath}\\user.json");
}
} catch(Exception e) { AppendLog("Failed to delete one or more cached files"); }
if(Directory.Exists(_tempPath)) {
AppendLog("Deleting temp path");
Directory.Delete(_tempPath, true);
@ -82,9 +80,9 @@ namespace BetterDiscordWI.panels {
Directory.CreateDirectory(_tempPath);
DownloadResource("BetterDiscord.zip", "https://github.com/Jiiks/BetterDiscordApp/archive/stable.zip");
DownloadResource("BetterDiscord.zip", "https://github.com/Jiiks/BetterDiscordApp/archive/stable16.zip");
while(!File.Exists(_tempPath + "\\BetterDiscord.zip")) {
while(!File.Exists($"{_tempPath}\\BetterDiscord.zip")) {
Debug.Print("Waiting for download");
Thread.Sleep(100);
}
@ -92,10 +90,8 @@ namespace BetterDiscordWI.panels {
AppendLog("Extracting BetterDiscord");
ZipArchive zar =
ZipFile.OpenRead(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
"\\BetterDiscord\\temp\\BetterDiscord.zip");
zar.ExtractToDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
"\\BetterDiscord\\temp\\");
ZipFile.OpenRead($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\BetterDiscord.zip");
zar.ExtractToDirectory($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\");
DeleteDirs();
});
@ -107,14 +103,14 @@ namespace BetterDiscordWI.panels {
private void DeleteDirs() {
int errors = 0;
Thread t = new Thread(() => {
String dir = GetParent().DiscordPath + "\\resources\\app";
string dir = $"{GetParent().DiscordPath}\\resources\\app";
if(Directory.Exists(dir)) {
try {
AppendLog("Deleting " + dir);
Directory.Delete(dir, true);
} catch {
AppendLog("Error: Failed to Delete the '" + dir + "\\resources\\app' Directory.");
AppendLog($"Error: Failed to Delete the '{dir}\\resources\\app' Directory.");
errors = 1;
Finalize(errors);
}
@ -125,11 +121,11 @@ namespace BetterDiscordWI.panels {
Thread.Sleep(100);
}
dir = GetParent().DiscordPath + "\\resources\\node_modules\\BetterDiscord";
dir = $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord";
if(Directory.Exists(dir)) {
AppendLog("Deleting " + dir);
AppendLog($"Deleting {dir}");
Directory.Delete(dir, true);
}
@ -139,12 +135,12 @@ namespace BetterDiscordWI.panels {
}
AppendLog("Extracting app.asar");
string appAsarPath = GetParent().DiscordPath + "\\resources\\app.asar";
string appAsarPath = $"{GetParent().DiscordPath}\\resources\\app.asar";
if(File.Exists(appAsarPath)) {
AsarArchive archive = new AsarArchive(appAsarPath);
AsarExtractor extractor = new AsarExtractor();
extractor.ExtractAll(archive, GetParent().DiscordPath + "\\resources\\app\\");
extractor.ExtractAll(archive, $"{GetParent().DiscordPath}\\resources\\app\\");
} else {
AppendLog("Error: app.asar file couldn't be found in 'resources' folder. Installation cannot Continue.");
errors = 1;
@ -153,7 +149,7 @@ namespace BetterDiscordWI.panels {
if(errors == 0) {
AppendLog("Moving BetterDiscord to resources\\node_modules\\");
Directory.Move(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BetterDiscord\\temp\\BetterDiscordApp-stable", GetParent().DiscordPath + "\\resources\\node_modules\\BetterDiscord");
Directory.Move($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\BetterDiscordApp-stable16", $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord");
try {
Splice();
@ -169,25 +165,23 @@ namespace BetterDiscordWI.panels {
t.Start();
}
private void DownloadResource(String resource, String url) {
private void DownloadResource(string resource, string url) {
AppendLog("Downloading Resource: " + resource);
WebClient webClient = new WebClient();
webClient.Headers["User-Agent"] = "Mozilla/5.0";
WebClient webClient = new WebClient {Headers = {["User-Agent"] = "Mozilla/5.0"}};
webClient.DownloadFile(new Uri(url), Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BetterDiscord\\temp\\" + resource);
webClient.DownloadFile(new Uri(url), $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\{resource}");
}
private void Splice() {
String indexloc = GetParent().DiscordPath + "\\resources\\app\\app\\index.js";
string indexloc = $"{GetParent().DiscordPath}\\resources\\app\\app\\index.js";
Thread t = new Thread(() => {
List<String> lines = new List<string>();
List<string> lines = new List<string>();
AppendLog("Spicing index");
using(FileStream fs = new FileStream(indexloc, FileMode.Open)) {
using(StreamReader reader = new StreamReader(fs)) {
String line = "";
string line = "";
while((line = reader.ReadLine()) != null) {
//if(GetParent().DiscordPath.Contains("Discord\\")) {
//if(GetParent().DiscordPath.Contains("DiscordCanary\\")) {
@ -195,9 +189,10 @@ namespace BetterDiscordWI.panels {
if(line.Replace(" ", "").Contains("var_fs=")) {
lines.Add(line);
lines.Add("var _betterDiscord = require('betterdiscord');");
lines.Add("var _betterDiscord2;");
} else if(line.Replace(" ", "").Contains("mainWindow=new")) {
lines.Add(line);
lines.Add(File.ReadAllText("splice"));
lines.Add(File.ReadAllText(@"splice"));
} else {
lines.Add(line);
}
@ -210,35 +205,32 @@ namespace BetterDiscordWI.panels {
File.WriteAllLines(indexloc, lines.ToArray());
AppendLog("Finished installation, verifying installation...");
int errors = 0;
String curPath = GetParent().DiscordPath + "\\resources\\app\\app\\index.js";
string curPath = $"{GetParent().DiscordPath}\\resources\\app\\app\\index.js";
if(!File.Exists(curPath)) {
AppendLog("ERROR: FILE: " + curPath + " DOES NOT EXIST!");
AppendLog($"ERROR: FILE: {curPath} DOES NOT EXIST!");
errors++;
}
curPath = GetParent().DiscordPath + "\\resources\\node_modules\\BetterDiscord";
curPath = $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord";
if(!Directory.Exists(curPath)) {
AppendLog("ERROR: DIRECTORY: " + curPath + " DOES NOT EXIST");
AppendLog($"ERROR: DIRECTORY: {curPath} DOES NOT EXIST!");
errors++;
}
String basePath = GetParent().DiscordPath + "\\resources\\node_modules\\BetterDiscord";
String[] bdFiles = { "\\package.json", "\\betterdiscord.js", "\\lib\\BetterDiscord.js", "\\lib\\config.json", "\\lib\\Utils.js" };
string basePath = $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord";
string[] bdFiles = { "\\package.json", "\\betterdiscord.js", "\\lib\\BetterDiscord.js", "\\lib\\config.json", "\\lib\\Utils.js" };
foreach(string s in bdFiles.Where(s => !File.Exists(basePath + s))) {
AppendLog("ERROR: FILE: " + basePath + s + " DOES NOT EXIST");
AppendLog($"ERROR: FILE: {basePath}{s} DOES NOT EXIST");
errors++;
}
Finalize(errors);
});
@ -246,24 +238,23 @@ namespace BetterDiscordWI.panels {
}
private void Finalize(int errors) {
AppendLog("Finished installing BetterDiscord with " + errors + " errors");
AppendLog($"Finished installing BetterDiscord with {errors} errors");
Invoke((MethodInvoker)delegate {
GetParent().finished = true;
GetParent().btnCancel.Text = "OK";
GetParent().Finished = true;
GetParent().btnCancel.Text = @"OK";
GetParent().btnCancel.Enabled = true;
});
if(GetParent().RestartDiscord) {
if(GetParent().DiscordPath.Contains("\\Discord\\")) {
Process.Start(GetParent().DiscordPath + "\\Discord.exe");
Process.Start($"{GetParent().DiscordPath}\\Discord.exe");
}
if(GetParent().DiscordPath.Contains("\\DiscordCanary\\")) {
Process.Start(GetParent().DiscordPath + "\\DiscordCanary.exe");
Process.Start($"{GetParent().DiscordPath}\\DiscordCanary.exe");
}
if(GetParent().DiscordPath.Contains("\\DiscordPTB\\")) {
Process.Start(GetParent().DiscordPath + "\\DiscordPTB.exe");
Process.Start($"{GetParent().DiscordPath}\\DiscordPTB.exe");
}
}
}
@ -272,15 +263,11 @@ namespace BetterDiscordWI.panels {
return (FormMain)ParentForm;
}
public void BtnNext() {
throw new NotImplementedException();
}
public void BtnNext() { }
public void BtnPrev() {
throw new NotImplementedException();
}
public void BtnPrev() { }
private void AppendLog(String message) {
private void AppendLog(string message) {
Invoke((MethodInvoker)delegate {
rtLog.AppendText(message + "\n");
rtLog.SelectionStart = rtLog.Text.Length;