[+] Load Jacket from A???\AssetBundleImages\jacket\*.png

pull/52/head
Clansty 2024-09-02 17:06:34 +08:00
parent d58fe84439
commit 489c00ebb0
No known key found for this signature in database
GPG Key ID: 3A6BE8BAF2EDE134
1 changed files with 48 additions and 27 deletions

View File

@ -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)
{
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;
}
}
public static bool Prefix(string filename, ref Texture2D __result, AssetManager __instance)
private static string[] imageExts = [".jpg", ".png", ".jpeg"];
private static Regex localAssetsJacketExt = new(@"(\d{6})\.(png|jpg|jpeg)");
private static Dictionary<string, string> jacketPaths = new();
[HarmonyPrefix]
[HarmonyPatch(typeof(DataManager), "LoadMusicBase")]
public static void LoadMusicPostfix(List<string> ____targetDirs)
{
var matches = Regex.Matches(filename, @"UI_Jacket_(\d+)(_s)?\.png");
if (matches.Count < 1)
foreach (var aDir in ____targetDirs)
{
return true;
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;
}
}
var id = matches[0].Groups[1].Value;
var texture = GetJacketTexture2D(id);
if (texture is null)
foreach (var laFile in Directory.EnumerateFiles(Path.Combine(Environment.CurrentDirectory, "LocalAssets")))
{
__result = __instance.LoadAsset<Texture2D>($"Jacket/UI_Jacket_{id}.png");
}
else
{
__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)