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.Globalization;
using System.IO;
using System.Linq;
using System.Xml;
using Newtonsoft.Json;
using RothenburgAR.Common;
using RothenburgAR.Updater;
using UnityEngine;
namespace RothenburgAR.Exhibition
{
public class ExhibitionJsonPreloader : IExhibitionPreloader
{
public bool CanLoadExhibitionDirectory(string exhibitionDirectory)
{
return false;
if (!Directory.Exists(exhibitionDirectory))
return false;
string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(exhibitionDirectory);
if (!File.Exists(xmlFilePath))
return false;
if (!File.Exists(Path.Combine(exhibitionDirectory, "tracker.xml")))
return false;
if (!File.Exists(Path.Combine(exhibitionDirectory, "tracker.dat")))
return false;
var languageDirs = new DirectoryInfo(exhibitionDirectory).GetDirectories().Select(d => d.Name).ToList();
foreach (var dir in languageDirs)
{
var metaFilePath = PathHelper.CombinePaths(exhibitionDirectory, dir, "meta.json");
if (!File.Exists(metaFilePath)) return false;
}
// Todo: More validations
return true;
}
public PreloadedExhibition PreloadExhibition(string exhibitionDirectory)
{
throw new System.NotImplementedException();
string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(exhibitionDirectory);
//throw new System.NotImplementedException();
if (!File.Exists(xmlFilePath))
throw new FileNotFoundException(xmlFilePath);
Dictionary<string, List<Exhibit>> exhibits = new Dictionary<string, List<Exhibit>>();
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
var languageDirs = new DirectoryInfo(exhibitionDirectory).GetDirectories().Select(d => d.Name).ToList();
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
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;
var exhibitIds = exhibits.Values.SelectMany(l => l.Select(e => e.Id)).Distinct().ToList();
string exhibitionId = new DirectoryInfo(exhibitionDirectory).Name;
PreloadedExhibition resultExhibition = new PreloadedExhibition
{
ContainedExhibits = new List<PreloadedExhibit>(),
@@ -54,137 +54,73 @@ namespace RothenburgAR.Exhibition
DatasetPath = exhibitionDirectory
};
var exhibitNodes = doc.SelectNodes("//exhibition/exhibit");
foreach (XmlNode exhibitNode in exhibitNodes)
foreach (var exhibitId in exhibitIds)
{
if (exhibitNode.Attributes == null || exhibitNode.Attributes["id"] == null)
{
// Todo: Log error
continue;
}
var newExhibit = PreloadExhibit(exhibitionDirectory, exhibitNode);
var newExhibit = PreloadExhibit(exhibits, exhibitId);
resultExhibition.ContainedExhibits.Add(newExhibit);
}
return resultExhibition;
}
private PreloadedExhibit PreloadExhibit(string exhibitionDirectory, XmlNode exhibitNode)
private PreloadedExhibit PreloadExhibit(Dictionary<string, List<Exhibit>> exhibits, string exhibitId)
{
PreloadedExhibit newExhibit = new PreloadedExhibit();
newExhibit.ID = exhibitNode.Attributes["id"].Value;
newExhibit.ID = exhibitId;
newExhibit.ReferencedPoiEntries = new List<PreloadedPoiReference>();
// 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);
newExhibit.Description = exhibitDescr;
var exhibit = exhibitPerLang.Value.FirstOrDefault(e => e.Id == exhibitId);
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
XmlNodeList titleList = exhibitNode.SelectNodes("title/text");
if (titleList != null && titleList.Count > 0)
//TODO calculate sizes dynamically and remove these default values
var position = new Vector3(0, .1f, -.52f);
var rotation = Vector3.zero;
var fontSize = .7f;
var boxHeight = .25f;
var boxWidth = .75f;
PreloadedExhibitTitle preTitle = new PreloadedExhibitTitle
{
TextElement exhibitTitle = TextElement.BuildFromXmlNode(exhibitionDirectory, titleList);
var fontSize = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/font"), "size");
var boxHeight = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/dimensions"), "height");
var boxWidth = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/dimensions"), "width");
Text = exhibitTitle,
Position = position,
Rotation = rotation,
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,
Position = GetVector3FromXmlNode(exhibitNode.SelectSingleNode("title/position")),
Rotation = GetVector3FromXmlNode(exhibitNode.SelectSingleNode("title/rotation")),
FontSize = fontSize ?? 20,
BoxWidth = boxWidth ?? 200,
BoxHeight = boxHeight ?? 25
ReferencedId = poi.Id,
Position = new Vector3(poi.X, 0, poi.Y),
Rotation = Vector3.zero,
Scale = Vector3.one
};
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;
}
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;
}
Debug.Log("BUILD; " + preloadedExhibition.ID);
var exhibitionBehaviour = ExhibitionFactory.BuildExhibition(preloadedExhibition);
@@ -139,7 +138,6 @@ namespace RothenburgAR.Exhibition
catch (Exception e)
{
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))
return false;
var languageDirs = Directory.GetDirectories(poiDirectory).ToList();
var languageDirs = new DirectoryInfo(poiDirectory).GetDirectories().Select(d => d.Name).ToList();
foreach (var dir in languageDirs)
{
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();
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)
{

View File

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