implemented ExhibitionJsonPreloader
This commit is contained in:
@@ -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 = TextElement.BuildFromXmlNode(exhibitionDirectory, descrList);
|
|
||||||
newExhibit.Description = exhibitDescr;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load Title Text
|
TextElement exhibitDescr = new TextElement();
|
||||||
XmlNodeList titleList = exhibitNode.SelectNodes("title/text");
|
TextElement exhibitTitle = new TextElement();
|
||||||
if (titleList != null && titleList.Count > 0)
|
foreach (var exhibitPerLang in exhibits)
|
||||||
{
|
{
|
||||||
TextElement exhibitTitle = TextElement.BuildFromXmlNode(exhibitionDirectory, titleList);
|
var exhibit = exhibitPerLang.Value.FirstOrDefault(e => e.Id == exhibitId);
|
||||||
var fontSize = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/font"), "size");
|
if (exhibit == null) continue;
|
||||||
var boxHeight = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/dimensions"), "height");
|
|
||||||
var boxWidth = GetFloatFromXmlNode(exhibitNode.SelectSingleNode("title/dimensions"), "width");
|
exhibitDescr.AddTextElement(exhibitPerLang.Key, TextEntryType.Inline, exhibit.Description);
|
||||||
|
exhibitTitle.AddTextElement(exhibitPerLang.Key, TextEntryType.Inline, exhibit.Title);
|
||||||
|
}
|
||||||
|
newExhibit.Description = exhibitDescr;
|
||||||
|
|
||||||
|
//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
|
PreloadedExhibitTitle preTitle = new PreloadedExhibitTitle
|
||||||
{
|
{
|
||||||
Text = exhibitTitle,
|
Text = exhibitTitle,
|
||||||
Position = GetVector3FromXmlNode(exhibitNode.SelectSingleNode("title/position")),
|
Position = position,
|
||||||
Rotation = GetVector3FromXmlNode(exhibitNode.SelectSingleNode("title/rotation")),
|
Rotation = rotation,
|
||||||
FontSize = fontSize ?? 20,
|
FontSize = fontSize,
|
||||||
BoxWidth = boxWidth ?? 200,
|
BoxWidth = boxWidth,
|
||||||
BoxHeight = boxHeight ?? 25
|
BoxHeight = boxHeight
|
||||||
};
|
};
|
||||||
newExhibit.Title = preTitle;
|
newExhibit.Title = preTitle;
|
||||||
}
|
|
||||||
|
|
||||||
|
var pois = exhibits
|
||||||
|
.SelectMany(e => e.Value)
|
||||||
|
.Where(e => e.Id == exhibitId)
|
||||||
|
.SelectMany(e => e.Pois)
|
||||||
|
.Distinct()
|
||||||
|
.ToList();
|
||||||
|
|
||||||
// Load POI References
|
foreach (var poi in pois)
|
||||||
XmlNodeList poiRefList = exhibitNode.SelectNodes("poiList/poi");
|
|
||||||
if (poiRefList != null && poiRefList.Count > 0)
|
|
||||||
{
|
{
|
||||||
foreach (XmlNode poiRefEntry in poiRefList)
|
var poiReference = new PreloadedPoiReference
|
||||||
{
|
{
|
||||||
if (poiRefEntry.Attributes == null || poiRefEntry.Attributes["ref-id"] == null)
|
ReferencedId = poi.Id,
|
||||||
continue;
|
Position = new Vector3(poi.X, 0, poi.Y),
|
||||||
|
Rotation = Vector3.zero,
|
||||||
PreloadedPoiReference poiReference =
|
Scale = Vector3.one
|
||||||
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);
|
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -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; }
|
||||||
|
|||||||
Reference in New Issue
Block a user