instantiationTools.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { Logger } from "./logger.js";
  2. import { GetClass } from "./typeStore.js";
  3. /**
  4. * Class used to enable instantiation of objects by class name
  5. */
  6. export class InstantiationTools {
  7. /**
  8. * Tries to instantiate a new object from a given class name
  9. * @param className defines the class name to instantiate
  10. * @returns the new object or null if the system was not able to do the instantiation
  11. */
  12. static Instantiate(className) {
  13. if (this.RegisteredExternalClasses && this.RegisteredExternalClasses[className]) {
  14. return this.RegisteredExternalClasses[className];
  15. }
  16. const internalClass = GetClass(className);
  17. if (internalClass) {
  18. return internalClass;
  19. }
  20. Logger.Warn(className + " not found, you may have missed an import.");
  21. const arr = className.split(".");
  22. let fn = window || this;
  23. for (let i = 0, len = arr.length; i < len; i++) {
  24. fn = fn[arr[i]];
  25. }
  26. if (typeof fn !== "function") {
  27. return null;
  28. }
  29. return fn;
  30. }
  31. }
  32. /**
  33. * Use this object to register external classes like custom textures or material
  34. * to allow the loaders to instantiate them
  35. */
  36. InstantiationTools.RegisteredExternalClasses = {};
  37. //# sourceMappingURL=instantiationTools.js.map