Skip to content

Commit

Permalink
[Store] Unpack to %temp% instead of boiler plating the folder name
Browse files Browse the repository at this point in the history
  • Loading branch information
KimihikoAkayasaki committed Mar 12, 2023
1 parent ace4ed8 commit 5866d14
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 42 deletions.
6 changes: 6 additions & 0 deletions Amethyst/Classes/Interfacing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ public static string GetAppDataPluginFolderDir(string relativeFilePath)
Environment.SpecialFolder.ApplicationData), "Amethyst", "Plugins", relativeFilePath);
}

public static string GetTempPluginFolderDir(string relativeFilePath)
{
Directory.CreateDirectory(Path.Join(Path.GetTempPath(), "Amethyst", "Plugins"));
return Path.Join(Path.GetTempPath(), "Amethyst", "Plugins", relativeFilePath);
}

public static string GetAppDataLogFileDir(string relativeFolderName, string relativeFilePath)
{
Directory.CreateDirectory(Path.Join(
Expand Down
28 changes: 19 additions & 9 deletions Amethyst/MVVM/StorePlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public void CancelPluginInstall()
private async Task<bool> ExecuteUpdates()
{
// Logic:
// - Download the zip to AppData\Roaming\Amethyst\Plugins\$({LatestRelease}.Guid).TEMPORARY
// - Download the zip to TempAppData\Amethyst\Plugins\$({LatestRelease}.Guid)
// - Unzip the plugin, dispose and delete the package if worked
// - Delete the .TEMPORARY suffix so Amethyst loads the plugin
// - Move from temporary AppData (tmp) to the regular one (roa)

try
{
Expand Down Expand Up @@ -190,7 +190,12 @@ private async Task<bool> ExecuteUpdates()
// Search for an empty folder in AppData
var installFolder = Interfacing.GetAppDataPluginFolderDir(
string.Join("_", LatestRelease.Guid.Split(
Path.GetInvalidFileNameChars().Append('.').ToArray())) + ".TEMPORARY");
Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Create an empty folder in TempAppData
var downloadFolder = Interfacing.GetTempPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Randomize the path if already exists
// Delete if only a single null folder
Expand All @@ -199,18 +204,23 @@ private async Task<bool> ExecuteUpdates()
if (Directory.EnumerateFileSystemEntries(installFolder).Any())
installFolder = Interfacing.GetAppDataPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())) + ".TEMPORARY");
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Else delete if empty
else Directory.Delete(installFolder, true);
}

// Try creating the install folder
Directory.CreateDirectory(installFolder!);
// Try reserving the install folder
if (Directory.Exists(installFolder!))
Directory.Delete(installFolder!, true);

// Try creating the download folder
if (!Directory.Exists(downloadFolder!))
Directory.CreateDirectory(downloadFolder!);

// Replace or create our installer file
var pluginArchive = await (await StorageFolder
.GetFolderFromPathAsync(installFolder)).CreateFileAsync(
.GetFolderFromPathAsync(downloadFolder)).CreateFileAsync(
"package.zip", CreationCollisionOption.ReplaceExisting);

// Create an output stream and push all the available data to it
Expand All @@ -223,13 +233,13 @@ private async Task<bool> ExecuteUpdates()

// Unpack the archive now
Logger.Info("Unpacking the new plugin from its archive...");
ZipFile.ExtractToDirectory(pluginArchive.Path, installFolder, true);
ZipFile.ExtractToDirectory(pluginArchive.Path, downloadFolder, true);

Logger.Info("Deleting the plugin installation package...");
File.Delete(pluginArchive.Path); // Cleanup after the install

// Rename the plugin folder if everything's fine
Directory.Move(installFolder, installFolder[..^".TEMPORARY".Length]);
Directory.Move(downloadFolder, installFolder);
}
catch (Exception e)
{
Expand Down
6 changes: 2 additions & 4 deletions Amethyst/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -367,12 +367,10 @@ public MainWindow()

// Search the "Plugins" AppData directory for assemblies that match the imports.
// Iterate over all directories in Plugins dir and add all * dirs to catalogs
// Discard working directories i.e. starting with ".TEMPORARY"
Logger.Info("Searching for shared plugins now...");
pluginDirectoryList.AddRange(Directory.EnumerateDirectories(
Interfacing.GetAppDataPluginFolderDir(""),
"*", SearchOption.TopDirectoryOnly)
.Where(x => !x.EndsWith(".TEMPORARY")));
Interfacing.GetAppDataPluginFolderDir(""),
"*", SearchOption.TopDirectoryOnly));

// Search for external plugins
try
Expand Down
83 changes: 54 additions & 29 deletions Amethyst/Pages/Plugins.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,12 @@ await ExecuteSearch(text // The provided text is a link
// Search for an empty folder in AppData
var installFolder = GetAppDataPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())) + ".TEMPORARY");
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Create an empty folder in TempAppData
var downloadFolder = GetTempPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Randomize the path if already exists
// Delete if only a single null folder
Expand All @@ -395,33 +400,37 @@ await ExecuteSearch(text // The provided text is a link
if (Directory.EnumerateFileSystemEntries(installFolder).Any())
installFolder = GetAppDataPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())) +
".TEMPORARY");
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Else delete if empty
else Directory.Delete(installFolder, true);
}

// Try creating the install folder
Directory.CreateDirectory(installFolder!);
// Try reserving the install folder
if (Directory.Exists(installFolder!))
Directory.Delete(installFolder!, true);

// Try creating the download folder
if (!Directory.Exists(downloadFolder!))
Directory.CreateDirectory(downloadFolder!);

// Unpack the archive now
Logger.Info("Unpacking the new plugin from its package...");
foreach (var x in files.ToList())
switch (x)
{
case StorageFile file:
Logger.Info($"Copying file {file.Name} to {installFolder}\\");
File.Copy(file.Path, Path.Join(installFolder, file.Name), true);
Logger.Info($"Copying file {file.Name} to {downloadFolder}\\");
File.Copy(file.Path, Path.Join(downloadFolder, file.Name), true);
break;
case StorageFolder folder:
Logger.Info($"Copying folder {folder.Name} to {installFolder}\\");
new DirectoryInfo(folder.Path).CopyToFolderAsync(installFolder);
Logger.Info($"Copying folder {folder.Name} to {downloadFolder}\\");
new DirectoryInfo(folder.Path).CopyToFolderAsync(downloadFolder);
break;
}

// Rename the plugin folder if everything's fine
Directory.Move(installFolder, installFolder[..^".TEMPORARY".Length]);
Directory.Move(downloadFolder, installFolder);

// Wait a bit
await Task.Delay(3000);
Expand Down Expand Up @@ -525,8 +534,12 @@ await ExecuteSearch(text // The provided text is a link
// Search for an empty folder in AppData
var installFolder = GetAppDataPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())) +
".TEMPORARY");
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Create an empty folder in TempAppData
var downloadFolder = GetTempPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Randomize the path if already exists
// Delete if only a single null folder
Expand All @@ -535,33 +548,37 @@ await ExecuteSearch(text // The provided text is a link
if (Directory.EnumerateFileSystemEntries(installFolder).Any())
installFolder = GetAppDataPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())) +
".TEMPORARY");
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Else delete if empty
else Directory.Delete(installFolder, true);
}

// Try creating the install folder
Directory.CreateDirectory(installFolder!);
// Try reserving the install folder
if (Directory.Exists(installFolder!))
Directory.Delete(installFolder!, true);

// Try creating the download folder
if (!Directory.Exists(downloadFolder!))
Directory.CreateDirectory(downloadFolder!);

// Unpack the archive now
Logger.Info("Unpacking the new plugin from its package...");
foreach (var x in await (files[0] as StorageFolder)!.GetItemsAsync())
switch (x)
{
case StorageFile file:
Logger.Info($"Copying file {file.Name} to {installFolder}\\");
File.Copy(file.Path, Path.Join(installFolder, file.Name), true);
Logger.Info($"Copying file {file.Name} to {downloadFolder}\\");
File.Copy(file.Path, Path.Join(downloadFolder, file.Name), true);
break;
case StorageFolder folder:
Logger.Info($"Copying folder {folder.Name} to {installFolder}\\");
new DirectoryInfo(folder.Path).CopyToFolderAsync(installFolder);
Logger.Info($"Copying folder {folder.Name} to {downloadFolder}\\");
new DirectoryInfo(folder.Path).CopyToFolderAsync(downloadFolder);
break;
}

// Rename the plugin folder if everything's fine
Directory.Move(installFolder, installFolder[..^".TEMPORARY".Length]);
Directory.Move(downloadFolder, installFolder);

// Wait a bit
await Task.Delay(3000);
Expand Down Expand Up @@ -660,8 +677,12 @@ await ExecuteSearch(text // The provided text is a link
// Search for an empty folder in AppData
var installFolder = GetAppDataPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())) +
".TEMPORARY");
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Create an empty folder in TempAppData
var downloadFolder = GetTempPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Randomize the path if already exists
// Delete if only a single null folder
Expand All @@ -670,22 +691,26 @@ await ExecuteSearch(text // The provided text is a link
if (Directory.EnumerateFileSystemEntries(installFolder).Any())
installFolder = GetAppDataPluginFolderDir(
string.Join("_", Guid.NewGuid().ToString().ToUpper()
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())) +
".TEMPORARY");
.Split(Path.GetInvalidFileNameChars().Append('.').ToArray())));

// Else delete if empty
else Directory.Delete(installFolder, true);
}

// Try creating the install folder
Directory.CreateDirectory(installFolder!);
// Try reserving the install folder
if (Directory.Exists(installFolder!))
Directory.Delete(installFolder!, true);

// Try creating the download folder
if (!Directory.Exists(downloadFolder!))
Directory.CreateDirectory(downloadFolder!);

// Unpack the archive now
Logger.Info("Unpacking the new plugin from its package...");
archive.ExtractToDirectory(installFolder, true);
archive.ExtractToDirectory(downloadFolder, true);

// Rename the plugin folder if everything's fine
Directory.Move(installFolder, installFolder[..^".TEMPORARY".Length]);
Directory.Move(downloadFolder, installFolder);

// Wait a bit
await Task.Delay(3000);
Expand Down

0 comments on commit 5866d14

Please sign in to comment.