2020-11-14 19:51:16 +11:00
|
|
|
|
using System;
|
2020-10-10 20:19:56 +11:00
|
|
|
|
using System.IO;
|
2020-11-03 20:59:13 +11:00
|
|
|
|
using UnityExplorer.Helpers;
|
2020-10-28 06:39:26 +11:00
|
|
|
|
using UnityEngine;
|
2020-11-14 19:51:16 +11:00
|
|
|
|
#if CPP
|
|
|
|
|
using UnhollowerBaseLib;
|
|
|
|
|
#endif
|
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
|
|
|
|
|
{
|
2020-12-03 22:12:30 +11:00
|
|
|
|
// LoadImage helper from a filepath
|
|
|
|
|
|
|
|
|
|
public static bool LoadImage(Texture2D tex, string filePath, bool markNonReadable)
|
|
|
|
|
{
|
|
|
|
|
if (!File.Exists(filePath))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return tex.LoadImage(File.ReadAllBytes(filePath), markNonReadable);
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-14 19:51:16 +11:00
|
|
|
|
#if CPP
|
2020-10-10 20:19:56 +11:00
|
|
|
|
// 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-12-03 22:12:30 +11:00
|
|
|
|
var iCall = ICallHelper.GetICall<d_EncodeToPNG>("UnityEngine.ImageConversion::EncodeToPNG");
|
|
|
|
|
|
|
|
|
|
IntPtr ptr = iCall.Invoke(tex.Pointer);
|
|
|
|
|
|
|
|
|
|
if (ptr == IntPtr.Zero)
|
|
|
|
|
return null;
|
2020-10-18 04:39:50 +11:00
|
|
|
|
|
2020-11-09 16:43:19 +11:00
|
|
|
|
return new Il2CppStructArray<byte>(ptr);
|
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-12-03 22:12:30 +11:00
|
|
|
|
// Sprite Sprite.Create
|
2020-10-10 20:19:56 +11:00
|
|
|
|
|
2020-12-03 22:12:30 +11:00
|
|
|
|
internal delegate IntPtr d_CreateSprite(IntPtr texture, ref Rect rect, ref Vector2 pivot, float pixelsPerUnit,
|
|
|
|
|
uint extrude, int meshType, ref Vector4 border, bool generateFallbackPhysicsShape);
|
|
|
|
|
|
|
|
|
|
public static Sprite CreateSprite(Texture texture, Rect rect, Vector2 pivot, float pixelsPerUnit, uint extrude, Vector4 border)
|
2020-10-10 20:19:56 +11:00
|
|
|
|
{
|
2020-12-03 22:12:30 +11:00
|
|
|
|
var iCall = ICallHelper.GetICall<d_CreateSprite>("UnityEngine.Sprite::CreateSprite_Injected");
|
2020-10-10 20:19:56 +11:00
|
|
|
|
|
2020-12-03 22:12:30 +11:00
|
|
|
|
var ptr = iCall.Invoke(texture.Pointer, ref rect, ref pivot, pixelsPerUnit, extrude, 1, ref border, false);
|
|
|
|
|
|
|
|
|
|
if (ptr == IntPtr.Zero)
|
|
|
|
|
return null;
|
|
|
|
|
else
|
|
|
|
|
return new Sprite(ptr);
|
2020-10-10 20:19:56 +11:00
|
|
|
|
}
|
2020-12-03 22:12:30 +11:00
|
|
|
|
#endif
|
2020-10-10 20:19:56 +11:00
|
|
|
|
}
|
2020-11-14 19:51:16 +11:00
|
|
|
|
}
|