Skip to content
Snippets Groups Projects
Commit 19ec0e4e authored by Marco Zimmer's avatar Marco Zimmer
Browse files

+ IJSONsavable<T> WIP: not working

parent 23ce7948
Branches
No related tags found
No related merge requests found
......@@ -106,6 +106,7 @@ public class AnimationEventWithUris : UnityEvent<List<string>> { }
public enum Directories
{
misc,
Stages,
ValidationSets,
FactStateMachines,
......
......@@ -99,9 +99,12 @@ public abstract class Fact
public GameObject Representation;
/// <value>
/// [ClassName] for JSON de-/serialization
/// [ClassName] for JSON de-/serialization.
/// Set in every non-abstract subclass of Fact.
/// Also add JsonSubtypes.KnownSubType decorator for deserialization to Fact!
/// </value>
public new string s_type;
[JsonProperty]
protected static readonly /*new*/ string s_type = "ERROR: set s_type in T:Fact"; // In the subtype! NOT here!
/// <value>
/// Unique Id. e.g.: MMT URI
......@@ -482,7 +485,8 @@ protected override bool EquivalentWrapped(AbstractLineFact f1, AbstractLineFact
public class PointFact : FactWrappedCRTP<PointFact>
{
/// \copydoc Fact.s_type
public new string s_type = "PointFact";
[JsonProperty]
protected static readonly new string s_type = "PointFact";
/// <summary> Position </summary>
public Vector3 Point;
......@@ -615,7 +619,8 @@ protected override bool EquivalentWrapped(PointFact f1, PointFact f2)
public class LineFact : AbstractLineFactWrappedCRTP<LineFact>
{
/// \copydoc Fact.s_type
public new string s_type = "LineFact";
[JsonProperty]
protected static readonly new string s_type = "LineFact";
/// <summary> Distance between <see cref="AbstractLineFact.Pid1"/> and <see cref="AbstractLineFact.Pid2"/></summary>
public float Distance;
......@@ -740,7 +745,8 @@ private void SetDistance()
public class RayFact : AbstractLineFactWrappedCRTP<RayFact>
{
/// \copydoc Fact.s_type
public new string s_type = "RayFact";
[JsonProperty]
protected static readonly new string s_type = "RayFact";
/// <summary> \copydoc Fact.Fact </summary>
public RayFact() : base() { }
......@@ -843,7 +849,8 @@ protected override bool EquivalentWrapped(RayFact f1, RayFact f2)
public class OnLineFact : FactWrappedCRTP<OnLineFact>
{
/// \copydoc Fact.s_type
public new string s_type = "OnLineFact";
[JsonProperty]
protected static readonly new string s_type = "OnLineFact";
public string
/// <summary> <see cref="PointFact"/>.<see cref="Fact.Id">Id</see> </summary>
......@@ -1000,7 +1007,8 @@ protected override bool EquivalentWrapped(OnLineFact f1, OnLineFact f2)
public class AngleFact : FactWrappedCRTP<AngleFact>
{
/// \copydoc Fact.s_type
public new string s_type = "AngleFact";
[JsonProperty]
protected static readonly new string s_type = "AngleFact";
/// @{ <summary>
/// One <see cref="Fact.Id">Id</see> of three <see cref="PointFact">PointFacts</see> defining Angle [<see cref="Pid1"/>, <see cref="Pid2"/>, <see cref="Pid3"/>].
......
......@@ -10,7 +10,7 @@
/// <summary>
/// Solution of a <see cref="Stage"/>
/// </summary>
public class SolutionOrganizer : FactOrganizer
public class SolutionOrganizer : FactOrganizer, IJSONsavable<SolutionOrganizer>
{
/// @{ <summary> adds to the end of the file name of a </summary>
private const string
......@@ -147,62 +147,30 @@ public List<Fact> getMasterFactsByIndex (int i)
/// @{
/// TODO? move to interface?
/// TODO: document
public new void store(string name, List<Directories> hierarchie = null, bool use_install_folder = false, bool overwrite = true)
bool IJSONsavable<SolutionOrganizer>.store(List<Directories> hierarchie, string name, bool use_install_folder, bool overwrite)
{
hierarchie ??= new List<Directories>();
hierarchie.AddRange(hierVal.AsEnumerable());
string path_o = path;
path = CreatePathToFile(out bool exists, name + endingVal, "JSON", hierarchie, use_install_folder);
hierarchie.RemoveRange(hierarchie.Count - hierVal.Count, hierVal.Count);
if (exists && !overwrite)
{
path = path_o;
return;
return (this as IJSONsavable<SolutionOrganizer>).store(hierarchie, name + endingVal, use_install_folder, overwrite);
}
JSONManager.WriteToJsonFile(path, this);
path = path_o;
}
public static bool load(ref SolutionOrganizer set, bool draw, string name, List<Directories> hierarchie = null, bool use_install_folder = false)
bool IJSONsavable<SolutionOrganizer>.load(List<Directories> hierarchie, string name, out SolutionOrganizer payload, bool use_install_folder)
{
hierarchie ??= new List<Directories>();
hierarchie.AddRange(hierVal.AsEnumerable());
string path = CreatePathToFile(out bool loadable, name + endingVal, "JSON", hierarchie, use_install_folder);
payload = null;
hierarchie.RemoveRange(hierarchie.Count - hierVal.Count, hierVal.Count);
if (!loadable)
if (!(this as IJSONsavable<SolutionOrganizer>).load(hierarchie, name, out SolutionOrganizer JsonTmp, use_install_folder))
return false;
FactOrganizer save = StageStatic.stage.factState;
StageStatic.stage.factState = new SolutionOrganizer(false) as FactOrganizer;
SolutionOrganizer JsonTmp = JSONManager.ReadFromJsonFile <SolutionOrganizer> (path);
ReInitializeFactOrganizer(ref StageStatic.stage.player_record.factState, JsonTmp, draw, out Dictionary<string, string> old_to_new);
set = (SolutionOrganizer)StageStatic.stage.factState;
set.path = path;
StageStatic.stage.factState = save;
payload = ReInitializeFactOrganizer(JsonTmp, false, out Dictionary<string, string> old_to_new)
as SolutionOrganizer;
payload.path = path;
foreach (var element in JsonTmp.ValidationSet)
// Parse and add
{
element.MasterIDs = new HashSet<string>(element.MasterIDs.Select(k => old_to_new[k]));
set.ValidationSet.Add(element);
payload.ValidationSet.Add(element);
}
return true;
}
public new void delete()
{
base.delete();
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
}
/// @}
}
......@@ -8,6 +8,77 @@
using System.Collections;
using UnityEngine;
using Newtonsoft.Json.Linq;
using static CommunicationEvents;
// I would go for static virtual methods, but C#9 does not allow me...
// static methods cannot be overwritten -> virtual
public interface IJSONsavable<T> where T : IJSONsavable<T>, new()
{
// stand in for non static methods
public static readonly IJSONsavable<T> Instance = new T();
public string path { get; set; }
protected static List<Directories>
hierarchie = new List<Directories> { Directories.misc };
public bool store(List<Directories> hierarchie, string name, bool use_install_folder = false, bool overwrite = true)
{
return store(hierarchie, name, this, use_install_folder, overwrite);
}
public virtual bool store(List<Directories> hierarchie, string name, IJSONsavable<T> payload, bool use_install_folder = false, bool overwrite = true)
{
hierarchie ??= new List<Directories>();
List<Directories> new_hierarchie = hierarchie.Concat(IJSONsavable<T>.hierarchie) as List<Directories>;
string path = CreatePathToFile(out bool exists, name, "JSON", new_hierarchie, use_install_folder);
if (exists && !overwrite)
return false;
JSONManager.WriteToJsonFile(path, payload);
return true;
}
public virtual bool load(List<Directories> hierarchie, string name, out T payload, bool use_install_folder = false)
{
payload = default(T);
hierarchie ??= new List<Directories>();
List<Directories> new_hierarchie = hierarchie.Concat(IJSONsavable<T>.hierarchie) as List<Directories>;
string path = CreatePathToFile(out bool loadable, name, "JSON", new_hierarchie, use_install_folder);
if (!loadable)
return false;
payload = JSONManager.ReadFromJsonFile<T>(path);
return true;
}
public virtual bool delete(List<Directories> hierarchie, string name, bool use_install_folder = false)
{
hierarchie ??= new List<Directories>();
List<Directories> new_hierarchie = hierarchie.Concat(IJSONsavable<T>.hierarchie) as List<Directories>;
string path = CreatePathToFile(out bool _, name, "JSON", new_hierarchie, use_install_folder);
return delete(path);
}
public virtual bool delete(string path)
{
if (!File.Exists(path))
return false;
File.Delete(path);
return true;
}
public virtual bool delete()
{
return this.delete(path);
}
}
public class MMTURICollection
{
......
fileFormatVersion: 2
guid: 94e80c57976c35c4ab7456cc01dd7a40
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
......@@ -5,6 +5,12 @@ EditorUserSettings:
m_ObjectHideFlags: 0
serializedVersion: 4
m_ConfigSettings:
RecentlyUsedSceneGuid-0:
value: 0502505152005e020c0d0e2446275e44144f19287f707e362c7c4b60b2b9353c
flags: 0
RecentlyUsedSceneGuid-1:
value: 57505505560608585a56557116730644404e4d7b7c7b7562787e4f66e4b1313e
flags: 0
RecentlyUsedScenePath-0:
value: 22424703114646680e0b0227036c721518021d39630928343f162e27e3f22076f7e93ffdfe
flags: 0
......@@ -43,9 +49,13 @@ EditorUserSettings:
m_VCDebugCmd: 0
m_VCDebugOut: 0
m_SemanticMergeMode: 2
m_DesiredImportWorkerCount: 4
m_StandbyImportWorkerCount: 2
m_IdleImportWorkerShutdownDelay: 60000
m_VCShowFailedCheckout: 1
m_VCOverwriteFailedCheckoutAssets: 1
m_VCProjectOverlayIcons: 1
m_VCHierarchyOverlayIcons: 1
m_VCOtherOverlayIcons: 1
m_VCAllowAsyncUpdate: 1
m_ArtifactGarbageCollection: 1
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment