Skip to content

Commit 1d9a2d4

Browse files
committed
Merge branch 'P4_Improvements' into main
2 parents a130c0a + 2be4345 commit 1d9a2d4

File tree

12 files changed

+132
-66
lines changed

12 files changed

+132
-66
lines changed

data/core/scene_manager/navigation_steps/NavigationSteps.tscn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ anchor_bottom = 1.0
3535
margin_top = -24.0
3636
custom_fonts/font = ExtResource( 8 )
3737
custom_colors/font_color = Color( 0.470588, 0.470588, 0.470588, 1 )
38-
text = "v1.0.0"
38+
text = "v1.0.1"
3939
align = 1
4040
__meta__ = {
4141
"_edit_use_anchors_": false

data/diagram_models/sfc/data/SfcEntity.cs

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,43 +70,45 @@ public PatchEntity Lookup(int key)
7070
}
7171

7272
/// <summary>
73-
/// Loads the data from the stream. Written in "WriteTo".
73+
/// Writes the data from the stream. Read by "ReadFrom".
7474
/// </summary>
75-
public void ReadFrom(BinaryReader reader)
75+
public Godot.Error TryWriteTo(string filepath)
7676
{
77-
PersistenceCheckHelper.CheckSectionNumber(reader, 0x11111111);
78-
_patchMap.Clear();
79-
int count = reader.ReadInt32();
80-
for (int i = 0; i < count; i++)
77+
Godot.Error progress = Godot.Error.Ok;
78+
try
8179
{
82-
PatchEntity entity = PatchEntity.CreateFrom(reader);
83-
AddPatch(entity);
80+
using (FileStream stream = File.Open(filepath, FileMode.OpenOrCreate))
81+
{
82+
using (BinaryWriter writer = new BinaryWriter(stream))
83+
{
84+
WriteTo(writer);
85+
}
86+
}
8487
}
85-
}
86-
87-
/// <summary>
88-
/// Writes the data from the stream. Read by "ReadFrom".
89-
/// </summary>
90-
public void WriteTo(BinaryWriter writer)
91-
{
92-
writer.Write(0x11111111);
93-
writer.Write(_patchMap.Count);
94-
foreach (PatchEntity entity in _patchMap.Values)
88+
catch (System.UnauthorizedAccessException)
9589
{
96-
entity.WriteTo(writer);
90+
progress = Godot.Error.FileNoPermission;
9791
}
92+
return progress;
9893
}
9994

10095
/// <summary>
101-
/// Tries to load a new entity from the given filepath. Null if the path is invalid
96+
/// Tries to load a new entity from the given filepath. Null if the path is invalid or we do not have access rights.
10297
/// </summary>
10398
public static SfcEntity TryLoadFromFile(string filepath)
10499
{
105100
if (!File.Exists(filepath))
106101
{
107102
return null;
108103
}
109-
return LoadFromFile(filepath);
104+
try
105+
{
106+
return LoadFromFile(filepath);
107+
}
108+
catch (System.UnauthorizedAccessException)
109+
{
110+
return null;
111+
}
110112
}
111113

112114
/// <summary>
@@ -125,5 +127,36 @@ public static SfcEntity LoadFromFile(string filepath)
125127
return entity;
126128
}
127129
#endregion
130+
131+
132+
#region ==================== Helpers ====================
133+
/// <summary>
134+
/// Loads the data from the stream. Written in "WriteTo".
135+
/// </summary>
136+
private void ReadFrom(BinaryReader reader)
137+
{
138+
PersistenceCheckHelper.CheckSectionNumber(reader, 0x11111111);
139+
_patchMap.Clear();
140+
int count = reader.ReadInt32();
141+
for (int i = 0; i < count; i++)
142+
{
143+
PatchEntity entity = PatchEntity.CreateFrom(reader);
144+
AddPatch(entity);
145+
}
146+
}
147+
148+
/// <summary>
149+
/// Writes the data from the stream. Read by "ReadFrom".
150+
/// </summary>
151+
private void WriteTo(BinaryWriter writer)
152+
{
153+
writer.Write(0x11111111);
154+
writer.Write(_patchMap.Count);
155+
foreach (PatchEntity entity in _patchMap.Values)
156+
{
157+
entity.WriteTo(writer);
158+
}
159+
}
160+
#endregion
128161
}
129162
}

data/diagram_models/sfc/editor/2d_editor/ProcessingData.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.IO;
21
using System.Collections.Generic;
32
using Osls.SfcEditor.Interpreters;
43

@@ -75,31 +74,29 @@ public bool LookupBoolVariable(string key)
7574
}
7675

7776
/// <summary>
78-
/// Loads the sfc file and repolaces the current data in the SfcEntity
77+
/// Loads the sfc file and if successful, replaces the current SfcEntity
78+
/// returns true if it could be loaded and replaced
7979
/// </summary>
80-
public void LoadData(string filepath)
80+
public bool TryLoadData(string filepath)
8181
{
82-
using (FileStream stream = File.Open(filepath, FileMode.OpenOrCreate))
82+
SfcEntity loaded = SfcEntity.TryLoadFromFile(filepath);
83+
if (loaded != null)
8384
{
84-
using (BinaryReader reader = new BinaryReader(stream))
85-
{
86-
SfcEntity.ReadFrom(reader);
87-
}
85+
SfcEntity = loaded;
86+
return true;
87+
}
88+
else
89+
{
90+
return false;
8891
}
8992
}
9093

9194
/// <summary>
9295
/// Saves the SfcEntity to a file
9396
/// </summary>
94-
public void SaveData(string filepath)
97+
public Godot.Error TrySaveData(string filepath)
9598
{
96-
using (FileStream stream = File.Open(filepath, FileMode.OpenOrCreate))
97-
{
98-
using (BinaryWriter writer = new BinaryWriter(stream))
99-
{
100-
SfcEntity.WriteTo(writer);
101-
}
102-
}
99+
return SfcEntity.TryWriteTo(filepath);
103100
}
104101
#endregion
105102
}

data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorControl.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,32 +63,34 @@ public void MarkStep(int id, bool setMark)
6363
/// Loads the file and builds the SFC diagram if the file exists
6464
/// Creates a default diagram if it could not be loaded
6565
/// </summary>
66-
public void LoadDiagramOrDefault(string filepath)
66+
/// <returns>True if the diagram could be loaded, false if it was created from default</returns>
67+
public bool LoadDiagramOrDefault(string filepath)
6768
{
68-
if (!System.IO.File.Exists(filepath))
69+
bool loadedFromFile = true;
70+
if (System.IO.File.Exists(filepath))
6971
{
70-
if (Data.SfcEntity.Lookup(1, 0) == null)
72+
bool success = Data.TryLoadData(filepath);
73+
if (!success)
7174
{
72-
PatchEntity entity = new PatchEntity(1, 1)
73-
{
74-
SfcStepType = StepType.StartingStep
75-
};
76-
Data.SfcEntity.AddPatch(entity);
75+
SetupDefaultSFC();
76+
loadedFromFile = false;
7777
}
7878
}
7979
else
8080
{
81-
Data.LoadData(filepath);
81+
SetupDefaultSFC();
82+
loadedFromFile = false;
8283
}
8384
InitialiseFromData();
85+
return loadedFromFile;
8486
}
8587

8688
/// <summary>
8789
/// Saves the SFC diagram to a file
8890
/// </summary>
89-
public void SaveDiagram(string filepath)
91+
public Error SaveDiagram(string filepath)
9092
{
91-
Data.SaveData(filepath);
93+
return Data.TrySaveData(filepath);
9294
}
9395

9496
/// <summary>
@@ -105,6 +107,18 @@ public void ApplyAllEdits()
105107

106108

107109
#region ==================== Helpers ====================
110+
private void SetupDefaultSFC()
111+
{
112+
if (Data.SfcEntity.Lookup(1, 0) == null)
113+
{
114+
PatchEntity entity = new PatchEntity(1, 1)
115+
{
116+
SfcStepType = StepType.StartingStep
117+
};
118+
Data.SfcEntity.AddPatch(entity);
119+
}
120+
}
121+
108122
/// <summary>
109123
/// Loads the data from the stream. Written in "WriteTo".
110124
/// </summary>

data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorNode.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,19 @@ public override void _Process(float delta)
4949
/// <summary>
5050
/// Saves the SFC diagram to a file
5151
/// </summary>
52-
public void SaveDiagram(string filepath)
52+
public Error SaveDiagram(string filepath)
5353
{
54-
Sfc2dEditorControl.SaveDiagram(filepath);
54+
return Sfc2dEditorControl.SaveDiagram(filepath);
5555
}
5656

5757
/// <summary>
5858
/// Loads the file and builds the SFC diagram if the file exists
5959
/// Creates a default diagram if it could not be loaded
6060
/// </summary>
61-
public void TryLoadDiagram(string filepath)
61+
/// <returns>True if the diagram could be loaded, false if it was created from default</returns>
62+
public bool TryLoadDiagram(string filepath)
6263
{
63-
Sfc2dEditorControl.LoadDiagramOrDefault(filepath);
64+
return Sfc2dEditorControl.LoadDiagramOrDefault(filepath);
6465
}
6566

6667
/// <summary>

data/diagram_models/sfc/editor/SfcEditorNode.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ public override void InitialiseWith(IMainNode mainNode, ILessonEntity openedLess
5151
public void SaveDiagram()
5252
{
5353
string filepath = OpenedLesson.CustomDiagramFilePath;
54-
Sfc2dEditorNode.SaveDiagram(filepath);
55-
GetNode<EditorControls>(EditorControlsPath).OnSaveDiagram();
54+
Error result = Sfc2dEditorNode.SaveDiagram(filepath);
55+
GetNode<EditorControls>(EditorControlsPath).OnSaveDiagram(result);
5656
}
5757

5858
/// <summary>

data/diagram_models/sfc/editor/controls/EditorControls.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ public void SaveDiagram()
3939
/// <summary>
4040
/// Called by the editor when de diagram was saved
4141
/// </summary>
42-
public void OnSaveDiagram()
42+
public void OnSaveDiagram(Error result)
4343
{
44-
GetNode<TextInfo>("TextInfo").ShowMessage("Saved " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath);
44+
switch (result)
45+
{
46+
case Error.Ok:
47+
GetNode<TextInfo>("TextInfo").ShowMessage("Saved " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath);
48+
break;
49+
case Error.FileNoPermission:
50+
GetNode<TextInfo>("TextInfo").ShowMessage("We do not have write permission at: " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath);
51+
break;
52+
default:
53+
GetNode<TextInfo>("TextInfo").ShowMessage("Could not save: " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath + " " + result);
54+
break;
55+
}
4556
}
4657
#endregion
4758
}

data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ public class SfcSimulationViewer : PageModule
1414
{
1515
#region ==================== Fields / Properties ====================
1616
[Export] private NodePath _sfc2dControlsPath = "HscRelative/Sfc2dViewer/Sfc2dControls";
17+
[Export] private NodePath _errorLabelPath = "HscRelative/Sfc2dViewer/ErrorLabel";
1718
private IMainNode _mainNode;
1819

1920
private LessonView _lessonView;
@@ -47,7 +48,7 @@ public override void InitialiseWith(IMainNode mainNode, ILessonEntity openedLess
4748
InitialiseDiagram(openedLesson);
4849
InitialiseSimulation(openedLesson);
4950
_breakpoints = new BreakpointManager(_simulationMaster, _sfc2dEditorNode);
50-
if (!_isExecutable) GetNode<Label>("HscRelative/Sfc2dViewer/ErrorLabel").Visible = true;
51+
if (!_isExecutable) GetNode<Label>(_errorLabelPath).Visible = true;
5152
}
5253

5354
public override void _Process(float delta)
@@ -112,7 +113,12 @@ private void InitialiseDiagram(ILessonEntity openedLesson)
112113
_sfc2dEditorNode = GetNode<Sfc2dEditorNode>("HscRelative/Sfc2dViewer/Sfc2dEditor");
113114
_sfc2dEditorNode.InitializeEditor(_processingData, false);
114115
string filepath = openedLesson.TemporaryDiagramFilePath;
115-
_sfc2dEditorNode.TryLoadDiagram(filepath);
116+
bool success = _sfc2dEditorNode.TryLoadDiagram(filepath);
117+
if (!success)
118+
{
119+
GetNode<Label>(_errorLabelPath).Text = "No file access permission in the lesson folder.";
120+
GetNode<Label>(_errorLabelPath).Visible = true;
121+
}
116122
}
117123

118124
/// <summary>

data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.tscn

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
[gd_scene load_steps=6 format=2]
1+
[gd_scene load_steps=7 format=2]
22

33
[ext_resource path="res://data/diagram_models/sfc/processing_viewer/controls/Sfc2dControls.tscn" type="PackedScene" id=1]
44
[ext_resource path="res://data/core/theme/nodes/HscRelative.tscn" type="PackedScene" id=2]
55
[ext_resource path="res://data/diagram_models/sfc/editor/2d_editor/Sfc2dEditor.tscn" type="PackedScene" id=3]
6+
[ext_resource path="res://data/core/theme/font/RobotoBoldFont.tres" type="DynamicFont" id=4]
67
[ext_resource path="res://data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.cs" type="Script" id=5]
78
[ext_resource path="res://data/core/lesson/viewer/LessonView.tscn" type="PackedScene" id=6]
89

@@ -35,8 +36,11 @@ mouse_default_cursor_shape = 8
3536

3637
[node name="ErrorLabel" type="Label" parent="HscRelative/Sfc2dViewer"]
3738
visible = false
38-
margin_right = 40.0
39-
margin_bottom = 38.0
39+
margin_left = 5.0
40+
margin_top = 5.0
41+
margin_right = 305.0
42+
margin_bottom = 29.0
43+
custom_fonts/font = ExtResource( 4 )
4044
custom_colors/font_color = Color( 1, 0, 0, 1 )
4145
text = "SFC contains errors!"
4246

export_presets.cfg

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ codesign/digest_algorithm=1
3232
codesign/description=""
3333
codesign/custom_options=PoolStringArray( )
3434
application/icon=""
35-
application/file_version="1.0.0"
36-
application/product_version="1.0.0"
35+
application/file_version="1.0.1"
36+
application/product_version="1.0.1"
3737
application/company_name=""
3838
application/product_name="Open Sequential Logic Simulation"
3939
application/file_description="Made with Godot Engine"
@@ -87,8 +87,8 @@ application/info="Made with Godot Engine"
8787
application/icon=""
8888
application/identifier=""
8989
application/signature=""
90-
application/short_version="1.0.0"
91-
application/version="1.0.0"
90+
application/short_version="1.0.1"
91+
application/version="1.0.1"
9292
application/copyright=""
9393
display/high_res=false
9494
privacy/camera_usage_description=""

0 commit comments

Comments
 (0)