1
0
Fork 0
This repository has been archived on 2024-02-06. You can view files and clone it, but cannot push or open issues or pull requests.
GeckoLoader/installer.cs

156 lines
4.6 KiB
C#
Raw Normal View History

2020-08-18 11:25:01 +09:00
using System;
2020-08-17 05:04:47 +09:00
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
public class Program
{
2020-08-18 11:36:42 +09:00
static void Main(string[] args)
{
Installer installer = new Installer();
installer.GetUserInput();
}
2020-08-17 05:04:47 +09:00
}
public class Installer
{
2020-08-18 11:36:42 +09:00
public string programfolder;
public bool copyfiles;
public bool overwrite;
2020-08-17 05:04:47 +09:00
public Installer()
{
2020-08-18 11:36:42 +09:00
programfolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GeckoLoader");
copyfiles = true;
overwrite = true;
}
private static void ClearConsoleLine(int index, bool moveto)
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, index);
Console.Write(new string(' ', Console.WindowWidth));
if (moveto) Console.SetCursorPosition(0, index);
else Console.SetCursorPosition(0, currentLineCursor);
}
private static string HandleConsoleQuestion(string msg, string[] options)
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
bool handled = false;
string input = String.Empty;
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
while (handled == false)
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
Console.Write("{0} ({1}): ", msg, String.Join("|", options));
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
input = Console.ReadLine();
if (options.Any(s => s.Contains(input.ToLower())))
{
handled = true;
2020-08-17 05:04:47 +09:00
}
2020-08-18 11:36:42 +09:00
else
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
ClearConsoleLine(Console.CursorTop - 1, true);
2020-08-17 05:04:47 +09:00
}
}
2020-08-18 11:36:42 +09:00
return input;
2020-08-17 05:04:47 +09:00
}
2020-08-18 11:36:42 +09:00
private void SetProgramFolderToPath(string folderName)
{
this.programfolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), folderName);
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
if (!Directory.Exists(this.programfolder)) Directory.CreateDirectory(this.programfolder);
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
var scope = EnvironmentVariableTarget.User;
var curPATH = Environment.GetEnvironmentVariable("PATH", scope);
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
if (!curPATH.Contains(this.programfolder))
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
var newValue = curPATH + ";" + this.programfolder;
Environment.SetEnvironmentVariable("PATH", newValue, scope);
2020-08-17 05:04:47 +09:00
}
2020-08-18 11:36:42 +09:00
}
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
private bool MoveFilesToprogramfolder(string wildcard, bool copy = true, bool overwrite = false)
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
DirectoryInfo cwd = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory()));
DirectoryInfo programspace = new DirectoryInfo(this.programfolder);
2020-08-18 11:25:01 +09:00
2020-08-18 11:36:42 +09:00
bool validinstall = false;
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
try
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
foreach (FileInfo file in programspace.EnumerateFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in programspace.EnumerateDirectories())
{
dir.Delete(true);
}
foreach (FileInfo file in cwd.EnumerateFiles(wildcard, SearchOption.TopDirectoryOnly))
{
string ext = ".exe.py.bin";
if (ext.Contains(file.Extension.ToLower()))
{
if (file.Extension.ToLower() == ".exe" && file.Name != "GeckoLoader.exe") continue;
if (file.Name.ToLower() == "geckoloader.py" || file.Name.ToLower() == "geckoloader.exe") validinstall = true;
file.CopyTo(Path.Combine(programspace.FullName, file.Name), true);
}
}
foreach (DirectoryInfo dir in cwd.EnumerateDirectories())
{
if (!Directory.Exists(Path.Combine(programspace.FullName, dir.Name)))
{
Directory.CreateDirectory(Path.Combine(programspace.FullName, dir.Name));
}
foreach (FileInfo subfile in dir.EnumerateFiles(wildcard, SearchOption.TopDirectoryOnly))
{
subfile.CopyTo(Path.Combine(programspace.FullName, dir.Name, subfile.Name), true);
}
}
2020-08-17 05:04:47 +09:00
}
2020-08-18 11:36:42 +09:00
catch (UnauthorizedAccessException e)
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
Console.WriteLine(String.Format("Insufficient privledges provided! {0}\nTry running with administrator privledges", e));
return false;
2020-08-17 05:04:47 +09:00
}
2020-08-18 11:36:42 +09:00
return validinstall;
}
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
public void GetUserInput()
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
string status;
string[] options = { "y", "n" };
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
Console.SetWindowSize(84, 30);
Console.Title = "GeckoLoader Installer";
Console.WriteLine("This installer modifies the Windows User PATH variable\n");
2020-08-17 05:04:47 +09:00
2020-08-18 11:36:42 +09:00
status = HandleConsoleQuestion("Are you sure you want to continue?", options);
2020-08-18 11:25:01 +09:00
2020-08-18 11:36:42 +09:00
if (status.ToLower() == "y")
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
this.SetProgramFolderToPath("GeckoLoader");
if (this.MoveFilesToprogramfolder("*", this.copyfiles, this.overwrite) == false)
{
Console.WriteLine("Failed to install :( Is Geckoloader and its dependancies in the same directory?");
}
2020-08-18 11:25:01 +09:00
else
{
2020-08-18 11:36:42 +09:00
Console.WriteLine("Finished installation successfully! You can run GeckoLoader from anywhere\nby simply calling \"GeckoLoader <dol> <gct|txt|folder> [options]\"");
2020-08-18 11:25:01 +09:00
}
2020-08-17 05:04:47 +09:00
}
2020-08-18 11:36:42 +09:00
else
2020-08-17 05:04:47 +09:00
{
2020-08-18 11:36:42 +09:00
Console.WriteLine("That's okay! You can always run this program again when you feel ready.");
2020-08-17 05:04:47 +09:00
}
2020-08-18 11:36:42 +09:00
Console.Write("Press any key to exit . . . ");
Console.ReadKey();
}
2020-08-17 05:04:47 +09:00
}