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
+ }
0 commit comments