1
Fork 0
mirror of https://github.com/redstrate/Physis.git synced 2025-04-20 11:47:46 +00:00

Better the imperfect config file documentation, rename struct

This commit is contained in:
Joshua Goins 2023-09-22 18:05:29 -04:00
parent c41f17a126
commit 5edc411bd7
2 changed files with 15 additions and 9 deletions

View file

@ -7,20 +7,26 @@ use std::io::{BufRead, BufReader, BufWriter, Cursor, Write};
use crate::cfg; use crate::cfg;
use crate::gamedata::MemoryBuffer; use crate::gamedata::MemoryBuffer;
/// Represents a collection of keys, mapped to their values.
#[derive(Debug)] #[derive(Debug)]
pub struct CFGSetting { pub struct ConfigMap {
/// A map of setting name to value.
pub keys: HashMap<String, String>, pub keys: HashMap<String, String>,
} }
/// Represents a config file, which is made up of categories and settings. Categories may have zero to one settings.
#[derive(Debug)] #[derive(Debug)]
pub struct CFG { pub struct ConfigFile {
/// The categories present in this config file.
pub categories: Vec<String>, pub categories: Vec<String>,
pub settings: HashMap<String, CFGSetting>, /// A mapping of category to keys.
pub settings: HashMap<String, ConfigMap>,
} }
impl CFG { impl ConfigFile {
pub fn from_existing(buffer: &MemoryBuffer) -> Option<CFG> { /// Parses an existing config file.
let mut cfg = CFG { pub fn from_existing(buffer: &MemoryBuffer) -> Option<ConfigFile> {
let mut cfg = ConfigFile {
categories: Vec::new(), categories: Vec::new(),
settings: HashMap::new() settings: HashMap::new()
}; };
@ -34,7 +40,6 @@ impl CFG {
// now parse the line! // now parse the line!
let unwrap = line.unwrap(); let unwrap = line.unwrap();
println!("{}", unwrap);
if !unwrap.is_empty() { if !unwrap.is_empty() {
if unwrap.contains('<') || unwrap.contains('>') { if unwrap.contains('<') || unwrap.contains('>') {
let name = &unwrap[1..unwrap.len() - 1]; let name = &unwrap[1..unwrap.len() - 1];
@ -44,7 +49,7 @@ impl CFG {
} else { } else {
let parts = unwrap.split_once('\t').unwrap(); let parts = unwrap.split_once('\t').unwrap();
if !cfg.settings.contains_key(&current_category.clone().unwrap()) { if !cfg.settings.contains_key(&current_category.clone().unwrap()) {
cfg.settings.insert(current_category.clone().unwrap(), cfg::CFGSetting{ keys: HashMap::new() }); cfg.settings.insert(current_category.clone().unwrap(), cfg::ConfigMap{ keys: HashMap::new() });
} }
cfg.settings.get_mut(&current_category.clone().unwrap()).unwrap().keys.insert(parts.0.to_string(), parts.1.to_string()); cfg.settings.get_mut(&current_category.clone().unwrap()).unwrap().keys.insert(parts.0.to_string(), parts.1.to_string());
@ -55,6 +60,7 @@ impl CFG {
Some(cfg) Some(cfg)
} }
/// Writes an existing config file to a buffer.
pub fn write_to_buffer(&self) -> Option<MemoryBuffer> { pub fn write_to_buffer(&self) -> Option<MemoryBuffer> {
let mut buffer = MemoryBuffer::new(); let mut buffer = MemoryBuffer::new();

View file

@ -86,7 +86,7 @@ pub mod cmp;
/// Reading character save datas files (DAT) /// Reading character save datas files (DAT)
pub mod chardat; pub mod chardat;
/// Reading config files (CFG) /// Reading and writing the plaintext config files (CFG) used by the game to store most of it's configuration.
pub mod cfg; pub mod cfg;
mod crc; mod crc;