Skip to content

Commit

Permalink
always try lower mt key first #124
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesWu committed Feb 27, 2024
1 parent 4591323 commit 62013ec
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 5 deletions.
2 changes: 1 addition & 1 deletion FreeMote.Plugins/Shells/MdfShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
stream.Position = pos;
}

return MPack.MdfDecompressToPsbStream(stream, size) as MemoryStream;
return MPack.MdfDecompressToStream(stream, size) as MemoryStream;
}


Expand Down
80 changes: 80 additions & 0 deletions FreeMote.PsBuild/PsbDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -827,5 +827,85 @@ static void EnsureDirectory(string path)
Directory.CreateDirectory(baseDir);
}
}

/// <summary>
/// Unpack MT19937 MDF
/// </summary>
/// <param name="filePath"></param>
/// <param name="outputPath"></param>
/// <param name="key"></param>
/// <param name="keyLength"></param>
/// <param name="fileKey"></param>
/// <param name="suffix"></param>
/// <exception cref="FormatException"></exception>
internal static void MtUnpack(string filePath, string outputPath, string key, int keyLength, string fileKey = "", string suffix = "")
{
if (!File.Exists(filePath))
{
Logger.LogError($"Cannot find input file: {filePath}");
return;
}

var bodyBytes = File.ReadAllBytes(filePath);
MPack.IsSignatureMPack(bodyBytes, out var shellType);
var fileName = Path.GetFileName(filePath);
fileKey ??= "";
var name = string.IsNullOrEmpty(fileKey) ? fileName : fileKey;
var possibleFileNames = PsbExtension.ArchiveInfo_GetAllPossibleFileNames(name, suffix);
//var relativePath = fileKey;
var finalContext = new Dictionary<string, object>()
{
{Context_MdfKeyLength, keyLength}
};
finalContext.Remove(Context_ArchiveSource);

var ms = MsManager.GetStream(bodyBytes);
MemoryStream mms = null;

if (!string.IsNullOrEmpty(shellType) && possibleFileNames.Count > 0)
{
foreach (var possibleFileName in possibleFileNames)
{
var bodyContext = new Dictionary<string, object>(finalContext)
{
[Context_MdfKey] = key + possibleFileName,
[Context_FileName] = possibleFileName
};

try
{
mms = PsbExtension.MdfConvert(ms, shellType, bodyContext);
}
catch (InvalidDataException)
{
ms = MsManager.GetStream(bodyBytes);
mms = null;
}

if (mms != null)
{
//relativePath = possibleFileName.Contains("/") ? possibleFileName :
// fileKey.Contains("/") ? Path.Combine(Path.GetDirectoryName(fileKey), possibleFileName) :
// possibleFileName;
//finalContext = bodyContext;
if (possibleFileName != possibleFileNames[0])
{
Logger.Log($" detected key name: {fileKey} -> {possibleFileName}");
}

//write to file
using var outFs = File.OpenWrite(string.IsNullOrEmpty(outputPath)
? Path.ChangeExtension(filePath, ".unpack.psb")
: outputPath);
mms.WriteTo(outFs);
break;
}
}
}
else
{
throw new FormatException("File is not a shell PSB.");
}
}
}
}
7 changes: 7 additions & 0 deletions FreeMote.Psb/PsbExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ public static string ArchiveInfo_GetFileNameAppendSuffix(string name, string suf
"script/ikusei.nut.m" -> packed with key ikusei.nut.m (ok fine)
"sound/bgm.psb" -> not packed (??)
"bg_c_whit.m" -> packed with key bg_c_whit.m.psb.m (???)
tolower might be used
*/

/// <summary>
Expand All @@ -1114,6 +1115,12 @@ public static List<string> ArchiveInfo_GetAllPossibleFileNames(string name, stri
results.AddRange(ArchiveInfo_GetAllPossibleFileNames(name.Substring(name.LastIndexOf('/') + 1), suffix, keepDirectory));
}

//check if name is fully lower case
if (name.ToLowerInvariant() != name)
{
results.AddRange(ArchiveInfo_GetAllPossibleFileNames(name.ToLowerInvariant(), suffix, keepDirectory));
}

List<string> exts = new List<string>();
var name2 = name;
while (Path.GetExtension(name2) != string.Empty)
Expand Down
3 changes: 1 addition & 2 deletions FreeMote.Tests/PsbTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System.Linq;
using System.Text;
using FreeMote.Plugins;
using FreeMote.Plugins.Shells;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FreeMote.Psb;

Expand Down Expand Up @@ -223,7 +222,7 @@ public void TestMdf()

using (var mdfStream = File.OpenRead(path))
{
using (var psbStream = MPack.MdfDecompressToPsbStream(mdfStream))
using (var psbStream = MPack.MdfDecompressToStream(mdfStream))
{
//using (var pureStream = new MemoryStream((int)psbStream.Length))
{
Expand Down
4 changes: 2 additions & 2 deletions FreeMote/MPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ public static int MdfGetOriginalLength(this Stream stream)
return BitConverter.ToInt32(buffer, 0);
}

public static void MdfDecompressToPsbFile(string inputPath, string outputPath)
public static void MdfDecompressToFile(string inputPath, string outputPath)
{
Stream mfs = File.OpenRead(inputPath);
mfs.Seek(10, SeekOrigin.Begin);
File.WriteAllBytes(outputPath, ZlibCompress.Decompress(mfs));
mfs.Dispose();
}

public static Stream MdfDecompressToPsbStream(Stream input, int size = 0)
public static Stream MdfDecompressToStream(Stream input, int size = 0)
{
input.Seek(10, SeekOrigin.Begin);
return ZlibCompress.DecompressToStream(input, size);
Expand Down

0 comments on commit 62013ec

Please sign in to comment.