From 739a808654af12ae1ca881c99711fa7a7b7804f7 Mon Sep 17 00:00:00 2001 From: alcexhim Date: Wed, 4 Jun 2014 20:18:25 -0400 Subject: [PATCH] Added DataFormat for TXB/CTB text files --- .../DataFormats/Text/Descent/TXBDataFormat.cs | 80 +++++++++++++++++++ .../UniversalEditor.Plugins.Descent.csproj | 1 + 2 files changed, 81 insertions(+) create mode 100644 CSharp/Plugins/UniversalEditor.Plugins.Descent/DataFormats/Text/Descent/TXBDataFormat.cs diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Descent/DataFormats/Text/Descent/TXBDataFormat.cs b/CSharp/Plugins/UniversalEditor.Plugins.Descent/DataFormats/Text/Descent/TXBDataFormat.cs new file mode 100644 index 00000000..d80c52b3 --- /dev/null +++ b/CSharp/Plugins/UniversalEditor.Plugins.Descent/DataFormats/Text/Descent/TXBDataFormat.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using UniversalEditor.IO; +using UniversalEditor.ObjectModels.Text.Plain; + +namespace UniversalEditor.DataFormats.Text.Descent +{ + public class TXBDataFormat : DataFormat + { + private static DataFormatReference _dfr = null; + public override DataFormatReference MakeReference() + { + if (_dfr == null) + { + _dfr = base.MakeReference(); + _dfr.Capabilities.Add(typeof(PlainTextObjectModel), DataFormatCapabilities.All); + _dfr.Filters.Add("Descent encrypted text file", new string[] { "*.txb", "*.ctb" }); + } + return _dfr; + } + + protected override void LoadInternal(ref ObjectModel objectModel) + { + PlainTextObjectModel text = (objectModel as PlainTextObjectModel); + if (text == null) throw new ObjectModelNotSupportedException(); + + Reader reader = base.Accessor.Reader; + StringBuilder sb = new StringBuilder(); + byte[] data = reader.ReadToEnd(); + for (int i = 0; i < data.Length; i++) + { + if (data[i] != 0x0D) + { + if (data[i] == 0x0A) + { + sb.Append((char)0x0D); + sb.Append((char)0x0A); + } + else + { + char decchar = (char)data[i]; + char encchar = (char)((((decchar & 0x3F) << 2) + ((decchar & 0xC0) >> 6)) ^ 0xA7); + sb.Append(encchar); + } + } + } + + text.Text = sb.ToString(); + } + + protected override void SaveInternal(ObjectModel objectModel) + { + PlainTextObjectModel text = (objectModel as PlainTextObjectModel); + if (text == null) throw new ObjectModelNotSupportedException(); + + Writer writer = base.Accessor.Writer; + char[] txt = text.Text.ToCharArray(); + for (int i = 0; i < txt.Length; i++) + { + if (txt[i] != 0x0D) + { + if (txt[i] == 0x0A) + { + writer.WriteChar((char)0x0D); + writer.WriteChar((char)0x0A); + } + else + { + char decchar = (char)txt[i]; + char encchar = (char)((((decchar & 0xFC) >> 2) + ((decchar & 0x03) << 6)) ^ 0xE9); + writer.WriteChar(encchar); + } + } + } + writer.Flush(); + } + } +} diff --git a/CSharp/Plugins/UniversalEditor.Plugins.Descent/UniversalEditor.Plugins.Descent.csproj b/CSharp/Plugins/UniversalEditor.Plugins.Descent/UniversalEditor.Plugins.Descent.csproj index 96f72012..73db7db2 100644 --- a/CSharp/Plugins/UniversalEditor.Plugins.Descent/UniversalEditor.Plugins.Descent.csproj +++ b/CSharp/Plugins/UniversalEditor.Plugins.Descent/UniversalEditor.Plugins.Descent.csproj @@ -36,6 +36,7 @@ +