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
297
298
use std::fs;
use std::path::Path;
use derivative::Derivative;
use ini::Ini;
use super::helpers::{Entry, u32_to_u8_arr};
use super::helpers::file_type::{FileType, ToBinary};
use super::kfn_ini::KfnIni;
/// KfnHeader depicting the header contents of a KFN file
#[derive(Derivative)]
#[derivative(Debug)]
pub struct KfnData {
/// The location of the Songs.ini file.
pub path_song_ini: String,
/// Files in the directory/library
pub entries: Vec<Entry>,
/// End of the directory header
pub offset_dir_end: usize,
/// Representation of the last file of the directory, the Song.ini.
#[derivative(Debug="ignore")]
pub song: KfnIni,
}
impl KfnData {
/// Creating a new KfnData with default values.
pub fn new() -> Self {
let dir_songs_ini = String::new();
let entries = Vec::new();
let offset_dir_end = 0;
let mut kfn_ini = KfnIni::new();
kfn_ini.populate_empty();
Self {
path_song_ini: dir_songs_ini, entries, offset_dir_end, song: kfn_ini
}
}
/// Get the Songs.ini file from the entries.
pub fn get_songs_ini(&self) -> Option<Entry> {
let mut song_ini = None;
for entry in self.entries.clone() {
if entry.filename == "Song.ini" {
song_ini = Some(entry);
break;
}
}
song_ini
}
/// Reads the INI file into the struct.
pub fn read_ini(&mut self) {
self.song.ini = Ini::load_from_str(String::from_utf8(self.get_songs_ini().unwrap().file_bin).unwrap().as_str()).unwrap();
}
/// Updates the ini file. Removes the Song.ini entry, then recreates the INI file from the struct.
pub fn update_ini(&mut self) {
// remove the entry
self.remove_entry_by_name("Song.ini");
// updating the ini
self.song.set_materials(self.entries.clone());
// creating a destination vector for the data
let mut writer = Vec::new();
// write the data into the vector
self.song.ini.write_to(&mut writer).unwrap();
let data = writer.to_owned();
// create a new entry
let new_entry = Entry {
file_type: FileType::SongIni,
filename: "Song.ini".to_string(),
len1: data.len(),
offset: 0,
len2: data.len(),
flags: usize::default(),
file_bin: data,
};
// and add the entry
self.add_entry(new_entry);
}
/// Used internally before writing to binary, to readjust the offsets that became misaligned.
fn adjust_dir_offset(&mut self) {
self.entries[0].offset = 0;
for i in 1..self.entries.len() {
self.entries[i].offset = self.entries[i-1].offset + self.entries[i-1].len1;
}
}
/// Adds an entry to the directory
pub fn add_entry(&mut self, new_entry: Entry) {
/* let last_entry = self.entries[self.entries.len()].clone();
let new_offset: usize = last_entry.offset + new_entry.len1;
new_entry.offset = new_offset; */
self.entries.push(new_entry);
}
/// Adding a new entry from the data.
pub fn add_entry_from_file(&mut self, filename: &str) {
// reading the file from the file system
let new_file = fs::read(filename).unwrap();
// splitting it at the point to get the extension
let parts : Vec<&str> = filename.split('.').collect();
// match the extension to the appropriate file type
let extension = match parts.last() {
Some(v) =>
match *v {
"png" => FileType::Image,
"jpg" => FileType::Image,
"mp3" => FileType::Music,
"wav" => FileType::Music,
"ttf" => FileType::Font,
"otf" => FileType::Font,
&_ => FileType::INVALID,
},
None => FileType::INVALID,
};
let filename = Path::new(filename);
let filename = filename.file_name().unwrap().to_str().unwrap();
// create an entry
let new_entry = Entry {
file_type: extension,
filename: filename.to_string(),
len1: new_file.len(),
offset: self.get_next_offset(),
len2: new_file.len(),
flags: 0,
file_bin: new_file,
};
// add the entry to the library
self.add_entry(new_entry);
// update the ini, so that it contains the new file as well
self.update_ini();
}
/// Returning an entry by ID, it it exists.
pub fn get_entry_by_id(&self, id: usize) -> Option<Entry> {
if self.entries.len() > id {
Some(self.entries[id].clone())
} else {
None
}
}
/// Returning an entry by file name.
pub fn get_entry_by_name(&self, name: &str) -> Option<Entry> {
let mut id: isize = -1;
for i in 0..self.entries.len() {
if self.entries[i].filename == name {
id = i as isize;
}
}
if id != -1 {
Some(self.entries[id as usize].clone())
} else {
None
}
}
/// Removing an entry from the data.
pub fn remove_entry_by_id(&mut self, id: usize) {
// Extract the entry and save it
// to have it's length later.
let removed_entry = self.entries.remove(id);
// iterate over the entries...
for i in id+1..self.entries.len()-1 {
// ...and remove the removed entry's length from their offset.
self.entries[i].offset -= removed_entry.len1;
}
}
/// Removing an entry by name from the data. If it doesn't exist, it wont delete.
pub fn remove_entry_by_name(&mut self, name: &str) {
let mut id: isize = -1;
for i in 0..self.entries.len() {
if self.entries[i].filename == name {
id = i as isize;
}
}
if id == -1 {
return;
}
// Extract the entry and save it to have it's length later.
let removed_entry = self.entries.remove(id as usize);
// iterate over the entries...
for i in id as usize+1..self.entries.len()-1 {
// ...and remove the removed entry's length from their offset.
self.entries[i as usize].offset -= removed_entry.len1;
}
}
/// Gets the next available offset for the new entry.
pub fn get_next_offset(&self) -> usize {
if self.entries.len() == 0 {
return 0;
}
// get the id of the last entry
let last_index = self.entries.len()-1;
// return the last entry's offset plus its length, removed the end of the dir header to get the new offset
self.entries[last_index].offset + self.entries[last_index].len1 - self.offset_dir_end
}
}
impl ToBinary for KfnData {
fn to_binary(&mut self) -> Vec<u8> {
self.adjust_dir_offset();
let mut data: Vec<u8> = Vec::new();
data.append(&mut u32_to_u8_arr(self.entries.len() as u32));
for entry in &self.entries {
// append the filename length
data.append(&mut u32_to_u8_arr(entry.filename.len() as u32));
// a. filename
data.append(&mut entry.filename.as_bytes().to_owned());
// a. file type
data.append(&mut u32_to_u8_arr(entry.file_type.into()));
// a. length 1
data.append(&mut u32_to_u8_arr(entry.len1 as u32));
// a. offset
data.append(&mut u32_to_u8_arr(entry.offset as u32));
// a. length 2
data.append(&mut u32_to_u8_arr(entry.len2 as u32));
// a. flags
data.append(&mut u32_to_u8_arr(entry.flags as u32));
}
// append the file data
for entry in &self.entries {
data.append(&mut entry.file_bin.to_owned());
}
//data.append(&mut self.get_songs_ini().unwrap().file_bin);
data
}
}