[RomTools] Try to make sort more safe

This commit is contained in:
Matt Nadareski
2016-09-11 17:40:46 -07:00
parent 9d3019dfaa
commit a595a54f7b
2 changed files with 32 additions and 13 deletions

View File

@@ -226,27 +226,35 @@ namespace SabreTools.Helper
/// <returns>True if it sorted correctly, false otherwise</returns> /// <returns>True if it sorted correctly, false otherwise</returns>
public static bool Sort(List<Rom> roms, bool norename) public static bool Sort(List<Rom> roms, bool norename)
{ {
roms.Sort(delegate (Rom x, Rom y) try
{ {
if (x.Metadata.SystemID == y.Metadata.SystemID) roms.Sort(delegate (Rom x, Rom y)
{ {
if (x.Metadata.SourceID == y.Metadata.SourceID) if (x.Metadata.SystemID == y.Metadata.SystemID)
{ {
if (x.Machine.Name == y.Machine.Name) if (x.Metadata.SourceID == y.Metadata.SourceID)
{ {
if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name)) if (x.Machine.Name == y.Machine.Name)
{ {
return Style.CompareNumeric(Path.GetFileName(x.Name), Path.GetFileName(y.Name)); if (Path.GetDirectoryName(x.Name) == Path.GetDirectoryName(y.Name))
{
return Style.CompareNumeric(Path.GetFileName(x.Name), Path.GetFileName(y.Name));
}
return Style.CompareNumeric(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name));
} }
return Style.CompareNumeric(Path.GetDirectoryName(x.Name), Path.GetDirectoryName(y.Name)); return Style.CompareNumeric(x.Machine.Name, y.Machine.Name);
} }
return Style.CompareNumeric(x.Machine.Name, y.Machine.Name); return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SourceID - y.Metadata.SourceID);
} }
return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SourceID - y.Metadata.SourceID); return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SystemID - y.Metadata.SystemID);
} });
return (norename ? String.Compare(x.Machine.Name, y.Machine.Name) : x.Metadata.SystemID - y.Metadata.SystemID); return true;
}); }
return true; catch (Exception ex)
{
Console.WriteLine(ex);
return false;
}
} }
#endregion #endregion

View File

@@ -514,6 +514,17 @@ namespace SabreTools.Helper
return 0; return 0;
} }
/// <summary>
/// http://stackoverflow.com/questions/146134/how-to-remove-illegal-characters-from-path-and-filenames
/// </summary>
public static string StripInvalidPathChars(string s)
{
string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
Regex r = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
s = r.Replace(s, "");
return s;
}
#endregion #endregion
} }
} }