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.

99 lines
2.7 KiB

  1. using IllusionPlugin;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEngine;
  7. using UnityEngine.SceneManagement;
  8. using Logger = IllusionPlugin.Logger;
  9. namespace IllusionInjector {
  10. public class CompositePlugin : IPlugin {
  11. IEnumerable<IPlugin> plugins;
  12. private delegate void CompositeCall(IPlugin plugin);
  13. private Logger debugLogger => PluginManager.debugLogger;
  14. public CompositePlugin(IEnumerable<IPlugin> plugins) {
  15. this.plugins = plugins;
  16. }
  17. public void OnApplicationStart() {
  18. Invoke(plugin => plugin.OnApplicationStart());
  19. }
  20. public void OnApplicationQuit() {
  21. Invoke(plugin => plugin.OnApplicationQuit());
  22. }
  23. public void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode) {
  24. foreach (var plugin in plugins) {
  25. try {
  26. plugin.OnSceneLoaded(scene, sceneMode);
  27. }
  28. catch (Exception ex) {
  29. debugLogger.Exception($"{plugin.Name}: {ex}");
  30. }
  31. }
  32. }
  33. public void OnSceneUnloaded(Scene scene) {
  34. foreach (var plugin in plugins) {
  35. try {
  36. plugin.OnSceneUnloaded(scene);
  37. }
  38. catch (Exception ex) {
  39. debugLogger.Exception($"{plugin.Name}: {ex}");
  40. }
  41. }
  42. }
  43. public void OnActiveSceneChanged(Scene prevScene, Scene nextScene) {
  44. foreach (var plugin in plugins) {
  45. try {
  46. plugin.OnActiveSceneChanged(prevScene, nextScene);
  47. }
  48. catch (Exception ex) {
  49. debugLogger.Exception($"{plugin.Name}: {ex}");
  50. }
  51. }
  52. }
  53. private void Invoke(CompositeCall callback) {
  54. foreach (var plugin in plugins) {
  55. try {
  56. callback(plugin);
  57. }
  58. catch (Exception ex) {
  59. debugLogger.Exception($"{plugin.Name}: {ex}");
  60. }
  61. }
  62. }
  63. public void OnUpdate() {
  64. Invoke(plugin => plugin.OnUpdate());
  65. }
  66. public void OnFixedUpdate() {
  67. Invoke(plugin => plugin.OnFixedUpdate());
  68. }
  69. public string Name {
  70. get { throw new NotImplementedException(); }
  71. }
  72. public string Version {
  73. get { throw new NotImplementedException(); }
  74. }
  75. public void OnLateUpdate() {
  76. Invoke(plugin => {
  77. if (plugin is IEnhancedPlugin)
  78. ((IEnhancedPlugin) plugin).OnLateUpdate();
  79. });
  80. }
  81. }
  82. }