mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Move sorting to DLL
This commit is contained in:
@@ -2,10 +2,7 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using System.Web;
|
|
||||||
|
|
||||||
using SabreTools.Helper;
|
using SabreTools.Helper;
|
||||||
|
|
||||||
@@ -25,7 +22,6 @@ namespace SabreTools
|
|||||||
private bool _old;
|
private bool _old;
|
||||||
|
|
||||||
// Private required variables
|
// Private required variables
|
||||||
private Dictionary<int, string> _headers;
|
|
||||||
private Logger _logger;
|
private Logger _logger;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -48,26 +44,13 @@ namespace SabreTools
|
|||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
// Take care of special outfolder cases
|
// Take care of special outfolder cases
|
||||||
_outdir = (outdir == "" ? outdir :
|
_outdir = (outdir == "" ? Environment.CurrentDirectory + Path.DirectorySeparatorChar :
|
||||||
(outdir.Contains("/") && !outdir.EndsWith("/") ? outdir + "/" :
|
(!outdir.EndsWith(Path.DirectorySeparatorChar.ToString()) ? outdir + Path.DirectorySeparatorChar : outdir)
|
||||||
(outdir.Contains("\\") && !outdir.EndsWith("\\") ? outdir + "\\" :
|
|
||||||
(!outdir.Contains("/") && !outdir.Contains("\\") ? outdir + "\\" : outdir)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
if (_outdir != "" && !Directory.Exists(_outdir))
|
if (_outdir != "" && !Directory.Exists(_outdir))
|
||||||
{
|
{
|
||||||
Directory.CreateDirectory(_outdir);
|
Directory.CreateDirectory(_outdir);
|
||||||
}
|
}
|
||||||
|
|
||||||
_headers = new Dictionary<int, string>();
|
|
||||||
_headers.Add(25, "a7800.xml");
|
|
||||||
_headers.Add(228, "fds.xml");
|
|
||||||
_headers.Add(31, "lynx.xml");
|
|
||||||
_headers.Add(0, "mega.xml"); // Merged version of all other headers
|
|
||||||
_headers.Add(234, "n64.xml");
|
|
||||||
_headers.Add(238, "nes.xml");
|
|
||||||
_headers.Add(241, "snes.xml"); // Self-created to deal with various headers
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -255,52 +238,12 @@ JOIN checksums
|
|||||||
SHA1 = sldr.GetString(12),
|
SHA1 = sldr.GetString(12),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (merged)
|
// Rename the game associated if it's still valid and we allow renames
|
||||||
|
if (merged && !_norename)
|
||||||
{
|
{
|
||||||
// If it's the first rom in the list, don't touch it
|
temp.Game = temp.Game +
|
||||||
if (roms.Count != 0)
|
(sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") +
|
||||||
{
|
(srcmerged ? " [" + temp.Source + "]" : "");
|
||||||
// Check if the rom is a duplicate
|
|
||||||
RomData last = roms[roms.Count - 1];
|
|
||||||
|
|
||||||
bool shouldcont = false;
|
|
||||||
if (temp.Type == "rom" && last.Type == "rom")
|
|
||||||
{
|
|
||||||
shouldcont = ((temp.Size != -1 && temp.Size == last.Size) && (
|
|
||||||
(temp.CRC != "" && last.CRC != "" && temp.CRC == last.CRC) ||
|
|
||||||
(temp.MD5 != "" && last.MD5 != "" && temp.MD5 == last.MD5) ||
|
|
||||||
(temp.SHA1 != "" && last.SHA1 != "" && temp.SHA1 == last.SHA1)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
else if (temp.Type == "disk" && last.Type == "disk")
|
|
||||||
{
|
|
||||||
shouldcont = ((temp.MD5 != "" && last.MD5 != "" && temp.MD5 == last.MD5) ||
|
|
||||||
(temp.SHA1 != "" && last.SHA1 != "" && temp.SHA1 == last.SHA1)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// If it's a duplicate, skip adding it to the output but add any missing information
|
|
||||||
if (shouldcont)
|
|
||||||
{
|
|
||||||
last.CRC = (last.CRC == "" && temp.CRC != "" ? temp.CRC : last.CRC);
|
|
||||||
last.MD5 = (last.MD5 == "" && temp.MD5 != "" ? temp.MD5 : last.MD5);
|
|
||||||
last.SHA1 = (last.SHA1 == "" && temp.SHA1 != "" ? temp.SHA1 : last.SHA1);
|
|
||||||
|
|
||||||
roms.RemoveAt(roms.Count - 1);
|
|
||||||
roms.Insert(roms.Count, last);
|
|
||||||
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Rename the game associated if it's still valid and we allow renames
|
|
||||||
if (!_norename)
|
|
||||||
{
|
|
||||||
temp.Game = temp.Game +
|
|
||||||
(sysmerged ? " [" + temp.Manufacturer + " - " + temp.System + "]" : "") +
|
|
||||||
(srcmerged ? " [" + temp.Source + "]" : "");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
roms.Add(temp);
|
roms.Add(temp);
|
||||||
@@ -309,25 +252,11 @@ JOIN checksums
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If we're in a merged mode, resort by the correct parameters
|
// If we're in a merged mode, merge and then resort by the correct parameters
|
||||||
if (merged)
|
if (merged)
|
||||||
{
|
{
|
||||||
roms.Sort(delegate (RomData x, RomData y)
|
roms = Sort.Merge(roms, true);
|
||||||
{
|
Sort.RomSort(roms, _norename);
|
||||||
if (x.SystemID == y.SystemID)
|
|
||||||
{
|
|
||||||
if (x.SourceID == y.SourceID)
|
|
||||||
{
|
|
||||||
if (x.Game == y.Game)
|
|
||||||
{
|
|
||||||
return String.Compare(x.Name, y.Name);
|
|
||||||
}
|
|
||||||
return String.Compare(x.Game, y.Game);
|
|
||||||
}
|
|
||||||
return (_norename ? String.Compare(x.Game, y.Game) : x.SourceID - y.SourceID);
|
|
||||||
}
|
|
||||||
return (_norename ? String.Compare(x.Game, y.Game) : x.SystemID - y.SystemID);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now check rename within games
|
// Now check rename within games
|
||||||
|
|||||||
@@ -76,6 +76,7 @@
|
|||||||
<Compile Include="Output.cs" />
|
<Compile Include="Output.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Remapping.cs" />
|
<Compile Include="Remapping.cs" />
|
||||||
|
<Compile Include="Sort.cs" />
|
||||||
<Compile Include="Structs.cs" />
|
<Compile Include="Structs.cs" />
|
||||||
<Compile Include="Style.cs" />
|
<Compile Include="Style.cs" />
|
||||||
<Compile Include="Build.cs" />
|
<Compile Include="Build.cs" />
|
||||||
|
|||||||
111
SabreHelper/Sort.cs
Normal file
111
SabreHelper/Sort.cs
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SabreTools.Helper
|
||||||
|
{
|
||||||
|
public class Sort
|
||||||
|
{
|
||||||
|
public static List<RomData> Merge(List<RomData> inroms, bool presorted = false)
|
||||||
|
{
|
||||||
|
List<RomData> outroms = new List<RomData>();
|
||||||
|
|
||||||
|
// First sort the roms by size, crc, sysid, srcid, md5, and sha1 (in order), if not sorted already
|
||||||
|
if (!presorted)
|
||||||
|
{
|
||||||
|
inroms.Sort(delegate (RomData x, RomData y)
|
||||||
|
{
|
||||||
|
if (x.Size == y.Size)
|
||||||
|
{
|
||||||
|
if (x.CRC == y.CRC)
|
||||||
|
{
|
||||||
|
if (x.SystemID == y.SystemID)
|
||||||
|
{
|
||||||
|
if (x.SourceID == y.SourceID)
|
||||||
|
{
|
||||||
|
if (x.MD5 == y.MD5)
|
||||||
|
{
|
||||||
|
return String.Compare(x.SHA1, y.SHA1);
|
||||||
|
}
|
||||||
|
return String.Compare(x.MD5, y.MD5);
|
||||||
|
}
|
||||||
|
return x.SourceID - y.SourceID;
|
||||||
|
}
|
||||||
|
return x.SystemID - y.SystemID;
|
||||||
|
}
|
||||||
|
return String.Compare(x.CRC, y.CRC);
|
||||||
|
}
|
||||||
|
return (int)(x.Size - y.Size);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then, deduplicate them by checking to see if data matches
|
||||||
|
foreach (RomData rom in inroms)
|
||||||
|
{
|
||||||
|
// If it's the first rom in the list, don't touch it
|
||||||
|
if (outroms.Count != 0)
|
||||||
|
{
|
||||||
|
// Check if the rom is a duplicate
|
||||||
|
RomData last = outroms[outroms.Count - 1];
|
||||||
|
|
||||||
|
bool shouldcont = false;
|
||||||
|
if (rom.Type == "rom" && last.Type == "rom")
|
||||||
|
{
|
||||||
|
shouldcont = ((rom.Size != -1 && rom.Size == last.Size) && (
|
||||||
|
(rom.CRC != "" && last.CRC != "" && rom.CRC == last.CRC) ||
|
||||||
|
(rom.MD5 != "" && last.MD5 != "" && rom.MD5 == last.MD5) ||
|
||||||
|
(rom.SHA1 != "" && last.SHA1 != "" && rom.SHA1 == last.SHA1)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else if (rom.Type == "disk" && last.Type == "disk")
|
||||||
|
{
|
||||||
|
shouldcont = ((rom.MD5 != "" && last.MD5 != "" && rom.MD5 == last.MD5) ||
|
||||||
|
(rom.SHA1 != "" && last.SHA1 != "" && rom.SHA1 == last.SHA1)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// If it's a duplicate, skip adding it to the output but add any missing information
|
||||||
|
if (shouldcont)
|
||||||
|
{
|
||||||
|
last.CRC = (last.CRC == "" && rom.CRC != "" ? rom.CRC : last.CRC);
|
||||||
|
last.MD5 = (last.MD5 == "" && rom.MD5 != "" ? rom.MD5 : last.MD5);
|
||||||
|
last.SHA1 = (last.SHA1 == "" && rom.SHA1 != "" ? rom.SHA1 : last.SHA1);
|
||||||
|
|
||||||
|
outroms.RemoveAt(inroms.Count - 1);
|
||||||
|
outroms.Insert(inroms.Count, last);
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Then return the result
|
||||||
|
return outroms;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Sort a list of RomData objects by SystemID, SourceID, Game, and Name (in order)
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="roms">List of RomData objects representing the roms to be sorted</param>
|
||||||
|
/// <param name="norename">True if files are not renamed, false otherwise</param>
|
||||||
|
public static void RomSort(List<RomData> roms, bool norename)
|
||||||
|
{
|
||||||
|
roms.Sort(delegate (RomData x, RomData y)
|
||||||
|
{
|
||||||
|
if (x.SystemID == y.SystemID)
|
||||||
|
{
|
||||||
|
if (x.SourceID == y.SourceID)
|
||||||
|
{
|
||||||
|
if (x.Game == y.Game)
|
||||||
|
{
|
||||||
|
return String.Compare(x.Name, y.Name);
|
||||||
|
}
|
||||||
|
return String.Compare(x.Game, y.Game);
|
||||||
|
}
|
||||||
|
return (norename ? String.Compare(x.Game, y.Game) : x.SourceID - y.SourceID);
|
||||||
|
}
|
||||||
|
return (norename ? String.Compare(x.Game, y.Game) : x.SystemID - y.SystemID);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user