Beispiel #1
0
        private void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                var cur_amiibo = CurrentSeriesAmiibos[AmiiboComboBox.SelectedIndex];
                if (string.IsNullOrEmpty(AmiiboNameBox.Text))
                {
                    ShowErrorBox("No amiibo name was specified.");
                    return;
                }


                bool use_name_as_dir = UseNameCheck.Checked;
                if (!use_name_as_dir && string.IsNullOrEmpty(DirectoryNameBox.Text))
                {
                    ShowErrorBox("No amiibo directory name was specified.");
                    return;
                }

                string name     = AmiiboNameBox.Text;
                string dir_name = name;
                if (!use_name_as_dir)
                {
                    dir_name = DirectoryNameBox.Text;
                }

                string out_path      = "";
                bool   use_last_path = LastPathCheck.Checked;
                if (use_last_path)
                {
                    if (string.IsNullOrEmpty(LastUsedPath))
                    {
                        use_last_path = false;
                    }
                }

                bool      save_to_ftp = FtpSaveCheck.Checked;
                IPAddress ftp_ip      = null;
                int       ftp_port    = 0;

                // For FTP, use a temp directory to save the resulting files from amiibo.Save() before transfer
                var ftp_tmp_path  = Path.Combine(Environment.CurrentDirectory, "temp_ftp");
                var ftp_sd_folder = $"/emuiibo/amiibo/{dir_name}/";
                var selected_path = "";

                if (save_to_ftp)
                {
                    // Prepare FTP path
                    out_path = Path.Combine(ftp_tmp_path, dir_name);

                    // Validate the FTP address
                    if (!IPAddress.TryParse(FtpAddressBox.Text, out ftp_ip))
                    {
                        ShowErrorBox("FTP address is invalid");
                        return;
                    }

                    if (!int.TryParse(FtpPortBox.Text, out ftp_port))
                    {
                        ShowErrorBox("FTP port is invalid");
                        return;
                    }

                    if (CancelAmiiboCreation($"'ftp://{ftp_ip.ToString()}:{ftp_port}{ftp_sd_folder}'"))
                    {
                        // User cancelled
                        return;
                    }
                }
                else
                {
                    if (use_last_path)
                    {
                        if (CancelAmiiboCreation("the last used path"))
                        {
                            // User cancelled
                            return;
                        }
                        out_path = Path.Combine(LastUsedPath, dir_name);
                    }
                    else
                    {
                        // If we're saving normally and we're not using the last path, ask the user for the path
                        selected_path = SelectDirectory();
                        if (selected_path == null)
                        {
                            // User cancelled
                            return;
                        }
                        out_path = Path.Combine(selected_path, dir_name);
                        if (CancelAmiiboCreation($"'{out_path}'"))
                        {
                            // User cancelled
                            return;
                        }
                    }
                }

                // Actually save the amiibo
                if (generateAllAmibosCheck.Checked)
                {
                    AmiiboSeries = Amiibos.GetAmiiboSeries();
                    if (AmiiboSeries.Any())
                    {
                        foreach (var series in AmiiboSeries)
                        {
                            CurrentSeriesAmiibos = Amiibos.GetAmiibosBySeries(series);
                            if (CurrentSeriesAmiibos.Any())
                            {
                                var index = 0;
                                foreach (var amiibo in CurrentSeriesAmiibos)
                                {
                                    var amiibo_build = AmiiboUtils.BuildAmiibo(CurrentSeriesAmiibos[index], amiibo.AmiiboName);
                                    amiibo_build.Save(out_path + "\\" + series + "\\" + amiibo.AmiiboName, RandomizeUuidCheck.Checked, SaveImageCheck.Checked);
                                    index++;
                                }
                            }
                        }
                    }
                }
                else
                {
                    var amiibo = AmiiboUtils.BuildAmiibo(cur_amiibo, name);
                    amiibo.Save(out_path, RandomizeUuidCheck.Checked, SaveImageCheck.Checked);
                }


                // Special handling for FTP
                if (save_to_ftp)
                {
                    var success = true;
                    using (var client = new FtpClient(ftp_ip.ToString(), ftp_port, new NetworkCredential("", "")))
                    {
                        client.ConnectTimeout = 1000;
                        client.Connect();
                        foreach (var file in Directory.GetFiles(out_path))
                        {
                            var file_name = Path.GetFileName(file);
                            // Upload each file created, creating directories along the way
                            var status = client.UploadFile(file, ftp_sd_folder + file_name, createRemoteDir: true);
                            if (status != FtpStatus.Success)
                            {
                                success = false;
                                break;
                            }
                        }
                        client.Disconnect();
                    }

                    ExceptionUtils.Unless(success, "Error during FTP upload, please try again");

                    // Clean the temp directory
                    Directory.Delete(ftp_tmp_path, true);
                }
                else
                {
                    if (!use_last_path)
                    {
                        // Update last used path
                        LastUsedPath = selected_path;
                    }
                }

                MessageBox.Show("The virtual amiibo was successfully created.", DialogCaption, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionUtils.LogExceptionMessage(ex);
            }
            LastPathLabel.Visible = LastPathCheck.Visible = !string.IsNullOrEmpty(LastUsedPath);
            if (LastPathLabel.Visible)
            {
                LastPathLabel.Text = "Last path: " + LastUsedPath;
            }
        }