Skip to content

Commit fecc1f0

Browse files
committed
tired
1 parent a98027b commit fecc1f0

File tree

15 files changed

+141
-90
lines changed

15 files changed

+141
-90
lines changed

Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ public static void Main(string[] args)
4444
task = Actions.Check.PerformCheck(cts.Token);
4545
break;
4646
case "add":
47-
if (args.Length != 2)
47+
if (args.Length < 2)
4848
{
4949
Log.Error($"Wrong number of arguments passed");
5050
return;
5151
}
5252

53-
task = Actions.Add.PerformAdd(args[1], cts.Token);
53+
task = Actions.Add.PerformAdd(args[1..], cts.Token);
5454
break;
5555
case "remove":
56-
if (args.Length != 2)
56+
if (args.Length < 2)
5757
{
5858
Log.Error($"Wrong number of arguments passed");
5959
return;
6060
}
6161

62-
task = Actions.Remove.PerformRemove(args[1], cts.Token);
62+
task = Actions.Remove.PerformRemove(args[1..], cts.Token);
6363
break;
6464
case "list":
6565
if (args.Length != 1)

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ Convinient command line mod manager for Minecraft using Modrinth.
2828

2929
> You can get the Modrinth id of a mod by going to the mod's page, clicking on the three dots (top right) and clicking "Copy ID".
3030
31+
## Known Issues
32+
33+
- When you use `mmm change`, it doesn't remove the unsupported mods from the listfile. (I mean it's fine, idk if I should fix it or not because it works)
34+
3135
## TODO
3236

3337
- Check for conflicts when adding/removing/updating a mod. (WIP)

Source/Actions/Add.cs

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,37 @@ namespace MMM.Actions;
22

33
public static class Add
44
{
5-
public static async Task PerformAdd(string query, CancellationToken ct)
5+
public static async Task PerformAdd(string[] queries, CancellationToken ct)
66
{
7-
if (Context.Instance.Modlist.Mods.Any(v => v.Id == query) &&
8-
Context.Instance.ModlistLock.Any(v => v.Id == query))
7+
foreach (string query in queries)
98
{
10-
Log.Error($"Mod {query} already added");
11-
return;
12-
}
9+
if (Context.Instance.Modlist.Mods.Any(v => v.Id == query) &&
10+
Context.Instance.ModlistLock.Any(v => v.Id == query))
11+
{
12+
Log.Error($"Mod {query} already added");
13+
continue;
14+
}
1315

14-
(string? id, string? name) = await ModrinthUtils.FindModOnline(query, ct);
16+
(string? id, string? name) = await ModrinthUtils.FindModOnline(query, ct);
1517

16-
if (id is null) return;
18+
if (id is null) continue;
1719

18-
if (Context.Instance.Modlist.Mods.Any(v => v.Id == id) &&
19-
Context.Instance.ModlistLock.Any(v => v.Id == id))
20-
{
21-
Log.Error($"Mod {name ?? query} already added");
22-
return;
23-
}
20+
if (Context.Instance.Modlist.Mods.Any(v => v.Id == id) &&
21+
Context.Instance.ModlistLock.Any(v => v.Id == id))
22+
{
23+
Log.Error($"Mod {name ?? query} already added");
24+
continue;
25+
}
2426

25-
Context.Instance.Modlist.Mods.Add(new ModEntry()
26-
{
27-
Id = id,
28-
Name = name,
29-
});
27+
Context.Instance.Modlist.Mods.Add(new ModEntry()
28+
{
29+
Id = id,
30+
Name = name,
31+
});
32+
}
3033

31-
(List<ModDownloadInfo> updates, List<ModUninstallInfo> uninstalls) = await ModInstaller.CheckChanges(ct);
34+
Changes changes = await ModInstaller.CheckChanges(ct);
3235

33-
await ModInstaller.PerformChanges(updates, uninstalls, ct);
36+
await ModInstaller.PerformChanges(changes, ct);
3437
}
3538
}

Source/Actions/Change.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ public static async Task PerformChange(string targetVersion, CancellationToken c
66
{
77
Context.Instance.Modlist.GameVersion = targetVersion;
88

9-
(List<ModDownloadInfo> modUpdates, List<ModUninstallInfo> unsupported) = await Update.CheckNewVersions(ct);
9+
Changes changes = await Update.CheckNewVersions(ct);
1010

11-
await ModInstaller.PerformChanges(modUpdates, unsupported, ct);
11+
await ModInstaller.PerformChanges(changes, ct);
1212
}
1313
}

Source/Actions/Check.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,8 @@ public static async Task PerformCheck(CancellationToken ct)
8282
}
8383
}
8484

85-
(List<ModDownloadInfo> updates, List<ModUninstallInfo> uninstalls) = await ModInstaller.CheckChanges(ct);
85+
Changes changes = await ModInstaller.CheckChanges(ct);
8686

87-
await ModInstaller.PerformChanges(updates, uninstalls, ct);
87+
await ModInstaller.PerformChanges(changes, ct);
8888
}
8989
}

Source/Actions/List.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ namespace MMM.Actions;
22

33
public static class List
44
{
5-
public static async Task PerformList(CancellationToken ct)
5+
public static Task PerformList(CancellationToken ct) => Task.Run(() =>
66
{
77
Log.MajorAction("Reading mods");
88

@@ -33,5 +33,5 @@ public static async Task PerformList(CancellationToken ct)
3333

3434
Log.NewLine();
3535
}
36-
}
36+
}, ct);
3737
}

Source/Actions/Remove.cs

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,69 @@ namespace MMM.Actions;
22

33
public static class Remove
44
{
5-
public static async Task PerformRemove(string modName, CancellationToken ct)
5+
public static async Task PerformRemove(string[] modNames, CancellationToken ct)
66
{
7-
string? modId = await Context.Instance.GetModId(modName, ct);
8-
ModEntry? mod = Context.Instance.Modlist.Mods.FirstOrDefault(v => v.Id == modId);
9-
ModLock? modlock = Context.Instance.ModlistLock.FirstOrDefault(v => v.Id == modId);
7+
List<(string ModId, ModEntry? Mod, ModLock Lock)> toUninstall = [];
108

11-
if (modlock is null)
9+
foreach (string modName in modNames)
1210
{
13-
Log.Error($"Mod {modName} not installed");
14-
return;
15-
}
11+
string? modId = await Context.Instance.GetModId(modName, ct);
1612

17-
List<ModLock> usedBy = [.. Context.Instance.ModlistLock.Where(v => v.Dependencies.Contains(modlock.Id))];
13+
if (modId is null)
14+
{
15+
Log.Error($"Mod {modName} not found");
16+
continue;
17+
}
1818

19-
if (usedBy.Count > 0)
20-
{
21-
Log.Error($"Mod {modlock.Name ?? modName} is used by the following mod(s):");
22-
foreach (ModLock item in usedBy)
19+
ModEntry? mod = Context.Instance.Modlist.Mods.FirstOrDefault(v => v.Id == modId);
20+
ModLock? modlock = Context.Instance.ModlistLock.FirstOrDefault(v => v.Id == modId);
21+
22+
if (modlock is null)
2323
{
24-
Log.None(item.Name ?? item.FileName ?? item.Id);
24+
Log.Error($"Mod {modName} not installed");
25+
continue;
2526
}
26-
}
2727

28-
if (mod is null)
29-
{
30-
Log.Error($"Mod {modName} was implicitly installed therefore cannot be uninstalled");
31-
return;
28+
toUninstall.Add((modId, mod, modlock));
3229
}
3330

34-
if (usedBy.Count > 0 && !Log.AskYesNo("Do you want to continue?", false))
31+
foreach ((string modId, ModEntry? mod, ModLock modlock) in toUninstall)
3532
{
36-
return;
37-
}
33+
string? modName = Context.Instance.GetModName(modId);
34+
35+
List<ModLock> usedBy = [
36+
.. Context.Instance.ModlistLock
37+
.Where(v => toUninstall.All(w => v.Id != w.ModId))
38+
.Where(v => v.Dependencies.Contains(modlock.Id))
39+
];
40+
41+
if (usedBy.Count > 0)
42+
{
43+
Log.Error($"Mod {modlock.Name ?? modName} is used by the following mod(s):");
44+
foreach (ModLock item in usedBy)
45+
{
46+
Log.None(item.Name ?? item.FileName ?? item.Id);
47+
}
48+
49+
if (mod is null) continue;
50+
}
51+
52+
if (mod is null)
53+
{
54+
Log.Error($"Mod {modName} was implicitly installed therefore cannot be uninstalled");
55+
continue;
56+
}
3857

39-
Context.Instance.Modlist.Mods.Remove(mod);
58+
if (usedBy.Count > 0 && !Log.AskYesNo("Do you want to continue?", false))
59+
{
60+
continue;
61+
}
62+
63+
Context.Instance.Modlist.Mods.Remove(mod);
64+
}
4065

41-
(List<ModDownloadInfo> updates, List<ModUninstallInfo> uninstalls) = await ModInstaller.CheckChanges(ct);
66+
Changes changes = await ModInstaller.CheckChanges(ct);
4267

43-
await ModInstaller.PerformChanges(updates, uninstalls, ct);
68+
await ModInstaller.PerformChanges(changes, ct);
4469
}
4570
}

Source/Actions/Update.cs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ public static class Update
66
{
77
public static async Task PerformUpdate(CancellationToken ct)
88
{
9-
(List<ModDownloadInfo> modUpdates, List<ModUninstallInfo> unsupported) = await CheckNewVersions(ct);
9+
Changes changes = await CheckNewVersions(ct);
1010

11-
await ModInstaller.PerformChanges(modUpdates, unsupported, ct);
11+
await ModInstaller.PerformChanges(changes, ct);
1212
}
1313

14-
public static async Task<(List<ModDownloadInfo> Updates, List<ModUninstallInfo> Unsupported)> CheckNewVersions(CancellationToken ct)
14+
public static async Task<Changes> CheckNewVersions(CancellationToken ct)
1515
{
16-
List<ModDownloadInfo> modUpdates = [];
16+
ImmutableArray<ModInstallInfo>.Builder modUpdates = ImmutableArray.CreateBuilder<ModInstallInfo>();
17+
ImmutableArray<ModUninstallInfo>.Builder unsupportedMods = ImmutableArray.CreateBuilder<ModUninstallInfo>();
1718

1819
Log.Section($"Checking for updates");
1920
ProgressBar progressBar = new();
@@ -33,15 +34,14 @@ void AddThese(IEnumerable<string> mods)
3334
AddThese(Context.Instance.Modlist.Mods.Select(v => v.Id));
3435

3536
ImmutableArray<string> checkThese = [.. checkTheseSet];
36-
List<ModUninstallInfo> unsupportedMods = [];
3737

3838
for (int i = 0; i < checkThese.Length; i++)
3939
{
4040
string modId = checkThese[i];
4141

4242
progressBar.Report(Context.Instance.GetModName(modId) ?? modId, i, checkThese.Length);
4343

44-
ModDownloadInfo? update;
44+
ModInstallInfo? update;
4545
try
4646
{
4747
update = await ModrinthUtils.GetModIfNeeded(modId, ct);
@@ -75,6 +75,10 @@ void AddThese(IEnumerable<string> mods)
7575

7676
progressBar.Dispose();
7777

78-
return (modUpdates, unsupportedMods);
78+
return new Changes()
79+
{
80+
Install = modUpdates.ToImmutable(),
81+
Uninstall = unsupportedMods.ToImmutable(),
82+
};
7983
}
8084
}

Source/Change/Changes.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Immutable;
2+
3+
namespace MMM;
4+
5+
public readonly struct Changes
6+
{
7+
public readonly required ImmutableArray<ModInstallInfo> Install { get; init; }
8+
public readonly required ImmutableArray<ModUninstallInfo> Uninstall { get; init; }
9+
10+
public bool IsEmpty => Install.IsEmpty && Uninstall.IsEmpty;
11+
}

Source/ModDownloadInfo.cs renamed to Source/Change/ModInstallInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace MMM;
22

3-
public class ModDownloadInfo
3+
public class ModInstallInfo
44
{
55
public required ModUpdateReason Reason { get; init; }
66
public required Modrinth.Models.Version Version { get; init; }

0 commit comments

Comments
 (0)