Skip to content

Commit

Permalink
I have nothing to gatekeep, i have nothing special...
Browse files Browse the repository at this point in the history
  • Loading branch information
yarik0chka committed Apr 28, 2024
1 parent d135a93 commit 054df3b
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 27 deletions.
6 changes: 3 additions & 3 deletions AssetStudio.GUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public MainForm()
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
InitializeComponent();
Text = $"Studio v{Application.ProductVersion}";
Text = $"YarikStudio v{Application.ProductVersion}";
InitializeExportOptions();
InitializeProgressBar();
InitializeLogger();
Expand Down Expand Up @@ -308,7 +308,7 @@ private async void BuildAssetStructures()
}
}

Text = $"Studio v{Application.ProductVersion} - {productName} - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}";
Text = $"YarikStudio v{Application.ProductVersion} - {productName} - {assetsManager.assetsFileList[0].unityVersion} - {assetsManager.assetsFileList[0].m_TargetPlatform}";

assetListView.VirtualListSize = visibleAssets.Count;

Expand Down Expand Up @@ -1484,7 +1484,7 @@ private void StatusStripUpdate(string statusText)

public void ResetForm()
{
Text = $"Studio v{Application.ProductVersion}";
Text = $"YarikStudio v{Application.ProductVersion}";
assetsManager.Clear();
assemblyLoader.Clear();
exportableAssets.Clear();
Expand Down
22 changes: 21 additions & 1 deletion AssetStudio.GUI/Studio.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,31 @@ public static int ExtractFile(string fileName, string savePath)
extractedCount += ExtractBlkFile(reader, savePath);
else if (reader.FileType == FileType.BlockFile)
extractedCount += ExtractBlockFile(reader, savePath);
else if (reader.FileType == FileType.Blb3File)
extractedCount += ExtractBlb3File(reader, savePath);
else
reader.Dispose();
return extractedCount;
}

private static int ExtractBlb3File(FileReader reader, string savePath)
{
StatusStripUpdate($"Decompressing {reader.FileName} ...");
try
{
var bundleFile = new Blb3File(reader, reader.FullPath);
reader.Dispose();
if (bundleFile.fileList.Count > 0)
{
var extractPath = Path.Combine(savePath, reader.FileName + "_unpacked");
return ExtractStreamFile(extractPath, bundleFile.fileList);
}
}
catch (InvalidCastException)
{
Logger.Error($"Game type mismatch, Expected {nameof(Mr0k)} but got {Game.Name} ({Game.GetType().Name}) !!");
}
return 0;
}
private static int ExtractBundleFile(FileReader reader, string savePath)
{
StatusStripUpdate($"Decompressing {reader.FileName} ...");
Expand Down
6 changes: 6 additions & 0 deletions AssetStudio/AssetStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
<None Update="Keys.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="oo2core_9_win64.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="UnityPlayer.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
8 changes: 4 additions & 4 deletions AssetStudio/AssetsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,8 +429,8 @@ private void LoadBlockFile(FileReader reader)
case FileType.BundleFile:
LoadBundleFile(subReader, reader.FullPath, offset, false);
break;
case FileType.BlbFile:
LoadBlbFile(subReader, reader.FullPath, offset, false);
case FileType.Blb3File:
LoadBlb3File(subReader, reader.FullPath, offset, false);
break;
case FileType.MhyFile:
LoadMhyFile(subReader, reader.FullPath, offset, false);
Expand Down Expand Up @@ -528,15 +528,15 @@ private void LoadMhyFile(FileReader reader, string originalPath = null, long ori
}
}

private void LoadBlbFile(FileReader reader, string originalPath = null, long originalOffset = 0, bool log = true)
private void LoadBlb3File(FileReader reader, string originalPath = null, long originalOffset = 0, bool log = true)
{
if (log)
{
Logger.Info("Loading " + reader.FullPath);
}
try
{
var blbFile = new BlbFile(reader, reader.FullPath);
var blbFile = new Blb3File(reader, reader.FullPath);
foreach (var file in blbFile.fileList)
{
var dummyPath = Path.Combine(Path.GetDirectoryName(reader.FullPath), file.fileName);
Expand Down
114 changes: 106 additions & 8 deletions AssetStudio/BlbFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,70 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace AssetStudio
{
public class BlbFile
public class Blb3File
{
private List<BundleFile.StorageBlock> m_BlocksInfo;
private List<BundleFile.Node> m_DirectoryInfo;
private byte[] Header;

private static string ModuleName = "UnityPlayer.dll";
private static IntPtr Module = IntPtr.Zero;

public BundleFile.Header m_Header;
public List<StreamFile> fileList;
public long Offset;


public BlbFile(FileReader reader, string path)
[DllImport("kernel32.dll")]
static extern bool VirtualProtect(IntPtr lpAddress, uint dwSize, uint flNewProtect, out uint lpflOldProtect);

[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32.dll")]
static extern IntPtr GetModuleHandle(string lpModuleName);

public delegate int DecryptBlb(ref byte buffer, ulong size, ref byte header, ulong headerSize, uint idk);

public static void Decrypt(byte[] header, Span<byte> buffer)
{
if (Module == IntPtr.Zero)
{
Module = LoadLibrary(ModuleName);

byte[] key1 = new byte[256] { 0x63, 0x7d, 0x75, 0x78, 0xf6, 0x6e, 0x69, 0xc2, 0x38, 0x08, 0x6d, 0x20, 0xf2, 0xda, 0xa5, 0x79, 0xda, 0x93, 0xdb, 0x6e, 0xee, 0x4c, 0x51, 0xe7, 0xb5, 0xcd, 0xb8, 0xb4, 0x80, 0xb9, 0x6c, 0xdf, 0x97, 0xdc, 0xb1, 0x05, 0x12, 0x1a, 0xd1, 0xeb, 0x1c, 0x8c, 0xcf, 0xda, 0x5d, 0xf5, 0x1f, 0x3a, 0x34, 0xf6, 0x11, 0xf0, 0x2c, 0xa3, 0x33, 0xad, 0x3f, 0x2b, 0xba, 0xd9, 0xd7, 0x1a, 0x8c, 0x4a, 0x49, 0xc2, 0x6e, 0x59, 0x5f, 0x2b, 0x1c, 0xe7, 0x1a, 0x72, 0x9c, 0xf8, 0x65, 0x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xcc, 0xcf, 0xd3, 0x90, 0x2d, 0x48, 0xd3, 0x8f, 0x75, 0xe6, 0xd9, 0x1d, 0x2a, 0xe5, 0xc0, 0xf7, 0x2b, 0x78, 0x81, 0x87, 0x44, 0x0e, 0x5f, 0x50, 0x00, 0xd4, 0x61, 0x8d, 0xbe, 0x7b, 0x05, 0x15, 0x07, 0x3b, 0x33, 0x82, 0x1f, 0x18, 0x70, 0x92, 0xda, 0x64, 0x54, 0xce, 0xb1, 0x85, 0x3e, 0x69, 0x15, 0xf8, 0x46, 0x6a, 0x04, 0x96, 0x73, 0x0e, 0xd9, 0x16, 0x2f, 0x67, 0x68, 0xd4, 0xf7, 0x4a, 0x4a, 0xd0, 0x57, 0x68, 0x76 };

IntPtr key1Ptr = GetModuleHandle(ModuleName) + 0x1FF7090;
IntPtr key2Ptr = GetModuleHandle(ModuleName) + 0x1FF6F90;

OverrideBytes(key1Ptr, key1, 256);
OverrideBytes(key2Ptr, key2, 256);
}
IntPtr function_raw = Module + 0xFC730;
DecryptBlb decrypt = (DecryptBlb)Marshal.GetDelegateForFunctionPointer(function_raw, typeof(DecryptBlb));

ulong header_size = (ulong)header.Length;
ulong buffer_size = (ulong)buffer.Length;


decrypt(ref buffer[0], buffer_size > 128 ? 128 : buffer_size, ref header[0], header_size, 0);
}

public Blb3File(FileReader reader, string path)
{
Offset = reader.Position;
reader.Endian = EndianType.LittleEndian;

var signature = reader.ReadStringToNull(4);
Logger.Verbose($"Parsed signature {signature}");
if (signature != "Blb\x02")
throw new Exception("not a Blb file");
if (signature != "Blb\x03")
throw new Exception("not a Blb3 file");

var size = reader.ReadUInt32();
m_Header = new BundleFile.Header
Expand All @@ -38,13 +80,25 @@ public BlbFile(FileReader reader, string path)
m_Header.uncompressedBlocksInfoSize = size;

Logger.Verbose($"Header: {m_Header}");
reader.ReadUInt32();
Header = reader.ReadBytes(16);

var header = reader.ReadBytes((int)m_Header.compressedBlocksInfoSize);

Decrypt(Header, header);

ReadBlocksInfoAndDirectory(header);
using var blocksStream = CreateBlocksStream(path);
ReadBlocks(reader, blocksStream);
ReadFiles(blocksStream, path);
}
private static void OverrideBytes(IntPtr address, byte[] bytes, int length)
{
uint oldprotect;
VirtualProtect(address, (uint)length, 0x40, out oldprotect);
Marshal.Copy(bytes, 0, address, length);
VirtualProtect(address, (uint)length, oldprotect, out oldprotect);
}

private void ReadBlocksInfoAndDirectory(byte[] header)
{
Expand Down Expand Up @@ -82,6 +136,10 @@ private void ReadBlocksInfoAndDirectory(byte[] header)

Logger.Verbose($"Block {i} Info: {m_BlocksInfo[i]}");
}
for (int i = m_BlocksInfo.Count - 1; i > 0; i--)
{
m_BlocksInfo[i].compressedSize = m_BlocksInfo[i].compressedSize - m_BlocksInfo[i - 1].compressedSize;
}

reader.Position = nodesInfoOffset;
m_DirectoryInfo = new List<BundleFile.Node>();
Expand Down Expand Up @@ -140,6 +198,40 @@ private void ReadBlocks(FileReader reader, Stream blocksStream)
reader.BaseStream.CopyTo(blocksStream, blockInfo.compressedSize);
break;
}
case CompressionType.Oodle: //Oodle
{
{
var compressedSize = (int)blockInfo.compressedSize;
var uncompressedSize = (int)blockInfo.uncompressedSize;

var compressedBytes = ArrayPool<byte>.Shared.Rent(compressedSize);
var uncompressedBytes = ArrayPool<byte>.Shared.Rent(uncompressedSize);

var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize);
var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize);

try
{

reader.Read(compressedBytesSpan);

Decrypt(Header, compressedBytesSpan);

var numWrite = OodleHelper.Decompress(compressedBytesSpan, uncompressedBytesSpan);
if (numWrite != uncompressedSize)
{
Logger.Warning($"Oodle decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes");
}
}
finally
{
blocksStream.Write(uncompressedBytesSpan);
ArrayPool<byte>.Shared.Return(compressedBytes, true);
ArrayPool<byte>.Shared.Return(uncompressedBytes, true);
}
break;
}
}
case CompressionType.Lzma: //LZMA
{
SevenZipHelper.StreamDecompress(reader.BaseStream, blocksStream, blockInfo.compressedSize, blockInfo.uncompressedSize);
Expand All @@ -154,21 +246,27 @@ private void ReadBlocks(FileReader reader, Stream blocksStream)
var compressedBytes = ArrayPool<byte>.Shared.Rent(compressedSize);
var uncompressedBytes = ArrayPool<byte>.Shared.Rent(uncompressedSize);

var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize);
var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize);

try
{
var compressedBytesSpan = compressedBytes.AsSpan(0, compressedSize);
var uncompressedBytesSpan = uncompressedBytes.AsSpan(0, uncompressedSize);

reader.Read(compressedBytesSpan);
Decrypt(Header, compressedBytesSpan);
var numWrite = LZ4.Instance.Decompress(compressedBytesSpan, uncompressedBytesSpan);
if (numWrite != uncompressedSize)
{
throw new IOException($"Lz4 decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes");
Logger.Warning($"Lz4 decompression error, write {numWrite} bytes but expected {uncompressedSize} bytes");
}
blocksStream.Write(uncompressedBytesSpan);
}
catch (Exception e)
{
Logger.Error($"Lz4 decompression error {e.Message}");
}
finally
{
blocksStream.Write(uncompressedBytesSpan);
ArrayPool<byte>.Shared.Return(compressedBytes, true);
ArrayPool<byte>.Shared.Return(uncompressedBytes, true);
}
Expand Down
1 change: 1 addition & 0 deletions AssetStudio/BundleFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum CompressionType
Zstd = 5,
Lz4Lit4 = 4,
Lz4Lit5 = 5,
Oodle = 9,
}

public class BundleFile
Expand Down
16 changes: 11 additions & 5 deletions AssetStudio/FileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class FileReader : EndianBinaryReader
private static readonly byte[] zipMagic = { 0x50, 0x4B, 0x03, 0x04 };
private static readonly byte[] zipSpannedMagic = { 0x50, 0x4B, 0x07, 0x08 };
private static readonly byte[] mhy0Magic = { 0x6D, 0x68, 0x79, 0x30 };
private static readonly byte[] blbMagic = { 0x42, 0x6C, 0x62, 0x02 };
private static readonly byte[] blb2Magic = { 0x42, 0x6C, 0x62, 0x02 };
private static readonly byte[] blb3Magic = { 0x42, 0x6C, 0x62, 0x03 };
private static readonly byte[] narakaMagic = { 0x15, 0x1E, 0x1C, 0x0D, 0x0D, 0x23, 0x21 };
private static readonly byte[] gunfireMagic = { 0x7C, 0x6D, 0x79, 0x72, 0x27, 0x7A, 0x73, 0x78, 0x3F };

Expand Down Expand Up @@ -86,11 +87,16 @@ private FileType CheckFileType()
return FileType.MhyFile;
}
Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(mhy0Magic)}");
if (blbMagic.SequenceEqual(magic))
if (blb2Magic.SequenceEqual(magic))
{
return FileType.BlbFile;
return FileType.Blb2File;
}
Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(mhy0Magic)}");
Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(blb2Magic)}");
if (blb3Magic.SequenceEqual(magic))
{
return FileType.Blb3File;
}
Logger.Verbose($"Parsed signature does not match with expected signature {Convert.ToHexString(blb3Magic)}");
magic = ReadBytes(7);
Position = 0;
Logger.Verbose($"Parsed signature is {Convert.ToHexString(magic)}");
Expand Down Expand Up @@ -229,7 +235,7 @@ public static FileReader PreProcessing(this FileReader reader, Game game)
break;
}
}
if (reader.FileType == FileType.BundleFile && game.Type.IsBlockFile() || reader.FileType == FileType.ENCRFile || reader.FileType == FileType.BlbFile)
if (reader.FileType == FileType.BundleFile && game.Type.IsBlockFile() || reader.FileType == FileType.ENCRFile || reader.FileType == FileType.Blb2File || reader.FileType == FileType.Blb3File)
{
Logger.Verbose("File might have multiple bundles !!");
try
Expand Down
3 changes: 2 additions & 1 deletion AssetStudio/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public enum FileType
ZipFile,
BlkFile,
MhyFile,
BlbFile,
Blb2File,
Blb3File,
ENCRFile,
BlockFile
}
Expand Down
Loading

2 comments on commit 054df3b

@akioukun
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi i have a skill issue, the game gives error

ERROR ISSUE 8; 0x837726 at NDIQWYDTEGA();

Game Version: enginewkly_rel_CN5.2.51

  • Cannot decrypt vmprotect 9.0 ultimate
    Error code: {3, 8, 1}.

@yarik0chka
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@akioukun fix decompression errors and i thank you

Please sign in to comment.