Skip to content

Commit

Permalink
massive fix for shells; implement MxbShell, Fix #87
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesWu committed Jun 11, 2023
1 parent 402605d commit ae1e7a4
Show file tree
Hide file tree
Showing 20 changed files with 310 additions and 87 deletions.
22 changes: 15 additions & 7 deletions FreeMote.Plugins.x64/Shells/MflShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class MflShell : IPsbShell
{
public string Name => "MFL";

public byte[] Signature => new byte[] { (byte)'m', (byte)'f', (byte)'l', 0 };
public byte[] Signature => new byte[] {(byte) 'm', (byte) 'f', (byte) 'l', 0};

public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
Expand Down Expand Up @@ -46,9 +46,9 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
{
uint? keyLength = context.ContainsKey(Context_MdfKeyLength)
? Convert.ToUInt32(context[Context_MdfKeyLength])
: (uint?)null;
: (uint?) null;

stream = PsbExtension.EncodeMdf(stream, (string)context[Context_MdfKey], keyLength);
stream = PsbExtension.EncodeMdf(stream, (string) context[Context_MdfKey], keyLength, true);
stream.Position = 0; //A new MemoryStream
}
}
Expand Down Expand Up @@ -84,6 +84,8 @@ public MemoryStream ToShell(Stream stream, Dictionary<string, object> context =
stream.Read(input, 0, input.Length);
}

var unzipLength = input.Length;

var output = FastLzNative.Compress(input);
var ms = new MemoryStream(output);

Expand All @@ -96,16 +98,22 @@ public MemoryStream ToShell(Stream stream, Dictionary<string, object> context =
}
else
{
keyLength = (uint?)null;
keyLength = (uint?) null;
}

var mms = PsbExtension.EncodeMdf(ms, (string)context[Context_MdfKey], keyLength);
var mms = PsbExtension.EncodeMdf(ms, (string) context[Context_MdfKey], keyLength, false);
ms?.Dispose(); //ms disposed
ms = mms;
}

return ms;
ms.Seek(0, SeekOrigin.Begin);
var shellMs = new MemoryStream((int) (ms.Length + 8));
shellMs.Write(Signature, 0, 4);
shellMs.Write(BitConverter.GetBytes(unzipLength), 0, 4);
ms.CopyTo(shellMs);
ms.Dispose();
shellMs.Seek(0, SeekOrigin.Begin);
return shellMs;
}

}
}
4 changes: 4 additions & 0 deletions FreeMote.Plugins/FreeMote.Plugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Shells\Lz4Shell.cs" />
<Compile Include="Shells\MdfShell.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Shells\MxbShell.cs" />
<Compile Include="Shells\MzsShell.cs" />
<Compile Include="Shells\PsdShell.cs" />
<Compile Include="Shells\PspShell.cs" />
Expand All @@ -105,6 +106,9 @@
<PackageReference Include="BCnEncoder.NetStd">
<Version>2.1.0-CI00002</Version>
</PackageReference>
<PackageReference Include="FreeMote.XMemCompress">
<Version>1.0.0.6</Version>
</PackageReference>
<PackageReference Include="K4os.Compression.LZ4.Streams">
<Version>1.3.5</Version>
</PackageReference>
Expand Down
4 changes: 2 additions & 2 deletions FreeMote.Plugins/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
[assembly: Guid("f37472b9-6501-440e-8898-7774304f7fec")]

// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("3.6.1.0")]
[assembly: AssemblyFileVersion("3.6.0.0")]
[assembly: AssemblyVersion("3.7.0.0")]
[assembly: AssemblyFileVersion("3.7.0.0")]
24 changes: 12 additions & 12 deletions FreeMote.Plugins/Shells/MdfShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
var header = new byte[4];
var pos = stream.Position;
stream.Read(header, 0, 4);
_ = stream.Read(header, 0, 4);
stream.Position = pos;
if (header.SequenceEqual(Signature))
{
Expand All @@ -42,13 +42,13 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
int size = 0;
if (context != null)
{
if (context.ContainsKey(Context_MdfKey))
if (context.TryGetValue(Context_MdfKey, out var mdfKey))
{
uint? keyLength = context.ContainsKey(Context_MdfKeyLength)
? Convert.ToUInt32(context[Context_MdfKeyLength])
uint? keyLength = context.TryGetValue(Context_MdfKeyLength, out var kl)
? Convert.ToUInt32(kl)
: (uint?) null;

stream = PsbExtension.EncodeMdf(stream, (string) context[Context_MdfKey], keyLength);
stream = PsbExtension.EncodeMdf(stream, (string) mdfKey, keyLength, true);
stream.Position = 0; //A new MemoryStream
}

Expand All @@ -72,26 +72,26 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
public MemoryStream ToShell(Stream stream, Dictionary<string, object> context = null)
{
bool fast = true; //mdf use fast mode by default
if (context != null && context.ContainsKey(Context_PsbZlibFastCompress))
if (context != null && context.TryGetValue(Context_PsbZlibFastCompress, out var fastCompress))
{
fast = (bool) context[Context_PsbZlibFastCompress];
fast = (bool) fastCompress;
}

var ms = MPack.CompressPsbToMdfStream(stream, fast);
var ms = MPack.CompressPsbToMdfStream(stream, fast); //this will prepend MDF header

if (context != null && context.ContainsKey(Context_MdfKey))
if (context != null && context.TryGetValue(Context_MdfKey, out var mdfKey))
{
uint? keyLength;
if (context.ContainsKey(Context_MdfKeyLength))
if (context.TryGetValue(Context_MdfKeyLength, out var kl))
{
keyLength = Convert.ToUInt32(context[Context_MdfKeyLength]);
keyLength = Convert.ToUInt32(kl);
}
else
{
keyLength = (uint?) null;
}

var mms = PsbExtension.EncodeMdf(ms, (string)context[Context_MdfKey], keyLength);
var mms = PsbExtension.EncodeMdf(ms, (string)mdfKey, keyLength, true);
ms?.Dispose(); //ms disposed
ms = mms;
}
Expand Down
99 changes: 99 additions & 0 deletions FreeMote.Plugins/Shells/MxbShell.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using FreeMote.Psb;
using System;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using XMemCompress;
using static FreeMote.Consts;

namespace FreeMote.Plugins.Shells
{
[Export(typeof(IPsbShell))]
[ExportMetadata("Name", "FreeMote.Mxb")]
[ExportMetadata("Author", "Ulysses")]
[ExportMetadata("Comment", "MXB (XMemCompress) support.")]
internal class MxbShell : IPsbShell
{
public string Name => "MXB";
public byte[] Signature { get; } = { (byte) 'm', (byte) 'x', (byte) 'b', 0 };

public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
var header = new byte[4];
var pos = stream.Position;
_ = stream.Read(header, 0, 4);
stream.Position = pos;
if (header.SequenceEqual(Signature))
{
if (context != null)
{
context[Context_PsbShellType] = Name;
}

return true;
}

return false;
}

public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = null)
{
if (context != null)
{
if (context.TryGetValue(Context_MdfKey, out var mdfKey))
{
uint? keyLength = context.TryGetValue(Context_MdfKeyLength, out var kl)
? Convert.ToUInt32(kl)
: (uint?) null;

stream = PsbExtension.EncodeMdf(stream, (string) mdfKey, keyLength, true);
stream.Position = 0; //A new MemoryStream
}
}

stream.Seek(4, SeekOrigin.Current);
var bytes = new byte[4];
_ = stream.Read(bytes, 0, 4);
var unzippedSize = BitConverter.ToInt32(bytes, 0);

var ms = new MemoryStream(unzippedSize);
XCompressFile.DecompressStream(stream, ms);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}

public MemoryStream ToShell(Stream stream, Dictionary<string, object> context = null)
{
var unzipLength = (int)stream.Length;
var ms = XCompressFile.CompressStream(stream);
if (context != null && context.TryGetValue(Context_MdfKey, out var mdfKey))
{
uint? keyLength;
if (context.TryGetValue(Context_MdfKeyLength, out var kl))
{
keyLength = Convert.ToUInt32(kl);
}
else
{
keyLength = (uint?) null;
}

var mms = PsbExtension.EncodeMdf(ms, (string) mdfKey, keyLength, false);
ms?.Dispose(); //ms disposed
ms = mms;
}

ms.Seek(0, SeekOrigin.Begin);
var shellMs = new MemoryStream((int) (ms.Length + 8));
shellMs.Write(Signature, 0, 4);
shellMs.Write(BitConverter.GetBytes(unzipLength), 0, 4);
ms.CopyTo(shellMs);
ms.Dispose();
shellMs.Seek(0, SeekOrigin.Begin);
return shellMs;
}
}
}
31 changes: 19 additions & 12 deletions FreeMote.Plugins/Shells/MzsShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
var header = new byte[4];
var pos = stream.Position;
stream.Read(header, 0, 4);
_ = stream.Read(header, 0, 4);
stream.Position = pos;
if (header.SequenceEqual(Signature))
{
Expand All @@ -44,13 +44,13 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
{
if (context != null)
{
if (context.ContainsKey(Context_MdfKey))
if (context.TryGetValue(Context_MdfKey, out var mdfKey))
{
uint? keyLength = context.ContainsKey(Context_MdfKeyLength)
? Convert.ToUInt32(context[Context_MdfKeyLength])
uint? keyLength = context.TryGetValue(Context_MdfKeyLength, out var kl)
? Convert.ToUInt32(kl)
: (uint?)null;

stream = PsbExtension.EncodeMdf(stream, (string)context[Context_MdfKey], keyLength);
stream = PsbExtension.EncodeMdf(stream, (string)mdfKey, keyLength, true);
stream.Position = 0; //A new MemoryStream
}
}
Expand All @@ -72,10 +72,11 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu

public MemoryStream ToShell(Stream stream, Dictionary<string, object> context = null)
{
var unzipLength = (int) stream.Length;
int? compressLevel = null;
if (context != null && context.ContainsKey(Context_PsbZStdCompressLevel))
if (context != null && context.TryGetValue(Context_PsbZStdCompressLevel, out var cl))
{
compressLevel = (int) context[Context_PsbZStdCompressLevel];
compressLevel = (int) cl;
if (compressLevel > CompressionOptions.MaxCompressionLevel)
{
compressLevel = CompressionOptions.MaxCompressionLevel;
Expand All @@ -101,24 +102,30 @@ public MemoryStream ToShell(Stream stream, Dictionary<string, object> context =
var output = compress.Wrap(input);
var ms = new MemoryStream(output);

if (context != null && context.ContainsKey(Context_MdfKey))
if (context != null && context.TryGetValue(Context_MdfKey, out var mdfKey))
{
uint? keyLength;
if (context.ContainsKey(Context_MdfKeyLength))
if (context.TryGetValue(Context_MdfKeyLength, out var kl))
{
keyLength = Convert.ToUInt32(context[Context_MdfKeyLength]);
keyLength = Convert.ToUInt32(kl);
}
else
{
keyLength = (uint?)null;
}

var mms = PsbExtension.EncodeMdf(ms, (string)context[Context_MdfKey], keyLength);
var mms = PsbExtension.EncodeMdf(ms, (string)mdfKey, keyLength, false);
ms?.Dispose(); //ms disposed
ms = mms;
}

return ms;
var shellMs = new MemoryStream((int) (ms.Length + 8));
shellMs.Write(Signature, 0, 4);
shellMs.Write(BitConverter.GetBytes(unzipLength), 0, 4);
ms.CopyTo(shellMs);
ms.Dispose();
shellMs.Seek(0, SeekOrigin.Begin);
return shellMs;
}

}
Expand Down
9 changes: 5 additions & 4 deletions FreeMote.Plugins/Shells/PsdShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,11 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
if (xmp is XmpResource xmpRes)
{
var type = xmpRes.Name.ToLowerInvariant();
//if (type != PsdTypePimg)
//{
// return null;
//}
if (type != PsdTypePimg)
{
Logger.LogWarn("Only pimg PSD can be converted to PSB.");
//return null;
}
}

var layers = new PsbList();
Expand Down
2 changes: 1 addition & 1 deletion FreeMote.Plugins/Shells/PspShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
var header = new byte[3];
var pos = stream.Position;
stream.Seek(5, SeekOrigin.Current);
stream.Read(header, 0, 3);
_ = stream.Read(header, 0, 3);
stream.Position = pos;
if (header.SequenceEqual(MAGIC))
{
Expand Down
12 changes: 6 additions & 6 deletions FreeMote.Plugins/Shells/PszShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
var header = new byte[4];
var pos = stream.Position;
stream.Read(header, 0, 4);
_ = stream.Read(header, 0, 4);
stream.Position = pos;
if (header[0] == Name[0] && header[1] == Name[1] && header[2] == Name[2] && header[3] == 0)
{
Expand Down Expand Up @@ -53,9 +53,9 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
public MemoryStream ToShell(Stream stream, Dictionary<string, object> context = null)
{
bool fast = false;
if (context != null && context.ContainsKey(Consts.Context_PsbZlibFastCompress))
if (context != null && context.TryGetValue(Consts.Context_PsbZlibFastCompress, out var fastCompress))
{
fast = (bool)context[Consts.Context_PsbZlibFastCompress];
fast = (bool)fastCompress;
}

var oriLen = (int)stream.Length;
Expand All @@ -65,9 +65,9 @@ public MemoryStream ToShell(Stream stream, Dictionary<string, object> context =
using (var bw = new BinaryWriter(ms, Encoding.UTF8, true))
{
stream.Position = pos;
Adler32 checksumer = new Adler32();
checksumer.Update(stream);
var checksum = (uint)checksumer.Checksum;
var adler32 = new Adler32();
adler32.Update(stream);
var checksum = (uint)adler32.Checksum;

bw.Write(Signature);
bw.Write((int)compressedStream.Length + 4);
Expand Down
Loading

0 comments on commit ae1e7a4

Please sign in to comment.