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.

64 lines
2.0 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. {
  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 OnApplicationQuit() {
  15. Invoke(plugin => plugin.Plugin.OnApplicationQuit());
  16. }
  17. public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) {
  18. Invoke(plugin => plugin.Plugin.OnSceneLoaded(scene, sceneMode));
  19. }
  20. public void OnSceneUnloaded(Scene scene) {
  21. Invoke(plugin => plugin.Plugin.OnSceneUnloaded(scene));
  22. }
  23. public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  24. Invoke(plugin => plugin.Plugin.OnActiveSceneChanged(prevScene, nextScene));
  25. }
  26. private void Invoke(CompositeCall callback) {
  27. foreach (var plugin in plugins) {
  28. try {
  29. if (plugin.Plugin != null)
  30. callback(plugin);
  31. }
  32. catch (Exception ex) {
  33. Logger.log.Error($"{plugin.Metadata.Name}: {ex}");
  34. }
  35. }
  36. }
  37. public void OnUpdate() {
  38. Invoke(plugin => plugin.Plugin.OnUpdate());
  39. }
  40. public void OnFixedUpdate() {
  41. Invoke(plugin => plugin.Plugin.OnFixedUpdate());
  42. }
  43. public string Name => throw new InvalidOperationException();
  44. public string Version => throw new InvalidOperationException();
  45. public void OnLateUpdate() {
  46. Invoke(plugin => {
  47. if (plugin.Plugin is IGenericEnhancedPlugin saberPlugin)
  48. saberPlugin.OnLateUpdate();
  49. });
  50. }
  51. }
  52. }