Skip to content

Commit

Permalink
Introduce config.json to hold user config
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed May 13, 2024
1 parent abfca56 commit ffd423a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 31 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions website/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ tower-http = { version = "0.5.0", features = ["fs"], git = "https://github.com/r
clap = { version = "4.4.12", features = ["derive"] }
base64 = "0.22.1"
anstyle = "1.0.7"
anyhow = "1"
10 changes: 5 additions & 5 deletions website/src/assets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{cli::Args, output::OutDir};
use crate::{config::Config, output::OutDir};
use std::fmt::Write;
use std::fs;
use std::path::Path;
Expand Down Expand Up @@ -35,7 +35,7 @@ fn hash(value: &[u8]) -> String {
}

impl AssetPaths {
pub fn new(args: &Args) -> AssetPaths {
pub fn new(config: &Config) -> AssetPaths {
let dir = OutDir::new("assets_static");

let style_css = {
Expand All @@ -59,7 +59,7 @@ impl AssetPaths {
dir.create_compressed_file(&format!("{hash}.png"), contents)
};

let legacy_subaction_render_js = if args.legacy_renderer {
let legacy_subaction_render_js = if config.legacy_renderer {
let contents = include_str!("subaction_render.js");

let hash = hash(contents.as_bytes());
Expand All @@ -69,7 +69,7 @@ impl AssetPaths {
};

const WASM_FILE_NAME: &str = "fighter_renderer_bg.wasm";
let fighter_renderer_wasm = if args.legacy_renderer {
let fighter_renderer_wasm = if config.legacy_renderer {
String::new()
} else {
{
Expand Down Expand Up @@ -113,7 +113,7 @@ impl AssetPaths {
}
};

let fighter_renderer_js = if args.legacy_renderer {
let fighter_renderer_js = if config.legacy_renderer {
String::new()
} else {
let mut contents =
Expand Down
23 changes: 8 additions & 15 deletions website/src/brawl_data.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::collections::HashMap;
use std::fs;
use std::fs::{DirEntry, File};
use std::io::Read;

use crate::cli::Args;
use crate::config::Config;
use crate::page::NavLink;
use brawllib_rs::brawl_mod::BrawlMod as BrawllibMod;
use brawllib_rs::fighter::ModType;
use brawllib_rs::high_level_fighter::HighLevelFighter;

use crate::cli::Args;
use crate::page::NavLink;
use std::collections::HashMap;
use std::fs;
use std::fs::DirEntry;

pub struct BrawlMods {
pub mods: Vec<BrawlMod>,
Expand All @@ -33,16 +31,11 @@ pub struct ScriptInfo {
}

impl BrawlMods {
pub fn new(args: &Args) -> Option<BrawlMods> {
let mut mod_order = String::new();
if let Ok(mut file) = File::open("../data/mods.txt") {
file.read_to_string(&mut mod_order).unwrap();
}

pub fn new(config: &Config, args: &Args) -> Option<BrawlMods> {
match fs::read_dir("../data") {
Ok(dir) => {
let mut mod_links = vec![];
for name in mod_order.trim().lines() {
for name in &config.mods {
mod_links.push(NavLink {
name: name.to_string(),
link: format!("/{}", name),
Expand Down
4 changes: 0 additions & 4 deletions website/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ pub struct Args {
#[clap(long, short = 'w', action)]
pub generate_web: bool,

/// Use the pure JS legacy renderer
#[clap(long, short = 'l', action)]
pub legacy_renderer: bool,

/// Serve the website at localhost:8000 after generating it
#[clap(long, short)]
#[clap(long, short, action)]
Expand Down
45 changes: 45 additions & 0 deletions website/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};
use std::{
env::current_dir,
path::{Path, PathBuf},
};

#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub legacy_renderer: bool,
pub mods: Vec<String>,
pub web_root: String,
}

impl Config {
pub fn load() -> Result<Self> {
let path = default_config_path();
if path.exists() {
serde_json::from_slice(&std::fs::read(path)?).map_err(|e| anyhow!(e))
} else {
let config = Config {
legacy_renderer: false,
mods: vec![],
web_root: "/".to_owned(),
};
config.save(&path);
Ok(config)
}
}

fn save(&self, path: &Path) {
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(path, serde_json::to_vec_pretty(self).unwrap())
.map_err(|e| anyhow::anyhow!("Failed to write to {path:?} {e}"))
.unwrap();
}
}

pub fn default_config_path() -> PathBuf {
current_dir()
.unwrap()
.join("..")
.join("data")
.join("config.json")
}
9 changes: 6 additions & 3 deletions website/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ extern crate serde_derive;
#[macro_use]
extern crate log;

use config::Config;
use handlebars::Handlebars;

pub mod assets;
pub mod brawl_data;
pub mod cli;
pub mod config;
pub mod gif;
pub mod logger;
pub mod output;
Expand All @@ -25,8 +27,9 @@ use brawl_data::BrawlMods;
fn main() {
logger::init();
let args = cli::args();
let config = Config::load().unwrap();

if let Some(brawl_mods) = BrawlMods::new(&args) {
if let Some(brawl_mods) = BrawlMods::new(&config, &args) {
info!("brawl files loaded");

if args.generate_web {
Expand All @@ -36,7 +39,7 @@ fn main() {
.unwrap();
info!("handlebars templates loaded");

let assets = AssetPaths::new(&args);
let assets = AssetPaths::new(&config);
page::index::generate(&handlebars, &brawl_mods, &assets);
page::error::generate(&handlebars, &brawl_mods, &assets);
page::brawl_mod::generate(&handlebars, &brawl_mods, &assets);
Expand All @@ -45,7 +48,7 @@ fn main() {
page::actions::generate(&handlebars, &brawl_mods, &assets);
page::action::generate(&handlebars, &brawl_mods, &assets);
page::subactions::generate(&handlebars, &brawl_mods, &assets);
page::subaction::generate(&handlebars, &brawl_mods, &assets, args.legacy_renderer);
page::subaction::generate(&handlebars, &brawl_mods, &assets, config.legacy_renderer);
page::script::generate(&handlebars, &brawl_mods, &assets);
page::scripts::generate(&handlebars, &brawl_mods, &assets);
page::variables::generate(&handlebars, &brawl_mods, &assets);
Expand Down
8 changes: 4 additions & 4 deletions website/templates/index.html.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
</ul>

<h2>Discord Bot</h2>
<p>Click <a href="https://discord.com/api/oauth2/authorize?client_id=554157366916939791&scope=applications.commands%20bot&permissions=18432">here</a> to add the discord bot to your server.</p>
<h2>Updates</h2>
<p>Significant updates are usually announced on my <a href="https://twitter.com/thisIsRukai">twitter</a>.</p>
<p>Click <a
href="https://discord.com/api/oauth2/authorize?client_id=554157366916939791&scope=applications.commands%20bot&permissions=18432">here</a>
to add the discord bot to your server.</p>
</div>

<!-- Display nothing from xs to sm, take up space from md to xl -->
Expand All @@ -30,4 +30,4 @@

{{/inline}}

{{~> base ~}}
{{~> base ~}}

0 comments on commit ffd423a

Please sign in to comment.