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

204 lines
6.5 KiB
C#
Raw Normal View History

2020-08-18 11:25:01 +09:00
using System;
2020-08-27 07:58:41 +09:00
using System.Collections.Generic;
2020-08-17 05:04:47 +09:00
using System.IO;
using System.Linq;
2020-08-25 21:06:56 +09:00
using System.Xml.Serialization;
2020-08-17 05:04:47 +09:00
public class Program
{
2020-08-18 11:36:42 +09:00
static void Main(string[] args)
{
2020-08-18 12:05:05 +09:00
Installer installer = new Installer();
installer.GetUserInput();
2020-08-18 11:36:42 +09:00
}
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 12:05:05 +09:00
programfolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GeckoLoader");
copyfiles = true;
overwrite = true;
2020-08-18 11:36:42 +09:00
}
2020-08-27 07:58:41 +09:00
void CopyAll (DirectoryInfo source, DirectoryInfo destination, string wildcard, string[] exclude, int maxdepth = 4)
2020-08-25 21:06:56 +09:00
{
if (maxdepth <= 0) return;
DirectoryInfo[] subdirs = source.GetDirectories();
foreach (DirectoryInfo dirPath in source.EnumerateDirectories())
2020-08-27 07:58:41 +09:00
{
2020-08-25 21:06:56 +09:00
Directory.CreateDirectory(dirPath.FullName.Replace(source.FullName, destination.FullName));
2020-08-27 07:58:41 +09:00
}
2020-08-25 21:06:56 +09:00
foreach (FileInfo filePath in source.EnumerateFiles(wildcard))
{
2020-08-27 07:58:41 +09:00
File.Copy(filePath.FullName, filePath.FullName.Replace(source.FullName, destination.FullName), true);
2020-08-25 21:06:56 +09:00
}
foreach (DirectoryInfo dir in subdirs)
{
DirectoryInfo dest = new DirectoryInfo(Path.Combine(destination.FullName, dir.Name));
DirectoryInfo src = new DirectoryInfo(dir.FullName);
CopyAll(src, dest, wildcard, exclude, maxdepth - 1);
}
}
2020-08-18 11:36:42 +09:00
private static void ClearConsoleLine(int index, bool moveto)
{
2020-08-18 12:05:05 +09:00
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);
2020-08-18 11:36:42 +09:00
}
private static string HandleConsoleQuestion(string msg, string[] options)
2020-08-17 05:04:47 +09:00
{
2020-08-18 12:05:05 +09:00
bool handled = false;
string input = String.Empty;
2020-08-17 05:04:47 +09:00
2020-08-18 12:05:05 +09:00
while (handled == false)
2020-08-17 05:04:47 +09:00
{
2020-08-18 12:05:05 +09:00
Console.Write("{0} ({1}): ", msg, String.Join("|", options));
2020-08-17 05:04:47 +09:00
2020-08-18 12:05:05 +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 12:05:05 +09:00
else
2020-08-17 05:04:47 +09:00
{
2020-08-18 12:05:05 +09:00
ClearConsoleLine(Console.CursorTop - 1, true);
2020-08-17 05:04:47 +09:00
}
}
2020-08-18 12:05:05 +09:00
return input;
2020-08-17 05:04:47 +09:00
}
2020-08-18 12:05:05 +09:00
private void SetProgramFolder(string folderName)
2020-08-18 11:36:42 +09:00
{
2020-08-27 07:58:41 +09:00
this.programfolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "GeckoLoader");
2020-08-18 12:05:05 +09:00
}
2020-08-27 07:58:41 +09:00
private void SetFolderToPath(string dir, string subdir)
2020-08-18 12:05:05 +09:00
{
var scope = EnvironmentVariableTarget.User;
var curPATH = Environment.GetEnvironmentVariable("PATH", scope);
2020-08-27 07:58:41 +09:00
if (!curPATH.Contains(Path.Combine(dir, subdir)))
2020-08-18 12:05:05 +09:00
{
2020-08-27 07:58:41 +09:00
var newValue = curPATH + ";" + Path.Combine(dir, subdir);
Environment.SetEnvironmentVariable("PATH", newValue.Replace(";;", ";"), scope);
2020-08-18 12:05:05 +09:00
}
}
2020-08-17 05:04:47 +09:00
2020-08-27 07:58:41 +09:00
private void RemoveFolderGroupFromPath(string dir)
2020-08-18 12:05:05 +09:00
{
var scope = EnvironmentVariableTarget.User;
var curPATH = Environment.GetEnvironmentVariable("PATH", scope);
2020-08-17 05:04:47 +09:00
2020-08-27 07:58:41 +09:00
string[] oldPATHList = curPATH.Split(';');
List<string> newPATHList = new List<string>();
foreach(string path in oldPATHList)
2020-08-17 05:04:47 +09:00
{
2020-08-27 07:58:41 +09:00
if (!path.ToLower().Contains(dir.ToLower()))
{
newPATHList.Add(path);
}
2020-08-17 05:04:47 +09:00
}
2020-08-27 07:58:41 +09:00
Environment.SetEnvironmentVariable("PATH", String.Join(";", newPATHList.ToArray()), scope);
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-26 12:10:37 +09:00
DirectoryInfo cwd = new DirectoryInfo(Path.Combine(Directory.GetCurrentDirectory(), "data"));
2020-08-18 12:05:05 +09:00
DirectoryInfo programspace = new DirectoryInfo(this.programfolder);
2020-08-18 11:25:01 +09:00
2020-08-26 11:59:19 +09:00
if (!programspace.Exists) programspace.Create();
2020-08-18 12:05:05 +09:00
try
2020-08-17 05:04:47 +09:00
{
2020-08-18 12:05:05 +09:00
foreach (FileInfo file in programspace.EnumerateFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in programspace.EnumerateDirectories())
{
dir.Delete(true);
}
2020-08-25 21:06:56 +09:00
//Copy dependancies
string[] exclude = { "installer.exe" };
this.CopyAll(cwd, programspace, wildcard, exclude);
2020-08-17 05:04:47 +09:00
}
2020-08-18 12:05:05 +09:00
catch (UnauthorizedAccessException e)
2020-08-17 05:04:47 +09:00
{
2020-08-18 12:05:05 +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-26 11:59:19 +09:00
return true;
2020-08-18 12:05:05 +09:00
}
private void DeleteProgramFolder()
{
if (Directory.Exists(this.programfolder)) Directory.Delete(this.programfolder, true);
2020-08-18 11:36:42 +09:00
}
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 12:05:05 +09:00
string status;
string[] continueoptions = { "y", "n" };
string[] actionoptions = { "install", "uninstall" };
2020-08-17 05:04:47 +09:00
2020-08-18 12:05:05 +09:00
Console.SetWindowSize(84, 20);
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 12:05:05 +09:00
status = HandleConsoleQuestion("Are you sure you want to continue?", continueoptions);
2020-08-18 11:25:01 +09:00
2020-08-18 12:05:05 +09:00
if (status.ToLower() == (string)continueoptions.GetValue(0))
2020-08-17 05:04:47 +09:00
{
2020-08-18 12:05:05 +09:00
this.SetProgramFolder("GeckoLoader");
status = HandleConsoleQuestion("What do you want to do?", actionoptions);
if (status.ToLower() == (string)actionoptions.GetValue(0))
{
2020-08-27 07:58:41 +09:00
this.RemoveFolderGroupFromPath(this.programfolder);
this.SetFolderToPath(this.programfolder, "");
2020-08-18 12:05:05 +09:00
if (this.MoveFilesToprogramfolder("*", this.copyfiles, this.overwrite) == false)
{
2020-08-27 07:58:41 +09:00
Console.WriteLine("Failed to install :(");
2020-08-18 12:05:05 +09:00
}
else
{
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
else
{
2020-08-27 07:58:41 +09:00
this.RemoveFolderGroupFromPath(this.programfolder);
2020-08-18 12:05:05 +09:00
this.DeleteProgramFolder();
Console.WriteLine("Uninstalled successfully!");
2020-08-18 11:25:01 +09:00
}
2020-08-18 12:05:05 +09:00
2020-08-17 05:04:47 +09:00
}
2020-08-18 12:05:05 +09:00
else
2020-08-17 05:04:47 +09:00
{
2020-08-18 12:05:05 +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 12:05:05 +09:00
Console.Write("Press any key to exit . . . ");
Console.ReadKey();
2020-08-18 11:36:42 +09:00
}
2020-08-17 05:04:47 +09:00
}