Skip to content

Commit

Permalink
Mame Tools v1.1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
darklinkpower committed Apr 23, 2021
1 parent a8d04ba commit db0ae24
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 12 deletions.
5 changes: 4 additions & 1 deletion Extensions/MameTools/Localization/en_US.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">
<sys:String x:Key="LOCMenuItemRenameRegionInNameDescription">Rename selected MAME games</sys:String>
<sys:String x:Key="LOCMenuItemRenameNoRegionInNameDescription">Rename selected MAME games (Without region in game name)</sys:String>
<sys:String x:Key="LOCMameExecutableSelectMessage">Select the MAME executable.</sys:String>
<sys:String x:Key="LOCMenuItemSetSnapAsBackgroundDescription">Import snapshots of selected game(s) as Background images from MAME into Playnite</sys:String>
<sys:String x:Key="LOCMenuItemSetSnapAsCoverDescription">Import snapshots of selected game(s) as Cover images from MAME into Playnite</sys:String><sys:String x:Key="LOCMameExecutableSelectMessage">Select the MAME executable.</sys:String>
<sys:String x:Key="LOCMameExecutablePathSavedMessage">MAME executable path saved.</sys:String>
<sys:String x:Key="LOCMameExecutableNotFoundMessage" xml:space="preserve">MAME executable not found in {0}.
Please run the extension again to configure it to the correct location.</sys:String>
<sys:String x:Key="LOCResultsMessage">Changed the name of {0} game(s).</sys:String>
<sys:String x:Key="LOCSnapshotsImportResultsMessage" xml:space="preserve">Imported snapshots of {0} game(s) to Playnite.
{0} gamee(s) have a missing snapshot or are not MAME games.</sys:String>
</ResourceDictionary>
121 changes: 110 additions & 11 deletions Extensions/MameTools/MameTools.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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.")
Expand All @@ -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}
Expand Down Expand Up @@ -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"
}

0 comments on commit db0ae24

Please sign in to comment.