2023-08-06 08:25:04 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2023-08-02 16:26:56 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::io::{BufRead, BufReader, BufWriter, Cursor, Write};
|
2023-08-06 08:25:04 -04:00
|
|
|
|
2023-08-02 16:26:56 -04:00
|
|
|
use crate::cfg;
|
2023-08-06 08:25:04 -04:00
|
|
|
use crate::gamedata::MemoryBuffer;
|
2023-08-02 16:26:56 -04:00
|
|
|
|
2023-09-22 18:05:29 -04:00
|
|
|
/// Represents a collection of keys, mapped to their values.
|
2023-08-02 16:26:56 -04:00
|
|
|
#[derive(Debug)]
|
2023-09-22 18:05:29 -04:00
|
|
|
pub struct ConfigMap {
|
|
|
|
/// A map of setting name to value.
|
2023-08-02 16:26:56 -04:00
|
|
|
pub keys: HashMap<String, String>,
|
|
|
|
}
|
|
|
|
|
2023-09-22 18:05:29 -04:00
|
|
|
/// Represents a config file, which is made up of categories and settings. Categories may have zero to one settings.
|
2023-08-02 16:26:56 -04:00
|
|
|
#[derive(Debug)]
|
2023-09-22 18:05:29 -04:00
|
|
|
pub struct ConfigFile {
|
|
|
|
/// The categories present in this config file.
|
2023-08-02 16:26:56 -04:00
|
|
|
pub categories: Vec<String>,
|
2023-09-22 18:05:29 -04:00
|
|
|
/// A mapping of category to keys.
|
|
|
|
pub settings: HashMap<String, ConfigMap>,
|
2023-08-02 16:26:56 -04:00
|
|
|
}
|
|
|
|
|
2023-09-22 18:05:29 -04:00
|
|
|
impl ConfigFile {
|
|
|
|
/// Parses an existing config file.
|
|
|
|
pub fn from_existing(buffer: &MemoryBuffer) -> Option<ConfigFile> {
|
|
|
|
let mut cfg = ConfigFile {
|
2023-08-02 16:26:56 -04:00
|
|
|
categories: Vec::new(),
|
|
|
|
settings: HashMap::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let cursor = Cursor::new(buffer);
|
|
|
|
let reader = BufReader::new(cursor);
|
|
|
|
|
|
|
|
let mut current_category: Option<String> = None;
|
|
|
|
|
|
|
|
for (_, line) in reader.lines().enumerate() {
|
|
|
|
// now parse the line!
|
|
|
|
|
|
|
|
let unwrap = line.unwrap();
|
|
|
|
if !unwrap.is_empty() {
|
|
|
|
if unwrap.contains('<') || unwrap.contains('>') {
|
|
|
|
let name = &unwrap[1..unwrap.len() - 1];
|
|
|
|
println!("{}", name);
|
|
|
|
current_category = Some(String::from(name));
|
|
|
|
cfg.categories.push(String::from(name));
|
|
|
|
} else {
|
|
|
|
let parts = unwrap.split_once('\t').unwrap();
|
|
|
|
if !cfg.settings.contains_key(¤t_category.clone().unwrap()) {
|
2023-09-22 18:05:29 -04:00
|
|
|
cfg.settings.insert(current_category.clone().unwrap(), cfg::ConfigMap{ keys: HashMap::new() });
|
2023-08-02 16:26:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.settings.get_mut(¤t_category.clone().unwrap()).unwrap().keys.insert(parts.0.to_string(), parts.1.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(cfg)
|
|
|
|
}
|
|
|
|
|
2023-09-22 18:05:29 -04:00
|
|
|
/// Writes an existing config file to a buffer.
|
2023-08-02 16:26:56 -04:00
|
|
|
pub fn write_to_buffer(&self) -> Option<MemoryBuffer> {
|
|
|
|
let mut buffer = MemoryBuffer::new();
|
|
|
|
|
|
|
|
{
|
|
|
|
let cursor = Cursor::new(&mut buffer);
|
|
|
|
let mut writer = BufWriter::new(cursor);
|
|
|
|
|
|
|
|
for category in &self.categories {
|
|
|
|
writer.write(format!("\n<{}>", category).as_ref());
|
|
|
|
|
|
|
|
if self.settings.contains_key(category) {
|
|
|
|
for key in &self.settings[category].keys {
|
|
|
|
writer.write(format!("\n{}\t{}", key.0, key.1).as_ref());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.write(b"\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(buffer)
|
|
|
|
}
|
|
|
|
}
|