public void Cycle() { deferredMode++; if (deferredMode == DeferredMode.Depth) { deferredMode = DeferredMode.Composite; } }
static Datamodel Load_Internal(Stream stream, DeferredMode defer_mode = DeferredMode.Automatic) { stream.Seek(0, SeekOrigin.Begin); var header = String.Empty; int b; while ((b = stream.ReadByte()) != -1) { header += (char)b; if (b == '>') { break; } if (header.Length > 128) // probably not a DMX at this point { break; } } var match = System.Text.RegularExpressions.Regex.Match(header, CodecUtilities.HeaderPattern_Regex); if (!match.Success || match.Groups.Count != 5) { throw new InvalidOperationException("Could not read file header."); } string encoding = match.Groups[1].Value; int encoding_version = int.Parse(match.Groups[2].Value); string format = match.Groups[3].Value; int format_version = int.Parse(match.Groups[4].Value); ICodec codec = GetCodec(encoding, encoding_version); var dm = codec.Decode(encoding_version, format, format_version, stream, defer_mode); if (defer_mode == DeferredMode.Automatic && codec is IDeferredAttributeCodec) { dm.Stream = stream; dm.Codec = (IDeferredAttributeCodec)codec; } dm.Format = format; dm.FormatVersion = format_version; dm.Encoding = encoding; dm.EncodingVersion = encoding_version; return(dm); }
/// <summary> /// Loads a Datamodel from a file path. /// </summary> /// <param name="path">The source file path.</param> /// <param name="defer_mode">How to handle deferred loading.</param> public static Datamodel Load(string path, DeferredMode defer_mode = DeferredMode.Automatic) { var stream = System.IO.File.OpenRead(path); Datamodel dm = null; try { dm = Load_Internal(stream, defer_mode); return(dm); } finally { if (defer_mode == DeferredMode.Disabled || (dm != null && dm.Codec == null)) { stream.Dispose(); } } }
public Datamodel Decode(int encoding_version, string format, int format_version, Stream stream, DeferredMode defer_mode) { DM = new Datamodel(format, format_version); stream.Seek(0, SeekOrigin.Begin); Reader = new StreamReader(stream, Datamodel.TextEncoding); Reader.ReadLine(); // skip DMX header Line = 1; string next; while (true) { try { next = Decode_NextToken(); } catch (EndOfStreamException) { break; } try { Decode_ParseElement(next); } catch (Exception err) { throw new CodecException(String.Format("KeyValues2 decode failed on line {0}:\n\n{1}", Line, err.Message), err); } } return DM; }
public void SetDepth(Button button) { deferredMode = DeferredMode.Depth; Switchselect(button); }
public void SetNormal(Button button) { deferredMode = DeferredMode.Normal; Switchselect(button); }
public void SetLighting(Button button) { deferredMode = DeferredMode.Lighting; Switchselect(button); }
public void SetBasic(Button button) { deferredMode = DeferredMode.Basic; Switchselect(button); }
public void SetComposite(Button button) { deferredMode = DeferredMode.Composite; Switchselect(button); }
public Datamodel Decode(int encoding_version, string format, int format_version, Stream stream, DeferredMode defer_mode) { stream.Seek(0, SeekOrigin.Begin); while (true) { var b = stream.ReadByte(); if (b == 0) { break; } } var dm = new Datamodel(format, format_version); EncodingVersion = encoding_version; Reader = new BinaryReader(stream, Datamodel.TextEncoding); if (EncodingVersion >= 9) { // Read prefix elements foreach (int prefix_elem in Enumerable.Range(0, Reader.ReadInt32())) { foreach (int attr_index in Enumerable.Range(0, Reader.ReadInt32())) { var name = ReadString_Raw(); var value = DecodeAttribute(dm, true); if (prefix_elem == 0) // skip subsequent elements...are they considered "old versions"? { dm.PrefixAttributes[name] = value; } } } } StringDict = new StringDictionary(this, Reader); var num_elements = Reader.ReadInt32(); // read index foreach (var i in Enumerable.Range(0, num_elements)) { var type = StringDict.ReadString(); var name = EncodingVersion >= 4 ? StringDict.ReadString() : ReadString_Raw(); var id_bits = Reader.ReadBytes(16); var id = new Guid(BitConverter.IsLittleEndian ? id_bits : id_bits.Reverse().ToArray()); var elem = new Element(dm, name, id, type); } // read attributes (or not, if we're deferred) foreach (var elem in dm.AllElements.ToArray()) { System.Diagnostics.Debug.Assert(!elem.Stub); var num_attrs = Reader.ReadInt32(); foreach (var i in Enumerable.Range(0, num_attrs)) { var name = StringDict.ReadString(); if (defer_mode == DeferredMode.Automatic) { CodecUtilities.AddDeferredAttribute(elem, name, Reader.BaseStream.Position); SkipAttribte(); } else { elem.Add(name, DecodeAttribute(dm, false)); } } } return(dm); }
public Datamodel Decode(int encoding_version, string format, int format_version, Stream stream, DeferredMode defer_mode) { DM = new Datamodel(format, format_version); stream.Seek(0, SeekOrigin.Begin); Reader = new StreamReader(stream, Datamodel.TextEncoding); Reader.ReadLine(); // skip DMX header Line = 1; string next; while (true) { try { next = Decode_NextToken(); } catch (EndOfStreamException) { break; } try { Decode_ParseElement(next); } catch (Exception err) { throw new CodecException(String.Format("KeyValues2 decode failed on line {0}:\n\n{1}", Line, err.Message), err); } } return(DM); }
public Datamodel Decode(int encoding_version, string format, int format_version, Stream stream, DeferredMode defer_mode) { stream.Seek(0, SeekOrigin.Begin); while (true) { var b = stream.ReadByte(); if (b == 0) break; } var dm = new Datamodel(format, format_version); EncodingVersion = encoding_version; Reader = new BinaryReader(stream, Datamodel.TextEncoding); if (EncodingVersion >= 9) { // Read prefix elements foreach (int prefix_elem in Enumerable.Range(0, Reader.ReadInt32())) { foreach (int attr_index in Enumerable.Range(0, Reader.ReadInt32())) { var name = ReadString_Raw(); var value = DecodeAttribute(dm); if (prefix_elem == 0) // skip subsequent elements...are they considered "old versions"? dm.PrefixAttributes[name] = value; } } } StringDict = new StringDictionary(this, Reader); var num_elements = Reader.ReadInt32(); // read index foreach (var i in Enumerable.Range(0, num_elements)) { var type = StringDict.ReadString(); var name = EncodingVersion >= 4 ? StringDict.ReadString() : ReadString_Raw(); var id_bits = Reader.ReadBytes(16); var id = new Guid(BitConverter.IsLittleEndian ? id_bits : id_bits.Reverse().ToArray()); var elem = new Element(dm, name, id, type); } // read attributes (or not, if we're deferred) foreach (var elem in dm.AllElements.ToArray()) { System.Diagnostics.Debug.Assert(!elem.Stub); var num_attrs = Reader.ReadInt32(); foreach (var i in Enumerable.Range(0, num_attrs)) { var name = StringDict.ReadString(); if (defer_mode == DeferredMode.Automatic) { CodecUtilities.AddDeferredAttribute(elem, name, Reader.BaseStream.Position); SkipAttribte(); } else { elem.Add(name, DecodeAttribute(dm)); } } } return dm; }
/// <summary> /// Loads a Datamodel from a byte array. /// </summary> /// <param name="stream">The input Stream.</param> /// <param name="defer_mode">How to handle deferred loading.</param> public static Datamodel Load(byte[] data, DeferredMode defer_mode = DeferredMode.Automatic) { return(Load_Internal(new MemoryStream(data, true), defer_mode)); }
/// <summary> /// Loads a Datamodel from a <see cref="Stream"/>. /// </summary> /// <param name="stream">The input Stream.</param> /// <param name="defer_mode">How to handle deferred loading.</param> public static Datamodel Load(Stream stream, DeferredMode defer_mode = DeferredMode.Automatic) { return(Load_Internal(stream, defer_mode)); }