ScriptableObjectAdapter.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #pragma once
  2. #include <v8-local-handle.h>
  3. #include <v8-persistent-handle.h>
  4. #include <functional>
  5. #include <memory>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <utility>
  9. #include <vector>
  10. namespace v8 {
  11. class Function;
  12. class Object;
  13. class Private;
  14. }
  15. namespace WallpaperEngine::Data::Model {
  16. struct ImageEffect;
  17. }
  18. namespace WallpaperEngine::Render::Objects {
  19. class CImage;
  20. class CParticle;
  21. }
  22. namespace WallpaperEngine::Scripting {
  23. class ScriptEngine;
  24. class ScriptableObject;
  25. }
  26. namespace WallpaperEngine::Scripting::Adapters {
  27. /** A member of a native object read (and, with a setter, written) from C++ on every access */
  28. struct NativeProperty {
  29. std::string name;
  30. std::function<v8::Local<v8::Value> ()> get;
  31. /** none for a read only member */
  32. std::function<void (v8::Local<v8::Value>)> set = nullptr;
  33. };
  34. /** What scripts reach a layer through. Outlives the layer, `object` is cleared when it goes */
  35. struct Layer {
  36. ScriptableObject* object;
  37. // one JS object per layer like scenescript64 (getLayer sub_181632350 -> sub_181652380), scripts compare
  38. // layers, key maps by them and tag them (3378399626's widget, 3577513994's icon.clicked)
  39. v8::Global<v8::Object> instance;
  40. /** ITextureAnimation of an image layer, one JS object per layer like WE's (sub_14020E670 keeps it at +1216) */
  41. v8::Global<v8::Object> textureAnimation;
  42. /** IParticleSystemInstance of a particle layer, the instance override struct (+1912) */
  43. v8::Global<v8::Object> particleInstance;
  44. };
  45. /** IAnimationLayer (wallpaper64 2.8.42 sub_14026C980) for the puppet animation layer with that serial */
  46. v8::Local<v8::Value> makeAnimationLayerHandle (ScriptEngine& engine, ScriptableObject& object, size_t serial);
  47. /** Animation group of an animation layer's own properties, which WE keys by the layer's own id (sub_14018DB00) */
  48. std::string animationLayerGroup (int objectId, size_t serial);
  49. class ScriptableObjectAdapter {
  50. public:
  51. explicit ScriptableObjectAdapter (ScriptEngine& engine);
  52. v8::Local<v8::Object> instantiate (ScriptableObject& object);
  53. /** The layer a JS value produced by instantiate () stands for, nullptr for anything else or a layer that is gone */
  54. ScriptableObject* getObject (v8::Local<v8::Value> value) const;
  55. Layer& layerOf (ScriptableObject& object);
  56. void forget (const ScriptableObject& object);
  57. v8::Local<v8::Value> textureAnimation (Layer& layer);
  58. v8::Local<v8::Value> particleInstance (Layer& layer);
  59. /** IEffect of one of an image's effects, one JS object per effect */
  60. v8::Local<v8::Value> effect (Data::Model::ImageEffect& effect);
  61. /** IAnimation of an animated property's timeline, one JS object per timeline (sub_14018DB00 keeps it at +248) */
  62. v8::Local<v8::Value> animation (int systemId, int clockId);
  63. /**
  64. * A script object the way scenescript64 builds WE's native ones: its members are own enumerable data properties
  65. * with live values, properties before methods, and writing a read only one throws
  66. */
  67. v8::Local<v8::Object> makeNativeObject (
  68. std::vector<NativeProperty> properties,
  69. const std::vector<std::pair<std::string, v8::Local<v8::Function>>>& methods
  70. );
  71. ScriptEngine& getEngine () const { return m_engine; }
  72. private:
  73. ScriptEngine& m_engine;
  74. std::unordered_map<const ScriptableObject*, std::unique_ptr<Layer>> m_layers;
  75. // layers of objects that are gone, scripts may still hold their JS objects
  76. std::vector<std::unique_ptr<Layer>> m_forgotten;
  77. std::unordered_map<int, v8::Global<v8::Object>> m_animations;
  78. std::unordered_map<const Data::Model::ImageEffect*, v8::Global<v8::Object>> m_effects;
  79. // the native objects' members, their accessors point into these
  80. std::vector<std::unique_ptr<std::vector<NativeProperty>>> m_nativeMembers;
  81. v8::Global<v8::Private> m_layerKey;
  82. // layers and native objects are instances of these, like WE's template objects: their own prototype over
  83. // Object.prototype with an anonymous constructor (V8 then names them '[object Object]' in its errors)
  84. v8::Global<v8::Function> m_layerConstructor;
  85. v8::Global<v8::Function> m_nativeConstructor;
  86. };
  87. }