Skip to content

Commit 4dde6ad

Browse files
2 parents 21e7df7 + 4a60fd7 commit 4dde6ad

File tree

7 files changed

+125
-1
lines changed

7 files changed

+125
-1
lines changed

MSURandomizer/Services/MsuWindowService.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public class MsuWindowService(ILogger<MsuWindowService> logger,
2323
IMsuSelectorService msuSelectorService,
2424
IMsuLookupService msuLookupService,
2525
IMsuMonitorService msuMonitorService,
26-
IRomLauncherService romLauncherService) : ControlService
26+
IRomLauncherService romLauncherService,
27+
IRomCopyService romCopyService) : ControlService
2728
{
2829
public MsuWindowViewModel Model { get; set; } = new();
2930

@@ -156,6 +157,21 @@ public bool GenerateMsu(out string error, out bool openContinuousWindow, out Msu
156157
openContinuousWindow = false;
157158
return false;
158159
}
160+
161+
if (!string.IsNullOrEmpty(userOptions.MsuUserOptions.OutputRomPath) &&
162+
!string.IsNullOrEmpty(userOptions.MsuUserOptions.CopyRomDirectory))
163+
{
164+
if (romCopyService.CopyRom(userOptions.MsuUserOptions.OutputRomPath, out var outPath, out var copyError))
165+
{
166+
userOptions.MsuUserOptions.OutputRomPath = outPath;
167+
}
168+
else
169+
{
170+
error = copyError ?? "Error copying rom to copy rom directory";
171+
openContinuousWindow = false;
172+
return false;
173+
}
174+
}
159175

160176
var options = userOptions.MsuUserOptions;
161177
if (options.RandomizationStyle == MsuRandomizationStyle.Single)

MSURandomizer/ViewModels/SettingsWindowViewModel.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ public class SettingsWindowViewModel : ViewModelBase
2828

2929
[Reactive] public List<MsuTypePath> MsuTypeNamePathsList { get; set; } = new();
3030

31+
[Reactive] public string? CopyRomDirectory { get; set; }
32+
3133
[Reactive]
3234
[ReactiveLinkedProperties(nameof(LaunchArgumentsEnabled))]
3335
public string? LaunchApplication { get; set; }

MSURandomizer/Views/SettingsWindow.axaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@
8787
</Grid>
8888
</controls:LabeledControl>
8989

90+
<controls:LabeledControl Text="Copy Rom Directory" Hint="Folder for roms to be copied to for MSUs if desired. If not set, the rom locations will be left alone." DisplayHint="True" Margin="3">
91+
<controls:FileControl
92+
FileInputType="Folder"
93+
DefaultPath="{Binding DefaultDirectory}"
94+
FilePath="{Binding CopyRomDirectory, Mode=TwoWay}"
95+
></controls:FileControl>
96+
</controls:LabeledControl>
97+
9098
<controls:LabeledControl Text="Launch Application" Hint="Application to launch a rom in" DisplayHint="True" Margin="3">
9199
<controls:FileControl
92100
FileInputType="OpenFile"

MSURandomizerLibrary/Configs/MsuUserOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public class MsuUserOptions
133133
/// </summary>
134134
public bool LaunchRom { get; set; }
135135

136+
/// <summary>
137+
/// Directory roms should be copied to before randomizing
138+
/// </summary>
139+
public string? CopyRomDirectory { get; set; }
140+
136141
/// <summary>
137142
/// Specific directories to load for specific MSU types
138143
/// </summary>

MSURandomizerLibrary/MsuRandomizerServiceExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public static IServiceCollection AddMsuRandomizerServices(this IServiceCollectio
3232
services.AddSingleton<IMsuMonitorService, MsuMonitorService>();
3333
services.AddSingleton<IRomLauncherService, RomLauncherService>();
3434
services.AddSingleton<IMsuHardwareService, MsuHardwareService>();
35+
services.AddSingleton<IRomCopyService, RomCopyService>();
3536

3637
services.AddSnesConnectorServices();
3738

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace MSURandomizerLibrary.Services;
2+
3+
/// <summary>
4+
/// Service for copying a rom to a specified directory
5+
/// </summary>
6+
public interface IRomCopyService
7+
{
8+
/// <summary>
9+
/// Copies a rom to the user specified directory
10+
/// </summary>
11+
/// <param name="romPath">The path to the rom</param>
12+
/// <param name="outPath">The path to the copied rom</param>
13+
/// <param name="error">The error from copying the folder</param>
14+
/// <returns>True if successful, false otherwise</returns>
15+
public bool CopyRom(string romPath, out string? outPath, out string? error);
16+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using Microsoft.Extensions.Logging;
2+
3+
namespace MSURandomizerLibrary.Services;
4+
5+
internal class RomCopyService(IMsuUserOptionsService options, ILogger<RomCopyService> logger) : IRomCopyService
6+
{
7+
public bool CopyRom(string romPath, out string? outPath, out string? error)
8+
{
9+
outPath = null;
10+
error = null;
11+
12+
if (!File.Exists(romPath))
13+
{
14+
error = $"{romPath} does not exist";
15+
return false;
16+
}
17+
18+
if (string.IsNullOrEmpty(options.MsuUserOptions.CopyRomDirectory))
19+
{
20+
error = $"No Copy Rom Directory specified";
21+
return false;
22+
}
23+
24+
if (!Directory.Exists(options.MsuUserOptions.CopyRomDirectory))
25+
{
26+
try
27+
{
28+
Directory.CreateDirectory(options.MsuUserOptions.CopyRomDirectory);
29+
}
30+
catch (Exception e)
31+
{
32+
error = $"Unable to create directory {options.MsuUserOptions.CopyRomDirectory}";
33+
logger.LogError(e, "Unable to create directory {Path}", options.MsuUserOptions.CopyRomDirectory);
34+
return false;
35+
}
36+
}
37+
38+
var fileInfo = new FileInfo(romPath);
39+
40+
var outputFolder = Path.Combine(options.MsuUserOptions.CopyRomDirectory,
41+
fileInfo.Name.Replace(fileInfo.Extension, ""));
42+
43+
if (!Directory.Exists(outputFolder))
44+
{
45+
try
46+
{
47+
Directory.CreateDirectory(outputFolder);
48+
}
49+
catch (Exception e)
50+
{
51+
error = $"Unable to create directory {outputFolder}";
52+
logger.LogError(e, "Unable to create directory {Path}", outputFolder);
53+
return false;
54+
}
55+
}
56+
57+
outPath = Path.Combine(outputFolder, fileInfo.Name);
58+
59+
if (File.Exists(outPath))
60+
{
61+
return true;
62+
}
63+
64+
try
65+
{
66+
File.Move(romPath, outPath);
67+
return true;
68+
}
69+
catch (Exception e)
70+
{
71+
error = $"Unable to move file to {outPath}";
72+
logger.LogError(e, "Unable to move file to {Path}", outPath);
73+
return false;
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)