OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
C4ScriptHost.h
Go to the documentation of this file.
1 /*
2  * OpenClonk, http://www.openclonk.org
3  *
4  * Copyright (c) 1998-2000, Matthes Bender
5  * Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
6  * Copyright (c) 2009-2016, The OpenClonk Team and contributors
7  *
8  * Distributed under the terms of the ISC license; see accompanying file
9  * "COPYING" for details.
10  *
11  * "Clonk" is a registered trademark of Matthes Bender, used with permission.
12  * See accompanying file "TRADEMARK" for details.
13  *
14  * To redistribute this file separately, substitute the full license texts
15  * for the above references.
16  */
17 
18 /* Component host for CAulScript */
19 
20 #ifndef INC_C4ScriptHost
21 #define INC_C4ScriptHost
22 
24 #include "script/C4Aul.h"
25 #include "script/C4AulAST.h"
26 
27 #include <bitset>
28 
29 // aul script state
31 {
32  ASS_NONE, // nothing
33  ASS_PREPARSED, // function list built; CodeSize set
34  ASS_LINKED, // includes and appends resolved
35  ASS_PARSED // byte code generated
36 };
37 
38 // generic script host for objects
40 {
41 public:
42  ~C4ScriptHost() override;
43  virtual bool Delete() { return false; } // do NOT delete this - it's just a class member!
44 
45  void Clear();
46  virtual bool Load(C4Group &hGroup, const char *szFilename,
47  const char *szLanguage, C4LangStringTable *pLocalTable);
48  virtual bool LoadData(const char *szFilename, const char *szData, class C4LangStringTable *pLocalTable);
49  void Reg2List(C4AulScriptEngine *pEngine); // reg to linked list
50  virtual C4PropListStatic * GetPropList() { return nullptr; }
51  const C4PropListStatic *GetPropList() const { return const_cast<C4ScriptHost*>(this)->GetPropList(); }
52  const char *GetScript() const { return Script.getData(); }
53  bool IsReady() { return State == ASS_PARSED; } // whether script calls may be done
54  // Translate a string using the script's lang table
55  std::string Translate(const std::string &text) const;
56  std::list<C4ScriptHost *> SourceScripts;
57  StdCopyStrBuf ScriptName; // script name
58 
59  bool IsWarningEnabled(const char *pos, C4AulWarningId warning) const;
60 
61 protected:
62  C4ScriptHost();
63  void Unreg(); // remove from list
64  void MakeScript();
65  virtual bool ReloadScript(const char *szPath, const char *szLanguage);
66 
67  bool Preparse(); // preparse script; return if successfull
68  virtual bool Parse(); // parse preparsed script; return if successfull
69  virtual void UnLink(); // reset to unlinked state
70 
71  void UnlinkOwnedFunctions();
72  void DeleteOwnedPropLists();
73 
74  void Warn(const char *pMsg, ...) GNUC_FORMAT_ATTRIBUTE_O;
75 
76  C4AulScriptEngine *Engine{nullptr}; //owning engine
77  C4ScriptHost *Prev{nullptr}, *Next{nullptr}; // tree structure
78 
79  std::list<StdCopyStrBuf> Includes; // include list
80  std::list<StdCopyStrBuf> Appends; // append list
81 
82  virtual void AddEngineFunctions() {}; // add any engine functions specific to this script host
84  bool ResolveIncludes(C4DefList *rDefs); // resolve includes
85  bool ResolveAppends(C4DefList *rDefs); // resolve appends
86  void DoAppend(C4Def *def);
87  bool Resolving; // set while include-resolving, to catch circular includes
89 
90  StdStrBuf Script; // script
93  C4AulScriptState State{ASS_NONE}; // script state
94 
95  // list of all functions generated from code in this script host
96  std::vector<C4Value> ownedFunctions;
97 
98  // list of all static proplists that refer to this script host
99  // filled in at link time and used to delete all proplists
100  // in Clear() even in case of cyclic references.
101  std::vector<C4Value> ownedPropLists;
102 
103  void EnableWarning(const char *pos, C4AulWarningId warning, bool enable = true);
104 
105  friend class C4AulParse;
106  friend class C4AulProfiler;
107  friend class C4AulScriptEngine;
108  friend class C4AulDebug;
109  friend class C4AulCompiler;
110  friend class C4AulScriptFunc;
111 
112 private:
113  std::map<const char*, std::bitset<(size_t)C4AulWarningId::WarningCount>> enabledWarnings;
114  std::unique_ptr<::aul::ast::Script> ast;
115 };
116 
117 // script host for System.ocg scripts and scenario section Objects.c
119 {
120  C4Value ParserPropList;
121 public:
122  C4ExtraScriptHost(C4String *parent_key_name = nullptr);
123  ~C4ExtraScriptHost() override;
124  void Clear();
125 
126  bool Delete() override { return true; }
127  C4PropListStatic * GetPropList() override;
128 };
129 
130 // script host for scenario section Objects.c
132 {
133 public:
135 };
136 
137 // script host for defs
139 {
140 public:
142 
143  void SetDef(C4Def *to_def) { Def=to_def; }
144  bool Parse() override;
145  C4PropListStatic * GetPropList() override;
146 protected:
147  C4Def *Def{nullptr}; // owning def file
148 };
149 
150 
151 // script host for scenario scripts
153 {
154 public:
156  ~C4GameScriptHost() override;
157  bool Load(C4Group &, const char *, const char *, C4LangStringTable *) override;
158  bool LoadData(const char *, const char *, C4LangStringTable *) override;
159  void Clear();
160  C4PropListStatic * GetPropList() override;
161  void Denumerate(C4ValueNumbers * numbers);
162  C4Value Call(const char *szFunction, C4AulParSet *pPars=nullptr, bool fPassError=false);
166 };
167 
169 
170 #endif
const char * getData() const
Definition: StdBuf.h:442
bool IncludesResolved
Definition: C4ScriptHost.h:88
void EnableWarning(const char *pos, C4AulWarningId warning, bool enable=true)
virtual bool ReloadScript(const char *szPath, const char *szLanguage)
C4AulWarningId
Definition: C4Aul.h:29
C4PropListStatic * GetPropList() override
std::list< StdCopyStrBuf > Appends
Definition: C4ScriptHost.h:80
C4Value Call(const char *szFunction, C4AulParSet *pPars=nullptr, bool fPassError=false)
bool LoadData(const char *, const char *, C4LangStringTable *) override
bool IsWarningEnabled(const char *pos, C4AulWarningId warning) const
C4AulScriptEngine * Engine
Definition: C4ScriptHost.h:76
virtual C4PropListStatic * GetPropList()
Definition: C4ScriptHost.h:50
C4PropListStatic * GetPropList() override
void Reg2List(C4AulScriptEngine *pEngine)
void CopyPropList(C4Set< C4Property > &from, C4PropListStatic *to)
virtual void AddEngineFunctions()
Definition: C4ScriptHost.h:82
#define GNUC_FORMAT_ATTRIBUTE_O
void UnlinkOwnedFunctions()
C4GameScriptHost GameScript
bool Load(C4Group &, const char *, const char *, C4LangStringTable *) override
const C4PropListStatic * GetPropList() const
Definition: C4ScriptHost.h:51
bool IsReady()
Definition: C4ScriptHost.h:53
void DeleteOwnedPropLists()
C4Value ScenPropList
Definition: C4ScriptHost.h:163
bool Parse() override
void DoAppend(C4Def *def)
Definition: C4AulLink.cpp:28
std::list< StdCopyStrBuf > Includes
Definition: C4ScriptHost.h:79
Definition: C4Def.h:98
~C4ExtraScriptHost() override
virtual bool LoadData(const char *szFilename, const char *szData, class C4LangStringTable *pLocalTable)
virtual bool Parse()
void Denumerate(C4ValueNumbers *numbers)
C4Value ScenPrototype
Definition: C4ScriptHost.h:164
bool ResolveIncludes(C4DefList *rDefs)
Definition: C4AulLink.cpp:83
C4AulScriptState
Definition: C4ScriptHost.h:30
void SetDef(C4Def *to_def)
Definition: C4ScriptHost.h:143
C4Set< C4Property > LocalValues
Definition: C4ScriptHost.h:92
bool Delete() override
Definition: C4ScriptHost.h:126
void Warn(const char *pMsg,...) GNUC_FORMAT_ATTRIBUTE_O
Definition: C4AulParse.cpp:123
~C4GameScriptHost() override
bool Preparse()
Definition: C4AulParse.cpp:679
C4PropListStatic * GetPropList() override
std::string Translate(const std::string &text) const
bool ResolveAppends(C4DefList *rDefs)
Definition: C4AulLink.cpp:46
C4Effect * pScenarioEffects
Definition: C4ScriptHost.h:165
C4ExtraScriptHost(C4String *parent_key_name=nullptr)
virtual void UnLink()
Definition: C4AulLink.cpp:130
virtual bool Delete()
Definition: C4ScriptHost.h:43
const char * GetScript() const
Definition: C4ScriptHost.h:52
std::list< C4ScriptHost * > SourceScripts
Definition: C4ScriptHost.h:56
std::vector< C4Value > ownedFunctions
Definition: C4ScriptHost.h:96
virtual bool Load(C4Group &hGroup, const char *szFilename, const char *szLanguage, C4LangStringTable *pLocalTable)
C4LangStringTable * stringTable
Definition: C4ScriptHost.h:91
StdCopyStrBuf ScriptName
Definition: C4ScriptHost.h:57
C4AulScriptState State
Definition: C4ScriptHost.h:93
~C4ScriptHost() override
C4ScriptHost * Next
Definition: C4ScriptHost.h:77
C4ScriptHost * Prev
Definition: C4ScriptHost.h:77
StdStrBuf Script
Definition: C4ScriptHost.h:90
std::vector< C4Value > ownedPropLists
Definition: C4ScriptHost.h:101