From bd87df9d89b62650f046c6c2e8a5af28a2ab155c Mon Sep 17 00:00:00 2001 From: Olfi01 Date: Thu, 19 Oct 2017 07:19:08 +0200 Subject: [PATCH] okay so hopefully i can update control now too --- ControlUpdater/App.config | 6 +++ ControlUpdater/ControlUpdater.csproj | 52 ++++++++++++++++++++ ControlUpdater/Program.cs | 27 ++++++++++ ControlUpdater/Properties/AssemblyInfo.cs | 36 ++++++++++++++ WhoAmIBot.sln | 11 ++++- WhoAmIBot/Classes/Node.cs | 13 ++--- WhoAmIBot/Classes/RestartEventArgs.cs | 14 ++++++ WhoAmIBot/WhoAmIBot.cs | 60 ++++++++++++++++++++++- WhoAmIBot/WhoAmIBot.csproj | 1 + WhoAmIBotNode/Program.cs | 9 ++-- 10 files changed, 216 insertions(+), 13 deletions(-) create mode 100644 ControlUpdater/App.config create mode 100644 ControlUpdater/ControlUpdater.csproj create mode 100644 ControlUpdater/Program.cs create mode 100644 ControlUpdater/Properties/AssemblyInfo.cs create mode 100644 WhoAmIBot/Classes/RestartEventArgs.cs diff --git a/ControlUpdater/App.config b/ControlUpdater/App.config new file mode 100644 index 0000000..731f6de --- /dev/null +++ b/ControlUpdater/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ControlUpdater/ControlUpdater.csproj b/ControlUpdater/ControlUpdater.csproj new file mode 100644 index 0000000..47f28ec --- /dev/null +++ b/ControlUpdater/ControlUpdater.csproj @@ -0,0 +1,52 @@ + + + + + Debug + AnyCPU + {4CA0144D-EC95-4069-8001-2640D31CE70B} + Exe + ControlUpdater + ControlUpdater + v4.6.1 + 512 + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ControlUpdater/Program.cs b/ControlUpdater/Program.cs new file mode 100644 index 0000000..036033a --- /dev/null +++ b/ControlUpdater/Program.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; + +namespace ControlUpdater +{ + class Program + { + static void Main(string[] args) + { + if (args.Length < 2) return; + string pathToCopyTo = args[0]; + string newFilePath = args[1]; + Thread.Sleep(5000); //give time to stop old program + if (File.Exists(pathToCopyTo)) File.Delete(pathToCopyTo); + File.Copy(newFilePath, pathToCopyTo); + string exePath = Path.Combine(Path.GetDirectoryName(pathToCopyTo), "WAIBControl.exe"); + ProcessStartInfo psi = new ProcessStartInfo(exePath); + Process.Start(psi); + } + } +} diff --git a/ControlUpdater/Properties/AssemblyInfo.cs b/ControlUpdater/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..98f29ae --- /dev/null +++ b/ControlUpdater/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// Allgemeine Informationen über eine Assembly werden über die folgenden +// Attribute gesteuert. Ändern Sie diese Attributwerte, um die Informationen zu ändern, +// die einer Assembly zugeordnet sind. +[assembly: AssemblyTitle("ControlUpdater")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("HP Inc.")] +[assembly: AssemblyProduct("ControlUpdater")] +[assembly: AssemblyCopyright("Copyright © HP Inc. 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Durch Festlegen von ComVisible auf FALSE werden die Typen in dieser Assembly +// für COM-Komponenten unsichtbar. Wenn Sie auf einen Typ in dieser Assembly von +// COM aus zugreifen müssen, sollten Sie das ComVisible-Attribut für diesen Typ auf "True" festlegen. +[assembly: ComVisible(false)] + +// Die folgende GUID bestimmt die ID der Typbibliothek, wenn dieses Projekt für COM verfügbar gemacht wird +[assembly: Guid("4ca0144d-ec95-4069-8001-2640d31ce70b")] + +// Versionsinformationen für eine Assembly bestehen aus den folgenden vier Werten: +// +// Hauptversion +// Nebenversion +// Buildnummer +// Revision +// +// Sie können alle Werte angeben oder Standardwerte für die Build- und Revisionsnummern verwenden, +// übernehmen, indem Sie "*" eingeben: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/WhoAmIBot.sln b/WhoAmIBot.sln index 14ffda3..25a8ba9 100644 --- a/WhoAmIBot.sln +++ b/WhoAmIBot.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26430.16 +VisualStudioVersion = 15.0.26730.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhoAmIBot", "WhoAmIBot\WhoAmIBot.csproj", "{BC4059AA-80EE-4E63-BFA2-A340644AF78D}" EndProject @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestApp", "TestApp\TestApp. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WhoAmIBotNode", "WhoAmIBotNode\WhoAmIBotNode.csproj", "{C6802A38-760F-4E9C-A28C-607AD9ED66B1}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ControlUpdater", "ControlUpdater\ControlUpdater.csproj", "{4CA0144D-EC95-4069-8001-2640D31CE70B}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,8 +29,15 @@ Global {C6802A38-760F-4E9C-A28C-607AD9ED66B1}.Debug|Any CPU.Build.0 = Debug|Any CPU {C6802A38-760F-4E9C-A28C-607AD9ED66B1}.Release|Any CPU.ActiveCfg = Release|Any CPU {C6802A38-760F-4E9C-A28C-607AD9ED66B1}.Release|Any CPU.Build.0 = Release|Any CPU + {4CA0144D-EC95-4069-8001-2640D31CE70B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4CA0144D-EC95-4069-8001-2640D31CE70B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4CA0144D-EC95-4069-8001-2640D31CE70B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4CA0144D-EC95-4069-8001-2640D31CE70B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {0D3FCF82-A2E1-4118-BDE2-41934D13569C} + EndGlobalSection EndGlobal diff --git a/WhoAmIBot/Classes/Node.cs b/WhoAmIBot/Classes/Node.cs index 93175d4..a73c7eb 100644 --- a/WhoAmIBot/Classes/Node.cs +++ b/WhoAmIBot/Classes/Node.cs @@ -10,13 +10,13 @@ namespace WhoAmIBotSpace.Classes public class Node { public Process Process { get; } - private AnonymousPipeServerStream Pipe { get; } + private NamedPipeServerStream Pipe { get; } public NodeState State { get; set; } = NodeState.Primary; public string Path { get; set; } private List queue = new List(); private Thread QThread; public event EventHandler NodeStopped; - + public Node(string path) { Console.WriteLine("Initializing Node at {0}", path); @@ -24,8 +24,8 @@ public Node(string path) Path = path; Process = new Process(); Process.StartInfo.FileName = path; - Pipe = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable); - Process.StartInfo.Arguments = Pipe.GetClientHandleAsString(); + Pipe = new NamedPipeServerStream(path, PipeDirection.Out); + Process.StartInfo.Arguments = "\"" + path.Trim('"') + "\""; Process.StartInfo.UseShellExecute = false; } @@ -38,7 +38,7 @@ public void Start(string token) Pipe.WaitForPipeDrain(); QThread.Start(); } - + public void Stop() { QThread.Abort(); @@ -68,6 +68,7 @@ private void Queue_Thread() { while (true) { + try { Pipe.WaitForConnection(); } catch (InvalidOperationException) { } while (queue.Count < 1) ; var data = queue[0]; sw.WriteLine(data); @@ -90,7 +91,7 @@ public void Queue(string data) queue.Add(data); } } - + public enum NodeState { Primary, diff --git a/WhoAmIBot/Classes/RestartEventArgs.cs b/WhoAmIBot/Classes/RestartEventArgs.cs new file mode 100644 index 0000000..c7b3a36 --- /dev/null +++ b/WhoAmIBot/Classes/RestartEventArgs.cs @@ -0,0 +1,14 @@ +namespace WhoAmIBotSpace.Classes +{ + public class RestartEventArgs + { + public string NewDllPath { get; } + public string OldDllPath { get; } + + public RestartEventArgs(string dllPath, string newDllPath) + { + OldDllPath = dllPath; + NewDllPath = newDllPath; + } + } +} \ No newline at end of file diff --git a/WhoAmIBot/WhoAmIBot.cs b/WhoAmIBot/WhoAmIBot.cs index 5c6c21a..c695447 100644 --- a/WhoAmIBot/WhoAmIBot.cs +++ b/WhoAmIBot/WhoAmIBot.cs @@ -14,7 +14,7 @@ using System.Linq; using Newtonsoft.Json; using System.Diagnostics; -using System.Security.Permissions; +using System.Reflection; namespace WhoAmIBotSpace { @@ -43,12 +43,17 @@ public class WhoAmIBot : FlomBot "WhoAmIBot\\"); private const string dateTimeFileFormat = "yyyy-MM-dd-HH-mm-ss"; private static readonly string defaultNodeDirectory = Path.Combine(appDataBaseDir, "default\\"); + private static readonly string controlUpdaterPath = Path.Combine(appDataBaseDir, + "git\\ControlUpdater\\bin\\Release\\ControlUpdater.exe"); private static readonly string gitNodeDirectory = Path.Combine(appDataBaseDir, "git\\"); #endregion #region Fields private SQLiteConnection sqliteConn; private List Nodes = new List(); #endregion + #region Events + public event EventHandler Restart; + #endregion #region Helpers private bool GlobalAdminExists(long id) @@ -191,7 +196,7 @@ public override bool StopBot() Nodes.Remove(Nodes[0]); continue; } - Nodes[0].Stop(); + if (Nodes[0].State != NodeState.Stopping) Nodes[0].SoftStop(); } client.OnReceiveError -= Client_OnReceiveError; client.OnReceiveGeneralError -= Client_OnReceiveError; @@ -231,6 +236,16 @@ protected override void Client_OnUpdate(object sender, UpdateEventArgs e) return; } } + else if (cmd == "/updatecontrol") + { + if (e.Update.Message.From.Id == Flom) + { + client.SendTextMessageAsync(e.Update.Message.Chat.Id, "Updating Control. You should try /ping in " + + "about ten seconds, given that all games have already finished."); + UpdateControl(); + return; + } + } if (StandaloneCommandExists(cmd)) { var node = Nodes.FirstOrDefault(x => x.State == NodeState.Primary); @@ -291,6 +306,11 @@ private void Client_OnCallbackQueryUpdateChecker(object sender, CallbackQueryEve case "update": Update(cmsg); break; + case "updatecontrol": + client.EditMessageTextAsync(cmsg.Chat.Id, cmsg.MessageId, "Updating Control. You should try /ping in " + + "about ten seconds, given that all games have already finished."); + UpdateControl(); + break; case "dontUpdate": client.EditMessageTextAsync(cmsg.Chat.Id, cmsg.MessageId, "Okay, no work for me :)"); break; @@ -357,6 +377,42 @@ public static void DeepCopy(DirectoryInfo source, DirectoryInfo target) file.CopyTo(Path.Combine(target.FullName, file.Name)); } + + public void UpdateControl() + { + StopBot(); + + string path = Assembly.GetExecutingAssembly().CodeBase; + if (path.StartsWith("file:///")) path = path.Substring(8).Replace("/", "\\"); + #region Update git + if (!Directory.Exists(appDataBaseDir)) Directory.CreateDirectory(appDataBaseDir); + if (!Directory.Exists(gitNodeDirectory)) Directory.CreateDirectory(gitNodeDirectory); + string firstDir = Path.Combine(gitNodeDirectory, "first.bat"); + if (!File.Exists(firstDir)) File.Copy("Updater\\first.bat", firstDir); + string runDir = Path.Combine(gitNodeDirectory, "run.bat"); + if (!File.Exists(runDir)) File.Copy("Updater\\run.bat", runDir); + Process first = new Process(); + first.StartInfo.FileName = firstDir; + first.StartInfo.UseShellExecute = false; + first.StartInfo.WorkingDirectory = Path.GetDirectoryName(firstDir); + first.Start(); + first.WaitForExit(); + Process run = new Process(); + run.StartInfo.FileName = runDir; + run.StartInfo.UseShellExecute = false; + run.StartInfo.WorkingDirectory = Path.GetDirectoryName(runDir); + run.Start(); + run.WaitForExit(); + string newDir = Path.Combine(appDataBaseDir, $"WhoAmIBotNode_{DateTime.Now.ToString(dateTimeFileFormat)}\\"); + if (!Directory.Exists(newDir)) Directory.CreateDirectory(newDir); + string gitDirToCopy = Path.Combine(gitNodeDirectory, "WhoAmIBot\\WhoAmIBotNode\\bin\\Release"); + DeepCopy(new DirectoryInfo(gitDirToCopy), new DirectoryInfo(newDir)); + #endregion + newDir = Path.Combine(newDir, "WhoAmIBot.dll"); + Restart?.Invoke(this, new RestartEventArgs(path, newDir)); + ProcessStartInfo psi = new ProcessStartInfo(controlUpdaterPath, "\"" + path.Trim('"') + "\" \"" + newDir.Trim('"') + "\""); + Process.Start(psi); + } #endregion #region Language diff --git a/WhoAmIBot/WhoAmIBot.csproj b/WhoAmIBot/WhoAmIBot.csproj index d4eef52..c1004f3 100644 --- a/WhoAmIBot/WhoAmIBot.csproj +++ b/WhoAmIBot/WhoAmIBot.csproj @@ -80,6 +80,7 @@ + diff --git a/WhoAmIBotNode/Program.cs b/WhoAmIBotNode/Program.cs index bdf55c4..c4feee8 100644 --- a/WhoAmIBotNode/Program.cs +++ b/WhoAmIBotNode/Program.cs @@ -483,8 +483,9 @@ private static void Dispose() static void Main(string[] args) { if (args.Length < 1) return; - using (PipeStream pipeClient = new AnonymousPipeClientStream(PipeDirection.In, args[0])) + using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", args[0].Trim('"'), PipeDirection.In)) { + pipeClient.Connect(); using (var sr = new StreamReader(pipeClient)) { string data; @@ -533,9 +534,8 @@ static void Main(string[] args) State = NodeState.Stopped; running = false; } - continue; } - if (!string.IsNullOrEmpty(data)) + else if (!string.IsNullOrEmpty(data)) { HandleData(data); } @@ -1243,7 +1243,8 @@ private static void Getgames_Command(Message msg) foreach (var s in list) { if (string.IsNullOrWhiteSpace(s)) continue; - var t = client.SendTextMessageAsync(msg.Chat.Id, s, replyMarkup: ReplyMarkupMaker.InlineGetGames(NodeGames, msg.Chat.Id)); + var t = client.SendTextMessageAsync(msg.Chat.Id, s, replyMarkup: ReplyMarkupMaker.InlineGetGames(NodeGames, msg.Chat.Id), + parseMode: ParseMode.Html); t.Wait(); sent.Add(t.Result); }