diff --git a/PCK-Studio/Classes/Misc/FTPClient.cs b/PCK-Studio/Classes/Misc/FTPClient.cs new file mode 100644 index 00000000..099f9029 --- /dev/null +++ b/PCK-Studio/Classes/Misc/FTPClient.cs @@ -0,0 +1,283 @@ +using System; +using System.IO; +using System.Net; + +namespace PckStudio.Classes.Misc +{ + public class FTPClient : IDisposable + { + private const int bufferSize = 2048; + + private Uri hostUri; + private NetworkCredential credentials; + + private FtpWebRequest request = null; + private FtpWebResponse response = null; + private Stream _stream = null; + + + public FTPClient(string host, string username) + : this(new Uri(host), username) + { + } + + public FTPClient(Uri uri, string username) + : this(uri, username, string.Empty) + { + } + + public FTPClient(string host, string username, string password) + : this(new Uri(host), username, password) + { + } + + public FTPClient(Uri uri, string username, string password) + { + hostUri = uri; + credentials = new NetworkCredential(username, password); + } + + /// + /// Creates a new FTP Request + /// + /// + /// + /// See + /// + public static FtpWebRequest CreateFTPWebRequest(Uri uri, ICredentials credentials, string method) + { + FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri); + request.Credentials = credentials; + request.Method = method; + return request; + } + + public void DownloadFile(string remoteFilepath, string localFile) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, remoteFilepath), credentials, WebRequestMethods.Ftp.DownloadFile); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.DownloadFile; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + _stream = response.GetResponseStream(); + FileStream fileStream = new FileStream(localFile, FileMode.OpenOrCreate); + byte[] buffer = new byte[Convert.ToInt32(GetFileSize(remoteFilepath))]; + int num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath))); + try + { + while (num > 0) + { + fileStream.Write(buffer, 0, num); + num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath))); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + fileStream.Close(); + _stream.Close(); + response.Close(); + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public void UploadFile(string localFile, string remoteFile) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, remoteFile), credentials, WebRequestMethods.Ftp.UploadFile); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + remoteFile); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.UploadFile; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + _stream = request.GetRequestStream(); + FileStream fileStream = new FileStream(localFile, FileMode.Open); + byte[] buffer = new byte[fileStream.Length]; + int num = fileStream.Read(buffer, 0, (int)fileStream.Length); + try + { + while (num != 0) + { + _stream.Write(buffer, 0, num); + num = fileStream.Read(buffer, 0, (int)fileStream.Length); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + fileStream.Close(); + _stream.Close(); + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public string[] ListDirectory(string directory) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, directory), credentials, WebRequestMethods.Ftp.ListDirectory); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + directory); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.ListDirectory; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + _stream = response.GetResponseStream(); + StreamReader streamReader = new StreamReader(_stream); + string text = string.Empty; + try + { + while (streamReader.Peek() != -1) + { + text += streamReader.ReadLine() + "|"; + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + + streamReader.Close(); + _stream.Close(); + response.Close(); + request = null; + + try + { + return text.Split("|".ToCharArray()); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } + return Array.Empty(); + } + + public void Rename(string name, string newName) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, name), credentials, WebRequestMethods.Ftp.Rename); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + name); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.Rename; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + request.RenameTo = newName; + response = (FtpWebResponse)request.GetResponse(); + response.Close(); + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public void DeleteFile(string filename) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, filename), credentials, WebRequestMethods.Ftp.DeleteFile); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + filename); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.DeleteFile; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + response.Close(); + + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public void CreateDirectory(string name) + { + try + { + request = CreateFTPWebRequest(new Uri(hostUri, name), credentials, WebRequestMethods.Ftp.MakeDirectory); + //request = (FtpWebRequest)WebRequest.Create(host + "/" + name); + //request.Credentials = credentials; + //request.Method = WebRequestMethods.Ftp.MakeDirectory; + + request.UseBinary = true; + request.UsePassive = true; + request.KeepAlive = true; + + response = (FtpWebResponse)request.GetResponse(); + response.Close(); + + request = null; + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); + } + } + + public long GetFileSize(string filepath) + { + FtpWebRequest ftpWebRequest = CreateFTPWebRequest(new Uri(hostUri, filepath), credentials, WebRequestMethods.Ftp.GetFileSize); + //FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(host + "/" + fileName); + //ftpWebRequest.Credentials = credentials; + //ftpWebRequest.Method = WebRequestMethods.Ftp.GetFileSize; + + ftpWebRequest.UseBinary = true; + + FtpWebResponse response = (FtpWebResponse)ftpWebRequest.GetResponse(); + long contentLength = response.ContentLength; + response.Close(); + return contentLength; + } + + public void Dispose() + { + _stream.Dispose(); + response.Dispose(); + request = null; + response = null; + _stream = null; + } + } +} \ No newline at end of file diff --git a/PCK-Studio/Forms/Utilities/installPS3.cs b/PCK-Studio/Forms/Utilities/installPS3.cs index 6b45c461..e8d306d5 100644 --- a/PCK-Studio/Forms/Utilities/installPS3.cs +++ b/PCK-Studio/Forms/Utilities/installPS3.cs @@ -1,5 +1,4 @@ -using FileTransferProtocolLib; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -7,6 +6,7 @@ using System.IO; using System.Net; using System.Windows.Forms; using MetroFramework.Forms; +using PckStudio.Classes.Misc; namespace PckStudio.Forms { @@ -292,7 +292,7 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } @@ -341,7 +341,7 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } diff --git a/PCK-Studio/Forms/Utilities/installVita.cs b/PCK-Studio/Forms/Utilities/installVita.cs index 1dc0502b..28c90622 100644 --- a/PCK-Studio/Forms/Utilities/installVita.cs +++ b/PCK-Studio/Forms/Utilities/installVita.cs @@ -1,5 +1,4 @@ -using FileTransferProtocolLib; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -7,6 +6,7 @@ using System.IO; using System.Net; using System.Windows.Forms; using MetroFramework.Forms; +using PckStudio.Classes.Misc; namespace PckStudio.Forms { @@ -292,7 +292,7 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } @@ -341,7 +341,7 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTP client = new FTP("ftp://" + textBoxHost.Text, "", ""); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } diff --git a/PCK-Studio/Forms/Utilities/installWiiU.cs b/PCK-Studio/Forms/Utilities/installWiiU.cs index c64e91c1..7e02d173 100644 --- a/PCK-Studio/Forms/Utilities/installWiiU.cs +++ b/PCK-Studio/Forms/Utilities/installWiiU.cs @@ -1,5 +1,4 @@ -using FileTransferProtocolLib; -using System; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; @@ -7,10 +6,12 @@ using System.IO; using System.IO.Compression; using System.Net; using System.Windows.Forms; +using System.Diagnostics; + using PckStudio.Classes.FileTypes; using PckStudio.Classes.IO.PCK; using PckStudio.Classes.IO.ARC; -using System.Diagnostics; +using PckStudio.Classes.Misc; namespace PckStudio.Forms { @@ -346,7 +347,7 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); if(TextBoxPackImage.Text != "") { @@ -411,7 +412,7 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); if (TextBoxPackImage.Text != "") { @@ -436,7 +437,7 @@ namespace PckStudio.Forms private void GetARCFromConsole() { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); client.DownloadFile(dlcPath + "../../Common/Media/MediaWiiU.arc", Program.AppData + "MediaWiiU.arc"); archive = ARCFileReader.Read(new MemoryStream(File.ReadAllBytes(Program.AppData + "MediaWiiU.arc"))); } @@ -451,7 +452,7 @@ namespace PckStudio.Forms private void SendARCToConsole() { - FTP client = new FTP("ftp://" + textBoxHost.Text, "", "a3262443"); + FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); MemoryStream ms = new MemoryStream(); ARCFileWriter.Write(ms, archive); File.WriteAllBytes(Program.AppData + "MediaWiiU.arc", ms.ToArray()); diff --git a/PCK-Studio/PckStudio.csproj b/PCK-Studio/PckStudio.csproj index 541d1abe..cd8eec19 100644 --- a/PCK-Studio/PckStudio.csproj +++ b/PCK-Studio/PckStudio.csproj @@ -105,9 +105,6 @@ Properties\app.manifest - - ..\filetransferprotocollib.dll - @@ -161,6 +158,7 @@ + diff --git a/filetransferprotocollib.dll b/filetransferprotocollib.dll deleted file mode 100644 index c2d9a867..00000000 Binary files a/filetransferprotocollib.dll and /dev/null differ