Skip to content

Commit

Permalink
[v220402] - Rewrite Code (Issue #124)
Browse files Browse the repository at this point in the history
Rewrote the code to use Ionic.ZipFile instead of System.IO.Compression in an attempt to fix issue #124

Co-Authored-By: Ryan Walpole <69621127+RyanWalpole@users.noreply.github.com>
  • Loading branch information
RWELabs and RyanWalpole committed Apr 4, 2024
1 parent 8a996f8 commit 15b8368
Showing 1 changed file with 63 additions and 80 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Stardew_Mod_Manager.Startup;
using Ionic.Zip;
using Stardew_Mod_Manager.Startup;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand All @@ -24,24 +25,29 @@ public MPInstaller()
string SMAPIVersionText = "SMAPI " + "v" + SMAPIVersion.ProductVersion;
SMAPIVersionInfo.Text = SMAPIVersionText;

//For each mod in mods directory...
foreach (string folder in Directory.GetDirectories(Properties.Settings.Default.ModsDir))
{
//Add to disable list
ModsToDisable.Items.Add(Path.GetFileName(folder));
}

//For each mod in the disable list
foreach (string item in ModsToDisable.Items)
{
//Move to the inactive mods folder
Directory.Move(Properties.Settings.Default.ModsDir + item, Properties.Settings.Default.InactiveModsDir + item);
}


string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string SDVAppData = AppData + @"\RWE Labs\SDV Mod Manager\";
string UnpackLocation = Properties.Settings.Default.StardewDir + @"\tmp\unpack\";
string LA = Path.GetFileName(Properties.Settings.Default.LaunchArguments);

//for every folder in the modpack (unpacked)
foreach (string folder in Directory.GetDirectories(UnpackLocation))
{
//mark mod as one to be overwritten
if (Directory.Exists(Properties.Settings.Default.InactiveModsDir + Path.GetFileName(folder)))
{
OverwriteMods.Items.Add(Path.GetFileName(folder));
Expand All @@ -52,6 +58,7 @@ public MPInstaller()

try
{
//read the metadata for the modpack if possible
MetaInfRead.LoadFile(UnpackLocation + @"meta.ini", RichTextBoxStreamType.PlainText);
foreach(string line in MetaInfRead.Lines)
{
Expand Down Expand Up @@ -92,19 +99,20 @@ private void DoModInstall_DoWork(object sender, DoWorkEventArgs e)
string zipPath = UnpackLocation + LA.Replace(".sdvmp", ".zip");
string extractPath = Properties.Settings.Default.InactiveModsDir;

//move the modpack to the unpack location, as a zip file
File.Move(Properties.Settings.Default.LaunchArguments, UnpackLocation + LA.Replace(".sdvmp", ".zip"));

using (ZipArchive archive = ZipFile.OpenRead(zipPath))
//extract the zip file to the inactive mods folder
using (Ionic.Zip.ZipFile zipFile = Ionic.Zip.ZipFile.Read(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
foreach(ZipEntry zipEntry in zipFile)
{
if (Directory.Exists(Properties.Settings.Default.InactiveModsDir + entry.FullName))
{
Directory.Delete(Properties.Settings.Default.InactiveModsDir + entry.FullName, true);
}
//overwrite if the file already exists!
zipEntry.Extract(extractPath, ExtractExistingFileAction.OverwriteSilently);
}
}

//delete the metadata for the modpack after installation
if(File.Exists(Properties.Settings.Default.ModsDir + @"\meta.ini"))
{
File.Delete(Properties.Settings.Default.ModsDir + @"\meta.ini");
Expand Down Expand Up @@ -139,96 +147,71 @@ private void DoModInstall_RunWorkerCompleted(object sender, RunWorkerCompletedEv
private void DoMovementOperation_Tick(object sender, EventArgs e)
{
DoMovementOperation.Stop();
DoModDelete.RunWorkerAsync();
DoFinish();
}

private void DoModDelete_DoWork(object sender, DoWorkEventArgs e)
private void DoFinish()
{
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string SDVAppData = AppData + @"\RWE Labs\SDV Mod Manager\";
string PresetName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.LaunchArguments);
//string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//string SDVAppData = AppData + @"\RWE Labs\SDV Mod Manager\";
string UnpackLocation = Properties.Settings.Default.StardewDir + @"\tmp\unpack\";
string LA = Path.GetFileName(Properties.Settings.Default.LaunchArguments);

string zipPath = UnpackLocation + LA.Replace(".sdvmp", ".zip");
string extractPath = Properties.Settings.Default.InactiveModsDir;

ZipFile.ExtractToDirectory(zipPath, extractPath);
}
//Move the zip file back to the original location, with SDVMP file extension
File.Move(UnpackLocation + LA.Replace(".sdvmp", ".zip"), Properties.Settings.Default.LaunchArguments);

private void DoModDelete_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
//Generate a preset based on the installed modpack
foreach (string item in ModsToInstall.Items)
{
MessageBox.Show("The operation was cancelled by the user or the system.", "Stardew Valley Modpack Installer", MessageBoxButtons.OK, MessageBoxIcon.Warning);
CreateErrorLog("Modpack installation was cancelled by the user or the system: " + e.Error.Message);
Application.Exit();
PresetGenerator.AppendText(item + Environment.NewLine);
}
else if (e.Error != null)
{
//resultLabel.Text = "Error: " + e.Error.Message;
MessageBox.Show("The application experienced an issue whilst trying to install the modpack: " + Environment.NewLine + e.Error.Message, "Stardew Valley Modpack Installer", MessageBoxButtons.OK, MessageBoxIcon.Error);
CreateErrorLog("The application experienced an issue whilst trying to install the modpack: " + e.Error.Message);
Application.Exit();
}
else

PresetGenerator.AppendText("ConsoleCommands" + Environment.NewLine);
PresetGenerator.AppendText("ErrorHandler" + Environment.NewLine);
PresetGenerator.AppendText("SaveBackup" + Environment.NewLine);

//Save the preset
PresetGenerator.SaveFile(Properties.Settings.Default.PresetsDir + PresetName + ".txt", RichTextBoxStreamType.PlainText);

//Tell user it was successful.
DialogResult dr = MessageBox.Show("The modpack has been successfully installed. We've added a preset so you can easily one-click enable this modpack. The preset is called: " + PresetName + Environment.NewLine + Environment.NewLine + "Would you like to open the Stardew Valley Mod Manager now, to re-enable your mods?", "Stardew Valley Mod Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
string PresetName = Path.GetFileNameWithoutExtension(Properties.Settings.Default.LaunchArguments);
string AppData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string SDVAppData = AppData + @"\RWE Labs\SDV Mod Manager\";
string UnpackLocation = Properties.Settings.Default.StardewDir + @"\tmp\unpack\";
string LA = Path.GetFileName(Properties.Settings.Default.LaunchArguments);
this.Hide();

Properties.Settings.Default.LaunchArguments = null;
Properties.Settings.Default.Save();

File.Move(UnpackLocation + LA.Replace(".sdvmp", ".zip"), Properties.Settings.Default.LaunchArguments);
MainPage splash = new MainPage();
splash.Show();
splash.Activate();

foreach (string item in ModsToInstall.Items)
string DeleteLocation = Properties.Settings.Default.StardewDir + @"\tmp\";
try
{
PresetGenerator.AppendText(item + Environment.NewLine);
Directory.Delete(DeleteLocation, true);
}
catch (Exception ex)
{
CreateErrorLog("Cleanup operations were unable to take place. Error Message: " + ex.Message);
}
}
else
{
Properties.Settings.Default.LaunchArguments = null;
Properties.Settings.Default.Save();
Application.Exit();

PresetGenerator.AppendText("ConsoleCommands" + Environment.NewLine);
PresetGenerator.AppendText("ErrorHandler" + Environment.NewLine);
PresetGenerator.AppendText("SaveBackup" + Environment.NewLine);

PresetGenerator.SaveFile(Properties.Settings.Default.PresetsDir + PresetName + ".txt", RichTextBoxStreamType.PlainText);

DialogResult dr = MessageBox.Show("The modpack has been successfully installed. We've added a preset so you can easily one-click enable this modpack. The preset is called: " + PresetName + Environment.NewLine + Environment.NewLine + "Would you like to open the Stardew Valley Mod Manager now, to re-enable your mods?", "Stardew Valley Mod Manager", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
string DeleteLocation = Properties.Settings.Default.StardewDir + @"\tmp\";
try
{
this.Hide();

Properties.Settings.Default.LaunchArguments = null;
Properties.Settings.Default.Save();

MainPage splash = new MainPage();
splash.Show();
splash.Activate();

string DeleteLocation = Properties.Settings.Default.StardewDir + @"\tmp\";
try
{
Directory.Delete(DeleteLocation, true);
}
catch (Exception ex)
{
CreateErrorLog("Cleanup operations were unable to take place. Error Message: " + ex.Message);
}
Directory.Delete(DeleteLocation, true);
}
else
catch (Exception ex)
{
Properties.Settings.Default.LaunchArguments = null;
Properties.Settings.Default.Save();
Application.Exit();

string DeleteLocation = Properties.Settings.Default.StardewDir + @"\tmp\";
try
{
Directory.Delete(DeleteLocation, true);
}
catch (Exception ex)
{
//MessageBox.Show("Cleanup operations were unable to take place.");
CreateErrorLog("Cleanup operations were unable to take place. Error Message: " + ex.Message);
}
//MessageBox.Show("Cleanup operations were unable to take place.");
CreateErrorLog("Cleanup operations were unable to take place. Error Message: " + ex.Message);
}
}
}
Expand Down

0 comments on commit 15b8368

Please sign in to comment.