aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-06-27 21:10:41 +0200
committermetamuffin <metamuffin@disroot.org>2025-06-27 21:10:41 +0200
commite4a1afec7a999858efdf3eb256101b5400bacbb3 (patch)
tree2408164ee703f4563a05833bf9d70de082d4cfe5
parent636c3ae17e8bf5ebc088b7c6a4a237e354f96e8e (diff)
downloadhurrycurry-e4a1afec7a999858efdf3eb256101b5400bacbb3.tar
hurrycurry-e4a1afec7a999858efdf3eb256101b5400bacbb3.tar.bz2
hurrycurry-e4a1afec7a999858efdf3eb256101b5400bacbb3.tar.zst
read windows registry to detect data dir
-rw-r--r--Cargo.lock36
-rw-r--r--server/Cargo.toml3
-rw-r--r--server/src/main.rs53
3 files changed, 78 insertions, 14 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 5507ccda..c1e302c7 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1309,6 +1309,7 @@ dependencies = [
"shlex",
"tokio",
"tokio-tungstenite",
+ "windows-registry",
]
[[package]]
@@ -3839,6 +3840,41 @@ dependencies = [
]
[[package]]
+name = "windows-link"
+version = "0.1.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a"
+
+[[package]]
+name = "windows-registry"
+version = "0.5.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b8a9ed28765efc97bbc954883f4e6796c33a06546ebafacbabee9696967499e"
+dependencies = [
+ "windows-link",
+ "windows-result",
+ "windows-strings",
+]
+
+[[package]]
+name = "windows-result"
+version = "0.3.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6"
+dependencies = [
+ "windows-link",
+]
+
+[[package]]
+name = "windows-strings"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57"
+dependencies = [
+ "windows-link",
+]
+
+[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/server/Cargo.toml b/server/Cargo.toml
index 2bd47507..9834c86a 100644
--- a/server/Cargo.toml
+++ b/server/Cargo.toml
@@ -35,6 +35,9 @@ hurrycurry-protocol = { path = "protocol" }
hurrycurry-client-lib = { path = "client-lib" }
hurrycurry-bot = { path = "bot" }
+[target.'cfg(windows)'.dependencies]
+windows-registry = "0.5"
+
[features]
default = ["mdns", "register", "upnp"]
mdns = ["dep:mdns-sd", "dep:get_if_addrs"]
diff --git a/server/src/main.rs b/server/src/main.rs
index 528ee69b..7fbc044a 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -15,7 +15,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
-use anyhow::{anyhow, Result};
+use anyhow::{bail, Result};
use clap::Parser;
use futures_util::{SinkExt, StreamExt};
use hurrycurry_protocol::{PacketC, PacketS, BINCODE_CONFIG, VERSION};
@@ -100,21 +100,36 @@ fn main() -> Result<()> {
let data_dir = if let Some(d) = args.data_dir.clone() {
d
} else {
- let d = PathBuf::from_str(
- [
- option_env!("DATA_SEARCH_PATH").unwrap_or("data"),
- "/usr/local/share/hurrycurry/data",
- "/usr/share/hurrycurry/data",
- "/opt/hurrycurry/data",
- ]
- .into_iter()
+ let mut test_order = Vec::new();
+
+ #[cfg(debug_assertions)]
+ test_order.push("data".to_string());
+
+ #[cfg(not(windows))]
+ test_order.extend([
+ "/usr/local/share/hurrycurry/data".to_string(),
+ "/usr/share/hurrycurry/data".to_string(),
+ "/opt/hurrycurry/data".to_string(),
+ ]);
+
+ #[cfg(windows)]
+ match read_windows_reg_datadir() {
+ Ok(path) => test_order.push(path),
+ Err(e) => warn!("Cannot find read datadir from windows registry: {e}"),
+ };
+
+ let Some(d) = test_order
+ .iter()
.find(|p| PathBuf::from_str(p).unwrap().join("index.yaml").exists())
- .ok_or(anyhow!(
- "Could not find the data directory. Please run the server next to the `data` directory or specify a path to it via arguments."
- ))?,
- )?;
+ else {
+ warn!("The following paths were tested without success: {test_order:#?}",);
+ bail!(
+ "Could not find the data directory. Use the --data-dir option to specify a path."
+ );
+ };
+
info!("Detected data dir to be {d:?}");
- d
+ PathBuf::from_str(d)?
};
*DATA_DIR.lock().unwrap() = Some(data_dir);
@@ -379,3 +394,13 @@ mod test {
});
}
}
+
+#[cfg(windows)]
+fn read_windows_reg_datadir() -> Result<String> {
+ use anyhow::Context;
+ Ok(windows_registry::CURRENT_USER
+ .open("Software\\Hurry Curry!")
+ .context("HKCU\\Hurry Curry!")?
+ .get_string("datadir")
+ .context("datadir subkey")?)
+}