Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Ionic.Zip;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace CoffeeCupServer
- {
- public class Updater
- {
- // Updates things
- public static void doBrowse()
- {
- string updatelog = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Starting update..." + Environment.NewLine;
- OpenFileDialog ofd = new OpenFileDialog();
- ofd.Filter = "ZIP Files|*.zip";
- ofd.Multiselect = false;
- ofd.Title = "Please select the package";
- if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- //MessageBox.Show(ofd.FileName);
- using (ZipFile zip = ZipFile.Read(ofd.FileName))
- {
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Package selected: [" + ofd.FileName + "]" + Environment.NewLine;
- string patchName = System.IO.Path.GetFileName(ofd.FileName);
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Package name: [" + patchName + "]" + Environment.NewLine;
- try
- {
- System.IO.Directory.Delete("C:\\CoffeeCup\\Patches\\" + patchName, true);
- }
- catch (Exception e)
- {
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Directory cleared: [C:\\CoffeeCup\\Patches\\" + patchName + "]" + Environment.NewLine;
- // Nothing
- }
- System.IO.Directory.CreateDirectory("C:\\CoffeeCup\\Patches\\" + patchName);
- zip.ExtractAll("C:\\CoffeeCup\\Patches\\" + patchName);
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Package extracted to [C:\\CoffeeCup\\Patches\\" + patchName + "]" + Environment.NewLine;
- try
- {
- updatelog += DirectoryCopy(@"C:\CoffeeCup\Patches\" + patchName + "\\www", "C:\\CoffeeCup\\UniServerZ\\www", true);
- }
- catch (Exception e)
- {
- MessageBox.Show("Package might be corrupted!", "Package filesystem might ", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " [ERROR] Error while moving files " + e.Message + Environment.NewLine;
- }
- try
- {
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Listing SQL files: [" + @"C:\CoffeeCup\Patches\" + patchName + "\\sql" + "]" + Environment.NewLine;
- DirectoryInfo sqlDir = new DirectoryInfo(@"C:\CoffeeCup\Patches\" + patchName + "\\sql\\");
- FileInfo[] files = sqlDir.GetFiles();
- foreach (FileInfo file in files)
- {
- string output = ExecuteSQLFile(file);
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Executing SQL file: [" + file.FullName + "]" + Environment.NewLine + output;
- }
- }
- catch (Exception e)
- {
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ERROR while executing SQL files: [" + e.Message + "]" + Environment.NewLine;
- }
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Update finished." + Environment.NewLine;
- File.WriteAllText(@"C:\CoffeeCup\Patches\log_" + patchName + ".log", updatelog);
- if (File.Exists(@"C:\CoffeeCup\Patches\" + patchName + "\\patchmsg.txt"))
- {
- try
- {
- MessageBox.Show(File.ReadAllText(@"C:\CoffeeCup\Patches\" + patchName + "\\patchmsg.txt"), "Update finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception e)
- {
- // notihng
- MessageBox.Show("CoffeeCup was updated", "Update finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
- //throw;
- }
- }
- else
- {
- MessageBox.Show("CoffeeCup was updated", "Update finished", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- } // end of using
- }
- else
- {
- updatelog += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " No package selected." + Environment.NewLine;
- }
- //MessageBox.Show(updatelog);
- }
- private static string ExecuteCMD(string cmd, string arguments)
- {
- ProcessStartInfo psi = new ProcessStartInfo(cmd, arguments);
- psi.UseShellExecute = false;
- psi.RedirectStandardOutput = true;
- psi.RedirectStandardError = true;
- psi.StandardErrorEncoding = Encoding.UTF8;
- psi.StandardOutputEncoding = Encoding.UTF8;
- Process proc = Process.Start(psi);
- string output = "[OUTPUT]: " + proc.StandardOutput.ReadToEnd() + Environment.NewLine + "[ERRORS]: " + proc.StandardError.ReadToEnd(); // > 0 ? Environment.NewLine + proc.StandardError.ReadToEnd() : "");
- proc.WaitForExit();
- return output;
- }
- private static string ExecuteSQLFile(FileInfo sqlFile)
- {
- IniFile ini = new IniFile(@"C:\CoffeeCup\PrintServer\database.ini");
- string mysql_database = ini.IniReadValue("MySQL", "database");
- string mysql_user = ini.IniReadValue("MySQL", "user");
- string mysql_password = ini.IniReadValue("MySQL", "pass");
- string mysql_exefile = ini.IniReadValue("MySQL", "exefile");
- string args = "/c \"" + mysql_exefile + "\"" + @""" --user=" + mysql_user + @" --password=" + mysql_password + @" " + mysql_database + @" < " + sqlFile.FullName + "\"";
- string output = ExecuteCMD("cmd", args);
- return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Executing: [" + "cmd" + "] args: [" + args + "]" + Environment.NewLine + output + Environment.NewLine;
- }
- private static string DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
- {
- string log = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Copying files from [" + sourceDirName + "] to [" + destDirName + "]" + Environment.NewLine;
- // Get the subdirectories for the specified directory.
- DirectoryInfo dir = new DirectoryInfo(sourceDirName);
- if (!dir.Exists)
- {
- /*throw new DirectoryNotFoundException(
- "Source directory does not exist or could not be found: "
- + sourceDirName);*/
- log += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " ERROR Directory does not exist! [" + sourceDirName + "]" + Environment.NewLine;
- }
- DirectoryInfo[] dirs = dir.GetDirectories();
- // If the destination directory doesn't exist, create it.
- if (!Directory.Exists(destDirName))
- {
- Directory.CreateDirectory(destDirName);
- log += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Creating directory: [" + destDirName + "]" + Environment.NewLine;
- }
- // Get the files in the directory and copy them to the new location.
- FileInfo[] files = dir.GetFiles();
- foreach (FileInfo file in files)
- {
- string temppath = Path.Combine(destDirName, file.Name);
- file.CopyTo(temppath, true);
- log += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Copying file from [" + file.FullName + "] to: [" + temppath + "]" + Environment.NewLine;
- }
- // If copying subdirectories, copy them and their contents to new location.
- if (copySubDirs)
- {
- foreach (DirectoryInfo subdir in dirs)
- {
- string temppath = Path.Combine(destDirName, subdir.Name);
- log += DirectoryCopy(subdir.FullName, temppath, copySubDirs);
- }
- }
- return log;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement