Skip to content

Commit

Permalink
Fixed being unable to export some textures
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Jun 2, 2021
1 parent cdf3bd8 commit aec2e69
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 27 deletions.
8 changes: 5 additions & 3 deletions Core_OverlayMods/Clothes/KoiClothesOverlayController.cs
Expand Up @@ -9,6 +9,7 @@
using MessagePack;
using UnityEngine;
using ExtensibleSaveFormat;
using KKAPI.Utilities;
#if KK || KKS
using CoordinateType = ChaFileDefine.CoordinateType;
using KKAPI.Studio;
Expand Down Expand Up @@ -85,7 +86,8 @@ public void DumpBaseTexture(string clothesId, Action<byte[]> callback)
if (tex == null)
throw new Exception("There is no texture to dump");

var t = tex.TextureToTexture2D();
// Fix being unable to save some texture formats with EncodeToPNG
var t = tex.ToTexture2D();
var bytes = t.EncodeToPNG();
Destroy(t);
callback(bytes);
Expand Down Expand Up @@ -449,9 +451,9 @@ private static void SetOverlayExtData(Dictionary<CoordinateType, Dictionary<stri
}
catch (Exception ex)
{
if (MakerAPI.InsideMaker)
if (MakerAPI.InsideMaker)
KoiSkinOverlayMgr.Logger.LogMessage("WARNING: Failed to load clothes overlay data");
else
else
KoiSkinOverlayMgr.Logger.LogDebug("WARNING: Failed to load clothes overlay data");
KoiSkinOverlayMgr.Logger.LogError(ex);

Expand Down
5 changes: 4 additions & 1 deletion Core_OverlayMods/Skin/KoiSkinOverlayGui.cs
Expand Up @@ -304,7 +304,10 @@ TexType GetTexType(bool cantBeBoth)
{
var tex = GetTex(GetTexType(true));
if (tex == null) return;
WriteAndOpenPng(tex.EncodeToPNG(), GetTexType(false).ToString());
// Fix being unable to save some texture formats with EncodeToPNG
var texCopy = tex.ToTexture2D();
WriteAndOpenPng(texCopy.EncodeToPNG(), GetTexType(false).ToString());
Destroy(texCopy);
}
catch (Exception ex)
{
Expand Down
25 changes: 2 additions & 23 deletions Core_OverlayMods/Skin/Util.cs
@@ -1,6 +1,7 @@
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using KKAPI.Utilities;
using UnityEngine;

namespace KoiSkinOverlayX
Expand All @@ -19,10 +20,7 @@ public static bool InsideStudio()
public static Texture2D TextureFromBytes(byte[] texBytes, TextureFormat format)
{
if (texBytes == null || texBytes.Length == 0) return null;

var tex = new Texture2D(2, 2, format, false);
tex.LoadImage(texBytes);
return tex;
return texBytes.LoadTexture(format);
}

/// <summary>
Expand All @@ -37,25 +35,6 @@ public static void OpenFileInExplorer(string filename)
catch (Exception) { Process.Start("explorer.exe", $"/select, \"{filename}\""); }
}

public static Texture2D TextureToTexture2D(this Texture tex)
{
var rt = RenderTexture.GetTemporary(tex.width, tex.height);
var prev = RenderTexture.active;
RenderTexture.active = rt;

GL.Clear(true, true, Color.clear);

Graphics.Blit(tex, rt);

var t = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false);
t.ReadPixels(new Rect(0, 0, tex.width, tex.height), 0, 0);
t.Apply(false);

RenderTexture.active = prev;
RenderTexture.ReleaseTemporary(rt);
return t;
}

private static class NativeMethods
{
/// <summary>
Expand Down

0 comments on commit aec2e69

Please sign in to comment.