FireWalletCosmos/FireWalletLite/Loader.cs

249 lines
9.2 KiB
C#
Raw Normal View History

2023-07-05 16:44:34 +10:00
using System.Diagnostics;
using System.Text.RegularExpressions;
using FireWallet;
2023-07-15 22:17:49 +10:00
namespace FireWalletLite;
public partial class Loader : Form
2023-07-05 16:44:34 +10:00
{
2023-07-19 15:06:25 +10:00
#region Constants
private readonly MainForm mainForm = new();
private readonly bool hideScreen = true; // Hide screen or not (for debug)
private readonly Process HSDProcess;
#endregion
2023-07-15 22:17:49 +10:00
public Loader()
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
InitializeComponent();
var splashScreen = new SplashScreen(false);
splashScreen.Show();
Application.DoEvents();
var start = DateTime.Now;
// Install and load node
var dir = mainForm.dir;
HSDProcess = new Process();
if (!Directory.Exists(dir)) Environment.Exit(1);
var hsdPath = dir + "hsd\\bin\\hsd.exe";
if (!Directory.Exists(dir + "hsd"))
{
var repositoryUrl = "https://github.com/handshake-org/hsd.git";
var destinationPath = dir + "hsd";
CloneRepository(repositoryUrl, destinationPath);
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
// Start HSD
HSDProcess.StartInfo.RedirectStandardInput = true;
HSDProcess.StartInfo.RedirectStandardOutput = false;
HSDProcess.StartInfo.UseShellExecute = false;
HSDProcess.StartInfo.FileName = "node.exe";
HSDProcess.StartInfo.Arguments = dir + "hsd\\bin\\hsd --agent=FireWallet --spv --prefix " + dir + "\\hsd_data";
HSDProcess.StartInfo.CreateNoWindow = hideScreen;
if (hideScreen)
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
HSDProcess.StartInfo.RedirectStandardError = true;
// Send errors to log
HSDProcess.ErrorDataReceived += (sender, e) => mainForm.AddLog("HSD Error: " + e.Data);
}
else
{
HSDProcess.StartInfo.RedirectStandardError = false;
}
2023-07-05 16:44:34 +10:00
2023-07-15 22:17:49 +10:00
HSDProcess.Start();
while ((DateTime.Now - start).TotalSeconds < 5)
{
Thread.Sleep(10);
2023-07-05 16:44:34 +10:00
Application.DoEvents();
2023-07-15 22:17:49 +10:00
}
splashScreen.CloseSplash();
while (!splashScreen.IsClosed)
{
Thread.Sleep(10);
Application.DoEvents();
}
splashScreen.Dispose();
mainForm.Show();
}
#region Git
public void CloneRepository(string repositoryUrl, string destinationPath)
{
try
{
// Check if git is installed
var testInstalled = new Process();
testInstalled.StartInfo.FileName = "git";
testInstalled.StartInfo.Arguments = "-v";
testInstalled.StartInfo.RedirectStandardOutput = true;
testInstalled.StartInfo.UseShellExecute = false;
testInstalled.StartInfo.CreateNoWindow = true;
testInstalled.Start();
var outputInstalled = testInstalled.StandardOutput.ReadToEnd();
testInstalled.WaitForExit();
if (!outputInstalled.Contains("git version"))
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
mainForm.AddLog("Git is not installed");
var notifyForm = new NotifyForm("Git is not installed\nPlease install it to install HSD dependencies",
"Install", "https://git-scm.com/download/win");
notifyForm.ShowDialog();
notifyForm.Dispose();
Environment.Exit(21);
return;
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
// Check if node installed
testInstalled = new Process();
testInstalled.StartInfo.FileName = "node";
testInstalled.StartInfo.Arguments = "-v";
testInstalled.StartInfo.RedirectStandardOutput = true;
testInstalled.StartInfo.UseShellExecute = false;
testInstalled.StartInfo.CreateNoWindow = true;
testInstalled.Start();
outputInstalled = testInstalled.StandardOutput.ReadToEnd();
testInstalled.WaitForExit();
if (!outputInstalled.Contains("v"))
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
mainForm.AddLog("Node is not installed");
var notifyForm = new NotifyForm("Node is not installed\nPlease install it to install HSD dependencies",
"Install", "https://nodejs.org/en/download");
notifyForm.ShowDialog();
notifyForm.Dispose();
Environment.Exit(22);
return;
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
// Check if npm installed
testInstalled = new Process();
testInstalled.StartInfo.FileName = "cmd.exe";
testInstalled.StartInfo.Arguments = "npm -v";
testInstalled.StartInfo.RedirectStandardOutput = true;
testInstalled.StartInfo.UseShellExecute = false;
testInstalled.StartInfo.CreateNoWindow = false;
testInstalled.Start();
// Wait 3 seconds and then kill
Thread.Sleep(3000);
testInstalled.Kill();
outputInstalled = testInstalled.StandardOutput.ReadToEnd();
testInstalled.WaitForExit();
if (Regex.IsMatch(outputInstalled, @"^\d+\.\d+\.\d+$"))
{
2023-07-15 22:17:49 +10:00
mainForm.AddLog("NPM is not installed");
mainForm.AddLog(outputInstalled);
var notifyForm = new NotifyForm("NPM is not installed\nPlease install it to install HSD dependencies",
"Install", "https://docs.npmjs.com/downloading-and-installing-node-js-and-npm");
notifyForm.ShowDialog();
notifyForm.Dispose();
Environment.Exit(23);
return;
}
2023-07-15 22:17:49 +10:00
mainForm.AddLog("Prerequisites installed");
var startInfo = new ProcessStartInfo();
startInfo.FileName = "git";
startInfo.Arguments = $"clone {repositoryUrl} {destinationPath}";
if (repositoryUrl == "https://github.com/handshake-org/hsd.git")
startInfo.Arguments = $"clone --depth 1 --branch latest {repositoryUrl} {destinationPath}";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = hideScreen;
var process = new Process();
process.StartInfo = startInfo;
process.Start();
var output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
while (!process.HasExited) output += process.StandardOutput.ReadToEnd();
var psiNpmRunDist = new ProcessStartInfo
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
FileName = "cmd",
RedirectStandardInput = true,
WorkingDirectory = destinationPath,
CreateNoWindow = hideScreen
};
var pNpmRunDist = Process.Start(psiNpmRunDist);
pNpmRunDist.StandardInput.WriteLine("npm install & exit");
pNpmRunDist.WaitForExit();
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
catch (Exception ex)
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
mainForm.AddLog("Git/NPM Install FAILED");
mainForm.AddLog(ex.Message);
if (ex.Message.Contains("to start process 'git'"))
2023-07-05 16:44:34 +10:00
{
2023-07-19 15:06:25 +10:00
var notifyForm = new NotifyForm("Git is not installed\nPlease install it to install HSD dependencies",
"Install", "https://git-scm.com/download/win");
2023-07-15 22:17:49 +10:00
notifyForm.ShowDialog();
notifyForm.Dispose();
2023-07-19 15:06:25 +10:00
Environment.Exit(21);
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
else if (ex.Message.Contains("to start process 'node'"))
2023-07-05 16:44:34 +10:00
{
2023-07-19 15:06:25 +10:00
var notifyForm = new NotifyForm("Node is not installed\nPlease install it to install HSD dependencies",
"Install", "https://nodejs.org/en/download");
2023-07-15 22:17:49 +10:00
notifyForm.ShowDialog();
notifyForm.Dispose();
2023-07-19 15:06:25 +10:00
Environment.Exit(22);
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
else if (ex.Message.Contains("to start process 'npm'"))
2023-07-05 16:44:34 +10:00
{
2023-07-19 15:06:25 +10:00
var notifyForm = new NotifyForm("NPM is not installed\nPlease install it to install HSD dependencies",
"Install", "https://docs.npmjs.com/downloading-and-installing-node-js-and-npm");
2023-07-15 22:17:49 +10:00
notifyForm.ShowDialog();
notifyForm.Dispose();
2023-07-19 15:06:25 +10:00
Environment.Exit(23);
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
else
2023-07-05 16:44:34 +10:00
{
2023-07-15 22:17:49 +10:00
var notifyForm = new NotifyForm("Git/NPM Install FAILED\nCheck logs for more details");
notifyForm.ShowDialog();
notifyForm.Dispose();
2023-07-19 15:06:25 +10:00
Environment.Exit(24);
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
}
2023-07-15 22:17:49 +10:00
public bool CheckNodeInstalled()
{
try
{
// Create a new process to execute the 'node' command
var process = new Process();
process.StartInfo.FileName = "node";
process.StartInfo.Arguments = "--version";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
// Start the process and read the output
process.Start();
var output = process.StandardOutput.ReadToEnd();
// Wait for the process to exit
process.WaitForExit();
// Check if the output contains a version number
return !string.IsNullOrEmpty(output);
}
catch (Exception)
{
// An exception occurred, indicating that 'node' is not installed or accessible
return false;
}
2023-07-05 16:44:34 +10:00
}
2023-07-15 22:17:49 +10:00
#endregion
2023-07-05 16:44:34 +10:00
}