Phasor  01.00.10.059
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Scripting.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Manager.h"
4 #include "Common/Streams.h"
5 #include "Common/noncopyable.h"
6 #include <array>
7 #include <set>
8 #include <string>
9 
10 typedef unsigned long DWORD;
11 
12 namespace scripting
13 {
14  class Scripts;
15 }
16 
17 extern std::unique_ptr<scripting::Scripts> g_Scripts;
18 
19 namespace scripting
20 {
22 
24  {
25  private:
26  std::set<std::string> blockedFunctions;
27  std::string name, path;
28  bool persistent;
29 
30  public:
31  std::unique_ptr<Manager::ScriptState> state;
32 
33  PhasorScript(std::unique_ptr<Manager::ScriptState> state, bool persistent)
34  : state(std::move(state)), persistent(persistent) {}
35  virtual ~PhasorScript() {}
36 
37  void BlockFunction(const std::string& func);
38  void SetInfo(const std::string& path, const std::string& name);
39 
40  const std::string& GetName();
41  const std::string& GetPath();
42  bool isPersistent() { return persistent; }
43 
44  // Checks if the specified script function is allowed to be called.
45  bool FunctionAllowed(const std::string& func);
46  };
47  class CheckedScriptReference;
48 
49  struct ScriptInfo
50  {
51  std::string script;
52  bool persistent;
53  ScriptInfo(const std::string & script, bool persistent)
54  : script(script), persistent(persistent)
55  {}
56  };
57 
58  // --------------------------------------------------------------------
59  // Only reason this is a class is to ensure the errstream is set.
60  class Scripts
61  {
62  private:
63  typedef std::map<std::string, std::unique_ptr<PhasorScript>> scripts_t;
64  std::string scriptsDir;
65  scripts_t scripts;
66  COutStream& errstream;
67 
68  // Checks if the script is compatible with this version of Phasor.
69  bool CheckScriptCompatibility(Manager::ScriptState& state, const char* script);
70 
71  // Called when an error is raised by a script.
72  void HandleError(PhasorScript& state, const std::string& desc);
73 
74  scripts_t::iterator CloseScript(scripts_t::iterator itr);
75 
76  public:
77  Scripts(COutStream& errstream, const std::wstring& scriptsDir);
78  ~Scripts();
79 
80  void LoadPersistentScripts();
81 
82  // Opens the script specified, relative to the scripts directory
83  bool OpenScript(const char* script, bool persistent);
84 
85  // Closes the specified script, if it exists.
86  void CloseScript(const char* script);
87  void CloseAllScripts(bool include_persistent);
88 
89  void ReloadScripts(bool include_persistent);
90  bool ReloadScript(const std::string& script);
91 
92  // Returns a list of the loaded scripts
93  std::list<ScriptInfo> getLoadedScripts() const;
94 
95  friend class PhasorCaller;
96  friend class CheckedScriptReference;
97  };
98 
104  {
105  private:
106  bool valid;
107 
108  static std::list<CheckedScriptReference*> make_list();
109  static void ScriptBeingClosed(PhasorScript* state);
110  static std::list<CheckedScriptReference*> refed_list;
111 
112  protected:
114  public:
116  virtual ~CheckedScriptReference();
117  bool still_valid() const;
118 
119  friend class Scripts;
120  };
121 
122  // --------------------------------------------------------------------
123  // Class: PhasorCaller
124  // Interface for invoking function calls on all loaded scripts.
125  //
126  typedef std::array<Common::obj_type, 5> results_t;
128  {
129  bool ignore_ret, result_set;
130  Scripts& scripts;
131 
132  bool Call(PhasorScript& phasor_state,
133  const std::string& function, const results_t& expected_types,
134  Result& out_result);
135 
136  public:
138  : Manager::Caller(), scripts(scripts), ignore_ret(false),
139  result_set(false) {}
140 
141  void ReturnValueIgnored() { ignore_ret = true; }
142 
143  // Calls the specified function on all loaded scripts.
144  Result Call(const std::string& function,
145  results_t expected_types = results_t());
146  // Calls the specified function on the specific script.
147  Result Call(PhasorScript& script, const std::string& function,
148  results_t expected_types = results_t());
149  };
150 
151 }