PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/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
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Delta.Utilities;
  5. using Delta.Utilities.Helpers;
  6. namespace Delta.ContentSystem.Rendering
  7. {
  8. /// <summary>
  9. /// Level data for loading and saving levels, which are currently just
  10. /// a bunch of meshes and the camera data.
  11. /// </summary>
  12. public class LevelData : Content, ISaveLoadBinary
  13. {
  14. #region Constants
  15. /// <summary>
  16. /// Version number for this MaterialData. If this goes above 1, we need
  17. /// to support loading older versions as well. Saving is always going
  18. /// to be the latest version (this one).
  19. /// </summary>
  20. private const int VersionNumber = 1;
  21. #endregion
  22. #region Get (Static)
  23. /// <summary>
  24. /// Get and load content based on the content name. This method makes sure
  25. /// we do not load the same content twice (the constructor is protected).
  26. /// </summary>
  27. /// <param name="contentName">Content name we want to load, this is
  28. /// passed onto the Content System, which will do the actual loading with
  29. /// help of the Load method in this class.</param>
  30. /// <returns>The loaded Content object, always unique for the same
  31. /// name, this helps comparing data.</returns>
  32. public static LevelData Get(string contentName)
  33. {
  34. return Get<LevelData>(contentName, ContentType.Level);
  35. }
  36. #endregion
  37. #region optimizedMeshes (Public)
  38. /// <summary>
  39. /// List of optimized level meshes that have all geometry merged by
  40. /// material usage. Stuff that does not fit into the index buffer range
  41. /// (65535) will be split up into multiple meshes (rarely happens).
  42. /// </summary>
  43. public List<MeshData> optimizedMeshes = new List<MeshData>();
  44. #endregion
  45. #region cameraData (Public)
  46. /// <summary>
  47. /// Quick hack for camera support for MWC demo
  48. /// </summary>
  49. public CameraData cameraData;
  50. #endregion
  51. #region Constructors
  52. /// <summary>
  53. /// Loads level data. Use the static Get method to call this.
  54. /// </summary>
  55. /// <param name="setLevelName">Set level name</param>
  56. protected LevelData(string setLevelName)
  57. : base(setLevelName, ContentType.Level)
  58. {
  59. }
  60. #endregion
  61. #region ISaveLoadBinary Members
  62. /// <summary>
  63. /// Load level data from a binary reader.
  64. /// </summary>
  65. /// <param name="reader">Reader to get the data from, underlying stream
  66. /// must be in the correct position.</param>
  67. public void Load(BinaryReader reader)
  68. {
  69. // We currently only support our version, if more versions are added,
  70. // we need to do different loading code depending on the version here.
  71. int version = reader.ReadInt32();
  72. if (version != VersionNumber)
  73. {
  74. Log.InvalidVersionWarning(GetType().Name, version, VersionNumber);
  75. return;
  76. }
  77. int numberOfMeshes = reader.ReadInt32();
  78. optimizedMeshes.Clear();
  79. for (int index = 0; index < numberOfMeshes; index++)
  80. {
  81. // Load meshes from just their unique names
  82. string meshDataName = reader.ReadString();
  83. optimizedMeshes.Add(MeshData.Get(meshDataName));
  84. }
  85. bool hasCamera = reader.ReadBoolean();
  86. if (hasCamera)
  87. {
  88. cameraData = CameraData.Get(reader.ReadString());
  89. }
  90. }
  91. /// <summary>
  92. /// Save level data into a stream.
  93. /// </summary>
  94. /// <param name="writer">Binary stream writer to save data into.</param>
  95. public void Save(BinaryWriter writer)
  96. {
  97. writer.Write(VersionNumber);
  98. writer.Write(optimizedMeshes.Count);
  99. foreach (MeshData mesh in optimizedMeshes)
  100. {
  101. // Save just the "clean" mesh data name without "<" and ">"
  102. string meshName = mesh.Name;
  103. if (meshName.StartsWith("<"))
  104. {
  105. meshName = meshName.Substring(1);
  106. }
  107. if (meshName.StartsWith(">"))
  108. {
  109. meshName = meshName.Substring(0, meshName.Length - 1);
  110. }
  111. writer.Write(meshName);
  112. }
  113. // Save if we have a camera.
  114. writer.Write(cameraData != null);
  115. if (cameraData != null)
  116. {
  117. writer.Write(cameraData.Name);
  118. }
  119. }
  120. #endregion
  121. #region Dispose (Public)
  122. /// <summary>
  123. /// Dispose meshes data, which is called when the content system disposes
  124. /// this class (or someone else wants to get rid of the memory).
  125. /// </summary>
  126. public override void Dispose()
  127. {
  128. // Dispose all meshes, they can all be automatically reload if their
  129. // data is needed again.
  130. foreach (MeshData mesh in optimizedMeshes)
  131. {
  132. mesh.Dispose();
  133. }
  134. }
  135. #endregion
  136. #region Methods (Private)
  137. #region Load
  138. /// <summary>
  139. /// Native load method, will just load the xml data.
  140. /// </summary>
  141. /// <param name="alreadyLoadedNativeData">
  142. /// The first instance that has already loaded the required content data
  143. /// of this content class or just 'null' if there is none loaded yet (or
  144. /// anymore).
  145. /// </param>
  146. protected override void Load(Content alreadyLoadedNativeData)
  147. {
  148. try
  149. {
  150. if (String.IsNullOrEmpty(RelativeFilePath) == false)
  151. {
  152. // Load via the ISaveLoadBinary interface methods below.
  153. // Cloning should not really happen for levels anyway.
  154. FileHelper.Load(RelativeFilePath, this);
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. Log.Warning("Failed to load level data from file '" +
  160. RelativeFilePath + "': " + ex);
  161. FailedToLoad = true;
  162. }
  163. }
  164. #endregion
  165. #endregion
  166. }
  167. }