protected static void Populate(Datamodel.Datamodel dm, string encoding_name, int encoding_version) { dm.Root = new Element(dm, "root", RootGuid); foreach (var value in AttributeValuesFor(encoding_name, encoding_version)) { if (value == null) { continue; } var name = value.GetType().Name; dm.Root[name] = value; Assert.AreSame(value, dm.Root[name]); name += " array"; var list = value.GetType().MakeListType().GetConstructor(Type.EmptyTypes).Invoke(null) as IList; list.Add(value); list.Add(value); dm.Root[name] = list; Assert.AreSame(list, dm.Root[name]); } dm.Root["Recursive"] = dm.Root; dm.Root["NoName"] = new Element(); dm.Root["ElemArray"] = new ElementArray(new Element[] { new Element(dm, Guid.NewGuid()), new Element(), dm.Root, new Element(dm, "TestElement") }); dm.Root["ElementStub"] = new Element(dm, Guid.NewGuid()); }
protected void SaveAndConvert(Datamodel.Datamodel dm, string encoding, int version) { dm.Save(DmxSavePath, encoding, version); var dmxconvert = new System.Diagnostics.Process(); dmxconvert.StartInfo = new System.Diagnostics.ProcessStartInfo() { FileName = System.IO.Path.Combine(Properties.Resources.ValveSourceBinaries, "dmxconvert.exe"), Arguments = String.Format("-i \"{0}\" -o \"{1}\" -oe {2}", DmxSavePath, DmxConvertPath, encoding), UseShellExecute = false, CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, }; Console.WriteLine(String.Join(" ", dmxconvert.StartInfo.FileName, dmxconvert.StartInfo.Arguments)); Assert.IsTrue(File.Exists(dmxconvert.StartInfo.FileName), String.Format("Could not find dmxconvert at {0}", dmxconvert.StartInfo.FileName)); Console.WriteLine(); dmxconvert.Start(); var err = dmxconvert.StandardOutput.ReadToEnd(); err += dmxconvert.StandardError.ReadToEnd(); dmxconvert.WaitForExit(); Console.WriteLine(err); if (dmxconvert.ExitCode != 0) { throw new AssertFailedException(err); } }
/// <summary> /// Gets or sets the value of the <see cref="Attribute"/> with the given name. /// </summary> /// <param name="name">The name to search for. Cannot be null.</param> /// <returns>The value associated with the given name.</returns> /// <exception cref="ArgumentNullException">Thrown when the value of name is null.</exception> /// <exception cref="KeyNotFoundException">Thrown when an attempt is made to get a name that is not present in this AttributeList.</exception> /// <exception cref="ElementOwnershipException">Thrown when an attempt is made to set the value of the attribute to an Element from a different <see cref="Datamodel"/>.</exception> /// <exception cref="AttributeTypeException">Thrown when an attempt is made to set a value that is not of a valid Datamodel attribute type.</exception> /// <exception cref="IndexOutOfRangeException">Thrown when the maximum number of Attributes allowed in an AttributeList has been reached.</exception> public virtual object this[string name] { get { if (name == null) { throw new ArgumentNullException("name"); } var attr = (Attribute)Inner[name]; if (attr == null) { throw new KeyNotFoundException(String.Format("{0} does not have an attribute called \"{1}\"", this, name)); } return(attr.Value); } set { if (name == null) { throw new ArgumentNullException("name"); } if (value != null && !Datamodel.IsDatamodelType(value.GetType())) { throw new AttributeTypeException(String.Format("{0} is not a valid Datamodel attribute type. (If this is an array, it must implement IList<T>).", value.GetType().FullName)); } if (Owner != null && this == Owner.PrefixAttributes && value.GetType() == typeof(Element)) { throw new AttributeTypeException("Elements are not supported as prefix attributes."); } Attribute old_attr; Attribute new_attr; int old_index = -1; lock (Attribute_ChangeLock) { old_attr = (Attribute)Inner[name]; new_attr = new Attribute(name, this, value); if (old_attr != null) { old_index = IndexOf(old_attr.Name); Inner.Remove(old_attr); } Insert(old_index == -1 ? Count : old_index, new Attribute(name, this, value), notify: false); } NotifyCollectionChangedEventArgs change_args; if (old_attr != null) { change_args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Replace, new_attr.ToKeyValuePair(), old_attr.ToKeyValuePair(), old_index); } else { change_args = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, new_attr.ToKeyValuePair(), Count); } OnCollectionChanged(change_args); } }
/// <summary> /// Creates a new Element with a specified name, optionally specifying an ID and class name. /// </summary> /// <param name="owner">The owner of this Element. Cannot be null.</param> /// <param name="id">A GUID that must be unique within the owning Datamodel. Can be null, in which case a random GUID is generated.</param> /// <param name="name">An arbitrary string. Does not have to be unique, and can be null.</param> /// <param name="class_name">An arbitrary string which loosely defines the type of Element this is. Cannot be null.</param> /// <exception cref="IndexOutOfRangeException">Thrown when the owner already contains the maximum number of Elements allowed in a Datamodel.</exception> public Element(Datamodel owner, string name, Guid?id = null, string class_name = "DmElement") : base(owner) { if (owner == null) { throw new ArgumentNullException("owner"); } if (class_name == null) { throw new ArgumentNullException("class_name"); } Name = name; ClassName = class_name; if (id.HasValue) { _ID = id.Value; } else { if (!owner.AllowRandomIDs) { throw new InvalidOperationException("Random IDs are not allowed in this Datamodel."); } _ID = Guid.NewGuid(); } Owner = owner; }
/// <summary> /// Perform a parallel loop over all elements and attributes /// </summary> protected void PrintContents(Datamodel.Datamodel dm) { System.Threading.Tasks.Parallel.ForEach <Datamodel.Element>(dm.AllElements, e => { System.Threading.Tasks.Parallel.ForEach(e, a => {; }); }); }
public override string ToString() { var type = Value != null?Value.GetType() : typeof(Element); var inner_type = Datamodel.GetArrayInnerType(type); return(String.Format("{0} <{1}>", Name, inner_type != null ? inner_type.FullName + "[]" : type.FullName)); }
public ComparisonDatamodel(Datamodel.Datamodel dm_left, Datamodel.Datamodel dm_right) { Datamodel_Left = dm_left; Datamodel_Right = dm_right; ComparedElements = new Dictionary <Guid, Element>(); Root = new ComparisonDatamodel.Element(this, Datamodel_Left.Root, Datamodel_Right.Root); }
public void Dota2_Binary_9() { var dm = DM.Load(Binary_9_File); PrintContents(dm); dm.Root.Get <Element>("skeleton").GetArray <Element>("children")[0].Any(); SaveAndConvert(dm, "binary", 9); Cleanup(); }
public void TF2_Binary_4() { var dm = DM.Load(Binary_4_File); PrintContents(dm); Get_TF2(dm); SaveAndConvert(dm, "binary", 4); Cleanup(); }
public void TF2_KeyValues2_1() { var dm = DM.Load(KeyValues2_1_File); PrintContents(dm); Get_TF2(dm); SaveAndConvert(dm, "keyvalues2", 1); Cleanup(); }
private void New_Executed(object sender, ExecutedRoutedEventArgs e) { var dm = new Datamodel.Datamodel("my_format", 1); dm.Root = new Element(dm, "root"); var vm = new ViewModel(dm); Datamodels.Add(vm); Tabs.SelectedItem = vm; e.Handled = true; }
private void CompareVector(DM dm, string name, float[] actual) { var expected = (IEnumerable <float>)dm.Root[name]; Assert.AreEqual(actual.Count(), expected.Count()); foreach (var t in actual.Zip(expected, (a, e) => new Tuple <float, float>(a, e))) { Assert.AreEqual(t.Item1, t.Item2, 1e-6, name); } }
/// <summary> /// Creates a new stub Element to represent an Element in another Datamodel. /// </summary> /// <seealso cref="Element.Stub"/> /// <param name="owner">The owner of this Element. Cannot be null.</param> /// <param name="id">The ID of the remote Element that this stub represents.</param> public Element(Datamodel owner, Guid id) : base(owner) { if (owner == null) { throw new ArgumentNullException("owner"); } _ID = id; Stub = true; Name = "Stub element"; Owner = owner; }
public IEnumerator GetEnumerator() { if (Value == null) { yield break; } if (Value is Element || Value is ElementArray || (Datamodel.IsDatamodelArrayType(Value.GetType()) && ArrayExpansion == ArrayExpandMode.AllArrays)) { foreach (var item in WrapEnumerable((IEnumerable)Value)) { yield return(item); } } }
void Load(FileStream f) { long elapsed = 0; Timer.Start(); foreach (var i in Enumerable.Range(0, Load_Iterations + 1)) { DM.Load(f, Datamodel.Codecs.DeferredMode.Disabled); if (i > 0) { Console.WriteLine(Timer.ElapsedMilliseconds); elapsed += Timer.ElapsedMilliseconds; } Timer.Restart(); } Timer.Stop(); Console.WriteLine("Average: {0}ms", elapsed / Load_Iterations); }
/// <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 new bool Equals(object x, object y) { var type_x = x == null ? null : x.GetType(); var type_y = y == null ? null : y.GetType(); if (type_x == null && type_y == null) { return(true); } if (type_x != type_y) { return(false); } var inner = Datamodel.GetArrayInnerType(type_x); if (inner != null) { var array_left = (IList)x; var array_right = (IList)y; if (array_left.Count != array_right.Count) { return(false); } return(!Enumerable.Range(0, array_left.Count).Any(i => !Equals(array_left[i], array_right[i]))); } else if (type_x == typeof(Element)) { return(Element.IDComparer.Default.Equals((Element)x, (Element)y)); } else { return(EqualityComparer <object> .Default.Equals(x, y)); } }
protected void ValidatePopulated(string encoding_name, int encoding_version) { var dm = DM.Load(DmxConvertPath); Assert.AreEqual(RootGuid, dm.Root.ID); foreach (var value in AttributeValuesFor(encoding_name, encoding_version)) { if (value == null) { continue; } var name = value.GetType().Name; if (value is ICollection) { CollectionAssert.AreEqual((ICollection)value, (ICollection)dm.Root[name]); } else if (value is System.Drawing.Color) { Assert.AreEqual(((System.Drawing.Color)value).ToArgb(), dm.Root.Get <System.Drawing.Color>(name).ToArgb()); } else if (value is Quaternion) { var quat = (Quaternion)value; var expected = dm.Root.Get <Quaternion>(name); Assert.AreEqual(quat.W, expected.W, 1e-6, name + " W"); Assert.AreEqual(quat.X, expected.X, 1e-6, name + " X"); Assert.AreEqual(quat.Y, expected.Y, 1e-6, name + " Y"); Assert.AreEqual(quat.Z, expected.Z, 1e-6, name + " Z"); } else { Assert.AreEqual(value, dm.Root[name], name); } } dm.Dispose(); }
public int GetHashCode(object obj) { var elem = obj as Element; if (elem != null) { return(elem.ID.GetHashCode()); } var inner = Datamodel.GetArrayInnerType(obj.GetType()); if (inner != null) { int hash = 0; foreach (var item in (IList)obj) { hash ^= item.GetHashCode(); } return(hash); } return(obj.GetHashCode()); }
public AttributeList(Datamodel owner) { Inner = new OrderedDictionary(); Owner = owner; }
private void CompareVector(DM dm, string name, float[] actual) { var expected = (IEnumerable<float>)dm.Root[name]; Assert.AreEqual(actual.Count(), expected.Count()); foreach (var t in actual.Zip(expected, (a, e) => new Tuple<float, float>(a, e))) Assert.AreEqual(t.Item1, t.Item2, 1e-6, name); }
public ViewModel(Datamodel.Datamodel datamodel) { Datamodel = datamodel; }
public DebugView(Datamodel dm) { DM = dm; }
void Get_TF2(Datamodel.Datamodel dm) { dm.Root.Get <Element>("skeleton").GetArray <Element>("children")[0].Any(); dm.FormatVersion = 22; // otherwise recent versions of dmxconvert fail }
internal ElementList(Datamodel owner) { Owner = owner; }
static Datamodel() { Datamodel.RegisterCodec(typeof(Codecs.Binary)); Datamodel.RegisterCodec(typeof(Codecs.KeyValues2)); TextEncoding = new System.Text.UTF8Encoding(false); }