From 1b6a9c0c3b6c6a213145a73ea98a84bad8f02723 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sun, 30 Jun 2013 15:39:03 +0200 Subject: [PATCH] Added validation on all settings. #611 --- lib/Slic3r/Config.pm | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm index 83088f58d..0ef9b7803 100644 --- a/lib/Slic3r/Config.pm +++ b/lib/Slic3r/Config.pm @@ -1340,6 +1340,33 @@ sub validate { # --extrusion-multiplier die "Invalid value for --extrusion-multiplier\n" if defined first { $_ <= 0 } @{$self->extrusion_multiplier}; + + # general validation, quick and dirty + foreach my $opt_key (keys %$Options) { + my $opt = $Options->{$opt_key}; + next unless defined $self->$opt_key; + next unless defined $opt->{cli} && $opt->{cli} =~ /=(.+)$/; + my $type = $1; + my @values = (); + if ($type =~ s/\@$//) { + die "Invalid value for $opt_key\n" if ref($self->$opt_key) ne 'ARRAY'; + @values = @{ $self->$opt_key }; + } else { + @values = ($self->$opt_key); + } + foreach my $value (@values) { + if ($type eq 'i' || $type eq 'f') { + die "Invalid value for $opt_key\n" + if ($type eq 'i' && $value !~ /^\d+$/) + || ($type eq 'f' && $value !~ /^(?:\d+|\d*\.\d+)$/) + || (defined $opt->{min} && $value < $opt->{min}) + || (defined $opt->{max} && $value > $opt->{max}); + } elsif ($type eq 's' && $opt->{type} eq 'select') { + die "Invalid value for $opt_key\n" + unless first { $_ eq $value } @{ $opt->{values} }; + } + } + } } sub replace_options {