1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
use crate::*;
use ayaka_bindings_types::VarMap;
use fallback::Fallback;
use serde::Deserialize;
use std::collections::HashMap;
#[derive(Debug, Deserialize)]
pub struct Paragraph {
pub tag: String,
pub title: Option<String>,
pub texts: Vec<Line>,
pub next: Option<Text>,
}
#[derive(Debug, Default, Deserialize)]
pub struct GameConfig {
pub title: String,
#[serde(default)]
pub author: String,
pub paras: String,
pub start: String,
#[serde(default)]
pub plugins: PluginConfig,
#[serde(default)]
pub props: HashMap<String, String>,
pub res: Option<String>,
pub base_lang: Locale,
}
#[derive(Debug, Default, Deserialize)]
pub struct PluginConfig {
pub dir: String,
#[serde(default)]
pub modules: Vec<String>,
}
pub struct Game {
pub config: GameConfig,
pub paras: HashMap<Locale, HashMap<String, Vec<Paragraph>>>,
pub res: HashMap<Locale, VarMap>,
}
impl Game {
pub fn start_context(&self) -> RawContext {
RawContext {
cur_base_para: self.config.start.clone(),
cur_para: self.config.start.clone(),
..Default::default()
}
}
fn choose_from_keys<'a, V>(&'a self, loc: &Locale, map: &'a HashMap<Locale, V>) -> &'a Locale {
loc.choose_from(map.keys())
.unwrap_or(&self.config.base_lang)
}
pub fn find_para(&self, loc: &Locale, base_tag: &str, tag: &str) -> Option<&Paragraph> {
if let Some(paras) = self.paras.get(loc) {
if let Some(paras) = paras.get(base_tag) {
for p in paras.iter() {
if p.tag == tag {
return Some(p);
}
}
}
}
None
}
pub fn find_para_fallback(
&self,
loc: &Locale,
base_tag: &str,
tag: &str,
) -> Fallback<&Paragraph> {
let key = self.choose_from_keys(loc, &self.paras);
let base_key = self.choose_from_keys(&self.config.base_lang, &self.paras);
Fallback::new(
if key == base_key {
None
} else {
self.find_para(key, base_tag, tag)
},
self.find_para(base_key, base_tag, tag),
)
}
fn find_res(&self, loc: &Locale) -> Option<&VarMap> {
self.res.get(loc)
}
pub fn find_res_fallback(&self, loc: &Locale) -> Fallback<&VarMap> {
let key = self.choose_from_keys(loc, &self.res);
let base_key = self.choose_from_keys(&self.config.base_lang, &self.res);
Fallback::new(
if key == base_key {
None
} else {
self.find_res(key)
},
self.find_res(base_key),
)
}
}