@@ -24,22 +24,12 @@ const PAGE_KEY: &str = "page";
24
24
struct ItemStore {
25
25
path : PathBuf ,
26
26
items : HashMap < String , String > ,
27
- modified : bool ,
28
27
}
29
28
30
29
impl ItemStore {
31
30
fn persist ( & mut self ) {
32
- if self . modified {
33
- let file = ok ! ( File :: create( & self . path) ) ;
34
- ron:: ser:: to_writer_pretty ( file, & self . items , Default :: default ( ) ) . wrest ( ) ;
35
- self . modified = false ;
36
- }
37
- }
38
- }
39
-
40
- impl Drop for ItemStore {
41
- fn drop ( & mut self ) {
42
- self . persist ( ) ;
31
+ let file = ok ! ( File :: create( & self . path) ) ;
32
+ ron:: ser:: to_writer_pretty ( file, & self . items , Default :: default ( ) ) . wrest ( ) ;
43
33
}
44
34
}
45
35
@@ -52,12 +42,7 @@ impl Config {
52
42
pub fn new ( ) -> Option < Self > {
53
43
let path = Self :: path ( ) ?;
54
44
let items = Self :: load ( & path) ;
55
- let store = ItemStore {
56
- path,
57
- items,
58
- modified : false ,
59
- } ;
60
-
45
+ let store = ItemStore { path, items } ;
61
46
Some ( Self {
62
47
store : Arc :: new ( RwLock :: new ( store) ) ,
63
48
} )
@@ -85,20 +70,19 @@ impl Config {
85
70
pub fn set_page ( & mut self , page : Page ) {
86
71
let text = ok ! ( ron:: to_string( & page) ) ;
87
72
self . set ( PAGE_KEY , text) ;
88
- self . persist ( ) ;
89
73
}
90
74
91
75
pub fn get_log_path ( & self ) -> Option < PathBuf > {
92
76
if let Some ( folder) = self . get ( LOG_PATH_KEY ) {
93
77
return Some ( PathBuf :: from ( folder) ) ;
94
78
}
79
+
95
80
Self :: get_default_log_path ( )
96
81
}
97
82
98
83
pub fn set_log_path ( & mut self , folder : & Path ) {
99
84
if let Some ( folder) = folder. to_str ( ) {
100
85
self . set ( LOG_PATH_KEY , folder. to_owned ( ) ) ;
101
- self . persist ( ) ;
102
86
} else {
103
87
println ! ( "Unable to convert path to string: {folder:?}" ) ;
104
88
}
@@ -108,13 +92,13 @@ impl Config {
108
92
if let Some ( folder) = self . get ( SAVE_PATH_KEY ) {
109
93
return Some ( PathBuf :: from ( folder) ) ;
110
94
}
95
+
111
96
Self :: get_default_save_path ( )
112
97
}
113
98
114
99
pub fn set_save_path ( & mut self , folder : & Path ) {
115
100
if let Some ( folder) = folder. to_str ( ) {
116
101
self . set ( SAVE_PATH_KEY , folder. to_owned ( ) ) ;
117
- self . persist ( ) ;
118
102
} else {
119
103
println ! ( "Unable to convert path to string: {folder:?}" ) ;
120
104
}
@@ -130,7 +114,6 @@ impl Config {
130
114
}
131
115
132
116
self . set ( STATS_AVATAR_KEY , avatar) ;
133
- self . persist ( ) ;
134
117
}
135
118
136
119
pub fn get_exp_avatar ( & self ) -> Option < String > {
@@ -143,7 +126,6 @@ impl Config {
143
126
}
144
127
145
128
self . set ( EXP_AVATAR_KEY , avatar) ;
146
- self . persist ( ) ;
147
129
}
148
130
149
131
pub fn get_notes ( & self , avatar : & str ) -> Option < String > {
@@ -168,7 +150,6 @@ impl Config {
168
150
}
169
151
170
152
self . set ( & key, notes) ;
171
- self . persist ( ) ;
172
153
}
173
154
174
155
pub fn get_plants ( & self ) -> Option < Vec < Plant > > {
@@ -181,13 +162,11 @@ impl Config {
181
162
// Remove the entry if plants is empty.
182
163
if plants. is_empty ( ) {
183
164
self . remove ( PLANTS_KEY ) ;
184
- self . persist ( ) ;
185
165
return ;
186
166
}
187
167
188
168
let text = ok ! ( ron:: to_string( plants) ) ;
189
169
self . set ( PLANTS_KEY , text) ;
190
- self . persist ( ) ;
191
170
}
192
171
193
172
pub fn get_crop_descriptions ( & self ) -> Option < BTreeSet < String > > {
@@ -200,13 +179,11 @@ impl Config {
200
179
// Remove the entry if the set is empty.
201
180
if descriptions. is_empty ( ) {
202
181
self . remove ( DESCRIPTIONS_KEY ) ;
203
- self . persist ( ) ;
204
182
return ;
205
183
}
206
184
207
185
let text = ok ! ( ron:: to_string( descriptions) ) ;
208
186
self . set ( DESCRIPTIONS_KEY , text) ;
209
- self . persist ( ) ;
210
187
}
211
188
212
189
pub fn get_avatar_skills ( & self , avatar : & str ) -> Option < HashMap < u32 , ( i32 , i32 ) > > {
@@ -236,13 +213,11 @@ impl Config {
236
213
let key = format ! ( "{avatar} {AVATAR_SKILLS}" ) ;
237
214
if skills. is_empty ( ) {
238
215
self . remove ( & key) ;
239
- self . persist ( ) ;
240
216
return ;
241
217
}
242
218
243
219
let text = ok ! ( ron:: to_string( & skills) ) ;
244
220
self . set ( & key, text) ;
245
- self . persist ( ) ;
246
221
}
247
222
248
223
fn path ( ) -> Option < PathBuf > {
@@ -262,20 +237,16 @@ impl Config {
262
237
fn set ( & mut self , key : & str , item : String ) {
263
238
let mut lock = self . store . write ( ) . wrest ( ) ;
264
239
lock. items . insert ( key. to_owned ( ) , item) ;
265
- lock. modified = true ;
240
+ lock. persist ( ) ;
266
241
}
267
242
268
243
fn remove ( & mut self , key : & str ) {
269
244
let mut lock = self . store . write ( ) . wrest ( ) ;
270
245
if lock. items . remove ( key) . is_some ( ) {
271
- lock. modified = true ;
246
+ lock. persist ( ) ;
272
247
}
273
248
}
274
249
275
- fn persist ( & mut self ) {
276
- self . store . write ( ) . wrest ( ) . persist ( ) ;
277
- }
278
-
279
250
fn get_sota_config_path ( ) -> Option < PathBuf > {
280
251
let path = dirs:: config_dir ( ) ?;
281
252
Some ( path. join ( "Portalarium" ) . join ( "Shroud of the Avatar" ) )
0 commit comments