-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for per coordinate skin overlays in kk; Dedupe skin ove…
…rlay textures
- Loading branch information
1 parent
d72bf20
commit 9b191d4
Showing
7 changed files
with
430 additions
and
119 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using ExtensibleSaveFormat; | ||
using KKAPI.Chara; | ||
using KKAPI.Maker; | ||
using KoiClothesOverlayX; | ||
using MessagePack; | ||
using UnityEngine; | ||
#if KK | ||
using CoordinateType = ChaFileDefine.CoordinateType; | ||
#elif EC | ||
using CoordinateType = KoikatsuCharaFile.ChaFileDefine.CoordinateType; | ||
#elif AI || HS2 | ||
using AIChara; | ||
#endif | ||
|
||
namespace KoiSkinOverlayX | ||
{ | ||
#if AI || HS2 | ||
public enum CoordinateType | ||
{ | ||
Unknown = 0 | ||
} | ||
#endif | ||
|
||
public class OverlayStorage | ||
{ | ||
private const string OverlayDataKey = "Lookup"; | ||
|
||
private readonly TextureStorage _textureStorage; | ||
private readonly ChaControl _chaControl; | ||
private Dictionary<CoordinateType, Dictionary<TexType, int>> _allOverlayTextures; | ||
|
||
public OverlayStorage(CharaCustomFunctionController controller) | ||
{ | ||
_chaControl = controller.ChaControl; | ||
_textureStorage = new TextureStorage(); | ||
_allOverlayTextures = new Dictionary<CoordinateType, Dictionary<TexType, int>>(); | ||
} | ||
|
||
private Dictionary<TexType, int> GetCurrentOverlayTextures() | ||
{ | ||
#if KK | ||
// Need to do this instead of polling the CurrentCoordinate prop because it's updated too late | ||
var coordinateType = (CoordinateType)_chaControl.fileStatus.coordinateType; | ||
#elif EC | ||
var coordinateType = CoordinateType.School01; | ||
#else | ||
var coordinateType = CoordinateType.Unknown; | ||
#endif | ||
return GetOverlayTextures(coordinateType); | ||
} | ||
|
||
private Dictionary<TexType, int> GetOverlayTextures(CoordinateType coordinateType) | ||
{ | ||
_allOverlayTextures.TryGetValue(coordinateType, out var dict); | ||
|
||
if (dict == null) | ||
{ | ||
dict = new Dictionary<TexType, int>(); | ||
_allOverlayTextures.Add(coordinateType, dict); | ||
} | ||
|
||
return dict; | ||
} | ||
|
||
//CoordinateType coordinateType | ||
public Texture2D GetTexture(TexType type) | ||
{ | ||
var texs = GetCurrentOverlayTextures(); | ||
if (texs.TryGetValue(type, out var id)) | ||
return _textureStorage.GetSharedTexture(id); | ||
|
||
return null; | ||
} | ||
|
||
public void SetTexture(TexType type, byte[] pngData) | ||
{ | ||
var texs = GetCurrentOverlayTextures(); | ||
if (pngData == null) | ||
{ | ||
texs.Remove(type); | ||
} | ||
else | ||
{ | ||
var id = _textureStorage.StoreTexture(pngData); | ||
texs[type] = id; | ||
} | ||
} | ||
|
||
public int GetCount(bool onlyCurrentCoord = true) | ||
{ | ||
return onlyCurrentCoord ? GetCurrentOverlayTextures().Count : _allOverlayTextures.Sum(x => x.Value.Count); | ||
} | ||
|
||
public void Clear() | ||
{ | ||
// Less garbage generated than clearing the whole dict? | ||
foreach (var dic in _allOverlayTextures) dic.Value.Clear(); | ||
_textureStorage.Clear(); | ||
} | ||
|
||
public void Load(PluginData data) | ||
{ | ||
data.data.TryGetValue(OverlayDataKey, out var lookup); | ||
if (lookup is byte[] lookuparr) | ||
{ | ||
try | ||
{ | ||
_allOverlayTextures = MessagePackSerializer.Deserialize<Dictionary<CoordinateType, Dictionary<TexType, int>>>(lookuparr); | ||
_textureStorage.Load(data); | ||
} | ||
catch (Exception ex) | ||
{ | ||
if (MakerAPI.InsideMaker) | ||
KoiSkinOverlayMgr.Logger.LogMessage("WARNING: Failed to load embedded overlay data for " + (_chaControl.chaFile?.charaFileName ?? "?")); | ||
else | ||
KoiSkinOverlayMgr.Logger.LogDebug("WARNING: Failed to load embedded overlay data for " + (_chaControl.chaFile?.charaFileName ?? "?")); | ||
KoiSkinOverlayMgr.Logger.LogError(ex); | ||
|
||
Clear(); | ||
} | ||
} | ||
} | ||
|
||
public void Save(PluginData data) | ||
{ | ||
PurgeUnused(); | ||
if (GetCount(false) > 0) | ||
{ | ||
_textureStorage.Save(data); | ||
data.data[OverlayDataKey] = MessagePackSerializer.Serialize(_allOverlayTextures); | ||
} | ||
} | ||
|
||
private void PurgeUnused() | ||
{ | ||
//foreach (var dic in _allOverlayTextures.ToList()) | ||
//{ | ||
// if (dic.Value.Count == 0) | ||
// _allOverlayTextures.Remove(dic.Key); | ||
//} | ||
_textureStorage.PurgeUnused(_allOverlayTextures.SelectMany(x => x.Value.Values)); | ||
} | ||
|
||
#if KK | ||
public bool IsPerCoord() | ||
{ | ||
Dictionary<TexType, int> first = null; | ||
foreach (var dic in _allOverlayTextures) | ||
{ | ||
if (first == null) | ||
first = dic.Value; | ||
else if (!dic.Value.SequenceEqual(first)) | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public void CopyToOtherCoords() | ||
{ | ||
var cur = GetCurrentOverlayTextures(); | ||
|
||
foreach (CoordinateType ctype in Enum.GetValues(typeof(CoordinateType))) | ||
{ | ||
var other = GetOverlayTextures(ctype); | ||
if (cur == other) continue; | ||
|
||
other.Clear(); | ||
foreach (var curval in cur) | ||
other.Add(curval.Key, curval.Value); | ||
} | ||
} | ||
#endif | ||
} | ||
} |
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.