AFK Macro

Idle Mafia Macro/Source/src/Install.cs

Install.cs

Installs the macro on a fresh start, if you say yes · 150 lines · 7 KB

#if !LINUX
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
namespace IdleMafiaBot
{
static class Install
{
internal static string Folder
{
get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Programs", Program.Name); }
}
internal static bool Offer(string[] args, string dir)
{
if (Program.Whale || Program.Reroller || args.Length > 0) return false;
try
{
if (File.Exists(Path.Combine(dir, "IdleMafiaBot.ini"))) return false;
return !Same(dir, Folder);
}
catch (Exception) { return false; }
}
static bool Same(string a, string b)
{
return string.Equals(Path.GetFullPath(a).TrimEnd('\\'), Path.GetFullPath(b).TrimEnd('\\'), StringComparison.OrdinalIgnoreCase);
}
internal static bool AskAndRun(string exePath)
{
var answer = MessageBox.Show(
"Install " + Program.Name + " on this PC?\n\n"
+ "It goes in its own folder with a shortcut on your desktop and in the Start menu. "
+ "Check for updates keeps it up to date there.\n\n"
+ "No: it runs from this folder, as before.",
Program.Name, MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer != MessageBoxResult.Yes) return false;
string why = Copy(exePath, Folder);
if (why != null)
{
MessageBox.Show("Couldn't install it (" + why + "). It runs from this folder instead.", Program.Name);
return false;
}
string installed = Path.Combine(Folder, "IdleMafiaBot.exe");
Shortcuts(installed);
try
{
Process.Start(new ProcessStartInfo(installed, "--restarted") { WorkingDirectory = Folder, UseShellExecute = true });
return true;
}
catch (Exception e)
{
MessageBox.Show("It was installed, but Windows wouldn't start it (" + e.Message + "). Start it from the shortcut on your desktop.", Program.Name);
return true;
}
}
internal static string Copy(string exePath, string folder)
{
try
{
Directory.CreateDirectory(folder);
string target = Path.Combine(folder, "IdleMafiaBot.exe");
bool replace = true;
if (File.Exists(target))
{
Version have, mine = Version.Parse(Program.Version);
var info = FileVersionInfo.GetVersionInfo(target);
if (Version.TryParse(info.FileVersion ?? "", out have) && have > mine) replace = false;
}
if (replace)
{
string fresh = target + ".new";
File.Copy(exePath, fresh, true);
if (File.Exists(target)) File.Delete(target);
File.Move(fresh, target);
string readme = Path.Combine(Path.GetDirectoryName(exePath), "README.txt");
if (File.Exists(readme)) File.Copy(readme, Path.Combine(folder, "README.txt"), true);
}
return null;
}
catch (Exception e) { return e.Message; }
}
internal static void Shortcuts(string exe)
{
string name = Program.Name + ".lnk";
foreach (var place in new[] { Environment.SpecialFolder.DesktopDirectory, Environment.SpecialFolder.Programs })
try { MakeLink(Path.Combine(Environment.GetFolderPath(place), name), exe); }
catch (Exception) { }
}
static void MakeLink(string lnk, string exe)
{
var link = (IShellLinkW)new ShellLink();
link.SetPath(exe);
link.SetWorkingDirectory(Path.GetDirectoryName(exe));
link.SetIconLocation(exe, 0);
link.SetDescription(Program.Name);
((IPersistFile)link).Save(lnk, true);
Marshal.ReleaseComObject(link);
}
[ComImport, Guid("00021401-0000-0000-C000-000000000046")]
class ShellLink { }
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("000214F9-0000-0000-C000-000000000046")]
interface IShellLinkW
{
void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder file, int max, IntPtr fd, int flags);
void GetIDList(out IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder name, int max);
void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string name);
void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder dir, int max);
void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string dir);
void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder args, int max);
void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string args);
void GetHotkey(out short key);
void SetHotkey(short key);
void GetShowCmd(out int cmd);
void SetShowCmd(int cmd);
void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder path, int max, out int icon);
void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string path, int icon);
void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string path, int reserved);
void Resolve(IntPtr hwnd, int flags);
void SetPath([MarshalAs(UnmanagedType.LPWStr)] string file);
}
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("0000010b-0000-0000-C000-000000000046")]
interface IPersistFile
{
void GetClassID(out Guid clsid);
[PreserveSig] int IsDirty();
void Load([MarshalAs(UnmanagedType.LPWStr)] string file, int mode);
void Save([MarshalAs(UnmanagedType.LPWStr)] string file, [MarshalAs(UnmanagedType.Bool)] bool remember);
void SaveCompleted([MarshalAs(UnmanagedType.LPWStr)] string file);
void GetCurFile([MarshalAs(UnmanagedType.LPWStr)] out string file);
}
}
}
#endif