Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions src/config/cg.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::HashMap;
use std::fs;
use serde::{Deserialize, Serialize};
use crate::config::ENGINE_CONFIG;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs};

lazy_static::lazy_static! {
pub static ref CG_LENGTH: CgLength = load_cg();
Expand Down Expand Up @@ -51,20 +50,18 @@ impl CgLength {
}

fn load_cg() -> CgLength {
let content = fs::read_to_string(format!(
"{}length.toml",
ENGINE_CONFIG.cg_path(),
))
.unwrap();
let content = fs::read_to_string(format!("{}length.toml", ENGINE_CONFIG.cg_path(),)).unwrap();
let name_item: LengthWrapper = toml::from_str(&content).unwrap();
let index_item = name_item.cast.clone();
CgLength {
cg_by_name: name_item.cast
cg_by_name: name_item
.cast
.into_iter()
.map(|length| (length.name, (length.index, length.length)))
.collect(),
cg_by_id: index_item.into_iter()
cg_by_id: index_item
.into_iter()
.map(|length| (length.index, (length.name, length.length)))
.collect(),
}
}
}
14 changes: 5 additions & 9 deletions src/config/character_volume.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::collections::HashMap;
use std::rc::Rc;
use crate::config::{user::USER_CONFIG, ENGINE_CONFIG};
use crate::executors::executor::Executor;
use crate::ui::initialize::{CharacterVolume, MainWindow};
use serde::{Deserialize, Serialize};
use slint::{Model, Weak};
use crate::config::ENGINE_CONFIG;
use crate::config::user::USER_CONFIG;
use crate::executor::executor::Executor;
use crate::ui::ui::{CharacterVolume, MainWindow};
use std::{collections::HashMap, rc::Rc};

#[derive(Debug, Deserialize, Serialize)]
pub(crate) struct CharacterVolumeConfig {
Expand Down Expand Up @@ -57,9 +55,7 @@ impl Executor {
})
.collect();

window.set_character_volumes(
Rc::new(slint::VecModel::from(volumes)).into()
);
window.set_character_volumes(Rc::new(slint::VecModel::from(volumes)).into());
}
}
}
19 changes: 9 additions & 10 deletions src/config/extra.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::fs;
use serde::{Deserialize, Serialize};
use crate::config::cg::CgConfig;
use crate::config::ENGINE_CONFIG;
use crate::config::{cg::CgConfig, ENGINE_CONFIG};
use crate::error::{EngineError, SaveError};
use crate::executor::executor::Executor;
use crate::executors::executor::Executor;
use serde::{Deserialize, Serialize};
use std::fs;

lazy_static::lazy_static! {
pub static ref EXTRA_CONFIG: ExtraConfig = load_extra_config();
Expand Down Expand Up @@ -34,11 +33,11 @@ fn load_extra_config() -> ExtraConfig {

pub fn save_extra_config(cg: u64) -> Result<(), EngineError> {
let path = format!("{}/extra.toml", ENGINE_CONFIG.save_path());
let content = toml::to_string(&ExtraConfig{cg: CgConfig::new(cg)}).map_err(SaveError::from)?;
fs::write(&path, content).map_err(|e| SaveError::Write {
path,
source: e,
})?;
let content = toml::to_string(&ExtraConfig {
cg: CgConfig::new(cg),
})
.map_err(SaveError::from)?;
fs::write(&path, content).map_err(|e| SaveError::Write { path, source: e })?;

Ok(())
}
18 changes: 8 additions & 10 deletions src/config/figure.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::config::ENGINE_CONFIG;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs;
use std::{collections::HashMap, fs};

lazy_static::lazy_static! {
pub static ref FIGURE_CONFIG: FigureConfig = load_figure();
Expand Down Expand Up @@ -42,6 +41,12 @@ struct FigureRead {
offset: Offset,
}

type FigureConfigRef<'a> = (
Option<&'a HashMap<String, f32>>,
Option<&'a HashMap<String, (f32, f32)>>,
Option<&'a f32>,
);

#[derive(Debug, Deserialize, Serialize)]
pub struct FigureConfig {
body_list: HashMap<String, HashMap<String, f32>>,
Expand All @@ -50,14 +55,7 @@ pub struct FigureConfig {
}

impl FigureConfig {
pub fn find(
&self,
name: &str,
) -> (
Option<&HashMap<String, f32>>,
Option<&HashMap<String, (f32, f32)>>,
Option<&f32>,
) {
pub fn find(&self, name: &str) -> FigureConfigRef<'_> {
(
self.body_list.get(name),
self.face_list.get(name),
Expand Down
2 changes: 1 addition & 1 deletion src/config/initialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Character {
pub(crate) fn list(&self) -> &HashMap<String, String> {
&self.0
}

pub(crate) fn name_list(&self) -> HashSet<&String> {
self.0.keys().collect()
}
Expand Down
20 changes: 11 additions & 9 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use std::collections::{HashMap, HashSet};
use crate::config::initialize::{Character, InitializeConfig};
use serde::{Deserialize, Serialize};
use std::fs;
use std::{
collections::{HashMap, HashSet},
fs,
};

pub mod figure;
pub mod initialize;
pub mod save_load;

pub mod cg;
pub mod character_volume;
pub mod extra;
pub mod system;
pub mod text;
pub mod user;
pub mod voice;
pub mod volume;
pub mod extra;
pub mod cg;
pub mod character_volume;

lazy_static::lazy_static! {
pub static ref ENGINE_CONFIG: EngineConfig = load_engine_config();
Expand All @@ -34,7 +36,7 @@ impl EngineConfig {
pub fn background_path(&self) -> &str {
&self.initialize.background_path
}

pub fn cg_path(&self) -> &str {
&self.initialize.cg_path
}
Expand Down Expand Up @@ -66,13 +68,13 @@ impl EngineConfig {
pub fn character_name_list(&self) -> HashSet<&String> {
self.character.name_list()
}

pub fn character_full_name_list(&self) -> HashSet<&String> {
self.character.full_name_list()
}

pub fn character_list(&self) -> &HashMap<String, String> {
&self.character.list()
self.character.list()
}
}

Expand Down
9 changes: 3 additions & 6 deletions src/config/save_load.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use crate::config::ENGINE_CONFIG;
use crate::error::{EngineError, SaveError};
use crate::executor::executor::Executor;
use crate::executors::executor::Executor;
use serde::{Deserialize, Serialize};
use slint::{Image, ToSharedString, VecModel};
use std::fs;
use std::path::Path;
use std::rc::Rc;
use std::{fs, path::Path, rc::Rc};

#[derive(Serialize, Deserialize, Debug)]
pub struct SaveData {
Expand Down Expand Up @@ -46,8 +44,7 @@ impl Executor {
path: path.clone(),
source: e,
})?;
let image =
Image::load_from_path(Path::new(&image_path)).unwrap_or(Image::default());
let image = Image::load_from_path(Path::new(&image_path)).unwrap_or_default();
load_items.push((
image,
explain.to_shared_string(),
Expand Down
9 changes: 6 additions & 3 deletions src/config/system.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::user::USER_CONFIG;
use crate::executor::executor::Executor;
use crate::ui::ui::MainWindow;
use crate::executors::executor::Executor;
use crate::ui::initialize::MainWindow;
use serde::{Deserialize, Serialize};
use slint::Weak;

Expand All @@ -12,7 +12,10 @@ pub struct AutoConfig {

impl Default for AutoConfig {
fn default() -> Self {
AutoConfig { delay: 3.5, is_wait: true }
AutoConfig {
delay: 3.5,
is_wait: true,
}
}
}

Expand Down
9 changes: 6 additions & 3 deletions src/config/text.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::user::USER_CONFIG;
use crate::executor::executor::Executor;
use crate::ui::ui::MainWindow;
use crate::executors::executor::Executor;
use crate::ui::initialize::MainWindow;
use serde::{Deserialize, Serialize};
use slint::Weak;

Expand All @@ -12,7 +12,10 @@ pub struct TextConfig {

impl Default for TextConfig {
fn default() -> Self {
TextConfig { speed: 50.0, opacity: 0.8 }
TextConfig {
speed: 50.0,
opacity: 0.8,
}
}
}
impl TextConfig {
Expand Down
15 changes: 7 additions & 8 deletions src/config/user.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use crate::config::system::AutoConfig;
use crate::config::text::TextConfig;
use crate::config::volume::VolumeConfig;
use crate::config::ENGINE_CONFIG;
use crate::config::{
character_volume::CharacterVolumeConfig, system::AutoConfig, text::TextConfig,
volume::VolumeConfig, ENGINE_CONFIG,
};
use crate::error::{EngineError, SaveError};
use crate::ui::ui::MainWindow;
use crate::ui::initialize::MainWindow;
use serde::{Deserialize, Serialize};
use slint::Weak;
use std::fs;
use crate::config::character_volume::CharacterVolumeConfig;

lazy_static::lazy_static! {
pub static ref USER_CONFIG: UserConfig = load_user_config();
Expand Down Expand Up @@ -60,7 +59,7 @@ impl UserConfig {
}

pub fn character_volume(&self, name: &str) -> f32 {
self.character_volume.volumes.get(name).unwrap().clone()
*self.character_volume.volumes.get(name).unwrap()
}

pub fn from_weak(weak: Weak<MainWindow>) -> Self {
Expand Down Expand Up @@ -99,7 +98,7 @@ fn load_user_config() -> UserConfig {
pub fn save_user_config(weak: Weak<MainWindow>) -> Result<(), EngineError> {
let path = format!("{}/user.toml", ENGINE_CONFIG.save_path());
write_config(&path, &UserConfig::from_weak(weak))?;

Ok(())
}

Expand Down
12 changes: 2 additions & 10 deletions src/config/voice.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::config::ENGINE_CONFIG;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::collections::HashMap;
use std::fs;
use serde::{Deserialize, Deserializer, Serialize};
use std::{collections::HashMap, fs};
use tokio::time::Duration;

lazy_static::lazy_static! {
Expand Down Expand Up @@ -60,10 +59,3 @@ where
let seconds = u64::deserialize(deserializer)?;
Ok(Duration::from_secs(seconds))
}

pub fn serialize_duration_as_secs<S>(duration: &Duration, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u64(duration.as_secs())
}
10 changes: 7 additions & 3 deletions src/config/volume.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::config::user::USER_CONFIG;
use crate::executor::executor::Executor;
use crate::ui::ui::MainWindow;
use crate::executors::executor::Executor;
use crate::ui::initialize::MainWindow;
use serde::{Deserialize, Serialize};
use slint::Weak;

Expand All @@ -13,7 +13,11 @@ pub(crate) struct VolumeConfig {

impl Default for VolumeConfig {
fn default() -> Self {
VolumeConfig { main: 100.0, bgm: 100.0, voice: 100.0 }
VolumeConfig {
main: 100.0,
bgm: 100.0,
voice: 100.0,
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub enum EngineError {
#[error("script error: {0}")]
Script(#[from] ScriptError),

#[error("executor error: {0}")]
#[error("executors error: {0}")]
Executor(#[from] ExecutorError),

#[error("save error: {0}")]
Expand Down Expand Up @@ -68,7 +68,7 @@ pub enum ExecutorError {
CgMetadataMissing(u64),

#[allow(dead_code)]
#[error("invalid executor state: {0}")]
#[error("invalid executors state: {0}")]
InvalidState(&'static str),
}

Expand Down
Loading