public static PaletteModel MapToModel(this Palette pal, Dictionary <IProjectResource, string> resourceMap, IColorFactory colorFactory) { var size = colorFactory.CreateColor(pal.ColorModel).Size; var model = new PaletteModel() { Name = pal.Name, ColorModel = pal.ColorModel, ZeroIndexTransparent = pal.ZeroIndexTransparent, }; if (pal.DataSource is not null && resourceMap.TryGetValue(pal.DataSource, out var dataFileKey)) { model.DataFileKey = dataFileKey; } int i = 0; while (i < pal.ColorSources.Length) { if (pal.ColorSources[i] is FileColorSource fileSource) { var sources = pal.ColorSources.Skip(i) .TakeWhile((x, i) => x is FileColorSource && (x as FileColorSource).Offset == (fileSource.Offset + i * size)) .ToList(); var sourceModel = new FileColorSourceModel(fileSource.Offset, sources.Count, fileSource.Endian); model.ColorSources.Add(sourceModel); i += sources.Count; } else if (pal.ColorSources[i] is ProjectNativeColorSource nativeSource) { var nativeModel = new ProjectNativeColorSourceModel(nativeSource.Value); model.ColorSources.Add(nativeModel); i++; } else if (pal.ColorSources[i] is ProjectForeignColorSource foreignSource) { var foreignModel = new ProjectForeignColorSourceModel(foreignSource.Value); model.ColorSources.Add(foreignModel); i++; } else if (pal.ColorSources[i] is ScatteredColorSource scatteredSource) { } } return(model); }
private bool TryDeserializePalette(XElement element, string resourceName, out PaletteModel paletteModel) { var model = new PaletteModel(); model.Name = resourceName; model.DataFileKey = element.Attribute("datafile").Value; model.ColorModel = Palette.StringToColorModel(element.Attribute("color").Value); model.ZeroIndexTransparent = bool.Parse(element.Attribute("zeroindextransparent").Value); foreach (var item in element.Elements()) { if (item.Name.LocalName == "filesource") { var source = new FileColorSourceModel(); var fileOffset = long.Parse(item.Attribute("fileoffset").Value, System.Globalization.NumberStyles.HexNumber); if (item.Attribute("bitoffset") is null) { source.FileAddress = new BitAddress(fileOffset, 0); } else { source.FileAddress = new BitAddress(fileOffset, int.Parse(element.Attribute("bitoffset").Value)); } source.Entries = int.Parse(item.Attribute("entries").Value); if (item.Attribute("endian") is not null) { if (item.Attribute("endian").Value == "big") { source.Endian = Endian.Big; } else if (item.Attribute("endian").Value == "little") { source.Endian = Endian.Little; } else { _errors.Add($"'endian' has unknown value '{item.Attribute("endian").Value}'"); } } model.ColorSources.Add(source); } else if (item.Name.LocalName == "nativecolor") { if (ColorParser.TryParse(item.Attribute("value").Value, ColorModel.Rgba32, out var nativeColor)) { model.ColorSources.Add(new ProjectNativeColorSourceModel((ColorRgba32)nativeColor)); } } else if (item.Name.LocalName == "foreigncolor") { if (ColorParser.TryParse(item.Attribute("value").Value, model.ColorModel, out var foreignColor)) { model.ColorSources.Add(new ProjectForeignColorSourceModel(foreignColor)); } } else if (item.Name.LocalName == "scatteredcolor") { } else if (item.Name.LocalName == "import") { } else if (item.Name.LocalName == "export") { } } paletteModel = model; return(true); }