Advertisement
Guest User

Untitled

a guest
Oct 1st, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.73 KB | None | 0 0
  1. using Ionic.Zip;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace CoffeeCupServer
  11. {
  12.     public class Updater
  13.     {
  14.         // Updates things
  15.         public static void doBrowse()
  16.         {
  17.             string updatelog = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Starting update..." + Environment.NewLine;
  18.             OpenFileDialog ofd = new OpenFileDialog();
  19.             ofd.Filter = "ZIP Files|*.zip";
  20.             ofd.Multiselect = false;
  21.             ofd.Title = "Please select the package";
  22.  
  23.             if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  24.             {
  25.                 //MessageBox.Show(ofd.FileName);
  26.                 using (ZipFile zip = ZipFile.Read(ofd.FileName))
  27.                 {
  28.                     updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Package selected: [" + ofd.FileName + "]" + Environment.NewLine;
  29.  
  30.                     string patchName = System.IO.Path.GetFileName(ofd.FileName);
  31.                     updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Package name: [" + patchName + "]" + Environment.NewLine;
  32.                     try
  33.                     {
  34.                         System.IO.Directory.Delete("C:\\CoffeeCup\\Patches\\" + patchName, true);
  35.                     }
  36.                     catch (Exception e)
  37.                     {
  38.                         updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Directory cleared: [C:\\CoffeeCup\\Patches\\" + patchName + "]" + Environment.NewLine;
  39.                         // Nothing
  40.                     }
  41.                     System.IO.Directory.CreateDirectory("C:\\CoffeeCup\\Patches\\" + patchName);
  42.                     zip.ExtractAll("C:\\CoffeeCup\\Patches\\" + patchName);
  43.                     updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Package extracted to [C:\\CoffeeCup\\Patches\\" + patchName + "]" + Environment.NewLine;
  44.  
  45.                     try
  46.                     {
  47.                         updatelog += DirectoryCopy(@"C:\CoffeeCup\Patches\" + patchName + "\\www", "C:\\CoffeeCup\\UniServerZ\\www", true);
  48.                     }
  49.                     catch (Exception e)
  50.                     {
  51.                         MessageBox.Show("Package might be corrupted!", "Package filesystem might ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  52.                         updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " [ERROR] Error while moving files " + e.Message + Environment.NewLine;
  53.                     }
  54.  
  55.  
  56.                     try
  57.                     {
  58.                         updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Listing SQL files: [" + @"C:\CoffeeCup\Patches\" + patchName + "\\sql" + "]" + Environment.NewLine;
  59.                         DirectoryInfo sqlDir = new DirectoryInfo(@"C:\CoffeeCup\Patches\" + patchName + "\\sql\\");
  60.                         FileInfo[] files = sqlDir.GetFiles();
  61.                         foreach (FileInfo file in files)
  62.                         {
  63.                             string output = ExecuteSQLFile(file);
  64.                             updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Executing SQL file: [" + file.FullName + "]" + Environment.NewLine + output;
  65.  
  66.                         }
  67.                     }
  68.                     catch (Exception e)
  69.                     {
  70.                         updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ERROR while executing SQL files: [" + e.Message + "]" + Environment.NewLine;
  71.                     }
  72.  
  73.                     updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Update finished." + Environment.NewLine;
  74.                     File.WriteAllText(@"C:\CoffeeCup\Patches\log_" + patchName + ".log", updatelog);
  75.                     if (File.Exists(@"C:\CoffeeCup\Patches\" + patchName + "\\patchmsg.txt"))
  76.                     {
  77.                         try
  78.                         {
  79.                             MessageBox.Show(File.ReadAllText(@"C:\CoffeeCup\Patches\" + patchName + "\\patchmsg.txt"), "Update finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
  80.  
  81.                         }
  82.                         catch (Exception e)
  83.                         {
  84.                             // notihng
  85.                             MessageBox.Show("CoffeeCup was updated", "Update finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
  86.                             //throw;
  87.                         }
  88.                     }
  89.                     else
  90.                     {
  91.                         MessageBox.Show("CoffeeCup was updated", "Update finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
  92.  
  93.                     }
  94.                 } // end of using
  95.  
  96.  
  97.  
  98.             }
  99.             else
  100.             {
  101.                 updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " No package selected." + Environment.NewLine;
  102.             }
  103.             //MessageBox.Show(updatelog);
  104.         }
  105.  
  106.         private static string ExecuteCMD(string cmd, string arguments)
  107.         {
  108.             ProcessStartInfo psi = new ProcessStartInfo(cmd, arguments);
  109.             psi.UseShellExecute = false;
  110.             psi.RedirectStandardOutput = true;
  111.             psi.RedirectStandardError = true;
  112.             psi.StandardErrorEncoding = Encoding.UTF8;
  113.             psi.StandardOutputEncoding = Encoding.UTF8;
  114.             Process proc = Process.Start(psi);
  115.             string output = "[OUTPUT]: " + proc.StandardOutput.ReadToEnd() + Environment.NewLine + "[ERRORS]: " + proc.StandardError.ReadToEnd(); // > 0 ? Environment.NewLine + proc.StandardError.ReadToEnd() : "");
  116.             proc.WaitForExit();
  117.             return output;
  118.         }
  119.  
  120.         private static string ExecuteSQLFile(FileInfo sqlFile)
  121.         {
  122.             IniFile ini = new IniFile(@"C:\CoffeeCup\PrintServer\database.ini");
  123.             string mysql_database = ini.IniReadValue("MySQL", "database");
  124.             string mysql_user = ini.IniReadValue("MySQL", "user");
  125.             string mysql_password = ini.IniReadValue("MySQL", "pass");
  126.             string mysql_exefile = ini.IniReadValue("MySQL", "exefile");
  127.             string args = "/c \"" + mysql_exefile + "\"" + @""" --user=" + mysql_user + @" --password=" + mysql_password + @" " + mysql_database + @" < " + sqlFile.FullName + "\"";
  128.             string output = ExecuteCMD("cmd", args);
  129.             return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Executing: [" + "cmd" + "] args: [" + args + "]" + Environment.NewLine + output + Environment.NewLine;
  130.         }
  131.  
  132.         private static string DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
  133.         {
  134.             string log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Copying files from [" + sourceDirName + "] to [" + destDirName + "]" + Environment.NewLine;
  135.  
  136.             // Get the subdirectories for the specified directory.
  137.             DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  138.  
  139.             if (!dir.Exists)
  140.             {
  141.                 /*throw new DirectoryNotFoundException(
  142.                     "Source directory does not exist or could not be found: "
  143.                     + sourceDirName);*/
  144.  
  145.                 log += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ERROR Directory does not exist! [" + sourceDirName + "]" + Environment.NewLine;
  146.             }
  147.  
  148.             DirectoryInfo[] dirs = dir.GetDirectories();
  149.             // If the destination directory doesn't exist, create it.
  150.             if (!Directory.Exists(destDirName))
  151.             {
  152.                 Directory.CreateDirectory(destDirName);
  153.                 log += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Creating directory: [" + destDirName + "]" + Environment.NewLine;
  154.             }
  155.  
  156.             // Get the files in the directory and copy them to the new location.
  157.             FileInfo[] files = dir.GetFiles();
  158.             foreach (FileInfo file in files)
  159.             {
  160.                 string temppath = Path.Combine(destDirName, file.Name);
  161.                 file.CopyTo(temppath, true);
  162.                 log += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Copying file from [" + file.FullName + "] to: [" + temppath + "]" + Environment.NewLine;
  163.             }
  164.  
  165.             // If copying subdirectories, copy them and their contents to new location.
  166.             if (copySubDirs)
  167.             {
  168.                 foreach (DirectoryInfo subdir in dirs)
  169.                 {
  170.                     string temppath = Path.Combine(destDirName, subdir.Name);
  171.                     log += DirectoryCopy(subdir.FullName, temppath, copySubDirs);
  172.                 }
  173.             }
  174.  
  175.             return log;
  176.         }
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement