Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Oodle and Dll Prep for better Linux and macOS support #120

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CUE4Parse-Conversion/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ public class Constants

public const int DXT_BITS_PER_PIXEL = 4;

public const string DETEX_DLL_NAME = "Detex.dll";
public const string DETEX_DLL_NAME = "detex";
public const string TEGRA_SWIZZLE_DLL_NAME = "tegra_swizzle_x64";
}
37 changes: 15 additions & 22 deletions CUE4Parse-Conversion/Textures/BC/Detex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,39 +57,32 @@ public static bool DecodeDetexLinear(byte[] inp, byte[] dst, int width, int heig
[DllImport(Constants.DETEX_DLL_NAME)]
private static extern unsafe bool detexDecompressTextureLinear(detexTexture* texture, byte* pixelBuffer,
uint pixelFormat);

private static void PrepareDllFile()
{
if (!OperatingSystem.IsWindows() || RuntimeInformation.ProcessArchitecture != Architecture.X64)
return;

using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CUE4Parse_Conversion.Resources.Detex.dll");
if (stream == null)
throw new MissingManifestResourceException("Couldn't find Detex.dll in Embedded Resources");

var ba = new byte[(int)stream.Length];
stream.Read(ba, 0, (int)stream.Length);

bool fileOk;
var dllFile = Constants.DETEX_DLL_NAME;
stream.ReadExactly(ba);

using (var sha1 = new SHA1CryptoServiceProvider())
var dllFile = Constants.DETEX_DLL_NAME + ".dll";

if (File.Exists(dllFile))
{
var fileHash = BitConverter.ToString(sha1.ComputeHash(ba)).Replace("-", string.Empty);
var bb = File.ReadAllBytes(dllFile);
var fileHash = SHA1.HashData(ba).AsSpan();
var fileHash2 = SHA1.HashData(bb).AsSpan();

if (File.Exists(dllFile))
{
var bb = File.ReadAllBytes(dllFile);
var fileHash2 = BitConverter.ToString(sha1.ComputeHash(bb)).Replace("-", string.Empty);

fileOk = fileHash == fileHash2;
}
else
{
fileOk = false;
}
if (fileHash.SequenceEqual(fileHash2))
return;
}

if (!fileOk)
{
File.WriteAllBytes(dllFile, ba);
}
File.WriteAllBytes(dllFile, ba);
}
}
}
44 changes: 19 additions & 25 deletions CUE4Parse-Conversion/Textures/PlatformDeswizzlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,49 +15,43 @@ static PlatformDeswizzlers()
PrepareDllFile();
}

[DllImport("tegra_swizzle_x64", EntryPoint = "deswizzle_block_linear")]
[DllImport(Constants.TEGRA_SWIZZLE_DLL_NAME, EntryPoint = "deswizzle_block_linear")]
private static extern unsafe void DeswizzleBlockLinearX64(ulong width, ulong height, ulong depth, byte* source, ulong sourceLength, byte[] destination, ulong destinationLength, ulong blockHeight, ulong bytesPerPixel);

[DllImport("tegra_swizzle_x64", EntryPoint = "swizzled_surface_size")]
[DllImport(Constants.TEGRA_SWIZZLE_DLL_NAME, EntryPoint = "swizzled_surface_size")]
private static extern ulong GetSurfaceSizeX64(ulong width, ulong height, ulong depth, ulong blockHeight, ulong bytesPerPixel);

[DllImport("tegra_swizzle_x64", EntryPoint = "block_height_mip0")]
[DllImport(Constants.TEGRA_SWIZZLE_DLL_NAME, EntryPoint = "block_height_mip0")]
private static extern ulong BlockHeightMip0X64(ulong height);

[DllImport("tegra_swizzle_x64", EntryPoint = "mip_block_height")]
[DllImport(Constants.TEGRA_SWIZZLE_DLL_NAME, EntryPoint = "mip_block_height")]
private static extern ulong MipBlockHeightX64(ulong mipHeight, ulong blockHeightMip0);

private static void PrepareDllFile()
{
if (!OperatingSystem.IsWindows() || RuntimeInformation.ProcessArchitecture != Architecture.X64)
return;

using var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("CUE4Parse_Conversion.Resources.tegra_swizzle_x64.dll");
if (stream == null)
throw new MissingManifestResourceException("Couldn't find tegra_swizzle_x64.dll in Embedded Resources");
var ba = new byte[(int) stream.Length];
stream.Read(ba, 0, (int) stream.Length);

var ba = new byte[(int)stream.Length];
stream.ReadExactly(ba);

bool fileOk;

using (var sha1 = SHA1.Create())
var dllFile = Constants.TEGRA_SWIZZLE_DLL_NAME + ".dll";
if (File.Exists(dllFile))
{
var fileHash = BitConverter.ToString(sha1.ComputeHash(ba)).Replace("-", string.Empty);

if (File.Exists("tegra_swizzle_x64.dll"))
{
var bb = File.ReadAllBytes("tegra_swizzle_x64.dll");
var fileHash2 = BitConverter.ToString(sha1.ComputeHash(bb)).Replace("-", string.Empty);
var bb = File.ReadAllBytes(dllFile);
var fileHash = SHA1.HashData(ba).AsSpan();
var fileHash2 = SHA1.HashData(bb).AsSpan();

fileOk = fileHash == fileHash2;
}
else
{
fileOk = false;
}
if (fileHash.SequenceEqual(fileHash2))
return;
}

if (!fileOk)
{
File.WriteAllBytes("tegra_swizzle_x64.dll", ba);
}
File.WriteAllBytes(dllFile, ba);
}

public static byte[] GetDeswizzledData(byte[] data, FTexture2DMipMap mip, FPixelFormatInfo formatInfo)
Expand Down
4 changes: 2 additions & 2 deletions CUE4Parse/CUE4Parse.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@

<ItemGroup>
<None Remove="Resources\ACE7Key.bin" />
<EmbeddedResource Include="Resources\ACE7Key.bin" />
<EmbeddedResource Include="Resources\ACE7Key.bin" LogicalName="CUE4Parse.Resources.ACE7Key.bin" />
<None Remove="Resources\PUBGNameHashMap.json" />
<EmbeddedResource Include="Resources\PUBGNameHashMap.json" />
<EmbeddedResource Include="Resources\PUBGNameHashMap.json" LogicalName="CUE4Parse.Resources.PUBGNameHashMap.json" />
</ItemGroup>

<PropertyGroup>
Expand Down
Loading