Skip to content

Commit

Permalink
Add pack support for info.psb. Need more test
Browse files Browse the repository at this point in the history
  • Loading branch information
UlyssesWu committed Jul 6, 2019
1 parent dfc5f6a commit 56bedcd
Show file tree
Hide file tree
Showing 22 changed files with 785 additions and 322 deletions.
2 changes: 1 addition & 1 deletion FreeMote.Plugins/Shells/Lz4Shell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
if (context != null)
{
context[Consts.PsbShellType] = Name;
context[Consts.Context_PsbShellType] = Name;
}
return true;
}
Expand Down
37 changes: 24 additions & 13 deletions FreeMote.Plugins/Shells/MdfShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace FreeMote.Plugins
class MdfShell : IPsbShell
{
public string Name => "MDF";

public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
var header = new byte[4];
Expand All @@ -28,7 +28,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
if (context != null)
{
context[PsbShellType] = Name;
context[Context_PsbShellType] = Name;
}

return true;
Expand All @@ -43,13 +43,15 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
{
if (context.ContainsKey(Context_MdfKey))
{
uint? keyLength = context.ContainsKey(Context_MdfKeyLength) ? (uint) context[Context_MdfKeyLength] : (uint?) null;
stream = EncodeMdf(stream, (string)context[Context_MdfKey], keyLength);
uint? keyLength = context.ContainsKey(Context_MdfKeyLength)
? Convert.ToUInt32(context[Context_MdfKeyLength])
: (uint?) null;
stream = EncodeMdf(stream, (string) context[Context_MdfKey], keyLength);
}

var pos = stream.Position;
stream.Seek(9, SeekOrigin.Current);
context[PsbZlibFastCompress] = stream.ReadByte() == (byte) 0x9C;
context[Context_PsbZlibFastCompress] = stream.ReadByte() == (byte) 0x9C;
stream.Position = pos;
}

Expand All @@ -76,10 +78,11 @@ private MemoryStream EncodeMdf(Stream stream, string key, uint? keyLength)
List<byte> keys = new List<byte>();
if (keyLength != null)
{
for (int i = 0; i < keyLength/4 + 1; i++)
for (int i = 0; i < keyLength / 4 + 1; i++)
{
keys.AddRange(BitConverter.GetBytes(gen.NextUIntInclusiveMaxValue()));
}

keys = keys.Take((int) keyLength.Value).ToList();
}
else
Expand All @@ -88,7 +91,6 @@ private MemoryStream EncodeMdf(Stream stream, string key, uint? keyLength)
{
keys.AddRange(BitConverter.GetBytes(gen.NextUIntInclusiveMaxValue()));
}

}

int currentKey = 0;
Expand Down Expand Up @@ -116,17 +118,26 @@ private MemoryStream EncodeMdf(Stream stream, string key, uint? keyLength)
public MemoryStream ToShell(Stream stream, Dictionary<string, object> context = null)
{
bool fast = true; //mdf use fast mode by default
if (context != null && context.ContainsKey(PsbZlibFastCompress))
if (context != null && context.ContainsKey(Context_PsbZlibFastCompress))
{
fast = (bool) context[PsbZlibFastCompress];
fast = (bool) context[Context_PsbZlibFastCompress];
}

var ms = MdfFile.CompressPsbToMdfStream(stream, fast) as MemoryStream;

if (context != null && context.ContainsKey(Context_MdfKey))
{
uint? keyLength = context.ContainsKey(Context_MdfKeyLength) ? (uint)context[Context_MdfKeyLength] : (uint?)null;
ms = EncodeMdf(ms, (string)context[Context_MdfKey], keyLength);
uint? keyLength;
if (context.ContainsKey(Context_MdfKeyLength))
{
keyLength = Convert.ToUInt32(context[Context_MdfKeyLength]);
}
else
{
keyLength = (uint?) null;
}

ms = EncodeMdf(ms, (string) context[Context_MdfKey], keyLength);
}

return ms;
Expand Down
2 changes: 1 addition & 1 deletion FreeMote.Plugins/Shells/PsdShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
if (context != null)
{
context[Consts.PsbShellType] = Name;
context[Consts.Context_PsbShellType] = Name;
}
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions FreeMote.Plugins/Shells/PszShell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public bool IsInShell(Stream stream, Dictionary<string, object> context = null)
{
if (context != null)
{
context[Consts.PsbShellType] = Name;
context[Consts.Context_PsbShellType] = Name;
}
return true;
}
Expand All @@ -43,7 +43,7 @@ public MemoryStream ToPsb(Stream stream, Dictionary<string, object> context = nu
var config = br.ReadByte(); //0x9C: fast; 0xDA: compact
if (context != null)
{
context[Consts.PsbZlibFastCompress] = config == (byte)0x9C;
context[Consts.Context_PsbZlibFastCompress] = config == (byte)0x9C;
}

return ZlibCompress.DecompressToStream(stream) as MemoryStream;
Expand All @@ -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.PsbZlibFastCompress))
if (context != null && context.ContainsKey(Consts.Context_PsbZlibFastCompress))
{
fast = (bool)context[Consts.PsbZlibFastCompress];
fast = (bool)context[Consts.Context_PsbZlibFastCompress];
}

var oriLen = (int)stream.Length;
Expand Down
36 changes: 29 additions & 7 deletions FreeMote.PsBuild/PsbCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public static void CompileToFile(string inputPath, string outputPath, string inp
break;
}

if (resx.Context != null && resx.Context.ContainsKey(Consts.PsbShellType) && keepShell)
if (resx.Context != null && resx.Context.ContainsKey(Consts.Context_PsbShellType) && keepShell)
{
var shellType = resx.Context[Consts.PsbShellType] as string;
var shellType = resx.Context[Consts.Context_PsbShellType] as string;
if (!string.IsNullOrEmpty(shellType) && shellType.ToUpperInvariant() != "PSB")
{
ext += $".{shellType.ToLowerInvariant()}";
Expand Down Expand Up @@ -169,7 +169,9 @@ public static byte[] Compile(string inputJson, string inputResJson, string baseD

if (resx.ExternalTextures)
{
#if DEBUG
Console.WriteLine("[INFO] External Texture mode ON, no resource will be compiled.");
#endif
}
else
{
Expand All @@ -192,8 +194,8 @@ public static byte[] Compile(string inputJson, string inputResJson, string baseD
}

var bytes = psb.Build();
//Convert

//Convert
if (cryptKey != null)
{
return PsbFile.EncodeToBytes(cryptKey.Value, bytes, EncodeMode.Encrypt, EncodePosition.Auto);
Expand All @@ -208,13 +210,15 @@ public static byte[] Compile(string inputJson, string inputResJson, string baseD
}

/// <summary>
/// Load PSB From Json file
/// Load PSB and Context From Json file, use <see cref="LoadPsbFromJsonFile"/> if you don't need context
/// </summary>
/// <param name="inputPath">Json file path</param>
/// <param name="inputResPath">Resource Json file</param>
/// <param name="version">PSB version</param>
/// <returns></returns>
public static PSB LoadPsbFromJsonFile(string inputPath, string inputResPath = null, ushort? version = null)
public static (PSB Psb, Dictionary<string, object> Context) LoadPsbAndContextFromJsonFile(string inputPath,
string inputResPath = null,
ushort? version = null)
{
if (string.IsNullOrEmpty(inputPath))
{
Expand All @@ -241,11 +245,13 @@ public static PSB LoadPsbFromJsonFile(string inputPath, string inputResPath = nu
//Parse
PSB psb = Parse(File.ReadAllText(inputPath), version ?? 3);
//Link
Dictionary<string, object> context = null;
if (!string.IsNullOrWhiteSpace(inputResJson))
{
if (inputResJson.Trim().StartsWith("{")) //resx.json
{
PsbResourceJson resx = JsonConvert.DeserializeObject<PsbResourceJson>(inputResJson);
context = resx.Context;
if (resx.PsbType != null)
{
psb.Type = resx.PsbType.Value;
Expand All @@ -258,7 +264,9 @@ public static PSB LoadPsbFromJsonFile(string inputPath, string inputResPath = nu

if (resx.ExternalTextures)
{
#if DEBUG
Console.WriteLine("[INFO] External Texture mode ON, no resource will be compiled.");
#endif
}
else
{
Expand All @@ -283,7 +291,19 @@ public static PSB LoadPsbFromJsonFile(string inputPath, string inputResPath = nu
}

psb.Merge();
return psb;
return (psb, context);
}

/// <summary>
/// Load PSB From Json file
/// </summary>
/// <param name="inputPath">Json file path</param>
/// <param name="inputResPath">Resource Json file</param>
/// <param name="version">PSB version</param>
/// <returns></returns>
public static PSB LoadPsbFromJsonFile(string inputPath, string inputResPath = null, ushort? version = null)
{
return LoadPsbAndContextFromJsonFile(inputPath, inputResPath, version).Psb;
}

internal static PSB Parse(string json, ushort version)
Expand Down Expand Up @@ -417,6 +437,7 @@ public static void Link(this PSB psb, IList<string> resPaths, string baseDir = n
{
psb.MotionResourceInstrument();
}

var resList = psb.CollectResources();
var context = FreeMount.CreateContext();
if (order == PsbLinkOrderBy.Order)
Expand All @@ -428,6 +449,7 @@ public static void Link(this PSB psb, IList<string> resPaths, string baseDir = n
byte[] data = LoadImageBytes(fullPath, resMd, context);
resMd.Resource.Data = data;
}

return;
}

Expand Down Expand Up @@ -481,7 +503,7 @@ public static void Link(this PSB psb, IList<string> resPaths, string baseDir = n
continue;
}

resMd = resList[(int)texIdx.Value];
resMd = resList[(int) texIdx.Value];
}
}
else //if (order == PsbLinkOrderBy.Convention)
Expand Down
12 changes: 6 additions & 6 deletions FreeMote.PsBuild/PsbDecompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static string Decompile(string path, out PSB psb, Dictionary<string, obje
var ms = ctx.OpenFromShell(fs, ref type);
if (ms != null)
{
ctx.Context[Consts.PsbShellType] = type;
ctx.Context[Consts.Context_PsbShellType] = type;
fs.Dispose();
stream = ms;
}
Expand All @@ -60,9 +60,9 @@ public static string Decompile(string path, out PSB psb, Dictionary<string, obje
{
stream.Position = 0;
uint? key = null;
if (ctx.Context.ContainsKey(Consts.CryptKey))
if (ctx.Context.ContainsKey(Consts.Context_CryptKey))
{
key = ctx.Context[Consts.CryptKey] as uint?;
key = ctx.Context[Consts.Context_CryptKey] as uint?;
}
else
{
Expand All @@ -79,7 +79,7 @@ public static string Decompile(string path, out PSB psb, Dictionary<string, obje
PsbFile.Encode(key.Value, EncodeMode.Decrypt, EncodePosition.Auto, stream, mms);
stream.Dispose();
psb = new PSB(mms);
ctx.Context[Consts.CryptKey] = key;
ctx.Context[Consts.Context_CryptKey] = key;
}
}
catch
Expand Down Expand Up @@ -313,7 +313,7 @@ public static void DecompileToFile(PSB psb, string outputPath, Dictionary<string
var context = FreeMount.CreateContext(additionalContext);
if (key != null)
{
context.Context[Consts.CryptKey] = key;
context.Context[Consts.Context_CryptKey] = key;
}

File.WriteAllText(outputPath, Decompile(psb)); //MARK: breaking change for json path
Expand All @@ -335,7 +335,7 @@ public static void DecompileToFile(string inputPath, PsbImageOption imageOption
var context = FreeMount.CreateContext();
if (key != null)
{
context.Context[Consts.CryptKey] = key;
context.Context[Consts.Context_CryptKey] = key;
}

File.WriteAllText(Path.ChangeExtension(inputPath, ".json"),
Expand Down
22 changes: 20 additions & 2 deletions FreeMote.PsBuild/PsbResourceJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,31 @@ public PsbResourceJson(PSB psb, Dictionary<string, object> context = null)

if (context != null)
{
CryptKey = context.ContainsKey(Consts.CryptKey)
? (uint?) context[Consts.CryptKey]
CryptKey = context.ContainsKey(Consts.Context_CryptKey)
? (uint?) context[Consts.Context_CryptKey]
: null;
Context = context;
}
}

public void AppliedToPsb(PSB psb)
{
if (PsbType != null)
{
psb.Type = PsbType.Value;
}

if (PsbVersion != null)
{
psb.Header.Version = PsbVersion.Value;
}

if (Platform != null && psb.Platform != PsbSpec.none)
{
psb.Platform = Platform.Value;
}
}

public string SerializeToJson()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
Expand Down
29 changes: 16 additions & 13 deletions FreeMote.PsBuild/PsbSpecConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ public static class PsbSpecConverter
/// <param name="psb"></param>
/// <param name="targetSpec"></param>
/// <param name="pixelFormat"></param>
public static void SwitchSpec(this PSB psb, PsbSpec targetSpec, PsbPixelFormat pixelFormat = PsbPixelFormat.None)
public static void SwitchSpec(this PSB psb, PsbSpec targetSpec,
PsbPixelFormat pixelFormat = PsbPixelFormat.None)
{
if (targetSpec == PsbSpec.other)
if (targetSpec == PsbSpec.other || targetSpec == PsbSpec.none)
{
return;
}
Expand All @@ -40,17 +41,19 @@ public static void SwitchSpec(this PSB psb, PsbSpec targetSpec, PsbPixelFormat p
switch (original)
{
case PsbSpec.win:
{
Common2KrkrConverter winKrkr = new Common2KrkrConverter();
winKrkr.Convert(psb);
break;
}
{
Common2KrkrConverter winKrkr = new Common2KrkrConverter();
winKrkr.Convert(psb);
break;
}

case PsbSpec.common:
{
Common2KrkrConverter commonKrkr = new Common2KrkrConverter();
commonKrkr.Convert(psb);
break;
}
{
Common2KrkrConverter commonKrkr = new Common2KrkrConverter();
commonKrkr.Convert(psb);
break;
}

default:
psb.Platform = targetSpec;
break;
Expand Down Expand Up @@ -108,4 +111,4 @@ public static void SwitchSpec(this PSB psb, PsbSpec targetSpec, PsbPixelFormat p
}
}
}
}
}
1 change: 0 additions & 1 deletion FreeMote.Psb/FreeMote.Psb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
</ItemGroup>
<ItemGroup>
<Compile Include="PSB.cs" />
<Compile Include="PsbFileExtension.cs" />
<Compile Include="PsbHelper.cs" />
<Compile Include="PsbPainter.cs" />
<Compile Include="PsbResCollector.cs" />
Expand Down
Loading

0 comments on commit 56bedcd

Please sign in to comment.