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.

73 lines
2.3 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 :
  8. #pragma warning disable CS0618 // Type or member is obsolete
  9. IBeatSaberPlugin,
  10. #pragma warning restore CS0618 // Type or member is obsolete
  11. _IPlugin
  12. {
  13. private readonly IEnumerable<PluginLoader.PluginInfo> plugins;
  14. private delegate void CompositeCall(PluginLoader.PluginInfo plugin);
  15. public CompositeBSPlugin(IEnumerable<PluginLoader.PluginInfo> plugins) {
  16. this.plugins = plugins;
  17. }
  18. [Obsolete]
  19. public void OnApplicationStart() {
  20. Invoke(plugin => (plugin.Plugin as IBeatSaberPlugin)?.OnApplicationStart());
  21. }
  22. public void OnApplicationQuit() {
  23. Invoke(plugin => plugin.Plugin.OnApplicationQuit());
  24. }
  25. public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) {
  26. Invoke(plugin => plugin.Plugin.OnSceneLoaded(scene, sceneMode));
  27. }
  28. public void OnSceneUnloaded(Scene scene) {
  29. Invoke(plugin => plugin.Plugin.OnSceneUnloaded(scene));
  30. }
  31. public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  32. Invoke(plugin => plugin.Plugin.OnActiveSceneChanged(prevScene, nextScene));
  33. }
  34. private void Invoke(CompositeCall callback) {
  35. foreach (var plugin in plugins) {
  36. try {
  37. if (plugin.Plugin != null)
  38. callback(plugin);
  39. }
  40. catch (Exception ex) {
  41. Logger.log.Error($"{plugin.Metadata.Name}: {ex}");
  42. }
  43. }
  44. }
  45. public void OnUpdate() {
  46. Invoke(plugin => plugin.Plugin.OnUpdate());
  47. }
  48. public void OnFixedUpdate() {
  49. Invoke(plugin => plugin.Plugin.OnFixedUpdate());
  50. }
  51. public string Name => throw new InvalidOperationException();
  52. public string Version => throw new InvalidOperationException();
  53. public void OnLateUpdate() {
  54. Invoke(plugin => {
  55. if (plugin.Plugin is IGenericEnhancedPlugin saberPlugin)
  56. saberPlugin.OnLateUpdate();
  57. });
  58. }
  59. }
  60. }