Skip to content

Commit

Permalink
Added: Create Mod Dialog & Duplicate Config Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Dec 9, 2021
1 parent 343671b commit 08aad61
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public CreateModViewModel(ModConfigService modConfigService)
/// Creates the mod.
/// </summary>
/// <param name="showNonUniqueWindow">Shows a message to tell the user the mod isn't unique.</param>
public async Task<PathTuple<ModConfig>> CreateMod(Action showNonUniqueWindow)
public async Task<PathTuple<ModConfig>?> CreateMod(Action showNonUniqueWindow)
{
if (!IsUnique(showNonUniqueWindow))
return null!;
return null;

var config = new ModConfig() { ModId = ModId };
var modDirectory = Path.Combine(IoC.Get<LoaderConfig>().ModConfigDirectory, IOEx.ForceValidFilePath(ModId));
Expand Down
34 changes: 34 additions & 0 deletions source/Reloaded.Mod.Launcher.Lib/Utility/ActionWrappers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,40 @@ public static void SleepOnConditionWithTimeout(Func<bool> condition, int timeout
return value;
}

/// <param name="getValue">Function that retrieves the value.</param>
/// <param name="timeout">The timeout in milliseconds.</param>
/// <param name="sleepTime">Amount of sleep per iteration/attempt.</param>
/// <param name="token">Token that allows for cancellation of the task.</param>
/// <exception cref="Exception">Timeout expired.</exception>
public static async Task<T?> TryGetValueAsync<T>(Func<T> getValue, int timeout, int sleepTime, CancellationToken token = default)
{
var watch = new Stopwatch();
watch.Start();
bool valueSet = false;
T? value = default;

while (watch.ElapsedMilliseconds < timeout)
{
if (token.IsCancellationRequested)
return value;

try
{
value = getValue();
valueSet = true;
break;
}
catch (Exception) { /* Ignored */ }

await Task.Delay(sleepTime, token);
}

if (valueSet == false)
throw new Exception($"Timeout limit {timeout} exceeded.");

return value;
}

/// <summary>
/// Attempts to obtain a value while either the timeout has not expired or the <paramref name="whileFunction"/> returns
/// true.
Expand Down
4 changes: 4 additions & 0 deletions source/Reloaded.Mod.Launcher/Assets/Languages/en-GB.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -338,4 +338,8 @@
<!-- Update 1.12.0: Extended Application Configuration -->
<sys:String x:Key="ConfigureModsRightClickAdvice">Right Click Mod for More Options</sys:String>

<!-- Update 1.12.0: Create Mod Dialog -->
<sys:String x:Key="CreateModDialogIdDesc1">Unique Mod Id</sys:String>
<sys:String x:Key="CreateModDialogIdDesc2">Please set a unique Mod Id in the format of "game.type.name". For example: `sonicheroes.skins.seasidehillmidnight`. Once the ID is set, it cannot be changed.</sys:String>

</ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,33 @@
xmlns:default="clr-namespace:Reloaded.WPF.Theme.Default;assembly=Reloaded.WPF.Theme.Default"
mc:Ignorable="d"
Title="{DynamicResource TitleCreateModDialog}"
Height="550"
Height="Auto"
Width="500"
SizeToContent="Height"
WindowStartupLocation="CenterOwner"
Style="{DynamicResource ReloadedWindow}">
<Grid>

</Grid>
<StackPanel DataContext="{Binding Path=RealViewModel, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}"
Margin="{DynamicResource PageMargin}">

<TextBlock Text="{DynamicResource CreateModDialogIdDesc1}"
Style="{DynamicResource DefaultTextBlock}"
Margin="{DynamicResource CommonItemVerticalMargin}"
HorizontalAlignment="Center" />

<TextBox Text="{Binding ModId}"
Style="{DynamicResource DefaultTextBox}"
Margin="{DynamicResource CommonItemVerticalMargin}" />

<Button Content="{DynamicResource MessageBoxButtonOK}"
Style="{DynamicResource DefaultButton}"
Margin="{DynamicResource CommonItemVerticalMargin}"
Click="Save_Click"/>

<TextBlock Text="{DynamicResource CreateModDialogIdDesc2}"
Style="{DynamicResource DefaultTextBlock}"
Margin="{DynamicResource CommonItemVerticalMargin}"
TextWrapping="Wrap"
HorizontalAlignment="Center" />

</StackPanel>
</default:ReloadedWindow>
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Windows;
using System.Threading.Tasks;
using System.Windows;
using Reloaded.Mod.Launcher.Lib;
using Reloaded.Mod.Launcher.Lib.Models.ViewModel.Dialog;
using Reloaded.Mod.Launcher.Lib.Static;
using Reloaded.Mod.Launcher.Lib.Utility;
using Reloaded.Mod.Loader.IO.Services;
using Reloaded.WPF.Theme.Default;
using MessageBox = Reloaded.Mod.Launcher.Pages.Dialogs.MessageBox;
Expand All @@ -20,13 +23,15 @@ public CreateModDialog(ModConfigService modConfigService)
RealViewModel = new CreateModViewModel(modConfigService);
}

public async void Save()
public async Task Save()
{
var mod = await RealViewModel.CreateMod(ShowNonUniqueWindow);
if (mod == null)
var createdMod = await RealViewModel.CreateMod(ShowNonUniqueWindow);
if (createdMod == null)
return;

var createModDialog = new EditModDialog(new EditModDialogViewModel(mod, IoC.Get<ApplicationConfigService>(), IoC.Get<ModConfigService>()));
var modConfigService = IoC.Get<ModConfigService>();
var mod = await ActionWrappers.TryGetValueAsync(() => modConfigService.ItemsById[createdMod.Config.ModId], 5000, 32);
var createModDialog = new EditModDialog(new EditModDialogViewModel(mod, IoC.Get<ApplicationConfigService>(), modConfigService));
createModDialog.Owner = Window.GetWindow(this);
createModDialog.ShowDialog();
this.Close();
Expand All @@ -38,4 +43,6 @@ private void ShowNonUniqueWindow()
messageBoxDialog.Owner = this;
messageBoxDialog.ShowDialog();
}

private async void Save_Click(object sender, RoutedEventArgs e) => await Save();
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@

<Button Style="{DynamicResource UnpaddedStretchedButton}"
Content="{DynamicResource TitleEditModDialog}"
Margin="{DynamicResource CommonItemMargin}"
Margin="0"
Command="{Binding EditModCommand}"/>

<!-- Mod Properties -->
Expand Down
6 changes: 3 additions & 3 deletions source/Reloaded.Mod.Loader.IO/Config/ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public class ModConfig : ObservableObject, IConfig<ModConfig>, IModConfig

public bool IsUniversalMod { get; set; } = false;

public string[] ModDependencies { get; set; }
public string[] OptionalDependencies { get; set; }
public string[] SupportedAppId { get; set; }
public string[] ModDependencies { get; set; } = Array.Empty<string>();
public string[] OptionalDependencies { get; set; } = Array.Empty<string>();
public string[] SupportedAppId { get; set; } = Array.Empty<string>();

/*
---------------
Expand Down
17 changes: 15 additions & 2 deletions source/Reloaded.Mod.Loader.IO/Services/ConfigServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,23 @@ private void CreateFileHandler(string fullPath)

private void AddItem(PathTuple<TConfigType> itemTuple)
{
// Check for existing item.
bool alreadyHasItem = ItemsByPath.TryGetValue(itemTuple.Path, out var existing);

ItemsByPath[itemTuple.Path] = itemTuple;
ItemsByFolder[Path.GetDirectoryName(itemTuple.Path)] = itemTuple;
Items.Add(itemTuple);
OnAddItem?.Invoke(itemTuple);
if (alreadyHasItem)
{
// Sometimes you might get directory, then file notification, so we might get a duplicate.
// We hackily filter out this duplicate here.
var index = Items.IndexOf(existing);
Items[index] = itemTuple;
}
else
{
Items.Add(itemTuple);
OnAddItem?.Invoke(itemTuple);
}
}

private void RemoveItem(PathTuple<TConfigType> itemTuple)
Expand Down

0 comments on commit 08aad61

Please sign in to comment.