private void PrepareClassDefFields(ClassDef result) { for (short i = 0; i < result.Meta.Items.Count; ++i) { var yi = result.Meta.Items[i]; short j = (short)(i + 1); // Capture. var wf = GetWriteFunc(yi.Type); var fd = new ClassDef.FieldDef { Name = yi.Tag(Options), Type = yi.Type }; if (yi.SerializeCond != null) { fd.WriteFunc = obj => { var value = yi.GetValue(obj); if (!yi.SerializeCond(obj, value)) { return; } writer.Write(j); wf(value); } } ; else { fd.WriteFunc = obj => { writer.Write(j); wf(yi.GetValue(obj)); } }; fd.WriteFuncCompact = obj => wf(yi.GetValue(obj)); result.Fields.Add(fd); } }
private void PrepareClassDefFieldsUnknown(ClassDef result) { for (int ourIndex = 0, theirIndex = 1, i = 0; ; ++i) { var yi = ourIndex < result.Meta.Items.Count ? result.Meta.Items[ourIndex] : null; var their = theirIndex < result.ReaderDef.Fields.Count ? result.ReaderDef.Fields[theirIndex] : null; if (yi == null && their == null) { break; } short j = (short)(i + 1); // Capture. var ourName = yi == null ? null : yi.Tag(Options); var cmp = their == null ? -1 : yi == null ? 1 : String.CompareOrdinal(ourName, their.Name); if (cmp <= 0) { var wf = GetWriteFunc(yi.Type); var fd = new ClassDef.FieldDef { Name = ourName, Type = yi.Type }; if (yi.SerializeCond != null) { fd.WriteFuncUnknown = (obj, storage, storageIndex) => { var value = yi.GetValue(obj); if (!yi.SerializeCond(obj, value)) { return; } writer.Write(j); wf(value); } } ; else { fd.WriteFuncUnknown = (obj, storage, storageIndex) => { writer.Write(j); wf(yi.GetValue(obj)); } }; result.Fields.Add(fd); ++ourIndex; if (cmp == 0) { ++theirIndex; } } else { var theirType = result.ReaderDef.Fields[theirIndex].Type; var wf = GetWriteFunc(theirType); result.Fields.Add(new ClassDef.FieldDef { Name = their.Name, Type = theirType, WriteFuncUnknown = (obj, storage, storageIndex) => { var si = storageIndex.Value; if (si < storage.Fields.Count && storage.Fields[si].Name == their.Name) { writer.Write(j); wf(storage.Fields[si].Value); ++storageIndex.Value; } } }); ++theirIndex; } } }