ScummVM API documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
script_runtime.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_ENGINE_SCRIPT_SCRIPT_RUNTIME_H
23 #define AGS_ENGINE_SCRIPT_SCRIPT_RUNTIME_H
24 
25 #include "ags/shared/core/types.h"
26 #include "ags/shared/script/cc_script.h" // ccScript
27 #include "ags/engine/script/cc_instance.h" // ccInstance
28 
29 namespace AGS3 {
30 
31 struct IScriptObject;
32 
33 using AGS::Shared::String;
34 
35 // Helper struct for declaring a script API function registration
36 struct ScFnRegister {
37  const char *Name = nullptr;
38  RuntimeScriptValue Fn; // for script VM calls
39  RuntimeScriptValue PlFn; // for plugins, direct unsafe call style
40 
41  ScFnRegister() = default;
42  ScFnRegister(const char *name, ScriptAPIFunction *fn, void *plfn = nullptr)
43  : Name(name), Fn(RuntimeScriptValue().SetStaticFunction(fn)), PlFn(RuntimeScriptValue().SetPluginMethod((Plugins::ScriptContainer *)plfn, nullptr)) {}
44  ScFnRegister(const char *name, ScriptAPIObjectFunction *fn, void *plfn = nullptr)
45  : Name(name), Fn(RuntimeScriptValue().SetObjectFunction(fn)), PlFn(RuntimeScriptValue().SetPluginMethod((Plugins::ScriptContainer *)plfn, nullptr)) {}
46  template<typename TPlFn>
47  ScFnRegister(const char *name, ScriptAPIFunction *fn, TPlFn plfn)
48  : Name(name), Fn(RuntimeScriptValue().SetStaticFunction(fn)), PlFn(RuntimeScriptValue().SetPluginMethod((Plugins::ScriptContainer *)plfn, nullptr)) {}
49  template<typename TPlFn>
50  ScFnRegister(const char *name, ScriptAPIObjectFunction *fn, TPlFn plfn)
51  : Name(name), Fn(RuntimeScriptValue().SetObjectFunction(fn)), PlFn(RuntimeScriptValue().SetPluginMethod((Plugins::ScriptContainer *)plfn, nullptr)) {}
52 };
53 
54 // Following functions register engine API symbols for script and plugins.
55 // Calls from script is handled by specific "translator" functions, which
56 // unpack script interpreter's values into the real arguments and call the
57 // actual engine's function. For plugins we have to provide actual engine
58 // function directly.
59 bool ccAddExternalStaticFunction(const String &name, ScriptAPIFunction *pfn);
60 bool ccAddExternalObjectFunction(const String &name, ScriptAPIObjectFunction *pfn);
61 bool ccAddExternalFunctionForPlugin(const String &name, Plugins::ScriptContainer *sc);
62 // Register a function, exported from a plugin. Requires direct function pointer only.
63 bool ccAddExternalPluginFunction(const String &name, Plugins::ScriptContainer *sc);
64 // Register engine objects for script's access.
65 bool ccAddExternalStaticArray(const String &name, void *ptr, CCStaticArray *array_mgr);
66 bool ccAddExternalScriptObject(const String &name, void *ptr, IScriptObject *manager);
67 // Register script own functions (defined in the linked scripts)
68 bool ccAddExternalScriptSymbol(const String &name, const RuntimeScriptValue &prval, ccInstance *inst);
69 // Remove the script access to a variable or function in your program
70 void ccRemoveExternalSymbol(const String &name);
71 // Remove all external symbols, allowing you to start from scratch
72 void ccRemoveAllSymbols();
73 
74 // FIXME: These functions should replace the older ones; for now they are duplicated to ease
75 // the transition
76 bool ccAddExternalStaticFunction361(const String &name, ScriptAPIFunction *scfn, void *dirfn = nullptr);
77 bool ccAddExternalObjectFunction361(const String &name, ScriptAPIObjectFunction *scfn, void *dirfn = nullptr);
78 bool ccAddExternalFunction361(const ScFnRegister &scfnreg);
79 // Registers an array of static functions
80 template<size_t N>
81 inline void ccAddExternalFunctions361(const ScFnRegister (&arr)[N]) {
82  for (const ScFnRegister *it = arr; it != (arr + N); ++it)
83  ccAddExternalFunction361(*it);
84 }
85 
86 // Get the address of an exported variable in the script
87 void *ccGetSymbolAddress(const String &name);
88 // Get a registered symbol's direct pointer; this is used solely for plugins
89 Plugins::PluginMethod ccGetSymbolAddressForPlugin(const String &name);
90 // Get a registered Script Object, optionally restricting to the given type name
91 void *ccGetScriptObjectAddress(const String &name, const String &type);
92 
93 // DEBUG HOOK
94 typedef void (*new_line_hook_type)(ccInstance *, int);
95 void ccSetDebugHook(new_line_hook_type jibble);
96 
97 // Set the script interpreter timeout values:
98 // * sys_poll_timeout - defines the timeout (ms) at which the interpreter will run system events poll;
99 // * abort_timeout - [temp disabled] defines the timeout (ms) at which the interpreter will cancel with error.
100 // * abort_loops - max script loops without an engine update after which the interpreter will error;
101 void ccSetScriptAliveTimer(unsigned sys_poll_timeout, unsigned abort_timeout, unsigned abort_loops);
102 // reset the current while loop counter
103 void ccNotifyScriptStillAlive();
104 // for calling exported plugin functions old-style
105 NumberPtr call_function(const Plugins::PluginMethod &method, const RuntimeScriptValue *object, int numparm, const RuntimeScriptValue *parms);
106 
107 } // namespace AGS3
108 
109 #endif
Definition: cc_script_object.h:60
Definition: runtime_script_value.h:63
Definition: cc_static_array.h:60
Definition: types.h:112
Definition: script_runtime.h:36
Definition: string.h:62
Definition: plugin_base.h:151
Definition: plugin_base.h:189
Definition: cc_instance.h:114
Definition: ags.h:40