ParticleEffectImporter.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Xml;
  5. using Microsoft.Xna.Framework.Content.Pipeline;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace MonoGame.Extended.Content.Pipeline.Particles;
  8. [ContentImporter(".ember", DefaultProcessor = nameof(ParticleEffectProcessor), DisplayName = "Particle Effect Importer - MonoGame.Extended")]
  9. public class ParticleEffectImporter : ContentImporter<ContentImporterResult<ParticleEffectFileContent>>
  10. {
  11. public override ContentImporterResult<ParticleEffectFileContent> Import(string filePath, ContentImporterContext context)
  12. {
  13. try
  14. {
  15. ArgumentException.ThrowIfNullOrEmpty(filePath);
  16. ContentLogger.Logger = context.Logger;
  17. ContentLogger.Log($"Importing '{filePath}'");
  18. string xmlContent = File.ReadAllText(filePath);
  19. List<string> textureReferences = ExtractTextureReferences(filePath, context);
  20. ParticleEffectFileContent fileContent = new(xmlContent, textureReferences);
  21. ContentLogger.Log($"Imported '{filePath}'");
  22. return new ContentImporterResult<ParticleEffectFileContent>(filePath, fileContent);
  23. }
  24. catch (Exception e)
  25. {
  26. context.Logger.LogImportantMessage(e.StackTrace);
  27. throw;
  28. }
  29. }
  30. private List<string> ExtractTextureReferences(string filePath, ContentImporterContext context)
  31. {
  32. List<string> textureReferences = [];
  33. string BaseDirectory = Path.GetDirectoryName(filePath);
  34. using XmlReader reader = XmlReader.Create(filePath, new XmlReaderSettings() { IgnoreComments = true, IgnoreWhitespace = true });
  35. while (reader.Read())
  36. {
  37. if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "TextureRegion")
  38. {
  39. string textureName = reader.GetAttribute("Name");
  40. if (!string.IsNullOrEmpty(textureName))
  41. {
  42. string texturePath = Path.Combine(BaseDirectory, textureName);
  43. if (!textureReferences.Contains(texturePath))
  44. {
  45. textureReferences.Add(texturePath);
  46. context.AddDependency(texturePath);
  47. ContentLogger.Log($"Adding dependency '{texturePath}'");
  48. }
  49. }
  50. }
  51. }
  52. return textureReferences;
  53. }
  54. }