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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
pub mod eff;
use ini::Ini;
use eff::{AnimEntry, Trajectory, Eff, Effect, Anim, Action, TransType};
use crate::kfn_header::KfnHeader;
use super::helpers::Entry;
/// Wrapper for the Song.ini file.
#[derive(Default, Clone)]
pub struct KfnIni {
/// The Song.ini file itself, represented using the ini-rust library.
/// To learn more: https://github.com/zonyitoo/rust-ini
pub ini: Ini,
/// Representation of the various effects, texts and syncs.
pub effs: Vec<Eff>,
}
impl KfnIni {
/// Creating a new ini file.
pub fn new() -> Self {
Self { ini: Ini::new(), effs: Vec::new(), }
}
/// Populating the General section with empty data.
pub fn populate_empty(&mut self) {
self.ini.with_section(Some("General"))
.set("Title", "")
.set("Artist", "")
.set("Album", "")
.set("Composer", "")
.set("Year", "")
.set("Track", "")
.set("GenreID", "-1")
.set("Copyright", "")
.set("Comment", "")
.set("Source", "")
.set("EffectCount", "")
.set("LanguageID", "")
.set("DiffMen", "")
.set("DiffWomen", "")
.set("KFNType", "0")
.set("Properties", "")
.set("KaraokeVersion", "")
.set("VocalGuide", "")
.set("KaraFunization", "");
}
/// Populating the General section with empty data.
pub fn populate_from_header(&mut self, header: KfnHeader) {
self.ini.with_section(Some("General"))
.set("Title", header.title)
.set("Artist", header.artist)
.set("Album", header.album)
.set("Composer", header.composer)
.set("Year", header.year)
.set("Track", header.trak)
.set("GenreID", header.genre.to_string())
.set("Copyright", header.copyright)
.set("Comment", "")
.set("Source", "")
.set("EffectCount", "")
.set("LanguageID", "")
.set("DiffMen", "")
.set("DiffWomen", "")
.set("KFNType", "0")
.set("Properties", "")
.set("KaraokeVersion", "")
.set("VocalGuide", "")
.set("KaraFunization", "");
}
/// Reading the Eff# headed sections
pub fn read_eff(&mut self) {
// get the number of effects to parse
let effect_count = self.ini.get_from(Some("General"), "EffectCount").unwrap().to_string().parse::<usize>().unwrap();
for i in 1..effect_count {
let mut eff = String::from("Eff");
eff.push_str(&i.to_string());
let section = self.ini.section(Some(eff)).unwrap();
// TODO implement the rest of the properties
let id = section.get("ID").unwrap().to_string().parse::<usize>().unwrap();
// number of animations
let nb_anim = section.get("NbAnim").unwrap().to_string().parse::<usize>().unwrap();
let text_count = section.get("TextCount").unwrap_or("0").to_string().parse::<usize>().unwrap();
let trajectory = Trajectory::from(
section.get("Trajectory").unwrap_or_default()
);
// list of animations in Anim# form
let mut anims: Vec<Anim> = Vec::new();
let mut syncs: Vec<usize> = Vec::new();
let mut texts: Vec<String> = Vec::new();
dbg!(nb_anim);
// reading the animations, if there are any.
if nb_anim != 0 {
for j in 0..nb_anim {
let mut anim_entries: Vec<AnimEntry> = Vec::new();
let mut key = String::from("Anim");
key.push_str(&j.to_string());
let value = section.get(key).unwrap();
// the time in ms, when the anim occurs. The first one will always be the time.
let time = value
.split('|')
.collect::<Vec<&str>>()
[0]
.parse::<usize>()
.unwrap();
let remaining: Vec<&str> = value
.split('|').collect::<Vec<&str>>()
.split_first()
.unwrap()
.1
.to_owned();
for i in 0..remaining.len() {
let tokens: Vec<&str> = remaining[i].split(',').collect();
// first one is always the action
let action = Action::from(tokens[0]);
let mut effect: Option<Effect> = None;
let mut trans_time: f64 = 0.0;
let mut trans_type = TransType::default();
for j in 0..tokens.len() {
let key = tokens[j].split('=').collect::<Vec<&str>>()[0];
let value = tokens[j].split('=').collect::<Vec<&str>>()[1];
match key {
"Effect" => effect = Some(Effect::from(value)),
"TransitionTime" => trans_time = value.parse().unwrap(),
"TransitionType" => trans_type = TransType::from(value),
&_ => ()
}
}
let anim_entry = AnimEntry { action, effect, trans_time, trans_type };
anim_entries.push(anim_entry)
}
anims.push(Anim {time, anim_entries});
} // for j in 0..nb_anim {
} // if nb_anim != 0 {
// reading sync data
for (key, value) in section.iter() {
// guard clause, only read Sync#, not InSync
if key.contains("Sync") && !key.contains("InSync") {
let mut sync_times: Vec<usize> = value.split(',').collect::<Vec<&str>>().iter().map(|s| s.parse::<usize>().unwrap()).collect::<Vec<usize>>();
syncs.append(&mut sync_times);
}
}
dbg!(&syncs);
if text_count != 0 {
for j in 0..text_count {
let mut key = String::from("Text");
key.push_str(&j.to_string());
let value = section.get(key).unwrap();
texts.push(value.to_owned());
}
}
dbg!(&texts);
self.effs.push(Eff { id, anims, syncs, texts, initial_trajectory: trajectory});
} // for i in 1..effect_count {
}
/// Method for setting up the effect in the Ini file.
pub fn set_eff(&mut self) {
// Set the EffectCount - number of Eff sections in the Ini.
self.ini.section_mut(Some("General")).unwrap().insert("EffectCount", self.effs.len().to_string());
// Iterate through ID of effects
for eff_n in 0..self.effs.clone().len() {
// prepare for section header
let mut eff_section = String::from("Eff");
// push number to section header, indexing starts at 1!
eff_section.push_str((eff_n + 1).to_string().as_str());
// get essential fields
self.ini.with_section(Some(eff_section.clone())).set("ID", self.effs[eff_n].id.to_string());
self.ini.with_section(Some(eff_section.clone())).set("NbAnim", self.effs[eff_n].anims.len().to_string());
self.ini.with_section(Some(eff_section.clone())).set("TextCount", self.effs[eff_n].texts.len().to_string());
// iterate through Anim#
for anim_n in 0..self.effs[eff_n].anims.len() {
// get into the appropriate section
let mut section = self.ini.with_section(Some(eff_section.as_str()));
// clone the Anim#
let anim = self.effs[eff_n].anims[anim_n].clone();
// prepare string for manipulation
let mut anim_key = String::from("Anim");
// attach row #
anim_key.push_str(anim_n.to_string().as_str());
// prepare value
let mut anim_val = String::new();
// add time, as that is always the first value in line
anim_val.push_str(anim.time.to_string().as_str());
// separator
anim_val.push('|');
// iterate through the entries
for anim_entry in anim.anim_entries {
// and push the appropriate value
anim_val.push_str(anim_entry.action.to_string().as_str())
}
// lastly set it
section.set(anim_key, anim_val);
}
self.ini.with_section(Some(eff_section.clone())).set("Sync0", self.effs[eff_n].syncs.to_owned().iter().map(|n| n.to_string()).collect::<Vec<String>>().join(","));
for text_n in 0..self.effs[eff_n].texts.len() {
let mut section = self.ini.with_section(Some(eff_section.as_str()));
let text_value = self.effs[eff_n].texts[text_n].clone();
// prepare string for manipulation
let mut text_key = String::from("Text");
// attach row #
text_key.push_str(text_n.to_string().as_str());
section.set(text_key, text_value);
}
}
}
/// Setting the source file for the KFN. This must be a music type file.
pub fn set_source(&mut self, source: &str) {
let mut value = String::from("1,I,");
value.push_str(source);
self.ini.with_section(Some("General")).set("Source", value);
}
/// Sets the list of files in the ini, based on the entries given.
pub fn set_materials(&mut self, materials: Vec<Entry>) {
let mat_count = materials.len()-1;
self.ini.with_section(Some("Materials")).set("MatCount", mat_count.to_string());
for n in 0..mat_count {
let mut key = String::from("Mat");
key.push_str(n.to_string().as_str());
let value = &materials[n].filename;
self.ini.with_section(Some("Materials")).set(key.as_str(), value.as_str());
}
}
}