HostExtensionServices.ts 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. import * as EditorEvents from "../editor/EditorEvents";
  23. import * as EditorUI from "../ui/EditorUI";
  24. /**
  25. * Generic registry for storing Editor Extension Services
  26. */
  27. class ServiceRegistry<T extends Editor.Extensions.EditorService> implements Editor.Extensions.ServiceRegistry<T> {
  28. registeredServices: T[] = [];
  29. /**
  30. * Adds a service to the registered services list for this type of service
  31. * @param {T} service the service to register
  32. */
  33. register(service: T) {
  34. this.registeredServices.push(service);
  35. }
  36. unregister(service: T) {
  37. var index = this.registeredServices.indexOf(service, 0);
  38. if (index > -1) {
  39. this.registeredServices.splice(index, 1);
  40. }
  41. }
  42. }
  43. interface ServiceEventSubscriber {
  44. /**
  45. * Allow this service registry to subscribe to events that it is interested in
  46. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  47. */
  48. subscribeToEvents(topLevelWindow: Atomic.UIWidget);
  49. }
  50. /**
  51. * Registry for service extensions that are concerned about project events
  52. */
  53. export class ProjectServiceRegistry extends ServiceRegistry<Editor.HostExtensions.ProjectService> implements ServiceEventSubscriber {
  54. constructor() {
  55. super();
  56. }
  57. /**
  58. * Allow this service registry to subscribe to events that it is interested in
  59. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  60. */
  61. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  62. eventDispatcher.subscribeToEvent(EditorEvents.LoadProjectNotification, (ev) => this.projectLoaded(ev));
  63. eventDispatcher.subscribeToEvent(EditorEvents.CloseProject, (ev) => this.projectUnloaded(ev));
  64. eventDispatcher.subscribeToEvent(EditorEvents.PlayerStartRequest, () => this.playerStarted());
  65. }
  66. /**
  67. * Called when the project is unloaded
  68. * @param {[type]} data Event info from the project unloaded event
  69. */
  70. projectUnloaded(data) {
  71. this.registeredServices.forEach((service) => {
  72. // Notify services that the project has been unloaded
  73. try {
  74. if (service.projectUnloaded) {
  75. service.projectUnloaded();
  76. }
  77. } catch (e) {
  78. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  79. }
  80. });
  81. }
  82. /**
  83. * Called when the project is loaded
  84. * @param {[type]} data Event info from the project unloaded event
  85. */
  86. projectLoaded(ev: Editor.EditorEvents.LoadProjectEvent) {
  87. this.registeredServices.forEach((service) => {
  88. try {
  89. // Notify services that the project has just been loaded
  90. if (service.projectLoaded) {
  91. service.projectLoaded(ev);
  92. }
  93. } catch (e) {
  94. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  95. }
  96. });
  97. }
  98. playerStarted() {
  99. this.registeredServices.forEach((service) => {
  100. try {
  101. // Notify services that the project has just been loaded
  102. if (service.playerStarted) {
  103. service.playerStarted();
  104. }
  105. } catch (e) {
  106. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  107. }
  108. });
  109. }
  110. }
  111. /**
  112. * Registry for service extensions that are concerned about Resources
  113. */
  114. export class ResourceServiceRegistry extends ServiceRegistry<Editor.HostExtensions.ResourceService> {
  115. constructor() {
  116. super();
  117. }
  118. /**
  119. * Allow this service registry to subscribe to events that it is interested in
  120. * @param {Atomic.UIWidget} topLevelWindow The top level window that will be receiving these events
  121. */
  122. subscribeToEvents(eventDispatcher: Editor.Extensions.EventDispatcher) {
  123. eventDispatcher.subscribeToEvent(EditorEvents.SaveResourceNotification, (ev) => this.saveResource(ev));
  124. eventDispatcher.subscribeToEvent(EditorEvents.DeleteResourceNotification, (ev) => this.deleteResource(ev));
  125. eventDispatcher.subscribeToEvent(EditorEvents.RenameResourceNotification, (ev) => this.renameResource(ev));
  126. }
  127. /**
  128. * Called after a resource has been saved
  129. * @param {Editor.EditorEvents.SaveResourceEvent} ev
  130. */
  131. saveResource(ev: Editor.EditorEvents.SaveResourceEvent) {
  132. // run through and find any services that can handle this.
  133. this.registeredServices.forEach((service) => {
  134. try {
  135. // Verify that the service contains the appropriate methods and that it can save
  136. if (service.save) {
  137. service.save(ev);
  138. }
  139. } catch (e) {
  140. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  141. }
  142. });
  143. }
  144. /**
  145. * Called when a resource has been deleted
  146. */
  147. deleteResource(ev: Editor.EditorEvents.DeleteResourceEvent) {
  148. this.registeredServices.forEach((service) => {
  149. try {
  150. // Verify that the service contains the appropriate methods and that it can delete
  151. if (service.delete) {
  152. service.delete(ev);
  153. }
  154. } catch (e) {
  155. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n ${e}\n ${e.stack}`);
  156. }
  157. });
  158. }
  159. /**
  160. * Called when a resource has been renamed
  161. * @param {Editor.EditorEvents.RenameResourceEvent} ev
  162. */
  163. renameResource(ev: Editor.EditorEvents.RenameResourceEvent) {
  164. this.registeredServices.forEach((service) => {
  165. try {
  166. // Verify that the service contains the appropriate methods and that it can handle the rename
  167. if (service.rename) {
  168. service.rename(ev);
  169. }
  170. } catch (e) {
  171. EditorUI.showModalError("Extension Error", `Error detected in extension ${service.name}\n \n ${e.stack}`);
  172. }
  173. });
  174. }
  175. }