@@ -20,6 +20,10 @@ open Aether
20
20
open Aether.Operators
21
21
22
22
module BookmarkUtils =
23
+ /// Returns a path to a folder with the same name as the scene file,
24
+ /// which lies inside the same folder as the scene file.
25
+ /// This path is used to store sequenced bookmarks as individual files
26
+ /// when saving the scene.
23
27
let basePathFromScenePath ( scenePath : string ) =
24
28
Path.combine [ Path.GetDirectoryName scenePath;
25
29
Path.GetFileNameWithoutExtension scenePath]
@@ -171,8 +175,12 @@ module BookmarkUtils =
171
175
let updatePath ( basePath : string ) ( bm : SequencedBookmark ) =
172
176
match bm with
173
177
| SequencedBookmark.LoadedBookmark loaded ->
174
- Log.line " Updating sequenced bookmark base path from %s to %s "
175
- ( string loaded.basePath) basePath
178
+ match loaded.basePath with
179
+ | Some oldBasePath ->
180
+ if not ( oldBasePath = basePath) then
181
+ Log.line " [BookmarkUtils] Updating sequenced bookmark base path from %s to %s "
182
+ ( string oldBasePath) basePath
183
+ | None -> ()
176
184
SequencedBookmark.LoadedBookmark { loaded with basePath = Some basePath}
177
185
| SequencedBookmarks.NotYetLoaded notLoaded ->
178
186
let newPath = Path.combine [ basePath; Path.GetFileName notLoaded.path]
@@ -189,13 +197,42 @@ module BookmarkUtils =
189
197
HashMap.map ( fun g bm -> updatePath basePath bm) m.bookmarks
190
198
{ m with bookmarks = bookmarks}
191
199
200
+ /// Checks the given folder sceneBasePath for
201
+ /// sequenced bookmark files, and deletes all files that do
202
+ /// not have a corresponding bookmark in bookmarks.
203
+ /// Should be used when saving a scene to clean up saved bookmarks
204
+ /// that were deleted in the viewer.
205
+ let cleanUpOldBookmarks ( sceneBasePath : string )
206
+ ( bookmarks : SequencedBookmarks ) =
207
+ let files = Directory.GetFiles sceneBasePath
208
+ for filePath in files do
209
+ let bmGuid =
210
+ SequencedBookmark.guidFromFilename filePath
211
+ let guidExists =
212
+ bmGuid
213
+ |> Option.map ( fun guid ->
214
+ HashMap.containsKey guid bookmarks.bookmarks)
215
+ match guidExists with
216
+ | Some true ->
217
+ ()
218
+ | _ ->
219
+ Log.line " [BookmarkUtils] Cleaning up old bookmarks %s "
220
+ filePath
221
+ try
222
+ File.Delete filePath
223
+ with e ->
224
+ Log.line " [BookmarkUtils] Could not clean up old bookmark %s " filePath
225
+
192
226
/// updates the paths of all bookmarks and
193
227
/// saves bookmarks to file system
194
228
let saveSequencedBookmarks ( basePath : string )
195
- ( bookmarks : SequencedBookmarks ) =
229
+ ( bookmarks : SequencedBookmarks ) =
230
+ let bookmarks = loadAll bookmarks
196
231
let bookmarks = updatePaths basePath bookmarks
197
232
if not ( Directory.Exists basePath) then
198
233
Directory.CreateDirectory basePath |> ignore
234
+ else // if the folder exists, clean up and delete all old bookmarks
235
+ cleanUpOldBookmarks basePath bookmarks
199
236
200
237
for guid, bm in bookmarks.bookmarks do
201
238
match bm with
0 commit comments