mirror of
https://github.com/All-Of-Us-Mods/MiraAPI.git
synced 2026-10-21 05:37:32 +00:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
245f2e6919 | ||
|
|
fed3abafb2 | ||
|
|
925d47a275 | ||
|
|
45a3597ab2 | ||
|
|
faeb39d3de | ||
|
|
6b46cffe5e | ||
|
|
00a60bb7ad | ||
|
|
d89bce6956 | ||
|
|
7f92e94293 | ||
|
|
6199e55110 |
No files matched your search
+2
-2
@@ -1,8 +1,8 @@
|
||||
<Project>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Reactor" Version="2.3.1" />
|
||||
<PackageReference Include="Reactor" Version="2.5.0-ci.371" />
|
||||
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.735" Private="false" ExcludeAssets="runtime;native" />
|
||||
<PackageReference Include="AmongUs.GameLibs.Steam" Version="2025.10.14" PrivateAssets="all" />
|
||||
<PackageReference Include="AmongUs.GameLibs.Steam" Version="2026.3.31" PrivateAssets="all" />
|
||||
|
||||
<PackageReference Include="BepInEx.AutoPlugin" Version="1.1.0" PrivateAssets="all" />
|
||||
<PackageReference Include="BepInEx.IL2CPP.MSBuild" Version="2.1.0-rc.1" PrivateAssets="all" ExcludeAssets="runtime" />
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
|
||||
|
||||
<VersionPrefix>0.3.8</VersionPrefix>
|
||||
<VersionPrefix>0.4.0</VersionPrefix>
|
||||
<VersionSuffix>dev</VersionSuffix>
|
||||
|
||||
<Authors>All Of Us, XtraCube, Angxl</Authors>
|
||||
|
||||
@@ -21,8 +21,6 @@ namespace MiraAPI.GameOptions;
|
||||
public static class ModdedOptionsManager
|
||||
{
|
||||
private static readonly Dictionary<PropertyInfo, ModdedOptionAttribute> OptionAttributes = [];
|
||||
private static readonly Dictionary<MethodBase, ModdedOptionAttribute> SetterAttributes = [];
|
||||
private static readonly Dictionary<MethodBase, ModdedOptionAttribute> GetterAttributes = [];
|
||||
private static readonly Dictionary<Type, AbstractOptionGroup> TypeToGroup = [];
|
||||
|
||||
internal static readonly Dictionary<OptionBehaviour, ModdedPlayerOption> CreatedPlayerOptions = [];
|
||||
@@ -102,20 +100,12 @@ public static class ModdedOptionsManager
|
||||
}
|
||||
|
||||
var setterOriginal = property.GetSetMethod();
|
||||
if (setterOriginal != null)
|
||||
{
|
||||
var setterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertySetterPatch));
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Harmony.Patch(setterOriginal, postfix: new HarmonyMethod(setterPatch));
|
||||
SetterAttributes[setterOriginal] = attribute;
|
||||
}
|
||||
var setterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertySetterPatch));
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Harmony.Patch(setterOriginal, postfix: new HarmonyMethod(setterPatch));
|
||||
|
||||
var getterOriginal = property.GetGetMethod();
|
||||
if (getterOriginal != null)
|
||||
{
|
||||
var getterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertyGetterPatch));
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Harmony.Patch(getterOriginal, prefix: new HarmonyMethod(getterPatch));
|
||||
GetterAttributes[getterOriginal] = attribute;
|
||||
}
|
||||
var getterPatch = typeof(ModdedOptionsManager).GetMethod(nameof(PropertyGetterPatch));
|
||||
PluginSingleton<MiraApiPlugin>.Instance.Harmony.Patch(getterOriginal, prefix: new HarmonyMethod(getterPatch));
|
||||
|
||||
OptionAttributes.Add(property, attribute);
|
||||
attribute.HolderOption = option;
|
||||
@@ -193,11 +183,7 @@ public static class ModdedOptionsManager
|
||||
public static void PropertySetterPatch(MethodBase __originalMethod, object value)
|
||||
#pragma warning restore CA1707
|
||||
{
|
||||
if (!SetterAttributes.TryGetValue(__originalMethod, out var attribute))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var attribute = OptionAttributes.First(pair => pair.Key.GetSetMethod() == __originalMethod).Value;
|
||||
attribute.SetValue(value);
|
||||
}
|
||||
|
||||
@@ -212,11 +198,7 @@ public static class ModdedOptionsManager
|
||||
public static bool PropertyGetterPatch(MethodBase __originalMethod, ref object __result)
|
||||
#pragma warning restore CA1707
|
||||
{
|
||||
if (!GetterAttributes.TryGetValue(__originalMethod, out var attribute))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var attribute = OptionAttributes.First(pair => pair.Key.GetGetMethod() == __originalMethod).Value;
|
||||
__result = attribute.GetValue();
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -93,11 +93,21 @@ public static class CustomMurderRpc
|
||||
beforeMurderEvent.Cancel();
|
||||
}
|
||||
|
||||
if (beforeMurderEvent.IsCancelled)
|
||||
if (target.ProtectedByGa())
|
||||
{
|
||||
beforeMurderEvent.Cancel();
|
||||
murderResultFlags = MurderResultFlags.FailedProtected;
|
||||
}
|
||||
else if (beforeMurderEvent.IsCancelled)
|
||||
{
|
||||
murderResultFlags = MurderResultFlags.FailedError;
|
||||
}
|
||||
|
||||
if (beforeMurderEvent.IsCancelled && source.AmOwner)
|
||||
{
|
||||
source.isKilling = true;
|
||||
}
|
||||
|
||||
if (!PlayerControl.LocalPlayer.IsHost())
|
||||
{
|
||||
return;
|
||||
@@ -141,6 +151,7 @@ public static class CustomMurderRpc
|
||||
{
|
||||
if (LobbyBehaviour.Instance || !host.IsHost() || target.Data.IsDead || target.Data.Disconnected)
|
||||
{
|
||||
source.isKilling = false;
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,8 +16,15 @@ public static class OptionsMenuBehaviourPatch
|
||||
private static void OpenPostfix(OptionsMenuBehaviour __instance)
|
||||
{
|
||||
_remap_rollover = GameObject.Find("Remap Controls")?.GetComponent<ButtonRolloverHandler>()!;
|
||||
_remap_background = _remap_rollover.transform.FindChild("Background").GetComponent<SpriteRenderer>();
|
||||
_remap_background.color = _conflicts ? Color.red : Color.white;
|
||||
try
|
||||
{
|
||||
_remap_background = _remap_rollover.transform.FindChild("Background").GetComponent<SpriteRenderer>();
|
||||
_remap_background.color = _conflicts ? Color.red : Color.white;
|
||||
}
|
||||
catch
|
||||
{
|
||||
// ignored, this normally breaks when on mobile since there's no remap button screen.
|
||||
}
|
||||
}
|
||||
|
||||
[HarmonyPostfix]
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using HarmonyLib;
|
||||
using System.Collections;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.LocalSettings;
|
||||
using MiraAPI.Utilities.Assets;
|
||||
using Reactor.Utilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Patches.Menu;
|
||||
@@ -19,6 +21,14 @@ public static class MainMenuManagerPatches
|
||||
public static void AwakePostfix()
|
||||
{
|
||||
AddressablesLoader.LoadAll();
|
||||
Coroutines.Start(SetFps());
|
||||
}
|
||||
|
||||
private static IEnumerator SetFps()
|
||||
{
|
||||
Application.targetFrameRate = (int)LocalSettingsTabSingleton<MiraApiSettings>.Instance.SetFpsSlider.Value;
|
||||
yield return new WaitForSeconds(1f);
|
||||
|
||||
Application.targetFrameRate = (int)LocalSettingsTabSingleton<MiraApiSettings>.Instance.SetFpsSlider.Value;
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using HarmonyLib;
|
||||
using MiraAPI.GameOptions;
|
||||
using MiraAPI.Modifiers;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Presets;
|
||||
using MiraAPI.Utilities.Assets;
|
||||
using Reactor.Utilities.Extensions;
|
||||
using TMPro;
|
||||
@@ -351,6 +352,11 @@ internal static class GameSettingMenuPatches
|
||||
|
||||
_modifierBtnOgPos = _modifiersButton.transform.localPosition;
|
||||
|
||||
foreach (var plugin in MiraPluginManager.Instance.RegisteredPlugins)
|
||||
{
|
||||
PresetManager.LoadPresets(plugin);
|
||||
}
|
||||
|
||||
UpdateText(__instance, __instance.GameSettingsTab, __instance.RoleSettingsTab);
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,6 @@ public static class RoleSettingMenuPatches
|
||||
private static Dictionary<RoleOptionsGroup, bool> RoleGroupHidden { get; set; } = [];
|
||||
private static List<GameObject> Headers { get; set; } = [];
|
||||
private static List<RoleOptionSetting> RoleOptionSettings { get; set; } = [];
|
||||
private static Dictionary<RoleBehaviour, Sprite> RoleScreenshotSprites { get; } = [];
|
||||
|
||||
private static float ScrollerNum { get; set; } = 0.522f;
|
||||
|
||||
@@ -507,17 +506,11 @@ public static class RoleSettingMenuPatches
|
||||
__instance.roleDescriptionText.transform.parent.localScale = new Vector3(0.0675f, 0.1494f, 0.5687f);
|
||||
labelBg.transform.localPosition = new Vector3(1.082f, 0.1054f, -2.5f);
|
||||
|
||||
if (!RoleScreenshotSprites.TryGetValue(role, out var screenshotSprite) || screenshotSprite == null)
|
||||
{
|
||||
screenshotSprite = Sprite.Create(
|
||||
role.RoleScreenshot.texture,
|
||||
new Rect(0, 0, 370, 230),
|
||||
Vector2.one / 2,
|
||||
100);
|
||||
RoleScreenshotSprites[role] = screenshotSprite;
|
||||
}
|
||||
|
||||
__instance.roleScreenshot.sprite = screenshotSprite;
|
||||
__instance.roleScreenshot.sprite = Sprite.Create(
|
||||
role.RoleScreenshot.texture,
|
||||
new Rect(0, 0, 370, 230),
|
||||
Vector2.one / 2,
|
||||
100);
|
||||
__instance.roleScreenshot.drawMode = SpriteDrawMode.Sliced;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using BepInEx.Configuration;
|
||||
using BepInEx.Unity.IL2CPP;
|
||||
using HarmonyLib;
|
||||
using MiraAPI.Colors;
|
||||
using MiraAPI.Events;
|
||||
using MiraAPI.GameEnd;
|
||||
@@ -23,7 +22,6 @@ using MiraAPI.Roles;
|
||||
using MiraAPI.Utilities;
|
||||
using Reactor.Networking;
|
||||
using Reactor.Utilities;
|
||||
using Rewired;
|
||||
|
||||
namespace MiraAPI.PluginLoading;
|
||||
|
||||
@@ -32,9 +30,6 @@ namespace MiraAPI.PluginLoading;
|
||||
/// </summary>
|
||||
public sealed class MiraPluginManager
|
||||
{
|
||||
private const BindingFlags DeclaredInstanceMembers = BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
|
||||
private const BindingFlags DeclaredStaticMembers = BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.DeclaredOnly;
|
||||
|
||||
private readonly Dictionary<Assembly, MiraPluginInfo> _registeredPlugins = [];
|
||||
|
||||
internal MiraPluginInfo[] RegisteredPlugins { get; private set; } = null!;
|
||||
@@ -54,24 +49,41 @@ public sealed class MiraPluginManager
|
||||
|
||||
var info = new MiraPluginInfo(miraPlugin, pluginInfo);
|
||||
var roles = new List<Type>();
|
||||
var registrationTimer = Stopwatch.StartNew();
|
||||
|
||||
var oldConfigSetting = info.PluginConfig.SaveOnConfigSet;
|
||||
info.PluginConfig.SaveOnConfigSet = false;
|
||||
|
||||
foreach (var type in GetAssemblyTypes(assembly))
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
if (type == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (type.GetCustomAttribute<MiraIgnoreAttribute>() != null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RegisterEvents(type);
|
||||
foreach (var method in AccessTools.GetDeclaredMethods(type))
|
||||
{
|
||||
var eventAttribute = method.GetCustomAttribute<RegisterEventAttribute>();
|
||||
if (eventAttribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!method.IsStatic)
|
||||
{
|
||||
Error($"Event method {method.Name} in {type.Name} must be static.");
|
||||
continue;
|
||||
}
|
||||
|
||||
var parameters = method.GetParameters();
|
||||
if (parameters.Length != 1 || !parameters[0].ParameterType.IsSubclassOf(typeof(MiraEvent)))
|
||||
{
|
||||
Error($"Invalid event registration method {method.Name} in {type.Name}");
|
||||
continue;
|
||||
}
|
||||
|
||||
var paramType = parameters[0].ParameterType;
|
||||
MiraEventManager.RegisterEventHandler(paramType, method, eventAttribute.Priority);
|
||||
}
|
||||
|
||||
if (RegisterModifier(type, info))
|
||||
{
|
||||
@@ -119,9 +131,7 @@ public sealed class MiraPluginManager
|
||||
info.SavePublicCollections();
|
||||
PresetManager.CreateDefaultPreset(info);
|
||||
PresetManager.LoadPresets(info);
|
||||
|
||||
registrationTimer.Stop();
|
||||
Info($"Registering mod {pluginInfo.Metadata.GUID} with Mira API in {registrationTimer.ElapsedMilliseconds}ms.");
|
||||
Info($"Registering mod {pluginInfo.Metadata.GUID} with Mira API.");
|
||||
};
|
||||
IL2CPPChainloader.Instance.Finished += PaletteManager.RegisterAllColors;
|
||||
IL2CPPChainloader.Instance.Finished += () =>
|
||||
@@ -149,45 +159,6 @@ public sealed class MiraPluginManager
|
||||
return Instance._registeredPlugins.Values.FirstOrDefault(plugin => plugin.PluginId == pluginId);
|
||||
}
|
||||
|
||||
private static void RegisterEvents(Type type)
|
||||
{
|
||||
foreach (var method in type.GetMethods(DeclaredStaticMembers))
|
||||
{
|
||||
var eventAttribute = method.GetCustomAttribute<RegisterEventAttribute>();
|
||||
if (eventAttribute == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var parameters = method.GetParameters();
|
||||
if (parameters.Length != 1 || !typeof(MiraEvent).IsAssignableFrom(parameters[0].ParameterType))
|
||||
{
|
||||
Error($"Invalid event registration method {method.Name} in {type.Name}");
|
||||
continue;
|
||||
}
|
||||
|
||||
MiraEventManager.RegisterEventHandler(parameters[0].ParameterType, method, eventAttribute.Priority);
|
||||
}
|
||||
}
|
||||
|
||||
private static Type?[] GetAssemblyTypes(Assembly assembly)
|
||||
{
|
||||
try
|
||||
{
|
||||
return assembly.GetTypes();
|
||||
}
|
||||
catch (ReflectionTypeLoadException exception)
|
||||
{
|
||||
return exception.Types;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsAutoPropertyBackingField(FieldInfo field)
|
||||
{
|
||||
return field.IsDefined(typeof(CompilerGeneratedAttribute), false) &&
|
||||
field.Name.Contains("k__BackingField", StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
private static bool RegisterGameOver(Type type)
|
||||
{
|
||||
try
|
||||
@@ -215,7 +186,7 @@ public sealed class MiraPluginManager
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach (var property in type.GetProperties(DeclaredInstanceMembers))
|
||||
foreach (var property in type.GetProperties())
|
||||
{
|
||||
if (property.GetMethod?.IsStatic == true)
|
||||
{
|
||||
@@ -238,7 +209,7 @@ public sealed class MiraPluginManager
|
||||
ModdedOptionsManager.RegisterAttributeOption(type, attribute, property, pluginInfo);
|
||||
}
|
||||
|
||||
foreach (var field in type.GetFields(DeclaredInstanceMembers).Where(f => !IsAutoPropertyBackingField(f) && f.FieldType.IsAssignableTo(typeof(IModdedOption))))
|
||||
foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(IModdedOption))))
|
||||
{
|
||||
Error($"{field.Name} is a field, not a property. Use properties for options.");
|
||||
}
|
||||
@@ -293,7 +264,7 @@ public sealed class MiraPluginManager
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var property in type.GetProperties(DeclaredStaticMembers))
|
||||
foreach (var property in type.GetProperties())
|
||||
{
|
||||
if (property.PropertyType != typeof(CustomColor))
|
||||
{
|
||||
@@ -309,7 +280,7 @@ public sealed class MiraPluginManager
|
||||
PaletteManager.CustomColors.Add(color);
|
||||
}
|
||||
|
||||
foreach (var field in type.GetFields(DeclaredStaticMembers).Where(f => !IsAutoPropertyBackingField(f) && f.FieldType.IsAssignableTo(typeof(CustomColor))))
|
||||
foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(CustomColor))))
|
||||
{
|
||||
Error($"{field.Name} is a field, not a property. Use properties for colors.");
|
||||
}
|
||||
@@ -361,13 +332,13 @@ public sealed class MiraPluginManager
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!LocalSettingsManager.TypeToTab.TryGetValue(type, out var tabInstance))
|
||||
foreach (var property in type.GetProperties())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (LocalSettingsManager.TypeToTab[type] is not { } tabInstance)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach (var property in type.GetProperties(DeclaredInstanceMembers))
|
||||
{
|
||||
if (property.GetCustomAttribute<LocalSettingsButtonAttribute>() != null &&
|
||||
property.PropertyType.IsAssignableTo(typeof(LocalSettingsButton)))
|
||||
{
|
||||
@@ -403,7 +374,7 @@ public sealed class MiraPluginManager
|
||||
attribute.CreateSetting(type, configEntry);
|
||||
}
|
||||
|
||||
foreach (var field in type.GetFields(DeclaredInstanceMembers).Where(f => !IsAutoPropertyBackingField(f) && f.FieldType.IsAssignableTo(typeof(ConfigEntryBase)) && f.GetCustomAttribute<LocalSettingAttribute>() != null))
|
||||
foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(ConfigEntryBase)) && f.GetCustomAttribute<LocalSettingAttribute>() != null))
|
||||
{
|
||||
Error($"{field.Name} is a field, not a property. Use properties for local settings.");
|
||||
}
|
||||
@@ -433,7 +404,7 @@ public sealed class MiraPluginManager
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var property in type.GetProperties(DeclaredStaticMembers))
|
||||
foreach (var property in type.GetProperties())
|
||||
{
|
||||
if (property.PropertyType != typeof(MiraKeybind))
|
||||
{
|
||||
@@ -457,7 +428,7 @@ public sealed class MiraPluginManager
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var field in type.GetFields(DeclaredStaticMembers).Where(f => !IsAutoPropertyBackingField(f) && f.FieldType.IsAssignableTo(typeof(MiraKeybind))))
|
||||
foreach (var field in type.GetFields().Where(f => f.FieldType.IsAssignableTo(typeof(MiraKeybind))))
|
||||
{
|
||||
Error($"{field.Name} is a field, not a property. Use properties for keybinds.");
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using System.Linq;
|
||||
using BepInEx.Configuration;
|
||||
using MiraAPI.PluginLoading;
|
||||
using MiraAPI.Roles;
|
||||
using Reactor.Utilities;
|
||||
using UnityEngine;
|
||||
|
||||
namespace MiraAPI.Presets;
|
||||
@@ -78,6 +79,8 @@ public static class PresetManager
|
||||
|
||||
foreach (var file in Directory.GetFiles(pluginPresetPath, "*.cfg"))
|
||||
{
|
||||
var fileName = Path.GetFileName(file);
|
||||
Info($"Loading preset file {fileName}");
|
||||
var presetName = Path.GetFileNameWithoutExtension(file);
|
||||
var presetConfig = new ConfigFile(file, false)
|
||||
{
|
||||
@@ -128,6 +131,9 @@ public static class PresetManager
|
||||
|
||||
foreach (var file in Directory.GetFiles(pluginPresetPath, "*.cfg"))
|
||||
{
|
||||
var fileName = Path.GetFileName(file);
|
||||
Info($"Loading preset file {fileName}");
|
||||
var presetName = Path.GetFileNameWithoutExtension(file);
|
||||
var presetConfig = new ConfigFile(file, false)
|
||||
{
|
||||
SaveOnConfigSet = false,
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Resources;
|
||||
@@ -14,8 +13,6 @@ namespace MiraAPI.Utilities.Assets;
|
||||
/// <param name="path">The path of the wave file.</param>
|
||||
public class LoadableAudioResourceAsset(string path) : LoadableAsset<AudioClip>
|
||||
{
|
||||
private static readonly object CacheLock = new();
|
||||
private static readonly Dictionary<(Assembly Assembly, string Path), AudioClip> AudioClipCache = [];
|
||||
private readonly Assembly _assembly = Assembly.GetCallingAssembly();
|
||||
|
||||
/// <summary>
|
||||
@@ -26,20 +23,6 @@ public class LoadableAudioResourceAsset(string path) : LoadableAsset<AudioClip>
|
||||
/// <exception cref="MissingManifestResourceException">Stream failed to load. Check if the name of your asset was correct.</exception>
|
||||
public override AudioClip LoadAsset()
|
||||
{
|
||||
if (LoadedAsset != null)
|
||||
{
|
||||
return LoadedAsset;
|
||||
}
|
||||
|
||||
lock (CacheLock)
|
||||
{
|
||||
if (AudioClipCache.TryGetValue((_assembly, path), out var cachedClip) && cachedClip != null)
|
||||
{
|
||||
LoadedAsset = cachedClip;
|
||||
return cachedClip;
|
||||
}
|
||||
}
|
||||
|
||||
using var assetStream = _assembly.GetManifestResourceStream(path)
|
||||
?? throw new MissingManifestResourceException($"Stream failed to load. Check if the asset name is correct: {path}");
|
||||
|
||||
@@ -109,13 +92,6 @@ public class LoadableAudioResourceAsset(string path) : LoadableAsset<AudioClip>
|
||||
var audioClip = AudioClip.Create(path, samples.Length / channels, channels, sampleRate, false);
|
||||
audioClip.SetData(samples, 0);
|
||||
|
||||
LoadedAsset = audioClip;
|
||||
|
||||
lock (CacheLock)
|
||||
{
|
||||
AudioClipCache[(_assembly, path)] = audioClip;
|
||||
}
|
||||
|
||||
return audioClip;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using Reactor.Utilities.Extensions;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -13,9 +12,6 @@ namespace MiraAPI.Utilities.Assets;
|
||||
/// <typeparam name="T">The type of the asset to be loaded.</typeparam>
|
||||
public class LoadableBundleAsset<T>(string name, AssetBundle bundle) : LoadableAsset<T> where T : UnityEngine.Object
|
||||
{
|
||||
private static readonly object CacheLock = new();
|
||||
private static readonly Dictionary<(int BundleId, Type AssetType, string Name), UnityEngine.Object> BundleAssetCache = [];
|
||||
|
||||
/// <summary>
|
||||
/// Loads the asset from the asset bundle.
|
||||
/// </summary>
|
||||
@@ -28,16 +24,6 @@ public class LoadableBundleAsset<T>(string name, AssetBundle bundle) : LoadableA
|
||||
return LoadedAsset;
|
||||
}
|
||||
|
||||
var cacheKey = (bundle.GetInstanceID(), typeof(T), name);
|
||||
lock (CacheLock)
|
||||
{
|
||||
if (BundleAssetCache.TryGetValue(cacheKey, out var cachedAsset) && cachedAsset is T typedAsset)
|
||||
{
|
||||
LoadedAsset = typedAsset;
|
||||
return typedAsset;
|
||||
}
|
||||
}
|
||||
|
||||
LoadedAsset = bundle.LoadAsset<T>(name);
|
||||
|
||||
if (LoadedAsset == null)
|
||||
@@ -45,11 +31,6 @@ public class LoadableBundleAsset<T>(string name, AssetBundle bundle) : LoadableA
|
||||
throw new InvalidOperationException($"INVALID ASSET: {name}");
|
||||
}
|
||||
|
||||
lock (CacheLock)
|
||||
{
|
||||
BundleAssetCache[cacheKey] = LoadedAsset;
|
||||
}
|
||||
|
||||
return LoadedAsset;
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
using Reactor.Utilities.Extensions;
|
||||
using UnityEngine;
|
||||
@@ -11,10 +10,6 @@ namespace MiraAPI.Utilities.Assets;
|
||||
/// </summary>
|
||||
public static class SpriteTools
|
||||
{
|
||||
private static readonly object CacheLock = new();
|
||||
private static readonly Dictionary<(Assembly Assembly, string ResourcePath), Texture2D> TextureCache = [];
|
||||
private static readonly Dictionary<(Assembly Assembly, string ResourcePath, float PixelsPerUnit), Sprite> SpriteCache = [];
|
||||
|
||||
/// <summary>
|
||||
/// Loads and returns a texture from a resource path using the specified assembly.
|
||||
/// </summary>
|
||||
@@ -24,20 +19,11 @@ public static class SpriteTools
|
||||
/// <exception cref="ArgumentException">Thrown when the resource cannot be found in the specified assembly.</exception>
|
||||
public static Texture2D LoadTextureFromResourcePath(string resourcePath, Assembly assembly)
|
||||
{
|
||||
lock (CacheLock)
|
||||
{
|
||||
if (TextureCache.TryGetValue((assembly, resourcePath), out var cachedTexture) && cachedTexture != null)
|
||||
{
|
||||
return cachedTexture;
|
||||
}
|
||||
}
|
||||
|
||||
var tex = new Texture2D(1, 1, TextureFormat.ARGB32, false)
|
||||
{
|
||||
wrapMode = TextureWrapMode.Clamp,
|
||||
};
|
||||
|
||||
using var myStream = assembly.GetManifestResourceStream(resourcePath);
|
||||
var myStream = assembly.GetManifestResourceStream(resourcePath);
|
||||
if (myStream != null)
|
||||
{
|
||||
var buttonTexture = myStream.ReadFully();
|
||||
@@ -49,12 +35,6 @@ public static class SpriteTools
|
||||
}
|
||||
|
||||
tex.name = resourcePath;
|
||||
|
||||
lock (CacheLock)
|
||||
{
|
||||
TextureCache[(assembly, resourcePath)] = tex;
|
||||
}
|
||||
|
||||
return tex;
|
||||
}
|
||||
|
||||
@@ -67,23 +47,9 @@ public static class SpriteTools
|
||||
/// <returns>A <see cref="Sprite"/> object created from the texture loaded from the specified resource path.</returns>
|
||||
public static Sprite LoadSpriteFromPath(string resourcePath, Assembly assembly, float pixelsPerUnit)
|
||||
{
|
||||
lock (CacheLock)
|
||||
{
|
||||
if (SpriteCache.TryGetValue((assembly, resourcePath, pixelsPerUnit), out var cachedSprite) && cachedSprite != null)
|
||||
{
|
||||
return cachedSprite;
|
||||
}
|
||||
}
|
||||
|
||||
var tex = LoadTextureFromResourcePath(resourcePath, assembly);
|
||||
var sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f), pixelsPerUnit);
|
||||
sprite.name = resourcePath;
|
||||
|
||||
lock (CacheLock)
|
||||
{
|
||||
SpriteCache[(assembly, resourcePath, pixelsPerUnit)] = sprite;
|
||||
}
|
||||
|
||||
return sprite;
|
||||
}
|
||||
}
|
||||
@@ -181,6 +181,16 @@ public static class Extensions
|
||||
return TutorialManager.InstanceExists || AmongUsClient.Instance.HostId == playerControl.OwnerId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a PlayerControl is protected by a Guardian Angel.
|
||||
/// </summary>
|
||||
/// <param name="playerControl">The player you're checking for.</param>
|
||||
/// <returns>If the player is protected, true, else false.</returns>
|
||||
public static bool ProtectedByGa(this PlayerControl playerControl)
|
||||
{
|
||||
return playerControl.protectedByGuardianId > -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used to convert a System.Collections.Generic.List to Il2cppSystem.
|
||||
/// </summary>
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using HarmonyLib;
|
||||
using Il2CppInterop.Runtime.InteropTypes;
|
||||
using Object = Il2CppSystem.Object;
|
||||
|
||||
namespace MiraAPI.Utilities;
|
||||
|
||||
@@ -17,6 +18,7 @@ public class StateMachineWrapper<T> where T : Il2CppObjectBase
|
||||
// normally it is fields, but IL2CPP turns them into properties
|
||||
private readonly PropertyInfo _thisProperty;
|
||||
private readonly PropertyInfo _stateProperty;
|
||||
private readonly PropertyInfo _currentProperty;
|
||||
private readonly Dictionary<string, PropertyInfo> _propertyCache;
|
||||
|
||||
private T? _parentInstance;
|
||||
@@ -37,8 +39,9 @@ public class StateMachineWrapper<T> where T : Il2CppObjectBase
|
||||
var type = _stateMachine.GetType();
|
||||
_thisProperty = AccessTools.Property(type, "__4__this");
|
||||
_stateProperty = AccessTools.Property(type, "__1__state");
|
||||
_currentProperty = AccessTools.Property(type, "__2__current");
|
||||
|
||||
if (_thisProperty == null || _stateProperty == null)
|
||||
if (_thisProperty == null || _stateProperty == null || _currentProperty == null)
|
||||
{
|
||||
throw new MissingMemberException($"Could not find required properties in type '{type}'.");
|
||||
}
|
||||
@@ -52,6 +55,18 @@ public class StateMachineWrapper<T> where T : Il2CppObjectBase
|
||||
/// <returns>The current state as an integer.</returns>
|
||||
public int GetState() => (int)_stateProperty.GetValue(_stateMachine)!;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the current state of the state machine.
|
||||
/// </summary>
|
||||
/// <param name="newState">The new state to use.</param>
|
||||
public void SetState(int newState) => _stateProperty.SetValue(_stateMachine, newState);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the newest yield return of the state machine.
|
||||
/// </summary>
|
||||
/// <param name="newReturn">The new return to use.</param>
|
||||
public void SetRecentReturn(Object newReturn) => _currentProperty.SetValue(_stateMachine, newReturn);
|
||||
|
||||
/// <summary>
|
||||
/// Gets a parameter from the state machine by its name.
|
||||
/// </summary>
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user