Jump to content
  • Advertisement
Sign in to follow this  
EvilNando

[XNA] Help creating custom importers

This topic is 3545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im really not getting it when it comes to creating a custom importer here is what I want to do: I have a custom XML filled with data, I dont want to use the xml content type I would like to treat these files as just text. heres a brief example of the xml data <?xml version="1.0" encoding="utf-8"?> <Animation> <Frames>30</Frames> <Objects>10</Objects> </Animation> here is the code ive been working with Any can please help me out?

    [ContentImporter(".xanm", DefaultProcessor = "AnimProcessor")] 
    public class AnimImporter : ContentImporter<AnimCode> 
    { 
        public override AnimCode Import(string filename, ContentImporterContext context) 
        { 
            string source_code = File.ReadAllText(filename); 
 
            return new AnimCode(source_code); 
        } 
    } 


   [ContentProcessor] 
    public class AnimProcessor : ContentProcessor<AnimCode, CompiledAnim> 
    { 
        public override CompiledAnim Process(AnimCode input, ContentProcessorContext context) 
        { 
            byte[] byte_array           = new byte[input.Data.Length]; 
            UTF8Encoding enconding      = new UTF8Encoding(); 
            byte_array                  = enconding.GetBytes(input.Data); 
 
            MemoryStream memory_stream  = new MemoryStream(byte_array); 
            XmlTextReader xml_reader    = new XmlTextReader(memory_stream); 
 
            int columns     = 0; 
            int rows        = 0; 
 
            xml_reader.ReadStartElement(); 
 
            xml_reader.ReadToFollowing("Frames"); 
            columns = Convert.ToInt32(xml_reader.ReadString()); 
 
            xml_reader.ReadToFollowing("Objects"); 
            rows = Convert.ToInt32(xml_reader.ReadString()); 
 
            return new CompiledAnim(columns, rows); 
        } 
    } 

   [ContentTypeWriter] 
    class AnimWriter : ContentTypeWriter<CompiledAnim> 
    { 
        protected override void Write(ContentWriter output, CompiledAnim value) 
        { 
            //output.Write(value.Size.X);  
            //output.Write(value.Size.Y);             
 
            output.WriteObject<Point>(value.Size); 
        } 
 
        public override string GetRuntimeType(TargetPlatform targetPlatform) 
        { 
            return typeof(CompiledAnim).AssemblyQualifiedName; 
        } 
 
        public override string GetRuntimeReader(TargetPlatform targetPlatform) 
        { 
            return typeof(CompiledAnim).AssemblyQualifiedName; 
        } 
    } 

    class AnimReader : ContentTypeReader<CompiledAnim> 
    { 
 
        protected override CompiledAnim Read(ContentReader input, CompiledAnim existingInstance) 
        { 
            System.Diagnostics.Debugger.Launch(); 
 
            existingInstance.Size = new Point(input.ReadInt32(), input.ReadInt32()); 
            return existingInstance; 
        } 
    } 


   [ContentProcessor] 
    public class AnimProcessor : ContentProcessor<AnimCode, CompiledAnim> 
    { 
        public override CompiledAnim Process(AnimCode input, ContentProcessorContext context) 
        { 
            byte[] byte_array           = new byte[input.Data.Length]; 
            UTF8Encoding enconding      = new UTF8Encoding(); 
            byte_array                  = enconding.GetBytes(input.Data); 
 
            MemoryStream memory_stream  = new MemoryStream(byte_array); 
            XmlTextReader xml_reader    = new XmlTextReader(memory_stream); 
 
            int columns     = 0; 
            int rows        = 0; 
 
            xml_reader.ReadStartElement(); 
 
            xml_reader.ReadToFollowing("Frames"); 
            columns = Convert.ToInt32(xml_reader.ReadString()); 
 
            xml_reader.ReadToFollowing("Objects"); 
            rows = Convert.ToInt32(xml_reader.ReadString()); 
 
            return new CompiledAnim(columns, rows); 
        } 
    } 

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!