ScummVM API documentation
plugin_base.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_PLUGINS_PLUGIN_BASE_H
23 #define AGS_PLUGINS_PLUGIN_BASE_H
24 
25 #include "ags/shared/util/string.h"
26 #include "common/hashmap.h"
27 #include "common/hash-str.h"
28 #include "common/textconsole.h"
29 
30 namespace AGS3 {
31 
32 class IAGSEditor;
33 class IAGSEngine;
34 
35 namespace Plugins {
36 
37 #define SCRIPT_METHOD(NAME, PROC) addMethod(#NAME, &PROC)
38 
39 #define SCRIPT_HASH_MACRO(TheClass, BaseClass, RegisterMethod) \
40  private: \
41  typedef void (TheClass::*MethodPtr)(ScriptMethodParams &params); \
42  Common::HashMap<Common::String, MethodPtr> _methods; \
43  inline void addMethod(const Common::String &name, MethodPtr fn) { \
44  _methods[name] = fn; \
45  _engine->RegisterMethod(name.c_str(), this); \
46  } \
47  public: \
48  void execMethod(const Common::String &name, ScriptMethodParams &params) override { \
49  if (_methods.contains(name)) \
50  (this->*_methods[name])(params); \
51  else \
52  BaseClass::execMethod(name, params); \
53  }
54 #define SCRIPT_HASH(TheClass) SCRIPT_HASH_MACRO(TheClass, PluginBase, RegisterScriptFunction)
55 #define BUILT_IN_HASH(TheClass) SCRIPT_HASH_MACRO(TheClass, ScriptContainer, RegisterBuiltInFunction)
56 #define SCRIPT_HASH_SUB(TheClass, BaseClass) SCRIPT_HASH_MACRO(TheClass, BaseClass, RegisterScriptFunction)
57 
58 inline float PARAM_TO_FLOAT(int32 xi) {
59  float x;
60  memcpy(&x, &xi, sizeof(float));
61  return x;
62 }
63 
64 inline int32 PARAM_FROM_FLOAT(float x) {
65  int32 xi;
66  memcpy(&xi, &x, sizeof(float));
67  return xi;
68 }
69 
70 #define PARAMS1(T1, N1) \
71  T1 N1 = (T1)params[0]
72 #define PARAMS2(T1, N1, T2, N2) \
73  T1 N1 = (T1)params[0]; \
74  T2 N2 = (T2)params[1]
75 #define PARAMS3(T1, N1, T2, N2, T3, N3) \
76  T1 N1 = (T1)params[0]; \
77  T2 N2 = (T2)params[1]; \
78  T3 N3 = (T3)params[2]
79 #define PARAMS4(T1, N1, T2, N2, T3, N3, T4, N4) \
80  T1 N1 = (T1)params[0]; \
81  T2 N2 = (T2)params[1]; \
82  T3 N3 = (T3)params[2]; \
83  T4 N4 = (T4)params[3]
84 #define PARAMS5(T1, N1, T2, N2, T3, N3, T4, N4, T5, N5) \
85  T1 N1 = (T1)params[0]; \
86  T2 N2 = (T2)params[1]; \
87  T3 N3 = (T3)params[2]; \
88  T4 N4 = (T4)params[3]; \
89  T5 N5 = (T5)params[4]
90 #define PARAMS6(T1, N1, T2, N2, T3, N3, T4, N4, T5, N5, T6, N6) \
91  T1 N1 = (T1)params[0]; \
92  T2 N2 = (T2)params[1]; \
93  T3 N3 = (T3)params[2]; \
94  T4 N4 = (T4)params[3]; \
95  T5 N5 = (T5)params[4]; \
96  T6 N6 = (T6)params[5]
97 #define PARAMS7(T1, N1, T2, N2, T3, N3, T4, N4, T5, N5, T6, N6, T7, N7) \
98  T1 N1 = (T1)params[0]; \
99  T2 N2 = (T2)params[1]; \
100  T3 N3 = (T3)params[2]; \
101  T4 N4 = (T4)params[3]; \
102  T5 N5 = (T5)params[4]; \
103  T6 N6 = (T6)params[5]; \
104  T7 N7 = (T7)params[6]
105 #define PARAMS8(T1, N1, T2, N2, T3, N3, T4, N4, T5, N5, T6, N6, T7, N7, T8, N8) \
106  T1 N1 = (T1)params[0]; \
107  T2 N2 = (T2)params[1]; \
108  T3 N3 = (T3)params[2]; \
109  T4 N4 = (T4)params[3]; \
110  T5 N5 = (T5)params[4]; \
111  T6 N6 = (T6)params[5]; \
112  T7 N7 = (T7)params[6]; \
113  T8 N8 = (T8)params[7]
114 #define PARAMS9(T1, N1, T2, N2, T3, N3, T4, N4, T5, N5, T6, N6, T7, N7, T8, N8, T9, N9) \
115  T1 N1 = (T1)params[0]; \
116  T2 N2 = (T2)params[1]; \
117  T3 N3 = (T3)params[2]; \
118  T4 N4 = (T4)params[3]; \
119  T5 N5 = (T5)params[4]; \
120  T6 N6 = (T6)params[5]; \
121  T7 N7 = (T7)params[6]; \
122  T8 N8 = (T8)params[7]; \
123  T9 N9 = (T9)params[8]
124 
125 class ScriptMethodParams;
126 class ScriptContainer;
127 
128 using string = const char *;
129 typedef uint32 HWND;
130 
131 class ScriptMethodParams : public Common::Array<intptr_t> {
132 public:
133  NumberPtr _result;
134 
136  ScriptMethodParams(int val1);
137  ScriptMethodParams(int val1, int val2);
138  ScriptMethodParams(int val1, int val2, int val3);
139  ScriptMethodParams(int val1, int val2, int val3, int val4);
140 
145  Common::String format(int formatIndex);
146 };
147 
152 public:
153  ScriptContainer() {}
154  virtual ~ScriptContainer() {}
155 
156 protected:
157  IAGSEngine *_engine = nullptr;
158 public:
159  virtual void AGS_EngineStartup(IAGSEngine *engine) {
160  _engine = engine;
161  }
162 
163  virtual void execMethod(const Common::String &name, ScriptMethodParams &params) {
164  error("Plugin does not contain method - %s", name.c_str());
165  }
166 };
167 
172 public:
173  PluginBase() {}
174  virtual ~PluginBase() {}
175 
176  virtual const char *AGS_GetPluginName() = 0;
177  virtual int AGS_PluginV2() const { return 1; }
178  virtual int AGS_EditorStartup(IAGSEditor *) { return 0; }
179  virtual void AGS_EditorShutdown() {}
180  virtual void AGS_EditorProperties(HWND) {}
181  virtual int AGS_EditorSaveGame(char *, int) { return 0; }
182  virtual void AGS_EditorLoadGame(char *, int) {}
183  virtual void AGS_EngineShutdown() {}
184  virtual int64 AGS_EngineOnEvent(int, NumberPtr) { return 0; }
185  virtual int AGS_EngineDebugHook(const char *, int, int) { return 0; }
186  virtual void AGS_EngineInitGfx(const char *driverID, void *data) {}
187 };
188 
190 private:
191  ScriptContainer *_sc;
192  Common::String _name;
193 public:
194  PluginMethod() : _sc(nullptr) {}
195  PluginMethod(ScriptContainer *sc, const Common::String &name) :
196  _sc(sc), _name(name) {
197  }
198 
199  operator bool() const {
200  return _sc != nullptr;
201  }
202 
203  NumberPtr operator()(ScriptMethodParams &params) const {
204  _sc->execMethod(_name, params);
205  return params._result;
206  }
207 
208  NumberPtr operator()(intptr_t val1) const {
209  ScriptMethodParams params(val1);
210  _sc->execMethod(_name, params);
211  return params._result;
212  }
213  NumberPtr operator()(intptr_t val1, intptr_t val2) const {
214  ScriptMethodParams params(val1, val2);
215  _sc->execMethod(_name, params);
216  return params._result;
217  }
218  NumberPtr operator()(intptr_t val1, intptr_t val2, intptr_t val3) const {
219  ScriptMethodParams params(val1, val2, val3);
220  _sc->execMethod(_name, params);
221  return params._result;
222  }
223  NumberPtr operator()(intptr_t val1, intptr_t val2, intptr_t val3, intptr_t val4) const {
224  ScriptMethodParams params(val1, val2, val3, val4);
225  _sc->execMethod(_name, params);
226  return params._result;
227  }
228 };
229 
230 extern PluginBase *pluginOpen(const char *filename);
231 
232 extern int pluginClose(Plugins::PluginBase *lib);
233 
234 extern const char *pluginError();
235 
236 } // namespace Plugins
237 } // namespace AGS3
238 
239 #endif
Definition: str.h:59
Definition: ags_plugin.h:207
const intptr_t * data() const
Definition: array.h:207
Definition: array.h:52
Common::String format(int formatIndex)
Definition: plugin_base.h:171
Definition: plugin_base.h:131
Definition: types.h:112
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: ags_plugin.h:296
Definition: plugin_base.h:151
Definition: plugin_base.h:189
Definition: ags.h:40