Skip to content

Commit

Permalink
Merge pull request #9 from azuravian/Add-ignore-list
Browse files Browse the repository at this point in the history
Adds ignore list capability for naming games based on folder names
  • Loading branch information
azuravian authored Dec 15, 2024
2 parents 348d0cd + 7adea40 commit cef94b0
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 82 deletions.
110 changes: 68 additions & 42 deletions FindInstallers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -46,13 +47,13 @@ public string GetDeepestDirectory(string path)
return new DirectoryInfo(path).Name;
}

public Tuple<string, string, List<GameAction>> GetActions(Game game)
public Tuple<string, string, List<GameAction>> GetActions(Game game)
{
string gameImagePath = null;
string gameInstallArgs = null;
List<GameAction> gameActions = new List<GameAction>();
if (game.GameActions != null)
{
{
gameActions = game.GameActions.ToList();
foreach (GameAction g in gameActions)
{
Expand Down Expand Up @@ -105,11 +106,36 @@ public Tuple<string, string, List<GameRom>> GetRoms(Game game)
return Tuple.Create(gameImagePath, gameInstallArgs, gameRoms);
}

public List<Game> AddGame(List<Game> gamesAdded, string dir, bool useActions, string source, string platform)
public List<Game> AddGame(List<Game> gamesAdded, string dir, bool useActions, string source, string platform, List<MergedItem> ignorelist)
{
string gamename = Path.GetFileName(dir);
if (ignorelist != null)
{
foreach (MergedItem item in ignorelist)
{
var type = item.Source;
var value = item.Value;

if (type == "String")
{
if (gamename.Contains(value))
{
gamename = gamename.Replace(value, "");
}
}
else if (type == "Regex")
{
if (Regex.IsMatch(gamename, value))
{
gamename = Regex.Replace(gamename, value, "");
}
}
gamename = Regex.Replace(gamename, @"\s+", " ").Trim();
}
}
Game newGame = new Game
{
Name = Path.GetFileName(dir),
Name = gamename,
Added = DateTime.Now,
PluginId = Guid.Parse("2d01017d-024e-444d-80d3-f62f5be3fca5"),
SourceId = API.Instance.Database.Sources.FirstOrDefault(a => a.Name == source)?.Id ?? Guid.Empty,
Expand All @@ -134,7 +160,7 @@ public List<Game> AddGame(List<Game> gamesAdded, string dir, bool useActions, st
gameInstaller = file;
}
}
if ( gameInstaller == "" )
if (gameInstaller == "")
{
return gamesAdded;
}
Expand Down Expand Up @@ -169,7 +195,7 @@ public List<Game> AddGame(List<Game> gamesAdded, string dir, bool useActions, st
return gamesAdded;
}

public void FindInstallers(List<string> installPaths, bool useActions, int lpercent, string source, string platform)
public void FindInstallers(List<string> installPaths, bool useActions, int lpercent, string source, string platform, List<MergedItem> ignorelist)
{
IEnumerable<Game> games = API.Instance.Database.Games;
List<string> gameInstallDirs = new List<string>();
Expand Down Expand Up @@ -219,7 +245,7 @@ public void FindInstallers(List<string> installPaths, bool useActions, int lperc
{
string doubled = Regex.Replace(p, @"\\", @"\\");
regPaths.Add(doubled);
}
}
string pattern = @"(" + String.Join("|", regPaths.ToArray()) + @")([^\\]*)\\*";
pattern.Replace(@"\", @"\\");
RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled;
Expand All @@ -243,51 +269,51 @@ public void FindInstallers(List<string> installPaths, bool useActions, int lperc
}

Levenshtein myLevenshtein = new Levenshtein();
List<string> posmatches = new List<string>();
gameInstallDirs.Sort();
foreach (string gameInstallDir in gameInstallDirs)
{
if (gameInstallDir == null)
{
continue;
}
string gameInstallDirName = GetDeepestDirectory(gameInstallDir);
int ldistance = myLevenshtein.Distance(dirName, gameInstallDirName);
float percent = 1 - (Convert.ToSingle(ldistance) / Convert.ToSingle(Math.Max(gameInstallDir.Length, dir.Length)));
percent = percent * 100;
if (percent >= lpercent)
List<string> posmatches = new List<string>();
gameInstallDirs.Sort();
foreach (string gameInstallDir in gameInstallDirs)
{
posmatches.Add(gameInstallDir);
}
if (gameInstallDir == null)
{
continue;
}
if (posmatches.Count() > 0)
string gameInstallDirName = GetDeepestDirectory(gameInstallDir);
int ldistance = myLevenshtein.Distance(dirName, gameInstallDirName);
float percent = 1 - (Convert.ToSingle(ldistance) / Convert.ToSingle(Math.Max(gameInstallDir.Length, dir.Length)));
percent = percent * 100;
if (percent >= lpercent)
{
posmatches.Add(gameInstallDir);
}
}
if (posmatches.Count() > 0)
{
if (posmatches.Count() == 1)
{
if (posmatches.Count() == 1)
{
continue;
}
else
{
posmatches.Sort();
ObservableCollection<string> lmatches = new ObservableCollection<string>(
posmatches.Select(match => match)
);

Application.Current.Dispatcher.Invoke(() =>
else
{
SelectionDialog dialog = new SelectionDialog(lmatches);
dialog.SelectText.Text = $"Below are possible matches for {dirName}. Select a match from the list or choose 'None are correct'.";
dialog.ShowDialog();
if (dialog.IsCancelled)
posmatches.Sort();
ObservableCollection<string> lmatches = new ObservableCollection<string>(
posmatches.Select(match => match)
);

Application.Current.Dispatcher.Invoke(() =>
{
SelectionDialog dialog = new SelectionDialog(lmatches);
dialog.SelectText.Text = $"Below are possible matches for {dirName}. Select a match from the list or choose 'None are correct'.";
dialog.ShowDialog();
if (dialog.IsCancelled)
{
gamesAdded = AddGame(gamesAdded, dir, useActions, source, platform);
}
});
}
gamesAdded = AddGame(gamesAdded, dir, useActions, source, platform, ignorelist);
}
});
}
}
else
{
gamesAdded = AddGame(gamesAdded, dir, useActions, source, platform);
gamesAdded = AddGame(gamesAdded, dir, useActions, source, platform, ignorelist);
}
}

Expand Down
4 changes: 0 additions & 4 deletions Levenshtein.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LocalLibrary
{
Expand Down
31 changes: 24 additions & 7 deletions LocalLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,30 @@ public override void OnLibraryUpdated(OnLibraryUpdatedEventArgs args)
var source = Settings.Settings.SelectedSource;
PluginIdUpdate(source);
}
//if (Settings.Settings.UsePaths)
//{
// Finder addGames = new Finder();
// var installPaths = Settings.Settings.InstallPaths;
// var ignorelist = Settings.Settings.RegexList.Select(item => new MergedItem { Value = item, Source = "Regex" })
// .Concat(Settings.Settings.StringList.Select(item => new MergedItem { Value = item, Source = "String" }))
// .ToList();
// addGames.FindInstallers(installPaths.ToList(), Settings.Settings.UseActions, Settings.Settings.Levenshtein, Settings.Settings.SelectedSource, Settings.Settings.SelectedPlatform, ignorelist);
//}

}

public override IEnumerable<GameMetadata> GetGames(LibraryGetGamesArgs args)
{
if (Settings.Settings.UsePaths)
{
Finder addGames = new Finder();
var installPaths = Settings.Settings.InstallPaths;
addGames.FindInstallers(installPaths.ToList(), Settings.Settings.UseActions, Settings.Settings.Levenshtein, Settings.Settings.SelectedSource, Settings.Settings.SelectedPlatform);
var ignorelist = Settings.Settings.RegexList.Select(item => new MergedItem { Value = item, Source = "Regex" })
.Concat(Settings.Settings.StringList.Select(item => new MergedItem { Value = item, Source = "String" }))
.ToList();
addGames.FindInstallers(installPaths.ToList(), Settings.Settings.UseActions, Settings.Settings.Levenshtein, Settings.Settings.SelectedSource, Settings.Settings.SelectedPlatform, ignorelist);
}

return new List<GameMetadata>();
}

public static void PluginIdUpdate(string source)
Expand Down Expand Up @@ -263,7 +280,7 @@ public void GameInstaller(Game game, LocalInstallController install)
gameImagePath = actionsTuple.Item1;
gameInstallArgs = actionsTuple.Item2;
gameActions = actionsTuple.Item3;

GameAction found = gameActions.FirstOrDefault(a => a.Path == gameImagePath);
if (found != null)
{
Expand Down Expand Up @@ -518,14 +535,14 @@ public bool Install_Extras(List<GameRom> extras, Game selectedGame, LocalInstall
string command = null;
string extraInstallArgs = null;
var extraPath = extra.Path;

if (Path.GetFileName(extraPath).EndsWith(".exe"))
{
command = extraPath;
}
else
{
continue;
else
{
continue;
}
try
{
Expand Down
1 change: 1 addition & 0 deletions LocalLibrary.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<Compile Include="MergedItem.cs" />
<Compile Include="FindInstallers.cs" />
<Compile Include="Levenshtein.cs" />
<Compile Include="LocalLibrarySettings.cs" />
Expand Down
Loading

0 comments on commit cef94b0

Please sign in to comment.