OpenMoHAA 0.82.0
Loading...
Searching...
No Matches
gamescript.h
1/*
2===========================================================================
3Copyright (C) 2025 the OpenMoHAA team
4
5This file is part of OpenMoHAA source code.
6
7OpenMoHAA source code is free software; you can redistribute it
8and/or modify it under the terms of the GNU General Public License as
9published by the Free Software Foundation; either version 2 of the License,
10or (at your option) any later version.
11
12OpenMoHAA source code is distributed in the hope that it will be
13useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with OpenMoHAA source code; if not, write to the Free Software
19Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20===========================================================================
21*/
22
23// gamescript.h: Subclass of script that preprocesses labels
24
25#pragma once
26
27#include "class.h"
28#include "script.h"
29#include "archive.h"
30
31class Listener;
32class ScriptThread;
33class ScriptVariable;
34class GameScript;
35
36typedef struct {
37 byte *codepos; // code position pointer
38 const_str key; // label name
39 bool isprivate; // new script engine implementation
41
42struct sourceinfo_t {
43 unsigned int sourcePos;
44 unsigned int startLinePos;
45 int column;
46 int line;
47
48 sourceinfo_t()
49 : sourcePos(0)
50 , column(0)
51 , line(0)
52 {}
53};
54
55class AbstractScript
56{
57public:
58 // File variables
59 const_str m_Filename;
60 char *m_SourceBuffer;
61 size_t m_SourceLength;
62
63 // Developper variable
65
66 sourceinfo_t cachedInfo[16];
67 size_t cachedInfoIndex;
68
69public:
70 AbstractScript();
71
72 str& Filename(void);
73 const_str ConstFilename(void);
74 bool GetSourceAt(size_t sourcePos, str *sourceLine, int& column, int& line);
75 bool GetSourceAt(const unsigned char *sourcePos, str *sourceLine, int& column, int& line);
76 void PrintSourcePos(sourceinfo_t *sourcePos, bool dev);
77 void PrintSourcePos(size_t sourcePos, bool dev);
78 void PrintSourcePos(unsigned char *m_pCodePos, bool dev);
79 void PrintSourcePos(str sourceLine, int column, int line, bool dev);
80};
81
82class StateScript : public Class
83{
84 friend class GameScript;
85
86private:
87 // Label list
89
90public:
91 // Parent gamescript
92 GameScript *m_Parent;
93
94public:
95 StateScript();
96
97 void Archive(Archiver& arc) override;
98
99 bool AddLabel(str label, unsigned char *pos, bool private_section = false);
100 bool AddLabel(const_str label, unsigned char *pos, bool private_section = false);
101 unsigned char *FindLabel(str label);
102 unsigned char *FindLabel(const_str label);
103 const_str NearestLabel(unsigned char *pos);
104};
105
107{
108public:
109 // program variable
110 StateScript m_StateScript;
111
112 // code position variables
113 unsigned char *m_TryStartCodePos;
114 unsigned char *m_TryEndCodePos;
115};
116
117class GameScript : public AbstractScript
118{
119protected:
120 // try/throw variable
121 Container<CatchBlock *> m_CatchBlocks;
122 Container<StateScript *> m_StateScripts;
123
124public:
125 // program variables
126 StateScript m_State;
127 unsigned char *m_ProgBuffer;
128 size_t m_ProgLength;
129
130 // compile variables
131 bool successCompile;
132 bool m_bPrecompiled;
133
134 // stack variables
135 unsigned int requiredStackSize;
136
137public:
138 GameScript();
139 GameScript(const char *filename);
140 ~GameScript();
141
142 void Archive(Archiver& arc);
143 static void Archive(Archiver& arc, GameScript *& scr);
144 void ArchiveCodePos(Archiver& arc, unsigned char **codePos);
145
146 void Close(void);
147 void Load(const void *sourceBuffer, size_t sourceLength);
148
149 bool GetCodePos(unsigned char *codePos, str& filename, int& pos);
150 bool SetCodePos(unsigned char *& codePos, str& filename, int pos);
151
152 unsigned int GetRequiredStackSize(void);
153
154 qboolean labelExists(const char *name);
155
156 StateScript *CreateCatchStateScript(unsigned char *try_begin_code_pos, unsigned char *try_end_code_pos);
157 StateScript *CreateSwitchStateScript(void);
158
159 StateScript *GetCatchStateScript(unsigned char *in, unsigned char *& out);
160
161 bool ScriptCheck(void);
162};
163
164class ScriptThreadLabel
165{
166private:
167 GameScript *m_Script;
168 const_str m_Label;
169
170public:
171 ScriptThreadLabel();
172
173 ScriptThread *Create(Listener *listener) const;
174 void Execute(Listener *listener = NULL) const;
175 void Execute(Listener *listener, Event& ev) const;
176 void Execute(Listener *listener, Event *ev) const;
177 void Execute(Listener *pSelf, const SafePtr<Listener>& listener, const SafePtr<Listener>& param) const;
178
179 void Clear();
180 void Set(const char *label);
181 void Set(const_str label);
182 void SetScript(const ScriptVariable& label);
183 void SetScript(const char *label);
184 void SetScript(const_str label);
185 void SetThread(const ScriptVariable& label);
186
187 bool TrySet(const_str label);
188 bool TrySet(const char *label);
189 bool TrySetScript(const_str label);
190 bool TrySetScript(const char *label);
191
192 bool IsSet(void) const;
193 bool IsFile(const_str filename) const;
194
195 void GetScriptValue(ScriptVariable *var) const;
196
197 void Archive(Archiver& arc);
198
199 friend bool operator==(const ScriptThreadLabel& a, const ScriptThreadLabel& b);
200};
201
202inline bool operator==(const ScriptThreadLabel& a, const ScriptThreadLabel& b)
203{
204 return a.m_Label == b.m_Label && a.m_Script == b.m_Script;
205}
Definition archive.h:86
Definition gamescript.h:107
Definition container.h:85
Definition listener.h:246
Definition gamescript.h:118
Definition listener.h:450
Definition safeptr.h:160
Definition gamescript.h:165
Definition scriptthread.h:28
Definition scriptvariable.h:75
Definition gamescript.h:83
Definition con_set.h:119
Definition str.h:77
Definition gamescript.h:36
Definition gamescript.h:42