Skip to content

Commit

Permalink
[+] Load png TabTitle
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Sep 6, 2024
1 parent 0eec8de commit 6580b78
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 99 deletions.
2 changes: 1 addition & 1 deletion AquaMai/AquaMai.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
<Compile Include="UX\ExtendTimer.cs" />
<Compile Include="UX\HideSelfMadeCharts.cs" />
<Compile Include="UX\ImmediateSave.cs" />
<Compile Include="UX\LoadJacketPng.cs" />
<Compile Include="UX\LoadAssetsPng.cs" />
<Compile Include="UX\LoadAssetBundleWithoutManifest.cs" />
<Compile Include="UX\LoadLocalBga.cs" />
<Compile Include="UX\QuickSkip.cs" />
Expand Down
4 changes: 3 additions & 1 deletion AquaMai/AquaMai.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ SinglePlayer=true
SkipToMusicSelection=false
# Set the version string displayed at the top-right corner of the screen
CustomVersionString=""
# Deprecated: Use `LoadAssetsPng` instead
# LoadJacketPng=true
# Load Jacket image from folder "LocalAssets" and filename "{MusicID}.png" for self-made charts
LoadJacketPng=true
LoadAssetsPng=true
# Use the png jacket above as BGA if BGA is not found for self-made charts
# Use together with `LoadJacketPng`
LoadLocalBga=true
Expand Down
4 changes: 3 additions & 1 deletion AquaMai/AquaMai.zh.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ SinglePlayer=true
SkipToMusicSelection=false
# 把右上角的版本更改为自定义文本
CustomVersionString=""
# 已弃用,请使用 LoadAssetsPng
# LoadJacketPng=true
# 通过游戏目录下 `LocalAssets\000000(歌曲 ID).png` 加载封面,自制谱用
LoadJacketPng=true
LoadAssetsPng=true
# 如果没有 dat 格式的 BGA 的话,就用歌曲的封面做背景,而不是显示迪拉熊的笑脸
# 请和 `LoadJacketPng` 一起用
LoadLocalBga=true
Expand Down
2 changes: 2 additions & 0 deletions AquaMai/Config.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using Tomlet.Attributes;

namespace AquaMai
{
Expand All @@ -23,6 +24,7 @@ public class UXConfig
public bool SkipWarningScreen { get; set; }
public bool SinglePlayer { get; set; }
public bool SkipToMusicSelection { get; set; }
public bool LoadAssetsPng { get; set; }
public bool LoadJacketPng { get; set; }
public bool LoadAssetBundleWithoutManifest { get; set; }
public bool QuickSkip { get; set; }
Expand Down
4 changes: 4 additions & 0 deletions AquaMai/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ public override void OnInitializeMelon()
// Read AquaMai.toml to load settings
AppConfig = TomletMain.To<Config>(System.IO.File.ReadAllText("AquaMai.toml"));

// Migrate old settings
AppConfig.UX.LoadAssetsPng = AppConfig.UX.LoadAssetsPng || AppConfig.UX.LoadJacketPng;
AppConfig.UX.LoadJacketPng = false;

// Fixes that does not have side effects
// These don't need to be configurable

Expand Down
171 changes: 171 additions & 0 deletions AquaMai/UX/LoadAssetsPng.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using HarmonyLib;
using UnityEngine;
using System.Text.RegularExpressions;
using MAI2.Util;
using Manager;
using MelonLoader;
using Monitor;
using Object = UnityEngine.Object;

namespace AquaMai.UX;

public class LoadAssetsPng
{
private static string[] imageExts = [".jpg", ".png", ".jpeg"];
private static Dictionary<string, string> jacketPaths = new();
private static Dictionary<string, string> tabTitlePaths = new();
private static Dictionary<string, string> localAssetsContents = new();

[HarmonyPrefix]
[HarmonyPatch(typeof(DataManager), "LoadMusicBase")]
public static void LoadMusicPostfix(List<string> ____targetDirs)
{
foreach (var aDir in ____targetDirs)
{
if (Directory.Exists(Path.Combine(aDir, @"AssetBundleImages\jacket")))
foreach (var file in Directory.GetFiles(Path.Combine(aDir, @"AssetBundleImages\jacket")))
{
if (!imageExts.Contains(Path.GetExtension(file).ToLowerInvariant())) continue;
var idStr = Path.GetFileName(file).Substring("ui_jacket_".Length, 6);
jacketPaths[idStr] = file;
}

if (Directory.Exists(Path.Combine(aDir, @"Common\Sprites\Tab\Title")))
foreach (var file in Directory.GetFiles(Path.Combine(aDir, @"Common\Sprites\Tab\Title")))
{
if (!imageExts.Contains(Path.GetExtension(file).ToLowerInvariant())) continue;
tabTitlePaths[Path.GetFileNameWithoutExtension(file).ToLowerInvariant()] = file;
}
}

MelonLogger.Msg($"[LoadAssetsPng] Loaded {jacketPaths.Count} Jacket, {tabTitlePaths.Count} Tab Titles from AssetBundleImages.");

if (Directory.Exists(Path.Combine(Environment.CurrentDirectory, "LocalAssets")))
foreach (var laFile in Directory.EnumerateFiles(Path.Combine(Environment.CurrentDirectory, "LocalAssets")))
{
if (!imageExts.Contains(Path.GetExtension(laFile).ToLowerInvariant())) continue;
localAssetsContents[Path.GetFileNameWithoutExtension(laFile).ToLowerInvariant()] = laFile;
}

MelonLogger.Msg($"[LoadAssetsPng] Loaded {localAssetsContents.Count} LocalAssets.");
}

private static string GetJacketPath(string id)
{
return localAssetsContents.TryGetValue(id, out var laPath) ? laPath : jacketPaths.GetValueOrDefault(id);
}

public static Texture2D GetJacketTexture2D(string id)
{
var path = GetJacketPath(id);
if (path == null)
{
return null;
}

var texture = new Texture2D(1, 1);
texture.LoadImage(File.ReadAllBytes(path));
return texture;
}

public static Texture2D GetJacketTexture2D(int id)
{
return GetJacketTexture2D($"{id:000000}");
}

/*
[HarmonyPatch]
public static class TabTitleLoader
{
public static IEnumerable<MethodBase> TargetMethods()
{
// Fxxk unity
// game load tab title by call Resources.Load<Sprite> directly
// patching Resources.Load<Sprite> need this stuff
// var method = typeof(Resources).GetMethods(BindingFlags.Public | BindingFlags.Static).First(it => it.Name == "Load" && it.IsGenericMethod).MakeGenericMethod(typeof(Sprite));
// return [method];
// but it not work, game will blackscreen if add prefix or postfix
//
// patching AssetBundleManager.LoadAsset will lead game memory error
// return [AccessTools.Method(typeof(AssetBundleManager), "LoadAsset", [typeof(string)], [typeof(Object)])];
// and this is not work because game not using this
//
// we load them manually after game load and no need to hook the load progress
}
public static bool Prefix(string path, ref Object __result)
{
if (!path.StartsWith("Common/Sprites/Tab/Title/")) return true;
var filename = Path.GetFileNameWithoutExtension(path).ToLowerInvariant();
var locPath = localAssetsContents.TryGetValue(filename, out var laPath) ? laPath : tabTitlePaths.GetValueOrDefault(filename);
if (locPath is null) return true;
var texture = new Texture2D(1, 1);
texture.LoadImage(File.ReadAllBytes(locPath));
__result = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
MelonLogger.Msg($"GetTabTitleSpritePrefix {locPath} {__result}");
return false;
}
}
*/

[HarmonyPostfix]
[HarmonyPatch(typeof(MusicSelectMonitor), "Initialize")]
public static void TabTitleLoader(MusicSelectMonitor __instance, Dictionary<int, Sprite> ____genreSprite, Dictionary<int, Sprite> ____versionSprite)
{
var genres = Singleton<DataManager>.Instance.GetMusicGenres();
foreach (var (id, genre) in genres)
{
if (____genreSprite.GetValueOrDefault(id) is not null) continue;
var filename = genre.FileName.ToLowerInvariant();
var locPath = localAssetsContents.TryGetValue(filename, out var laPath) ? laPath : tabTitlePaths.GetValueOrDefault(filename);
if (locPath is null) continue;
var texture = new Texture2D(1, 1);
texture.LoadImage(File.ReadAllBytes(locPath));
____genreSprite[id] = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}

var versions = Singleton<DataManager>.Instance.GetMusicVersions();
foreach (var (id, version) in versions)
{
if (____versionSprite.GetValueOrDefault(id) is not null) continue;
var filename = version.FileName.ToLowerInvariant();
var locPath = localAssetsContents.TryGetValue(filename, out var laPath) ? laPath : tabTitlePaths.GetValueOrDefault(filename);
if (locPath is null) continue;
var texture = new Texture2D(1, 1);
texture.LoadImage(File.ReadAllBytes(locPath));
____versionSprite[id] = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
}
}

[HarmonyPatch]
public static class JacketLoader
{
public static IEnumerable<MethodBase> TargetMethods()
{
var AM = typeof(AssetManager);
return [AM.GetMethod("GetJacketThumbTexture2D", [typeof(string)]), AM.GetMethod("GetJacketTexture2D", [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;
}
}
}
95 changes: 0 additions & 95 deletions AquaMai/UX/LoadJacketPng.cs

This file was deleted.

2 changes: 1 addition & 1 deletion AquaMai/UX/LoadLocalBga.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static void LoadLocalBgaAwake(GameObject ____movieMaskObj)
var moviePath = string.Format(Singleton<OptionDataManager>.Instance.GetMovieDataPath($"{music.movieName.id:000000}") + ".dat");
if (!moviePath.Contains("dummy")) return;

var jacket = LoadJacketPng.GetJacketTexture2D(music.movieName.id);
var jacket = LoadAssetsPng.GetJacketTexture2D(music.movieName.id);
if (jacket is null)
{
MelonLogger.Msg("No jacket found for music " + music);
Expand Down

0 comments on commit 6580b78

Please sign in to comment.