Implemented PoiJsonPreloader

This commit is contained in:
2018-09-26 18:03:43 +02:00
parent 0985518be2
commit 225fc7702c

View File

@@ -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<PoiData> PreloadPoi(string poiDirectory)
{
throw new System.NotImplementedException();
string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(poiDirectory);
List<PoiData> preloadedPois = new List<PoiData>();
Dictionary<string, List<Exhibit>> exhibits = new Dictionary<string, List<Exhibit>>();
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<List<Exhibit>>(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<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()));
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> { 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;
}
}
}