summaryrefslogtreecommitdiff
path: root/server/src
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-06-26 23:06:23 +0200
committermetamuffin <metamuffin@disroot.org>2024-06-26 23:06:23 +0200
commit211e3d619e82aeb73dc6c52868991384682869f5 (patch)
tree375a6b72658cf2dbce0590b165360563cc6e6f98 /server/src
parente38a2ee78910edcec4425c07903e36fb5749ae3b (diff)
downloadhurrycurry-211e3d619e82aeb73dc6c52868991384682869f5.tar
hurrycurry-211e3d619e82aeb73dc6c52868991384682869f5.tar.bz2
hurrycurry-211e3d619e82aeb73dc6c52868991384682869f5.tar.zst
server auto-detect data dir
Diffstat (limited to 'server/src')
-rw-r--r--server/src/data.rs21
-rw-r--r--server/src/main.rs37
2 files changed, 47 insertions, 11 deletions
diff --git a/server/src/data.rs b/server/src/data.rs
index e980ccbd..e2944e3d 100644
--- a/server/src/data.rs
+++ b/server/src/data.rs
@@ -25,7 +25,9 @@ use serde::{Deserialize, Serialize};
use std::{
collections::{HashMap, HashSet},
fs::File,
- sync::RwLock,
+ path::PathBuf,
+ str::FromStr,
+ sync::{Mutex, RwLock},
};
#[derive(Debug, Deserialize, Serialize, Clone, Copy, Default)]
@@ -104,9 +106,18 @@ pub struct DataIndex {
pub recipes: HashSet<String>,
}
+pub static DATA_DIR: Mutex<Option<PathBuf>> = Mutex::new(None);
+fn data_dir() -> PathBuf {
+ DATA_DIR
+ .lock()
+ .unwrap()
+ .to_owned()
+ .unwrap_or_else(|| PathBuf::from_str("data").unwrap())
+}
+
impl DataIndex {
pub fn reload(&mut self) -> anyhow::Result<()> {
- *self = serde_yaml::from_reader(File::open("data/index.yaml")?)?;
+ *self = serde_yaml::from_reader(File::open(data_dir().join("index.yaml"))?)?;
Ok(())
}
@@ -127,9 +138,9 @@ impl DataIndex {
bail!("unknown recipes: {recipes:?}");
}
- let demands_path = format!("data/demands/{demands}.yaml");
- let map_path = format!("data/maps/{map}.yaml");
- let recipes_path = format!("data/recipes/{recipes}.yaml");
+ let demands_path = data_dir().join(format!("demands/{demands}.yaml"));
+ let map_path = data_dir().join(format!("maps/{map}.yaml"));
+ let recipes_path = data_dir().join(format!("recipes/{recipes}.yaml"));
let demands_in = serde_yaml::from_reader(File::open(demands_path).unwrap()).unwrap();
let map_in = serde_yaml::from_reader(File::open(map_path).unwrap()).unwrap();
diff --git a/server/src/main.rs b/server/src/main.rs
index 7c072319..db667779 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -15,10 +15,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use anyhow::Result;
+use anyhow::{anyhow, Result};
use futures_util::{SinkExt, StreamExt};
-use log::{debug, info, warn};
-use std::{env::args, process::exit, sync::Arc, time::Duration};
+use log::{debug, info, warn, LevelFilter};
+use std::{env::args, path::PathBuf, process::exit, str::FromStr, sync::Arc, time::Duration};
use tokio::{
net::TcpListener,
spawn,
@@ -27,13 +27,16 @@ use tokio::{
};
use tokio_tungstenite::tungstenite::Message;
use undercooked::{
+ data::DATA_DIR,
protocol::{PacketC, PacketS, PlayerID},
state::State,
};
-#[tokio::main]
-async fn main() -> Result<()> {
- env_logger::init_from_env("LOG");
+fn main() -> Result<()> {
+ env_logger::builder()
+ .filter_level(LevelFilter::Info)
+ .parse_env("LOG")
+ .init();
if let Some(arg) = args().nth(1) {
match arg.as_str() {
@@ -43,6 +46,28 @@ async fn main() -> Result<()> {
exit(0);
}
+ let data_dir = PathBuf::from_str(
+ [
+ "/usr/local/share/undercooked/data",
+ "/usr/share/undercooked/data",
+ "./data",
+ ]
+ .into_iter()
+ .find(|p| PathBuf::from_str(p).unwrap().join("index.yaml").exists())
+ .ok_or(anyhow!("no data dir detected"))?,
+ )
+ .unwrap();
+ info!("Detected data dir to be {data_dir:?}");
+ *DATA_DIR.lock().unwrap() = Some(data_dir);
+
+ tokio::runtime::Builder::new_multi_thread()
+ .enable_all()
+ .build()?
+ .block_on(run())?;
+ Ok(())
+}
+
+async fn run() -> anyhow::Result<()> {
let ws_listener = TcpListener::bind("0.0.0.0:27032").await?;
info!("listening for websockets on {}", ws_listener.local_addr()?);