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.

98 lines
2.8 KiB

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