view api/Factories/Xml/WarFoundryXmlFactory.cs @ 17:a99d3b8466ba

Change "CreateFactory" method to "GetFactory" method to allow for caching no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Mon, 26 Jan 2009 20:29:37 +0000
parents 306558904c2a
children f9846f896df3
line wrap: on
line source

// This file (WarFoundryXmlFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
//
// The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections.Generic;
using System.Text;
using IBBoard;
using IBBoard.IO;
using IBBoard.Lang;
using IBBoard.Logging;
using IBBoard.Xml;
using IBBoard.WarFoundry.API.Requirements;
using IBBoard.WarFoundry.API.Objects;
using ICSharpCode.SharpZipLib.Zip;

namespace IBBoard.WarFoundry.API.Factories.Xml
{
	/// <summary>
	/// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load.
	/// </summary>
	public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory
	{
		private static WarFoundryXmlFactory factory;
		private Dictionary<IWarFoundryObject, XmlDocument> extraData = new Dictionary<IWarFoundryObject, XmlDocument>();
		private XmlResolver xmlResolver;

		public static AbstractNativeWarFoundryFactory GetFactory()
		{
			if (factory == null)
			{
				factory = new WarFoundryXmlFactory();
			}
			
			return factory;
		}
		
		protected WarFoundryXmlFactory() : base()
		{
			xmlResolver = new IBBXmlResolver(Constants.ExecutablePath);
		}
		
		protected override bool CheckCanFindArmyFileContent(ZipFile file)
		{
			return file.FindEntry("data.armyx", true) > -1;
		}
		
		protected override bool CheckCanFindSystemFileContent(ZipFile file)
		{
			return file.FindEntry("data.systemx", true) > -1;
		}
		
		protected override bool CheckCanFindRaceFileContent(ZipFile file)
		{
			return file.FindEntry("data.racex", true) > -1;
		}
		
		protected XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName)
		{
			XmlDocument doc = CreateXmlDocumentFromStream(stream);			
			XmlElement elem = (XmlElement)doc.LastChild;
			
			if (!elem.Name.Equals(elementName.Value))
			{
				throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name));
			}
			
			return elem;
		}
		
		protected override Stream GetArmyDataStream(ZipFile file)
		{
			return file.GetInputStream(file.FindEntry("data.armyx", true));
		}
		
		protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream)
		{
			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT);
			return CreateArmyFromElement(file, elem);
		}
		
		private Army CreateArmyFromElement(ZipFile file, XmlElement elem)
		{
			string name = elem.GetAttribute("name");
			string systemID = elem.GetAttribute("gameSystem");
			GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID);
			string raceID = elem.GetAttribute("race");
			Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID);
			string pointsString = elem.GetAttribute("maxPoints");
			int points = 0;
			
			try
			{
				points = int.Parse(pointsString);
			}
			catch(FormatException)
			{
				throw new FormatException("Attribute 'maxPoints' of army '"+name+"' was not a valid number");
			}
			
			Army army = new Army(race, name, points, file);
			extraData[army] = elem.OwnerDocument;
			return army;
		}

		protected override Stream GetGameSystemDataStream (ZipFile file)
		{
			return file.GetInputStream(file.FindEntry("data.systemx", true));
		}
		
		protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream)
		{
			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT);
			LogNotifier.Debug(GetType(), "Create GameSystem");
			return CreateSystemFromElement(file, elem);
		}
		
		private GameSystem CreateSystemFromElement(ZipFile file, XmlElement elem)
		{
			string id = elem.GetAttribute("id");
			string name = elem.GetAttribute("name");
			GameSystem system = new GameSystem(id, name, this);
			extraData[system] = elem.OwnerDocument;
			return system;
		}
		
		protected override Stream GetRaceDataStream (ZipFile file)
		{
			return file.GetInputStream(file.FindEntry("data.racex", true));
		}
		
		protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream)
		{
			XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT);
			LogNotifier.Debug(GetType(), "Create Race");
			return CreateRaceFromElement(file, elem);
		}
		
		private Race CreateRaceFromElement(ZipFile file, XmlElement elem)
		{
			string id = elem.GetAttribute("id");
			string subid = elem.GetAttribute("subid");
			string systemID = elem.GetAttribute("system");
			string name = elem.GetAttribute("name");
			Race race = new Race(id, subid, name, systemID, this);
			extraData[race] = elem.OwnerDocument;
			return race;
		}

		public override void CompleteLoading(IWarFoundryStagedLoadObject obj)
		{			
			LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID);
							
			if (obj is GameSystem)
			{
				CompleteLoading((GameSystem)obj);
			}
			else if (obj is Race)
			{
				CompleteLoading((Race)obj);
			}
		}
	
		public XmlNode GetExtraData(IWarFoundryObject obj)
		{
			XmlDocument extra = null;
			extraData.TryGetValue(obj, out extra);
			XmlNode node = null;
			
			if (extra !=null)
			{
				node = extra.LastChild;
			}
			
			return node;
		}
		
		public void CompleteLoading(GameSystem system)
		{
			if (system.IsFullyLoaded)
			{
				LogNotifier.DebugFormat(GetType(), "Object of type GameSystem with ID {0} is already fully loaded", system.ID);
				return;
			}
			
			XmlNode elem = GetExtraData(system);			
			XmlNode catsElem = elem.FirstChild;
			LoadCategoriesForSystem(system, elem);
						
			XmlElement statsElem = (XmlElement)catsElem.NextSibling;					
			LoadSystemStatsFromElement(statsElem, system);
			string defaultStatsID = statsElem.GetAttribute("defaultStats");
								
			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
			system.StandardSystemStatsID = defaultStatsID;
			system.SetAsFullyLoaded();
		}
		
		private void LoadCategoriesForSystem(GameSystem system, XmlNode elem)
		{
			WarFoundryObject tempObj;
						
			foreach (XmlElement cat in elem.SelectNodes("//"+WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
			{
				tempObj = CreateObjectFromElement(cat);
				
				if (tempObj is Category)
				{
					system.AddCategory((Category)tempObj);
				}
				else
				{
					LogNotifier.WarnFormat(GetType(), "Object for string {0} was not of type Category", cat.OuterXml);
				}
			}
		}
		
		public void CompleteLoading(Race race)
		{
			if (race.IsFullyLoaded)
			{
				LogNotifier.DebugFormat(GetType(), "Object of type Race with ID {0} is already fully loaded", race.ID);
				return;
			}
			
			XmlNode elem = GetExtraData(race);
			XmlNode colNode = elem.FirstChild;
						
			foreach (XmlElement node in colNode.ChildNodes)
			{
				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
				race.AddUnitType(type);
			}
			
			colNode = colNode.NextSibling;
			
			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value)
			{
				foreach (XmlElement node in colNode.ChildNodes)
				{
					race.AddCategory(CreateCategoryFromElement(node));
				}
				
				colNode = colNode.NextSibling;
			}
							
			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
			{
				foreach (XmlElement node in colNode.ChildNodes)
				{
					EquipmentItem item = CreateEquipmentItemFromElement(node, race);
					race.AddEquipmentItem(item);
				}
			}
			
			Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
			//TODO: Load abilities
			race.SetAbilities(raceAbilities);
			race.SetAsFullyLoaded();
			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name);
		}
		
		protected XmlDocument CreateXmlDocumentFromStream(Stream stream)
		{
			XmlDocument doc = new XmlDocument();
			XmlReaderSettings settings = new XmlReaderSettings();
			settings.XmlResolver = xmlResolver;
			settings.ValidationType = ValidationType.DTD;
			settings.ProhibitDtd = false;
			settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
			XmlReader reader = XmlReader.Create(stream, settings);
			
			try
			{
				doc.Load(reader);			
			}
			//Don't catch XMLSchemaExceptions - let them get thrown out
			finally
			{
				reader.Close();
			}
			
			if (doc.ChildNodes.Count!=3)
			{
				throw new InvalidFileException(Translation.GetTranslation("ErrorInvalidXmlFile", "XML file was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node."));
			}

			return doc;
		}

		protected XmlDocument CreateXmlDocumentFromString(string xmlString)
		{			
			XmlReaderSettings settings = new XmlReaderSettings();
			settings.XmlResolver = xmlResolver;
			settings.ValidationType = ValidationType.DTD;
			settings.ProhibitDtd = false;
			settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
			XmlReader reader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(xmlString)), settings);
			XmlDocument doc = new XmlDocument();

			try
			{
				doc.Load(reader);
			}
			catch(XmlSchemaException ex)
			{
				throw new InvalidFileException(Translation.GetTranslation("ErrorInvalidXmlString", "Failed to parse invalid XML string"), ex);
			}
			finally
			{
				//We might not need to make sure the memory stream is closed, but do it just in case
				reader.Close();
			}
			
			if (doc.ChildNodes.Count!=3)
			{
				throw new InvalidFileException(String.Format(Translation.GetTranslation("ErrorInvalidXmlFile", "XML string was not a valid XML file. It should contain three child nodes - XML definition, DocType and root node.")));
			}

			return doc;
		}
					
		private WarFoundryObject CreateObjectFromElement(XmlElement elem)
		{
			WarFoundryObject obj = null;
			LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name);
			
			if (elem.Name.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
			{
				LogNotifier.Debug(GetType(), "Create Category");
				obj = CreateCategoryFromElement(elem);
			}
			else
			{
				LogNotifier.Debug(GetType(), "No match");
			}
			
			return obj;
		}
		
		private Category CreateCategoryFromElement(XmlElement elem)
		{
			string id = elem.GetAttribute("id");
			string name = elem.GetAttribute("name");
			int minPc, maxPc, minPts, maxPts, minChoices, maxChoices, baseValue, incValue, incAmount;
			minPc = GetIntValueFromAttribute(elem, "minPercentage");
			maxPc = GetIntValueFromAttribute(elem, "maxPercentage");
			minPts = GetIntValueFromAttribute(elem, "minPoints");
			maxPts = GetIntValueFromAttribute(elem, "maxPoints");
			minChoices = GetIntValueFromAttribute(elem, "minChoices");
			maxChoices = GetIntValueFromAttribute(elem, "maxChoices");
			baseValue = GetIntValueFromAttribute(elem, "baseValue");
			incValue = GetIntValueFromAttribute(elem, "incValue");
			incAmount = GetIntValueFromAttribute(elem, "incAmount");
						
			return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount);
		}
				
		private int GetIntValueFromAttribute(XmlElement elem, string attributeName)
		{
			try
			{
				return int.Parse(elem.GetAttribute(attributeName));
			}
			catch(FormatException)
			{
				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
			}
		}
						
		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
		{
			string id = elem.GetAttribute("id");
			string name = elem.GetAttribute("typeName");
			UnitType type = new UnitType(id, name, parentRace);
			type.MinNumber = GetIntValueFromAttribute(elem, "minNum");
			type.MaxNumber = GetIntValueFromAttribute(elem, "maxNum");
			type.MinSize = GetIntValueFromAttribute(elem, "minSize");
			type.MaxSize = GetIntValueFromAttribute(elem, "maxSize");
			//TODO: Add base size
			type.CostPerTrooper = GetIntValueFromAttribute(elem, "points");
			type.BaseUnitCost = GetIntValueFromAttribute(elem, "unitPoints");
			
			XmlNode node = elem.FirstChild;
			
			foreach(XmlElement cat in node.ChildNodes)
			{
				string catID = cat.GetAttribute("catID");
				type.AddCategory(parentRace.GetCategory(catID));
			}

			string mainCatID = elem.GetAttribute("cat");
			type.MainCategory = parentRace.GetCategory(mainCatID);						
			node = node.NextSibling;			
			type.UnitStats = ParseUnitStats((XmlElement)node, system);
			//TODO: Add unit requirements
			
			return type;
		}
		
		private Stats ParseUnitStats(XmlElement elem, GameSystem system)
		{
			List<Stat> statsList = new List<Stat>();
			String statsID = elem.GetAttribute("statSet");
			SystemStats statsSet;
			
			if (statsID == "")
			{
				statsSet = system.StandardSystemStats;
			}
			else
			{
				statsSet = system.GetSystemStatsForID(statsID);
			}
			
			Stats stats = new Stats(statsSet);
			
			foreach (XmlElement stat in elem.ChildNodes)
			{
				String statID = stat.GetAttribute("name");
				StatSlot slot = statsSet[statID];
				
				if (slot!=null)
				{
					statsList.Add(new Stat(slot, stat.InnerText));
				}
				else
				{
					throw new InvalidFileException("The stat "+statID+" was not found in stats set "+statsID);
				}
			}
			
			stats.SetStats(statsList);
			
			return stats;
		}
		
		private void LoadSystemStatsFromElement(XmlElement elem, GameSystem system)
		{
			foreach (XmlElement stats in elem.ChildNodes)
			{
				SystemStats sysStats = CreateSystemStatsFromElement(stats);
				system.AddSystemStats(sysStats);
			}
		}
		
		private SystemStats CreateSystemStatsFromElement(XmlElement elem)
		{
			List<StatSlot> slots = new List<StatSlot>();
			string id = elem.GetAttribute("id");	
			
			foreach (XmlElement slot in elem.ChildNodes)
			{
				StatSlot statSlot = new StatSlot(slot.GetAttribute("name"));
				slots.Add(statSlot);
			}
			
			return new SystemStats(id, slots.ToArray());
		}
		
		private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race)
		{
			string id = elem.GetAttribute("id");
			string name = elem.GetAttribute("name");
			float cost = 0, min = 0, max = 0;
			ArmourType armourType;
			
			try
			{
				cost = float.Parse(elem.GetAttribute("cost"));
			}
			catch(FormatException)
			{
				throw new FormatException("Attribute 'cost' of equipment item "+id+" was not a valid number");
			}			
			
			try
			{
				min = float.Parse(elem.GetAttribute("min"));
			}
			catch(FormatException)
			{
				throw new FormatException("Attribute 'min' of equipment item "+id+" was not a valid number");
			}			
			
			try
			{
				max = float.Parse(elem.GetAttribute("max"));
			}
			catch(FormatException)
			{
				throw new FormatException("Attribute 'max' of equipment item "+id+" was not a valid number");
			}
			
			try
			{
				armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType"));
			}
			catch(FormatException)
			{
				throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration");
			}
			
			if (elem.ChildNodes.Count>0)
			{
				//It has stats!
				//TODO: Parse equipment stats
			}
			
			return new EquipmentItem(id, name, cost, min, max, armourType, race);
		}
		
		private void ValidationEventMethod(object sender, ValidationEventArgs e)
		{
			//TODO: Fire a validation failure event
    		LogNotifier.WarnFormat(GetType(), "Validation Error: {0}", e.Message);
		}
	}
}