Skip to content

Commit 940a10c

Browse files
authored
Merge pull request #129 from matthew-pye/Playnite-10
Add option to install ROMs flat
2 parents 082135b + 0829008 commit 940a10c

9 files changed

Lines changed: 74 additions & 17 deletions

Downloads/DownloadQueueController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ private async Task DownloadAndInstall(DownloadQueueItem item, DownloadRequest re
177177
}
178178

179179
// Extract if needed (we treat extract as 0..100 in its own bar)
180-
if (req.HasMultipleFiles || (req.AutoExtract && IsFileCompressed(req.GamePath)))
180+
// Don't extract archives when install flat is enabled
181+
if (req.HasMultipleFiles || (req.AutoExtract && IsFileCompressed(req.GamePath) && !req.InstallFlat))
181182
{
182183
item.SetStatus(DownloadStatus.Extracting, "Extracting...");
183184
Logger.Info($"Extracting {req.GamePath}...");
@@ -323,7 +324,8 @@ private void TryCleanupPartialInstall(DownloadRequest req)
323324
SafeDeleteFileWithRetry(req.GamePath);
324325

325326
// delete folder (recursively) if it exists
326-
SafeDeleteDirectoryWithRetry(req.InstallDir);
327+
if(!req.InstallFlat)
328+
SafeDeleteDirectoryWithRetry(req.InstallDir);
327329
}
328330
catch (Exception ex)
329331
{

Downloads/DownloadRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class DownloadRequest
1616
public bool AutoExtract { get; set; } = true;
1717
public bool Use7z { get; set; } = false;
1818
public string PathTo7Z { get; set; } = "";
19-
19+
public bool InstallFlat { get; set; } = false;
2020

2121
/// Optional function used after extraction to build rom list for Playnite
2222
public Func<List<GameRom>> BuildRoms { get; set; }

Games/RomMGameInfo.Plugin.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public EmulatorMapping Mapping
2626

2727
public InstallController GetInstallController(Game game, RomM romm, GameInstallInfo GameData) => new RomMInstallController(game, romm, GameData);
2828

29-
public UninstallController GetUninstallController(Game game, RomM romm) => new RomMUninstallController(game, romm);
29+
public UninstallController GetUninstallController(Game game, RomM romm, EmulatorMapping mapping) => new RomMUninstallController(game, romm, mapping);
3030

3131
protected IEnumerable<string> GetDescriptionLines()
3232
{

Games/RomMImport.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,8 @@ private Game ImportGame(RomMRom ROM, Guid StatusID)
210210
return null;
211211
}
212212

213-
var gameInstallDir = RomMInstallPaths.InstallDir(rootInstallDir, folderName, fileName);
214-
var pathToGame = RomMInstallPaths.GamePath(rootInstallDir, folderName, fileName);
213+
var gameInstallDir = _mapping.InstallFlat ? rootInstallDir : RomMInstallPaths.InstallDir(rootInstallDir, folderName, fileName);
214+
var pathToGame = _mapping.InstallFlat ? $"{rootInstallDir}\\{fileName}" : RomMInstallPaths.GamePath(rootInstallDir, folderName, fileName);
215215

216216
var status = _plugin.Playnite.Database.CompletionStatuses.Get(StatusID);
217217
var completionStatusProperty = status != null ? new MetadataNameProperty(status.Name) : null;
@@ -246,6 +246,7 @@ private Game ImportGame(RomMRom ROM, Guid StatusID)
246246
}
247247
};
248248

249+
249250
// Import new game
250251
Game game = _plugin.Playnite.Database.ImportGame(metadata, _plugin);
251252

Games/RomMInstallController.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
using Playnite.SDK;
1+
using Newtonsoft.Json;
2+
using Playnite.SDK;
23
using Playnite.SDK.Models;
34
using Playnite.SDK.Plugins;
45
using RomM.Downloads;
56
using RomM.Models.RomM.Rom;
7+
using RomM.Settings;
68
using SharpCompress.Archives;
79
using System;
810
using System.Collections.Generic;
911
using System.IO;
1012
using System.Linq;
11-
using Newtonsoft.Json;
1213

1314
namespace RomM.Games
1415
{
@@ -47,6 +48,9 @@ public override void Install(InstallActionArgs args)
4748
// IsInstalled detection lines up.
4849
var installDir = RomMInstallPaths.InstallDir(dstPath, _gameData.FolderName, _gameData.FileName);
4950

51+
if (_gameData.Mapping.InstallFlat)
52+
installDir = dstPath;
53+
5054
// If RomM indicates multiple files, we download as an archive name (zip) into the install folder.
5155
// Otherwise we download the single ROM file.
5256
var downloadFilePath = _gameData.HasMultipleFiles
@@ -66,6 +70,7 @@ public override void Install(InstallActionArgs args)
6670

6771
HasMultipleFiles = _gameData.HasMultipleFiles,
6872
AutoExtract = _gameData.Mapping != null && _gameData.Mapping.AutoExtract,
73+
InstallFlat = _gameData.Mapping.InstallFlat,
6974

7075
// Called by queue AFTER download/extract is done
7176
BuildRoms = () =>

Games/RomMUninstallController.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Playnite.SDK.Models;
22
using Playnite.SDK.Plugins;
3+
using RomM.Settings;
34
using System.IO;
45
using System.Windows;
56

@@ -8,24 +9,38 @@ namespace RomM.Games
89
internal class RomMUninstallController : UninstallController
910
{
1011
private readonly IRomM _romM;
12+
private EmulatorMapping _mapping;
1113

12-
internal RomMUninstallController(Game game, IRomM romM) : base(game)
14+
internal RomMUninstallController(Game game, IRomM romM, EmulatorMapping mapping) : base(game)
1315
{
1416
Name = "Uninstall";
1517
_romM = romM;
18+
_mapping = mapping;
1619
}
1720

1821
public override void Uninstall(UninstallActionArgs args)
1922
{
20-
if (new DirectoryInfo(Game.InstallDirectory).Exists)
23+
if(_mapping.InstallFlat)
2124
{
22-
Directory.Delete(Game.InstallDirectory, true);
25+
foreach (var RomFile in Game.Roms)
26+
{
27+
if(File.Exists(RomFile.Path))
28+
File.Delete(RomFile.Path);
29+
}
2330
}
2431
else
2532
{
26-
_romM.Playnite.Dialogs.ShowMessage($"\"{Game.Name}\" folder could not be found. Marking as uninstalled.", "Game not found", MessageBoxButton.OK);
33+
if (new DirectoryInfo(Game.InstallDirectory).Exists)
34+
{
35+
Directory.Delete(Game.InstallDirectory, true);
36+
}
37+
else
38+
{
39+
_romM.Playnite.Dialogs.ShowMessage($"\"{Game.Name}\" folder could not be found. Marking as uninstalled.", "Game not found", MessageBoxButton.OK);
40+
}
2741
}
28-
Game.Roms.Clear();
42+
43+
Game.Roms.Clear();
2944
InvokeOnUninstalled(new GameUninstalledEventArgs());
3045
}
3146
}

RomM.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,28 @@ public override IEnumerable<UninstallController> GetUninstallActions(GetUninstal
490490
{
491491
if (args.Game.PluginId == Id)
492492
{
493-
yield return new RomMUninstallController(args.Game, this);
493+
EmulatorMapping mapping = null;
494+
495+
try
496+
{
497+
var splitID = args.Game.GameId.Split(':');
498+
string sidecarPath = $"{ROMDataPath}{splitID[1]}.json";
499+
if (File.Exists(sidecarPath))
500+
{
501+
var existingJson = File.ReadAllText(sidecarPath);
502+
var localROM = JsonConvert.DeserializeObject<RomMRomLocal>(existingJson);
503+
mapping = Settings.Mappings.FirstOrDefault(x => x.MappingId == localROM.MappingID);
504+
}
505+
}
506+
catch (Exception)
507+
{
508+
Logger.Error($"{args.Game.Name} GameID is malformed or json file is corrupted!");
509+
}
510+
511+
if (mapping == null)
512+
yield return null;
513+
514+
yield return new RomMUninstallController(args.Game, this, mapping);
494515
}
495516
}
496517
public override void OnGameInstalled(OnGameInstalledEventArgs args)

Settings/EmulatorMapping.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class EmulatorMapping : ObservableObject
4141
public int _romMPlatformId = -1;
4242
[JsonIgnore]
4343
private string _destinationPath = "";
44+
[JsonIgnore]
45+
private bool _installFlat = false;
4446

4547
public EmulatorMapping(List<RomMPlatform> romMPlatforms)
4648
{
@@ -232,10 +234,20 @@ public string DestinationPath
232234
{
233235
_destinationPath = value;
234236
OnPropertyChanged();
235-
}
236-
}
237+
}
238+
}
239+
240+
public bool InstallFlat
241+
{
242+
get => _installFlat;
243+
set
244+
{
245+
_installFlat = value;
246+
OnPropertyChanged();
247+
}
248+
}
237249

238-
[JsonIgnore]
250+
[JsonIgnore]
239251
public static IEnumerable<Emulator> AvailableEmulators => SettingsViewModel.Instance.PlayniteAPI.Database.Emulators?.OrderBy(x => x.Name) ?? Enumerable.Empty<Emulator>();
240252
[JsonIgnore]
241253
public IEnumerable<EmulatorProfile> AvailableProfiles

Settings/SettingsView.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
<StackPanel Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Orientation="Horizontal" Margin="0,2,0,8">
230230
<CheckBox Content="Enabled" Margin="0,0,16,0" IsChecked="{Binding Enabled, Mode=TwoWay}" />
231231
<CheckBox Content="Auto-extract" Margin="0,0,16,0" IsChecked="{Binding AutoExtract, Mode=TwoWay}" ToolTip="Extract ROMs stored on the server as Rar, Zip, Tar, 7z, GZip, or BZip2." />
232+
<CheckBox Content="Install Flat" Margin="0,0,16,0" IsChecked="{Binding InstallFlat, Mode=TwoWay}" ToolTip="Will download ROMs into the Path/ instead of Path/Gamename/" />
232233
<CheckBox Content="Prefer .m3u" IsChecked="{Binding UseM3U, Mode=TwoWay}" ToolTip="Use the .m3u file for multi-file ROMs when the emulator supports it." />
233234
</StackPanel>
234235

0 commit comments

Comments
 (0)