using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using Microsoft.Xna.Framework.Content.Pipeline; using Microsoft.Xna.Framework.Content.Pipeline.Processors; using Microsoft.Xna.Framework.Content.Pipeline.Graphics; using Microsoft.Xna.Framework.Content.Pipeline.Serialization.Compiler; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; namespace Processors { public class DictionaryTree { private DictionaryNode root; public DictionaryTree(DictionaryNode root) { this.root = root; } public void AddWord(string word) { root.AddWord(word.ToCharArray(), 0); } internal void WriteTree(ContentWriter writer, StreamWriter sw) { root.WriteNode(writer, sw); } internal void ReadTree(ContentReader reader) { root.ReadNode(reader); } } public class DictionaryNode { private Dictionary children; private bool isWord; private char nodeLetter; public DictionaryNode(char letter) { children = new Dictionary(); isWord = false; nodeLetter = letter; } public void AddWord(char[] word, short offset) { if (offset == word.Length) isWord = true; else { char oChar = word[offset]; if (!children.ContainsKey(oChar)) children.Add(oChar, new DictionaryNode(oChar)); children[oChar].AddWord(word, (short)(offset + 1)); } } internal void WriteNode(ContentWriter writer, StreamWriter sw) { writer.Write(isWord); writer.Write(children.Count); sw.Write(isWord); sw.Write(children.Count); foreach (char c in children.Keys) { writer.Write(c); sw.Write(c); children[c].WriteNode(writer, sw); } } internal void ReadNode(ContentReader reader) { isWord = reader.ReadBoolean(); int numChildren = reader.ReadInt32(); children = new Dictionary(numChildren); for (int c = 0; c < numChildren; c++) { char oChar = reader.ReadChar(); children.Add(oChar, new DictionaryNode(oChar)); children[oChar].ReadNode(reader); } } } [ContentImporter(".dict", DefaultProcessor = "DictionaryProcessor")] class DictionaryImporter : ContentImporter { public override string[] Import(string filename, ContentImporterContext context) { string[] strings = File.ReadAllLines(filename); return strings; } } [ContentProcessor] class DictionaryProcessor : ContentProcessor { public override DictionaryTree Process(string[] input, ContentProcessorContext context) { Debugger.Launch(); DictionaryTree tree = new DictionaryTree(new DictionaryNode(' ')); for (int w = 0; w < input.Length; w++) tree.AddWord(input[w]); return tree; } } [ContentTypeWriter] class DictionaryWriter : ContentTypeWriter { protected override void Write(ContentWriter output, DictionaryTree value) { FileStream stream = File.Create("Test3.txt"); StreamWriter sw = new StreamWriter(stream); value.WriteTree(output, sw); sw.Close(); } public override string GetRuntimeType(TargetPlatform targetPlatform) { return typeof(DictionaryTree).AssemblyQualifiedName; } public override string GetRuntimeReader(TargetPlatform targetPlatform) { return "Processors.DictionaryReader, Processors, Version=1.0.0.0, Culture=neutral"; } } class DictionaryReader : ContentTypeReader { protected override DictionaryTree Read(ContentReader input, DictionaryTree existingInstance) { Debugger.Launch(); DictionaryTree tree = new DictionaryTree(new DictionaryNode(' ')); tree.ReadTree(input); return tree; } } }