/ContentSystem/Rendering/EffectData.cs
C# | 173 lines | 107 code | 10 blank | 56 comment | 10 complexity | 332ae7413f22f81c5a3971ce7c8d7bcc MD5 | raw file
Possible License(s): Apache-2.0
- using System;
- using System.Collections.Generic;
- using System.IO;
- using Delta.ContentSystem.Rendering.Helpers;
- using Delta.Engine.Dynamic;
- using Delta.Utilities;
- using Delta.Utilities.Helpers;
-
- namespace Delta.ContentSystem.Rendering
- {
- /// <summary>
- /// The effect data class is the content implementation of a effect and
- /// handles all the saving and loading of an effect.
- /// Basically this class only contains a list of Emitters that is filled
- /// during loading. The further load logic is then in the EmitterData class.
- /// </summary>
- public class EffectData : Content, ISaveLoadBinary
- {
- #region Get (Static)
- /// <summary>
- /// This is the only method to load XmlData content. If a content object
- /// has already been loaded, it will be returned again.
- /// </summary>
- /// <param name="contentName">Name of the Xml content to load</param>
- /// <returns>The loaded XmlData object (or fallback if it failed)</returns>
- public static EffectData Get(string contentName)
- {
- return Get<EffectData>(contentName, ContentType.ParticleEffect);
- }
- #endregion
-
- #region Emitters (Public)
- /// <summary>
- /// List of emitters.
- /// </summary>
- public List<EmitterData> Emitters
- {
- get;
- private set;
- }
- #endregion
-
- #region Constructors
- /// <summary>
- /// Create and load new effect data. Use the static Get method to call this.
- /// </summary>
- /// <param name="setEffectName">Name of the effect.</param>
- protected EffectData(string setEffectName)
- : base(setEffectName, ContentType.ParticleEffect)
- {
- }
-
- /// <summary>
- /// Create effect data from list of emitter data. Only used internally
- /// for the editors, it makes no sense to create content objects in the
- /// engine itself. You can dynamically create rendering classes like in
- /// this case a Effect class with custom emitters, but it is not a loaded
- /// EffectData content class then (the link to data will stay null in the
- /// Effect class).
- /// </summary>
- /// <param name="setEmitters">Set emitter data list</param>
- /// <exception cref="NullReferenceException">To create 'EffectData' you
- /// have to provide a valid emitter data list!</exception>
- internal EffectData(List<EmitterData> setEmitters)
- // This is not a full content object yet, it is empty and needs to
- // be saved and then loaded again to become content.
- : base(EmptyName, ContentType.ParticleEffect)
- {
- if (setEmitters == null)
- {
- throw new NullReferenceException("To create 'EffectData' you have " +
- "to provide a valid emitter data list!");
- }
-
- Emitters = setEmitters;
- }
- #endregion
-
- #region ISaveLoadBinary Members
- /// <summary>
- /// Load saved data back into this EffectData, usually only called from
- /// the Load method above from the Content System.
- /// </summary>
- /// <param name="reader">Reader to read the data from</param>
- public void Load(BinaryReader reader)
- {
- Emitters = new List<EmitterData>();
- try
- {
- // Simply read the number of emitters and load each of them.
- int numberOfEmitters = reader.ReadInt32();
- for (int index = 0; index < numberOfEmitters; index++)
- {
- Emitters.Add(Factory.Load<EmitterData>(reader));
- }
- }
- catch (Exception ex)
- {
- Log.Warning("Failed to load effect data '" + Name + "': " + ex);
- FailedToLoad = true;
- }
- }
-
- /// <summary>
- /// Save effect data into stream. Usually called by the tool that saves
- /// this data out (EffectEditor) or the ContentSystem for optimizations.
- /// </summary>
- /// <param name="writer">Writer for the stream to write into</param>
- public void Save(BinaryWriter writer)
- {
- // Write how many emitters we have
- writer.Write(Emitters.Count);
- // And save them out one by one
- for (int index = 0; index < Emitters.Count; index++)
- {
- Factory.Save(writer, Emitters[index]);
- }
- }
- #endregion
-
- #region Methods (Private)
-
- #region Load
- /// <summary>
- /// Native load method, will just load or clone the effect 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 (alreadyLoadedNativeData != null)
- {
- EffectData alreadyLoadedEffect = (EffectData)alreadyLoadedNativeData;
- Emitters = new List<EmitterData>(alreadyLoadedEffect.Emitters);
- } // if
- else if (String.IsNullOrEmpty(RelativeFilePath))
- {
- // No file given, this is a generated effect, do not load anything!
- Emitters = new List<EmitterData>();
- }
- else if (FileHelper.Exists(RelativeFilePath))
- {
- // Load via the ISaveLoadBinary interface methods below.
- // Cloning should not really happen for effects anyway.
- FileHelper.Load(RelativeFilePath, this);
- } // else if
- else
- {
- Log.Warning("Couldn't load the " + GetType().Name + " '" +
- Name + "' because the (relative) file path '" +
- RelativeFilePath +
- "' isn't valid or doesn't exists. Will just return the default" +
- " object.");
- Emitters = new List<EmitterData>();
- } // else
- } // try
- catch (Exception ex)
- {
- Log.Warning("Failed to load the '" + GetType().Name +
- "' because of reason:" + ex);
- FailedToLoad = true;
- } // catch
- }
- #endregion
-
- #endregion
- }
- }