19 static extern bool Compress(IntPtr compressor,
byte[] input, UIntPtr inputBytes,
byte[] output, UIntPtr outputBytes, out UIntPtr written);
25 long inputTotal=0, outputTotal=0;
26 foreach(var file
in Files)
28 IntPtr compressor=IntPtr.Zero;
31 var raw=File.ReadAllBytes(file.ItemSpec);
32 if(raw.Length<4 || raw.Length>16*1024*1024 || raw[0]!=
'D' || raw[1]!=
'X' || raw[2]!=
'B' || raw[3]!=
'C')
33 throw new InvalidDataException(
"Expected bounded DXBC bytecode: "+file.ItemSpec);
34 if(!
CreateCompressor(4,IntPtr.Zero,out compressor))
throw new System.ComponentModel.Win32Exception();
36 if(
Compress(compressor,raw,(UIntPtr)raw.Length,
null,UIntPtr.Zero,out required) || Marshal.GetLastWin32Error()!=122)
37 throw new System.ComponentModel.Win32Exception();
38 var compressed=
new byte[checked((
int)required.ToUInt64())];
40 if(!
Compress(compressor,raw,(UIntPtr)raw.Length,compressed,(UIntPtr)compressed.Length,out written))
41 throw new System.ComponentModel.Win32Exception();
43 int length=checked((
int)written.ToUInt64());
44 if(length+8<raw.Length)
46 packed=
new byte[length+8];packed[0]=(byte)
'R';packed[1]=(byte)
'Z';packed[2]=(byte)
'1';packed[3]=(byte)
'1';
47 Buffer.BlockCopy(BitConverter.GetBytes(raw.Length),0,packed,4,4);
48 Buffer.BlockCopy(compressed,0,packed,8,length);
50 var output=Path.ChangeExtension(file.ItemSpec,
".csz");
51 if(!File.Exists(output) || !File.ReadAllBytes(output).SequenceEqual(packed))File.WriteAllBytes(output,packed);
52 inputTotal+=raw.Length;outputTotal+=packed.Length;
54 catch(Exception error){Log.LogErrorFromException(error,
true);
return false;}
62 var encoding=
new UTF8Encoding(
true);
63 var resource=encoding.GetPreamble().Concat(encoding.GetBytes(String.Join(
"\r\n",
ResourceLines)+
"\r\n")).ToArray();
66 Log.LogMessage(MessageImportance.High,
"Packed {0} shader resources: {1:N0} -> {2:N0} bytes (lossless)",
Files.Length,inputTotal,outputTotal);