private List <WargameUnit> GetUnits(List <CollectionItemValueHolder> references) { var units = new List <WargameUnit>(); instanceIdToUnitId = GetInstanceIdToUnitIdMap(references); unitIdToUnit = new Dictionary <uint, WargameUnit>(); unitIdToTransports = new Dictionary <uint, List <uint> >(); unitUpgradeTree = new Dictionary <uint, uint>(); //<unit, next unit> BuildUnitUpgradeTree(); foreach (var reference in references) { NdfMap map = (NdfMap)reference.Value; var unit = GetUnit(map); uint unitId = (uint)((NdfUInt32)map.Key.Value).Value; unitIdToTransports[unitId] = GetTransportsIds(unit); var rawCategory = GetCategory(unit); Categories category = categories.ContainsKey(rawCategory) ? categories[rawCategory] : Categories.UNKNOWN; var modulesProperty = unit.PropertyValues.Where(p => p.Property.Name == "Modules").First(); var modules = (NdfCollection)modulesProperty.Value; var filteredList = modules.Where(m => ((NdfStringReference)((NdfString)((NdfMap)m.Value).Key.Value).Value).Value == "TypeUnit").First(); var tModuleSelector = ((NdfObjectReference)((MapValueHolder)((NdfMap)filteredList.Value).Value).Value).Instance; var defaultProperty = tModuleSelector.PropertyValues.Where(p => p.Property.Name == "Default").First(); var typeUnitModuleDescriptor = ((NdfObjectReference)defaultProperty.Value).Instance; var filtersProperty = typeUnitModuleDescriptor.PropertyValues.Where(p => p.Property.Name == "Filters").First(); var subCategoryLocalizationHash = "None"; if (filtersProperty.Value.GetType() == typeof(NdfMapList)) { subCategoryLocalizationHash = ((NdfCollection)((MapValueHolder)((NdfMap)((NdfMapList)filtersProperty.Value)[0].Value).Value).Value)[0].Value.ToString(); } Subcategories subcategory = subcategories.ContainsKey(subCategoryLocalizationHash) ? subcategories[subCategoryLocalizationHash] : Subcategories.NONE; var wUnit = new WargameUnit(unitId, GetNumberOfCards(unit), category, subcategory, GetYear(unit), GetCountry(unit), IsPrototype(unit), IsCommander(unit), GetName(unit)); wUnit.AvailableVeterancy.AddRange(GetAvailableVeterancy(unit)); List <Specializations> specs = GetAvailableSpecializations(unit); foreach (var spec in specs) { wUnit.Specializations.Add(spec); } units.Add(wUnit); unitIdToUnit[unitId] = wUnit; } foreach (var unit in units) { foreach (var transportId in unitIdToTransports[unit.UnitId]) { unit.Transports.Add(unitIdToUnit[transportId]); } } return(units); }
private Dictionary <uint, uint> GetInstanceIdToUnitIdMap(List <CollectionItemValueHolder> references) { Dictionary <uint, uint> instanceIdToUnitId = new Dictionary <uint, uint>(); foreach (var reference in references) { NdfMap map = (NdfMap)reference.Value; NdfObjectReference unitReference = (NdfObjectReference)((MapValueHolder)map.Value).Value; instanceIdToUnitId[(uint)unitReference.InstanceId] = (uint)((NdfUInt32)map.Key.Value).Value; } return(instanceIdToUnitId); }
private NdfObject GetUnit(NdfMap map) { MapValueHolder valueHolder = (MapValueHolder)map.Value; NdfObjectReference unitReference = (NdfObjectReference)valueHolder.Value; ObservableCollection <NdfObject> instances; if (unitReference.Class.Name == "TUniteAuSolDescriptor") { instances = tUniteAuSolDescriptor.Instances; } else { instances = tUniteDescriptor.Instances; } return(instances.Where(i => i.Id == unitReference.InstanceId).First()); }
private NdfValueWrapper GetCopiedValue(IValueHolder toCopy) { NdfValueWrapper copiedValue = null; switch (toCopy.Value.Type) { case NdfType.ObjectReference: var origInst = toCopy.Value as NdfObjectReference; if (origInst != null && !origInst.Instance.IsTopObject) { copiedValue = new NdfObjectReference(origInst.Class, CopyInstance(origInst.Instance).Id); } else { copiedValue = NdfTypeManager.GetValue(toCopy.Value.GetBytes(), toCopy.Value.Type, toCopy.Manager); } break; case NdfType.List: case NdfType.MapList: var copiedItems = new List <CollectionItemValueHolder>(); var collection = toCopy.Value as NdfCollection; if (collection != null) { copiedItems.AddRange(collection.Select(entry => new CollectionItemValueHolder(GetCopiedValue(entry), toCopy.Manager))); } copiedValue = new NdfCollection(copiedItems); break; case NdfType.Map: var map = toCopy.Value as NdfMap; if (map != null) { copiedValue = new NdfMap(new MapValueHolder(GetCopiedValue(map.Key), toCopy.Manager), new MapValueHolder(GetCopiedValue(map.Value as IValueHolder), toCopy.Manager), toCopy.Manager); } break; default: copiedValue = NdfTypeManager.GetValue(toCopy.Value.GetBytes(), toCopy.Value.Type, toCopy.Manager); break; } return(copiedValue); }
/// <summary> /// Return true if it succeded getting a property from the query and cast it into T, then output it. /// </summary> /// <param name="propertyPath"></param> /// <param name="value"></param> /// <returns></returns> public bool TryGetValueFromQuery<T>(string query, out T value) where T : NdfValueWrapper { NdfValueWrapper val = null; value = null; if (TryGetValueFromQuery(query, out val)) { value = val as T; if(val.Type == Types.NdfType.Map) { NdfMap map = val as NdfMap; MapValueHolder mapval = map.Value as MapValueHolder; value = mapval.Value as T; } if (value != null) return true; } return false; }
/// <summary> /// Reads the value of a Property inside a object instance. /// </summary> /// <param name="ms"></param> /// <param name="binary"></param> /// <returns>A NdfValueWrapper Instance.</returns> protected NdfValueWrapper ReadValue(Stream ms, NdfBinary binary) { uint contBufferlen; NdfValueWrapper value; var buffer = new byte[4]; ms.Read(buffer, 0, buffer.Length); NdfType type = NdfTypeManager.GetType(buffer); if (type == NdfType.Unknown) { throw new InvalidDataException("Unknown datatypes are not supported!"); } if (type == NdfType.Reference) { ms.Read(buffer, 0, buffer.Length); type = NdfTypeManager.GetType(buffer); } switch (type) { case NdfType.Unknown: case NdfType.WideString: case NdfType.List: case NdfType.MapList: case NdfType.Blob: case NdfType.ZipBlob: ms.Read(buffer, 0, buffer.Length); contBufferlen = BitConverter.ToUInt32(buffer, 0); if (type == NdfType.ZipBlob) { if (ms.ReadByte() != 1) { throw new InvalidDataException("has to be checked."); } } break; default: contBufferlen = NdfTypeManager.SizeofType(type); break; } switch (type) { case NdfType.MapList: case NdfType.List: NdfCollection lstValue = type == NdfType.List ? new NdfCollection() : new NdfMapList(); for (int i = 0; i < contBufferlen; i++) { CollectionItemValueHolder res; if (type == NdfType.List) { res = new CollectionItemValueHolder(ReadValue(ms, binary), binary); } else { res = new CollectionItemValueHolder( new NdfMap( new MapValueHolder(ReadValue(ms, binary), binary), new MapValueHolder(ReadValue(ms, binary), binary), binary), binary); } lstValue.Add(res); } value = lstValue; break; case NdfType.Map: value = new NdfMap( new MapValueHolder(ReadValue(ms, binary), binary), new MapValueHolder(ReadValue(ms, binary), binary), binary); break; default: var contBuffer = new byte[contBufferlen]; ms.Read(contBuffer, 0, contBuffer.Length); value = NdfTypeManager.GetValue(contBuffer, type, binary); break; } return(value); }
/// <summary> /// Reads the value of a Property inside a object instance. /// </summary> /// <param name="ms"></param> /// <param name="triggerBreak"></param> /// <param name="prop"></param> /// <returns>A NdfValueWrapper Instance.</returns> protected NdfValueWrapper ReadValue(MemoryStream ms, out bool triggerBreak, NdfPropertyValue prop) { var buffer = new byte[4]; uint contBufferlen; NdfValueWrapper value; triggerBreak = false; ms.Read(buffer, 0, buffer.Length); var type = NdfTypeManager.GetType(buffer); if (type == NdfType.Reference) { ms.Read(buffer, 0, buffer.Length); type = NdfTypeManager.GetType(buffer); } switch (type) { case NdfType.WideString: case NdfType.List: case NdfType.MapList: ms.Read(buffer, 0, buffer.Length); contBufferlen = BitConverter.ToUInt32(buffer, 0); break; default: contBufferlen = NdfTypeManager.SizeofType(type); break; } if (type == NdfType.Unknown) { //var t = _unknownTypes.SingleOrDefault(x => Utils.ByteArrayCompare(x, buffer)); //if (t == default(byte[])) //{ // _unknownTypes.Add(buffer); // _unknownTypesCount.Add(1); //} //else // _unknownTypesCount[_unknownTypes.IndexOf(t)]++; triggerBreak = true; return(new NdfUnkown(buffer, ms.Position)); } if (type == NdfType.List || type == NdfType.MapList) { NdfCollection lstValue; if (type == NdfType.List) { lstValue = new NdfCollection(ms.Position); } else { lstValue = new NdfMapList(ms.Position); } CollectionItemValueHolder res; for (int i = 0; i < contBufferlen; i++) { if (type == NdfType.List) { res = new CollectionItemValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset); } else { res = new CollectionItemValueHolder( new NdfMap( new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset), new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset), ms.Position), this, prop.InstanceOffset); } lstValue.Add(res); if (triggerBreak) { break; } } value = lstValue; } else if (type == NdfType.Map) { value = new NdfMap( new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset), new MapValueHolder(ReadValue(ms, out triggerBreak, prop), this, prop.InstanceOffset), ms.Position); } else { var contBuffer = new byte[contBufferlen]; ms.Read(contBuffer, 0, contBuffer.Length); if (prop != null) { prop.ValueData = contBuffer; } value = NdfTypeManager.GetValue(contBuffer, type, this, ms.Position - contBuffer.Length); } return(value); }