// Returns new header stringBlockOffset public int GenerateStringOffsetsMap(Binding.Binding binding) { // Start at 1 as 0 is hardcoded as '\0' int stringBlockOffset = 1; // Performance gain by collecting the fields to iterate first var fields = binding.Fields.Where(field => field.Type == BindingType.STRING_OFFSET).ToArray(); OffsetStorage = new Dictionary <int, int>(); ReverseStorage = new Dictionary <int, string>(); // Populate string <-> offset lookup maps for (int i = 0; i < Records.Count; ++i) { foreach (var entry in fields) { string str = Records[i][entry.Name].ToString(); if (str.Length == 0) { continue; } var key = str.GetHashCode(); if (!OffsetStorage.ContainsKey(key)) { OffsetStorage.Add(key, stringBlockOffset); ReverseStorage.Add(stringBlockOffset, str); stringBlockOffset += Encoding.UTF8.GetByteCount(str) + 1; } } } return(stringBlockOffset); }
public void RemoveBinding() { tlog.Debug(tag, $"EXamlExtensionsLoadFromEXamlPath START"); using (View view = new View()) { Assert.IsNotNull(view, "Create View failed"); try { view.RemoveBinding(View.PositionProperty); Binding.Binding binding = new Binding.Binding(); view.SetBinding(View.PositionProperty, binding); view.RemoveBinding(View.PositionProperty); } catch (Exception e) { tlog.Debug(tag, e.Message.ToString()); Assert.Fail("Caught Exception: Failed!"); } view.Dispose(); } tlog.Debug(tag, $"RemoveBinding END"); }
public void BindingConstructor() { tlog.Debug(tag, $"BindingConstructor START"); Binding.Binding t2 = new Binding.Binding(); Assert.IsNotNull(t2, "null Binding"); Assert.IsInstanceOf <Binding.Binding>(t2, "Should return Binding instance."); tlog.Debug(tag, $"BindingConstructor END"); }
public void ModeTest() { tlog.Debug(tag, $"ModeTest START"); Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Assert.IsInstanceOf <Binding.Binding>(t2, "Should return BindingBase instance."); var ret = t2.Mode; Assert.AreEqual(BindingMode.Default, ret, "Should be equal"); tlog.Debug(tag, $"ModeTest END"); }
public void ConverterTest() { tlog.Debug(tag, $"ConverterTest START"); Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Assert.IsInstanceOf <Binding.Binding>(t2, "Should return Binding instance."); var ret = t2.Converter; Assert.IsNull(ret, "null Converter"); tlog.Debug(tag, $"ConverterTest END"); }
public void BindingTest3() { tlog.Debug(tag, $"BindingTest3 START"); BindingCondition mb = new BindingCondition(); Assert.IsNotNull(mb, "Should not be null"); var b = new Binding.Binding(); mb.IsSealed = true; Assert.Throws <InvalidOperationException>(() => mb.Binding = b); tlog.Debug(tag, $"BindingTest3 END"); }
public void BindingTest2() { tlog.Debug(tag, $"BindingTest2 START"); DataTrigger dt = new DataTrigger(typeof(View)); Assert.IsNotNull(dt, "Should not be null"); var b = new Binding.Binding(); dt.Binding = b; tlog.Debug(tag, $"BindingTest2 END"); }
public void PathTest() { tlog.Debug(tag, $"PathTest START"); Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Assert.IsInstanceOf <Binding.Binding>(t2, "Should return Binding instance."); var ret = t2.Path; Assert.AreEqual("Test", ret, "null Path"); tlog.Debug(tag, $"PathTest END"); }
public void StringFormatTest() { tlog.Debug(tag, $"StringFormatTest START"); Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Assert.IsInstanceOf <Binding.Binding>(t2, "Should return BindingBase instance."); var ret = t2.StringFormat; Assert.IsNull(ret, "null StringFormat"); tlog.Debug(tag, $"StringFormatTest END"); }
public void ConditionChangedTest2() { tlog.Debug(tag, $"ConditionChangedTest2 START"); BindingCondition mb = new BindingCondition(); Assert.IsNotNull(mb, "Should not be null"); var b = new Binding.Binding(); var ac = (Action <BindableObject, bool, bool>)((bindable, b, c) => throw new Exception()); mb.ConditionChanged = ac; tlog.Debug(tag, $"ConditionChangedTest2 END"); }
public void SourceTest() { tlog.Debug(tag, $"SourceTest START"); Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Assert.IsInstanceOf <Binding.Binding>(t2, "Should return Binding instance."); var ret = t2.Source; Assert.IsNull(ret, "null Source"); tlog.Debug(tag, $"SourceTest END"); }
public string GetTableCreateString(Binding.Binding binding) { StringBuilder str = new StringBuilder(); str.Append(@"CREATE TABLE IF NOT EXISTS `{0}` ("); foreach (var field in binding.Fields) { switch (field.Type) { case BindingType.UINT: { str.Append(string.Format(@"`{0}` int(10) unsigned NOT NULL DEFAULT '0', ", field.Name)); break; } case BindingType.INT: { str.Append(string.Format(@"`{0}` int(11) NOT NULL DEFAULT '0', ", field.Name)); break; } case BindingType.FLOAT: { str.Append(string.Format(@"`{0}` FLOAT NOT NULL DEFAULT '0', ", field.Name)); break; } case BindingType.STRING_OFFSET: { str.Append(string.Format(@"`{0}` TEXT CHARACTER SET utf8, ", field.Name)); break; } default: throw new Exception($"ERROR: Unhandled type: {field.Type} on field: {field.Name} on binding: {binding.Name}"); } } var idField = binding.Fields.Where(record => record.Name.ToLower().Equals("id")).FirstOrDefault(); if (idField != null) { str.Append($"PRIMARY KEY (`{idField.Name}`)) "); } else { str = str.Remove(str.Length - 2, 2); str = str.Append(") "); } str.Append("ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;"); return(str.ToString()); }
public void IsAppliedTest() { tlog.Debug(tag, $"ContextTest START"); try { Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Assert.IsFalse(t2.IsApplied, "Should be false by default"); } catch (Exception e) { Assert.Fail("Caught Exception" + e.ToString()); } tlog.Debug(tag, $"ContextTest END"); }
public void BindingTest2() { tlog.Debug(tag, $"BindingTest2 START"); BindingCondition mb = new BindingCondition(); Assert.IsNotNull(mb, "Should not be null"); var b = new Binding.Binding(); mb.Binding = b; var ret = mb.Binding; //null Assert.AreEqual(b, ret, "Should be equal"); tlog.Debug(tag, $"BindingTest2 END"); }
public void ContextTest() { tlog.Debug(tag, $"ContextTest START"); try { Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); t2.Context = null; Assert.IsNull(t2.Context, "Should go here"); } catch (Exception e) { Assert.Fail("Caught Exception" + e.ToString()); } tlog.Debug(tag, $"ContextTest END"); }
public void CloneTest() { tlog.Debug(tag, $"CloneTest START"); try { Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Binding.Binding c = t2.Clone() as Binding.Binding; Assert.IsNotNull(c, "null Binding"); } catch (Exception e) { Assert.Fail("Caught Exception" + e.ToString()); } tlog.Debug(tag, $"CloneTest END"); }
public void ApplyTest() { tlog.Debug(tag, $"ApplyTest START"); try { Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); t2.Apply(false); Assert.True(true, "Should go here");; } catch (Exception e) { Assert.Fail("Caught Exception" + e.ToString()); } tlog.Debug(tag, $"ApplyTest END"); }
public string GetTableCreateString(Binding.Binding binding) { StringBuilder str = new StringBuilder(); str.Append(@"CREATE TABLE IF NOT EXISTS `{0}` ("); foreach (var field in binding.Fields) { switch (field.Type) { case BindingType.UINT: str.Append($@"`{field.Name}` INTEGER(10) NOT NULL DEFAULT '0', "); break; case BindingType.INT: str.Append($@"`{field.Name}` INTEGER(11) NOT NULL DEFAULT '0', "); break; case BindingType.UINT8: str.Append($@"`{field.Name}` TINYINT NOT NULL DEFAULT '0', "); break; case BindingType.FLOAT: str.Append($@"`{field.Name}` REAL NOT NULL DEFAULT '0', "); break; case BindingType.STRING_OFFSET: str.Append($@"`{field.Name}` TEXT, "); break; default: throw new Exception($"ERROR: Unhandled type: {field.Type} on field: {field.Name} on binding: {binding.Name}"); } } var idField = binding.Fields.FirstOrDefault(record => record.Name.ToLower().Equals("id")); if (idField != null && binding.OrderOutput) { str.Append($"PRIMARY KEY (`{idField.Name}`)"); } else { str.Remove(str.Length - 2, 2); } str.Append(");"); return(str.ToString()); }
public void AllowChainingTest() { tlog.Debug(tag, $"AllowChainingTest START"); try { Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); t2.AllowChaining = true; Assert.True(t2.AllowChaining, "Should be true"); t2.AllowChaining = false; Assert.False(t2.AllowChaining, "Should be false");; } catch (Exception e) { Assert.Fail("Caught Exception" + e.ToString()); } tlog.Debug(tag, $"AllowChainingTest END"); }
public void CloneTest() { tlog.Debug(tag, $"CloneTest START"); try { Binding.Binding t2 = new Binding.Binding("Test"); Assert.IsNotNull(t2, "null Binding"); Binding.Binding c = t2.Clone() as Binding.Binding; Assert.IsNotNull(c, "null Binding"); t2.TargetNullValue = new object(); var ret = t2.GetSourceValue(null, null); Assert.IsNotNull(ret, "Should not be null"); } catch (Exception e) { Assert.Fail("Caught Exception" + e.ToString()); } tlog.Debug(tag, $"CloneTest END"); }
protected void SaveDbcFile(MainWindow.UpdateProgressFunc updateProgress, DBCBodyToSerialize body, Binding.Binding binding) { string path = $"Export/{binding.Name}.dbc"; Directory.CreateDirectory(Path.GetDirectoryName(path)); if (File.Exists(path)) { File.Delete(path); } using (FileStream fileStream = new FileStream(path, FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(fileStream)) { // Write the header file int count = Marshal.SizeOf(typeof(DBCHeader)); byte[] buffer = new byte[count]; GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned); Marshal.StructureToPtr(Header, handle.AddrOfPinnedObject(), true); writer.Write(buffer, 0, count); handle.Free(); // Write each record for (int i = 0; i < Header.RecordCount; ++i) { if (i % 250 == 0) { // Visual studio says these casts are redundant but it does not work without them double percent = (double)i / (double)Header.RecordCount; updateProgress(percent); } var record = body.Records[i]; foreach (var entry in binding.Fields) { if (!record.Table.Columns.Contains(entry.Name)) { throw new Exception($"Column binding not found {entry.Name} in table, using binding {binding.Name}.txt"); } var data = record[entry.Name].ToString(); if (entry.Type == BindingType.INT) { if (int.TryParse(data, out int value)) { writer.Write(value); } else { writer.Write(0); } } else if (entry.Type == BindingType.UINT) { if (uint.TryParse(data, out uint value)) { writer.Write(value); } else { writer.Write(0u); } } else if (entry.Type == BindingType.FLOAT) { if (float.TryParse(data, out float value)) { writer.Write(value); } else { writer.Write(0f); } } else if (entry.Type == BindingType.DOUBLE) { if (double.TryParse(data, out double value)) { writer.Write(value); } else { writer.Write(0d); } } else if (entry.Type == BindingType.STRING_OFFSET) { writer.Write(data.Length == 0 ? 0 : body.OffsetStorage[data.GetHashCode()]); } else { throw new Exception($"Unknwon type: {entry.Type} on entry {entry.Name} binding {binding.Name}"); } } } // Write string block int[] offsetsStored = body.OffsetStorage.Values.ToArray(); writer.Write(Encoding.UTF8.GetBytes("\0")); for (int i = 0; i < offsetsStored.Length; ++i) { writer.Write(Encoding.UTF8.GetBytes(body.ReverseStorage[offsetsStored[i]] + "\0")); } } } }