Skip to content

Commit

Permalink
Merge branch 'P4_Improvements' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Kersoph committed Jan 24, 2022
2 parents a130c0a + 2be4345 commit 1d9a2d4
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ anchor_bottom = 1.0
margin_top = -24.0
custom_fonts/font = ExtResource( 8 )
custom_colors/font_color = Color( 0.470588, 0.470588, 0.470588, 1 )
text = "v1.0.0"
text = "v1.0.1"
align = 1
__meta__ = {
"_edit_use_anchors_": false
Expand Down
75 changes: 54 additions & 21 deletions data/diagram_models/sfc/data/SfcEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,43 +70,45 @@ public PatchEntity Lookup(int key)
}

/// <summary>
/// Loads the data from the stream. Written in "WriteTo".
/// Writes the data from the stream. Read by "ReadFrom".
/// </summary>
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);
}
}
}
}

/// <summary>
/// Writes the data from the stream. Read by "ReadFrom".
/// </summary>
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;
}

/// <summary>
/// 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.
/// </summary>
public static SfcEntity TryLoadFromFile(string filepath)
{
if (!File.Exists(filepath))
{
return null;
}
return LoadFromFile(filepath);
try
{
return LoadFromFile(filepath);
}
catch (System.UnauthorizedAccessException)
{
return null;
}
}

/// <summary>
Expand All @@ -125,5 +127,36 @@ public static SfcEntity LoadFromFile(string filepath)
return entity;
}
#endregion


#region ==================== Helpers ====================
/// <summary>
/// Loads the data from the stream. Written in "WriteTo".
/// </summary>
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);
}
}

/// <summary>
/// Writes the data from the stream. Read by "ReadFrom".
/// </summary>
private void WriteTo(BinaryWriter writer)
{
writer.Write(0x11111111);
writer.Write(_patchMap.Count);
foreach (PatchEntity entity in _patchMap.Values)
{
entity.WriteTo(writer);
}
}
#endregion
}
}
29 changes: 13 additions & 16 deletions data/diagram_models/sfc/editor/2d_editor/ProcessingData.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.IO;
using System.Collections.Generic;
using Osls.SfcEditor.Interpreters;

Expand Down Expand Up @@ -75,31 +74,29 @@ public bool LookupBoolVariable(string key)
}

/// <summary>
/// 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
/// </summary>
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;
}
}

/// <summary>
/// Saves the SfcEntity to a file
/// </summary>
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
}
Expand Down
36 changes: 25 additions & 11 deletions data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// </summary>
public void LoadDiagramOrDefault(string filepath)
/// <returns>True if the diagram could be loaded, false if it was created from default</returns>
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;
}

/// <summary>
/// Saves the SFC diagram to a file
/// </summary>
public void SaveDiagram(string filepath)
public Error SaveDiagram(string filepath)
{
Data.SaveData(filepath);
return Data.TrySaveData(filepath);
}

/// <summary>
Expand All @@ -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);
}
}

/// <summary>
/// Loads the data from the stream. Written in "WriteTo".
/// </summary>
Expand Down
9 changes: 5 additions & 4 deletions data/diagram_models/sfc/editor/2d_editor/Sfc2dEditorNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ public override void _Process(float delta)
/// <summary>
/// Saves the SFC diagram to a file
/// </summary>
public void SaveDiagram(string filepath)
public Error SaveDiagram(string filepath)
{
Sfc2dEditorControl.SaveDiagram(filepath);
return Sfc2dEditorControl.SaveDiagram(filepath);
}

/// <summary>
/// Loads the file and builds the SFC diagram if the file exists
/// Creates a default diagram if it could not be loaded
/// </summary>
public void TryLoadDiagram(string filepath)
/// <returns>True if the diagram could be loaded, false if it was created from default</returns>
public bool TryLoadDiagram(string filepath)
{
Sfc2dEditorControl.LoadDiagramOrDefault(filepath);
return Sfc2dEditorControl.LoadDiagramOrDefault(filepath);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions data/diagram_models/sfc/editor/SfcEditorNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public override void InitialiseWith(IMainNode mainNode, ILessonEntity openedLess
public void SaveDiagram()
{
string filepath = OpenedLesson.CustomDiagramFilePath;
Sfc2dEditorNode.SaveDiagram(filepath);
GetNode<EditorControls>(EditorControlsPath).OnSaveDiagram();
Error result = Sfc2dEditorNode.SaveDiagram(filepath);
GetNode<EditorControls>(EditorControlsPath).OnSaveDiagram(result);
}

/// <summary>
Expand Down
15 changes: 13 additions & 2 deletions data/diagram_models/sfc/editor/controls/EditorControls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,20 @@ public void SaveDiagram()
/// <summary>
/// Called by the editor when de diagram was saved
/// </summary>
public void OnSaveDiagram()
public void OnSaveDiagram(Error result)
{
GetNode<TextInfo>("TextInfo").ShowMessage("Saved " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath);
switch (result)
{
case Error.Ok:
GetNode<TextInfo>("TextInfo").ShowMessage("Saved " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath);
break;
case Error.FileNoPermission:
GetNode<TextInfo>("TextInfo").ShowMessage("We do not have write permission at: " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath);
break;
default:
GetNode<TextInfo>("TextInfo").ShowMessage("Could not save: " + SfcEditorNode.OpenedLesson.CustomDiagramFilePath + " " + result);
break;
}
}
#endregion
}
Expand Down
10 changes: 8 additions & 2 deletions data/diagram_models/sfc/processing_viewer/SfcSimulationViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,7 +48,7 @@ public override void InitialiseWith(IMainNode mainNode, ILessonEntity openedLess
InitialiseDiagram(openedLesson);
InitialiseSimulation(openedLesson);
_breakpoints = new BreakpointManager(_simulationMaster, _sfc2dEditorNode);
if (!_isExecutable) GetNode<Label>("HscRelative/Sfc2dViewer/ErrorLabel").Visible = true;
if (!_isExecutable) GetNode<Label>(_errorLabelPath).Visible = true;
}

public override void _Process(float delta)
Expand Down Expand Up @@ -112,7 +113,12 @@ private void InitialiseDiagram(ILessonEntity openedLesson)
_sfc2dEditorNode = GetNode<Sfc2dEditorNode>("HscRelative/Sfc2dViewer/Sfc2dEditor");
_sfc2dEditorNode.InitializeEditor(_processingData, false);
string filepath = openedLesson.TemporaryDiagramFilePath;
_sfc2dEditorNode.TryLoadDiagram(filepath);
bool success = _sfc2dEditorNode.TryLoadDiagram(filepath);
if (!success)
{
GetNode<Label>(_errorLabelPath).Text = "No file access permission in the lesson folder.";
GetNode<Label>(_errorLabelPath).Visible = true;
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
[gd_scene load_steps=6 format=2]
[gd_scene load_steps=7 format=2]

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

Expand Down Expand Up @@ -35,8 +36,11 @@ mouse_default_cursor_shape = 8

[node name="ErrorLabel" type="Label" parent="HscRelative/Sfc2dViewer"]
visible = false
margin_right = 40.0
margin_bottom = 38.0
margin_left = 5.0
margin_top = 5.0
margin_right = 305.0
margin_bottom = 29.0
custom_fonts/font = ExtResource( 4 )
custom_colors/font_color = Color( 1, 0, 0, 1 )
text = "SFC contains errors!"

Expand Down
8 changes: 4 additions & 4 deletions export_presets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ codesign/digest_algorithm=1
codesign/description=""
codesign/custom_options=PoolStringArray( )
application/icon=""
application/file_version="1.0.0"
application/product_version="1.0.0"
application/file_version="1.0.1"
application/product_version="1.0.1"
application/company_name=""
application/product_name="Open Sequential Logic Simulation"
application/file_description="Made with Godot Engine"
Expand Down Expand Up @@ -87,8 +87,8 @@ application/info="Made with Godot Engine"
application/icon=""
application/identifier=""
application/signature=""
application/short_version="1.0.0"
application/version="1.0.0"
application/short_version="1.0.1"
application/version="1.0.1"
application/copyright=""
display/high_res=false
privacy/camera_usage_description=""
Expand Down
Binary file removed lessons/1/User/ThreeStars.sfc
Binary file not shown.
Binary file removed lessons/1/User/TwoStars.sfc
Binary file not shown.

0 comments on commit 1d9a2d4

Please sign in to comment.