Skip to content

Commit f186fe6

Browse files
committed
fixes to mods bulk edit
1 parent abfa685 commit f186fe6

2 files changed

Lines changed: 83 additions & 19 deletions

File tree

‎Torch.Server/Views/ModListControl.xaml.cs‎

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -266,27 +266,26 @@ private void BulkButton_OnClick(object sender, RoutedEventArgs e)
266266
//blocking
267267
editor.Edit<string>(idList, "Mods");
268268

269-
modList.RemoveAll(m =>
270-
{
271-
var mod = m.ToString();
272-
return idList.Any(id =>
273-
id.IndexOf('-') > 0
274-
? mod.Equals(id, StringComparison.Ordinal)
275-
: m.PublishedFileId.Equals(ulong.Parse(id)));
276-
});
269+
modList.Clear();
277270
modList.AddRange(idList.Select(id =>
278271
{
279-
var info = new ModItemInfo(ModItemUtils.Create(id));
272+
if (!ModItemUtils.TryParse(id, out var item))
273+
return null;
274+
275+
var info = new ModItemInfo(item);
280276
tasks.Add(Task.Run(info.UpdateModInfoAsync));
281277
return info;
282-
}));
278+
}).Where(b => b != null));
279+
283280
_instanceManager.DedicatedConfig.Mods.Clear();
284281
foreach (var mod in modList)
285282
_instanceManager.DedicatedConfig.Mods.Add(mod);
286283

287-
if (tasks.Any())
288-
Task.WhenAll(tasks.ToArray())
289-
.ContinueWith(_ => Dispatcher.Invoke(() => _instanceManager.DedicatedConfig.Save()));
284+
Task.Run(async () =>
285+
{
286+
await Task.WhenAll(tasks);
287+
_instanceManager.DedicatedConfig.Save();
288+
});
290289
}
291290

292291
private void UgcServiceTypeBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)

‎Torch/Utils/ModItemUtils.cs‎

Lines changed: 71 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,89 @@
1-
using System.Collections.Generic;
2-
using System.Threading.Tasks;
1+
using System;
32
using Sandbox.Engine.Networking;
4-
using Torch.API;
53
using VRage.Game;
64

75
namespace Torch.Utils
86
{
97
public static class ModItemUtils
108
{
9+
/// <summary>
10+
/// Creates <see cref="MyObjectBuilder_Checkpoint.ModItem"/> from mod id and optionally service name.
11+
/// </summary>
12+
/// <param name="modId">Mod id.</param>
13+
/// <param name="serviceType">Service name, will be default if null passed.</param>
14+
/// <returns><see cref="MyObjectBuilder_Checkpoint.ModItem"/></returns>
1115
public static MyObjectBuilder_Checkpoint.ModItem Create(ulong modId, string serviceType = null)
1216
{
1317
return new MyObjectBuilder_Checkpoint.ModItem(modId, serviceType ?? GetDefaultServiceName());
1418
}
1519

20+
/// <summary>
21+
/// Creates <see cref="MyObjectBuilder_Checkpoint.ModItem"/> from <see cref="string"/> containing mod id and the service name.
22+
/// </summary>
23+
/// <param name="str">String containing mod id and the service name.</param>
24+
/// <returns><see cref="MyObjectBuilder_Checkpoint.ModItem"/></returns>
25+
/// <exception cref="FormatException" />
26+
/// <exception cref="OverflowException" />
27+
/// <exception cref="ArgumentOutOfRangeException" />
1628
public static MyObjectBuilder_Checkpoint.ModItem Create(string str)
1729
{
1830
var arr = str.Split('-');
19-
// backward compat
20-
return new MyObjectBuilder_Checkpoint.ModItem(ulong.Parse(arr[0]), arr.Length > 1 ? arr[1] : GetDefaultServiceName());
31+
return new MyObjectBuilder_Checkpoint.ModItem(ulong.Parse(arr[0]), arr[1]);
32+
}
33+
34+
/// <summary>
35+
/// Tries to parse mod id <see cref="string"/> with or without the service name.
36+
/// </summary>
37+
/// <param name="str"><see cref="string"/> containing mod id and optionally the service name.</param>
38+
/// <param name="item">Parsed <see cref="MyObjectBuilder_Checkpoint.ModItem"/> or default if parsing was unsuccessful.</param>
39+
/// <returns>Parsing operation was successful or not.</returns>
40+
public static bool TryParse(string str, out MyObjectBuilder_Checkpoint.ModItem item)
41+
{
42+
item = default;
43+
44+
var arr = str.Split('-');
45+
46+
if (arr.Length == 0 || arr.Length > 2)
47+
return false;
48+
49+
if (!ulong.TryParse(arr[0], out var id))
50+
return false;
51+
52+
if (arr.Length == 1 || !TryParseServiceName(arr[1], out var serviceName))
53+
serviceName = GetDefaultServiceName();
54+
55+
item = new MyObjectBuilder_Checkpoint.ModItem(id, serviceName);
56+
return true;
57+
}
58+
59+
/// <summary>
60+
/// Tries to format service name to standardized and recognizable by game.
61+
/// </summary>
62+
/// <param name="str">Raw service name.</param>
63+
/// <param name="serviceName">Parsed service name or null if parsing was unsuccessful.</param>
64+
/// <returns>Parsing operation was successful or not.</returns>
65+
public static bool TryParseServiceName(string str, out string serviceName)
66+
{
67+
if (str.Equals("steam", StringComparison.OrdinalIgnoreCase))
68+
{
69+
serviceName = "Steam";
70+
return true;
71+
}
72+
if (str.Equals("mod.io", StringComparison.OrdinalIgnoreCase) ||
73+
str.Equals("eos", StringComparison.OrdinalIgnoreCase))
74+
{
75+
serviceName = "mod.io";
76+
return true;
77+
}
78+
79+
serviceName = null;
80+
return false;
2181
}
2282

23-
//because KEEEN!
83+
/// <summary>
84+
/// Default service name according to current configuration.
85+
/// </summary>
86+
/// <returns>UGC Service name.</returns>
2487
public static string GetDefaultServiceName()
2588
{
2689
try
@@ -29,7 +92,9 @@ public static string GetDefaultServiceName()
2992
}
3093
catch
3194
{
95+
#pragma warning disable CS0618
3296
return TorchBase.Instance.Config.UgcServiceType.ToString();
97+
#pragma warning restore CS0618
3398
}
3499
}
35100
}

0 commit comments

Comments
 (0)