using System;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.UIElements;
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
namespace SwyfPhotoImport;
[BepInPlugin("be.mahyass.swyf.photoimport", "Photo Import", "0.2.0")]
public sealed class Plugin : BaseUnityPlugin
{
internal static ManualLogSource Log;
private void Awake()
{
//IL_0010: Unknown result type (might be due to invalid IL or missing references)
//IL_0016: Expected O, but got Unknown
Log = ((BaseUnityPlugin)this).Logger;
Harmony harmony = new Harmony("be.mahyass.swyf.photoimport");
Hook(harmony, "Kolkata.UI.KolkataOsCharacterCreatorApp, Assembly-CSharp", "BuildArtworkPanel", "AfterBuildArtworkPanel");
Hook(harmony, "Metater.RandomNpcSkinPreviewUi, ScriptsAssDef", "Initialize", "AfterEmployeeSetup");
}
private static void Hook(Harmony harmony, string typeName, string method, string postfix)
{
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0025: Expected O, but got Unknown
try
{
Type type = Type.GetType(typeName, throwOnError: true);
MethodInfo methodInfo = AccessTools.Method(type, method, (Type[])null, (Type[])null);
HarmonyMethod val = new HarmonyMethod(typeof(Plugin), postfix, (Type[])null);
harmony.Patch((MethodBase)methodInfo, (HarmonyMethod)null, val, (HarmonyMethod)null, (HarmonyMethod)null, (HarmonyMethod)null);
Log.LogInfo((object)("Hooked " + method));
}
catch (Exception ex)
{
Log.LogError((object)("Could not hook " + method + " (game updated?): " + ex));
}
}
private static void AfterEmployeeSetup(object __instance)
{
try
{
new NameTagTool(__instance).Attach();
}
catch (Exception ex)
{
Log.LogError((object)ex);
}
}
private static void AfterBuildArtworkPanel(object __instance, VisualElement __result)
{
try
{
new PhotoTool(__instance).Attach(__result);
}
catch (Exception ex)
{
Log.LogError((object)ex);
}
}
}
internal sealed class PhotoTool
{
private readonly object editor;
private VisualElement controls;
private VisualElement overlay;
private VisualElement handle;
private Button importButton;
private Image preview;
private SliderInt sizeSlider;
private Color32[] source;
private int sourceWidth;
private int sourceHeight;
private Texture2D sourceTexture;
private int canvasWidth;
private int canvasHeight;
private Rect photo;
private int pointer = -1;
private bool resizing;
private Vector2 dragStart;
private Rect rectStart;
internal PhotoTool(object editor)
{
this.editor = editor;
}
private T Field<T>(string name)
{
return (T)AccessTools.Field(editor.GetType(), name).GetValue(editor);
}
private object Call(string name, params object[] args)
{
return AccessTools.Method(editor.GetType(), name, (Type[])null, (Type[])null).Invoke(editor, args);
}
private void Status(string message)
{
Call("SetStatus", message);
}
internal void Attach(VisualElement panel)
{
//IL_0068: Unknown result type (might be due to invalid IL or missing references)
//IL_0072: Expected O, but got Unknown
//IL_007e: Unknown result type (might be due to invalid IL or missing references)
//IL_0095: Unknown result type (might be due to invalid IL or missing references)
//IL_009f: Expected O, but got Unknown
//IL_00c4: Unknown result type (might be due to invalid IL or missing references)
VisualElement val = UQueryExtensions.Q<VisualElement>(panel, (string)null, "character-creator__layers-panel");
if (val == null)
{
Plugin.Log.LogError((object)"Layers panel not found.");
return;
}
importButton = MakeButton("Import photo", Import);
((VisualElement)importButton).AddToClassList("character-creator__add-layer");
val.Insert(Math.Min(2, val.childCount), (VisualElement)(object)importButton);
controls = new VisualElement();
controls.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)1);
sizeSlider = new SliderInt(5, 300, (SliderDirection)0, 0f);
((BaseField<int>)(object)sizeSlider).label = "Size %";
((VisualElement)((BaseField<int>)(object)sizeSlider).labelElement).style.minWidth = StyleLength.op_Implicit(60f);
INotifyValueChangedExtensions.RegisterValueChangedCallback<int>((INotifyValueChanged<int>)(object)sizeSlider, (EventCallback<ChangeEvent<int>>)delegate(ChangeEvent<int> e)
{
ResizeTo(e.newValue);
});
controls.Add((VisualElement)(object)sizeSlider);
controls.Add((VisualElement)(object)MakeButton("Rotate 90°", Rotate));
controls.Add((VisualElement)(object)MakeButton("Confirm", Confirm));
controls.Add((VisualElement)(object)MakeButton("Cancel", Close));
val.Insert(val.IndexOf((VisualElement)(object)importButton) + 1, controls);
}
private static Button MakeButton(string text, Action action)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0007: Expected O, but got Unknown
Button val = new Button(action);
((TextElement)val).text = text;
((VisualElement)val).AddToClassList("character-creator__small-button");
return val;
}
private void Import()
{
//IL_008d: Unknown result type (might be due to invalid IL or missing references)
//IL_0093: Expected O, but got Unknown
//IL_00af: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Expected O, but got Unknown
//IL_0195: Unknown result type (might be due to invalid IL or missing references)
//IL_019a: Unknown result type (might be due to invalid IL or missing references)
try
{
Call("FinishStroke");
string text = FileDialog.OpenImage();
if (string.IsNullOrEmpty(text))
{
return;
}
if (new FileInfo(text).Length > 20971520)
{
Status("Photo too large (20 MB maximum).");
return;
}
VisualElement val = Field<VisualElement>("m_canvasSurface");
object obj = Field<object>("m_canvas");
if (val == null || obj == null)
{
return;
}
Texture2D val2 = (Texture2D)AccessTools.Property(obj.GetType(), "Texture").GetValue(obj, null);
canvasWidth = ((Texture)val2).width;
canvasHeight = ((Texture)val2).height;
Texture2D val3 = new Texture2D(2, 2, (TextureFormat)4, false);
if (!ImageConversion.LoadImage(val3, File.ReadAllBytes(text)))
{
Object.Destroy((Object)(object)val3);
Status("Unreadable image. Use a PNG or JPG file.");
return;
}
sourceWidth = ((Texture)val3).width;
sourceHeight = ((Texture)val3).height;
source = val3.GetPixels32();
Object.Destroy((Object)(object)val3);
while (Math.Max(sourceWidth, sourceHeight) > 1600)
{
Halve();
}
float num = Math.Min((float)canvasWidth / (float)sourceWidth, (float)canvasHeight / (float)sourceHeight);
float num2 = (float)sourceWidth * num;
float num3 = (float)sourceHeight * num;
photo = new Rect(((float)canvasWidth - num2) / 2f, ((float)canvasHeight - num3) / 2f, num2, num3);
Open(val);
}
catch (Exception ex)
{
Plugin.Log.LogError((object)ex);
Status("Photo import failed.");
Close();
}
}
private void Halve()
{
//IL_0058: Unknown result type (might be due to invalid IL or missing references)
//IL_005d: Unknown result type (might be due to invalid IL or missing references)
int num = Math.Max(1, sourceWidth / 2);
int num2 = Math.Max(1, sourceHeight / 2);
Color32[] array = (Color32[])(object)new Color32[num * num2];
for (int i = 0; i < num2; i++)
{
for (int j = 0; j < num; j++)
{
ref Color32 reference = ref array[i * num + j];
reference = Sample((float)(j * 2) + 0.5f, (float)(i * 2) + 0.5f);
}
}
source = array;
sourceWidth = num;
sourceHeight = num2;
}
private void Open(VisualElement surface)
{
//IL_0014: Unknown result type (might be due to invalid IL or missing references)
//IL_001e: Expected O, but got Unknown
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_005e: Unknown result type (might be due to invalid IL or missing references)
//IL_0078: Unknown result type (might be due to invalid IL or missing references)
//IL_0092: Unknown result type (might be due to invalid IL or missing references)
//IL_00a8: Unknown result type (might be due to invalid IL or missing references)
//IL_00b3: Unknown result type (might be due to invalid IL or missing references)
//IL_00bd: Expected O, but got Unknown
//IL_00d5: Unknown result type (might be due to invalid IL or missing references)
//IL_00f1: Unknown result type (might be due to invalid IL or missing references)
//IL_00fb: Expected O, but got Unknown
//IL_0113: Unknown result type (might be due to invalid IL or missing references)
//IL_012d: Unknown result type (might be due to invalid IL or missing references)
//IL_0147: Unknown result type (might be due to invalid IL or missing references)
//IL_0161: Unknown result type (might be due to invalid IL or missing references)
//IL_017b: Unknown result type (might be due to invalid IL or missing references)
//IL_019f: Unknown result type (might be due to invalid IL or missing references)
//IL_01a4: Unknown result type (might be due to invalid IL or missing references)
//IL_0273: Unknown result type (might be due to invalid IL or missing references)
RemoveOverlay();
overlay = new VisualElement();
overlay.style.position = StyleEnum<Position>.op_Implicit((Position)1);
overlay.style.left = StyleLength.op_Implicit(0f);
overlay.style.top = StyleLength.op_Implicit(0f);
overlay.style.right = StyleLength.op_Implicit(0f);
overlay.style.bottom = StyleLength.op_Implicit(0f);
overlay.style.overflow = StyleEnum<Overflow>.op_Implicit((Overflow)1);
preview = new Image();
((VisualElement)preview).pickingMode = (PickingMode)1;
((VisualElement)preview).style.position = StyleEnum<Position>.op_Implicit((Position)1);
overlay.Add((VisualElement)(object)preview);
handle = new VisualElement();
handle.pickingMode = (PickingMode)1;
handle.style.position = StyleEnum<Position>.op_Implicit((Position)1);
handle.style.width = StyleLength.op_Implicit(18f);
handle.style.height = StyleLength.op_Implicit(18f);
handle.style.marginLeft = StyleLength.op_Implicit(-18f);
handle.style.marginTop = StyleLength.op_Implicit(-18f);
handle.style.backgroundColor = StyleColor.op_Implicit(new Color(0.25f, 0.7f, 1f));
overlay.Add(handle);
((CallbackEventHandler)overlay).RegisterCallback<PointerDownEvent>((EventCallback<PointerDownEvent>)OnDown, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<PointerMoveEvent>((EventCallback<PointerMoveEvent>)OnMove, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<PointerUpEvent>((EventCallback<PointerUpEvent>)OnUp, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<PointerCaptureOutEvent>((EventCallback<PointerCaptureOutEvent>)delegate(PointerCaptureOutEvent e)
{
pointer = -1;
((EventBase)e).StopPropagation();
}, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<WheelEvent>((EventCallback<WheelEvent>)delegate(WheelEvent e)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
SliderInt obj = sizeSlider;
((BaseField<int>)(object)obj).value = ((BaseField<int>)(object)obj).value + ((!(e.delta.y < 0f)) ? (-3) : 3);
((EventBase)e).StopPropagation();
}, (TrickleDown)0);
VisualElement current = overlay;
((CallbackEventHandler)overlay).RegisterCallback<DetachFromPanelEvent>((EventCallback<DetachFromPanelEvent>)delegate
{
if (overlay == current)
{
Close();
}
}, (TrickleDown)0);
surface.Add(overlay);
controls.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)0);
((VisualElement)importButton).SetEnabled(false);
RefreshTexture();
((BaseField<int>)(object)sizeSlider).SetValueWithoutNotify(100);
Layout();
Status("Drag the photo, resize with the mouse wheel or the blue corner, then click Confirm.");
}
private void RefreshTexture()
{
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Expected O, but got Unknown
if ((Object)(object)sourceTexture != (Object)null)
{
Object.Destroy((Object)(object)sourceTexture);
}
sourceTexture = new Texture2D(sourceWidth, sourceHeight, (TextureFormat)4, false);
((Texture)sourceTexture).wrapMode = (TextureWrapMode)1;
sourceTexture.SetPixels32(source);
sourceTexture.Apply(false, false);
preview.image = (Texture)(object)sourceTexture;
}
private float FitWidth()
{
return (float)sourceWidth * Math.Min((float)canvasWidth / (float)sourceWidth, (float)canvasHeight / (float)sourceHeight);
}
private void ResizeTo(int percent)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_004f: Unknown result type (might be due to invalid IL or missing references)
//IL_0054: Unknown result type (might be due to invalid IL or missing references)
Vector2 center = ((Rect)(ref photo)).center;
float num = FitWidth() * (float)percent / 100f;
float num2 = num * (float)sourceHeight / (float)sourceWidth;
photo = new Rect(center.x - num / 2f, center.y - num2 / 2f, num, num2);
Layout();
}
private void Layout()
{
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
//IL_0063: Unknown result type (might be due to invalid IL or missing references)
//IL_0068: Unknown result type (might be due to invalid IL or missing references)
//IL_0096: Unknown result type (might be due to invalid IL or missing references)
//IL_009b: Unknown result type (might be due to invalid IL or missing references)
//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
//IL_00ce: Unknown result type (might be due to invalid IL or missing references)
//IL_010b: Unknown result type (might be due to invalid IL or missing references)
//IL_0110: Unknown result type (might be due to invalid IL or missing references)
//IL_014d: Unknown result type (might be due to invalid IL or missing references)
//IL_0152: Unknown result type (might be due to invalid IL or missing references)
if (preview != null)
{
((VisualElement)preview).style.left = StyleLength.op_Implicit(Length.Percent(((Rect)(ref photo)).x * 100f / (float)canvasWidth));
((VisualElement)preview).style.top = StyleLength.op_Implicit(Length.Percent(((Rect)(ref photo)).y * 100f / (float)canvasHeight));
((VisualElement)preview).style.width = StyleLength.op_Implicit(Length.Percent(((Rect)(ref photo)).width * 100f / (float)canvasWidth));
((VisualElement)preview).style.height = StyleLength.op_Implicit(Length.Percent(((Rect)(ref photo)).height * 100f / (float)canvasHeight));
handle.style.left = StyleLength.op_Implicit(Length.Percent(Mathf.Clamp(((Rect)(ref photo)).xMax * 100f / (float)canvasWidth, 5f, 100f)));
handle.style.top = StyleLength.op_Implicit(Length.Percent(Mathf.Clamp(((Rect)(ref photo)).yMax * 100f / (float)canvasHeight, 5f, 100f)));
}
}
private Vector2 ToCanvas(Vector2 local)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
Rect contentRect = overlay.contentRect;
return new Vector2(local.x * (float)canvasWidth / ((Rect)(ref contentRect)).width, local.y * (float)canvasHeight / ((Rect)(ref contentRect)).height);
}
private void OnDown(PointerDownEvent e)
{
//IL_0021: Unknown result type (might be due to invalid IL or missing references)
//IL_0026: Unknown result type (might be due to invalid IL or missing references)
//IL_002b: Unknown result type (might be due to invalid IL or missing references)
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0037: Unknown result type (might be due to invalid IL or missing references)
//IL_003c: Unknown result type (might be due to invalid IL or missing references)
//IL_0047: Unknown result type (might be due to invalid IL or missing references)
//IL_004c: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
//IL_00c0: Unknown result type (might be due to invalid IL or missing references)
((EventBase)e).StopPropagation();
if (((PointerEventBase<PointerDownEvent>)(object)e).button == 0)
{
pointer = ((PointerEventBase<PointerDownEvent>)(object)e).pointerId;
dragStart = ToCanvas(Vector2.op_Implicit(((PointerEventBase<PointerDownEvent>)(object)e).localPosition));
rectStart = photo;
Rect contentRect = overlay.contentRect;
Vector2 val = default(Vector2);
((Vector2)(ref val))..ctor(Mathf.Clamp(((Rect)(ref photo)).xMax / (float)canvasWidth, 0.05f, 1f) * ((Rect)(ref contentRect)).width - 9f, Mathf.Clamp(((Rect)(ref photo)).yMax / (float)canvasHeight, 0.05f, 1f) * ((Rect)(ref contentRect)).height - 9f);
resizing = Vector2.Distance(Vector2.op_Implicit(((PointerEventBase<PointerDownEvent>)(object)e).localPosition), val) <= 18f;
PointerCaptureHelper.CapturePointer((IEventHandler)(object)overlay, pointer);
}
}
private void OnMove(PointerMoveEvent e)
{
//IL_001a: Unknown result type (might be due to invalid IL or missing references)
//IL_001f: Unknown result type (might be due to invalid IL or missing references)
//IL_0024: Unknown result type (might be due to invalid IL or missing references)
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Unknown result type (might be due to invalid IL or missing references)
//IL_0034: Unknown result type (might be due to invalid IL or missing references)
//IL_0102: Unknown result type (might be due to invalid IL or missing references)
//IL_0107: Unknown result type (might be due to invalid IL or missing references)
//IL_0108: Unknown result type (might be due to invalid IL or missing references)
//IL_00d6: Unknown result type (might be due to invalid IL or missing references)
//IL_00db: Unknown result type (might be due to invalid IL or missing references)
((EventBase)e).StopPropagation();
if (((PointerEventBase<PointerMoveEvent>)(object)e).pointerId == pointer)
{
Vector2 val = ToCanvas(Vector2.op_Implicit(((PointerEventBase<PointerMoveEvent>)(object)e).localPosition)) - dragStart;
if (resizing)
{
float num = 1f + (val.x / ((Rect)(ref rectStart)).width + val.y / ((Rect)(ref rectStart)).height) / 2f;
float num2 = Mathf.Clamp(((Rect)(ref rectStart)).width * num * 100f / FitWidth(), 5f, 300f);
float num3 = FitWidth() * num2 / 100f;
photo = new Rect(((Rect)(ref rectStart)).x, ((Rect)(ref rectStart)).y, num3, num3 * (float)sourceHeight / (float)sourceWidth);
((BaseField<int>)(object)sizeSlider).SetValueWithoutNotify(Mathf.RoundToInt(num2));
}
else
{
((Rect)(ref photo)).position = ((Rect)(ref rectStart)).position + val;
}
Layout();
}
}
private void OnUp(PointerUpEvent e)
{
((EventBase)e).StopPropagation();
if (((PointerEventBase<PointerUpEvent>)(object)e).pointerId == pointer)
{
PointerCaptureHelper.ReleasePointer((IEventHandler)(object)overlay, pointer);
pointer = -1;
}
}
private void Rotate()
{
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_005b: Unknown result type (might be due to invalid IL or missing references)
if (source == null)
{
return;
}
Color32[] array = (Color32[])(object)new Color32[source.Length];
for (int i = 0; i < sourceHeight; i++)
{
for (int j = 0; j < sourceWidth; j++)
{
ref Color32 reference = ref array[(sourceWidth - 1 - j) * sourceHeight + i];
reference = source[i * sourceWidth + j];
}
}
source = array;
int num = sourceWidth;
sourceWidth = sourceHeight;
sourceHeight = num;
RefreshTexture();
ResizeTo(((BaseField<int>)(object)sizeSlider).value);
}
private Color32 Sample(float x, float y)
{
//IL_0091: Unknown result type (might be due to invalid IL or missing references)
//IL_00c9: Unknown result type (might be due to invalid IL or missing references)
//IL_00fb: Unknown result type (might be due to invalid IL or missing references)
//IL_012d: Unknown result type (might be due to invalid IL or missing references)
//IL_0191: Unknown result type (might be due to invalid IL or missing references)
//IL_0154: Unknown result type (might be due to invalid IL or missing references)
x = Mathf.Clamp(x, 0f, (float)(sourceWidth - 1));
y = Mathf.Clamp(y, 0f, (float)(sourceHeight - 1));
int num = (int)x;
int num2 = (int)y;
int num3 = Math.Min(num + 1, sourceWidth - 1);
int num4 = Math.Min(num2 + 1, sourceHeight - 1);
float num5 = x - (float)num;
float num6 = y - (float)num2;
float r = 0f;
float g = 0f;
float b = 0f;
float a = 0f;
Add(source[num2 * sourceWidth + num], (1f - num5) * (1f - num6), ref r, ref g, ref b, ref a);
Add(source[num2 * sourceWidth + num3], num5 * (1f - num6), ref r, ref g, ref b, ref a);
Add(source[num4 * sourceWidth + num], (1f - num5) * num6, ref r, ref g, ref b, ref a);
Add(source[num4 * sourceWidth + num3], num5 * num6, ref r, ref g, ref b, ref a);
if (a <= 0f)
{
return new Color32((byte)0, (byte)0, (byte)0, (byte)0);
}
return new Color32((byte)(r / a + 0.5f), (byte)(g / a + 0.5f), (byte)(b / a + 0.5f), (byte)Mathf.Min(255f, a + 0.5f));
}
private static void Add(Color32 c, float weight, ref float r, ref float g, ref float b, ref float a)
{
float num = (float)(int)c.a * weight;
r += (float)(int)c.r * num;
g += (float)(int)c.g * num;
b += (float)(int)c.b * num;
a += num;
}
private byte[] Render()
{
//IL_020e: Unknown result type (might be due to invalid IL or missing references)
//IL_0215: Expected O, but got Unknown
//IL_01af: Unknown result type (might be due to invalid IL or missing references)
//IL_01b4: Unknown result type (might be due to invalid IL or missing references)
//IL_0124: Unknown result type (might be due to invalid IL or missing references)
Color32[] array = (Color32[])(object)new Color32[canvasWidth * canvasHeight];
int num = Mathf.Clamp(Mathf.CeilToInt((float)sourceWidth / ((Rect)(ref photo)).width), 1, 4);
for (int i = 0; i < canvasHeight; i++)
{
for (int j = 0; j < canvasWidth; j++)
{
float r = 0f;
float g = 0f;
float b = 0f;
float a = 0f;
for (int k = 0; k < num; k++)
{
for (int l = 0; l < num; l++)
{
float num2 = ((float)j + ((float)l + 0.5f) / (float)num - ((Rect)(ref photo)).x) / ((Rect)(ref photo)).width;
float num3 = ((float)(canvasHeight - i - 1) + ((float)k + 0.5f) / (float)num - ((Rect)(ref photo)).y) / ((Rect)(ref photo)).height;
if (!(num2 < 0f) && !(num2 >= 1f) && !(num3 < 0f) && !(num3 >= 1f))
{
Add(Sample(num2 * (float)sourceWidth - 0.5f, (1f - num3) * (float)sourceHeight - 0.5f), 1f / (float)(num * num), ref r, ref g, ref b, ref a);
}
}
}
if (a > 0f)
{
ref Color32 reference = ref array[i * canvasWidth + j];
reference = new Color32((byte)(r / a + 0.5f), (byte)(g / a + 0.5f), (byte)(b / a + 0.5f), (byte)Mathf.Min(255f, a + 0.5f));
}
}
}
if (array[0].a == byte.MaxValue)
{
array[0].a = 254;
}
Texture2D val = new Texture2D(canvasWidth, canvasHeight, (TextureFormat)4, false);
val.SetPixels32(array);
val.Apply(false, false);
byte[] result = ImageConversion.EncodeToPNG(val);
Object.Destroy((Object)(object)val);
return result;
}
private void Confirm()
{
//IL_002a: Unknown result type (might be due to invalid IL or missing references)
if (source == null)
{
return;
}
if (!((Rect)(ref photo)).Overlaps(new Rect(0f, 0f, (float)canvasWidth, (float)canvasHeight)))
{
Status("The photo is entirely outside the artwork.");
return;
}
try
{
byte[] array = Render();
Call("AddDrawingLayer");
object obj = Field<object>("m_canvas");
if (!(bool)AccessTools.Method(obj.GetType(), "LoadImageWithUndo", (Type[])null, (Type[])null).Invoke(obj, new object[1] { array }))
{
Status("The photo could not be placed.");
return;
}
Call("ApplyDrawingSettingsToActiveLayer");
Call("RebuildCanvasVisualStack");
Call("RefreshLayerList");
Call("UpdateHistoryButtons");
AccessTools.Field(editor.GetType(), "m_draftDirty").SetValue(editor, true);
Close();
Status("Photo added on a new layer.");
}
catch (Exception ex)
{
Plugin.Log.LogError((object)ex);
Status("The photo could not be placed.");
Close();
}
}
private void RemoveOverlay()
{
VisualElement val = overlay;
overlay = null;
if (val != null)
{
val.RemoveFromHierarchy();
}
preview = null;
pointer = -1;
}
private void Close()
{
//IL_0047: Unknown result type (might be due to invalid IL or missing references)
RemoveOverlay();
if ((Object)(object)sourceTexture != (Object)null)
{
Object.Destroy((Object)(object)sourceTexture);
}
sourceTexture = null;
source = null;
if (controls != null)
{
controls.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)1);
}
if (importButton != null)
{
((VisualElement)importButton).SetEnabled(true);
}
}
}
internal static class FileDialog
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private sealed class OpenFileName
{
public int size;
public IntPtr owner;
public IntPtr instance;
public string filter;
public string customFilter;
public int maxCustomFilter;
public int filterIndex;
public IntPtr file;
public int maxFile;
public IntPtr fileTitle;
public int maxFileTitle;
public string initialDir;
public string title;
public int flags;
public short fileOffset;
public short fileExtension;
public string defaultExtension;
public IntPtr data;
public IntPtr hook;
public IntPtr templateName;
public IntPtr reserved;
public int reservedValue;
public int flagsEx;
}
[DllImport("comdlg32.dll", CharSet = CharSet.Unicode, EntryPoint = "GetOpenFileNameW")]
private static extern bool GetOpenFileName([In][Out] OpenFileName dialog);
[DllImport("user32.dll")]
private static extern IntPtr GetActiveWindow();
internal static string OpenImage()
{
IntPtr intPtr = Marshal.AllocHGlobal(65536);
try
{
Marshal.WriteInt16(intPtr, 0);
OpenFileName openFileName = new OpenFileName();
openFileName.size = Marshal.SizeOf(typeof(OpenFileName));
openFileName.owner = GetActiveWindow();
openFileName.filter = "Images (*.png;*.jpg;*.jpeg)\0*.png;*.jpg;*.jpeg\0\0";
openFileName.file = intPtr;
openFileName.maxFile = 32768;
openFileName.title = "Import photo";
openFileName.flags = 530444;
return (!GetOpenFileName(openFileName)) ? null : Marshal.PtrToStringUni(intPtr);
}
finally
{
Marshal.FreeHGlobal(intPtr);
}
}
}
internal sealed class NameTagTool
{
private const int Width = 128;
private const int Height = 64;
private readonly object ui;
private VisualElement controls;
private VisualElement overlay;
private Image preview;
private Texture2D previewTexture;
private Button importButton;
private SliderInt size;
private SliderInt threshold;
private Toggle shading;
private Toggle invert;
private Color32[] source;
private int sourceWidth;
private int sourceHeight;
private Vector2 center;
private byte[] result;
private int pointer = -1;
private Vector2 dragStart;
private Vector2 centerStart;
internal NameTagTool(object ui)
{
this.ui = ui;
}
private T Field<T>(string name)
{
return (T)AccessTools.Field(ui.GetType(), name).GetValue(ui);
}
internal void Attach()
{
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
//IL_002f: Expected O, but got Unknown
//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
//IL_00ba: Expected O, but got Unknown
//IL_00c6: Unknown result type (might be due to invalid IL or missing references)
//IL_011e: Unknown result type (might be due to invalid IL or missing references)
//IL_0125: Expected O, but got Unknown
//IL_012d: Unknown result type (might be due to invalid IL or missing references)
Button val = Field<Button>("pen");
VisualElement val2 = Field<VisualElement>("namePage");
importButton = new Button((Action)Import);
((TextElement)importButton).text = "Import photo";
foreach (string @class in ((VisualElement)val).GetClasses())
{
if (@class != "is-selected")
{
((VisualElement)importButton).AddToClassList(@class);
}
}
((VisualElement)val).parent.Insert(((VisualElement)val).parent.IndexOf((VisualElement)(object)val) + 2, (VisualElement)(object)importButton);
controls = new VisualElement();
controls.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)1);
size = MakeSlider("Size %", 20, 400);
threshold = MakeSlider("Darkness", 5, 95);
shading = MakeToggle("Shading (for photos)");
invert = MakeToggle("Invert colours");
VisualElement val3 = new VisualElement();
val3.style.flexDirection = StyleEnum<FlexDirection>.op_Implicit((FlexDirection)2);
val3.Add((VisualElement)(object)MakeButton("Rotate 90°", Rotate, val));
val3.Add((VisualElement)(object)MakeButton("Confirm", Confirm, val));
val3.Add((VisualElement)(object)MakeButton("Cancel", Close, val));
controls.Add(val3);
val2.Add(controls);
}
private SliderInt MakeSlider(string label, int low, int high)
{
//IL_0008: Unknown result type (might be due to invalid IL or missing references)
//IL_000e: Expected O, but got Unknown
//IL_0020: Unknown result type (might be due to invalid IL or missing references)
//IL_0025: Unknown result type (might be due to invalid IL or missing references)
SliderInt val = new SliderInt(low, high, (SliderDirection)0, 0f);
((BaseField<int>)(object)val).label = label;
((VisualElement)((BaseField<int>)(object)val).labelElement).style.color = StyleColor.op_Implicit(Color.white);
INotifyValueChangedExtensions.RegisterValueChangedCallback<int>((INotifyValueChanged<int>)(object)val, (EventCallback<ChangeEvent<int>>)delegate
{
Render();
});
controls.Add((VisualElement)(object)val);
return val;
}
private Toggle MakeToggle(string label)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0007: Expected O, but got Unknown
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
Toggle val = new Toggle(label);
((VisualElement)((BaseField<bool>)(object)val).labelElement).style.color = StyleColor.op_Implicit(Color.white);
INotifyValueChangedExtensions.RegisterValueChangedCallback<bool>((INotifyValueChanged<bool>)(object)val, (EventCallback<ChangeEvent<bool>>)delegate
{
Render();
});
controls.Add((VisualElement)(object)val);
return val;
}
private static Button MakeButton(string text, Action action, Button model)
{
//IL_0001: Unknown result type (might be due to invalid IL or missing references)
//IL_0007: Expected O, but got Unknown
Button val = new Button(action);
((TextElement)val).text = text;
foreach (string @class in ((VisualElement)model).GetClasses())
{
if (@class != "is-selected")
{
((VisualElement)val).AddToClassList(@class);
}
}
return val;
}
private void Import()
{
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0036: Expected O, but got Unknown
try
{
string text = FileDialog.OpenImage();
if (string.IsNullOrEmpty(text) || new FileInfo(text).Length > 20971520)
{
return;
}
Texture2D val = new Texture2D(2, 2, (TextureFormat)4, false);
if (!ImageConversion.LoadImage(val, File.ReadAllBytes(text)))
{
Object.Destroy((Object)(object)val);
return;
}
source = val.GetPixels32();
sourceWidth = ((Texture)val).width;
sourceHeight = ((Texture)val).height;
Object.Destroy((Object)(object)val);
while (Math.Max(sourceWidth, sourceHeight) > 512)
{
Halve();
}
Open();
}
catch (Exception ex)
{
Plugin.Log.LogError((object)ex);
Close();
}
}
private void Halve()
{
//IL_0098: Unknown result type (might be due to invalid IL or missing references)
//IL_009d: Unknown result type (might be due to invalid IL or missing references)
//IL_00b6: Unknown result type (might be due to invalid IL or missing references)
//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
//IL_00d4: Unknown result type (might be due to invalid IL or missing references)
//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
//IL_00f2: Unknown result type (might be due to invalid IL or missing references)
//IL_00f7: Unknown result type (might be due to invalid IL or missing references)
//IL_018d: Unknown result type (might be due to invalid IL or missing references)
//IL_0192: Unknown result type (might be due to invalid IL or missing references)
int num = Math.Max(1, sourceWidth / 2);
int num2 = Math.Max(1, sourceHeight / 2);
Color32[] array = (Color32[])(object)new Color32[num * num2];
for (int i = 0; i < num2; i++)
{
for (int j = 0; j < num; j++)
{
int num3 = Math.Min(j * 2, sourceWidth - 1);
int num4 = Math.Min(num3 + 1, sourceWidth - 1);
int num5 = Math.Min(i * 2, sourceHeight - 1);
int num6 = Math.Min(num5 + 1, sourceHeight - 1);
Color32 val = source[num5 * sourceWidth + num3];
Color32 val2 = source[num5 * sourceWidth + num4];
Color32 val3 = source[num6 * sourceWidth + num3];
Color32 val4 = source[num6 * sourceWidth + num4];
ref Color32 reference = ref array[i * num + j];
reference = new Color32((byte)((val.r + val2.r + val3.r + val4.r) / 4), (byte)((val.g + val2.g + val3.g + val4.g) / 4), (byte)((val.b + val2.b + val3.b + val4.b) / 4), (byte)((val.a + val2.a + val3.a + val4.a) / 4));
}
}
source = array;
sourceWidth = num;
sourceHeight = num2;
}
private void Open()
{
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_004e: Expected O, but got Unknown
//IL_005a: Unknown result type (might be due to invalid IL or missing references)
//IL_0064: Expected O, but got Unknown
//IL_0132: Unknown result type (might be due to invalid IL or missing references)
//IL_013c: Expected O, but got Unknown
//IL_0170: Unknown result type (might be due to invalid IL or missing references)
//IL_0175: Unknown result type (might be due to invalid IL or missing references)
//IL_01b8: Unknown result type (might be due to invalid IL or missing references)
RemoveOverlay();
Image val = Field<Image>("drawingImage");
AccessTools.Method(ui.GetType(), "HideBrush", (Type[])null, (Type[])null).Invoke(ui, null);
overlay = new VisualElement();
Stretch(overlay);
preview = new Image();
((VisualElement)preview).pickingMode = (PickingMode)1;
Stretch((VisualElement)(object)preview);
overlay.Add((VisualElement)(object)preview);
((CallbackEventHandler)overlay).RegisterCallback<PointerDownEvent>((EventCallback<PointerDownEvent>)delegate(PointerDownEvent e)
{
//IL_0030: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
//IL_0050: Unknown result type (might be due to invalid IL or missing references)
//IL_0055: Unknown result type (might be due to invalid IL or missing references)
((EventBase)e).StopPropagation();
if (((PointerEventBase<PointerDownEvent>)(object)e).button == 0)
{
pointer = ((PointerEventBase<PointerDownEvent>)(object)e).pointerId;
dragStart = ToBadge(Vector2.op_Implicit(((PointerEventBase<PointerDownEvent>)(object)e).localPosition));
centerStart = center;
PointerCaptureHelper.CapturePointer((IEventHandler)(object)overlay, pointer);
}
}, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<PointerMoveEvent>((EventCallback<PointerMoveEvent>)delegate(PointerMoveEvent e)
{
//IL_0029: Unknown result type (might be due to invalid IL or missing references)
//IL_0035: Unknown result type (might be due to invalid IL or missing references)
//IL_003a: Unknown result type (might be due to invalid IL or missing references)
//IL_003f: Unknown result type (might be due to invalid IL or missing references)
//IL_0044: Unknown result type (might be due to invalid IL or missing references)
//IL_004f: Unknown result type (might be due to invalid IL or missing references)
//IL_0054: Unknown result type (might be due to invalid IL or missing references)
//IL_0059: Unknown result type (might be due to invalid IL or missing references)
((EventBase)e).StopPropagation();
if (((PointerEventBase<PointerMoveEvent>)(object)e).pointerId == pointer)
{
center = centerStart + ToBadge(Vector2.op_Implicit(((PointerEventBase<PointerMoveEvent>)(object)e).localPosition)) - dragStart;
Render();
}
}, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<PointerUpEvent>((EventCallback<PointerUpEvent>)delegate(PointerUpEvent e)
{
((EventBase)e).StopPropagation();
if (((PointerEventBase<PointerUpEvent>)(object)e).pointerId == pointer)
{
PointerCaptureHelper.ReleasePointer((IEventHandler)(object)overlay, pointer);
pointer = -1;
}
}, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<PointerCaptureOutEvent>((EventCallback<PointerCaptureOutEvent>)delegate(PointerCaptureOutEvent e)
{
pointer = -1;
((EventBase)e).StopPropagation();
}, (TrickleDown)0);
((CallbackEventHandler)overlay).RegisterCallback<WheelEvent>((EventCallback<WheelEvent>)delegate(WheelEvent e)
{
//IL_0012: Unknown result type (might be due to invalid IL or missing references)
//IL_0017: Unknown result type (might be due to invalid IL or missing references)
SliderInt obj = size;
((BaseField<int>)(object)obj).value = ((BaseField<int>)(object)obj).value + ((!(e.delta.y < 0f)) ? (-5) : 5);
((EventBase)e).StopPropagation();
}, (TrickleDown)0);
VisualElement current = overlay;
((CallbackEventHandler)overlay).RegisterCallback<DetachFromPanelEvent>((EventCallback<DetachFromPanelEvent>)delegate
{
if (overlay == current)
{
Close();
}
}, (TrickleDown)0);
previewTexture = new Texture2D(128, 64, (TextureFormat)4, false);
((Texture)previewTexture).filterMode = (FilterMode)0;
preview.image = (Texture)(object)previewTexture;
((VisualElement)val).Add(overlay);
center = new Vector2(64f, 32f);
((BaseField<int>)(object)size).SetValueWithoutNotify(100);
((BaseField<int>)(object)threshold).SetValueWithoutNotify(50);
((BaseField<bool>)(object)shading).SetValueWithoutNotify(true);
((BaseField<bool>)(object)invert).SetValueWithoutNotify(false);
controls.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)0);
((VisualElement)importButton).SetEnabled(false);
Render();
}
private static void Stretch(VisualElement element)
{
//IL_0007: Unknown result type (might be due to invalid IL or missing references)
//IL_001c: Unknown result type (might be due to invalid IL or missing references)
//IL_0031: Unknown result type (might be due to invalid IL or missing references)
//IL_0046: Unknown result type (might be due to invalid IL or missing references)
//IL_005b: Unknown result type (might be due to invalid IL or missing references)
element.style.position = StyleEnum<Position>.op_Implicit((Position)1);
element.style.left = StyleLength.op_Implicit(0f);
element.style.top = StyleLength.op_Implicit(0f);
element.style.right = StyleLength.op_Implicit(0f);
element.style.bottom = StyleLength.op_Implicit(0f);
}
private Vector2 ToBadge(Vector2 local)
{
//IL_0006: Unknown result type (might be due to invalid IL or missing references)
//IL_000b: Unknown result type (might be due to invalid IL or missing references)
//IL_003e: Unknown result type (might be due to invalid IL or missing references)
Rect contentRect = overlay.contentRect;
return new Vector2(local.x * 128f / ((Rect)(ref contentRect)).width, (((Rect)(ref contentRect)).height - local.y) * 64f / ((Rect)(ref contentRect)).height);
}
private void Render()
{
//IL_0106: Unknown result type (might be due to invalid IL or missing references)
//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
//IL_010b: Unknown result type (might be due to invalid IL or missing references)
if (source != null && !((Object)(object)previewTexture == (Object)null))
{
float scale = Math.Min(128f / (float)sourceWidth, 64f / (float)sourceHeight) * (float)((BaseField<int>)(object)size).value / 100f;
result = Convert(source, sourceWidth, sourceHeight, center.x, center.y, scale, (float)((BaseField<int>)(object)threshold).value / 100f, ((BaseField<bool>)(object)shading).value, ((BaseField<bool>)(object)invert).value);
Color32[] array = (Color32[])(object)new Color32[8192];
for (int i = 0; i < array.Length; i++)
{
ref Color32 reference = ref array[i];
reference = (((result[i >> 3] & (1 << (i & 7))) == 0) ? new Color32((byte)250, (byte)247, (byte)238, byte.MaxValue) : new Color32((byte)25, (byte)22, (byte)20, byte.MaxValue));
}
previewTexture.SetPixels32(array);
previewTexture.Apply(false, false);
((VisualElement)preview).MarkDirtyRepaint();
}
}
internal static byte[] Convert(Color32[] source, int sw, int sh, float cx, float cy, float scale, float threshold, bool shading, bool invert)
{
//IL_00d9: Unknown result type (might be due to invalid IL or missing references)
//IL_00de: Unknown result type (might be due to invalid IL or missing references)
float[] array = new float[8192];
bool[] array2 = new bool[8192];
float num = cx - (float)sw * scale / 2f;
float num2 = cy - (float)sh * scale / 2f;
for (int i = 0; i < 64; i++)
{
for (int j = 0; j < 128; j++)
{
float num3 = 0f;
int num4 = 0;
for (int k = 0; k < 4; k++)
{
for (int l = 0; l < 4; l++)
{
float num5 = ((float)j + ((float)l + 0.5f) / 4f - num) / scale;
float num6 = ((float)i + ((float)k + 0.5f) / 4f - num2) / scale;
if (num5 < 0f || num6 < 0f || num5 >= (float)sw || num6 >= (float)sh)
{
num3 += 1f;
continue;
}
Color32 val = source[(int)num6 * sw + (int)num5];
float num7 = (float)(int)val.a / 255f;
float num8 = (0.299f * (float)(int)val.r + 0.587f * (float)(int)val.g + 0.114f * (float)(int)val.b) / 255f;
if (invert)
{
num8 = 1f - num8;
}
num3 += num8 * num7 + (1f - num7);
num4++;
}
}
array[i * 128 + j] = num3 / 16f;
array2[i * 128 + j] = num4 > 0;
}
}
byte[] array3 = new byte[1024];
float num9 = threshold - 0.5f;
for (int m = 0; m < array.Length; m++)
{
if (array2[m])
{
array[m] -= num9;
}
}
for (int num10 = 63; num10 >= 0; num10--)
{
for (int n = 0; n < 128; n++)
{
int num11 = num10 * 128 + n;
if (!array2[num11])
{
continue;
}
bool flag = array[num11] < 0.5f;
if (flag)
{
array3[num11 >> 3] |= (byte)(1 << (num11 & 7));
}
if (!shading)
{
continue;
}
float num12 = array[num11] - ((!flag) ? 1f : 0f);
if (n + 1 < 128)
{
array[num11 + 1] += num12 * 7f / 16f;
}
if (num10 != 0)
{
if (n > 0)
{
array[num11 - 128 - 1] += num12 * 3f / 16f;
}
array[num11 - 128] += num12 * 5f / 16f;
if (n + 1 < 128)
{
array[num11 - 128 + 1] += num12 / 16f;
}
}
}
}
return array3;
}
private void Rotate()
{
//IL_0056: Unknown result type (might be due to invalid IL or missing references)
//IL_005b: Unknown result type (might be due to invalid IL or missing references)
if (source == null)
{
return;
}
Color32[] array = (Color32[])(object)new Color32[source.Length];
for (int i = 0; i < sourceHeight; i++)
{
for (int j = 0; j < sourceWidth; j++)
{
ref Color32 reference = ref array[(sourceWidth - 1 - j) * sourceHeight + i];
reference = source[i * sourceWidth + j];
}
}
source = array;
int num = sourceWidth;
sourceWidth = sourceHeight;
sourceHeight = num;
Render();
}
private void Confirm()
{
if (result != null)
{
try
{
object obj = Field<object>("drawing");
Type type = obj.GetType();
AccessTools.Method(type, "EndStroke", (Type[])null, (Type[])null).Invoke(obj, null);
AccessTools.Method(type, "Remember", (Type[])null, (Type[])null).Invoke(obj, null);
AccessTools.Field(type, "ink").SetValue(obj, result.Clone());
AccessTools.Method(type, "Redraw", (Type[])null, (Type[])null).Invoke(obj, null);
AccessTools.Method(ui.GetType(), "DrawingChanged", (Type[])null, (Type[])null).Invoke(ui, null);
}
catch (Exception ex)
{
Plugin.Log.LogError((object)ex);
}
Close();
}
}
private void RemoveOverlay()
{
VisualElement val = overlay;
overlay = null;
if (val != null)
{
val.RemoveFromHierarchy();
}
pointer = -1;
}
private void Close()
{
//IL_004e: Unknown result type (might be due to invalid IL or missing references)
RemoveOverlay();
if ((Object)(object)previewTexture != (Object)null)
{
Object.Destroy((Object)(object)previewTexture);
}
previewTexture = null;
source = null;
result = null;
if (controls != null)
{
controls.style.display = StyleEnum<DisplayStyle>.op_Implicit((DisplayStyle)1);
}
if (importButton != null)
{
((VisualElement)importButton).SetEnabled(true);
}
}
}