Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

Commit

Permalink
update extractor target net5 for the initial 5-> 6 update
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuki-Codes committed May 30, 2022
1 parent 6b9f76a commit 5f72c59
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 104 deletions.
205 changes: 103 additions & 102 deletions UpdateExtractor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,137 +1,138 @@
// © Anamnesis.
// Licensed under the MIT license.

namespace UpdateExtractor;

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;

public class Program
namespace UpdateExtractor
{
public static void Main(string[] args)
{
string processName = "Unknown";
using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;

try
public class Program
{
public static void Main(string[] args)
{
if (args.Length != 2)
throw new Exception("Invalid arguments. Update Extractor must be run with the following arguments: 1) destination directory, 2) name of orignal process.");

string? destDir = args[0];
processName = args[1];
Console.WriteLine($"{processName} Updater");
string processName = "Unknown";

Console.Write($"Waiting for {processName} to terminate");
while (true)
try
{
Process[] procs = Process.GetProcessesByName(processName);
if (procs.Length <= 0)
{
break;
}
else
{
Thread.Sleep(100);
Console.Write(".");
}
}
if (args.Length != 2)
throw new Exception("Invalid arguments. Update Extractor must be run with the following arguments: 1) destination directory, 2) name of orignal process.");

Console.WriteLine(" done.");
string? destDir = args[0];
processName = args[1];
Console.WriteLine($"{processName} Updater");

string sourceDir = Path.GetTempPath() + "/AnamnesisUpdateLatest/";
Console.Write($"Waiting for {processName} to terminate");
while (true)
{
Process[] procs = Process.GetProcessesByName(processName);
if (procs.Length <= 0)
{
break;
}
else
{
Thread.Sleep(100);
Console.Write(".");
}
}

if (!Directory.Exists(sourceDir))
throw new Exception("Unable to determine current process path");
Console.WriteLine(" done.");

if (string.IsNullOrEmpty(sourceDir))
throw new Exception("Unable to determine source directory");
string sourceDir = Path.GetTempPath() + "/AnamnesisUpdateLatest/";

if (string.IsNullOrEmpty(destDir))
throw new Exception("Unable to determine destination directory");
if (!Directory.Exists(sourceDir))
throw new Exception("Unable to determine current process path");

// Is destDir actually a file path, get a directory instead.
if (File.Exists(destDir))
{
destDir = Path.GetDirectoryName(destDir);
if (string.IsNullOrEmpty(sourceDir))
throw new Exception("Unable to determine source directory");

if (string.IsNullOrEmpty(destDir))
{
throw new Exception("Unable to determine destination directory");

// Is destDir actually a file path, get a directory instead.
if (File.Exists(destDir))
{
destDir = Path.GetDirectoryName(destDir);

if (string.IsNullOrEmpty(destDir))
{
throw new Exception("Unable to determine destination directory");
}
}
}

destDir = destDir.Replace('/', '\\');
destDir = destDir.Trim('\\');
destDir += "\\";
destDir = destDir.Replace('/', '\\');
destDir = destDir.Trim('\\');
destDir += "\\";

string oldExe = destDir + "Anamnesis.exe";
string oldExe = destDir + "Anamnesis.exe";

if (!File.Exists(oldExe))
throw new Exception($"No Anamnesis executable found at: {oldExe}");
if (!File.Exists(oldExe))
throw new Exception($"No Anamnesis executable found at: {oldExe}");

Console.WriteLine("Cleaning old version");
DeleteFileIfExists(destDir + "Anamnesis.exe");
DeleteFileIfExists(destDir + "AnamnesisLauncher.exe");
DeleteFileIfExists(destDir + "Anamnesis.pdb");
DeleteFileIfExists(destDir + "Anamnesis.xml");
DeleteFileIfExists(destDir + "Version.txt");
Console.WriteLine("Cleaning old version");
DeleteFileIfExists(destDir + "Anamnesis.exe");
DeleteFileIfExists(destDir + "AnamnesisLauncher.exe");
DeleteFileIfExists(destDir + "Anamnesis.pdb");
DeleteFileIfExists(destDir + "Anamnesis.xml");
DeleteFileIfExists(destDir + "Version.txt");

DeleteDirectoryIfExists(destDir + "Data");
DeleteDirectoryIfExists(destDir + "Languages");
DeleteDirectoryIfExists(destDir + "Updater");
DeleteDirectoryIfExists(destDir + "bin");
DeleteDirectoryIfExists(destDir + "Data");
DeleteDirectoryIfExists(destDir + "Languages");
DeleteDirectoryIfExists(destDir + "Updater");
DeleteDirectoryIfExists(destDir + "bin");

Console.WriteLine("Copying Update Files");
Console.WriteLine("Copying Update Files");

string[] files = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);
foreach (string sourceFile in files)
{
string destFile = sourceFile.Replace(sourceDir, destDir);
Console.WriteLine(" > " + destFile);
string[] files = Directory.GetFiles(sourceDir, "*.*", SearchOption.AllDirectories);
foreach (string sourceFile in files)
{
string destFile = sourceFile.Replace(sourceDir, destDir);
Console.WriteLine(" > " + destFile);

string? directory = Path.GetDirectoryName(destFile);
if (directory != null && !Directory.Exists(directory))
Directory.CreateDirectory(directory);
string? directory = Path.GetDirectoryName(destFile);
if (directory != null && !Directory.Exists(directory))
Directory.CreateDirectory(directory);

File.Copy(sourceFile, destFile, true);
}
File.Copy(sourceFile, destFile, true);
}

Console.WriteLine("Restarting application");
Console.WriteLine("Restarting application");

string launch = destDir + "AnamnesisSetup.exe";
Console.WriteLine(" > " + launch);
ProcessStartInfo start = new ProcessStartInfo(launch);
Process.Start(start);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Failed to update {processName}");
Console.WriteLine(ex.Message);
Console.WriteLine();
Console.WriteLine("Please download the update manually.");
Console.WriteLine();
Console.WriteLine("Press any key to close this window.");
Console.ReadKey();
string launch = destDir + "AnamnesisSetup.exe";
Console.WriteLine(" > " + launch);
ProcessStartInfo start = new ProcessStartInfo(launch);
Process.Start(start);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Failed to update {processName}");
Console.WriteLine(ex.Message);
Console.WriteLine();
Console.WriteLine("Please download the update manually.");
Console.WriteLine();
Console.WriteLine("Press any key to close this window.");
Console.ReadKey();
}
}
}

private static void DeleteFileIfExists(string path)
{
if (!File.Exists(path))
return;
private static void DeleteFileIfExists(string path)
{
if (!File.Exists(path))
return;

File.Delete(path);
}
File.Delete(path);
}

private static void DeleteDirectoryIfExists(string path)
{
if (!Directory.Exists(path))
return;
private static void DeleteDirectoryIfExists(string path)
{
if (!Directory.Exists(path))
return;

Directory.Delete(path, true);
Directory.Delete(path, true);
}
}
}
}
4 changes: 2 additions & 2 deletions UpdateExtractor/UpdateExtractor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<RuntimeIdentifiers>win-x64</RuntimeIdentifiers>
<AppendTargetFramework>False</AppendTargetFramework>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
Expand Down

0 comments on commit 5f72c59

Please sign in to comment.