Skip to content

Commit

Permalink
move pattern formatter to the common dumper code and use it in linux …
Browse files Browse the repository at this point in the history
…build

also add explicit invalid character filter for the resulting name that should fix #20
  • Loading branch information
13xforever committed Feb 20, 2022
1 parent 2d4884a commit 84d7e21
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 26 deletions.
9 changes: 7 additions & 2 deletions Ps3DiscDumper/Dumper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Management;
Expand All @@ -21,7 +22,7 @@ namespace Ps3DiscDumper
{
public class Dumper: IDisposable
{
public const string Version = "3.2.1";
public const string Version = "3.2.2";

private static readonly HashSet<char> InvalidChars = new(Path.GetInvalidFileNameChars());
private static readonly char[] MultilineSplit = {'\r', '\n'};
Expand Down Expand Up @@ -163,7 +164,11 @@ private void CheckParamSfo(ParamSfo sfo)

public void DetectDisc(string inDir = "", Func<Dumper, string> outputDirFormatter = null)
{
outputDirFormatter ??= d => $"[{d.ProductCode}] {d.Title}";
outputDirFormatter ??= d => PatternFormatter.Format($"%{Patterns.Title}% [%{Patterns.ProductCode}%]", new()
{
[Patterns.ProductCode] = d.ProductCode,
[Patterns.Title] = d.Title,
});
string discSfbPath = null;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Expand Down
34 changes: 34 additions & 0 deletions Ps3DiscDumper/Utils/PatternFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Specialized;
using System.IO;
using System.Linq;

namespace Ps3DiscDumper.Utils
{
public static class PatternFormatter
{
public static string Format(string pattern, NameValueCollection items)
{
if (string.IsNullOrEmpty(pattern))
return pattern;

foreach (var k in items.AllKeys)
pattern = pattern.Replace($"%{k}%", items[k]);
pattern = pattern.Replace("®", "")
.Replace("™", "")
.Replace("(TM)", "")
.Replace("(R)", "");
pattern = ReplaceInvalidChars(pattern);
if (string.IsNullOrEmpty(pattern))
pattern = ReplaceInvalidChars($"{DateTime.Now:s}");
return pattern;
}

private static string ReplaceInvalidChars(string dirName)
{
foreach (var invalidChar in Path.GetInvalidFileNameChars().Concat(Path.GetInvalidPathChars()).Distinct())
dirName = dirName.Replace(invalidChar, '_');
return dirName;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace UI.WinForms.Msil
namespace Ps3DiscDumper.Utils
{
internal static class Patterns
public static class Patterns
{
public const string ProductCode = "product_code";
public const string ProductCodeLetters = "product_code_letters";
Expand Down
1 change: 1 addition & 0 deletions UI.WinForms.Msil/Forms/SettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Specialized;
using System.IO;
using System.Windows.Forms;
using Ps3DiscDumper.Utils;

namespace UI.WinForms.Msil
{
Expand Down
21 changes: 0 additions & 21 deletions UI.WinForms.Msil/PatternFormatter.cs

This file was deleted.

3 changes: 2 additions & 1 deletion UI.WinForms.Msil/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Configuration;
using Ps3DiscDumper.Utils;

namespace UI.WinForms.Msil
{
Expand All @@ -10,7 +11,7 @@ internal sealed class Settings : ApplicationSettingsBase
[UserScopedSetting, DefaultSettingValue(@".\ird")]
public string IrdDir { get => (string)this[nameof(IrdDir)]; set => this[nameof(IrdDir)] = value; }

[UserScopedSetting, DefaultSettingValue("%" + Patterns.Title + "% [%" + Patterns.ProductCode + "%]")]
[UserScopedSetting, DefaultSettingValue($"%{Patterns.Title}% [%{Patterns.ProductCode}%]")]
public string DumpNameTemplate { get => (string)this[nameof(DumpNameTemplate)]; set => this[nameof(DumpNameTemplate)] = value; }

[UserScopedSetting, DefaultSettingValue("false")]
Expand Down

0 comments on commit 84d7e21

Please sign in to comment.