2009-05-08 13:28:41 +00:00
|
|
|
/*
|
|
|
|
* OpenClonk, http://www.openclonk.org
|
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* Copyright (c) 1998-2000, Matthes Bender
|
|
|
|
* Copyright (c) 2001-2009, RedWolf Design GmbH, http://www.clonk.de/
|
2016-04-03 18:18:29 +00:00
|
|
|
* Copyright (c) 2009-2016, The OpenClonk Team and contributors
|
2009-05-08 13:28:41 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* Distributed under the terms of the ISC license; see accompanying file
|
|
|
|
* "COPYING" for details.
|
2009-05-08 13:28:41 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* "Clonk" is a registered trademark of Matthes Bender, used with permission.
|
|
|
|
* See accompanying file "TRADEMARK" for details.
|
2009-05-08 13:28:41 +00:00
|
|
|
*
|
2013-12-17 20:01:09 +00:00
|
|
|
* To redistribute this file separately, substitute the full license texts
|
|
|
|
* for the above references.
|
2009-05-08 13:28:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Component host for CAulScript */
|
|
|
|
|
|
|
|
#ifndef INC_C4ScriptHost
|
|
|
|
#define INC_C4ScriptHost
|
|
|
|
|
2016-04-03 18:07:56 +00:00
|
|
|
#include "c4group/C4ComponentHost.h"
|
|
|
|
#include "script/C4Aul.h"
|
2016-05-12 17:43:48 +00:00
|
|
|
#include "script/C4AulAST.h"
|
2017-04-30 08:49:09 +00:00
|
|
|
|
2017-02-13 13:53:17 +00:00
|
|
|
#include <bitset>
|
2018-07-24 12:35:12 +00:00
|
|
|
#include <deque>
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2016-01-03 02:38:05 +00:00
|
|
|
// aul script state
|
|
|
|
enum C4AulScriptState
|
|
|
|
{
|
|
|
|
ASS_NONE, // nothing
|
|
|
|
ASS_PREPARSED, // function list built; CodeSize set
|
|
|
|
ASS_LINKED, // includes and appends resolved
|
|
|
|
ASS_PARSED // byte code generated
|
|
|
|
};
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
// generic script host for objects
|
2016-02-03 03:11:36 +00:00
|
|
|
class C4ScriptHost: public C4ComponentHost
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-05-07 11:50:00 +00:00
|
|
|
~C4ScriptHost() override;
|
2016-01-03 19:37:05 +00:00
|
|
|
virtual bool Delete() { return false; } // do NOT delete this - it's just a class member!
|
2012-05-15 17:32:58 +00:00
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
void Clear();
|
2011-10-04 20:16:27 +00:00
|
|
|
virtual bool Load(C4Group &hGroup, const char *szFilename,
|
2011-09-29 01:49:21 +00:00
|
|
|
const char *szLanguage, C4LangStringTable *pLocalTable);
|
2014-08-03 17:29:19 +00:00
|
|
|
virtual bool LoadData(const char *szFilename, const char *szData, class C4LangStringTable *pLocalTable);
|
2016-01-03 19:37:05 +00:00
|
|
|
void Reg2List(C4AulScriptEngine *pEngine); // reg to linked list
|
2017-05-07 11:50:00 +00:00
|
|
|
virtual C4PropListStatic * GetPropList() { return nullptr; }
|
2016-04-23 15:15:03 +00:00
|
|
|
const C4PropListStatic *GetPropList() const { return const_cast<C4ScriptHost*>(this)->GetPropList(); }
|
2011-10-13 16:01:02 +00:00
|
|
|
const char *GetScript() const { return Script.getData(); }
|
2016-01-03 02:38:05 +00:00
|
|
|
bool IsReady() { return State == ASS_PARSED; } // whether script calls may be done
|
2016-01-03 02:42:17 +00:00
|
|
|
// Translate a string using the script's lang table
|
|
|
|
std::string Translate(const std::string &text) const;
|
2018-07-24 12:35:12 +00:00
|
|
|
std::deque<C4ScriptHost *> SourceScripts;
|
2016-01-03 19:37:05 +00:00
|
|
|
StdCopyStrBuf ScriptName; // script name
|
2016-07-06 12:20:54 +00:00
|
|
|
|
2017-02-13 13:53:17 +00:00
|
|
|
bool IsWarningEnabled(const char *pos, C4AulWarningId warning) const;
|
|
|
|
|
2010-03-28 17:58:21 +00:00
|
|
|
protected:
|
2011-10-24 15:02:27 +00:00
|
|
|
C4ScriptHost();
|
2016-01-03 19:37:05 +00:00
|
|
|
void Unreg(); // remove from list
|
2010-03-28 17:58:21 +00:00
|
|
|
void MakeScript();
|
2016-01-03 19:37:05 +00:00
|
|
|
virtual bool ReloadScript(const char *szPath, const char *szLanguage);
|
2011-10-13 16:01:02 +00:00
|
|
|
|
|
|
|
bool Preparse(); // preparse script; return if successfull
|
2012-05-27 21:47:07 +00:00
|
|
|
virtual bool Parse(); // parse preparsed script; return if successfull
|
|
|
|
virtual void UnLink(); // reset to unlinked state
|
2011-10-13 16:01:02 +00:00
|
|
|
|
2016-10-01 03:25:28 +00:00
|
|
|
void UnlinkOwnedFunctions();
|
|
|
|
void DeleteOwnedPropLists();
|
|
|
|
|
2016-01-03 02:50:04 +00:00
|
|
|
void Warn(const char *pMsg, ...) GNUC_FORMAT_ATTRIBUTE_O;
|
2012-05-27 21:47:07 +00:00
|
|
|
|
2017-05-07 11:50:00 +00:00
|
|
|
C4AulScriptEngine *Engine{nullptr}; //owning engine
|
|
|
|
C4ScriptHost *Prev{nullptr}, *Next{nullptr}; // tree structure
|
2016-01-03 19:37:05 +00:00
|
|
|
|
2013-03-03 18:25:18 +00:00
|
|
|
std::list<StdCopyStrBuf> Includes; // include list
|
|
|
|
std::list<StdCopyStrBuf> Appends; // append list
|
2012-05-27 21:47:07 +00:00
|
|
|
|
2013-03-18 23:35:00 +00:00
|
|
|
virtual void AddEngineFunctions() {}; // add any engine functions specific to this script host
|
2012-08-08 23:21:54 +00:00
|
|
|
void CopyPropList(C4Set<C4Property> & from, C4PropListStatic * to);
|
2016-07-07 19:11:57 +00:00
|
|
|
bool ResolveIncludes(C4DefList *rDefs); // resolve includes
|
|
|
|
bool ResolveAppends(C4DefList *rDefs); // resolve appends
|
|
|
|
void DoAppend(C4Def *def);
|
2012-05-27 21:47:07 +00:00
|
|
|
bool Resolving; // set while include-resolving, to catch circular includes
|
|
|
|
bool IncludesResolved;
|
|
|
|
|
2011-10-13 16:01:02 +00:00
|
|
|
StdStrBuf Script; // script
|
2016-01-03 02:42:17 +00:00
|
|
|
C4LangStringTable *stringTable;
|
2012-05-27 21:47:07 +00:00
|
|
|
C4Set<C4Property> LocalValues;
|
2017-05-07 11:50:00 +00:00
|
|
|
C4AulScriptState State{ASS_NONE}; // script state
|
2016-07-06 12:20:54 +00:00
|
|
|
|
|
|
|
// list of all functions generated from code in this script host
|
2016-10-18 12:13:10 +00:00
|
|
|
std::vector<C4Value> ownedFunctions;
|
2016-07-06 12:20:54 +00:00
|
|
|
|
2016-10-01 03:25:28 +00:00
|
|
|
// list of all static proplists that refer to this script host
|
|
|
|
// filled in at link time and used to delete all proplists
|
|
|
|
// in Clear() even in case of cyclic references.
|
|
|
|
std::vector<C4Value> ownedPropLists;
|
|
|
|
|
2017-02-13 13:53:17 +00:00
|
|
|
void EnableWarning(const char *pos, C4AulWarningId warning, bool enable = true);
|
|
|
|
|
2012-04-12 19:03:14 +00:00
|
|
|
friend class C4AulParse;
|
2016-01-03 19:01:37 +00:00
|
|
|
friend class C4AulProfiler;
|
2016-01-03 19:37:05 +00:00
|
|
|
friend class C4AulScriptEngine;
|
2011-10-13 16:01:02 +00:00
|
|
|
friend class C4AulDebug;
|
2016-05-12 17:43:48 +00:00
|
|
|
friend class C4AulCompiler;
|
2016-10-18 12:13:10 +00:00
|
|
|
friend class C4AulScriptFunc;
|
2016-05-12 17:43:48 +00:00
|
|
|
|
|
|
|
private:
|
2017-02-13 13:53:17 +00:00
|
|
|
std::map<const char*, std::bitset<(size_t)C4AulWarningId::WarningCount>> enabledWarnings;
|
2016-05-12 17:43:48 +00:00
|
|
|
std::unique_ptr<::aul::ast::Script> ast;
|
2010-03-28 17:58:21 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2013-12-22 22:47:40 +00:00
|
|
|
// script host for System.ocg scripts and scenario section Objects.c
|
2011-10-24 15:02:27 +00:00
|
|
|
class C4ExtraScriptHost: public C4ScriptHost
|
|
|
|
{
|
|
|
|
C4Value ParserPropList;
|
|
|
|
public:
|
2016-11-02 23:58:02 +00:00
|
|
|
C4ExtraScriptHost(C4String *parent_key_name = nullptr);
|
2017-05-07 11:50:00 +00:00
|
|
|
~C4ExtraScriptHost() override;
|
2011-10-24 15:02:27 +00:00
|
|
|
void Clear();
|
|
|
|
|
2017-05-07 11:50:00 +00:00
|
|
|
bool Delete() override { return true; }
|
|
|
|
C4PropListStatic * GetPropList() override;
|
2011-10-24 15:02:27 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2013-12-22 22:47:40 +00:00
|
|
|
// script host for scenario section Objects.c
|
|
|
|
class C4ScenarioObjectsScriptHost : public C4ExtraScriptHost
|
|
|
|
{
|
|
|
|
public:
|
2014-01-03 17:59:31 +00:00
|
|
|
C4ScenarioObjectsScriptHost();
|
2013-12-22 22:47:40 +00:00
|
|
|
};
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
// script host for defs
|
2011-10-24 15:02:27 +00:00
|
|
|
class C4DefScriptHost: public C4ScriptHost
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-05-07 11:50:00 +00:00
|
|
|
C4DefScriptHost() : C4ScriptHost() { }
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2013-04-01 10:48:02 +00:00
|
|
|
void SetDef(C4Def *to_def) { Def=to_def; }
|
2017-05-07 11:50:00 +00:00
|
|
|
bool Parse() override;
|
|
|
|
C4PropListStatic * GetPropList() override;
|
2010-03-28 17:58:21 +00:00
|
|
|
protected:
|
2017-05-07 11:50:00 +00:00
|
|
|
C4Def *Def{nullptr}; // owning def file
|
2010-03-28 17:58:21 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
// script host for scenario scripts
|
|
|
|
class C4GameScriptHost : public C4ScriptHost
|
2010-03-28 17:58:21 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
C4GameScriptHost();
|
2017-05-07 11:50:00 +00:00
|
|
|
~C4GameScriptHost() override;
|
|
|
|
bool Load(C4Group &, const char *, const char *, C4LangStringTable *) override;
|
|
|
|
bool LoadData(const char *, const char *, C4LangStringTable *) override;
|
2011-09-25 21:24:29 +00:00
|
|
|
void Clear();
|
2017-05-07 11:50:00 +00:00
|
|
|
C4PropListStatic * GetPropList() override;
|
2015-12-23 22:10:48 +00:00
|
|
|
void Denumerate(C4ValueNumbers * numbers);
|
2017-05-07 11:50:00 +00:00
|
|
|
C4Value Call(const char *szFunction, C4AulParSet *pPars=nullptr, bool fPassError=false);
|
2012-06-01 15:27:59 +00:00
|
|
|
C4Value ScenPropList;
|
|
|
|
C4Value ScenPrototype;
|
2016-11-02 23:58:02 +00:00
|
|
|
C4Effect * pScenarioEffects = nullptr;
|
2010-03-28 17:58:21 +00:00
|
|
|
};
|
2009-05-08 13:28:41 +00:00
|
|
|
|
2011-01-08 16:04:20 +00:00
|
|
|
extern C4GameScriptHost GameScript;
|
|
|
|
|
2009-05-08 13:28:41 +00:00
|
|
|
#endif
|