-
Notifications
You must be signed in to change notification settings - Fork 1
/
BankAPI.cs
117 lines (114 loc) · 5.32 KB
/
BankAPI.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using BepInEx;
using FMODUnity;
using TNHBGLoader;
using TNHBGLoader.Soundtrack;
using UnityEngine;
namespace TNH_BGLoader
{
public class BankAPI
{
//Bank Index, Bank Name, Bank Location
//Standard: Bank Index
public static List<string> LoadedBankLocations = new List<string>();
public static int CurrentBankIndex = 0;
public static string CurrentBankLocation => LoadedBankLocations[CurrentBankIndex];
//i don't actually think this will ever return true. TODO: check
public static bool BanksEmptyOrNull => (LoadedBankLocations == null || LoadedBankLocations.Count == 0);
public static List<string> GetLegacyBanks() {
List<string> banks = new List<string>();
banks.Add(Path.Combine(Application.streamingAssetsPath, "MX_TAH.bank")); //the loader patch just checks for MX_TAH, not the full root path so this bypasses the check
// surely this won't throw an access error!
banks = banks.Concat(Directory.GetFiles(Paths.PluginPath, "MX_TAH_*.bank", SearchOption.AllDirectories).ToList()).ToList();
// removes all files with parent dir "resources"
foreach (var bank in banks)
if (Path.GetFileName(Path.GetDirectoryName(bank))?.ToLower() == "resources")
LoadedBankLocations.Remove(bank);
PluginMain.DebugLog.LogInfo(banks.Count + " banks loaded via legacy bank loader!");
// i'm supposed to ignore any files thrown into the plugin folder, but idk how to do that. toodles!
return banks;
}
public static string GetNameFromLocation(string loc) => Path.GetFileNameWithoutExtension(loc).Split('_').Last();
public static string GetNameFromIndex(int index, bool returnWithIndex = false)
{
string bankpath = BankAPI.LoadedBankLocations[index];
string bankname = Path.GetFileNameWithoutExtension(bankpath).Split('_').Last();
if (bankname == "TAH") bankname = "Default";
if (returnWithIndex) bankname = (index + 1) + ": " + bankname;
return bankname;
}
public delegate void bevent(); public static event bevent OnBankSwapped;
public static void SwapBank(int newBank)
{
//If over the # of banks, overflow into Soundtrack API.
//if (newBank > LoadedBankLocations.Count - 1) {
//Due to some changes, it should never reach this point.
//Not gonna touch it though. Too scared it'll still break smthn.
//Shit over the workload to the soundtrack API.
//newBank - LoadedBankLocations.Count results in the # as it corresponds to in the soundtrack data
//Ex: If you have 5 Banks, and you ask for Index 5 (6th bank), it corresponds to Index 0 (1st soundtrack)
//Yeah, its hacky. But the man who plans still hasn't started and my code works.
//SoundtrackAPI.LoadSoundtrack(newBank - LoadedBankLocations.Count);
//} else { //If not overflow, do outdated Bank method.
//Ensure the game doesn't think we're doing soundtrack method.
//Flagging is done in SoundtrackAPI.LoadSoundtrack.
PluginMain.IsSoundtrack.Value = false;
UnloadBankHard(CurrentBankLocation); //force it to be unloaded
CurrentBankIndex = newBank; //set banknum to new bank
NukeSongSnippets();
//load new bank (MX_TAH sets off the patcher Patcher_FMOD.FMODRuntimeManagerPatch_LoadBank)
RuntimeManager.LoadBank("MX_TAH");
PluginMain.LastLoadedBank.Value =
Path.GetFileNameWithoutExtension(CurrentBankLocation); //set last loaded bank
//}
}
public static void NukeSongSnippets()
{
if (OnBankSwapped != null) OnBankSwapped(); //null moment!
}
//literal copy of RuntimeManager.UnloadBank but hard unloads
public static void UnloadBankHard(string bankName)
{
PluginMain.DebugLog.LogInfo("Hard unloading " + Path.GetFileName(bankName));
RuntimeManager.LoadedBank value;
if (RuntimeManager.Instance.loadedBanks.TryGetValue(bankName, out value)) {
string name = "";
value.Bank.getPath(out name);
PluginMain.DebugLog.LogInfo($"Loaded bank that's being unloaded: {name}");
value.RefCount = 0;
value.Bank.unload();
RuntimeManager.Instance.loadedBanks.Remove(bankName);
}
}
//please co-routine this. doing this on the main thread is just asking for a freeze
//granted it's 256x256 (usually), how hard can it be to load that?
public static Texture2D GetBankIcon(string bankName)
{
if (bankName == "Your Mix") {
var mixes = Directory.GetFiles(PluginMain.AssemblyDirectory + "/default/", "your_mix_*.png", SearchOption.TopDirectoryOnly);
var mix = mixes[UnityEngine.Random.Range(0, mixes.Length)];
return GeneralAPI.GetIcon(bankName, new [] {mix});
}
//get the name and base path of the bank
var pbase = Path.GetDirectoryName(bankName) + "/";
var name = Path.GetFileNameWithoutExtension(bankName).Split('_').Last();
//assembles all the potential locations for the icon, in descending order of importance.
string[] paths = new string[] //this is fucking terrible. less so than the announcer one tho
{
pbase + name + ".png",
Directory.GetParent(pbase) + name + ".png",
pbase + "iconhq.png",
Directory.GetParent(pbase) + "iconhq.png",
pbase + "icon.png",
Directory.GetParent(pbase) + "icon.png"
};
//get default bank loc
if (bankName == Path.Combine(Application.streamingAssetsPath, "MX_TAH.bank"))
paths = new string[] {PluginMain.AssemblyDirectory + "/default/bank_default.png"};
return GeneralAPI.GetIcon(bankName, paths);
}
}
}