SuperTuxKart
script_engine.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2014-2015 SuperTuxKart Team
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 3
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 
19 #ifndef HEADER_SCRIPT_ENGINE_HPP
20 #define HEADER_SCRIPT_ENGINE_HPP
21 
22 #include "scriptengine/script_utils.hpp"
23 #include "utils/no_copy.hpp"
24 #include "utils/ptr_vector.hpp"
25 #include "utils/singleton.hpp"
26 
27 #include <angelscript.h>
28 #include <functional>
29 #include <map>
30 #include <string>
31 
33 
34 namespace Scripting
35 {
38  {
39  double m_time;
40 
44  std::string m_callback_name;
45  asIScriptFunction* m_callback_delegate;
46 
47  PendingTimeout(double time, const std::string& callback_name)
48  {
49  m_callback_delegate = NULL;
50  m_time = time;
51  m_callback_name = callback_name;
52  }
53 
54  PendingTimeout(double time, asIScriptFunction* callback_delegate);
55 
56  ~PendingTimeout();
57  };
58 
59  class ScriptEngine : public AbstractSingleton<ScriptEngine>
60  {
61  ScriptEngine();
62  ~ScriptEngine();
63 
64  // Give the singleton access to the constructor.
65  friend class AbstractSingleton<ScriptEngine>;
66 
67  public:
68 
69 
70  void runFunction(bool warn_if_not_found, std::string function_name);
71  void runFunction(bool warn_if_not_found, std::string function_name,
72  std::function<void(asIScriptContext*)> callback);
73  void runFunction(bool warn_if_not_found, std::string function_name,
74  std::function<void(asIScriptContext*)> callback,
75  std::function<void(asIScriptContext*)> get_return_value);
76  void runDelegate(asIScriptFunction* delegate_fn);
77  void evalScript(std::string script_fragment);
78  void cleanupCache();
79 
80  bool loadScript(std::string script_path, bool clear_previous);
81  bool compileLoadedScripts();
82 
83  void addPendingTimeout(double time, const std::string& callback_name);
84  void addPendingTimeout(double time, asIScriptFunction* delegate_fn);
85  void update(float dt);
86 
87  asIScriptEngine* getEngine() { return m_engine; }
88 
89  private:
90  asIScriptEngine *m_engine;
91  std::map<std::string, asIScriptFunction*> m_functions_cache;
92  PtrVector<PendingTimeout> m_pending_timeouts;
93 
94  void configureEngine(asIScriptEngine *engine);
95  }; // class ScriptEngine
96 
97 }
98 #endif
99 
Manages the abstract singleton at runtime. This has been designed to allow multi-inheritance....
Definition: singleton.hpp:35
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Definition: ptr_vector.hpp:44
Definition: script_engine.hpp:60
void runFunction(bool warn_if_not_found, std::string function_name)
runs the specified script
Definition: script_engine.cpp:327
void configureEngine(asIScriptEngine *engine)
Configures the script engine by binding functions, enums.
Definition: script_engine.cpp:495
Base class for all track object presentation classes.
Definition: track_object_presentation.hpp:60
Represents a scripting function to execute after a given time.
Definition: script_engine.hpp:38
std::string m_callback_name
We have two callback types: a string containing the name of the function to call (simple callback) or...
Definition: script_engine.hpp:44