public static void UseNewLanguageFile(string fileName, long id, int msgId) { var msg = "Moving file to production..\n"; msg += "Checking paths for duplicate language file...\n"; Bot.Api.EditMessageTextAsync(id, msgId, msg).Wait(); fileName += ".xml"; var tempPath = Bot.TempLanguageDirectory; var langPath = Bot.LanguageDirectory; var newFilePath = Path.Combine(tempPath, fileName); var copyToPath = Path.Combine(langPath, fileName); //get the new files language var doc = XDocument.Load(newFilePath); var newFileLang = new { Name = doc.Descendants("language").First().Attribute("name").Value, Base = doc.Descendants("language").First().Attribute("base").Value, Variant = doc.Descendants("language").First().Attribute("variant").Value }; //check for existing file var langs = Directory.GetFiles(langPath, "*.xml").Select(x => new LangFile(x)).ToList(); var lang = langs.FirstOrDefault(x => x.Name == newFileLang.Name && x.FilePath != copyToPath); if (lang != null) { msg += $"Found duplicate language (name attribute) with filename {Path.GetFileNameWithoutExtension(lang.FilePath)}\n"; copyToPath = lang.FilePath; } else { lang = langs.FirstOrDefault(x => x.Base == newFileLang.Base && x.Variant == newFileLang.Variant && x.Name != newFileLang.Name); if (lang != null) { msg += $"Found duplicate language (matching base and variant) with filename {Path.GetFileNameWithoutExtension(lang.FilePath)}\n"; msg += "Aborting!"; Bot.Api.EditMessageTextAsync(id, msgId, msg); return; } } System.IO.File.Copy(newFilePath, copyToPath, true); msg += "File copied to bot\n"; //#if RELEASE // msg += $"File copied to bot 1\n"; //#elif RELEASE2 // msg += $"File copied to bot 2\n"; //#endif //Bot.Api.EditMessageTextAsync(id, msgId, msg); //#if RELEASE // copyToPath = copyToPath.Replace("Werewolf 3.0", "Werewolf 3.0 Clone"); // System.IO.File.Copy(newFilePath, copyToPath, true); // msg += $"File copied to bot 2\n"; // Bot.Api.EditMessageTextAsync(id, msgId, msg); //#endif #if !DEBUG try { var gitPath = Path.Combine(@"C:\Werewolf Source\Werewolf\Werewolf for Telegram\Languages", Path.GetFileName(copyToPath)); File.Copy(newFilePath, gitPath, true); System.IO.File.Delete(newFilePath); msg += $"File copied to git directory\n"; if (Path.GetFileName(newFilePath) == Program.MasterLanguage) { var p = new Process { StartInfo = { FileName = @"C:\Werewolf Source\Werewolf\Werewolf for Telegram\Languages\commit.bat", Arguments = $"\"Syncing langfiles from Telegram (English.xml update)\"", WorkingDirectory = @"C:\Werewolf Source\Werewolf\Werewolf for Telegram\Languages", UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true } }; p.Start(); msg += "Started the committing process. Reading output from git..."; Bot.Edit(id, msgId, msg).Wait(); var output = ""; while (!p.StandardOutput.EndOfStream) { output += p.StandardOutput.ReadLine() + Environment.NewLine; } while (!p.StandardError.EndOfStream) { output += p.StandardError.ReadLine() + Environment.NewLine; } msg += "\nValidating the output..."; Bot.Edit(id, msgId, msg).Wait(); //validate the output if (output.Contains("failed")) { msg += "\n<b>Failed</b> to commit files. See control output for information"; Console.WriteLine(output); } else if (output.Contains("nothing to commit")) { msg += "\nNothing to commit."; } else { //try to grab the commit var regex = new Regex("(\\[master .*])"); var match = regex.Match(output); var commit = ""; if (match.Success) { commit = match.Value.Replace("[master ", "").Replace("]", ""); } msg += $"\n<b>Files committed successfully.</b> {(String.IsNullOrEmpty(commit) ? "" : $"<a href=\"https://github.com/GreyWolfDev/Werewolf/commit/" + commit + $"\">{commit}</a>")}"; } } } catch (Exception e) { var trace = e.StackTrace; var error = e.Message; do { e = e.InnerException; if (e == null) { break; } error += "\n" + e.Message; }while (true); Bot.Send("Error while trying to commit to git:\n" + error + "\n\n" + trace, id); } #endif using (var db = new WWContext()) { var newFile = new LangFile(copyToPath); // search for language file entry and update it or add it if it's not present yet Language language = db.Language.FirstOrDefault(x => x.FileName == newFile.FileName); if (language == null) { language = db.Language.Add(new Language { FileName = newFile.FileName }); } language.Base = newFile.Base; language.IsDefault = newFile.IsDefault; language.LangCode = newFile.LangCode; language.Name = newFile.Name; language.Variant = newFile.Variant; db.SaveChanges(); } var info = new ReloadLangInfo { LangName = Path.GetFileNameWithoutExtension(copyToPath) }; var json = JsonConvert.SerializeObject(info); foreach (var n in Bot.Nodes) { n.Broadcast(json); } msg += "\n<b>Operation complete.</b>"; Bot.Edit(id, msgId, msg, parsemode: ParseMode.Html); }
internal static async void MonitorUpdates() { #if !DEBUG try #endif { var baseDirectory = Path.Combine(Bot.RootDirectory, ".."); //go up one directory var updateDirectory = Path.Combine(Bot.RootDirectory, "Update"); while (Bot.Nodes.Count == 0) { await Task.Delay(500); } var currentVersion = Bot.Nodes.Max(x => Version.Parse(x.Version)); var currentChoice = new NodeChoice(); while (true) { //check nodes first foreach (var dir in Directory.GetDirectories(baseDirectory, "*Node*")) { //get the node exe in this directory var file = Directory.GetFiles(dir, "Werewolf Node.exe").First(); Version fvi = Version.Parse(FileVersionInfo.GetVersionInfo(file).FileVersion); if (fvi > currentChoice.Version) { currentChoice.Path = file; currentChoice.Version = fvi; } } if (currentChoice.Version > currentVersion) { currentVersion = currentChoice.Version; //alert dev group Bot.Send($"New node with version {currentVersion} found. Stopping old nodes.", -1001077134233); //kill existing nodes foreach (var node in Bot.Nodes) { node.ShutDown(); } await Task.Delay(500); foreach (var node in Bot.Nodes) { node.ShutDown(); } } //now check for Control update if (Directory.GetFiles(updateDirectory).Count() > 1) { //update available //sleep 5 seconds to allow any nodes to connect and whatnot. await Task.Delay(5000); //await Bot.Send($"New control found. Updating.", -1001077134233); //fire off the updater Process.Start(Path.Combine(Bot.RootDirectory, "Resources\\update.exe"), "-1001077134233"); Bot.Running = false; Program.Running = false; Bot.Api.StopReceiving(); //Thread.Sleep(500); using (var db = new WWContext()) { var bot = #if DEBUG 4; #elif BETA 3; #elif RELEASE 1; #elif RELEASE2 2; #endif #if !DEBUG var status = await db.BotStatus.FindAsync(bot); status.BotStatus = "Updating"; await db.SaveChangesAsync(); #endif } Environment.Exit(1); } //check once every 5 seconds await Task.Delay(5000); } //now we have the most recent version, launch one } #if !DEBUG catch (Exception e) { Bot.Send($"Error in update monitor: {e.Message}\n{e.StackTrace}", -1001077134233, parseMode: ParseMode.Default); } #endif }