/ContentSystem/Rendering/LevelData.cs
C# | 181 lines | 107 code | 14 blank | 60 comment | 11 complexity | 7fb610979467bd1c6dfd88b8ccb399bc MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Delta.Utilities;
- using Delta.Utilities.Helpers;
-
- namespace Delta.ContentSystem.Rendering
- {
- /// <summary>
- /// Level data for loading and saving levels, which are currently just
- /// a bunch of meshes and the camera data.
- /// </summary>
- public class LevelData : Content, ISaveLoadBinary
- {
- #region Constants
- /// <summary>
- /// Version number for this MaterialData. If this goes above 1, we need
- /// to support loading older versions as well. Saving is always going
- /// to be the latest version (this one).
- /// </summary>
- private const int VersionNumber = 1;
- #endregion
-
- #region Get (Static)
- /// <summary>
- /// Get and load content based on the content name. This method makes sure
- /// we do not load the same content twice (the constructor is protected).
- /// </summary>
- /// <param name="contentName">Content name we want to load, this is
- /// passed onto the Content System, which will do the actual loading with
- /// help of the Load method in this class.</param>
- /// <returns>The loaded Content object, always unique for the same
- /// name, this helps comparing data.</returns>
- public static LevelData Get(string contentName)
- {
- return Get<LevelData>(contentName, ContentType.Level);
- }
- #endregion
-
- #region optimizedMeshes (Public)
- /// <summary>
- /// List of optimized level meshes that have all geometry merged by
- /// material usage. Stuff that does not fit into the index buffer range
- /// (65535) will be split up into multiple meshes (rarely happens).
- /// </summary>
- public List<MeshData> optimizedMeshes = new List<MeshData>();
- #endregion
-
- #region cameraData (Public)
- /// <summary>
- /// Quick hack for camera support for MWC demo
- /// </summary>
- public CameraData cameraData;
- #endregion
-
- #region Constructors
- /// <summary>
- /// Loads level data. Use the static Get method to call this.
- /// </summary>
- /// <param name="setLevelName">Set level name</param>
- protected LevelData(string setLevelName)
- : base(setLevelName, ContentType.Level)
- {
- }
- #endregion
-
- #region ISaveLoadBinary Members
- /// <summary>
- /// Load level data from a binary reader.
- /// </summary>
- /// <param name="reader">Reader to get the data from, underlying stream
- /// must be in the correct position.</param>
- public void Load(BinaryReader reader)
- {
- // We currently only support our version, if more versions are added,
- // we need to do different loading code depending on the version here.
- int version = reader.ReadInt32();
- if (version != VersionNumber)
- {
- Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
- return;
- }
-
- int numberOfMeshes = reader.ReadInt32();
- optimizedMeshes.Clear();
- for (int index = 0; index < numberOfMeshes; index++)
- {
- // Load meshes from just their unique names
- string meshDataName = reader.ReadString();
- optimizedMeshes.Add(MeshData.Get(meshDataName));
- }
-
- bool hasCamera = reader.ReadBoolean();
- if (hasCamera)
- {
- cameraData = CameraData.Get(reader.ReadString());
- }
- }
-
- /// <summary>
- /// Save level data into a stream.
- /// </summary>
- /// <param name="writer">Binary stream writer to save data into.</param>
- public void Save(BinaryWriter writer)
- {
- writer.Write(VersionNumber);
- writer.Write(optimizedMeshes.Count);
- foreach (MeshData mesh in optimizedMeshes)
- {
- // Save just the "clean" mesh data name without "<" and ">"
- string meshName = mesh.Name;
- if (meshName.StartsWith("<"))
- {
- meshName = meshName.Substring(1);
- }
- if (meshName.StartsWith(">"))
- {
- meshName = meshName.Substring(0, meshName.Length - 1);
- }
- writer.Write(meshName);
- }
-
- // Save if we have a camera.
- writer.Write(cameraData != null);
- if (cameraData != null)
- {
- writer.Write(cameraData.Name);
- }
- }
- #endregion
-
- #region Dispose (Public)
- /// <summary>
- /// Dispose meshes data, which is called when the content system disposes
- /// this class (or someone else wants to get rid of the memory).
- /// </summary>
- public override void Dispose()
- {
- // Dispose all meshes, they can all be automatically reload if their
- // data is needed again.
- foreach (MeshData mesh in optimizedMeshes)
- {
- mesh.Dispose();
- }
- }
- #endregion
-
- #region Methods (Private)
-
- #region Load
- /// <summary>
- /// Native load method, will just load the xml data.
- /// </summary>
- /// <param name="alreadyLoadedNativeData">
- /// The first instance that has already loaded the required content data
- /// of this content class or just 'null' if there is none loaded yet (or
- /// anymore).
- /// </param>
- protected override void Load(Content alreadyLoadedNativeData)
- {
- try
- {
- if (String.IsNullOrEmpty(RelativeFilePath) == false)
- {
- // Load via the ISaveLoadBinary interface methods below.
- // Cloning should not really happen for levels anyway.
- FileHelper.Load(RelativeFilePath, this);
- }
- }
- catch (Exception ex)
- {
- Log.Warning("Failed to load level data from file '" +
- RelativeFilePath + "': " + ex);
- FailedToLoad = true;
- }
- }
- #endregion
-
- #endregion
- }
- }