Guest User

Untitled

a guest
Dec 12th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Net;
  12.  
  13. namespace Patcher2
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         public Form1()
  18.         {
  19.             InitializeComponent();
  20.         }
  21.        
  22.         public FtpWebRequest request;
  23.         public string ToFake(string name)
  24.         {
  25.             return name.Replace(@"\", "/");
  26.         }
  27.         public string FromFake(string name)
  28.         {
  29.             return name.Replace("/", @"\");
  30.         }
  31.         public void Upload(string fileName, string fn2)
  32.         {
  33.             fileName = ToFake(fileName);
  34.             this.request = (FtpWebRequest)WebRequest.Create("ftp://amit1532.comule.com/public_html/Patcher/" + fileName);
  35.             string fN = fileName;
  36.             int place = 0;
  37.             if (fN.IndexOf("/") != -1)
  38.             {
  39.                 while (place != -1)
  40.                 {
  41.                         if (fileName.IndexOf("/") == -1)
  42.                         {
  43.                             break;
  44.                         }
  45.                         place = fileName.IndexOf("/", place + 1);
  46.                         if (place == -1)
  47.                             break;
  48.                         fN = fileName.Substring(0, place);
  49.                        
  50.                        
  51.                         WebRequest req = WebRequest.Create("ftp://amit1532.comule.com/public_html/Patcher/" + fN);
  52.                         req.Credentials = new NetworkCredential("a7566948", "1l0o0744");
  53.  
  54.                         req.Method = WebRequestMethods.Ftp.MakeDirectory;
  55.                         try
  56.                         {
  57.                             WebResponse res = req.GetResponse();
  58.                         }
  59.                         catch
  60.                         {
  61.                         }
  62.                 }
  63.             }
  64.             this.request.Method = "STOR";
  65.             this.request.Credentials = new NetworkCredential("a7566948", "1l0o0744");
  66.             StreamReader sourceStream = new StreamReader(fn2);
  67.             byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
  68.             sourceStream.Close();
  69.             this.request.ContentLength = (long)fileContents.Length;
  70.             Stream requestStream = this.request.GetRequestStream();
  71.             requestStream.Write(fileContents, 0, fileContents.Length);
  72.             requestStream.Close();
  73.             FtpWebResponse response = (FtpWebResponse)this.request.GetResponse();
  74.             response.Close();
  75.         }
  76.         public void UploadText(string fileName, string text)
  77.         {
  78.             this.request = (FtpWebRequest)WebRequest.Create("ftp://amit1532.comule.com/public_html/Patcher/" + fileName);
  79.             this.request.Method = "STOR";
  80.             request.Headers.Add("Overwrite", "t");
  81.             this.request.Credentials = new NetworkCredential("a7566948", "1l0o0744");
  82.             byte[] fileContents = StrToByteArray(text);
  83.             string bitbit = "";
  84.             for (int i = 0; i < fileContents.Length; i++)
  85.             {
  86.                 bitbit += fileContents[i].ToString();
  87.             }
  88.             this.request.ContentLength = (long)fileContents.Length;
  89.             Stream requestStream = this.request.GetRequestStream();
  90.             requestStream.Write(fileContents, 0, fileContents.Length);
  91.             requestStream.Close();
  92.             FtpWebResponse response = (FtpWebResponse)this.request.GetResponse();
  93.             response.Close();
  94.         }
  95.         public void CreateFile(string name, string text)
  96.         {
  97.             name = FromFake(name);
  98.             string fN = name;
  99.             while (fN.IndexOf("\\") != -1)
  100.             {
  101.                 string nDir = name.Substring(0, fN.IndexOf("\\"));
  102.                 if (!Directory.Exists(nDir))
  103.                 {
  104.                     DirectoryInfo di = Directory.CreateDirectory(fN);
  105.  
  106.                     // Delete the directory.
  107.                     di.Delete();
  108.                 }
  109.                
  110.                 fN = fN.Substring(fN.IndexOf("\\") + 1);
  111.             }
  112.             FileStream fs = new FileStream(name, FileMode.OpenOrCreate, FileAccess.Write);
  113.  
  114.             fs.Write(StrToByteArray(text), 0, text.Length);
  115.             fs.Close();
  116.         }
  117.         public string ReadSrvFile(string name)
  118.         {
  119.             WebClient request = new WebClient();
  120.             string url = "ftp://amit1532.comule.com/public_html/Patcher/" + name;
  121.             byte[] newFileData = new byte[0];
  122.             try
  123.             {
  124.                 request.Credentials = new NetworkCredential("a7566948", "1l0o0744");
  125.                 newFileData = request.DownloadData(url);
  126.             }
  127.             catch
  128.             {
  129.                 return "";
  130.             }
  131.             return Encoding.UTF8.GetString(newFileData);
  132.         }
  133.         private void button1_Click(object sender, EventArgs e)
  134.         {
  135.             List<string> strs = this.GetFiles(this.GetVersion());
  136.             DownloadFiles(strs);
  137.  
  138.             MessageBox.Show("your files have been downloaded");
  139.         }
  140.         private void button2_Click(object sender, EventArgs e)
  141.         {
  142.             string[] filePaths = Directory.GetFiles("toUploading", "*.*", SearchOption.AllDirectories);
  143.  
  144.             progressBar1.Maximum = filePaths.Length;
  145.             progressBar1.Minimum = 0;
  146.  
  147.             for (int i = 0; i<filePaths.Length; ++i)
  148.             {
  149.                 filePaths[i] = filePaths[i].Substring(filePaths[i].IndexOf("\\")+1);
  150.                
  151.                 Upload(filePaths[i], @"toUploading\"+filePaths[i]);
  152.  
  153.                 progressBar1.Increment(1);
  154.             }
  155.  
  156.             UpdatePatcher(filePaths);
  157.             MessageBox.Show("your files have been uploaded");
  158.         }
  159.         void UpdatePatcher(string[] files)
  160.         {
  161.  
  162.             string srvText = ReadSrvFile("patcher.txt");
  163.             float version = float.Parse(srvText.Substring(srvText.LastIndexOf("version: ") + 9, srvText.IndexOf("\n", srvText.LastIndexOf("version: ")) - (srvText.LastIndexOf("version: ") + 9)));
  164.             version += 0.1f;
  165.             srvText += "\r\n\r\nversion: " + version.ToString() + "\r\nfile: ";
  166.             for (int i = 0; i < files.Length; ++i)
  167.             {
  168.                 srvText += " \"" + files[i] + "\"";
  169.             }
  170.             UploadText("patcher.txt", srvText);
  171.         }
  172.         public string ReadFile(string name)
  173.         {
  174.             return File.ReadAllText(name);
  175.         }
  176.         public static byte[] StrToByteArray(string str)
  177.         {
  178.             UTF8Encoding encoding = new UTF8Encoding();
  179.             return encoding.GetBytes(str);
  180.         }
  181.         public string GetVersion()
  182.         {
  183.             string text = this.ReadFile("keyfile.txt");
  184.  
  185.             return text.Substring(9);
  186.         }
  187.         public void SetVersion(string ver)
  188.         {
  189.             ver = "version: " + ver;
  190.             FileStream fs = new FileStream("keyfile.txt", FileMode.OpenOrCreate, FileAccess.Write);
  191.            
  192.             fs.Write(Form1.StrToByteArray(ver), 0, ver.Length);
  193.             fs.Close();
  194.         }
  195.         public List<string> GetFiles(string ver)
  196.         {
  197.             List<string> files = new List<string>();
  198.             string srvText = this.ReadSrvFile("patcher.txt");
  199.             int place = srvText.IndexOf("version: ", srvText.IndexOf("version: " + ver)+1);
  200.             if (place == -1)
  201.             {
  202.                 MessageBox.Show("you already have the newest update");
  203.                 return files;
  204.             }
  205.             place = srvText.IndexOf("\"", srvText.IndexOf("file: ", place));
  206.            
  207.             while (srvText.IndexOf("\"", place + 1) != -1 && place != -1)
  208.             {
  209.                
  210.                 string str = srvText.Substring(place + 1, srvText.IndexOf("\"", place + 1)-(place+1));
  211.                
  212.                 place = srvText.IndexOf("\"", srvText.IndexOf("\"", place + 1)+1);
  213.                 files.Add(str);
  214.             }
  215.  
  216.             SetVersion(srvText.Substring(srvText.LastIndexOf("version: ") + 9, srvText.IndexOf("\n", srvText.LastIndexOf("version: ")) - (srvText.LastIndexOf("version: ") + 9)));
  217.             return files;
  218.         }
  219.         public void DownloadFiles(List<string> files)
  220.         {
  221.             progressBar1.Maximum = files.Count;
  222.             progressBar1.Minimum = 0;
  223.  
  224.             for (int i = 0; i < files.Count; ++i)
  225.             {
  226.                 string str = ReadSrvFile(files[i]);
  227.                 CreateFile(files[i], str);
  228.  
  229.                 progressBar1.Increment(1);
  230.             }
  231.         }
  232.     }
  233. }
Add Comment
Please, Sign In to add comment