Skip to content

Commit 4f1f6c6

Browse files
committed
Merge branch 'dev'
2 parents 5f06993 + 81849f2 commit 4f1f6c6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+11030
-4823
lines changed

pnpm-lock.yaml

Lines changed: 1440 additions & 1173 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src-tauri/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ tauri-build = { version = "1", features = [] }
1212

1313
[dependencies]
1414
anyhow = "1.0.86"
15+
thiserror = "1.0.63"
1516
showfile = "0.1.1"
1617
tauri = { version = "1", features = ["dialog-all", "shell-open"] }
1718
serde = { version = "1", features = ["derive"] }
1819
serde_json = "1"
1920
flate2 = "1.0.30"
2021
tar = "0.4.41"
2122
dirs-next = "2.0.0"
22-
filesize = "0.2.0"
23+
filesize = "0.2.0" # or "postgres", or "mysql"
2324

2425
[features]
2526
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!

src-tauri/src/app.rs

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
use std::{path::PathBuf, sync::Mutex};
2+
3+
use crate::{cache, editor, errors, prefs, project};
4+
5+
pub struct AppState {
6+
pub prefs: Mutex<prefs::Prefs>,
7+
pub user_cache: Mutex<cache::UserCache>,
8+
pub projects: Mutex<Vec<project::Project>>,
9+
pub editors: Mutex<Vec<editor::UnityEditorInstall>>,
10+
}
11+
12+
pub fn get_save_path(name: &str, app_handle: &tauri::AppHandle) -> anyhow::Result<PathBuf> {
13+
let config = app_handle.config();
14+
let config_dir = tauri::api::path::app_config_dir(&config)
15+
.expect("failed to get app config dir");
16+
17+
std::fs::create_dir_all(&config_dir)?;
18+
19+
let path = config_dir.join(name).with_extension("json");
20+
Ok(path)
21+
}
22+
23+
pub fn load_from_disk<T>(path: impl Into<PathBuf>) -> anyhow::Result<T>
24+
where T: Default + serde::Serialize + serde::de::DeserializeOwned {
25+
let path = path.into();
26+
let json = std::fs::read_to_string(&path)
27+
.unwrap_or("{}".to_string());
28+
let (exists, field) = match serde_json::from_str(&json) {
29+
Ok(field) => (true, field),
30+
Err(_) => (false, T::default()),
31+
};
32+
33+
if !exists {
34+
save_to_disk::<T>(path, &field)?;
35+
}
36+
37+
Ok(field)
38+
}
39+
40+
pub fn save_new_to_disk<T>(path: impl Into<PathBuf>) -> anyhow::Result<T>
41+
where T: Default + serde::Serialize {
42+
let value = T::default();
43+
save_to_disk(path, &value)?;
44+
Ok(value)
45+
}
46+
47+
pub fn save_to_disk<T>(path: impl Into<PathBuf>, field: &T) -> anyhow::Result<()>
48+
where T: serde::Serialize {
49+
let path = path.into();
50+
let json = serde_json::to_string_pretty(&field)?;
51+
std::fs::write(path, json)?;
52+
Ok(())
53+
}
54+
55+
// prefs
56+
57+
pub fn get_prefs_save_path(app_handle: &tauri::AppHandle) -> anyhow::Result<PathBuf> {
58+
let path = get_save_path("prefs", app_handle)?;
59+
Ok(path)
60+
}
61+
62+
pub fn get_prefs(app_state: &tauri::State<AppState>) -> anyhow::Result<prefs::Prefs> {
63+
let prefs = app_state.prefs.lock()
64+
.map_err(|_| errors::str_error("Failed to get prefs. Is it locked?"))?;
65+
66+
Ok(prefs.clone())
67+
}
68+
69+
pub fn load_prefs_from_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<prefs::Prefs> {
70+
let path = get_prefs_save_path(app_handle)?;
71+
load_from_disk(&path)
72+
}
73+
74+
pub fn save_new_prefs_to_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<prefs::Prefs> {
75+
let path = get_prefs_save_path(app_handle)?;
76+
save_new_to_disk::<prefs::Prefs>(path)
77+
}
78+
79+
pub fn save_prefs_to_disk(prefs: &prefs::Prefs, app_handle: &tauri::AppHandle) -> anyhow::Result<()> {
80+
let path = get_prefs_save_path(app_handle)?;
81+
save_to_disk(path, prefs)?;
82+
Ok(())
83+
}
84+
85+
// user cache
86+
87+
pub fn get_user_cache_save_path(app_handle: &tauri::AppHandle) -> anyhow::Result<PathBuf> {
88+
let path = get_save_path("user_cache", app_handle)?;
89+
Ok(path)
90+
}
91+
92+
pub fn get_user_cache(app_state: &tauri::State<AppState>) -> anyhow::Result<cache::UserCache> {
93+
let user_cache = app_state.user_cache.lock()
94+
.map_err(|_| errors::str_error("Failed to get user_cache. Is it locked?"))?;
95+
96+
Ok(user_cache.clone())
97+
}
98+
99+
pub fn load_user_cache_from_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<cache::UserCache> {
100+
let path = get_user_cache_save_path(app_handle)?;
101+
load_from_disk(&path)
102+
}
103+
104+
pub fn save_new_user_cache_to_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<cache::UserCache> {
105+
let path = get_user_cache_save_path(app_handle)?;
106+
save_new_to_disk::<cache::UserCache>(path)
107+
}
108+
109+
pub fn save_user_cache_to_disk(user_cache: &cache::UserCache, app_handle: &tauri::AppHandle) -> anyhow::Result<()> {
110+
let path = get_user_cache_save_path(app_handle)?;
111+
save_to_disk(path, &user_cache)
112+
}
113+
114+
// projects
115+
116+
pub fn get_projects_save_path(app_handle: &tauri::AppHandle) -> anyhow::Result<PathBuf> {
117+
let path = get_save_path("projects", app_handle)?;
118+
Ok(path)
119+
}
120+
121+
pub fn get_projects(app_state: &tauri::State<AppState>) -> anyhow::Result<Vec<project::Project>> {
122+
let projects = app_state.projects.lock()
123+
.map_err(|_| errors::str_error("Failed to get projects. Is it locked?"))?;
124+
125+
Ok(projects.clone())
126+
}
127+
128+
pub fn load_projects_from_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<Vec<project::Project>> {
129+
let path = get_projects_save_path(app_handle)?;
130+
load_from_disk(&path)
131+
}
132+
133+
pub fn save_new_projects_to_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<Vec<project::Project>> {
134+
let path = get_projects_save_path(app_handle)?;
135+
save_new_to_disk::<Vec<project::Project>>(path)
136+
}
137+
138+
pub fn save_projects_to_disk(projects: &Vec<project::Project>, app_handle: &tauri::AppHandle) -> anyhow::Result<()> {
139+
let path = get_projects_save_path(app_handle)?;
140+
save_to_disk(path, &projects)
141+
}
142+
143+
// editors
144+
145+
// pub fn get_editors_save_path(app_handle: &tauri::AppHandle) -> anyhow::Result<PathBuf> {
146+
// let path = get_save_path("editors", app_handle)?;
147+
// Ok(path)
148+
// }
149+
150+
pub fn get_editors(app_state: &tauri::State<AppState>) -> anyhow::Result<Vec<editor::UnityEditorInstall>> {
151+
let editors = app_state.editors.lock()
152+
.map_err(|_| errors::str_error("Failed to get editors. Is it locked?"))?;
153+
154+
Ok(editors.clone())
155+
}
156+
157+
// pub fn load_editors_from_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<Vec<editor::UnityEditorInstall>> {
158+
// let path = get_editors_save_path(app_handle)?;
159+
// load_from_disk(&path)
160+
// }
161+
162+
// pub fn save_new_editors_to_disk(app_handle: &tauri::AppHandle) -> anyhow::Result<Vec<editor::UnityEditorInstall>> {
163+
// let path = get_projects_save_path(app_handle)?;
164+
// save_new_to_disk::<Vec<editor::UnityEditorInstall>>(path)
165+
// }
166+
167+
// pub fn save_editors_to_disk(editors: &Vec<editor::UnityEditorInstall>, app_handle: &tauri::AppHandle) -> anyhow::Result<()> {
168+
// let path = get_editors_save_path(app_handle)?;
169+
// save_to_disk(path, &editors)
170+
// }
171+
172+
// commands
173+
174+
#[tauri::command]
175+
pub fn cmd_show_path_in_file_manager(path: String) {
176+
showfile::show_path_in_file_manager(path);
177+
}
178+
179+
#[tauri::command]
180+
pub fn cmd_is_valid_path(path: String) -> bool {
181+
let path = std::path::Path::new(&path);
182+
path.exists()
183+
}
184+
185+
#[tauri::command]
186+
pub fn cmd_is_valid_dir(path: String) -> bool {
187+
let path = std::path::Path::new(&path);
188+
path.exists() && path.is_dir()
189+
}
190+
191+
#[tauri::command]
192+
pub fn cmd_is_valid_file(path: String) -> bool {
193+
let path = std::path::Path::new(&path);
194+
path.exists() && path.is_file()
195+
}

src-tauri/src/cache.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use crate::{app::{self, AppState}, errors};
2+
3+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
4+
pub enum UserCacheKey {
5+
LastEditorVersion
6+
}
7+
8+
#[derive(serde::Serialize, serde::Deserialize, Clone, Debug)]
9+
#[serde(rename_all = "camelCase")]
10+
pub struct UserCache {
11+
pub last_editor_version: Option<String>
12+
}
13+
14+
impl Default for UserCache {
15+
fn default() -> Self {
16+
Self {
17+
last_editor_version: None
18+
}
19+
}
20+
}
21+
22+
// commands
23+
24+
#[tauri::command]
25+
pub fn cmd_get_user_cache(app_state: tauri::State<AppState>) -> Result<UserCache, errors::AnyError> {
26+
let user_cache = app::get_user_cache(&app_state)?;
27+
Ok(user_cache.clone())
28+
}
29+
30+
#[tauri::command]
31+
pub fn cmd_save_user_cache(app_handle: tauri::AppHandle, app_state: tauri::State<AppState>) -> Result<(), errors::AnyError> {
32+
let user_cache = app::get_user_cache(&app_state)?;
33+
app::save_user_cache_to_disk(&user_cache, &app_handle)?;
34+
Ok(())
35+
}
36+
37+
#[tauri::command]
38+
pub fn cmd_set_user_cache_value(app_handle: tauri::AppHandle, app_state: tauri::State<AppState>, key: UserCacheKey, value: serde_json::Value) -> Result<(), errors::AnyError> {
39+
let mut user_cache = app_state.user_cache.lock()
40+
.map_err(|_| errors::str_error("Failed to get user_cache. Is it locked?"))?;
41+
42+
match key {
43+
UserCacheKey::LastEditorVersion => {
44+
user_cache.last_editor_version = serde_json::from_value(value).unwrap_or(None);
45+
}
46+
_ => return Err(errors::str_error("Invalid key")),
47+
}
48+
49+
app::save_user_cache_to_disk(&user_cache, &app_handle)?;
50+
Ok(())
51+
}

0 commit comments

Comments
 (0)