Skip to content

Commit

Permalink
final workings
Browse files Browse the repository at this point in the history
  • Loading branch information
eatenpancreas committed Jun 26, 2024
1 parent 5dcf078 commit e4ecf62
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 74 deletions.
20 changes: 13 additions & 7 deletions mkprov/commands/own.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::common::{Config, Province};
use crate::common::{Config, Id, PdxFile};
use clap::Args;
use std::path::PathBuf;

#[derive(Debug, Args)]
pub struct CmdArgs {
Expand All @@ -15,10 +14,17 @@ pub struct CmdArgs {

pub fn run(args: CmdArgs) {
let cfg = Config::current();
let mod_dir = PathBuf::from(cfg.require_mod_directory());
let game_dir = PathBuf::from(cfg.require_game_directory());
let mut file = PdxFile::pull(Id(args.id), cfg, "history/provinces/").unwrap();

let mut prov = Province::pull(args.id, mod_dir, game_dir).unwrap();
// prov.set_owner(args.tag);
prov.save();
if !file.contents.mutate_key_val("owner",
|kv| kv.set_value(args.tag.clone())) {
file.contents.push_field_kv("owner", args.tag.clone())
}

if !file.contents.mutate_key_val("controller",
|kv| kv.set_value(args.tag.clone())) {
file.contents.push_field_kv("controller", args.tag)
}

file.save();
}
77 changes: 68 additions & 9 deletions mkprov/common/files.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,76 @@
use std::fs::DirEntry;
use std::path::PathBuf;
use std::{fs, io};
use paradox_file::{Object, Parser};
use crate::common::{Config};

// pub fn dir_has_id(dir: &PathBuf, id: u16) -> bool {
// find_id(dir, id).is_some()
// }
pub struct PdxFile {
pub(crate) contents: Object,
location: String,
}

pub trait AsFilename {
fn as_filename(&self, dir: &PathBuf) -> Option<String>;
}

#[derive(Clone, Copy)]
pub struct Id(pub u16);

impl AsFilename for Id {
fn as_filename(&self, dir: &PathBuf) -> Option<String> {
if let Some(Ok(entry)) = find_id(dir, self.0) {
let os = entry.file_name();
os.to_str().and_then(|x| Some(x.to_string()))
} else {
None
}
}
}

impl PdxFile {
pub fn save(&self) {
fs::write(&self.location, self.contents.to_string()).unwrap();
}

pub fn pull<T: AsFilename>(file_n: T, cfg: Config, sub_dir: &str) -> Option<Self> {
let mut mod_dir = PathBuf::from(cfg.require_mod_directory());
let mut game_dir = PathBuf::from(cfg.require_game_directory());
mod_dir.push(sub_dir);
game_dir.push(sub_dir);
let file_contents;
let location;

if let Some(filename) = file_n.into_filename(&mod_dir, id) {
println!("Getting file from mod dir: {filename}");

mod_dir.push(filename);
let mod_path = mod_dir.to_str()?;
location = mod_path.to_string();

file_contents = String::from_utf8(fs::read(mod_path).ok()?).ok()?;
} else if let Some(filename) = file_n.into_filename(&game_dir, id) {
println!("Getting file from base game dir: {filename}");

game_dir.push(&filename);
let game_path = game_dir.to_str()?;

mod_dir.push(filename);
let mod_path = mod_dir.to_str()?;
location = mod_path.to_string();

file_contents = String::from_utf8(fs::read(&game_path).ok()?).ok()?;

fs::write(mod_path, &file_contents).ok()?;
} else {
eprintln!("File {file_n} is not present in mod or basegame");
return None;
}

pub fn dir_get_id_filename(dir: &PathBuf, id: u16) -> Option<String> {
if let Some(Ok(entry)) = find_id(dir, id) {
let os = entry.file_name();
os.to_str().and_then(|x| Some(x.to_string()))
} else {
None
let mut p = Parser::include_lexer(file_contents.as_str()).unwrap();
Some(PdxFile {
contents: p.parse().unwrap(),
location,
})
}
}

Expand Down
2 changes: 0 additions & 2 deletions mkprov/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
mod config;
mod files;
mod province;

pub use config::*;
pub use files::*;
pub use province::*;

use rand::Rng;

Expand Down
54 changes: 0 additions & 54 deletions mkprov/common/province.rs

This file was deleted.

6 changes: 6 additions & 0 deletions paradox_file/parsed_structure/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ impl IntoLiteral for &str {
}
}

impl IntoLiteral for String {
fn into_literal(self) -> Literal {
Literal::String(self)
}
}

impl IntoLiteral for f32 {
fn into_literal(self) -> Literal {
Literal::F32(self)
Expand Down
6 changes: 4 additions & 2 deletions paradox_file/parsed_structure/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ impl Object {
} else { None }).collect()
}

pub fn mutate_key_val(&mut self, key: Literal, mutate: impl Fn(&mut KeyVal)) {
pub fn mutate_key_val<T: IntoLiteral>(&mut self, key: T, mutate: impl Fn(&mut KeyVal)) -> bool {
for field in &mut self.fields {
if let FieldType::KeyVal(kv) = &mut field.ft {
if field.key == key {
mutate(kv);
return true
}
}
}
false
}

pub fn push_field_kv(&mut self, key: Literal, value: Literal) {
pub fn push_field_kv<KT: IntoLiteral, VT: IntoLiteral>(&mut self, key: KT, value: VT) {
self.fields.push(Field::new(key, KeyVal::new(value)));
}
}
Expand Down

0 comments on commit e4ecf62

Please sign in to comment.