From db0ae2493fea71300e485e89bc53d684f972f014 Mon Sep 17 00:00:00 2001 From: Brandon Date: Thu, 22 Apr 2021 19:19:57 -0500 Subject: [PATCH] Mame Tools v1.1.1 --- Extensions/MameTools/Localization/en_US.xaml | 5 +- Extensions/MameTools/MameTools.psm1 | 121 +++++++++++++++++-- 2 files changed, 114 insertions(+), 12 deletions(-) diff --git a/Extensions/MameTools/Localization/en_US.xaml b/Extensions/MameTools/Localization/en_US.xaml index a113fe29dd..7ce085cb61 100644 --- a/Extensions/MameTools/Localization/en_US.xaml +++ b/Extensions/MameTools/Localization/en_US.xaml @@ -1,9 +1,12 @@  Rename selected MAME games Rename selected MAME games (Without region in game name) - Select the MAME executable. + Import snapshots of selected game(s) as Background images from MAME into Playnite + Import snapshots of selected game(s) as Cover images from MAME into PlayniteSelect the MAME executable. MAME executable path saved. MAME executable not found in {0}. Please run the extension again to configure it to the correct location. Changed the name of {0} game(s). + Imported snapshots of {0} game(s) to Playnite. +{0} gamee(s) have a missing snapshot or are not MAME games. \ No newline at end of file diff --git a/Extensions/MameTools/MameTools.psm1 b/Extensions/MameTools/MameTools.psm1 index 0b9c97a5de..8b8ede0f13 100644 --- a/Extensions/MameTools/MameTools.psm1 +++ b/Extensions/MameTools/MameTools.psm1 @@ -7,22 +7,28 @@ function GetMainMenuItems $menuItem1 = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $menuItem1.Description = [Playnite.SDK.ResourceProvider]::GetString("LOCMenuItemRenameRegionInNameDescription") $menuItem1.FunctionName = "Rename-WithInfo" - $menuItem1.MenuSection = "@Mame Renamer" + $menuItem1.MenuSection = "@Mame Tools" $menuItem2 = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem $menuItem2.Description = [Playnite.SDK.ResourceProvider]::GetString("LOCMenuItemRenameNoRegionInNameDescription") $menuItem2.FunctionName = "Rename-NoInfo" - $menuItem2.MenuSection = "@Mame Renamer" + $menuItem2.MenuSection = "@Mame Tools" - return $menuItem1, $menuItem2 + $menuItem3 = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem + $menuItem3.Description = [Playnite.SDK.ResourceProvider]::GetString("LOCMenuItemSetSnapAsBackgroundDescription") + $menuItem3.FunctionName = "Set-SnapshotAsCoverImage" + $menuItem3.MenuSection = "@Mame Tools" + + $menuItem4 = New-Object Playnite.SDK.Plugins.ScriptMainMenuItem + $menuItem4.Description = [Playnite.SDK.ResourceProvider]::GetString("LOCMenuItemSetSnapAsCoverDescription") + $menuItem4.FunctionName = "Set-SnapshotAsBackgroundImage" + $menuItem4.MenuSection = "@Mame Tools" + + return $menuItem1, $menuItem2, $menuItem3, $menuItem4 } -function Rename-SelectedMameGames +function Get-MamePath { - param ( - $keepRegionInfo - ) - $mameSavedPath = Join-Path -Path $CurrentExtensionDataPath -ChildPath 'mameSavedPath.txt' if (Test-Path $mameSavedPath) { @@ -31,10 +37,10 @@ function Rename-SelectedMameGames else { $PlayniteApi.Dialogs.ShowMessage([Playnite.SDK.ResourceProvider]::GetString("LOCMameExecutableSelectMessage"), "MAME renamer") - $mamePath = $PlayniteApi.Dialogs.SelectFile("MAME executable|mame64.exe") + $mamePath = $PlayniteApi.Dialogs.SelectFile("MAME executable|mame*.exe") if (!$mamePath) { - exit + return } [System.IO.File]::WriteAllLines($mameSavedPath, $mamePath) $__logger.Info("MAME renamer - Saved `"$mamePath`" executable path.") @@ -46,7 +52,21 @@ function Rename-SelectedMameGames [System.IO.File]::Delete($mameSavedPath) $__logger.Info("MAME renamer - Executable not found in `"$mamePath`" and saved path was deleted.") $PlayniteApi.Dialogs.ShowMessage(([Playnite.SDK.ResourceProvider]::GetString("LOCMameExecutableNotFoundMessage") -f $mamePath), "MAME renamer") - exit + return + } + return $mamePath +} + +function Rename-SelectedMameGames +{ + param ( + $keepRegionInfo + ) + + $mamePath = Get-MamePath + if ($null -eq $mamePath) + { + return } $gameDatabase = $PlayniteApi.MainView.SelectedGames | Where-Object {$_.GameImagePath} @@ -94,4 +114,83 @@ function Rename-NoInfo ) Rename-SelectedMameGames $false +} + +function Set-MameSnapshotToPlayniteMedia +{ + param ( + $targetMedia + ) + + $mamePath = Get-MamePath + if ($null -eq $mamePath) + { + return + } + + $mameDirectory = [System.IO.Path]::GetDirectoryName($mamePath) + $gameDatabase = $PlayniteApi.MainView.SelectedGames | Where-Object {$_.GameImagePath} + $snapshotsImportCount = 0 + $snapshotsMissingCount = 0 + foreach ($game in $gameDatabase) { + $romFileName = [System.IO.Path]::GetFileNameWithoutExtension($game.GameImagePath) + $arguments = @("-listxml", $fileName) + [xml]$output = & $mamePath $arguments + if ($output.mame.machine[0].cloneof) + { + $romFileName = $output.mame.machine[0].cloneof + } + $sourceScreenshotPath = [System.IO.Path]::Combine($mameDirectory, "Snap", $romFileName + ".png") + if (!(Test-Path $sourceScreenshotPath)) + { + $snapshotsMissingCount++ + $__logger.Info("$($game.Name) is not a MAME game or is missing a screenshot.") + continue + } + if ($targetMedia -eq "Background Image") + { + if ($game.BackgroundImage) + { + $PlayniteApi.Database.RemoveFile($game.BackgroundImage) + $PlayniteApi.Database.Games.Update($game) + } + $copiedImage = $PlayniteApi.Database.AddFile($sourceScreenshotPath, $game.Id) + $game.BackgroundImage = $copiedImage + $PlayniteApi.Database.Games.Update($game) + $snapshotsImportCount++ + } + elseif ($targetMedia -eq "Cover Image") + { + if ($game.CoverImage) + { + $PlayniteApi.Database.RemoveFile($game.CoverImage) + $PlayniteApi.Database.Games.Update($game) + } + $copiedImage = $PlayniteApi.Database.AddFile($sourceScreenshotPath, $game.Id) + $game.CoverImage = $copiedImage + $PlayniteApi.Database.Games.Update($game) + $snapshotsImportCount++ + } + + } + + $PlayniteApi.Dialogs.ShowMessage(([Playnite.SDK.ResourceProvider]::GetString("LOCSnapshotsImportResultsMessage") -f $snapshotsImportCount, $snapshotsMissingCount), "MAME renamer") +} + +function Set-SnapshotAsCoverImage +{ + param( + $scriptMainMenuItemActionArgs + ) + + Set-MameSnapshotToPlayniteMedia "Background Image" +} + +function Set-SnapshotAsBackgroundImage +{ + param( + $scriptMainMenuItemActionArgs + ) + + Set-MameSnapshotToPlayniteMedia "Cover Image" } \ No newline at end of file