namespace DarkWynterEngine.Globals
{
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Xml;
using System.Diagnostics;
using System.Threading;
#endregion
using Globals;
using ObjectLib;
///
/// The base class used in the XML layer of the architecture.
/// XML loads several configuration files, parses their main structure, and stores the nodes in public variables that are accessed by the rest of the program.
///
public static class XML
{
///
/// Contains the name, description, and filepath for each level.
///
public struct LevelInfo
{
///
/// User friendly name for level.
///
public string name;
///
/// User friendly description for level.
///
public string description;
///
/// Filepath of the level xml, which contains all xml data for that level.
///
public string filepath;
}
///
/// List of availible levels contained in _Levels.xml
///
public static List levelInfo;
///
/// All nodes making up an XML level description are posted here
/// for the rest of the Engine to access in it's load procedures.
///
public struct GameObjectInfo
{
#region Environment
///
/// Physics data stored in Xml level description.
///
public XmlNode physicsNode;
///
/// Fog data stored in Xml level description.
///
public XmlNode fogNode;
///
/// Music data stored in Xml level description.
///
public XmlNode musicNode;
#endregion
#region Level Info
///
/// Terrain data stored in Xml level description.
///
public XmlNode terrainNode;
///
/// Skyphere data stored in Xml level description.
///
public XmlNode skyNode;
///
/// Human data stored in Xml level description.
///
public XmlNode humanNodes;
///
/// AI data stored in Xml level description.
///
public XmlNode botNodes;
///
/// Particle attack object data stored in Xml level description.
///
public XmlNode particleNode;
///
/// Bullet attack object data stored in Xml level description.
///
public XmlNode bulletNode;
///
/// Generic GameObject data stored in Xml level description.
///
public XmlNode gameObjectNodes;
///
/// Generic Prop data stored in Xml level description.
///
public XmlNode propNode;
///
/// GpuObject data stored in Xml level description.
///
public XmlNode gpuObjectNodes;
///
/// Billboard data stored in Xml level description.
///
public XmlNode billboardNode;
#endregion
}
///
/// Instantiation of the GameObjectInfo struct.
///
public static GameObjectInfo gameObjectInfo;
///
/// Contains the name, modelPath, texturePath, bumpTexturePath,
/// and scale information for a character.
///
public struct PlayerInfo
{
///
/// User friendly name of character.
///
public string name;
///
/// Path to model file.
///
public string modelPath;
///
/// Path to texture file.
///
public string texPath;
///
/// Path to bump texture file.
///
public string bumpPath;
///
/// Model scale.
///
public Vector3 scale;
}
///
/// List of instantiations of the PlayerInfo struct, contained in PlayerModels.xml.
///
public static List playerInfo;
///
/// Loads xml System settings from SystemSettings.xml.
///
public static void LoadSystem()
{
try
{
// Load physics, music, and fog
XmlDocument reader = new XmlDocument();
reader.Load("_xml/UserSettings/UserSettings.xml");
XmlNodeList allNodes = reader.ChildNodes;
foreach (XmlNode settingsNode in allNodes)
{
if (settingsNode.Name == "GameSettings")
{
XmlNodeList settingsNodes = settingsNode.ChildNodes;
foreach (XmlNode node in settingsNodes)
{
if (node.Name == "EnableShadowMap1")
{
string value = node.Attributes["value"].Value;
if (value == "Yes")
{
Statics.SystemSettings.enableShadowMap1 = true;
}
else
{
Statics.SystemSettings.enableShadowMap1 = false;
}
}
else if (node.Name == "EnableShadowMap2")
{
string value = node.Attributes["value"].Value;
if (value == "Yes")
{
Statics.SystemSettings.enableShadowMap2 = true;
}
else
{
Statics.SystemSettings.enableShadowMap2 = false;
}
}
else if (node.Name == "EnableTerrainBumpMapping")
{
string value = node.Attributes["value"].Value;
if (value == "Yes")
{
Statics.SystemSettings.enableTerrainBumpMapping = true;
}
else
{
Statics.SystemSettings.enableTerrainBumpMapping = false;
}
}
else if (node.Name == "EnableTerrainMultiTexturing")
{
string value = node.Attributes["value"].Value;
if (value == "Yes")
{
Statics.SystemSettings.enableTerrainMultiTexturing = true;
}
else
{
Statics.SystemSettings.enableTerrainMultiTexturing = false;
}
}
else if (node.Name == "NumberOfTerrainTextures")
{
int value = int.Parse(node.Attributes["value"].Value);
if (value == 4)
{
Statics.SystemSettings.numberOfTerrainTextures = 4;
}
else if (value == 3)
{
Statics.SystemSettings.numberOfTerrainTextures = 3;
}
else
{
Statics.SystemSettings.numberOfTerrainTextures = 2;
}
}
else if (node.Name == "SingleLightSource")
{
string value = node.Attributes["value"].Value;
if (value == "Yes")
{
Statics.SystemSettings.singleLightSource = true;
}
else
{
Statics.SystemSettings.singleLightSource = false;
}
}
else if (node.Name == "MusicEnabled")
{
string value = node.Attributes["value"].Value;
if (value == "Yes")
{
Statics.SystemSettings.music = true;
}
else
{
Statics.SystemSettings.music = false;
}
}
else if (node.Name == "SoundEffects")
{
string value = node.Attributes["value"].Value;
if (value == "Yes")
{
Statics.SystemSettings.soundFX = true;
}
else
{
Statics.SystemSettings.soundFX = false;
}
}
}
}
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Error reading xml");
throw new Exception("Error reading XML");
}
}
///
/// Saves xml System settings in SystemSettings.xml.
///
public static void SaveSystem()
{
XmlTextWriter settingsFile = new XmlTextWriter("_xml/UserSettings/UserSettings.xml", null);
settingsFile.Formatting = Formatting.Indented;
settingsFile.WriteStartDocument();
settingsFile.WriteStartElement("SystemSettings");
settingsFile.WriteStartElement("EnableShadowMap1");
if (Statics.SystemSettings.enableShadowMap1)
{
settingsFile.WriteAttributeString("value", "Yes");
}
else
{
settingsFile.WriteAttributeString("value", "No");
}
settingsFile.WriteEndElement();
settingsFile.WriteStartElement("EnableShadowMap2");
if (Statics.SystemSettings.enableShadowMap2)
{
settingsFile.WriteAttributeString("value", "Yes");
}
else
{
settingsFile.WriteAttributeString("value", "No");
}
settingsFile.WriteEndElement();
settingsFile.WriteStartElement("EnableTerrainBumpMapping");
if (Statics.SystemSettings.enableTerrainBumpMapping)
{
settingsFile.WriteAttributeString("value", "Yes");
}
else
{
settingsFile.WriteAttributeString("value", "No");
}
settingsFile.WriteEndElement();
settingsFile.WriteStartElement("EnableTerrainMultiTexturing");
if (Statics.SystemSettings.enableTerrainMultiTexturing)
{
settingsFile.WriteAttributeString("value", "Yes");
}
else
{
settingsFile.WriteAttributeString("value", "No");
}
settingsFile.WriteEndElement();
settingsFile.WriteStartElement("NumberOfTerrainTextures");
if (Statics.SystemSettings.numberOfTerrainTextures == 4)
{
settingsFile.WriteAttributeString("value", "4");
}
else if (Statics.SystemSettings.numberOfTerrainTextures == 3)
{
settingsFile.WriteAttributeString("value", "3");
}
else
{
settingsFile.WriteAttributeString("value", "2");
}
settingsFile.WriteEndElement();
settingsFile.WriteStartElement("SingleLightSource");
if (Statics.SystemSettings.singleLightSource)
{
settingsFile.WriteAttributeString("value", "Yes");
}
else
{
settingsFile.WriteAttributeString("value", "No");
}
settingsFile.WriteEndElement();
settingsFile.WriteStartElement("MusicEnabled");
if (Statics.SystemSettings.music)
{
settingsFile.WriteAttributeString("value", "Yes");
}
else
{
settingsFile.WriteAttributeString("value", "No");
}
settingsFile.WriteEndElement();
settingsFile.WriteStartElement("SoundEffects");
if (Statics.SystemSettings.soundFX)
{
settingsFile.WriteAttributeString("value", "Yes");
}
else
{
settingsFile.WriteAttributeString("value", "No");
}
settingsFile.WriteEndElement();
settingsFile.WriteEndElement();
settingsFile.WriteEndDocument();
settingsFile.Flush();
settingsFile.Close();
}
///
/// Loads availible Levels from _Levels.xml and stores them in the LevelInfo list.
///
public static void LoadLevelIndex()
{
string LevelXMLFile = "_xml/LevelEdit/_Levels.xml";
levelInfo = new List();
try
{
// Load physics, music, and fog
XmlDocument reader = new XmlDocument();
reader.Load(LevelXMLFile);
XmlNodeList allNodes = reader.ChildNodes;
foreach (XmlNode levelNode in allNodes)
{
if (levelNode.Name == "levels")
{
XmlNodeList levelNodes = levelNode.ChildNodes;
foreach (XmlNode node in levelNodes)
{
if (levelInfo.Count >= Statics.GameSettings.MAX_LEVELS)
{
break;
}
if (node.Name == "level")
{
LevelInfo level = new LevelInfo();
level.name = node.Attributes["name"].Value;
level.filepath = node.Attributes["xmlFile"].Value;
level.description = node.Attributes["description"].Value;
levelInfo.Add(level);
}
}
}
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Error reading xml");
throw new Exception("Error reading XML");
}
}
///
/// Loads availible Players from PlayerInfo.xml and stores them in the PlayerInfo list.
///
public static void LoadPlayerIndex()
{
string playerXMLFile = "_xml/CharacterEdit/PlayerModels.xml";
List modelNames = new List();
playerInfo = new List();
int index = 0;
//setup the player scrolling
//GameMenuImageScroller scroller = (GameMenuImageScroller) menuSystem.playerSetup.GetMenu().GetMenuElement(0);
//scroller.Clear();
try
{
// Load physics, music, and fog
XmlDocument reader = new XmlDocument();
reader.Load(playerXMLFile);
XmlNodeList allNodes = reader.ChildNodes;
foreach (XmlNode playerNode in allNodes)
{
if (playerNode.Name == "CharacterModels")
{
XmlNodeList playerNodes = playerNode.ChildNodes;
foreach (XmlNode node in playerNodes)
{
if (node.Name == "Model")
{
PlayerInfo player = new PlayerInfo();
player.name = node.Attributes["name"].Value;
modelNames.Add(player.name);
player.modelPath = node.Attributes["modelFile"].Value;
player.texPath = node.Attributes["texFile"].Value;
player.bumpPath = node.Attributes["bumpFile"].Value;
player.scale = new Vector3(float.Parse(node.Attributes["scale"].Value));
playerInfo.Add(player);
index++;
//scroller.AddNew(player.name,
// Statics.SystemSettings.content.Load("Content/_textures/patient" + index),
// Statics.SystemSettings.content.Load("Content/_textures/patient" + index));
}
}
}
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Error reading xml");
throw new Exception("Error reading XML");
}
}
///
/// Uses information from the selected LevelInfo node to load an xml file that describes the level.
/// Each node is parsed from the main structure and stored in the GameObjectInfo struct.
/// GameObjects then access these nodes to load themselves.
///
/// Xml level description file stored in LevelInfo node.
public static void LoadLevel(string xmlFileName)
{
try
{
// Load physics, music, and fog
XmlDocument reader = new XmlDocument();
reader.Load(xmlFileName);
XmlNodeList allNodes = reader.ChildNodes;
foreach (XmlNode levelNode in allNodes)
{
if (levelNode.Name == "level")
{
string levelName = levelNode.Attributes["name"].Value;
XmlNodeList levelNodes = levelNode.ChildNodes;
foreach (XmlNode node in levelNodes)
{
#region Environment Settings - Process Locally
// Environment settings include level size and other dependent parameters
// so that ObjectLibrary load sequence is _not_ order dependent.
if (node.Name == "Physics")
{
gameObjectInfo.physicsNode = node;
Statics.GameSettings.gravityForce = float.Parse(XML.gameObjectInfo.physicsNode.Attributes["gravity"].Value);
Statics.GameSettings.accelDueToGravity = new Vector3(0.0f, Statics.GameSettings.gravityForce, 0.0f);
Statics.LevelSettings.TERRAIN_FRICTION = float.Parse(XML.gameObjectInfo.physicsNode.Attributes["friction"].Value);
}
else if (node.Name == "Fog")
{
gameObjectInfo.fogNode = node;
Statics.LevelSettings.fogDensity = float.Parse(XML.gameObjectInfo.fogNode.Attributes["density"].Value);
Statics.LevelSettings.zMaxDepth = float.Parse(XML.gameObjectInfo.fogNode.Attributes["depth"].Value);
Statics.LevelSettings.fogColor[0] = float.Parse(XML.gameObjectInfo.fogNode.Attributes["red"].Value);
Statics.LevelSettings.fogColor[1] = float.Parse(XML.gameObjectInfo.fogNode.Attributes["green"].Value);
Statics.LevelSettings.fogColor[2] = float.Parse(XML.gameObjectInfo.fogNode.Attributes["blue"].Value);
Statics.LevelSettings.fogColor[3] = float.Parse(XML.gameObjectInfo.fogNode.Attributes["alpha"].Value);
}
else if (node.Name == "Music")
{
gameObjectInfo.musicNode = node;
}
else if (node.Name == "Dimensions")
{
Statics.TerrainSettings.terrainScaleFactor = int.Parse(node.Attributes["scale"].Value);
}
else if (node.Name == "Terrain")
{
gameObjectInfo.terrainNode = node;
}
//else if (node.Name == "Sky")
//{
// gameObjectInfo.skyNode = node;
//}
#endregion
if (node.Name == "Humans")
{
gameObjectInfo.humanNodes = node;
}
if (node.Name == "Particle")
{
gameObjectInfo.particleNode = node;
}
if (node.Name == "Bullet")
{
gameObjectInfo.bulletNode = node;
}
if (node.Name == "Room")
{
XmlNodeList rooms = node.ChildNodes;
foreach (XmlNode subNode in rooms)
{
if (subNode.Name == "Bots")
{
gameObjectInfo.botNodes = subNode;
}
if (subNode.Name == "GameObjects")
{
gameObjectInfo.gameObjectNodes = subNode;
}
if (subNode.Name == "Props")
{
gameObjectInfo.propNode = subNode;
}
if (subNode.Name == "GpuObjects")
{
gameObjectInfo.gpuObjectNodes = subNode;
}
if (subNode.Name == "Billboards")
{
gameObjectInfo.billboardNode = subNode;
}
}
}
}
}
}
}
catch
{
System.Diagnostics.Debug.WriteLine("Error reading xml");
throw new Exception("Error reading XML");
}
Statics.LevelSettings.gameTimer = new Stopwatch();
Statics.LevelSettings.gameTimer.Start();
}
}
}