Divide Framework 0.1
A free and open-source 3D Framework under heavy development
Loading...
Searching...
No Matches
Script.h
Go to the documentation of this file.
1/*
2Copyright (c) 2018 DIVIDE-Studio
3Copyright (c) 2009 Ionut Cava
4
5This file is part of DIVIDE Framework.
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software
9and associated documentation files (the "Software"), to deal in the Software
10without restriction,
11including without limitation the rights to use, copy, modify, merge, publish,
12distribute, sublicense,
13and/or sell copies of the Software, and to permit persons to whom the
14Software is furnished to do so,
15subject to the following conditions:
16
17The above copyright notice and this permission notice shall be included in
18all copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21IMPLIED,
22INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
23PARTICULAR PURPOSE AND NONINFRINGEMENT.
24IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
25DAMAGES OR OTHER LIABILITY,
26WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
27IN CONNECTION WITH THE SOFTWARE
28OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30*/
31
32#pragma once
33#ifndef DVD_SCRIPTING_SCRIPT_H_
34#define DVD_SCRIPTING_SCRIPT_H_
35
36namespace FW {
37 FWD_DECLARE_MANAGED_CLASS(FileWatcher);
38}
39
40namespace chaiscript {
42}
43
44namespace Divide {
45
46enum class FileUpdateEvent : U8;
47
48class Script : public GUIDWrapper {
49public:
50 explicit Script(const string& scriptPathOrCode, FileType fileType = FileType::TEXT);
51 virtual ~Script() override;
52
53 static void idle();
54 static bool OnStartup();
55 static bool OnShutdown();
56
57 template<typename T>
58 void addGlobal(const T& var, const char* name, bool asConst, bool overwrite);
59
60 template <typename T>
61 void registerType(const char* typeName);
62
63 template <typename Func>
64 void registerFunction(const Func& function, const char* functionName);
65
66 template<typename T = void>
67 T eval();
68
69 static void onScriptModify(std::string_view script, FileUpdateEvent& evt);
70
71protected:
72 void compile();
73 void bootstrap();
74 void extractAtoms();
75 void preprocessIncludes(const std::string& source, I32 level /*= 0 */);
76 void caughtException(const char* message, bool isEvalException) const;
77
78protected:
79 static void handleOutput(std::string_view msg);
80
81protected:
82 //ToDo: Move this somewhere else to avoid having the include in this file -Ionut
83 chaiscript::ChaiScript_uptr _script;
84 std::string _scriptSource;
88
90
93 static bool s_scriptsReady;
94};
95
96} //namespace Divide
97
98#endif //DVD_SCRIPTING_SCRIPT_H_
99
100#include "Script.inl"
#define FWD_DECLARE_MANAGED_CLASS(T)
Utility class that adds basic GUID management to objects.
Definition: GUIDWrapper.h:44
hashMap< I64, Script * > ScriptMap
Definition: Script.h:91
static void onScriptModify(std::string_view script, FileUpdateEvent &evt)
Definition: Script.cpp:191
static bool s_scriptsReady
Definition: Script.h:93
void bootstrap()
Definition: Script.cpp:116
static I64 s_scriptFileWatcher
Definition: Script.h:89
FileNameAndPath _scriptFile
Definition: Script.h:85
void caughtException(const char *message, bool isEvalException) const
Definition: Script.cpp:214
static void handleOutput(std::string_view msg)
Definition: Script.cpp:186
FileType _scriptFileType
Definition: Script.h:86
void preprocessIncludes(const std::string &source, I32 level)
Definition: Script.cpp:134
static void idle()
Definition: Script.cpp:62
std::string _scriptSource
Definition: Script.h:84
static bool OnStartup()
Definition: Script.cpp:65
void registerType(const char *typeName)
Definition: Script.inl:54
static ScriptMap s_scripts
Definition: Script.h:92
void compile()
Definition: Script.cpp:105
void addGlobal(const T &var, const char *name, bool asConst, bool overwrite)
Definition: Script.inl:41
virtual ~Script() override
Definition: Script.cpp:52
vector< ResourcePath > _usedAtoms
Definition: Script.h:87
chaiscript::ChaiScript_uptr _script
Definition: Script.h:83
static bool OnShutdown()
Definition: Script.cpp:92
void extractAtoms()
Definition: Script.cpp:171
void registerFunction(const Func &function, const char *functionName)
Definition: Script.inl:59
Handle console commands that start with a forward slash.
Definition: AIProcessor.cpp:7
int32_t I32
uint8_t U8
eastl::vector< Type > vector
Definition: Vector.h:42
hashAlg::unordered_map< K, V, HashFun, Predicate > hashMap
Definition: HashMap.h:55
int64_t I64