Rework asset system (TemplateCollections)

This commit is contained in:
xeon 2024-05-24 18:34:35 +03:00
parent d76277add3
commit 560daa08ac
8 changed files with 52 additions and 15 deletions

View file

@ -1,18 +1,51 @@
mod templates; mod templates;
use lazy_static::lazy_static; use std::path::Path;
use anyhow::{bail, Result};
use paste::paste;
pub use templates::*; pub use templates::*;
use tokio::sync::OnceCell;
const MAIN_CITY_OBJECT_COLLECTION_JSON: &str = macro_rules! template_collections {
include_str!("../../TemplateCollections/MainCityObjectTemplateCollection.json"); ($($template_type:ident;)*) => {
$(paste! {
static [<$template_type:snake:upper _COLLECTION>]: OnceCell<Vec<[<$template_type Template>]>> = OnceCell::const_new();
})*
lazy_static! { fn init_template_collections() -> Result<()> {
static ref MAIN_CITY_OBJECT_COLLECTION: Vec<MainCityObjectTemplate> = $(paste! {
serde_json::from_str(MAIN_CITY_OBJECT_COLLECTION_JSON).unwrap(); let path = concat!("assets/TemplateCollections/", stringify!($template_type), "TemplateCollection.json");
let data = std::fs::read_to_string(path)?;
[<$template_type:snake:upper _COLLECTION>].set(serde_json::from_str(&data)?).unwrap();
})*
Ok(())
}
$(paste! {
pub fn [<iter_ $template_type:snake _collection>]() -> ::std::slice::Iter<'static, [<$template_type Template>]> {
[<$template_type:snake:upper _COLLECTION>].get().unwrap().iter()
}
})*
};
}
template_collections! {
MainCityObject;
}
pub fn init_assets() -> Result<()> {
if !Path::new("assets/").exists() {
bail!(
"Assets directory not found! Make sure you have it in the same directory with executable."
)
}
init_template_collections()?;
Ok(())
} }
pub fn get_main_city_object(tag_id: i32, npc_id: i32) -> Option<&'static MainCityObjectTemplate> { pub fn get_main_city_object(tag_id: i32, npc_id: i32) -> Option<&'static MainCityObjectTemplate> {
MAIN_CITY_OBJECT_COLLECTION iter_main_city_object_collection().find(|o| o.tag_id == tag_id && o.npc_id == npc_id)
.iter()
.find(|object| object.tag_id == tag_id && object.npc_id == npc_id)
} }

View file

@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize}; #![allow(dead_code)]
use serde::Deserialize;
#[derive(Serialize, Deserialize, Debug)] #[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")] #[serde(rename_all = "PascalCase")]
pub struct MainCityObjectTemplate { pub struct MainCityObjectTemplate {
#[serde(rename = "TagID")] #[serde(rename = "TagID")]

View file

@ -12,7 +12,7 @@ use super::manager::{
}; };
pub struct GameContext { pub struct GameContext {
pub player: Arc<AtomicRefCell<PlayerInfo>>, #[allow(unused)]
pub uid_manager: Arc<AtomicRefCell<UniqueIDManager>>, pub uid_manager: Arc<AtomicRefCell<UniqueIDManager>>,
pub item_manager: Arc<AtomicRefCell<ItemManager>>, pub item_manager: Arc<AtomicRefCell<ItemManager>>,
pub dungeon_manager: Arc<AtomicRefCell<DungeonManager>>, pub dungeon_manager: Arc<AtomicRefCell<DungeonManager>>,
@ -28,7 +28,6 @@ impl GameContext {
let uid_manager = Arc::new(AtomicRefCell::new(UniqueIDManager::new())); let uid_manager = Arc::new(AtomicRefCell::new(UniqueIDManager::new()));
Self { Self {
player: player.clone(),
uid_manager: uid_manager.clone(), uid_manager: uid_manager.clone(),
item_manager: Arc::new(AtomicRefCell::new(ItemManager::new( item_manager: Arc::new(AtomicRefCell::new(ItemManager::new(
uid_manager.clone(), uid_manager.clone(),

View file

@ -494,6 +494,7 @@ impl DungeonManager {
) )
} }
#[allow(dead_code)]
pub fn is_in_tutorial(&self) -> bool { pub fn is_in_tutorial(&self) -> bool {
let cur_scene_uid = self.get_cur_scene_uid(); let cur_scene_uid = self.get_cur_scene_uid();

View file

@ -1,6 +1,7 @@
use std::path::Path; use std::path::Path;
use anyhow::Result; use anyhow::Result;
use config::init_assets;
use tracing::Level; use tracing::Level;
mod config; mod config;
@ -19,6 +20,7 @@ async fn main() -> Result<()> {
ansi_term::enable_ansi_support().unwrap(); ansi_term::enable_ansi_support().unwrap();
init_config()?; init_config()?;
init_assets()?;
init_tracing(); init_tracing();
let span = tracing::span!(Level::DEBUG, "main"); let span = tracing::span!(Level::DEBUG, "main");

View file

@ -49,7 +49,7 @@ pub async fn on_rpc_end_battle_arg(session: &NetworkSession, arg: &RpcEndBattleA
session.send_rpc_arg(210, &sync_event).await?; session.send_rpc_arg(210, &sync_event).await?;
} else { } else {
let dungeon_manager = session.context.dungeon_manager.borrow(); let dungeon_manager = session.context.dungeon_manager.borrow();
let cur_scene = *dungeon_manager let _ = *dungeon_manager
.hollow_finished() .hollow_finished()
.send_changes(session) .send_changes(session)
.await?; .await?;
@ -154,7 +154,7 @@ pub async fn on_rpc_run_hollow_event_graph_arg(
if hollow_finished { if hollow_finished {
let dungeon_manager = session.context.dungeon_manager.borrow(); let dungeon_manager = session.context.dungeon_manager.borrow();
let cur_scene = *dungeon_manager let _ = *dungeon_manager
.hollow_finished() .hollow_finished()
.send_changes(session) .send_changes(session)
.await?; .await?;

View file

@ -11,6 +11,7 @@ use super::handlers::*;
use super::NetworkSession; use super::NetworkSession;
pub struct Packet { pub struct Packet {
#[allow(unused)]
pub to_channel: u16, pub to_channel: u16,
pub header: ProtocolHeader, pub header: ProtocolHeader,
pub body: Vec<u8>, pub body: Vec<u8>,