From 4b26c00dc6f3d55f31cde32911716e9b1e1dd5e1 Mon Sep 17 00:00:00 2001 From: Marco Burri <26461040+Kersoph@users.noreply.github.com> Date: Mon, 24 Jan 2022 12:44:08 +0100 Subject: [PATCH 1/3] Informing the user in case the application does not have access rights to the saves. --- data/diagram_models/sfc/data/SfcEntity.cs | 75 +++++++++++++------ .../sfc/editor/2d_editor/ProcessingData.cs | 29 ++++--- .../editor/2d_editor/Sfc2dEditorControl.cs | 36 ++++++--- .../sfc/editor/2d_editor/Sfc2dEditorNode.cs | 9 ++- .../sfc/editor/SfcEditorNode.cs | 4 +- .../sfc/editor/controls/EditorControls.cs | 15 +++- .../processing_viewer/SfcSimulationViewer.cs | 10 ++- .../SfcSimulationViewer.tscn | 10 ++- 8 files changed, 127 insertions(+), 61 deletions(-) diff --git a/data/diagram_models/sfc/data/SfcEntity.cs b/data/diagram_models/sfc/data/SfcEntity.cs index cfc707b..428c763 100644 --- a/data/diagram_models/sfc/data/SfcEntity.cs +++ b/data/diagram_models/sfc/data/SfcEntity.cs @@ -70,35 +70,30 @@ public PatchEntity Lookup(int key) } /// - /// Loads the data from the stream. Written in "WriteTo". + /// Writes the data from the stream. Read by "ReadFrom". /// - public void ReadFrom(BinaryReader reader) + public Godot.Error TryWriteTo(string filepath) { - PersistenceCheckHelper.CheckSectionNumber(reader, 0x11111111); - _patchMap.Clear(); - int count = reader.ReadInt32(); - for (int i = 0; i < count; i++) + Godot.Error progress = Godot.Error.Ok; + try { - PatchEntity entity = PatchEntity.CreateFrom(reader); - AddPatch(entity); + using (FileStream stream = File.Open(filepath, FileMode.OpenOrCreate)) + { + using (BinaryWriter writer = new BinaryWriter(stream)) + { + WriteTo(writer); + } + } } - } - - /// - /// Writes the data from the stream. Read by "ReadFrom". - /// - public void WriteTo(BinaryWriter writer) - { - writer.Write(0x11111111); - writer.Write(_patchMap.Count); - foreach (PatchEntity entity in _patchMap.Values) + catch (System.UnauthorizedAccessException) { - entity.WriteTo(writer); + progress = Godot.Error.FileNoPermission; } + return progress; } /// - /// Tries to load a new entity from the given filepath. Null if the path is invalid + /// Tries to load a new entity from the given filepath. Null if the path is invalid or we do not have access rights. /// public static SfcEntity TryLoadFromFile(string filepath) { @@ -106,7 +101,14 @@ public static SfcEntity TryLoadFromFile(string filepath) { return null; } - return LoadFromFile(filepath); + try + { + return LoadFromFile(filepath); + } + catch (System.UnauthorizedAccessException) + { + return null; + } } /// @@ -125,5 +127,36 @@ public static SfcEntity LoadFromFile(string filepath) return entity; } #endregion + + + #region ==================== Helpers ==================== + /// + /// Loads the data from the stream. Written in "WriteTo". + /// + private void ReadFrom(BinaryReader reader) + { + PersistenceCheckHelper.CheckSectionNumber(reader, 0x11111111); + _patchMap.Clear(); + int count = reader.ReadInt32(); + for (int i = 0; i < count; i++) + { + PatchEntity entity = PatchEntity.CreateFrom(reader); + AddPatch(entity); + } + } + + /// + /// Writes the data from the stream. Read by "ReadFrom". + /// + private void WriteTo(BinaryWriter writer) + { + writer.Write(0x11111111); + writer.Write(_patchMap.Count); + foreach (PatchEntity entity in _patchMap.Values) + { + entity.WriteTo(writer); + } + } + #endregion } } diff --git a/data/diagram_models/sfc/editor/2d_editor/ProcessingData.cs b/data/diagram_models/sfc/editor/2d_editor/ProcessingData.cs index a61b732..534a613 100644 --- a/data/diagram_models/sfc/editor/2d_editor/ProcessingData.cs +++ b/data/diagram_models/sfc/editor/2d_editor/ProcessingData.cs @@ -1,4 +1,3 @@ -using System.IO; using System.Collections.Generic; using Osls.SfcEditor.Interpreters; @@ -75,31 +74,29 @@ public bool LookupBoolVariable(string key) } /// - /// Loads the sfc file and repolaces the current data in the SfcEntity + /// Loads the sfc file and if successful, replaces the current SfcEntity + /// returns true if it could be loaded and replaced /// - public void LoadData(string filepath) + public bool TryLoadData(string filepath) { - using (FileStream stream = File.Open(filepath, FileMode.OpenOrCreate)) + SfcEntity loaded = SfcEntity.TryLoadFromFile(filepath); + if (loaded != null) { - using (BinaryReader reader = new BinaryReader(stream)) - { - SfcEntity.ReadFrom(reader); - } + SfcEntity = loaded; + return true; + } + else + { + return false; } } /// /// Saves the SfcEntity to a file /// - public void SaveData(string filepath) + public Godot.Error TrySaveData(string filepath) { - using (FileStream stream = File.Open(filepath, FileMode.OpenOrCreate)) - { - using (BinaryWriter writer = new BinaryWriter(stream)) - { - SfcEntity.WriteTo(writer); - } - } + return SfcEntity.TryWriteTo(filepath); } #endregion } diff --git a/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorControl.cs b/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorControl.cs index 4ce3584..a712d1c 100644 --- a/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorControl.cs +++ b/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorControl.cs @@ -63,32 +63,34 @@ public void MarkStep(int id, bool setMark) /// Loads the file and builds the SFC diagram if the file exists /// Creates a default diagram if it could not be loaded /// - public void LoadDiagramOrDefault(string filepath) + /// True if the diagram could be loaded, false if it was created from default + public bool LoadDiagramOrDefault(string filepath) { - if (!System.IO.File.Exists(filepath)) + bool loadedFromFile = true; + if (System.IO.File.Exists(filepath)) { - if (Data.SfcEntity.Lookup(1, 0) == null) + bool success = Data.TryLoadData(filepath); + if (!success) { - PatchEntity entity = new PatchEntity(1, 1) - { - SfcStepType = StepType.StartingStep - }; - Data.SfcEntity.AddPatch(entity); + SetupDefaultSFC(); + loadedFromFile = false; } } else { - Data.LoadData(filepath); + SetupDefaultSFC(); + loadedFromFile = false; } InitialiseFromData(); + return loadedFromFile; } /// /// Saves the SFC diagram to a file /// - public void SaveDiagram(string filepath) + public Error SaveDiagram(string filepath) { - Data.SaveData(filepath); + return Data.TrySaveData(filepath); } /// @@ -105,6 +107,18 @@ public void ApplyAllEdits() #region ==================== Helpers ==================== + private void SetupDefaultSFC() + { + if (Data.SfcEntity.Lookup(1, 0) == null) + { + PatchEntity entity = new PatchEntity(1, 1) + { + SfcStepType = StepType.StartingStep + }; + Data.SfcEntity.AddPatch(entity); + } + } + /// /// Loads the data from the stream. Written in "WriteTo". /// diff --git a/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorNode.cs b/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorNode.cs index f6058c1..9e2de1b 100644 --- a/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorNode.cs +++ b/data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorNode.cs @@ -49,18 +49,19 @@ public override void _Process(float delta) /// /// Saves the SFC diagram to a file /// - public void SaveDiagram(string filepath) + public Error SaveDiagram(string filepath) { - Sfc2dEditorControl.SaveDiagram(filepath); + return Sfc2dEditorControl.SaveDiagram(filepath); } /// /// Loads the file and builds the SFC diagram if the file exists /// Creates a default diagram if it could not be loaded /// - public void TryLoadDiagram(string filepath) + /// True if the diagram could be loaded, false if it was created from default + public bool TryLoadDiagram(string filepath) { - Sfc2dEditorControl.LoadDiagramOrDefault(filepath); + return Sfc2dEditorControl.LoadDiagramOrDefault(filepath); } /// diff --git a/data/diagram_models/sfc/editor/SfcEditorNode.cs b/data/diagram_models/sfc/editor/SfcEditorNode.cs index 99fc802..c35bdfd 100644 --- a/data/diagram_models/sfc/editor/SfcEditorNode.cs +++ b/data/diagram_models/sfc/editor/SfcEditorNode.cs @@ -51,8 +51,8 @@ public override void InitialiseWith(IMainNode mainNode, ILessonEntity openedLess public void SaveDiagram() { string filepath = OpenedLesson.CustomDiagramFilePath; - Sfc2dEditorNode.SaveDiagram(filepath); - GetNode(EditorControlsPath).OnSaveDiagram(); + Error result = Sfc2dEditorNode.SaveDiagram(filepath); + GetNode(EditorControlsPath).OnSaveDiagram(result); } /// diff --git a/data/diagram_models/sfc/editor/controls/EditorControls.cs b/data/diagram_models/sfc/editor/controls/EditorControls.cs index 900d80f..85a5aee 100644 --- a/data/diagram_models/sfc/editor/controls/EditorControls.cs +++ b/data/diagram_models/sfc/editor/controls/EditorControls.cs @@ -39,9 +39,20 @@ public void SaveDiagram() /// /// Called by the editor when de diagram was saved /// - public void OnSaveDiagram() + public void OnSaveDiagram(Error result) { - GetNode("TextInfo").ShowMessage("Saved " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath); + switch (result) + { + case Error.Ok: + GetNode("TextInfo").ShowMessage("Saved " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath); + break; + case Error.FileNoPermission: + GetNode("TextInfo").ShowMessage("We do not have write permission at: " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath); + break; + default: + GetNode("TextInfo").ShowMessage("Could not save: " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath + " " + result); + break; + } } #endregion } diff --git a/data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.cs b/data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.cs index 7afb28d..bbf3768 100644 --- a/data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.cs +++ b/data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.cs @@ -14,6 +14,7 @@ public class SfcSimulationViewer : PageModule { #region ==================== Fields / Properties ==================== [Export] private NodePath _sfc2dControlsPath = "HscRelative/Sfc2dViewer/Sfc2dControls"; + [Export] private NodePath _errorLabelPath = "HscRelative/Sfc2dViewer/ErrorLabel"; private IMainNode _mainNode; private LessonView _lessonView; @@ -47,7 +48,7 @@ public override void InitialiseWith(IMainNode mainNode, ILessonEntity openedLess InitialiseDiagram(openedLesson); InitialiseSimulation(openedLesson); _breakpoints = new BreakpointManager(_simulationMaster, _sfc2dEditorNode); - if (!_isExecutable) GetNode