2020-10-10 20:19:56 +11:00
|
|
|
|
#if CPP
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2020-11-03 20:59:13 +11:00
|
|
|
|
using UnityExplorer.Helpers;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
using UnhollowerBaseLib;
|
|
|
|
|
using UnityEngine;
|
2020-10-10 20:19:56 +11:00
|
|
|
|
|
2020-11-09 16:43:19 +11:00
|
|
|
|
namespace UnityExplorer.Unstrip
|
2020-10-10 20:19:56 +11:00
|
|
|
|
{
|
|
|
|
|
public static class ImageConversionUnstrip
|
|
|
|
|
{
|
|
|
|
|
// byte[] ImageConversion.EncodeToPNG(this Texture2D image);
|
|
|
|
|
|
2020-11-09 16:43:19 +11:00
|
|
|
|
internal delegate IntPtr d_EncodeToPNG(IntPtr tex);
|
2020-10-16 19:40:01 +11:00
|
|
|
|
|
2020-10-10 20:19:56 +11:00
|
|
|
|
public static byte[] EncodeToPNG(this Texture2D tex)
|
|
|
|
|
{
|
2020-11-09 16:43:19 +11:00
|
|
|
|
IntPtr ptr = ICallHelper.GetICall<d_EncodeToPNG>("UnityEngine.ImageConversion::EncodeToPNG")
|
2020-10-16 19:40:01 +11:00
|
|
|
|
.Invoke(tex.Pointer);
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
2020-11-09 16:43:19 +11:00
|
|
|
|
return new Il2CppStructArray<byte>(ptr);
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
2020-11-09 16:43:19 +11:00
|
|
|
|
//// This is a bit of a hack. The iCall actually returns an Il2CppStructArray<byte>...
|
|
|
|
|
|
|
|
|
|
// byte[] data = ICallHelper.GetICall<d_EncodeToPNG>("UnityEngine.ImageConversion::EncodeToPNG")
|
|
|
|
|
// .Invoke(tex.Pointer);
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
2020-11-09 16:43:19 +11:00
|
|
|
|
//// However, if you try to use that result with for example File.WriteAllBytes, it won't work.
|
|
|
|
|
//// Simple fix: iterate into a new managed array.
|
|
|
|
|
|
|
|
|
|
//byte[] safeData = new byte[data.Length];
|
|
|
|
|
//for (int i = 0; i < data.Length; i++)
|
|
|
|
|
// safeData[i] = (byte)data[i];
|
|
|
|
|
|
|
|
|
|
//return safeData;
|
2020-10-10 20:19:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// bool ImageConversion.LoadImage(this Texture2D tex, byte[] data, bool markNonReadable);
|
|
|
|
|
|
2020-10-18 04:39:50 +11:00
|
|
|
|
internal delegate bool d_LoadImage(IntPtr tex, IntPtr data, bool markNonReadable);
|
2020-10-16 19:40:01 +11:00
|
|
|
|
|
2020-10-10 20:19:56 +11:00
|
|
|
|
public static bool LoadImage(this Texture2D tex, byte[] data, bool markNonReadable)
|
|
|
|
|
{
|
2020-10-28 06:39:26 +11:00
|
|
|
|
Il2CppStructArray<byte> il2cppArray = new Il2CppStructArray<byte>(data.Length);
|
2020-10-18 21:41:04 +11:00
|
|
|
|
for (int i = 0; i < data.Length; i++)
|
|
|
|
|
il2cppArray[i] = data[i];
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
bool ret = ICallHelper.GetICall<d_LoadImage>("UnityEngine.ImageConversion::LoadImage")
|
2020-10-18 21:41:04 +11:00
|
|
|
|
.Invoke(tex.Pointer, il2cppArray.Pointer, markNonReadable);
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
|
|
|
|
return ret;
|
2020-10-10 20:19:56 +11:00
|
|
|
|
}
|
|
|
|
|
|
2020-10-16 19:40:01 +11:00
|
|
|
|
// Helper for LoadImage from filepath
|
2020-10-10 20:19:56 +11:00
|
|
|
|
|
2020-10-16 19:40:01 +11:00
|
|
|
|
public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable)
|
2020-10-10 20:19:56 +11:00
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 06:39:26 +11:00
|
|
|
|
byte[] data = File.ReadAllBytes(filePath);
|
2020-10-10 20:19:56 +11:00
|
|
|
|
return tex.LoadImage(data, markNonReadable);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|