|
1 | 1 | using Facepunch.ActionGraphs; |
2 | 2 | using Sandbox.ActionGraphs; |
3 | 3 | using System; |
| 4 | +using System.IO; |
4 | 5 |
|
5 | 6 | namespace Editor; |
6 | 7 |
|
@@ -305,62 +306,49 @@ public void Reload() |
305 | 306 | public void Save( bool saveAs ) |
306 | 307 | { |
307 | 308 | bool isPrefab = Scene is PrefabScene; |
308 | | - var saveLocation = string.Empty; |
| 309 | + string extension = isPrefab ? "prefab" : "scene"; |
| 310 | + string fileType = isPrefab ? "Prefab" : "Scene"; |
309 | 311 |
|
310 | | - if ( Scene.Source is not null && AssetSystem.FindByPath( Scene.Source.ResourcePath ) is Asset sourceAsset ) |
| 312 | + var saveLocation = string.Empty; |
| 313 | + if ( !saveAs && Scene.Source is not null && AssetSystem.FindByPath( Scene.Source.ResourcePath ) is Asset sourceAsset ) |
311 | 314 | { |
312 | 315 | saveLocation = sourceAsset.AbsolutePath; |
313 | 316 | } |
314 | 317 | else |
315 | 318 | { |
316 | | - saveAs = true; |
317 | | - } |
318 | | - |
319 | | - string extension = isPrefab ? "prefab" : "scene"; |
320 | | - string fileType = isPrefab ? "Prefab" : "Scene"; |
321 | | - |
322 | | - if ( saveAs ) |
323 | | - { |
324 | | - if ( string.IsNullOrEmpty( saveLocation ) ) |
| 319 | + saveLocation = ProjectCookie.GetString( $"LastSaveLocation.{extension}", string.Empty ); |
| 320 | + if ( !Directory.Exists( saveLocation ) ) |
325 | 321 | { |
326 | | - saveLocation = System.IO.Path.Combine( |
327 | | - System.IO.Path.GetDirectoryName( ProjectCookie.GetString( $"LastSaveLocation.{extension}", Project.Current.GetAssetsPath() ) ), |
328 | | - $"untitled.{extension}" ); |
| 322 | + saveLocation = Project.Current.GetAssetsPath(); |
329 | 323 | } |
330 | 324 |
|
331 | | - saveLocation = EditorUtility.SaveFileDialog( $"Save {fileType} As..", extension, saveLocation ); |
332 | | - |
| 325 | + saveLocation = EditorUtility.SaveFileDialog( $"Save {fileType} As..", extension, |
| 326 | + Path.Combine( saveLocation, $"untitled.{extension}" ) ); |
333 | 327 | if ( saveLocation is null ) |
334 | 328 | return; |
335 | 329 |
|
336 | | - ProjectCookie.SetString( $"LastSaveLocation.{extension}", System.IO.Path.GetDirectoryName( saveLocation ) ); |
337 | | - } |
| 330 | + if ( !saveLocation.NormalizeFilename( false ).StartsWith( Project.Current.GetAssetsPath().NormalizeFilename( false ) ) ) |
| 331 | + { |
| 332 | + // enforce saving inside Assets/ - if it's outside, complain and let them retry |
| 333 | + EditorUtility.DisplayDialog( $"Save Failed: Invalid location", |
| 334 | + $"{fileType}s must be saved inside the Assets folder of your project", "Cancel", "Retry", |
| 335 | + () => Save( true ) ); |
| 336 | + return; |
| 337 | + } |
338 | 338 |
|
339 | | - EditorEvent.Run( "scene.beforesave", SceneEditorSession.Active.Scene ); |
| 339 | + ProjectCookie.SetString( $"LastSaveLocation.{extension}", Path.GetDirectoryName( saveLocation ) ); |
| 340 | + } |
340 | 341 |
|
341 | | - if ( Scene is PrefabScene prefabScene ) |
342 | | - { |
343 | | - var prefabFile = prefabScene.ToPrefabFile(); |
344 | | - var asset = AssetSystem.CreateResource( "prefab", saveLocation ); |
345 | | - asset.SaveToDisk( prefabFile ); |
| 342 | + EditorEvent.Run( "scene.beforesave", Active.Scene ); |
346 | 343 |
|
347 | | - // Update this scene's path |
348 | | - Scene.Source = prefabFile; |
349 | | - Scene.Name = System.IO.Path.GetFileNameWithoutExtension( saveLocation ); |
350 | | - } |
351 | | - else |
352 | | - { |
353 | | - var sceneFile = Scene.CreateSceneFile(); |
354 | | - var asset = AssetSystem.CreateResource( "scene", saveLocation ); |
355 | | - asset.SaveToDisk( sceneFile ); |
| 344 | + var asset = AssetSystem.CreateResource( extension, saveLocation ); |
| 345 | + Assert.NotNull( asset, $"Failed to CreateResource for {fileType} at {saveLocation}" ); |
356 | 346 |
|
357 | | - // Update this scene's path |
358 | | - Scene.Source = sceneFile; |
359 | | - Scene.Name = System.IO.Path.GetFileNameWithoutExtension( saveLocation ); |
360 | | - } |
| 347 | + GameResource resource = Scene is PrefabScene prefabScene ? prefabScene.ToPrefabFile() : Scene.CreateSceneFile(); |
| 348 | + asset.SaveToDisk( resource ); |
361 | 349 |
|
362 | 350 | HasUnsavedChanges = false; |
363 | | - EditorEvent.Run( "scene.saved", SceneEditorSession.Active.Scene ); |
| 351 | + EditorEvent.Run( "scene.saved", Active.Scene ); |
364 | 352 |
|
365 | 353 | UpdateEditorTitle(); |
366 | 354 | } |
|
0 commit comments