[DatFile] Add restore flag for level split

This commit is contained in:
Matt Nadareski
2016-10-31 14:55:51 -07:00
parent b7527ca174
commit 5e4f17f634
5 changed files with 121 additions and 116 deletions

View File

@@ -204,6 +204,7 @@ namespace SabreTools.Helper.Data
helptext.Add(" -ls, --lvl-split Split a SuperDAT or folder by internal path");
helptext.Add(" -out= Output directory");
helptext.Add(" -s, --short Use short output names");
helptext.Add(" -r, --restore Restore original filename to each output DAT");
// Sort
helptext.Add(" -ss, --sort Sort input files by a set of DATs");

View File

@@ -5026,9 +5026,10 @@ namespace SabreTools.Helper.Dats
/// <param name="outDir">Name of the directory to write the DATs out to</param>
/// <param name="basepath">Parent path for replacement</param>
/// <param name="shortname">True if short names should be used, false otherwise</param>
/// <param name="restore">True if original filenames should be used as the base for output filename, false otherwise</param>
/// <param name="logger">Logger object for console and file writing</param>
/// <returns>True if split succeeded, false otherwise</returns>
public bool SplitByLevel(string outDir, string basepath, bool shortname, Logger logger)
public bool SplitByLevel(string outDir, string basepath, bool shortname, bool restore, Logger logger)
{
// Sanitize the basepath to be more predictable
basepath = (basepath.EndsWith(Path.DirectorySeparatorChar.ToString()) ? basepath : basepath + Path.DirectorySeparatorChar);
@@ -5051,7 +5052,7 @@ namespace SabreTools.Helper.Dats
if (tempDat.Name != null && tempDat.Name != Style.GetDirectoryName(key))
{
// Process and output the DAT
SplitByLevelHelper(tempDat, outDir, shortname, logger);
SplitByLevelHelper(tempDat, outDir, shortname, restore, logger);
// Reset the DAT for the next items
tempDat = (DatFile)CloneHeader();
@@ -5078,7 +5079,7 @@ namespace SabreTools.Helper.Dats
}
// Then we write the last DAT out since it would be skipped otherwise
SplitByLevelHelper(tempDat, outDir, shortname, logger);
SplitByLevelHelper(tempDat, outDir, shortname, restore, logger);
return true;
}
@@ -5108,8 +5109,9 @@ namespace SabreTools.Helper.Dats
/// <param name="datFile">DAT to clean and write out</param>
/// <param name="outDir">Directory to write out to</param>
/// <param name="shortname">True if short naming scheme should be used, false otherwise</param>
/// <param name="restore">True if original filenames should be used as the base for output filename, false otherwise</param>
/// <param name="logger">Logger object for file and console output</param>
private void SplitByLevelHelper(DatFile datFile, string outDir, bool shortname, Logger logger)
private void SplitByLevelHelper(DatFile datFile, string outDir, bool shortname, bool restore, Logger logger)
{
// Get the path that the file will be written out to
string path = HttpUtility.HtmlDecode(String.IsNullOrEmpty(datFile.Name)
@@ -5124,7 +5126,8 @@ namespace SabreTools.Helper.Dats
: datFile.Name.Replace("/", " - ").Replace("\\", " - ")
)
);
datFile.Description += " (" + datFile.Name.Replace("/", " - ").Replace("\\", " - ") + ")";
datFile.FileName = (restore ? FileName + " (" + datFile.FileName + ")" : datFile.FileName);
datFile.Description = Description + " (" + datFile.Name.Replace("/", " - ").Replace("\\", " - ") + ")";
datFile.Name = Name + " (" + datFile.Name.Replace("/", " - ").Replace("\\", " - ") + ")";
datFile.Type = null;

View File

@@ -28,6 +28,60 @@ namespace SabreTools.Helper.Tools
{
#region DAT Cleaning
/// <summary>
/// Clean a game (or rom) name to the WoD standard
/// </summary>
/// <param name="game">Name of the game to be cleaned</param>
/// <returns>The cleaned name</returns>
public static string CleanGameName(string game)
{
///Run the name through the filters to make sure that it's correct
game = NormalizeChars(game);
game = RussianToLatin(game);
game = SearchPattern(game);
game = new Regex(@"(([[(].*[\)\]] )?([^([]+))").Match(game).Groups[1].Value;
game = game.TrimStart().TrimEnd();
return game;
}
/// <summary>
/// Clean a game (or rom) name to the WoD standard
/// </summary>
/// <param name="game">Array representing the path to be cleaned</param>
/// <returns>The cleaned name</returns>
public static string CleanGameName(string[] game)
{
game[game.Length - 1] = CleanGameName(game[game.Length - 1]);
string outgame = String.Join(Path.DirectorySeparatorChar.ToString(), game);
outgame = outgame.TrimStart().TrimEnd();
return outgame;
}
/// <summary>
/// Clean a hash string and pad to the correct size
/// </summary>
/// <param name="hash">Hash string to sanitize</param>
/// <param name="padding">Amount of characters to pad to</param>
/// <returns>Cleaned string</returns>
public static string CleanHashData(string hash, int padding)
{
// First get the hash to the correct length
hash = (String.IsNullOrEmpty(hash) ? "" : hash.Trim());
hash = (hash.StartsWith("0x") ? hash.Remove(0, 2) : hash);
hash = (hash == "-" ? "" : hash);
hash = (String.IsNullOrEmpty(hash) ? "" : hash.PadLeft(padding, '0'));
hash = hash.ToLowerInvariant();
// Then make sure that it has the correct characters
if (!Regex.IsMatch(hash, "[0-9a-f]{" + padding + "}"))
{
hash = "";
}
return hash;
}
/// <summary>
/// Generate a proper outfile name based on a DAT and output directory
/// </summary>
@@ -196,85 +250,10 @@ namespace SabreTools.Helper.Tools
return outfile;
}
/// <summary>
/// Clean a game (or rom) name to the WoD standard
/// </summary>
/// <param name="game">Name of the game to be cleaned</param>
/// <returns>The cleaned name</returns>
public static string CleanGameName(string game)
{
///Run the name through the filters to make sure that it's correct
game = NormalizeChars(game);
game = RussianToLatin(game);
game = SearchPattern(game);
game = new Regex(@"(([[(].*[\)\]] )?([^([]+))").Match(game).Groups[1].Value;
game = game.TrimStart().TrimEnd();
return game;
}
/// <summary>
/// Clean a game (or rom) name to the WoD standard
/// </summary>
/// <param name="game">Array representing the path to be cleaned</param>
/// <returns>The cleaned name</returns>
public static string CleanGameName(string[] game)
{
game[game.Length - 1] = CleanGameName(game[game.Length - 1]);
string outgame = String.Join(Path.DirectorySeparatorChar.ToString(), game);
outgame = outgame.TrimStart().TrimEnd();
return outgame;
}
/// <summary>
/// Clean a hash string and pad to the correct size
/// </summary>
/// <param name="hash">Hash string to sanitize</param>
/// <param name="padding">Amount of characters to pad to</param>
/// <returns>Cleaned string</returns>
public static string CleanHashData(string hash, int padding)
{
// First get the hash to the correct length
hash = (String.IsNullOrEmpty(hash) ? "" : hash.Trim());
hash = (hash.StartsWith("0x") ? hash.Remove(0, 2) : hash);
hash = (hash == "-" ? "" : hash);
hash = (String.IsNullOrEmpty(hash) ? "" : hash.PadLeft(padding, '0'));
hash = hash.ToLowerInvariant();
// Then make sure that it has the correct characters
if (!Regex.IsMatch(hash, "[0-9a-f]{" + padding + "}"))
{
hash = "";
}
return hash;
}
#endregion
#region String Manipulation
/// <summary>
/// Get if a string contains Unicode characters
/// </summary>
/// <param name="s">Input string to test</param>
/// <returns>True if the string contains at least one Unicode character, false otherwise</returns>
public static bool IsUnicode(string s)
{
return (s.Any(c => c > 255));
}
/// <summary>
/// Remove all chars that are considered path unsafe
/// </summary>
/// <param name="s">Input string to clean</param>
/// <returns>Cleaned string</returns>
public static string RemovePathUnsafeCharacters(string s)
{
List<char> invalidPath = Path.GetInvalidPathChars().ToList();
return new string(s.Where(c => !invalidPath.Contains(c)).ToArray());
}
/// <summary>
/// Compare strings as numeric
/// </summary>
@@ -396,6 +375,27 @@ namespace SabreTools.Helper.Tools
.ToArray());
}
/// <summary>
/// Get if a string contains Unicode characters
/// </summary>
/// <param name="s">Input string to test</param>
/// <returns>True if the string contains at least one Unicode character, false otherwise</returns>
public static bool IsUnicode(string s)
{
return (s.Any(c => c > 255));
}
/// <summary>
/// Remove all chars that are considered path unsafe
/// </summary>
/// <param name="s">Input string to clean</param>
/// <returns>Cleaned string</returns>
public static string RemovePathUnsafeCharacters(string s)
{
List<char> invalidPath = Path.GetInvalidPathChars().ToList();
return new string(s.Where(c => !invalidPath.Contains(c)).ToArray());
}
/// <summary>
/// Split a line as if it were a CMP rom line
/// </summary>
@@ -549,6 +549,38 @@ namespace SabreTools.Helper.Tools
return input;
}
/// <summary>
/// Convert Cyrillic lettering to Latin lettering
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string RussianToLatin(string input)
{
string[,] charmap = {
{ "А", "A" }, { "Б", "B" }, { "В", "V" }, { "Г", "G" }, { "Д", "D" },
{ "Е", "E" }, { "Ё", "Yo" }, { "Ж", "Zh" }, { "З", "Z" }, { "И", "I" },
{ "Й", "J" }, { "К", "K" }, { "Л", "L" }, { "М", "M" }, { "Н", "N" },
{ "О", "O" }, { "П", "P" }, { "Р", "R" }, { "С", "S" }, { "Т", "T" },
{ "У", "U" }, { "Ф", "f" }, { "Х", "Kh" }, { "Ц", "Ts" }, { "Ч", "Ch" },
{ "Ш", "Sh" }, { "Щ", "Sch" }, { "Ъ", "" }, { "Ы", "y" }, { "Ь", "" },
{ "Э", "e" }, { "Ю", "yu" }, { "Я", "ya" }, { "а", "a" }, { "б", "b" },
{ "в", "v" }, { "г", "g" }, { "д", "d" }, { "е", "e" }, { "ё", "yo" },
{ "ж", "zh" }, { "з", "z" }, { "и", "i" }, { "й", "j" }, { "к", "k" },
{ "л", "l" }, { "м", "m" }, { "н", "n" }, { "о", "o" }, { "п", "p" },
{ "р", "r" }, { "с", "s" }, { "т", "t" }, { "у", "u" }, { "ф", "f" },
{ "х", "kh" }, { "ц", "ts" }, { "ч", "ch" }, { "ш", "sh" }, { "щ", "sch" },
{ "ъ", "" }, { "ы", "y" }, { "ь", "" }, { "э", "e" }, { "ю", "yu" },
{ "я", "ya" },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = input.Replace(charmap[i, 0], charmap[i, 1]);
}
return input;
}
/// <summary>
/// Replace special characters and patterns
/// </summary>
@@ -593,38 +625,6 @@ namespace SabreTools.Helper.Tools
return input;
}
/// <summary>
/// Convert Cyrillic lettering to Latin lettering
/// </summary>
/// <param name="input">String to be parsed</param>
/// <returns>String with characters replaced</returns>
public static string RussianToLatin(string input)
{
string[,] charmap = {
{ "А", "A" }, { "Б", "B" }, { "В", "V" }, { "Г", "G" }, { "Д", "D" },
{ "Е", "E" }, { "Ё", "Yo" }, { "Ж", "Zh" }, { "З", "Z" }, { "И", "I" },
{ "Й", "J" }, { "К", "K" }, { "Л", "L" }, { "М", "M" }, { "Н", "N" },
{ "О", "O" }, { "П", "P" }, { "Р", "R" }, { "С", "S" }, { "Т", "T" },
{ "У", "U" }, { "Ф", "f" }, { "Х", "Kh" }, { "Ц", "Ts" }, { "Ч", "Ch" },
{ "Ш", "Sh" }, { "Щ", "Sch" }, { "Ъ", "" }, { "Ы", "y" }, { "Ь", "" },
{ "Э", "e" }, { "Ю", "yu" }, { "Я", "ya" }, { "а", "a" }, { "б", "b" },
{ "в", "v" }, { "г", "g" }, { "д", "d" }, { "е", "e" }, { "ё", "yo" },
{ "ж", "zh" }, { "з", "z" }, { "и", "i" }, { "й", "j" }, { "к", "k" },
{ "л", "l" }, { "м", "m" }, { "н", "n" }, { "о", "o" }, { "п", "p" },
{ "р", "r" }, { "с", "s" }, { "т", "t" }, { "у", "u" }, { "ф", "f" },
{ "х", "kh" }, { "ц", "ts" }, { "ч", "ch" }, { "ш", "sh" }, { "щ", "sch" },
{ "ъ", "" }, { "ы", "y" }, { "ь", "" }, { "э", "e" }, { "ю", "yu" },
{ "я", "ya" },
};
for (int i = 0; i < charmap.GetLength(0); i++)
{
input = input.Replace(charmap[i, 0], charmap[i, 1]);
}
return input;
}
#endregion
#region Externally sourced methods

View File

@@ -257,7 +257,8 @@ namespace SabreTools
/// <param name="inputs">List of inputs to be used</param>
/// <param name="outDir">Output directory for the split files</param>
/// <param name="shortname">True if short filenames should be used, false otherwise</param>
private static void InitLevelSplit(List<string> inputs, string outDir, bool shortname)
/// <param name="restore">True if original filenames should be used as the base for output filename, false otherwise</param>
private static void InitLevelSplit(List<string> inputs, string outDir, bool shortname, bool restore)
{
// Loop over the input files
foreach (string input in inputs)
@@ -266,7 +267,7 @@ namespace SabreTools
{
DatFile datFile = new DatFile();
datFile.Parse(Path.GetFullPath(input), 0, 0, _logger, softlist: true, keep: true);
datFile.SplitByLevel(outDir, Path.GetDirectoryName(input), shortname, _logger);
datFile.SplitByLevel(outDir, Path.GetDirectoryName(input), shortname, restore, _logger);
}
else if (Directory.Exists(input))
{
@@ -274,7 +275,7 @@ namespace SabreTools
{
DatFile datFile = new DatFile();
datFile.Parse(Path.GetFullPath(file), 0, 0, _logger, softlist: true, keep: true);
datFile.SplitByLevel(outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), shortname, _logger);
datFile.SplitByLevel(outDir, (input.EndsWith(Path.DirectorySeparatorChar.ToString()) ? input : input + Path.DirectorySeparatorChar), shortname, restore, _logger);
}
}
else

View File

@@ -994,7 +994,7 @@ namespace SabreTools
// Split a SuperDAT by lowest available level
else if (splitByLevel)
{
InitLevelSplit(inputs, outDir, shortname);
InitLevelSplit(inputs, outDir, shortname, restore);
}
// Split a DAT by item type