BetterDiscordApp-v1/Installers/dotNet/BetterDiscordWI/panels/Panel2.cs

311 lines
12 KiB
C#
Raw Normal View History

2015-12-04 02:17:37 +01:00
using System;
2015-12-05 02:15:00 +01:00
using System.Collections.Generic;
2015-12-04 02:17:37 +01:00
using System.Diagnostics;
using System.IO;
2015-12-05 02:15:00 +01:00
using System.IO.Compression;
2015-12-12 21:54:46 +01:00
using System.Linq;
2015-12-05 02:15:00 +01:00
using System.Net;
using System.Threading;
2015-12-04 02:17:37 +01:00
using System.Windows.Forms;
2015-12-05 02:15:00 +01:00
using asardotnet;
2015-12-04 02:17:37 +01:00
namespace BetterDiscordWI.panels {
public partial class Panel2: UserControl, IPanel {
2016-05-06 21:03:17 +02:00
private string _dataPath, _tempPath;
2015-12-04 02:17:37 +01:00
private Utils _utils;
public Panel2() {
2015-12-04 02:17:37 +01:00
InitializeComponent();
}
public void SetVisible() {
2015-12-04 02:17:37 +01:00
GetParent().btnBack.Enabled = false;
GetParent().btnNext.Enabled = false;
2015-12-05 02:15:00 +01:00
GetParent().btnBack.Visible = false;
GetParent().btnNext.Visible = false;
GetParent().btnCancel.Enabled = false;
2015-12-04 02:17:37 +01:00
2015-12-05 02:15:00 +01:00
_utils = new Utils();
KillProcessIfInstalling("Discord");
KillProcessIfInstalling("DiscordCanary");
KillProcessIfInstalling("DiscordPTB");
CreateDirectories();
}
private void KillProcessIfInstalling(string app) {
2016-05-06 21:03:17 +02:00
if (!GetParent().DiscordPath.Contains(app + "\\")) return;
AppendLog("Killing " + app);
foreach(var process in Process.GetProcessesByName(app)) {
process.Kill();
}
2015-12-04 02:17:37 +01:00
}
private void CreateDirectories() {
Thread t = new Thread(() => {
2016-05-06 21:03:17 +02:00
_dataPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord";
_tempPath = $"{_dataPath}\\temp";
AppendLog("Deleting old cached files");
try {
2016-05-06 21:03:17 +02:00
if(File.Exists($"{_dataPath}\\emotes_bttv.json")) {
File.Delete($"{_dataPath}\\emotes_bttv.json");
}
2016-05-06 21:03:17 +02:00
if(File.Exists($"{_dataPath}\\emotes_bttv_2.json")) {
File.Delete($"{_dataPath}\\emotes_bttv_2.json");
}
2016-05-06 21:03:17 +02:00
if(File.Exists($"{_dataPath}\\emotes_ffz.json")) {
File.Delete($"{_dataPath}\\emotes_ffz.json");
}
2016-05-06 21:03:17 +02:00
if(File.Exists($"{_dataPath}\\emotes_twitch_global.json")) {
File.Delete($"{_dataPath}\\emotes_twitch_global.json");
}
2016-05-06 21:03:17 +02:00
if(File.Exists($"{_dataPath}\\emotes_twitch_subscriber.json")) {
File.Delete($"{_dataPath}\\emotes_twitch_subscriber.json");
}
2016-05-06 21:03:17 +02:00
if(File.Exists($"{_dataPath}\\user.json")) {
File.Delete($"{_dataPath}\\user.json");
}
} catch(Exception e) { AppendLog("Failed to delete one or more cached files"); }
2015-12-04 02:17:37 +01:00
if(Directory.Exists(_tempPath)) {
2015-12-05 02:15:00 +01:00
AppendLog("Deleting temp path");
Directory.Delete(_tempPath, true);
}
2015-12-04 02:17:37 +01:00
while(Directory.Exists(_tempPath)) {
2015-12-05 02:15:00 +01:00
Debug.Print("Waiting for dirdel");
Thread.Sleep(100);
}
2015-12-04 02:17:37 +01:00
Directory.CreateDirectory(_tempPath);
2016-05-06 21:03:17 +02:00
DownloadResource("BetterDiscord.zip", "https://github.com/Jiiks/BetterDiscordApp/archive/stable16.zip");
2015-12-04 02:17:37 +01:00
2016-05-06 21:03:17 +02:00
while(!File.Exists($"{_tempPath}\\BetterDiscord.zip")) {
2015-12-05 02:15:00 +01:00
Debug.Print("Waiting for download");
Thread.Sleep(100);
}
AppendLog("Extracting BetterDiscord");
ZipArchive zar =
2016-05-06 21:03:17 +02:00
ZipFile.OpenRead($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\BetterDiscord.zip");
zar.ExtractToDirectory($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\");
2015-12-05 02:15:00 +01:00
DeleteDirs();
});
t.Start();
2015-12-04 02:17:37 +01:00
}
2015-12-05 02:15:00 +01:00
private void DeleteDirs() {
int errors = 0;
Thread t = new Thread(() => {
2016-05-06 21:03:17 +02:00
string dir = $"{GetParent().DiscordPath}\\resources\\app";
2015-12-05 02:15:00 +01:00
if(Directory.Exists(dir)) {
try {
AppendLog("Deleting " + dir);
Directory.Delete(dir, true);
} catch {
2016-05-06 21:03:17 +02:00
AppendLog($"Error: Failed to Delete the '{dir}\\resources\\app' Directory.");
errors = 1;
Finalize(errors);
}
2015-12-05 02:15:00 +01:00
}
2015-12-04 02:17:37 +01:00
while(Directory.Exists(dir)) {
2015-12-05 02:15:00 +01:00
Debug.Print("Waiting for direl");
Thread.Sleep(100);
}
2015-12-04 02:17:37 +01:00
if (!Directory.Exists($"{GetParent().DiscordPath}\\resources\\node_modules\\")) {
Debug.Print("node_modules doesn't exist, creating");
AppendLog("node_modules doesn't exist, creating");
Directory.CreateDirectory($"{GetParent().DiscordPath}\\resources\\node_modules\\");
}
2016-05-06 21:03:17 +02:00
dir = $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord";
2015-12-04 02:17:37 +01:00
if(Directory.Exists(dir)) {
2016-05-06 21:03:17 +02:00
AppendLog($"Deleting {dir}");
2015-12-05 02:15:00 +01:00
Directory.Delete(dir, true);
}
2015-12-04 02:17:37 +01:00
while(Directory.Exists(dir)) {
2015-12-05 02:15:00 +01:00
Debug.Print("Waiting for direl");
Thread.Sleep(100);
}
2015-12-04 02:17:37 +01:00
2016-02-11 20:00:33 +01:00
AppendLog("Extracting app.asar");
2016-05-06 21:03:17 +02:00
string appAsarPath = $"{GetParent().DiscordPath}\\resources\\app.asar";
2016-02-11 20:00:33 +01:00
if(File.Exists(appAsarPath)) {
AsarArchive archive = new AsarArchive(appAsarPath);
AsarExtractor extractor = new AsarExtractor();
2016-05-06 21:03:17 +02:00
extractor.ExtractAll(archive, $"{GetParent().DiscordPath}\\resources\\app\\");
2016-02-11 20:00:33 +01:00
} else {
AppendLog("Error: app.asar file couldn't be found in 'resources' folder. Installation cannot Continue.");
errors = 1;
Finalize(errors);
}
if(errors == 0) {
AppendLog("Moving BetterDiscord to resources\\node_modules\\");
2016-05-06 21:03:17 +02:00
Directory.Move($"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\BetterDiscordApp-stable16", $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord");
2016-02-11 20:00:33 +01:00
try {
2016-02-11 19:47:34 +01:00
Splice();
2016-02-11 20:00:33 +01:00
} catch {
AppendLog("Error: Extracting app.asar: Newtonsoft.Json.dll might not be present in the Installer Folder. Installation cannot Continue.");
2016-02-11 19:47:34 +01:00
errors = 1;
Finalize(errors);
}
2016-02-11 20:00:33 +01:00
}
2015-12-05 02:15:00 +01:00
});
2015-12-05 02:15:00 +01:00
t.Start();
}
2015-12-04 02:17:37 +01:00
2016-05-06 21:03:17 +02:00
private void DownloadResource(string resource, string url) {
2015-12-05 02:15:00 +01:00
AppendLog("Downloading Resource: " + resource);
2016-05-06 21:03:17 +02:00
WebClient webClient = new WebClient {Headers = {["User-Agent"] = "Mozilla/5.0"}};
2016-05-06 21:03:17 +02:00
webClient.DownloadFile(new Uri(url), $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\BetterDiscord\\temp\\{resource}");
2015-12-05 02:15:00 +01:00
}
2015-12-04 02:17:37 +01:00
private void Splice() {
string indexloc = null;
if(File.Exists($"{GetParent().DiscordPath}\\resources\\app\\app\\index.js"))
{
//Normal path
indexloc = $"{GetParent().DiscordPath}\\resources\\app\\app\\index.js";
} else if (File.Exists($"{GetParent().DiscordPath}\\resources\\app\\index.js"))
{
//Canary 0.0.138 changed path to app\\index.js
indexloc = $"{GetParent().DiscordPath}\\resources\\app\\index.js";
}
if(indexloc == null)
{
AppendLog($"Error: index.js not found");
Finalize(1);
return;
}
if(!File.Exists(@"splice"))
{
AppendLog($"Error: splice install file not found, this should be included with the installer.");
Finalize(1);
return;
}
2015-12-04 02:17:37 +01:00
Thread t = new Thread(() => {
2016-05-06 21:03:17 +02:00
List<string> lines = new List<string>();
2015-12-05 02:15:00 +01:00
AppendLog("Spicing index");
using(FileStream fs = new FileStream(indexloc, FileMode.Open)) {
using(StreamReader reader = new StreamReader(fs)) {
2016-05-06 21:03:17 +02:00
string line = "";
while((line = reader.ReadLine()) != null) {
//if(GetParent().DiscordPath.Contains("Discord\\")) {
//if(GetParent().DiscordPath.Contains("DiscordCanary\\")) {
//if(GetParent().DiscordPath.Contains("DiscordPTB\\")) {
if(line.Replace(" ", "").Contains("var_fs=")) {
lines.Add(line);
lines.Add("var _betterDiscord = require('betterdiscord');");
2016-05-06 21:03:17 +02:00
lines.Add("var _betterDiscord2;");
} else if(line.Replace(" ", "").Contains("mainWindow=new")) {
lines.Add(line);
2016-05-06 21:03:17 +02:00
lines.Add(File.ReadAllText(@"splice"));
} else {
lines.Add(line);
2015-12-05 02:15:00 +01:00
}
//}
2015-12-05 02:15:00 +01:00
}
2015-12-04 02:17:37 +01:00
}
}
2015-12-05 02:15:00 +01:00
AppendLog("Writing index");
2015-12-04 02:17:37 +01:00
2015-12-05 02:15:00 +01:00
File.WriteAllLines(indexloc, lines.ToArray());
2015-12-12 21:54:46 +01:00
AppendLog("Finished installation, verifying installation...");
int errors = 0;
2016-05-06 21:03:17 +02:00
string curPath = $"{GetParent().DiscordPath}\\resources\\app\\app\\index.js";
string curPath2 = $"{GetParent().DiscordPath}\\resources\\app\\index.js";
if (!File.Exists(curPath) && !File.Exists(curPath2))
{
AppendLog($"ERROR: index.js not found in {curPath} or {curPath2}");
2015-12-12 21:54:46 +01:00
errors++;
}
2016-05-06 21:03:17 +02:00
curPath = $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord";
2015-12-12 21:54:46 +01:00
if(!Directory.Exists(curPath)) {
2016-05-06 21:03:17 +02:00
AppendLog($"ERROR: DIRECTORY: {curPath} DOES NOT EXIST!");
2015-12-12 21:54:46 +01:00
errors++;
}
2016-05-06 21:03:17 +02:00
string basePath = $"{GetParent().DiscordPath}\\resources\\node_modules\\BetterDiscord";
string[] bdFiles = { "\\package.json", "\\betterdiscord.js", "\\lib\\BetterDiscord.js", "\\lib\\config.json", "\\lib\\Utils.js" };
2015-12-12 21:54:46 +01:00
foreach(string s in bdFiles.Where(s => !File.Exists(basePath + s))) {
2016-05-06 21:03:17 +02:00
AppendLog($"ERROR: FILE: {basePath}{s} DOES NOT EXIST");
2015-12-12 21:54:46 +01:00
errors++;
}
Finalize(errors);
2015-12-05 02:15:00 +01:00
});
2015-12-12 21:54:46 +01:00
2015-12-13 13:36:44 +01:00
t.Start();
2015-12-04 02:17:37 +01:00
}
private void Finalize(int errors) {
2016-05-06 21:03:17 +02:00
AppendLog($"Finished installing BetterDiscord with {errors} errors");
2015-12-05 02:15:00 +01:00
Invoke((MethodInvoker)delegate {
2016-05-06 21:03:17 +02:00
GetParent().Finished = true;
GetParent().btnCancel.Text = @"OK";
2015-12-05 02:15:00 +01:00
GetParent().btnCancel.Enabled = true;
});
2015-12-18 17:00:52 +01:00
if(GetParent().RestartDiscord) {
if(GetParent().DiscordPath.Contains("\\Discord\\")) {
2016-05-06 21:03:17 +02:00
Process.Start($"{GetParent().DiscordPath}\\Discord.exe");
}
if(GetParent().DiscordPath.Contains("\\DiscordCanary\\")) {
2016-05-06 21:03:17 +02:00
Process.Start($"{GetParent().DiscordPath}\\DiscordCanary.exe");
}
if(GetParent().DiscordPath.Contains("\\DiscordPTB\\")) {
2016-05-06 21:03:17 +02:00
Process.Start($"{GetParent().DiscordPath}\\DiscordPTB.exe");
}
2015-12-18 17:00:52 +01:00
}
2015-12-04 02:17:37 +01:00
}
public FormMain GetParent() {
2015-12-04 02:17:37 +01:00
return (FormMain)ParentForm;
}
2016-05-06 21:03:17 +02:00
public void BtnNext() { }
2015-12-04 02:17:37 +01:00
2016-05-06 21:03:17 +02:00
public void BtnPrev() { }
2015-12-04 02:17:37 +01:00
2016-05-06 21:03:17 +02:00
private void AppendLog(string message) {
Invoke((MethodInvoker)delegate {
2015-12-05 02:15:00 +01:00
rtLog.AppendText(message + "\n");
rtLog.SelectionStart = rtLog.Text.Length;
rtLog.ScrollToCaret();
});
2015-12-04 02:17:37 +01:00
}
}
}