implemented ExhibitionJsonPreloader

This commit is contained in:
2018-09-26 20:50:06 +02:00
parent 225fc7702c
commit 0d6d8141fa
4 changed files with 73 additions and 138 deletions

View File

@@ -1,52 +1,52 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.IO; using System.IO;
using System.Linq;
using System.Xml; using System.Xml;
using Newtonsoft.Json;
using RothenburgAR.Common; using RothenburgAR.Common;
using RothenburgAR.Updater;
using UnityEngine; using UnityEngine;
namespace RothenburgAR.Exhibition namespace RothenburgAR.Exhibition
{ {
public class ExhibitionJsonPreloader : IExhibitionPreloader public class ExhibitionJsonPreloader : IExhibitionPreloader
{ {
public bool CanLoadExhibitionDirectory(string exhibitionDirectory) public bool CanLoadExhibitionDirectory(string exhibitionDirectory)
{ {
return false;
if (!Directory.Exists(exhibitionDirectory)) if (!Directory.Exists(exhibitionDirectory))
return false; return false;
string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(exhibitionDirectory); var languageDirs = new DirectoryInfo(exhibitionDirectory).GetDirectories().Select(d => d.Name).ToList();
foreach (var dir in languageDirs)
if (!File.Exists(xmlFilePath)) {
return false; var metaFilePath = PathHelper.CombinePaths(exhibitionDirectory, dir, "meta.json");
if (!File.Exists(Path.Combine(exhibitionDirectory, "tracker.xml"))) if (!File.Exists(metaFilePath)) return false;
return false; }
if (!File.Exists(Path.Combine(exhibitionDirectory, "tracker.dat")))
return false;
// Todo: More validations // Todo: More validations
return true; return true;
} }
public PreloadedExhibition PreloadExhibition(string exhibitionDirectory) public PreloadedExhibition PreloadExhibition(string exhibitionDirectory)
{ {
throw new System.NotImplementedException(); //throw new System.NotImplementedException();
string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(exhibitionDirectory);
if (!File.Exists(xmlFilePath)) Dictionary<string, List<Exhibit>> exhibits = new Dictionary<string, List<Exhibit>>();
throw new FileNotFoundException(xmlFilePath);
XmlDocument doc = new XmlDocument(); var languageDirs = new DirectoryInfo(exhibitionDirectory).GetDirectories().Select(d => d.Name).ToList();
doc.Load(xmlFilePath); foreach (var lang in languageDirs)
{
var metaFilePath = PathHelper.CombinePaths(exhibitionDirectory, lang, "meta.json");
if (!File.Exists(metaFilePath)) continue;
var exhibitList = JsonConvert.DeserializeObject<List<Exhibit>>(File.ReadAllText(metaFilePath, System.Text.Encoding.UTF8));
exhibits.Add(lang, exhibitList);
}
// Read the ID from the xml var exhibitIds = exhibits.Values.SelectMany(l => l.Select(e => e.Id)).Distinct().ToList();
if (doc.DocumentElement == null || doc.DocumentElement.Attributes == null ||
doc.DocumentElement.Attributes["id"] == null)
throw new InvalidXMLException("Could not read doc.DocumentElement.Attributes['id] ");
string exhibitionId = doc.DocumentElement.Attributes["id"].Value;
string exhibitionId = new DirectoryInfo(exhibitionDirectory).Name;
PreloadedExhibition resultExhibition = new PreloadedExhibition PreloadedExhibition resultExhibition = new PreloadedExhibition
{ {
ContainedExhibits = new List<PreloadedExhibit>(), ContainedExhibits = new List<PreloadedExhibit>(),
@@ -54,137 +54,73 @@ namespace RothenburgAR.Exhibition
DatasetPath = exhibitionDirectory DatasetPath = exhibitionDirectory
}; };
var exhibitNodes = doc.SelectNodes("//exhibition/exhibit"); foreach (var exhibitId in exhibitIds)
foreach (XmlNode exhibitNode in exhibitNodes)
{ {
if (exhibitNode.Attributes == null || exhibitNode.Attributes["id"] == null) var newExhibit = PreloadExhibit(exhibits, exhibitId);
{
// Todo: Log error
continue;
}
var newExhibit = PreloadExhibit(exhibitionDirectory, exhibitNode);
resultExhibition.ContainedExhibits.Add(newExhibit); resultExhibition.ContainedExhibits.Add(newExhibit);
} }
return resultExhibition; return resultExhibition;
} }
private PreloadedExhibit PreloadExhibit(string exhibitionDirectory, XmlNode exhibitNode) private PreloadedExhibit PreloadExhibit(Dictionary<string, List<Exhibit>> exhibits, string exhibitId)
{ {
PreloadedExhibit newExhibit = new PreloadedExhibit(); PreloadedExhibit newExhibit = new PreloadedExhibit();
newExhibit.ID = exhibitNode.Attributes["id"].Value;
newExhibit.ID = exhibitId;
newExhibit.ReferencedPoiEntries = new List<PreloadedPoiReference>(); newExhibit.ReferencedPoiEntries = new List<PreloadedPoiReference>();
// Load Description Text // Load Description Text
XmlNodeList descrList = exhibitNode.SelectNodes("description/text");
if (descrList != null && descrList.Count > 0) TextElement exhibitDescr = new TextElement();
TextElement exhibitTitle = new TextElement();
foreach (var exhibitPerLang in exhibits)
{ {
TextElement exhibitDescr = TextElement.BuildFromXmlNode(exhibitionDirectory, descrList); var exhibit = exhibitPerLang.Value.FirstOrDefault(e => e.Id == exhibitId);
newExhibit.Description = exhibitDescr; if (exhibit == null) continue;
exhibitDescr.AddTextElement(exhibitPerLang.Key, TextEntryType.Inline, exhibit.Description);
exhibitTitle.AddTextElement(exhibitPerLang.Key, TextEntryType.Inline, exhibit.Title);
} }
newExhibit.Description = exhibitDescr;
// Load Title Text //TODO calculate sizes dynamically and remove these default values
XmlNodeList titleList = exhibitNode.SelectNodes("title/text"); var position = new Vector3(0, .1f, -.52f);
if (titleList != null && titleList.Count > 0) var rotation = Vector3.zero;
var fontSize = .7f;
var boxHeight = .25f;
var boxWidth = .75f;
PreloadedExhibitTitle preTitle = new PreloadedExhibitTitle
{ {
TextElement exhibitTitle = TextElement.BuildFromXmlNode(exhibitionDirectory, titleList); Text = exhibitTitle,
var fontSize = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/font"), "size"); Position = position,
var boxHeight = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/dimensions"), "height"); Rotation = rotation,
var boxWidth = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/dimensions"), "width"); FontSize = fontSize,
BoxWidth = boxWidth,
BoxHeight = boxHeight
};
newExhibit.Title = preTitle;
PreloadedExhibitTitle preTitle = new PreloadedExhibitTitle var pois = exhibits
.SelectMany(e => e.Value)
.Where(e => e.Id == exhibitId)
.SelectMany(e => e.Pois)
.Distinct()
.ToList();
foreach (var poi in pois)
{
var poiReference = new PreloadedPoiReference
{ {
Text = exhibitTitle, ReferencedId = poi.Id,
Position = GetVector3FromXmlNode(exhibitNode.SelectSingleNode("title/position")), Position = new Vector3(poi.X, 0, poi.Y),
Rotation = GetVector3FromXmlNode(exhibitNode.SelectSingleNode("title/rotation")), Rotation = Vector3.zero,
FontSize = fontSize ?? 20, Scale = Vector3.one
BoxWidth = boxWidth ?? 200,
BoxHeight = boxHeight ?? 25
}; };
newExhibit.Title = preTitle; newExhibit.ReferencedPoiEntries.Add(poiReference);
} }
// Load POI References
XmlNodeList poiRefList = exhibitNode.SelectNodes("poiList/poi");
if (poiRefList != null && poiRefList.Count > 0)
{
foreach (XmlNode poiRefEntry in poiRefList)
{
if (poiRefEntry.Attributes == null || poiRefEntry.Attributes["ref-id"] == null)
continue;
PreloadedPoiReference poiReference =
new PreloadedPoiReference
{
ReferencedId = poiRefEntry.Attributes["ref-id"].Value,
Position = GetVector3FromXmlNode(poiRefEntry.SelectSingleNode("position")),
Rotation = GetVector3FromXmlNode(poiRefEntry.SelectSingleNode("rotation")),
Scale = GetVector3FromXmlNode(poiRefEntry.SelectSingleNode("scale"))
};
newExhibit.ReferencedPoiEntries.Add(poiReference);
}
}
return newExhibit; return newExhibit;
} }
private string GetStringFromXmlNode(XmlNode node, string key)
{
if (node == null)
return null;
if (node.Attributes == null)
return null;
if (node.Attributes[key] == null)
return null;
return node.Attributes[key].Value;
}
private float? GetFloatFromXmlNode(XmlNode node, string key)
{
var str = GetStringFromXmlNode(node, key);
if (str == null)
return null;
try
{
return float.Parse(str, CultureInfo.InvariantCulture.NumberFormat);
}
catch
{
return null;
}
}
private Vector3? GetVector3FromXmlNode(XmlNode node)
{
if (node == null)
return null;
if (node.Attributes == null)
return null;
if (node.Attributes["x"] == null)
return null;
if (node.Attributes["y"] == null)
return null;
if (node.Attributes["z"] == null)
return null;
try
{
float posX = float.Parse(node.Attributes["x"].Value,
CultureInfo.InvariantCulture.NumberFormat);
float posY = float.Parse(node.Attributes["y"].Value,
CultureInfo.InvariantCulture.NumberFormat);
float posZ = float.Parse(node.Attributes["z"].Value,
CultureInfo.InvariantCulture.NumberFormat);
return new Vector3(posX, posY, posZ);
}
catch
{
// TODO: log error
return null;
}
}
} }
} }

View File

@@ -129,7 +129,6 @@ namespace RothenburgAR.Exhibition
return; return;
} }
Debug.Log("BUILD; " + preloadedExhibition.ID); Debug.Log("BUILD; " + preloadedExhibition.ID);
var exhibitionBehaviour = ExhibitionFactory.BuildExhibition(preloadedExhibition); var exhibitionBehaviour = ExhibitionFactory.BuildExhibition(preloadedExhibition);
@@ -139,7 +138,6 @@ namespace RothenburgAR.Exhibition
catch (Exception e) catch (Exception e)
{ {
Debug.LogError("Could not load path '" + path + "'; Ignoring; Error: " + e.Message); Debug.LogError("Could not load path '" + path + "'; Ignoring; Error: " + e.Message);
throw;
} }
} }

View File

@@ -17,7 +17,7 @@ namespace RothenburgAR.PointOfInterest
if (!Directory.Exists(poiDirectory)) if (!Directory.Exists(poiDirectory))
return false; return false;
var languageDirs = Directory.GetDirectories(poiDirectory).ToList(); var languageDirs = new DirectoryInfo(poiDirectory).GetDirectories().Select(d => d.Name).ToList();
foreach (var dir in languageDirs) foreach (var dir in languageDirs)
{ {
var metaFilePath = PathHelper.CombinePaths(poiDirectory, dir, "meta.json"); var metaFilePath = PathHelper.CombinePaths(poiDirectory, dir, "meta.json");
@@ -46,7 +46,8 @@ namespace RothenburgAR.PointOfInterest
var poiIds = exhibits.Values.SelectMany(l => l.SelectMany(e => e.Pois.Select(p => p.Id))).Distinct().ToList(); var poiIds = exhibits.Values.SelectMany(l => l.SelectMany(e => e.Pois.Select(p => p.Id))).Distinct().ToList();
Dictionary<string, List<Poi>> poisPerLang = new Dictionary<string, List<Poi>>(); Dictionary<string, List<Poi>> poisPerLang = new Dictionary<string, List<Poi>>();
exhibits.Keys.ToList().ForEach(k => poisPerLang.Add(k, exhibits[k].SelectMany(l => l.Pois.Select(p => p)).Distinct().ToList())); exhibits.Keys.ToList().ForEach(k =>
poisPerLang.Add(k, exhibits[k].SelectMany(l => l.Pois.Select(p => p)).Distinct().ToList()));
foreach (var poiID in poiIds) foreach (var poiID in poiIds)
{ {

View File

@@ -116,10 +116,10 @@ namespace RothenburgAR.Updater
public string MediaId { get; set; } public string MediaId { get; set; }
[JsonProperty("x")] [JsonProperty("x")]
public double X { get; set; } public float X { get; set; }
[JsonProperty("y")] [JsonProperty("y")]
public double Y { get; set; } public float Y { get; set; }
[JsonProperty("createdTime")] [JsonProperty("createdTime")]
public long CreatedTime { get; set; } public long CreatedTime { get; set; }