From f69e2d06ebca2a59df20f5c781b4d3846395d4ec Mon Sep 17 00:00:00 2001 From: Anaminus Date: Sun, 9 Jun 2024 11:19:06 +0000 Subject: [PATCH] Implement Columns field of imported .gpl palettes. (#1025) * Implement Columns field of imported .gpl palettes. - Fixes Columns field being ignored when imported. - _fill_imported_palette_with_colors is modified to accept an optional width parameter, which defaults to Palette.DEFAULT_WIDTH instead of a hard-coded 8. - A maximum of width of 16384 is enforced to mitigate maliciously large values. * Run gdformat. * Run gdlint. * Consume all whitespace between field and value. --- src/Autoload/Palettes.gd | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/Autoload/Palettes.gd b/src/Autoload/Palettes.gd index e348a7a74..d7209aebd 100644 --- a/src/Autoload/Palettes.gd +++ b/src/Autoload/Palettes.gd @@ -10,6 +10,8 @@ enum NewPalettePresetType {EMPTY, FROM_CURRENT_PALETTE, FROM_CURRENT_SPRITE, FRO ## Color options when user creates a new palette from current sprite or selection enum GetColorsFrom { CURRENT_FRAME, CURRENT_CEL, ALL_FRAMES } const DEFAULT_PALETTE_NAME := "Default" +## Maximum allowed width of imported palettes. +const MAX_IMPORT_PAL_WIDTH = 1 << 14 var palettes_write_path := Global.home_data_directory.path_join("Palettes") ## All available palettes var palettes := {} @@ -447,6 +449,7 @@ func _import_gpl(path: String, text: String) -> Palette: var line_number := 0 var palette_name := path.get_basename().get_file() var comments := "" + var columns := 0 var colors := PackedColorArray() for line in lines: @@ -464,8 +467,11 @@ func _import_gpl(path: String, text: String) -> Palette: elif line.begins_with("Name: "): palette_name = line.replace("Name: ", "") elif line.begins_with("Columns: "): - # Number of colors in this palette. Unnecessary and often wrong - continue + # The width of the palette. + line = line.trim_prefix("Columns: ").strip_edges() + if !line.is_valid_int(): + continue + columns = line.to_int() elif line_number > 0 && line.length() >= 9: line = line.replace("\t", " ") var color_data: PackedStringArray = line.split(" ", false, 4) @@ -481,7 +487,7 @@ func _import_gpl(path: String, text: String) -> Palette: line_number += 1 if line_number > 0: - return _fill_imported_palette_with_colors(palette_name, colors, comments) + return _fill_imported_palette_with_colors(palette_name, colors, comments, columns) return result @@ -522,13 +528,20 @@ func _import_image_palette(path: String, image: Image) -> Palette: return _fill_imported_palette_with_colors(path.get_basename().get_file(), colors) -## Fills a new [Palette] with colors. Used when importing files. -## TODO: Somehow let the users choose the fixed height or width, instead of hardcoding 8. +## Fills a new [Palette] with colors. Used when importing files. Dimensions are +## determined by taking colors as a one-dimensional array that is wrapped by +## width. func _fill_imported_palette_with_colors( - palette_name: String, colors: PackedColorArray, comment := "" + palette_name: String, + colors: PackedColorArray, + comment := "", + width := Palette.DEFAULT_WIDTH, ) -> Palette: - var height := ceili(colors.size() / 8.0) - var result := Palette.new(palette_name, 8, height, comment) + if width <= 0: + width = Palette.DEFAULT_WIDTH + width = clampi(width, 1, MAX_IMPORT_PAL_WIDTH) + var height := ceili(colors.size() / float(width)) + var result := Palette.new(palette_name, width, height, comment) for color in colors: result.add_color(color)