OpenBF1942-Plus
Refractor 1, rebuilt from scratch
Loading...
Searching...
No Matches
con.h
Go to the documentation of this file.
1// CON script interpreter. Spec: docs/formats/con.md; reference: tools/con.py (same model and rules).
2#pragma once
3#include <deque>
4#include <map>
5#include <string>
6#include <unordered_map>
7#include <utility>
8#include <vector>
9
10#include "vfs.h"
11
12namespace obf {
13
14struct ConObject {
15 std::string type, name; // templates: create <type> <name>; instances: name = template
16 std::vector<std::pair<std::string, std::vector<std::string>>> props; // method lower-cased
17 bool deleted = false;
18 std::string dir; // folder of the script that created it (relative paths such as loadSoundScript)
19};
20
21class Con {
22public:
23 using Scope = std::unordered_map<std::string, std::string>;
24 explicit Con(const Vfs& vfs) : vfs_(vfs) {}
25
26 // Engine-side loading (docs/formats/con.md "What the engine loads by itself").
27 void load_game(); // bf1942/game/init.con + every objects/** folder
28 void load_level(const std::string& level, const std::string& mode = "conquest");
29
30 void run(const std::string& target, const std::vector<std::string>& args = {}, const std::string& cur_dir = "");
31 void execute(const std::string& text, Scope& scope, const std::string& cur_dir = "");
32 std::string resolve(const std::string& target, const std::string& cur_dir) const; // "" if missing
33
34 // subsystem -> lower name -> template; node-based map, so references stay valid.
35 std::unordered_map<std::string, std::unordered_map<std::string, ConObject>> templates;
36 std::deque<ConObject> instances; // deque: stable references while appending
37 // Commands with no active object (renderer.*, textureManager.*): subsystem -> [(method, args)].
38 std::unordered_map<std::string, std::vector<std::pair<std::string, std::vector<std::string>>>> settings;
39 std::map<std::string, int> unknown, missing;
40 std::string level_dir;
41
42private:
43 void command(const std::string& name, const std::vector<std::string>& args);
44 bool test(const std::vector<std::string>& tok, const Scope& scope);
45 const Vfs& vfs_;
46 std::unordered_map<std::string, ConObject*> active_;
47 int depth_ = 0; // run/include nesting; cycles in mod scripts would otherwise overflow the stack
48 std::vector<std::string> args_; // arguments of the script running now
49 std::string dir_; // folder of the script running now
50};
51
52std::string lower(std::string s);
53
54} // namespace obf
std::map< std::string, int > missing
Definition con.h:39
std::unordered_map< std::string, std::string > Scope
Definition con.h:23
void load_game()
Definition con.cpp:184
std::unordered_map< std::string, std::unordered_map< std::string, ConObject > > templates
Definition con.h:35
void load_level(const std::string &level, const std::string &mode="conquest")
Definition con.cpp:193
std::unordered_map< std::string, std::vector< std::pair< std::string, std::vector< std::string > > > > settings
Definition con.h:38
void execute(const std::string &text, Scope &scope, const std::string &cur_dir="")
Definition con.cpp:76
std::map< std::string, int > unknown
Definition con.h:39
std::string level_dir
Definition con.h:40
std::deque< ConObject > instances
Definition con.h:36
void run(const std::string &target, const std::vector< std::string > &args={}, const std::string &cur_dir="")
Definition con.cpp:49
Con(const Vfs &vfs)
Definition con.h:24
std::string resolve(const std::string &target, const std::string &cur_dir) const
Definition con.cpp:38
Definition vfs.h:11
Definition anim.cpp:7
std::string lower(std::string s)
Definition con.cpp:10
Definition con.h:14
std::string name
Definition con.h:15
std::string dir
Definition con.h:18
std::vector< std::pair< std::string, std::vector< std::string > > > props
Definition con.h:16
bool deleted
Definition con.h:17
std::string type
Definition con.h:15