Skip to content

Commit

Permalink
Explorer Experimental Release 0.1.0.8
Browse files Browse the repository at this point in the history
  • Loading branch information
CheeseJedi committed Sep 23, 2018
1 parent 0771226 commit f42d68b
Show file tree
Hide file tree
Showing 17 changed files with 453 additions and 172 deletions.
4 changes: 2 additions & 2 deletions HELLION.CrewSync/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.1.2.91")]
[assembly: AssemblyFileVersion("0.1.2.91")]
[assembly: AssemblyVersion("0.1.2.92")]
[assembly: AssemblyFileVersion("0.1.2.92")]
165 changes: 117 additions & 48 deletions HELLION.DataStructures/BaseClasses/Json_File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,40 +28,45 @@ public class Json_File
/// Constructor that takes a FileInfo and, if the file exists, triggers the load.
/// </summary>
/// <param name="PassedFileInfo">The FileInfo representing the file to be loaded.</param>
public Json_File(IParent_Json_File ownerObject, FileInfo passedFileInfo, bool autoDeserialise = false )
public Json_File(IParent_Json_File ownerObject, FileInfo passedFileInfo, bool autoDeserialise = false)
: this(ownerObject)
{
//Debug.Print("Json_File.ctor(FileInfo) called {0}", passedFileInfo.FullName);

AutoLoadOnFileInfoSet = true;
AutoDeserialiseOnJdataModification = autoDeserialise;

// Set the FileInfo object and ensure it's not null.
File = passedFileInfo ?? throw new NullReferenceException("Json_File.ctor(FileInfo) - passedFileInfo was null.");

// Load the file.
if (LoadFile()) Debug.Print("File failed to load from constructor - LoadFile() returned true.");
else
{
// Trigger the first de-serialisation.
if (autoDeserialise) Deserialise();

// Switch on automatic de-serialisation of the JData when it changes, if specified.
// This is done _after_ the de-serialisation to prevent excessive
// triggered de-serialisation.
AutoDeserialiseOnJdataModification = autoDeserialise;

}
//if (LoadFile()) Debug.Print("File failed to load from constructor - LoadFile() returned true.");
//else
//{
// // Trigger the first de-serialisation.
// if (autoDeserialise) Deserialise();

// // Switch on automatic de-serialisation of the JData when it changes, if specified.
// // This is done _after_ the de-serialisation to prevent excessive
// // triggered de-serialisation.
// AutoDeserialiseOnJdataModification = autoDeserialise;

//}
}

/// <summary>
/// Constructor for creating a Json_File in memory from a JToken.
/// </summary>
/// <param name="ownerObject"></param>
/// <param name="jdata"></param>
public Json_File(IParent_Json_File ownerObject, JToken jdata, bool autoDeserialise = false )
public Json_File(IParent_Json_File ownerObject, JToken jdata, bool autoDeserialise = false)
: this(ownerObject)
{
//Debug.Print("Json_File.ctor(FileInfo) called - HasValues [{0}]",
// jdata != null ? jdata.HasValues.ToString() : "null");

AutoLoadOnFileInfoSet = false;

// Switch on automatic de-serialisation of the JData when it changes.
AutoDeserialiseOnJdataModification = autoDeserialise;

Expand All @@ -85,7 +90,21 @@ public Json_File(IParent_Json_File ownerObject, JToken jdata, bool autoDeseriali
/// <summary>
/// FileInfo object that represents the file on disk that is to be worked with.
/// </summary>
public FileInfo File { get; protected set; } = null;
public FileInfo File
{
get => _file;
protected set
{
if (_file != value)
{
_file = value;

ProcessChangedFileInfo();

}
}
}


/// <summary>
/// Public property to get and set the JToken that was loaded from the file.
Expand All @@ -109,6 +128,11 @@ public JToken JData
/// </summary>
public bool AutoDeserialiseOnJdataModification { get; set; } = false;

/// <summary>
/// Determines whether a new FileInfo being set will trigger a LoadFile() operation.
/// </summary>
public bool AutoLoadOnFileInfoSet { get; set; } = false;

/// <summary>
/// Used to determine whether the file is loaded.
/// </summary>
Expand Down Expand Up @@ -197,6 +221,35 @@ public bool IsDirty

#region Methods

/// <summary>
/// Responsible for triggering load and de-serialisation when the FileInfo changes.
/// </summary>
private void ProcessChangedFileInfo()
{
if (AutoLoadOnFileInfoSet)
{
bool autoDeserialise = AutoDeserialiseOnJdataModification;

// The JData object changes a lot during a load so the auto-deserialise is deactivated.
AutoDeserialiseOnJdataModification = false;

// Load the file.
if (LoadFile()) Debug.Print("File failed to load from constructor - LoadFile() returned true.");
else
{

// Trigger the first de-serialisation.
if (autoDeserialise) Deserialise();

// Switch on automatic de-serialisation of the JData when it changes, if specified.
// This is done _after_ the de-serialisation to prevent excessive
// triggered de-serialisation.
AutoDeserialiseOnJdataModification = autoDeserialise;

}
}
}

/// <summary>
/// Load file data from FileName and parse to the JData JObject of type IOrderedEnumerable<JToken>
/// </summary>
Expand Down Expand Up @@ -246,46 +299,74 @@ public virtual bool LoadFile()

// FileInfo was null or file does not exist.

if (File == null) Debug.Print("FileInfo was null.");
if (!File.Exists) Debug.Print("File does not exist");
if (File != null && !File.Exists) Debug.Print("File does not exist");
else Debug.Print("FileInfo was null.");

LoadError = true;
return true;
}

/// <summary>
/// Handles changes to the JData object.
/// </summary>
protected virtual void ProcessChangedJData()
{
if (AutoDeserialiseOnJdataModification)
{
// Triggered de-serialisation.
Deserialise();
}
}

/// <summary>
/// Does nothing - This method is a stub to be overridden by derived classes.
/// </summary>
public virtual void Deserialise() => throw new NotImplementedException
("This method is a stub to be overridden by derived classes.");

/// <summary>
/// Does nothing - This method is a stub to be overridden by derived classes.
/// </summary>
public virtual void Serialise() => throw new NotImplementedException
("This method is a stub to be overridden by derived classes.");

/// <summary>
/// Save the file data.
/// </summary>
/// <returns></returns>
public bool SaveFile(bool CreateBackup = true)
public bool SaveFile(bool createBackup = true, FileInfo file = null)
{
//Debug.Print("SaveFile Called, CreateBackup="+CreateBackup.ToString());

if (File.Exists && CreateBackup)
// Unless a new FileInfo was specified (SaveAs), use the existing File (Save)
if (file == null) file = File;


if (file.Exists && createBackup)
{
string backupFullName = File.FullName + ".backup";
string backupFullName = file.FullName + ".backup";
// Check to see if the backup file already exists
if (System.IO.File.Exists(backupFullName))
{
// It does, so remove it.
Console.WriteLine("Deleting {0}", backupFullName);
System.IO.File.Delete(backupFullName);
}
// MainFile already exists, create a backup copy (.save.bak)
Console.WriteLine("Backing up {0} to {1}", File.FullName, backupFullName);
System.IO.File.Move(File.FullName, backupFullName);
// The file already exists, create a backup copy (.save.bak)
Console.WriteLine("Backing up {0} to {1}", file.FullName, backupFullName);
System.IO.File.Move(file.FullName, backupFullName);
}
else
{
// Remove the existing file
File.Delete();
file.Delete();
}

//try
{
Console.WriteLine("Writing to file: {0}...", File.FullName);
Console.WriteLine("Writing to file: {0}...", file.FullName);

using (StreamWriter sw = new StreamWriter(File.FullName))
using (StreamWriter sw = new StreamWriter(file.FullName))
{
// Process the stream with the Json Text Reader in to a JToken
using (JsonTextWriter jtw = new JsonTextWriter(sw))
Expand All @@ -302,9 +383,13 @@ public bool SaveFile(bool CreateBackup = true)
// Some error handling to be implemented here
}

// We should have some data in the array
// Clear the IsDirty flag.
IsDirty = false;

// Set the FileInfo to the new file
File = file;


return false;
}

Expand Down Expand Up @@ -332,29 +417,12 @@ public virtual bool Close()
}
}

/// <summary>
/// Does nothing - This method is a stub to be overridden by derived classes.
/// </summary>
public virtual void Serialise() => throw new NotImplementedException
("This method is a stub to be overridden by derived classes.");

/// <summary>
/// Does nothing - This method is a stub to be overridden by derived classes.
/// </summary>
public virtual void Deserialise() => throw new NotImplementedException
("This method is a stub to be overridden by derived classes.");

/// <summary>
/// Handles changes to the JData object.
/// </summary>
protected virtual void ProcessChangedJData()
{
if (AutoDeserialiseOnJdataModification)
{
// Triggered de-serialisation.
Deserialise();
}
}





/// <summary>
///
Expand Down Expand Up @@ -383,6 +451,7 @@ protected virtual void ProcessChangedJData()
protected bool _isDirty = false;
protected bool _readOnlyOverride = true;
protected bool _logToDebug = true;
private FileInfo _file = null;

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,23 @@ public int? OrderID
/// <summary>
/// Whether this docking port is locked (and not advertised to the docking system.)
/// </summary>
/// <remarks>
/// Defaults to unlocked, and converts nulls to unlocked.
/// </remarks>
[JsonProperty]
public bool? Locked
{
get => _locked;
get
{
if (_locked == null) return false;
else return (bool)_locked;
}
set
{
if (_locked != value)
{
_locked = value;
if (value == null) _locked = false;
else _locked = value;
}
}
}
Expand Down Expand Up @@ -502,7 +510,7 @@ public void AttemptSetOrderIDFromPortName()
private DockingPortType? _dockedPortName = null;
private BlueprintDockingPort _dockedPort = null;
private int? _orderID = null;
private bool? _locked = null;
private bool? _locked = false;
private StructureSceneID _structureSceneID;

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public StructureSceneID? SceneID
// public bool ShouldSerializeSceneName() { return OwnerStructure == null || (OwnerStructure != null && OwnerStructure.IsTemplate) ? true : false; }

[JsonProperty]
public String StructureType
public string StructureType
{
get => SceneID?.GetEnumDescription(); // ?? HEBlueprintStructureSceneID.Unspecified.ToString();
set
Expand Down Expand Up @@ -262,7 +262,7 @@ public HEBlueprintStructureType? StructureType
/// <returns></returns>
public BlueprintDockingPort GetDockingPort(string name)
{
if (name == null || name == String.Empty || !(DockingPorts.Count > 0)) return null;
if (name == null || name == string.Empty || !(DockingPorts.Count > 0)) return null;

IEnumerable<BlueprintDockingPort> results = DockingPorts.
Where(f => f.PortName.ToString() == name);
Expand Down Expand Up @@ -301,6 +301,19 @@ public BlueprintDockingPort GetDockingPort(BlueprintStructure dockedStructure)
return results.Count() == 1 ? results.Single() : null;
}


public BlueprintDockingPort GetDockingPortByOrderID(int orderID)
{
//if (orderID > 0 || !(DockingPorts.Count > 0)) return null;

IEnumerable<BlueprintDockingPort> results = DockingPorts.
Where(f => f.OrderID == orderID);

return results.Count() == 1 ? results.Single() : null;

}


/// <summary>
/// Gets the Structure Root for this structure.
/// </summary>
Expand Down
Loading

0 comments on commit f42d68b

Please sign in to comment.