From 225fc7702c085a941869518e7be861a45dbd0248 Mon Sep 17 00:00:00 2001 From: Matt Date: Wed, 26 Sep 2018 18:03:43 +0200 Subject: [PATCH] Implemented PoiJsonPreloader --- .../PointOfInterest/PoiJsonPreloader.cs | 127 +++++++----------- 1 file changed, 50 insertions(+), 77 deletions(-) diff --git a/Assets/RothenburgAR/Scripts/PointOfInterest/PoiJsonPreloader.cs b/Assets/RothenburgAR/Scripts/PointOfInterest/PoiJsonPreloader.cs index 4b27089..3813bd5 100644 --- a/Assets/RothenburgAR/Scripts/PointOfInterest/PoiJsonPreloader.cs +++ b/Assets/RothenburgAR/Scripts/PointOfInterest/PoiJsonPreloader.cs @@ -3,7 +3,9 @@ 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.PointOfInterest @@ -12,9 +14,6 @@ namespace RothenburgAR.PointOfInterest { public bool CanLoadPoiDirectory(string poiDirectory) { - //TODO remove - return false; - if (!Directory.Exists(poiDirectory)) return false; @@ -32,91 +31,65 @@ namespace RothenburgAR.PointOfInterest public List PreloadPoi(string poiDirectory) { - throw new System.NotImplementedException(); - string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(poiDirectory); + List preloadedPois = new List(); + Dictionary> exhibits = new Dictionary>(); - if (!File.Exists(xmlFilePath)) - throw new FileNotFoundException(xmlFilePath); - - XmlDocument doc = new XmlDocument(); - doc.Load(xmlFilePath); - - - // 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 poiId = doc.DocumentElement.Attributes["id"].Value; - - // Read the description from the xml - XmlNodeList descNodes = doc.SelectNodes("//poi/description/text"); - TextElement descElement = TextElement.BuildFromXmlNode(poiDirectory, descNodes); - - // Read the title from the xml - XmlNodeList titleNodes = doc.SelectNodes("//poi/title/text"); - TextElement titleElement = TextElement.BuildFromXmlNode(poiDirectory, titleNodes); - - - PoiData poiData = new PoiData + var languageDirs = new DirectoryInfo(poiDirectory).GetDirectories().Select(d => d.Name).ToList(); + foreach (var lang in languageDirs) { - ID = poiId, - Description = descElement, - Title = titleElement, - SourcePath = poiDirectory - }; + var metaFilePath = PathHelper.CombinePaths(poiDirectory, lang, "meta.json"); + if (!File.Exists(metaFilePath)) continue; + var exhibitList = JsonConvert.DeserializeObject>(File.ReadAllText(metaFilePath, System.Text.Encoding.UTF8)); + exhibits.Add(lang, exhibitList); + } - // Read the model value - XmlNode modelNode = doc.SelectSingleNode("//poi/model"); - if (modelNode != null) + var poiIds = exhibits.Values.SelectMany(l => l.SelectMany(e => e.Pois.Select(p => p.Id))).Distinct().ToList(); + + Dictionary> poisPerLang = new Dictionary>(); + exhibits.Keys.ToList().ForEach(k => poisPerLang.Add(k, exhibits[k].SelectMany(l => l.Pois.Select(p => p)).Distinct().ToList())); + + foreach (var poiID in poiIds) { - var pathNode = modelNode.SelectSingleNode("path"); - var scaleNode = modelNode.SelectSingleNode("scale"); - var rotationNode = modelNode.SelectSingleNode("rotation"); - var positionNode = modelNode.SelectSingleNode("position"); + TextElement descElement = new TextElement(); + TextElement titleElement = new TextElement(); + string mediaID = null; - var poiModelDescription = new PoiModelDescription + foreach (var poiPerLang in poisPerLang) { - ModelPath = Path.Combine(poiDirectory, pathNode.Attributes["value"].Value), - ModelPrefabName = pathNode.Attributes["prefabName"].Value, - Scale = GetVector3FromXmlNode(scaleNode).GetValueOrDefault(Vector3.one), - Rotation = GetVector3FromXmlNode(rotationNode).GetValueOrDefault(Vector3.zero), - Position = GetVector3FromXmlNode(positionNode).GetValueOrDefault(Vector3.zero) + var poi = poiPerLang.Value.FirstOrDefault(p => p.Id == poiID); + if (poi == null) continue; + + descElement.AddTextElement(poiPerLang.Key, TextEntryType.Inline, poi.Description); + titleElement.AddTextElement(poiPerLang.Key, TextEntryType.Inline, poi.Title); + mediaID = poi.MediaId; + } + + PoiData poiData = new PoiData + { + ID = poiID, + Description = descElement, + Title = titleElement, + SourcePath = poiDirectory }; - poiData.ModelDescription = poiModelDescription; - } - return new List { poiData }; - } - 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; + if (mediaID != null) + { + var modelDescription = new PoiModelDescription(); + modelDescription.ModelPath = mediaID; - 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); + //TODO dont have model size information, find a way to scale model to fit into view? + modelDescription.ModelPrefabName = string.Empty; + modelDescription.Scale = Vector3.one; + modelDescription.Position = Vector3.zero; + modelDescription.Rotation = Vector3.zero; - return new Vector3(posX, posY, posZ); - } - catch - { - // TODO: log error - return null; + poiData.ModelDescription = modelDescription; + } + + preloadedPois.Add(poiData); } + + return preloadedPois; } } } \ No newline at end of file