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 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; } }
private void CreateAmiibo(string name, string dir_name, string base_dir, AmiiboAPI.Amiibo cur_amiibo) { 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/"; if (!string.IsNullOrEmpty(dir_name)) { ftp_sd_folder += $"{dir_name}/"; } 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; } } else { if (use_last_path) { out_path = Path.Combine(LastUsedPath, dir_name); } else { out_path = Path.Combine(base_dir, dir_name); } } // Actually save the amiibo 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(); } Utils.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 = base_dir; } } }
private void Button1_Click(object sender, EventArgs e) { try { var cur_amiibo = CurrentSeriesAmiibos[comboBox2.SelectedIndex]; if (string.IsNullOrEmpty(textBox1.Text)) { MessageBox.Show("No amiibo name was specified.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } bool use_name_as_dir = checkBox3.Checked; if (!use_name_as_dir && string.IsNullOrEmpty(textBox2.Text)) { MessageBox.Show("No amiibo directory name was specified.", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } string name = textBox1.Text; string dir_name = name; if (!use_name_as_dir) { dir_name = textBox2.Text; } if (!chkFTP.Checked) { // save to local location string emuiibo_dir = ""; if (DriveInfo.GetDrives().Any()) { foreach (var drive in DriveInfo.GetDrives()) { if (drive.IsReady) { if (Directory.Exists(Path.Combine(drive.Name, Path.Combine("emuiibo", "amiibo")))) { emuiibo_dir = Path.Combine(drive.Name, Path.Combine("emuiibo", "amiibo")); } else if (Directory.Exists(Path.Combine(drive.Name, "emuiibo"))) { Directory.CreateDirectory(Path.Combine(drive.Name, Path.Combine("emuiibo", "amiibo"))); emuiibo_dir = Path.Combine(drive.Name, Path.Combine("emuiibo", "amiibo")); } if (!string.IsNullOrEmpty(emuiibo_dir)) { MessageBox.Show($"Emuiibo directory was found in drive '{drive.VolumeLabel}', so defaulting to that directory.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } } } } string dir = ""; // check to see if the user has already selected a path if (string.IsNullOrEmpty(last_used_path)) { // no path set, present the browser dialog and allow them to pick FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog { Description = "Select root directory to generate the virtual amiibo on", ShowNewFolderButton = false, SelectedPath = emuiibo_dir }; if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { dir = Path.Combine(folderBrowserDialog.SelectedPath, dir_name); last_used_path = folderBrowserDialog.SelectedPath; if (MessageBox.Show($"Virtual amiibo will be created in '{dir}'.\n\nThe directory will be deleted if it already exists.\n\nProceed with amiibo creation?", Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) { // cancelling out of creation, unset the last_used_path last_used_path = null; return; } } } else { // the user has already selected a path, use that one dir = Path.Combine(last_used_path, dir_name); if (MessageBox.Show($"Virtual amiibo will be created in '{dir}'.\n\nThe directory will be deleted if it already exists.\n\nProceed with amiibo creation?", Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) { // cancelling out of creation, unset the last_used_path last_used_path = null; return; } } var amiibo = AmiiboUtils.BuildAmiibo(cur_amiibo, name); amiibo.Save(dir, checkBox1.Checked, checkBox2.Checked); MessageBox.Show("Virtual amiibo was successfully created.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { // save to FTP location try { var tmp_path = Environment.CurrentDirectory + "\\tmp"; var dir = Path.Combine(tmp_path, dir_name); var sd_folder = $"/emuiibo/amiibo/{dir_name}/"; // first let's validate the FTP address IPAddress ip; if (!IPAddress.TryParse(txtFTPAddress.Text, out ip)) { MessageBox.Show("FTP address is invalid", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int port; if (!Int32.TryParse(txtFTPPort.Text, out port)) { MessageBox.Show("FTP Port is invalid", Text, MessageBoxButtons.OK, MessageBoxIcon.Error); return; } if (MessageBox.Show($"Virtual amiibo will be created in 'ftp://{ip.ToString()}:{port}{sd_folder}'.\n\nThe directory will be deleted if it already exists.\n\nProceed with amiibo creation?", Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question) != DialogResult.OK) { return; } // create a tmp directory to output the resulting files from amiibo.Save() before transfer if (!Directory.Exists(tmp_path)) { Directory.CreateDirectory(tmp_path); } var amiibo = AmiiboUtils.BuildAmiibo(cur_amiibo, name); amiibo.Save(dir, checkBox1.Checked, checkBox2.Checked); var success = true; using (FtpClient client = new FtpClient(ip.ToString(), port, new NetworkCredential("", ""))) { client.ConnectTimeout = 1000; client.Connect(); foreach (var file in Directory.GetFiles(dir)) { string file_name = Path.GetFileName(file); // upload each file created, creating directories along the way var status = client.UploadFile(file, sd_folder + file_name, createRemoteDir: true); success &= status == FtpStatus.Success; } client.Disconnect(); } if (success) { // recursively delete the tmp directory to clean up Directory.Delete(tmp_path, true); MessageBox.Show("Virtual amiibo was successfully created.", Text, MessageBoxButtons.OK, MessageBoxIcon.Information); } else { throw new Exception("Error during FTP upload, please try again"); } } catch (Exception ftp_ex) { ExceptionUtils.LogExceptionMessage(ftp_ex); } } } catch (Exception ex) { ExceptionUtils.LogExceptionMessage(ex); } }