Skip to content

Commit

Permalink
Merge pull request #143 from Shivansps/mod-install-rework
Browse files Browse the repository at this point in the history
Mod install/modify rework
  • Loading branch information
Shivansps authored Jan 28, 2024
2 parents 44b9967 + 79ca8dc commit dd78206
Show file tree
Hide file tree
Showing 4 changed files with 252 additions and 45 deletions.
51 changes: 50 additions & 1 deletion Knossos.NET/Models/Mod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public List<ModDependency> GetMissingDependenciesList(bool overrideSettings = fa
missingDep.id = dep.id;
missingDep.version = dep.version;
missingDep.packages = missingPkg;
missingDep.originalDependency = dep;
missingDependencyList.Add(missingDep);
}
}
Expand Down Expand Up @@ -885,6 +886,26 @@ public static bool IsMetaUpdate(Mod modA, Mod modB)
}
return false;
}

/// <summary>
/// Try to track down the mod pkg that a ModDependency belongs too
/// </summary>
/// <param name="dependency"></param>
/// <returns>Returns the modpkg or null if not found</returns>
public ModPackage? FindPackageWithDependency(ModDependency? dependency)
{
if(dependency != null && packages != null && packages.Any())
{
foreach(var pkg in packages)
{
if(pkg.dependencies != null && pkg.dependencies.FirstOrDefault(dep => dep == dependency) != null)
{
return pkg;
}
}
}
return null;
}
}

public class ModMember
Expand Down Expand Up @@ -917,10 +938,29 @@ public class ModPackage
public object? checkNotes { get; set; }

/* Knet Only */
/// <summary>
/// used for pkg display in a checkbox.
/// NOT Saved in the json
/// </summary>
[JsonIgnore]
public bool isSelected { get; set; } = false;

/// <summary>
/// used for display (to enable/disabled chkbox) and to enable/disable the package in devmode
/// Saved in the json
/// </summary>
public bool isEnabled { get; set; } = true;
/// <summary>
/// Used to display checkbox tooltip
/// NOT saved in the Json
/// </summary>
[JsonIgnore]
public string tooltip { get; set; } = string.Empty;
/// <summary>
/// Used to indicate a pkg is needed during mod install checkbox selection
/// Used to change checkbox foreground color during mod install/modify display
/// </summary>
[JsonIgnore]
public bool isRequired { get; set; } = false;
[JsonIgnore]
public int buildScore { get; set; } = 0;
}
Expand All @@ -931,6 +971,15 @@ public class ModDependency
public string? version { get; set; } // required, https://getcomposer.org/doc/01-basic-usage.md#package-versions
public List<string>? packages { get; set; } // optional, specifies which optional and recommended packages are also required

/// <summary>
/// Used to store the original dependency reference during mod.GetMissingDependencies
/// Useful to track down the original pkg this dependency belongs to
/// mod.FindPackageWithDependency()
/// Not saved in Json
/// </summary>
[JsonIgnore]
public ModDependency? originalDependency;

/// <summary>
/// Returns the best installed mod that meets this dependency by semantic version, null if none.
/// Also returns null if the ID is a FSO build.
Expand Down
47 changes: 47 additions & 0 deletions Knossos.NET/ViewModels/Templates/TaskItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2988,6 +2988,7 @@ public async Task<bool> InstallMod(Mod mod, CancellationTokenSource cancelSource
installed = Knossos.GetInstalledMod(mod.id, mod.version);
if (installed != null)
{
installed.ReLoadJson();
Name = "Modify " + mod.ToString().Replace("_", "-"); ;
compressMod = installed.modSettings.isCompressed;
}
Expand Down Expand Up @@ -3058,7 +3059,52 @@ public async Task<bool> InstallMod(Mod mod, CancellationTokenSource cancelSource
await Dispatcher.UIThread.InvokeAsync(() => TaskList.Insert(0, msg));
}

/* Delete pkgs */
if (installed != null)
{
bool save = false;
foreach (var modpkg in mod.packages.ToList())
{
var installedPkg = installed.packages.FirstOrDefault(p => p.name == modpkg.name);
if (modpkg.filelist != null && !modpkg.isSelected && installedPkg != null)
{
int delCount = 0;
var newTask = new TaskItemViewModel();
Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.InstallMod(delete mod file)", "Deleting package: " + modpkg.name + " MOD: " + mod);
Dispatcher.UIThread.Invoke(() =>
{
newTask.ShowMsg("Deleting pkg: " + modpkg.name, null, Brushes.Red);
TaskList.Add(newTask);
});
foreach (var file in modpkg.filelist)
{
try
{
if (File.Exists(installed.fullPath + Path.DirectorySeparatorChar + file.filename))
{
File.Delete(installed.fullPath + Path.DirectorySeparatorChar + file.filename);
delCount++;
}
}
catch(Exception ex)
{
Log.Add(Log.LogSeverity.Error, "TaskItemViewModel.InstallMod(delete mod file)", ex);
}
}
Log.Add(Log.LogSeverity.Information, "TaskItemViewModel.InstallMod(delete mod file)", "Files deleted: " + delCount);
installed.packages.Remove(installedPkg);
mod.packages.Remove(modpkg);
save = true;
}
}
if(save)
{
installed.SaveJson();
}
}

int vPExtractionNeeded = 0;

for (int i = mod.packages.Count - 1; i >= 0; i--)
{
bool alreadyInstalled = false;
Expand Down Expand Up @@ -3099,6 +3145,7 @@ public async Task<bool> InstallMod(Mod mod, CancellationTokenSource cancelSource
}
}


/* Is there is nothing new to install just end the task */
if(files.Count == 0 && !metaUpdate)
{
Expand Down
Loading

0 comments on commit dd78206

Please sign in to comment.