Skip to content

Commit

Permalink
[+] Load Jacket from A???\AssetBundleImages\jacket\*.png
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Sep 2, 2024
1 parent d58fe84 commit 489c00e
Showing 1 changed file with 47 additions and 26 deletions.
73 changes: 47 additions & 26 deletions AquaMai/UX/LoadJacketPng.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,77 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using UnityEngine;
using System.Text.RegularExpressions;
using Manager;
using MelonLoader;

namespace AquaMai.UX
{
[HarmonyPatch]
public class LoadJacketPng
{
public static IEnumerable<MethodBase> TargetMethods()
[HarmonyPatch]
public static class Loader
{
var AM = typeof(AssetManager);
return new[] { AM.GetMethod("GetJacketThumbTexture2D", new[] { typeof(string) }), AM.GetMethod("GetJacketTexture2D", new[] { typeof(string) }) };
}
public static IEnumerable<MethodBase> TargetMethods()
{
var AM = typeof(AssetManager);
return new[] { AM.GetMethod("GetJacketThumbTexture2D", new[] { typeof(string) }), AM.GetMethod("GetJacketTexture2D", new[] { typeof(string) }) };
}

public static bool Prefix(string filename, ref Texture2D __result, AssetManager __instance)
{
var matches = Regex.Matches(filename, @"UI_Jacket_(\d+)(_s)?\.png");
if (matches.Count < 1)
public static bool Prefix(string filename, ref Texture2D __result, AssetManager __instance)
{
return true;
var matches = Regex.Matches(filename, @"UI_Jacket_(\d+)(_s)?\.png");
if (matches.Count < 1)
{
return true;
}

var id = matches[0].Groups[1].Value;

var texture = GetJacketTexture2D(id);
__result = texture ?? __instance.LoadAsset<Texture2D>($"Jacket/UI_Jacket_{id}.png");

return false;
}
}

var id = matches[0].Groups[1].Value;
private static string[] imageExts = [".jpg", ".png", ".jpeg"];
private static Regex localAssetsJacketExt = new(@"(\d{6})\.(png|jpg|jpeg)");
private static Dictionary<string, string> jacketPaths = new();

var texture = GetJacketTexture2D(id);
if (texture is null)
[HarmonyPrefix]
[HarmonyPatch(typeof(DataManager), "LoadMusicBase")]
public static void LoadMusicPostfix(List<string> ____targetDirs)
{
foreach (var aDir in ____targetDirs)
{
__result = __instance.LoadAsset<Texture2D>($"Jacket/UI_Jacket_{id}.png");
if (!Directory.Exists(Path.Combine(aDir, @"AssetBundleImages\jacket"))) continue;
foreach (var file in Directory.GetFiles(Path.Combine(aDir, @"AssetBundleImages\jacket")))
{
if (!imageExts.Contains(Path.GetExtension(file))) continue;
var idStr = Path.GetFileName(file).Substring("ui_jacket_".Length, 6);
jacketPaths[idStr] = file;
}
}
else

foreach (var laFile in Directory.EnumerateFiles(Path.Combine(Environment.CurrentDirectory, "LocalAssets")))
{
__result = texture;
var match = localAssetsJacketExt.Match(Path.GetFileName(laFile));
if (!match.Success) continue;
jacketPaths[match.Groups[1].Value] = laFile;
}

return false;
MelonLogger.Msg($"Loaded {jacketPaths.Count} custom jacket images.");
}

private static string GetJacketPath(string id)
{
foreach (var ext in new[] { ".jpg", ".png", ".webp", ".bmp", ".gif" })
{
if (File.Exists(Path.Combine(Environment.CurrentDirectory, "LocalAssets", id + ext)))
{
return Path.Combine(Environment.CurrentDirectory, "LocalAssets", id + ext);
}
}

return null;
return jacketPaths.GetValueOrDefault(id);
}

public static Texture2D GetJacketTexture2D(string id)
Expand Down

0 comments on commit 489c00e

Please sign in to comment.