owmods_core/
lib.rs

1#![doc = include_str!("../README.md")]
2#![doc(
3    html_logo_url = "https://github.com/ow-mods/ow-mod-man/blob/main/.github/assets/logo-core.png?raw=true"
4)]
5#![warn(missing_docs)]
6
7/// Fetch database alerts and get mod warnings.
8pub mod alerts;
9
10/// Send analytics events.
11pub mod analytics;
12
13/// Work with the configuration of the app.
14pub mod config;
15
16/// Useful constants
17pub mod constants;
18
19/// Work with both remote and local databases.
20pub mod db;
21
22/// Download and install mods and OWML.
23pub mod download;
24
25/// Utilities when working with files.
26pub mod file;
27
28/// Run the game and setup prerequisites on Linux.
29pub mod game;
30
31/// Import and export mods from JSON arrays.
32pub mod io;
33
34/// Work with local and remote mods.
35pub mod mods;
36
37/// Work with the OWML config.
38pub mod owml;
39
40/// Open shortcuts and mod readmes.
41pub mod open;
42
43/// Utilities for managing and parsing progress bars.
44pub mod progress;
45
46/// Uninstall mods
47pub mod remove;
48
49/// Listen to logs from the game.
50pub mod socket;
51
52/// Enable/Disable mods.
53pub mod toggle;
54
55/// Check for and update mods.
56pub mod updates;
57
58/// Validate the local database for common issues
59pub mod validate;
60
61/// Generalized searching
62pub mod search;
63
64/// Utility for parsing the owmods:// protocol.
65pub mod protocol;
66
67/// Utilities for testing the library.
68#[cfg(test)]
69pub(crate) mod test_utils {
70    use std::path::{Path, PathBuf};
71
72    use tempfile::TempDir;
73
74    use crate::{
75        config::Config,
76        db::{LocalDatabase, RemoteDatabase},
77        download::install_mod_from_zip,
78        mods::local::{LocalMod, UnsafeLocalMod},
79    };
80
81    /// A test context for testing the library.
82    pub struct TestContext {
83        /// The temporary directory
84        pub temp_dir: TempDir,
85        /// The OWML directory
86        pub owml_dir: PathBuf,
87        /// The config
88        pub config: Config,
89        /// The local database
90        pub local_db: LocalDatabase,
91        /// The remote database
92        pub remote_db: RemoteDatabase,
93    }
94
95    /// Create a temporary directory for testing.
96    pub fn make_test_dir() -> TempDir {
97        TempDir::new().unwrap()
98    }
99
100    /// Get a test file from the test_files directory.
101    pub fn get_test_file(path: &str) -> PathBuf {
102        Path::new(env!("CARGO_MANIFEST_DIR"))
103            .join("test_files")
104            .join(path)
105    }
106
107    impl TestContext {
108        /// Create a new test context. This creates:
109        /// - A temporary directory
110        /// - An OWML directory inside the temporary directory
111        /// - A config with the OWML directory as the OWML path, and the settings.json file inside the temporary directory
112        /// - A local database pointed to the OWML directory
113        /// - A remote database pointed to the default database URL
114        pub fn new() -> Self {
115            let temp_dir = make_test_dir();
116            let owml_dir = temp_dir.path().join("OWML");
117            let mut config = Config::default(Some(temp_dir.path().join("settings.json"))).unwrap();
118            config.owml_path = owml_dir.to_str().unwrap().to_string();
119            let local_db = LocalDatabase::default();
120            let remote_db = RemoteDatabase::default();
121            Self {
122                temp_dir,
123                owml_dir,
124                config,
125                local_db,
126                remote_db,
127            }
128        }
129
130        /// Join the temporary mods folder to a path
131        pub fn join_mods_folder(&self, path: &str) -> PathBuf {
132            self.owml_dir.join("Mods").join(path)
133        }
134
135        /// Get the path to a test mod, if the mod isn't in the database the unique name is simply joined to the mods folder
136        pub fn get_test_path(&self, unique_name: &str) -> PathBuf {
137            if let Some(local_mod) = self.local_db.get_mod(unique_name) {
138                PathBuf::from(&local_mod.mod_path)
139            } else {
140                self.owml_dir.join("Mods").join(unique_name)
141            }
142        }
143
144        /// Refresh the local database
145        pub fn fetch_local_db(&mut self) {
146            self.local_db = LocalDatabase::fetch(&self.config.owml_path).unwrap();
147        }
148
149        /// Refresh the remote database
150        pub async fn fetch_remote_db(&mut self) {
151            self.remote_db = RemoteDatabase::fetch(&self.config.database_url)
152                .await
153                .unwrap();
154        }
155
156        /// Insert a test mod into the local database
157        pub fn insert_test_mod(&mut self, local_mod: &LocalMod) {
158            self.local_db.mods.insert(
159                local_mod.manifest.unique_name.clone(),
160                UnsafeLocalMod::Valid(Box::new(local_mod.clone())),
161            );
162        }
163
164        /// Install a test mod from a zip file in test_files
165        pub fn install_test_zip(&mut self, zip_name: &str, refresh: bool) -> LocalMod {
166            let zip_path = get_test_file(zip_name);
167            let local_mod = install_mod_from_zip(&zip_path, &self.config, &self.local_db).unwrap();
168            if refresh {
169                self.fetch_local_db();
170            }
171            local_mod
172        }
173    }
174}