Skip to content

Commit

Permalink
always create containing folder
Browse files Browse the repository at this point in the history
  • Loading branch information
gantoine committed Mar 24, 2024
1 parent 06d4c43 commit a03dd48
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions Games/RomMInstallController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public override void Install(InstallActionArgs args)
HttpResponseMessage response = await RomM.GetAsync(info.DownloadUrl, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();

string installDir = dstPath;
string gamePath = Path.Combine(dstPath, info.FileName);
List<GameRom> roms = new List<GameRom>();
string installDir = Path.Combine(dstPath, Path.GetFileNameWithoutExtension(info.FileName));
string gamePath = Path.Combine(installDir, info.FileName);
if (info.IsMulti)
{
gamePath = Path.Combine(dstPath, info.FileName + ".zip");
// File name for multi-file archives is the folder name, so we append .zip
gamePath = Path.Combine(installDir, info.FileName + ".zip");
}

if (_romM.Playnite.ApplicationInfo.IsPortable)
Expand All @@ -62,6 +64,7 @@ public override void Install(InstallActionArgs args)
}

Logger.Debug($"Downloading {Game.Name} to {gamePath}.");
Directory.CreateDirectory(installDir);

// Stream the file directly to disk
using (var fileStream = new FileStream(gamePath, FileMode.Create, FileAccess.Write, FileShare.None))
Expand All @@ -70,29 +73,34 @@ public override void Install(InstallActionArgs args)
await httpStream.CopyToAsync(fileStream);
}

Logger.Debug($"Download of {Game.Name} complete!");
Logger.Debug($"Download of {Game.Name} complete.");

// ALways extract top-level file of multi-file archives
if (info.IsMulti || (info.Mapping.AutoExtract && IsFileCompressed(gamePath)))
{
// Create the install directory if it doesn't exist
installDir = Path.Combine(dstPath, Path.GetFileNameWithoutExtension(info.FileName));
Directory.CreateDirectory(installDir);

// Extract the archive to the install directory
ExtractArchive(gamePath, installDir);

// Delete the compressed file
File.Delete(gamePath);

// Extract nested archives if auto-extract is enabled
if (info.IsMulti && info.Mapping.AutoExtract)
{
ExtractNestedArchives(installDir);
}

// Delete the compressed file
File.Delete(gamePath);
// Add just the ROM files to the list
string[] actualRomFiles = Directory.GetFiles(installDir, "*", SearchOption.AllDirectories)
.Where(file => !file.EndsWith(".m3u", StringComparison.OrdinalIgnoreCase))
.ToArray();

// Update the game path to the extracted game
gamePath = Path.Combine(installDir, Directory.GetFiles(installDir, "*", SearchOption.AllDirectories).Where(file => !file.EndsWith(".m3u", StringComparison.OrdinalIgnoreCase)).ToArray()[0]);
foreach (var romFile in actualRomFiles) {
roms.Add(new GameRom(Game.Name, romFile));
}
} else {
// Add the single ROM file to the list
roms.Add(new GameRom(Game.Name, gamePath));
}

// Update the game's installation status
Expand All @@ -103,7 +111,7 @@ public override void Install(InstallActionArgs args)
InvokeOnInstalled(new GameInstalledEventArgs(new GameInstallationData()
{
InstallDirectory = installDir,
Roms = new List<GameRom>() { new GameRom(Game.Name, gamePath) },
Roms = roms,
}));
}
catch (Exception ex)
Expand Down

0 comments on commit a03dd48

Please sign in to comment.