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.

90 lines
2.7 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<IBeatSaberPlugin> plugins;
  10. private delegate void CompositeCall(IBeatSaberPlugin plugin);
  11. public CompositeBSPlugin(IEnumerable<IBeatSaberPlugin> plugins) {
  12. this.plugins = plugins;
  13. }
  14. public void OnApplicationStart() {
  15. Invoke(plugin => plugin.OnApplicationStart());
  16. }
  17. public void OnApplicationQuit() {
  18. Invoke(plugin => plugin.OnApplicationQuit());
  19. }
  20. public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) {
  21. foreach (var plugin in plugins) {
  22. try {
  23. plugin.OnSceneLoaded(scene, sceneMode);
  24. }
  25. catch (Exception ex) {
  26. Logger.log.Error($"{plugin.Name}: {ex}");
  27. }
  28. }
  29. }
  30. public void OnSceneUnloaded(Scene scene) {
  31. foreach (var plugin in plugins) {
  32. try {
  33. plugin.OnSceneUnloaded(scene);
  34. }
  35. catch (Exception ex) {
  36. Logger.log.Error($"{plugin.Name}: {ex}");
  37. }
  38. }
  39. }
  40. public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  41. foreach (var plugin in plugins) {
  42. try {
  43. plugin.OnActiveSceneChanged(prevScene, nextScene);
  44. }
  45. catch (Exception ex) {
  46. Logger.log.Error($"{plugin.Name}: {ex}");
  47. }
  48. }
  49. }
  50. private void Invoke(CompositeCall callback) {
  51. foreach (var plugin in plugins) {
  52. try {
  53. callback(plugin);
  54. }
  55. catch (Exception ex) {
  56. Logger.log.Error($"{plugin.Name}: {ex}");
  57. }
  58. }
  59. }
  60. public void OnUpdate() {
  61. Invoke(plugin => plugin.OnUpdate());
  62. }
  63. public void OnFixedUpdate() {
  64. Invoke(plugin => plugin.OnFixedUpdate());
  65. }
  66. public string Name => throw new NotImplementedException();
  67. public string Version => throw new NotImplementedException();
  68. public ModsaberModInfo ModInfo => throw new NotImplementedException();
  69. public void OnLateUpdate() {
  70. Invoke(plugin => {
  71. if (plugin is IEnhancedBeatSaberPlugin saberPlugin)
  72. saberPlugin.OnLateUpdate();
  73. });
  74. }
  75. }
  76. }