From 560daa08ac6ba1a10080696a30d44368aa5fea9c Mon Sep 17 00:00:00 2001 From: xeon Date: Fri, 24 May 2024 18:34:35 +0300 Subject: [PATCH] Rework asset system (TemplateCollections) --- .../MainCityObjectTemplateCollection.json | 0 gameserver/src/config/mod.rs | 51 +++++++++++++++---- gameserver/src/config/templates.rs | 5 +- gameserver/src/game/context.rs | 3 +- gameserver/src/game/manager/dungeon.rs | 1 + gameserver/src/main.rs | 2 + gameserver/src/net/handlers/hollow.rs | 4 +- gameserver/src/net/packet.rs | 1 + 8 files changed, 52 insertions(+), 15 deletions(-) rename {gameserver => assets}/TemplateCollections/MainCityObjectTemplateCollection.json (100%) diff --git a/gameserver/TemplateCollections/MainCityObjectTemplateCollection.json b/assets/TemplateCollections/MainCityObjectTemplateCollection.json similarity index 100% rename from gameserver/TemplateCollections/MainCityObjectTemplateCollection.json rename to assets/TemplateCollections/MainCityObjectTemplateCollection.json diff --git a/gameserver/src/config/mod.rs b/gameserver/src/config/mod.rs index 91bfcf6..3db0b0e 100644 --- a/gameserver/src/config/mod.rs +++ b/gameserver/src/config/mod.rs @@ -1,18 +1,51 @@ mod templates; -use lazy_static::lazy_static; +use std::path::Path; + +use anyhow::{bail, Result}; +use paste::paste; pub use templates::*; +use tokio::sync::OnceCell; -const MAIN_CITY_OBJECT_COLLECTION_JSON: &str = - include_str!("../../TemplateCollections/MainCityObjectTemplateCollection.json"); +macro_rules! template_collections { + ($($template_type:ident;)*) => { + $(paste! { + static [<$template_type:snake:upper _COLLECTION>]: OnceCell]>> = OnceCell::const_new(); + })* -lazy_static! { - static ref MAIN_CITY_OBJECT_COLLECTION: Vec = - serde_json::from_str(MAIN_CITY_OBJECT_COLLECTION_JSON).unwrap(); + fn init_template_collections() -> Result<()> { + $(paste! { + 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 []() -> ::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> { - MAIN_CITY_OBJECT_COLLECTION - .iter() - .find(|object| object.tag_id == tag_id && object.npc_id == npc_id) + iter_main_city_object_collection().find(|o| o.tag_id == tag_id && o.npc_id == npc_id) } diff --git a/gameserver/src/config/templates.rs b/gameserver/src/config/templates.rs index 7007abb..abc569a 100644 --- a/gameserver/src/config/templates.rs +++ b/gameserver/src/config/templates.rs @@ -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")] pub struct MainCityObjectTemplate { #[serde(rename = "TagID")] diff --git a/gameserver/src/game/context.rs b/gameserver/src/game/context.rs index fdea08d..a386b8e 100644 --- a/gameserver/src/game/context.rs +++ b/gameserver/src/game/context.rs @@ -12,7 +12,7 @@ use super::manager::{ }; pub struct GameContext { - pub player: Arc>, + #[allow(unused)] pub uid_manager: Arc>, pub item_manager: Arc>, pub dungeon_manager: Arc>, @@ -28,7 +28,6 @@ impl GameContext { let uid_manager = Arc::new(AtomicRefCell::new(UniqueIDManager::new())); Self { - player: player.clone(), uid_manager: uid_manager.clone(), item_manager: Arc::new(AtomicRefCell::new(ItemManager::new( uid_manager.clone(), diff --git a/gameserver/src/game/manager/dungeon.rs b/gameserver/src/game/manager/dungeon.rs index b14570c..cd52f62 100644 --- a/gameserver/src/game/manager/dungeon.rs +++ b/gameserver/src/game/manager/dungeon.rs @@ -494,6 +494,7 @@ impl DungeonManager { ) } + #[allow(dead_code)] pub fn is_in_tutorial(&self) -> bool { let cur_scene_uid = self.get_cur_scene_uid(); diff --git a/gameserver/src/main.rs b/gameserver/src/main.rs index d22ebf3..6c569ef 100644 --- a/gameserver/src/main.rs +++ b/gameserver/src/main.rs @@ -1,6 +1,7 @@ use std::path::Path; use anyhow::Result; +use config::init_assets; use tracing::Level; mod config; @@ -19,6 +20,7 @@ async fn main() -> Result<()> { ansi_term::enable_ansi_support().unwrap(); init_config()?; + init_assets()?; init_tracing(); let span = tracing::span!(Level::DEBUG, "main"); diff --git a/gameserver/src/net/handlers/hollow.rs b/gameserver/src/net/handlers/hollow.rs index 05ae51d..50183ba 100644 --- a/gameserver/src/net/handlers/hollow.rs +++ b/gameserver/src/net/handlers/hollow.rs @@ -49,7 +49,7 @@ pub async fn on_rpc_end_battle_arg(session: &NetworkSession, arg: &RpcEndBattleA session.send_rpc_arg(210, &sync_event).await?; } else { let dungeon_manager = session.context.dungeon_manager.borrow(); - let cur_scene = *dungeon_manager + let _ = *dungeon_manager .hollow_finished() .send_changes(session) .await?; @@ -154,7 +154,7 @@ pub async fn on_rpc_run_hollow_event_graph_arg( if hollow_finished { let dungeon_manager = session.context.dungeon_manager.borrow(); - let cur_scene = *dungeon_manager + let _ = *dungeon_manager .hollow_finished() .send_changes(session) .await?; diff --git a/gameserver/src/net/packet.rs b/gameserver/src/net/packet.rs index ceb93d2..bdb43e8 100644 --- a/gameserver/src/net/packet.rs +++ b/gameserver/src/net/packet.rs @@ -11,6 +11,7 @@ use super::handlers::*; use super::NetworkSession; pub struct Packet { + #[allow(unused)] pub to_channel: u16, pub header: ProtocolHeader, pub body: Vec,