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.IO;
using System.Linq; 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.PointOfInterest namespace RothenburgAR.PointOfInterest
@@ -12,9 +14,6 @@ namespace RothenburgAR.PointOfInterest
{ {
public bool CanLoadPoiDirectory(string poiDirectory) public bool CanLoadPoiDirectory(string poiDirectory)
{ {
//TODO remove
return false;
if (!Directory.Exists(poiDirectory)) if (!Directory.Exists(poiDirectory))
return false; return false;
@@ -32,91 +31,65 @@ namespace RothenburgAR.PointOfInterest
public List<PoiData> PreloadPoi(string poiDirectory) public List<PoiData> PreloadPoi(string poiDirectory)
{ {
throw new System.NotImplementedException(); List<PoiData> preloadedPois = new List<PoiData>();
string xmlFilePath = PathHelper.GetXmlPathFromDirectoryPath(poiDirectory); Dictionary<string, List<Exhibit>> exhibits = new Dictionary<string, List<Exhibit>>();
if (!File.Exists(xmlFilePath)) var languageDirs = new DirectoryInfo(poiDirectory).GetDirectories().Select(d => d.Name).ToList();
throw new FileNotFoundException(xmlFilePath); foreach (var lang in languageDirs)
{
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);
}
XmlDocument doc = new XmlDocument(); var poiIds = exhibits.Values.SelectMany(l => l.SelectMany(e => e.Pois.Select(p => p.Id))).Distinct().ToList();
doc.Load(xmlFilePath);
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()));
// Read the ID from the xml foreach (var poiID in poiIds)
if (doc.DocumentElement == null || doc.DocumentElement.Attributes == null || {
doc.DocumentElement.Attributes["id"] == null) TextElement descElement = new TextElement();
throw new InvalidXMLException("Could not read doc.DocumentElement.Attributes['id] "); TextElement titleElement = new TextElement();
string mediaID = null;
string poiId = doc.DocumentElement.Attributes["id"].Value; foreach (var poiPerLang in poisPerLang)
{
// Read the description from the xml var poi = poiPerLang.Value.FirstOrDefault(p => p.Id == poiID);
XmlNodeList descNodes = doc.SelectNodes("//poi/description/text"); if (poi == null) continue;
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);
descElement.AddTextElement(poiPerLang.Key, TextEntryType.Inline, poi.Description);
titleElement.AddTextElement(poiPerLang.Key, TextEntryType.Inline, poi.Title);
mediaID = poi.MediaId;
}
PoiData poiData = new PoiData PoiData poiData = new PoiData
{ {
ID = poiId, ID = poiID,
Description = descElement, Description = descElement,
Title = titleElement, Title = titleElement,
SourcePath = poiDirectory SourcePath = poiDirectory
}; };
// Read the model value if (mediaID != null)
XmlNode modelNode = doc.SelectSingleNode("//poi/model");
if (modelNode != null)
{ {
var pathNode = modelNode.SelectSingleNode("path"); var modelDescription = new PoiModelDescription();
var scaleNode = modelNode.SelectSingleNode("scale"); modelDescription.ModelPath = mediaID;
var rotationNode = modelNode.SelectSingleNode("rotation");
var positionNode = modelNode.SelectSingleNode("position");
var poiModelDescription = new PoiModelDescription //TODO dont have model size information, find a way to scale model to fit into view?
{ modelDescription.ModelPrefabName = string.Empty;
ModelPath = Path.Combine(poiDirectory, pathNode.Attributes["value"].Value), modelDescription.Scale = Vector3.one;
ModelPrefabName = pathNode.Attributes["prefabName"].Value, modelDescription.Position = Vector3.zero;
Scale = GetVector3FromXmlNode(scaleNode).GetValueOrDefault(Vector3.one), modelDescription.Rotation = Vector3.zero;
Rotation = GetVector3FromXmlNode(rotationNode).GetValueOrDefault(Vector3.zero),
Position = GetVector3FromXmlNode(positionNode).GetValueOrDefault(Vector3.zero) poiData.ModelDescription = modelDescription;
};
poiData.ModelDescription = poiModelDescription;
}
return new List<PoiData> { poiData };
} }
private Vector3? GetVector3FromXmlNode(XmlNode node) preloadedPois.Add(poiData);
{
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;
} }
return preloadedPois;
} }
} }
} }