KAZIK/build/installer/CasinoSetup/PackageUninstaller.cs

147 lines
4.6 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml.Linq;
using Microsoft.Win32;
namespace CasinoSetup;
public static class PackageUninstaller
{
public static int Remove(string target, bool removeSaves, bool integration)
{
target = InstallPaths.Normalize(target);
InstallPaths.ValidateTarget(target, null);
if (!File.Exists(InstallPaths.SafeFile(target, "casino-install.xml")))
{
throw new IOException("Нет списка установленных файлов. Автоматическое удаление запрещено.");
}
HashSet<string> hashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
hashSet.Add(Path.Combine(target, "runtime"));
int num = 0;
InstallPaths.NoLinks(Path.Combine(target, ".casino-install.lock"));
using (new FileStream(Path.Combine(target, ".casino-install.lock"), FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, 1, FileOptions.DeleteOnClose))
{
List<XElement> list = PackageInstaller.ReadReceipt(target);
foreach (XElement item in list)
{
string path = InstallPaths.SafeFile(target, (string)item.Attribute("path"));
if (File.Exists(path) && InstallPaths.HashFile(path).Equals((string)item.Attribute("sha256"), StringComparison.OrdinalIgnoreCase))
{
using (new FileStream(path, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
}
}
}
foreach (XElement item2 in list)
{
string path2 = InstallPaths.SafeFile(target, (string)item2.Attribute("path"));
string directoryName = Path.GetDirectoryName(path2);
while (directoryName != target && directoryName != null)
{
hashSet.Add(directoryName);
directoryName = Path.GetDirectoryName(directoryName);
}
if (File.Exists(path2))
{
if (!InstallPaths.HashFile(path2).Equals((string)item2.Attribute("sha256"), StringComparison.OrdinalIgnoreCase))
{
num++;
}
else
{
File.Delete(path2);
}
}
}
if (integration)
{
string text = PackageInstaller.RegistryKey(target);
using (RegistryKey registryKey = Registry.CurrentUser.OpenSubKey(text))
{
if (registryKey != null && !target.Equals((string)registryKey.GetValue("InstallLocation"), StringComparison.OrdinalIgnoreCase))
{
throw new IOException("Запись удаления относится к другой папке.");
}
}
Registry.CurrentUser.DeleteSubKeyTree(text, throwOnMissingSubKey: false);
RemoveShortcut(target);
}
if (removeSaves)
{
string path3 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "AssemblerCasino");
DeleteSaveTree(path3);
}
File.Delete(InstallPaths.SafeFile(target, "casino-install.xml"));
foreach (string item3 in hashSet.OrderByDescending((string d) => d.Length))
{
InstallPaths.NoLinks(item3);
if (Directory.Exists(item3) && !Directory.EnumerateFileSystemEntries(item3).Any())
{
Directory.Delete(item3);
}
}
}
if (!Directory.EnumerateFileSystemEntries(target).Any())
{
Directory.Delete(target);
}
return num;
}
private static void DeleteSaveTree(string path)
{
InstallPaths.NoLinks(path);
if (Directory.Exists(path))
{
string[] files = Directory.GetFiles(path);
foreach (string path2 in files)
{
InstallPaths.NoLinks(path2);
File.Delete(path2);
}
string[] directories = Directory.GetDirectories(path);
foreach (string path3 in directories)
{
DeleteSaveTree(path3);
}
Directory.Delete(path);
}
}
private static void RemoveShortcut(string target)
{
string text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "Assembler Casino.lnk");
if (!File.Exists(text))
{
return;
}
Type typeFromProgID = Type.GetTypeFromProgID("WScript.Shell");
if (typeFromProgID == null)
{
return;
}
object obj = Activator.CreateInstance(typeFromProgID);
object obj2 = null;
try
{
obj2 = typeFromProgID.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, obj, new object[1] { text });
string text2 = (string)obj2.GetType().InvokeMember("TargetPath", BindingFlags.GetProperty, null, obj2, null);
if (text2.Equals(Path.Combine(target, "Assembler Casino.exe"), StringComparison.OrdinalIgnoreCase))
{
File.Delete(text);
}
}
finally
{
if (obj2 != null)
{
Marshal.FinalReleaseComObject(obj2);
}
Marshal.FinalReleaseComObject(obj);
}
}
}