Skip to content

Commit

Permalink
Make sure setupwizard is shown at the right moment, and that loading …
Browse files Browse the repository at this point in the history
…is done in the right order
  • Loading branch information
mvriel committed Aug 17, 2023
1 parent e430454 commit c68550c
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 29 deletions.
55 changes: 38 additions & 17 deletions Assets/Scripts/Configuration/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Linq;
using Netherlands3D.Coordinates;
using Netherlands3D.Twin.Features;
using Newtonsoft.Json;
using SimpleJSON;
using UnityEngine;
using UnityEngine.Events;
Expand All @@ -19,7 +18,6 @@ public class Configuration : ScriptableObject, IConfiguration
[SerializeField] private string title = "Amersfoort";
[SerializeField] private Coordinate origin = new(CoordinateSystem.RD, 161088, 503050, 300);

[JsonProperty(ItemIsReference = true)]
[SerializeField] public List<Feature> Features = new();

public string Title
Expand All @@ -42,15 +40,21 @@ public Coordinate Origin
}
}

[JsonIgnore] public UnityEvent<Coordinate> OnOriginChanged = new();
[JsonIgnore] public UnityEvent<string> OnTitleChanged = new();
/// <summary>
/// By default, we should start the setup wizard to configure the twin, unless configuration was successfully
/// loaded from the URL or from the Configuration File.
/// </summary>
public bool ShouldStartSetup { get; set; } = true;

public UnityEvent<Coordinate> OnOriginChanged = new();
public UnityEvent<string> OnTitleChanged = new();

/// <summary>
/// Overwrites the contents of this Scriptable Object with the serialized JSON file at the provided location.
/// </summary>
public IEnumerator PopulateFromFile(string externalConfigFilePath)
{
Debug.Log($"Attempting to load configuration from ${externalConfigFilePath}");
Debug.Log($"Attempting to load configuration from {externalConfigFilePath}");
using UnityWebRequest request = UnityWebRequest.Get(externalConfigFilePath);
yield return request.SendWebRequest();
if (request.result == UnityWebRequest.Result.Success)
Expand All @@ -59,7 +63,8 @@ public IEnumerator PopulateFromFile(string externalConfigFilePath)
var json = request.downloadHandler.text;

// populate object and when settings are missing, use the defaults from the provided object
JsonConvert.PopulateObject(json, this);
Populate(JSON.Parse(json));
ShouldStartSetup = false;
}
else
{
Expand All @@ -69,30 +74,37 @@ public IEnumerator PopulateFromFile(string externalConfigFilePath)
yield return null;
}

public bool Populate(Uri url)
public void Populate(Uri url)
{
var queryParameters = new NameValueCollection();
url.Query.ParseAsQueryString(queryParameters);

return Populate(queryParameters);
Populate(queryParameters);
}

public bool Populate(NameValueCollection queryParameters)
public void Populate(NameValueCollection queryParameters)
{
if (UrlContainsConfiguration(queryParameters) == false)
if (UrlContainsConfiguration(queryParameters))
{
return false;
ShouldStartSetup = false;
}

var originFromQueryString = queryParameters.Get("origin");
if (string.IsNullOrEmpty(originFromQueryString) == false)
{
LoadOriginFromString(originFromQueryString);
}

var featuresFromQueryString = queryParameters.Get("features");
if (featuresFromQueryString != null)
{
LoadFeaturesFromString(featuresFromQueryString);
}

LoadOriginFromString(queryParameters.Get("origin"));
LoadFeaturesFromString(queryParameters.Get("features"));
foreach (var feature in Features)
{
var config = feature.configuration as IConfiguration;
config?.Populate(queryParameters);
}

return true;
}

public string ToQueryString()
Expand All @@ -118,19 +130,26 @@ public string ToQueryString()

public void Populate(JSONNode jsonNode)
{
Title = jsonNode["title"];
if (jsonNode["title"])
{
Title = jsonNode["title"];
}

Origin = new Coordinate(
jsonNode["origin"]["epsg"],
jsonNode["origin"]["x"],
jsonNode["origin"]["y"],
jsonNode["origin"]["z"]
);
Debug.Log($"Set origin '{Origin}' from Configuration file");

foreach (var element in jsonNode["features"])
{
var feature = Features.FirstOrDefault(feature => feature.Id == element.Key);
if (!feature) continue;

feature.Populate(element.Value);
if (feature.IsEnabled) Debug.Log($"Enabled feature '{feature.Id}' from Configuration file");
}
}

Expand Down Expand Up @@ -173,6 +192,7 @@ private void LoadOriginFromString(string origin)
int.TryParse(originParts[2].Trim(), out int z);

Origin = new Coordinate(CoordinateSystem.RD, x, y, z);
Debug.Log($"Set origin '{Origin}' from URL");
}

private void LoadFeaturesFromString(string features)
Expand All @@ -181,6 +201,7 @@ private void LoadFeaturesFromString(string features)
foreach (var feature in Features)
{
feature.IsEnabled = featureIdentifiers.Contains(feature.Id);
if (feature.IsEnabled) Debug.Log($"Enabled feature '{feature.Id}' from URL");
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion Assets/Scripts/Configuration/Configurator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ public IEnumerator Execute()
url = debugUrl;
#endif

if (string.IsNullOrEmpty(url) || configuration.Populate(new Uri(url)) == false)
if (string.IsNullOrEmpty(url) == false)
{
configuration.Populate(new Uri(url));
}

if (configuration.ShouldStartSetup)
{
StartSetup();
}
Expand Down
13 changes: 7 additions & 6 deletions Assets/Scripts/Configuration/Indicators/Configuration.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Specialized;
using Newtonsoft.Json;
using Netherlands3D.Twin.Features;
using SimpleJSON;
using UnityEngine;
using UnityEngine.Events;
Expand All @@ -24,23 +24,24 @@ public string DossierId
}
}

public bool Populate(NameValueCollection queryParameters)
public void Populate(NameValueCollection queryParameters)
{
var id = queryParameters.Get("indicators.dossier");
if (id == null) return false;
if (id == null) return;

Debug.Log($"Set dossier id '{id}' from URL");
DossierId = id;

return true;
}

public string ToQueryString()
{
return $"indicators.dossier=${dossierId}";
return $"indicators.dossier={dossierId}";
}

public void Populate(JSONNode jsonNode)
{
if (string.IsNullOrEmpty(jsonNode["dossierId"])) return;

DossierId = jsonNode["dossierId"];
}

Expand Down
6 changes: 6 additions & 0 deletions Assets/Scripts/Configuration/Indicators/DossierLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ private void OnDisable()

private void OnDossierStartLoading(string dossierId)
{
if (string.IsNullOrEmpty(dossierId))
{
dossier.Close();
return;
}

StartCoroutine(dossier.Open(dossierId));
}

Expand Down
10 changes: 10 additions & 0 deletions Packages/eu.netherlands3d.twin-features/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ All notable changes to this package will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Added

- IConfiguration interface to declare configuration per feature including SimpleJSON mapping and QueryParameter mapping
- IQueryStringMapper to help populate feature configuration from a URI string
- ISimpleJsonMapper to help populate feature configuration from a JSON object

## [1.0.0] - 2023-06-13

### Changes

- Initial release
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Netherlands3D.Twin.Features;

namespace Netherlands3D.Twin
namespace Netherlands3D.Twin.Features
{
public interface IConfiguration : ISimpleJsonMapper, IQueryStringMapper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using System.Collections.Specialized;

namespace Netherlands3D.Twin
namespace Netherlands3D.Twin.Features
{
public interface IQueryStringMapper
{
bool Populate(NameValueCollection queryParameters);
void Populate(NameValueCollection queryParameters);
string ToQueryString();
}
}

0 comments on commit c68550c

Please sign in to comment.