Skip to content

Commit f1d9748

Browse files
authored
Merge pull request #11 from LinpgFoundation/dev
[REVISION] vns 2.3
2 parents 3158f38 + e03c325 commit f1d9748

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1254
-277
lines changed

examples/chapter1_dialogs_exp.json

+38-35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,41 @@
11
{
22
"compiler": {
3-
"compiledAt": 1716322057,
4-
"reversion": 2,
3+
"compiledAt": 1720842345,
4+
"patch": 0,
5+
"reversion": 3,
56
"version": 2
67
},
78
"dialogues": {
89
"dialog_example": {
10+
"branch_choices": {
11+
"background_image": "bg2.png",
12+
"background_music": "bgm2.ogg",
13+
"character_images": [
14+
"test_character1.png&silent",
15+
"test_character2.png&silent"
16+
],
17+
"contents": [
18+
"Hello I am test character 2."
19+
],
20+
"narrator": "Test Character 2",
21+
"next": {
22+
"target": [
23+
{
24+
"id": "jumping_point1",
25+
"text": "click here to go to jump point 1"
26+
},
27+
{
28+
"id": "last_one",
29+
"text": "click here to go to the end"
30+
}
31+
],
32+
"type": "options"
33+
},
34+
"notes": [
35+
"this the second dialogue of section 2"
36+
],
37+
"previous": "~03"
38+
},
939
"head": {
1040
"background_image": "bg1.png",
1141
"background_music": "bgm1.ogg",
@@ -41,7 +71,8 @@
4171
"next": {
4272
"target": "~07",
4373
"type": "default"
44-
}
74+
},
75+
"previous": "branch_choices"
4576
},
4677
"last_one": {
4778
"background_image": "bg2.png",
@@ -54,7 +85,7 @@
5485
"Ok this is the end"
5586
],
5687
"narrator": "Test Character 2",
57-
"previous": "~04"
88+
"previous": "branch_choices"
5889
},
5990
"~01": {
6091
"background_image": "bg1.png",
@@ -143,42 +174,14 @@
143174
],
144175
"narrator": "Test Character 1",
145176
"next": {
146-
"target": "~04",
177+
"target": "branch_choices",
147178
"type": "default"
148179
},
149180
"notes": [
150-
"this the first dialogue of section 2"
181+
"this the first dialogue of section 2,",
182+
"and there are more than one note"
151183
]
152184
},
153-
"~04": {
154-
"background_image": "bg2.png",
155-
"background_music": "bgm2.ogg",
156-
"character_images": [
157-
"test_character1.png&silent",
158-
"test_character2.png&silent"
159-
],
160-
"contents": [
161-
"Hello I am test character 2."
162-
],
163-
"narrator": "Test Character 2",
164-
"next": {
165-
"target": [
166-
{
167-
"id": "jumping_point1",
168-
"text": "click here to go to jump point 1"
169-
},
170-
{
171-
"id": "last_one",
172-
"text": "click here to go to the end"
173-
}
174-
],
175-
"type": "options"
176-
},
177-
"notes": [
178-
"this the second dialogue of section 2"
179-
],
180-
"previous": "~03"
181-
},
182185
"~07": {
183186
"background_image": "bg2.png",
184187
"background_music": "bgm2.ogg",

examples/chapter_branches_tests.vns

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Fundamental parameters
2+
[vns]>=2.3
3+
[id]2
4+
[language]English
5+
6+
[section]dialog_example
7+
8+
9+
[bgi]bg1.png
10+
[bgm]bgm1.ogg
11+
12+
The dev:
13+
- This is a file use for testing branch
14+
- such as jump and option
15+
16+
The dev:
17+
- And I really hope everything works.
18+
19+
[jump]first_jump
20+
21+
[block]
22+
23+
The dev:
24+
- This is a void
25+
- You should not be here
26+
27+
[end]
28+
29+
[label] first_jump
30+
31+
The dev:
32+
- And if they do, you will be here
33+
- And you should be able to go back
34+
35+
The dev:
36+
- Then cheers, we did!
37+
38+
The dev:
39+
- What about a jump where we cannot go back?
40+
41+
[jump] no_prev_jump
42+
43+
[label] jump_prev
44+
45+
The dev:
46+
- This is a void
47+
- You should not be here
48+
49+
[jump_]the_end
50+
51+
[block]
52+
53+
[label] no_prev_jump
54+
55+
The dev:
56+
- In this case, you should not be able to go back
57+
58+
[jump]jump_prev
59+
60+
[label]the_end
61+
62+
The dev:
63+
- This is the end

examples/chapter_example.vns

+7-2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ null:
3535
[scene]bg2.png
3636
[bgm]bgm2.ogg
3737
[display]test_character1.png test_character2.png
38-
# this the first dialogue of section 2
38+
# this the first dialogue of section 2,
39+
// add a extra comment and space
40+
41+
# and there are more than one note
3942
Test Character 1:
4043
- Hello I am test character 1.
4144
# this the second dialogue of section 2
45+
[label]branch_choices
4246
Test Character 2:
4347
- Hello I am test character 2.
44-
[option]click here to go to jump point 1 -> jumping_point1
48+
[option] click here to go to jump point 1 -> jumping_point1
4549
[option]click here to go to the end -> last_one
4650

4751
[label]last_one
@@ -58,4 +62,5 @@ Test Character 3:
5862

5963
Test Character 3:
6064
- My previous should be jump point 1
65+
6166
- And the next should be None

examples/dummy.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a dummy file used for testing!

vns-cpp/CMakeLists.txt

+8-7
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,21 @@ add_subdirectory(extern/json-schema-validator)
1919

2020
# Add source to this project's executable.
2121
add_executable(vns ${SOURCES} vns.cpp vns.hpp scriptProcessor.cpp naming.cpp naming.hpp dialogue.hpp dialogue.cpp
22-
dialogueNext.hpp dialogueNext.cpp scriptProcessor.hpp functions.hpp tests.hpp tests.cpp functions.cpp
23-
compiler.hpp compiler.cpp version.hpp dialoguesManager.hpp dialoguesManager.cpp decompiler.cpp decompiler.hpp
24-
Event.cpp Event.hpp expressionParser.cpp expressionParser.hpp number.cpp number.hpp operation.hpp
25-
validator.hpp validator.cpp schema.hpp)
22+
dialogueNext.hpp dialogueNext.cpp scriptProcessor.hpp functions.hpp functions.cpp compiler.hpp compiler.cpp
23+
version.hpp dialoguesManager.hpp dialoguesManager.cpp decompiler.cpp decompiler.hpp event.cpp event.hpp
24+
expressionParser.cpp expressionParser.hpp number.cpp number.hpp operation.hpp validator.hpp validator.cpp
25+
tests.hpp tests.cpp
26+
schema.hpp tags.hpp version.cpp)
2627

2728
target_link_libraries(vns PRIVATE nlohmann_json::nlohmann_json nlohmann_json_schema_validator)
2829

2930
set_property(TARGET vns PROPERTY CXX_STANDARD 20)
3031

3132
add_library(vns_lib STATIC ${SOURCES} scriptProcessor.cpp naming.cpp naming.hpp dialogue.hpp dialogue.cpp
3233
dialogueNext.hpp dialogueNext.cpp scriptProcessor.hpp functions.hpp functions.cpp
33-
compiler.hpp compiler.cpp version.hpp dialoguesManager.hpp dialoguesManager.cpp decompiler.cpp decompiler.hpp
34-
Event.cpp Event.hpp expressionParser.cpp expressionParser.hpp number.cpp number.hpp operation.hpp
35-
validator.hpp validator.cpp schema.hpp)
34+
compiler.hpp compiler.cpp version.hpp version.cpp dialoguesManager.hpp dialoguesManager.cpp decompiler.hpp
35+
decompiler.cpp event.cpp event.hpp expressionParser.cpp expressionParser.hpp number.cpp number.hpp operation.hpp
36+
validator.hpp validator.cpp schema.hpp tags.hpp)
3637

3738
target_link_libraries(vns_lib PRIVATE nlohmann_json::nlohmann_json nlohmann_json_schema_validator)
3839

vns-cpp/compiler.cpp

+64-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ std::unordered_map<std::string, size_t> Compiler::get_compiler_info()
99
{
1010
return {{"version", VERSION},
1111
{"reversion", REVISION},
12+
{"patch", PATCH},
1213
{"compiledAt", static_cast<size_t>(std::time(nullptr))}};
1314
}
1415

@@ -62,35 +63,89 @@ nlohmann::json Compiler::output(const ScriptProcessor &processor)
6263
return json_data;
6364
}
6465

65-
// compile the file
66+
// compile the file/files
6667
void Compiler::compile(const std::filesystem::path &path)
6768
{
68-
compile(path, path.root_directory());
69+
compile(path, is_directory(path) ? path : path.parent_path());
6970
}
7071

7172
void Compiler::compile(const std::filesystem::path &path, const std::filesystem::path &out_dir)
7273
{
73-
if (!is_directory(path))
74-
{
75-
save(load_as_json(path), out_dir);
76-
} else
74+
if (is_directory(path))
7775
{
7876
for (const auto &entry: std::filesystem::directory_iterator(path))
7977
{
8078
if (entry.path().extension() == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
8179
{
82-
compile(entry.path());
80+
compile_script(entry.path(), out_dir);
81+
} else if (entry.is_directory())
82+
{
83+
compile(entry.path(), out_dir / entry.path().filename());
84+
}
85+
}
86+
} else if (path.extension() == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
87+
{
88+
compile_script(path, out_dir);
89+
}
90+
}
91+
92+
// compile a script file and save it to given output dir
93+
void Compiler::compile_script(const std::filesystem::path &path, const std::filesystem::path &out_dir)
94+
{
95+
save(load_as_json(path), out_dir);
96+
}
97+
98+
// compile the file/files using multithreading
99+
void Compiler::parallel_compile(const std::filesystem::path &path)
100+
{
101+
parallel_compile(path, is_directory(path) ? path : path.parent_path());
102+
}
103+
104+
void Compiler::parallel_compile(const std::filesystem::path &path, const std::filesystem::path &out_dir)
105+
{
106+
std::vector<std::thread> tasks;
107+
add_tasks(path, out_dir, tasks);
108+
for (std::thread &task: tasks)
109+
task.join();
110+
}
111+
112+
// create thread(s) that start compiling script(s)
113+
void Compiler::add_tasks(
114+
const std::filesystem::path &path, const std::filesystem::path &out_dir, std::vector<std::thread> &tasks)
115+
{
116+
if (is_directory(path))
117+
{
118+
for (const auto &entry: std::filesystem::directory_iterator(path))
119+
{
120+
const std::filesystem::path &entryPath = entry.path();
121+
if (entryPath.extension() == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
122+
{
123+
tasks.emplace_back([entryPath, out_dir]() {
124+
Compiler::compile_script(entryPath, out_dir);
125+
});
126+
} else if (entry.is_directory())
127+
{
128+
add_tasks(entryPath, out_dir / entryPath.filename(), tasks);
83129
}
84130
}
131+
} else if (path.extension() == ScriptProcessor::SCRIPTS_FILE_EXTENSION)
132+
{
133+
tasks.emplace_back([path, out_dir]() {
134+
Compiler::compile_script(path, out_dir);
135+
});
85136
}
86137
}
87138

88139
// save the json data into json
89140
void Compiler::save(const nlohmann::json &json_data, const std::filesystem::path &dir_path)
90141
{
91142
std::stringstream file_name;
92-
std::string id = json_data.at("id");
93-
std::string lang = json_data.at("language");
143+
const std::string id = json_data.at("id");
144+
const std::string lang = json_data.at("language");
94145
file_name << "chapter" << id << "_dialogs_" << lang << ".json";
146+
// check output dir if it does not exist
147+
if (!exists(dir_path))
148+
create_directories(dir_path);
149+
// save data
95150
save_json(dir_path / file_name.str(), json_data);
96151
}

vns-cpp/compiler.hpp

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef COMPILER_HPP
22
#define COMPILER_HPP
33

4+
#include <thread>
45
#include <filesystem>
56
#include <unordered_map>
67
#include <nlohmann/json.hpp>
@@ -18,6 +19,10 @@ class Compiler
1819

1920
static void compile(const std::filesystem::path &, const std::filesystem::path &);
2021

22+
static void parallel_compile(const std::filesystem::path &);
23+
24+
static void parallel_compile(const std::filesystem::path &, const std::filesystem::path &);
25+
2126
static std::string load_as_string(const std::filesystem::path &);
2227

2328
static std::string load_as_string(const std::string &);
@@ -30,6 +35,10 @@ class Compiler
3035
static nlohmann::json output(const ScriptProcessor &);
3136

3237
static void save(const nlohmann::json &, const std::filesystem::path &);
38+
39+
static void compile_script(const std::filesystem::path &, const std::filesystem::path &);
40+
41+
static void add_tasks(const std::filesystem::path &, const std::filesystem::path &, std::vector<std::thread> &);
3342
};
3443

3544
#endif

vns-cpp/dialogue.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ Dialogue::Dialogue(const std::string &content_id, const nlohmann::json &data)
3838
}
3939
}
4040

41+
bool Dialogue::has_previous() const
42+
{
43+
return !previous.empty();
44+
}
45+
46+
4147
bool Dialogue::has_next() const
4248
{
4349
return !next.is_null();

vns-cpp/dialogue.hpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include <unordered_map>
66
#include <vector>
77
#include "dialogueNext.hpp"
8-
#include "Event.hpp"
8+
#include "event.hpp"
99

1010
using dialogue_data_t = std::unordered_map<std::string, std::variant<std::string, std::vector<std::string>, dialogue_next_t, std::vector<event_t>>>;
1111

@@ -38,6 +38,8 @@ class Dialogue
3838
{
3939
}
4040

41+
[[nodiscard]] bool has_previous() const;
42+
4143
[[nodiscard]] bool has_next() const;
4244

4345
void set_next(const std::string &, const dialogue_next_target_t &);

0 commit comments

Comments
 (0)