-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce94cee
commit 9826116
Showing
6 changed files
with
170 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 25 additions & 18 deletions
43
Assets/Script/Serialization/Xbox/XboxCONInnerFileRetriever.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,34 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
using System.Threading; | ||
using Cysharp.Threading.Tasks; | ||
|
||
namespace YARG.Serialization { | ||
public static class XboxCONInnerFileRetriever { | ||
public static byte[] RetrieveFile(string CONname, string filename, uint filesize, uint[] fileOffsets){ | ||
public static class XboxCONInnerFileRetriever { | ||
public static async UniTask<byte[]> RetrieveFile(string location, uint filesize, uint[] fileOffsets, CancellationToken? ct) { | ||
byte[] f = new byte[filesize]; | ||
|
||
byte[] f = new byte[filesize]; | ||
uint lastSize = filesize % 0x1000; | ||
await UniTask.RunOnThreadPool(() => { | ||
uint lastSize = filesize % 0x1000; | ||
|
||
Parallel.For(0, fileOffsets.Length, i => { | ||
uint ReadLen = (i == fileOffsets.Length - 1) ? lastSize : 0x1000; | ||
using var fs = new FileStream(CONname, FileMode.Open, FileAccess.Read); | ||
using var br = new BinaryReader(fs, new ASCIIEncoding()); | ||
fs.Seek(fileOffsets[i], SeekOrigin.Begin); | ||
Array.Copy(br.ReadBytes((int)ReadLen), 0, f, i*0x1000, (int)ReadLen); | ||
}); | ||
for (int i = 0; i < fileOffsets.Length; i++) { | ||
ct?.ThrowIfCancellationRequested(); | ||
|
||
return f; | ||
} | ||
} | ||
// TODO: This is unoptimized | ||
uint ReadLen = (i == fileOffsets.Length - 1) ? lastSize : 0x1000; | ||
using var fs = new FileStream(location, FileMode.Open, FileAccess.Read); | ||
using var br = new BinaryReader(fs, new ASCIIEncoding()); | ||
fs.Seek(fileOffsets[i], SeekOrigin.Begin); | ||
Array.Copy(br.ReadBytes((int) ReadLen), 0, f, i * 0x1000, (int) ReadLen); | ||
} | ||
}); | ||
|
||
return f; | ||
} | ||
|
||
public static byte[] RetrieveFile(string location, uint filesize, uint[] fileOffsets) { | ||
return RetrieveFile(location, filesize, fileOffsets, null).AsTask().Result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,61 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Threading; | ||
using Cysharp.Threading.Tasks; | ||
using UnityEngine; | ||
using UnityEngine.Experimental.Rendering; | ||
|
||
namespace YARG.Serialization { | ||
public static class XboxImageTextureGenerator { | ||
public static Texture2D GetTexture(byte[] xboxImageBytes){ | ||
var ms = new MemoryStream(xboxImageBytes); | ||
|
||
// Parse header | ||
byte[] header = ms.ReadBytes(32); | ||
byte BitsPerPixel = header[1]; | ||
int Format = BitConverter.ToInt32(header, 2); | ||
short Width = BitConverter.ToInt16(header, 7); | ||
short Height = BitConverter.ToInt16(header, 9); | ||
byte[] DXTBlocks; | ||
|
||
// Parse DXT-compressed blocks, depending on format | ||
if ((BitsPerPixel == 0x04) && (Format == 0x08)) { | ||
// If DXT-1 format already, read the bytes straight up | ||
ms.Seek(32, SeekOrigin.Begin); | ||
DXTBlocks = ms.ReadBytes((int) (ms.Length - 32)); | ||
} else { | ||
// If DXT-3 format, we have to omit the alpha bytes | ||
List<byte> extractedDXT3 = new List<byte>(); | ||
ms.ReadBytes(8); //skip the first 8 alpha bytes | ||
for (int i = 8; i < (ms.Length - 32) / 2; i += 8) { | ||
extractedDXT3.AddRange(ms.ReadBytes(8)); // We want to read these 8 bytes | ||
ms.ReadBytes(8); // and skip these 8 bytes | ||
} | ||
DXTBlocks = extractedDXT3.ToArray(); | ||
} | ||
|
||
// Swap bytes because xbox is weird like that | ||
for(int i = 0; i < DXTBlocks.Length / 2; i++) | ||
(DXTBlocks[i * 2], DXTBlocks[i * 2 + 1]) = (DXTBlocks[i * 2 + 1], DXTBlocks[i * 2]); | ||
|
||
// apply DXT1 formatted bytes to a Texture2D | ||
var tex = new Texture2D(Width, Height, GraphicsFormat.RGBA_DXT1_SRGB, TextureCreationFlags.None); | ||
tex.LoadRawTextureData(DXTBlocks); | ||
tex.Apply(); | ||
|
||
return tex; | ||
} | ||
} | ||
public static class XboxImageTextureGenerator { | ||
public static async UniTask<Texture2D> GetTexture(byte[] xboxImageBytes, CancellationToken ct) { | ||
var ms = new MemoryStream(xboxImageBytes); | ||
|
||
// Parse header | ||
byte[] header = ms.ReadBytes(32); | ||
byte BitsPerPixel = header[1]; | ||
int Format = BitConverter.ToInt32(header, 2); | ||
short Width = BitConverter.ToInt16(header, 7); | ||
short Height = BitConverter.ToInt16(header, 9); | ||
byte[] DXTBlocks = null; | ||
|
||
ct.ThrowIfCancellationRequested(); | ||
|
||
await UniTask.RunOnThreadPool(() => { | ||
// Parse DXT-compressed blocks, depending on format | ||
if ((BitsPerPixel == 0x04) && (Format == 0x08)) { | ||
// If DXT-1 format already, read the bytes straight up | ||
ms.Seek(32, SeekOrigin.Begin); | ||
DXTBlocks = ms.ReadBytes((int) (ms.Length - 32)); | ||
} else { | ||
// If DXT-3 format, we have to omit the alpha bytes | ||
var extractedDXT3 = new List<byte>(); | ||
ms.ReadBytes(8); //skip the first 8 alpha bytes | ||
for (int i = 8; i < (ms.Length - 32) / 2; i += 8) { | ||
ct.ThrowIfCancellationRequested(); | ||
|
||
extractedDXT3.AddRange(ms.ReadBytes(8)); // We want to read these 8 bytes | ||
ms.ReadBytes(8); // and skip these 8 bytes | ||
} | ||
DXTBlocks = extractedDXT3.ToArray(); | ||
} | ||
|
||
// Swap bytes because xbox is weird like that | ||
for (int i = 0; i < DXTBlocks.Length / 2; i++) { | ||
ct.ThrowIfCancellationRequested(); | ||
|
||
(DXTBlocks[i * 2], DXTBlocks[i * 2 + 1]) = (DXTBlocks[i * 2 + 1], DXTBlocks[i * 2]); | ||
} | ||
}); | ||
|
||
ct.ThrowIfCancellationRequested(); | ||
|
||
// apply DXT1 formatted bytes to a Texture2D | ||
var tex = new Texture2D(Width, Height, GraphicsFormat.RGBA_DXT1_SRGB, TextureCreationFlags.None); | ||
tex.LoadRawTextureData(DXTBlocks); | ||
tex.Apply(); | ||
|
||
return tex; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.