From 989ec5cf4d2c0ac7765380deb660070ec5669f75 Mon Sep 17 00:00:00 2001 From: Alessandro Ranellucci Date: Sat, 22 Nov 2014 21:55:45 +0100 Subject: [PATCH] Fixes to UTF-8 handling in file paths --- lib/Slic3r.pm | 5 +++++ lib/Slic3r/Config.pm | 3 ++- lib/Slic3r/GUI.pm | 11 ++++++----- lib/Slic3r/GUI/MainFrame.pm | 2 +- slic3r.pl | 7 ++++++- 5 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/Slic3r.pm b/lib/Slic3r.pm index 7abbf228a..5638dd563 100644 --- a/lib/Slic3r.pm +++ b/lib/Slic3r.pm @@ -234,6 +234,11 @@ sub encode_path { return encode('locale_fs', $filename); } +sub decode_path { + my ($filename) = @_; + return decode('locale_fs', $filename); +} + sub open { my ($fh, $mode, $filename) = @_; return CORE::open $$fh, $mode, encode_path($filename); diff --git a/lib/Slic3r/Config.pm b/lib/Slic3r/Config.pm index dddc1461f..529984729 100644 --- a/lib/Slic3r/Config.pm +++ b/lib/Slic3r/Config.pm @@ -380,7 +380,8 @@ sub read_ini { my ($file) = @_; local $/ = "\n"; - Slic3r::open(\my $fh, '<', $file); + Slic3r::open(\my $fh, '<', $file) + or die "Unable to open $file: $!\n"; binmode $fh, ':utf8'; my $ini = { _ => {} }; diff --git a/lib/Slic3r/GUI.pm b/lib/Slic3r/GUI.pm index 842ff4407..969c7fa40 100644 --- a/lib/Slic3r/GUI.pm +++ b/lib/Slic3r/GUI.pm @@ -75,20 +75,20 @@ sub OnInit { # locate or create data directory $datadir ||= Wx::StandardPaths::Get->GetUserDataDir; - $datadir = Slic3r::encode_path($datadir); + my $enc_datadir = Slic3r::encode_path($datadir); Slic3r::debugf "Data directory: %s\n", $datadir; # just checking for existence of $datadir is not enough: it may be an empty directory # supplied as argument to --datadir; in that case we should still run the wizard - my $run_wizard = (-d $datadir && -e "$datadir/slic3r.ini") ? 0 : 1; - for ($datadir, "$datadir/print", "$datadir/filament", "$datadir/printer") { + my $run_wizard = (-d $enc_datadir && -e "$enc_datadir/slic3r.ini") ? 0 : 1; + for ($enc_datadir, "$enc_datadir/print", "$enc_datadir/filament", "$enc_datadir/printer") { mkdir or $self->fatal_error("Slic3r was unable to create its data directory at $_ (errno: $!).") unless -d $_; } # load settings my $last_version; - if (-f "$datadir/slic3r.ini") { + if (-f "$enc_datadir/slic3r.ini") { my $ini = eval { Slic3r::Config->read_ini("$datadir/slic3r.ini") }; $Settings = $ini if $ini; $last_version = $Settings->{_}{version}; @@ -212,7 +212,8 @@ sub presets { my ($self, $section) = @_; my %presets = (); - opendir my $dh, "$Slic3r::GUI::datadir/$section" or die "Failed to read directory $Slic3r::GUI::datadir/$section (errno: $!)\n"; + opendir my $dh, Slic3r::encode_path("$Slic3r::GUI::datadir/$section") + or die "Failed to read directory $Slic3r::GUI::datadir/$section (errno: $!)\n"; foreach my $file (grep /\.ini$/i, readdir $dh) { my $name = basename($file); $name =~ s/\.ini$//; diff --git a/lib/Slic3r/GUI/MainFrame.pm b/lib/Slic3r/GUI/MainFrame.pm index 8c4d66dbd..76371292a 100644 --- a/lib/Slic3r/GUI/MainFrame.pm +++ b/lib/Slic3r/GUI/MainFrame.pm @@ -77,7 +77,7 @@ sub _init_tabpanel { my $simple_config; if ($self->{mode} eq 'simple') { $simple_config = Slic3r::Config->load("$Slic3r::GUI::datadir/simple.ini") - if -e "$Slic3r::GUI::datadir/simple.ini"; + if -e Slic3r::encode_path("$Slic3r::GUI::datadir/simple.ini"); } my $class_prefix = $self->{mode} eq 'simple' ? "Slic3r::GUI::SimpleTab::" : "Slic3r::GUI::Tab::"; diff --git a/slic3r.pl b/slic3r.pl index 6dce339ba..8c3a9c2ca 100755 --- a/slic3r.pl +++ b/slic3r.pl @@ -60,6 +60,7 @@ my %cli_options = (); my @external_configs = (); if ($opt{load}) { foreach my $configfile (@{$opt{load}}) { + $configfile = Slic3r::decode_path($configfile); if (-e $configfile) { push @external_configs, Slic3r::Config->load($configfile); } elsif (-e "$FindBin::Bin/$configfile") { @@ -92,7 +93,7 @@ my $gui; if (!@ARGV && !$opt{save} && eval "require Slic3r::GUI; 1") { { no warnings 'once'; - $Slic3r::GUI::datadir = $opt{datadir}; + $Slic3r::GUI::datadir = Slic3r::decode_path($opt{datadir}); $Slic3r::GUI::no_plater = $opt{no_plater}; $Slic3r::GUI::mode = $opt{gui_mode}; $Slic3r::GUI::autosave = $opt{autosave}; @@ -111,6 +112,7 @@ if (@ARGV) { # slicing from command line if ($opt{repair}) { foreach my $file (@ARGV) { + $file = Slic3r::decode_path($file); die "Repair is currently supported only on STL files\n" if $file !~ /\.stl$/i; @@ -126,6 +128,7 @@ if (@ARGV) { # slicing from command line if ($opt{cut}) { foreach my $file (@ARGV) { + $file = Slic3r::decode_path($file); my $model = Slic3r::Model->read_from_file($file); $model->add_default_instances; my $mesh = $model->mesh; @@ -145,6 +148,7 @@ if (@ARGV) { # slicing from command line if ($opt{split}) { foreach my $file (@ARGV) { + $file = Slic3r::decode_path($file); my $model = Slic3r::Model->read_from_file($file); $model->add_default_instances; my $mesh = $model->mesh; @@ -161,6 +165,7 @@ if (@ARGV) { # slicing from command line } while (my $input_file = shift @ARGV) { + $input_file = Slic3r::decode_path($input_file); my $model; if ($opt{merge}) { my @models = map Slic3r::Model->read_from_file($_), $input_file, (splice @ARGV, 0);