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.

78 lines
2.1 KiB

  1. using IPA.Old;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. using Logger = IPA.Logging.Logger;
  6. namespace IPA.Loader.Composite
  7. {
  8. #pragma warning disable CS0618 // Type or member is obsolete
  9. internal class CompositeIPAPlugin : Old.IPlugin
  10. {
  11. private readonly IEnumerable<Old.IPlugin> plugins;
  12. private delegate void CompositeCall(Old.IPlugin plugin);
  13. public CompositeIPAPlugin(IEnumerable<Old.IPlugin> plugins)
  14. {
  15. this.plugins = plugins;
  16. }
  17. public void OnApplicationStart()
  18. {
  19. Invoke(plugin => plugin.OnApplicationStart());
  20. }
  21. public void OnApplicationQuit()
  22. {
  23. Invoke(plugin => plugin.OnApplicationQuit());
  24. }
  25. private void Invoke(CompositeCall callback, [CallerMemberName] string member = "")
  26. {
  27. foreach (var plugin in plugins)
  28. {
  29. try
  30. {
  31. callback(plugin);
  32. }
  33. catch (Exception ex)
  34. {
  35. Logger.log.Error($"{plugin.Name} {member}: {ex}");
  36. }
  37. }
  38. }
  39. public void OnUpdate()
  40. {
  41. Invoke(plugin => plugin.OnUpdate());
  42. }
  43. public void OnFixedUpdate()
  44. {
  45. Invoke(plugin => plugin.OnFixedUpdate());
  46. }
  47. public string Name => throw new InvalidOperationException();
  48. public string Version => throw new InvalidOperationException();
  49. public void OnLateUpdate()
  50. {
  51. Invoke(plugin => {
  52. if (plugin is Old.IEnhancedPlugin saberPlugin)
  53. saberPlugin.OnLateUpdate();
  54. });
  55. }
  56. public void OnLevelWasLoaded(int level)
  57. {
  58. Invoke(plugin => plugin.OnLevelWasLoaded(level));
  59. }
  60. public void OnLevelWasInitialized(int level)
  61. {
  62. Invoke(plugin => plugin.OnLevelWasInitialized(level));
  63. }
  64. }
  65. #pragma warning restore CS0618 // Type or member is obsolete
  66. }