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;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
private String _dataPath, _tempPath;
|
|
|
|
|
private Utils _utils;
|
|
|
|
|
|
|
|
|
|
public Panel2()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void SetVisible()
|
|
|
|
|
{
|
|
|
|
|
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();
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AppendLog("Killing Discord");
|
|
|
|
|
foreach (var process in Process.GetProcessesByName("Discord"))
|
|
|
|
|
{
|
|
|
|
|
process.Kill();
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
|
|
|
|
CreateDirectories();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void CreateDirectories()
|
|
|
|
|
{
|
2015-12-05 02:15:00 +01:00
|
|
|
|
Thread t = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
_dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BetterDiscord";
|
|
|
|
|
_tempPath = _dataPath + "\\temp";
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
if (Directory.Exists(_tempPath))
|
|
|
|
|
{
|
|
|
|
|
AppendLog("Deleting temp path");
|
|
|
|
|
Directory.Delete(_tempPath, true);
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
while (Directory.Exists(_tempPath))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Waiting for dirdel");
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
|
|
|
|
Directory.CreateDirectory(_tempPath);
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
DownloadResource("BetterDiscord.zip", "https://github.com/Jiiks/BetterDiscordApp/archive/stable.zip");
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
while (!File.Exists(_tempPath + "\\BetterDiscord.zip"))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Waiting for download");
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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\\");
|
|
|
|
|
|
|
|
|
|
DeleteDirs();
|
|
|
|
|
});
|
|
|
|
|
t.Start();
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
|
|
|
|
|
private void DeleteDirs()
|
2015-12-04 02:17:37 +01:00
|
|
|
|
{
|
2015-12-05 02:15:00 +01:00
|
|
|
|
Thread t = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
String dir = GetParent().DiscordPath + "\\resources\\app";
|
|
|
|
|
|
|
|
|
|
if (Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
AppendLog("Deleting " + dir);
|
|
|
|
|
Directory.Delete(dir, true);
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
while (Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Waiting for direl");
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
dir = GetParent().DiscordPath + "\\resources\\node_modules\\BetterDiscord";
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
if (Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
AppendLog("Deleting " + dir);
|
|
|
|
|
Directory.Delete(dir, true);
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
while (Directory.Exists(dir))
|
|
|
|
|
{
|
|
|
|
|
Debug.Print("Waiting for direl");
|
|
|
|
|
Thread.Sleep(100);
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AppendLog("Moving BetterDiscord to resources\\node_modules\\");
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
Directory.Move(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BetterDiscord\\temp\\BetterDiscordApp-stable", GetParent().DiscordPath + "\\resources\\node_modules\\BetterDiscord");
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AppendLog("Extracting app.asar");
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AsarArchive archive = new AsarArchive(GetParent().DiscordPath + "\\resources\\app.asar");
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AsarExtractor extractor = new AsarExtractor();
|
|
|
|
|
extractor.ExtractAll(archive, GetParent().DiscordPath + "\\resources\\app\\");
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
Splice();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.Start();
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
private void DownloadResource(String resource, String url)
|
2015-12-04 02:17:37 +01:00
|
|
|
|
{
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AppendLog("Downloading Resource: " + resource);
|
|
|
|
|
|
|
|
|
|
WebClient webClient = new WebClient();
|
|
|
|
|
webClient.Headers["User-Agent"] = "Mozilla/5.0";
|
|
|
|
|
|
|
|
|
|
webClient.DownloadFile(new Uri(url), Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\BetterDiscord\\temp\\" + resource);
|
|
|
|
|
}
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
private void Splice()
|
|
|
|
|
{
|
|
|
|
|
String indexloc = GetParent().DiscordPath + "\\resources\\app\\app\\index.js";
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
Thread t = new Thread(() =>
|
2015-12-04 02:17:37 +01:00
|
|
|
|
{
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
List<String> lines = new List<string>();
|
2015-12-04 02:17:37 +01:00
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AppendLog("Spicing index");
|
|
|
|
|
|
|
|
|
|
using (FileStream fs = new FileStream(indexloc, FileMode.Open))
|
2015-12-04 02:17:37 +01:00
|
|
|
|
{
|
2015-12-05 02:15:00 +01:00
|
|
|
|
using (StreamReader reader = new StreamReader(fs))
|
2015-12-04 02:17:37 +01:00
|
|
|
|
{
|
2015-12-05 02:15:00 +01:00
|
|
|
|
String line = "";
|
|
|
|
|
while((line = reader.ReadLine()) != null)
|
|
|
|
|
{
|
|
|
|
|
if (line.Contains("var _overlay2"))
|
|
|
|
|
{
|
|
|
|
|
AppendLog("Splicing require");
|
|
|
|
|
lines.Add(line);
|
|
|
|
|
lines.Add("var _betterDiscord = require('betterdiscord');");
|
|
|
|
|
}else if (line.Contains("mainWindow = new _BrowserWindow2"))
|
|
|
|
|
{
|
|
|
|
|
AppendLog("Splicing function call");
|
|
|
|
|
lines.Add(line);
|
|
|
|
|
lines.Add("betterDiscord(mainWindow);");
|
2015-12-12 17:29:06 +01:00
|
|
|
|
AppendLog("Splicing plugin&theme loaders");
|
|
|
|
|
|
|
|
|
|
lines.Add(" mainWindow.webContents.on('dom-ready', function() {");
|
|
|
|
|
lines.Add(" var fs = require('fs');");
|
|
|
|
|
lines.Add(" fs.readdir('G:/BetterDiscord/plugins/',function(err,files) {");
|
|
|
|
|
lines.Add(" if (err) { console.log(err); return; } ");
|
|
|
|
|
lines.Add(" mainWindow.webContents.executeJavaScript('var bdplugins = {};');");
|
|
|
|
|
lines.Add(" files.forEach(function(fileName){");
|
|
|
|
|
lines.Add(" var plugin = fs.readFileSync('G:/BetterDiscord/plugins/' + fileName,'utf8');");
|
|
|
|
|
lines.Add(" var meta = plugin.split('\\n')[0];");
|
|
|
|
|
lines.Add(" if(meta.indexOf('META') < 0) {");
|
|
|
|
|
lines.Add(" console.log('BetterDiscord: ERROR[Plugin META not found in file: '+fileName+']');");
|
|
|
|
|
lines.Add(" return;");
|
|
|
|
|
lines.Add(" }");
|
|
|
|
|
lines.Add(" var pluginVar = meta.substring(meta.lastIndexOf('//META')+6, meta.lastIndexOf('*//'));");
|
|
|
|
|
lines.Add(" var parse = JSON.parse(pluginVar);");
|
|
|
|
|
lines.Add(" var pluginName = parse['name'];");
|
|
|
|
|
lines.Add(" console.log('BetterDiscord: Loading Plugin: ' + pluginName);");
|
|
|
|
|
lines.Add(" mainWindow.webContents.executeJavaScript(plugin);");
|
|
|
|
|
lines.Add(" mainWindow.webContents.executeJavaScript('(function() { var plugin = new '+pluginName+'(); bdplugins[plugin.getName()] = { \"plugin\": plugin, \"enabled\": false } })();')");
|
|
|
|
|
lines.Add(" });");
|
|
|
|
|
lines.Add(" });");
|
|
|
|
|
lines.Add("\n");
|
|
|
|
|
lines.Add(" fs.readdir('G:/BetterDiscord/themes/', function(err, files) {");
|
|
|
|
|
lines.Add(" if (err) { console.log(err); return; }");
|
|
|
|
|
lines.Add(" mainWindow.webContents.executeJavaScript('var bdthemes = {};');");
|
|
|
|
|
lines.Add(" files.forEach(function(fileName) {");
|
|
|
|
|
lines.Add(" var theme = fs.readFileSync('G:/BetterDiscord/themes/' + fileName, 'utf8');");
|
|
|
|
|
lines.Add(" var split = theme.split('\\n');");
|
|
|
|
|
lines.Add(" var meta = split[0];");
|
|
|
|
|
lines.Add(" if(meta.indexOf('META') < 0) {");
|
|
|
|
|
lines.Add(" console.log('BetterDiscord: ERROR[Theme META not found in file: '+fileName+']');");
|
|
|
|
|
lines.Add(" return;");
|
|
|
|
|
lines.Add(" }");
|
|
|
|
|
lines.Add(" var themeVar = meta.substring(meta.lastIndexOf('//META')+6, meta.lastIndexOf('*//'));");
|
|
|
|
|
lines.Add(" var parse = JSON.parse(themeVar);");
|
|
|
|
|
lines.Add(" var themeName = parse['name'];");
|
|
|
|
|
lines.Add(" console.log('BetterDiscord: Loading Theme: ' + themeName);");
|
|
|
|
|
lines.Add(" split.splice(0, 1);");
|
|
|
|
|
lines.Add(" theme = split.join('\\n');");
|
|
|
|
|
lines.Add(" theme = theme.replace(/(\\r\\n|\\n|\\r)/gm,'');");
|
|
|
|
|
lines.Add(" theme = theme.replace(/\\s/g, '');");
|
|
|
|
|
lines.Add(" mainWindow.webContents.executeJavaScript('var theme' + themeName + ' = \"' + escape(theme) + '\";');");
|
|
|
|
|
lines.Add(" mainWindow.webContents.executeJavaScript('(function() { bdthemes[theme'+themeName+'] = false })();');");
|
|
|
|
|
lines.Add(" });");
|
|
|
|
|
lines.Add(" });");
|
|
|
|
|
lines.Add(" });\n");
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
}
|
|
|
|
|
else if (line.Contains("main();"))
|
|
|
|
|
{
|
|
|
|
|
AppendLog("Splicing function");
|
|
|
|
|
lines.Add("function betterDiscord(mw) { _betterDiscord = new _betterDiscord.BetterDiscord(mw); _betterDiscord.init(); }");
|
|
|
|
|
lines.Add(line);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
lines.Add(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
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());
|
|
|
|
|
|
|
|
|
|
Finalize();
|
|
|
|
|
});
|
|
|
|
|
t.Start();
|
2015-12-04 02:17:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-05 02:15:00 +01:00
|
|
|
|
private void Finalize()
|
2015-12-04 02:17:37 +01:00
|
|
|
|
{
|
2015-12-05 02:15:00 +01:00
|
|
|
|
AppendLog("Finished installing BetterDiscord");
|
|
|
|
|
|
|
|
|
|
Invoke((MethodInvoker) delegate
|
|
|
|
|
{
|
|
|
|
|
GetParent().finished = true;
|
|
|
|
|
GetParent().btnCancel.Text = "OK";
|
|
|
|
|
GetParent().btnCancel.Enabled = true;
|
|
|
|
|
});
|
2015-12-04 02:17:37 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public FormMain GetParent()
|
|
|
|
|
{
|
|
|
|
|
return (FormMain)ParentForm;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BtnNext()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void BtnPrev()
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void AppendLog(String message)
|
|
|
|
|
{
|
2015-12-05 02:15:00 +01:00
|
|
|
|
Invoke((MethodInvoker) delegate
|
|
|
|
|
{
|
|
|
|
|
rtLog.AppendText(message + "\n");
|
|
|
|
|
rtLog.SelectionStart = rtLog.Text.Length;
|
|
|
|
|
rtLog.ScrollToCaret();
|
|
|
|
|
});
|
|
|
|
|
|
2015-12-04 02:17:37 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|