You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.2 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine.SceneManagement;
  4. using Logger = IPA.Logging.Logger;
  5. namespace IPA.Loader.Composite
  6. {
  7. internal class CompositeBSPlugin : IBeatSaberPlugin
  8. {
  9. private readonly IEnumerable<PluginLoader.PluginInfo> plugins;
  10. private delegate void CompositeCall(PluginLoader.PluginInfo plugin);
  11. public CompositeBSPlugin(IEnumerable<PluginLoader.PluginInfo> plugins) {
  12. this.plugins = plugins;
  13. }
  14. public void OnApplicationStart() {
  15. Invoke(plugin => plugin.Plugin.OnApplicationStart());
  16. }
  17. public void OnApplicationQuit() {
  18. Invoke(plugin => plugin.Plugin.OnApplicationQuit());
  19. }
  20. public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) {
  21. Invoke(plugin => plugin.Plugin.OnSceneLoaded(scene, sceneMode));
  22. }
  23. public void OnSceneUnloaded(Scene scene) {
  24. Invoke(plugin => plugin.Plugin.OnSceneUnloaded(scene));
  25. }
  26. public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  27. Invoke(plugin => plugin.Plugin.OnActiveSceneChanged(prevScene, nextScene));
  28. }
  29. private void Invoke(CompositeCall callback) {
  30. foreach (var plugin in plugins) {
  31. try {
  32. callback(plugin);
  33. }
  34. catch (Exception ex) {
  35. Logger.log.Error($"{plugin.Metadata.Name}: {ex}");
  36. }
  37. }
  38. }
  39. public void OnUpdate() {
  40. Invoke(plugin => plugin.Plugin.OnUpdate());
  41. }
  42. public void OnFixedUpdate() {
  43. Invoke(plugin => plugin.Plugin.OnFixedUpdate());
  44. }
  45. public string Name => throw new InvalidOperationException();
  46. public string Version => throw new InvalidOperationException();
  47. public ModsaberModInfo ModInfo => throw new InvalidOperationException();
  48. public void OnLateUpdate() {
  49. Invoke(plugin => {
  50. if (plugin.Plugin is IEnhancedBeatSaberPlugin saberPlugin)
  51. saberPlugin.OnLateUpdate();
  52. });
  53. }
  54. }
  55. }