private void PrintMessage(string message, Color color) { try { if (!IsDebug) { return; } if (LogPlace.InvokeRequired) { LogPlace.Invoke(new MethodInvoker(() => { SetDataToRichTextBox(LogPlace, message, color); })); } else { SetDataToRichTextBox(LogPlace, message, color); } ConsoleHistory.Add(message); LogCount++; } catch (Exception e) { Console.WriteLine(e); } }
private static bool ExecuteCommand(CommandContext context, List <string> commandAndArgs) { if (commandAndArgs.Count == 0) { return(true); } // Look up the command var command = context.GetCommand(commandAndArgs[0]); if (command == null) { return(false); } // Execute it commandAndArgs.RemoveAt(0); try { ExecuteCommand(command, commandAndArgs); } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message); ConsoleHistory.Dump("hott_*_crash.log"); } return(true); }
public void MovingCursor() { var history = new ConsoleHistory(); history.Add("Oldest"); history.Add("Middle"); history.Add("Youngest"); history.MoveCursor(SeekOrigin.Begin, 0); history.CurrentEntry.Should().Be("Oldest"); history.MoveCursor(SeekOrigin.Begin, 1); history.CurrentEntry.Should().Be("Middle"); history.MoveCursor(SeekOrigin.End, 0); history.CurrentEntry.Should().BeNull(); history.MoveCursor(SeekOrigin.End, -1); history.CurrentEntry.Should().Be("Youngest"); history.MoveCursor(SeekOrigin.End, -2); history.CurrentEntry.Should().Be("Middle"); history.MoveCursor(SeekOrigin.End, -3); history.CurrentEntry.Should().Be("Oldest"); history.MoveCursor(1); history.CurrentEntry.Should().Be("Middle"); history.Invoking(h => h.MoveCursor((SeekOrigin)0x10, 0)) .Should().Throw <ArgumentOutOfRangeException>(); }
public void TestEmptyHistory() { var history = new ConsoleHistory(); history.EntryCount.Should().Be(0); history.CurrentEntry.Should().BeNull(); history.MoveCursor(SeekOrigin.Current, 0).Should().BeTrue(); history.MoveCursor(SeekOrigin.Current, 1).Should().BeFalse(); history.MoveCursor(SeekOrigin.Current, -1).Should().BeFalse(); }
public bool ConsoleHaveLine(string line) { foreach (var l in ConsoleHistory.ToArray()) { if (l == line) { return(true); } } return(false); }
public void HistoryWithMax() { var history = new ConsoleHistory(2); history.Add("First"); history.EntryCount.Should().Be(1); history.Add("Second"); history.EntryCount.Should().Be(2); history.Add("Third"); history.EntryCount.Should().Be(2); }
public void TestConstructionThrowsOnNonPositiveMaxCount() { const int anyNegativeCount = -1; Action createWithZero = () => { var x = new ConsoleHistory(0); }; createWithZero.Should().Throw <ArgumentOutOfRangeException>(); Action createWithNegative = () => { var x = new ConsoleHistory(anyNegativeCount); }; createWithNegative.Should().Throw <ArgumentOutOfRangeException>(); }
public void AddEmptyEntry() { var history = new ConsoleHistory(); history.Add("Hello"); history.EntryCount.Should().Be(1); history.Add(null); history.EntryCount.Should().Be(1); history.Add(string.Empty); history.EntryCount.Should().Be(1); }
public void SingleEntry() { var history = new ConsoleHistory(); history.Add("Something"); history.EntryCount.Should().Be(1); history.CurrentEntry.Should().BeNull(); history.MoveCursor(SeekOrigin.Current, 0).Should().BeTrue(); history.MoveCursor(SeekOrigin.Current, 1).Should().BeFalse(); history.MoveCursor(SeekOrigin.Current, -2).Should().BeFalse(); history.MoveCursor(SeekOrigin.Current, -1).Should().BeTrue(); history.CurrentEntry.Should().Be("Something"); }
public override bool Execute(List <string> args) { if (args.Count > 1) { return(false); } string path = args.Count == 0 ? null : args[0]; var result = ConsoleHistory.Dump(path); Console.WriteLine("Successfully dumped log to '{0}'.", result); return(true); }
private ConsoleLineInput CreateInput(IConsoleOutput consoleOutput = null, ConsoleCompletionHandler completionHandler = null) { consoleOutput = consoleOutput ?? Substitute.For <IConsoleOutput>(); var buffer = new ConsoleInputBuffer(); var history = new ConsoleHistory(); var input = new ConsoleLineInput(consoleOutput, buffer, history, completionHandler); input.ConsoleOutput.Should().BeSameAs(consoleOutput); input.Buffer.Should().BeSameAs(buffer); input.History.Should().BeSameAs(history); input.CompletionHandler.Should().BeSameAs(completionHandler); input.InsertMode.Should().BeTrue(); return(input); }
private static bool ExecuteCommand(CommandContext context, List <string> commandAndArgs) { if (commandAndArgs.Count == 0) { return(true); } // Look up the command var command = context.GetCommand(commandAndArgs[0]); if (command == null) { if ((command = context.GetCommand(commandAndArgs[0].ToLower())) == null) { return(false); } } // Execute it commandAndArgs.RemoveAt(0); #if !DEBUG try { #endif if (!command.Execute(commandAndArgs).Equals(true)) { Console.WriteLine("{0}: {1}", command.Name, command.Description); Console.WriteLine(); Console.WriteLine("Usage:"); Console.WriteLine("{0}", command.Usage); Console.WriteLine(); Console.WriteLine("Use \"help {0}\" for more information.", command.Name); } #if !DEBUG } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message); Console.WriteLine("STACKTRACE: " + Environment.NewLine + e.StackTrace); ConsoleHistory.Dump("hott_*_crash.log"); } #endif return(true); }
public void create(ConsoleHistory consoleHistory) { if (consoleHistory.Timestamp == null) { consoleHistory.Timestamp = DateTime.Now; } var connection = DataUtils.NewDbConnection(DbConnectionMode.ReadWriteCreate); try { connection.Open(); var cmd = connection.CreateCommand(); cmd.Parameters.Add() foreach (SqliteParameter param in cmd.Parameters) { if (!first) { columns.Append(", "); values.Append(", "); } columns.Append($"'{param.ParameterName.Replace("@", "")}'"); values.Append($"{param.ParameterName}"); first = false; } // @TODO: toLower only first character cmd.CommandText = $"INSERT INTO {baseModelKey.GetType().Name.ToLower()} " + $"({columns}) " + $"VALUES ({values});"; cmd.ExecuteNonQuery(); } catch (Exception e) { // @TODO values must be set throw e; } finally { connection.Close(); } }
public void MultipleEntries() { var history = new ConsoleHistory(); history.Add("Older"); history.Add("Newer"); history.EntryCount.Should().Be(2); history.CurrentEntry.Should().BeNull(); history.MoveCursor(SeekOrigin.Current, -1).Should().BeTrue(); history.CurrentEntry.Should().Be("Newer"); history.MoveCursor(SeekOrigin.Current, -1).Should().BeTrue(); history.CurrentEntry.Should().Be("Older"); history.MoveCursor(SeekOrigin.Current, 1).Should().BeTrue(); history.CurrentEntry.Should().Be("Newer"); history.MoveCursor(SeekOrigin.Current, 1).Should().BeTrue(); history.MoveCursor(SeekOrigin.Current, -2).Should().BeTrue(); history.CurrentEntry.Should().Be("Older"); }
private void PrintMessage(string message, Color color) { try { //if (!IsDebug) return; if (richTextBox.InvokeRequired) { richTextBox.Invoke(new MethodInvoker(() => { SetDataToRichTextBox(message, color); })); } else { SetDataToRichTextBox(message, color); } ConsoleHistory.Add(message); } catch (Exception e) { Console.WriteLine(e); } }
static void Main(string[] args) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-US"); ConsoleHistory.Initialize(); // Get the file path from the first argument // If no argument is given, load tags.dat var filePath = (args.Length > 0) ? args[0] : "tags.dat"; // If there are extra arguments, use them to automatically execute a command List <string> autoexecCommand = null; if (args.Length > 1) { autoexecCommand = args.Skip(1).ToList(); } if (autoexecCommand == null) { Console.WriteLine("Tag Tool [{0}]", Assembly.GetExecutingAssembly().GetName().Version); Console.WriteLine(); Console.WriteLine("Please report any bugs and feature requests at"); Console.WriteLine("<https://github.com/camden-smallwood/TagTool/issues>."); Console.WriteLine(); Console.Write("Reading tags..."); } // Load the tag cache FileInfo fileInfo = null; TagCache cache = null; try { fileInfo = new FileInfo(filePath); using (var stream = fileInfo.Open(FileMode.Open, FileAccess.Read)) cache = new TagCache(stream); } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message); ConsoleHistory.Dump("hott_*_tags_init.log"); return; } if (autoexecCommand == null) { Console.WriteLine("{0} tags loaded.", cache.Tags.Count); } // Version detection DefinitionSet closestVersion; var version = Definition.Detect(cache, out closestVersion); if (version != DefinitionSet.Unknown) { if (autoexecCommand == null) { var buildDate = DateTime.FromFileTime(cache.Timestamp); Console.WriteLine("- Detected target engine version {0}.", Definition.GetVersionString(closestVersion)); Console.WriteLine("- This cache file was built on {0} at {1}.", buildDate.ToShortDateString(), buildDate.ToShortTimeString()); } } else { Console.WriteLine("WARNING: The cache file's version was not recognized!"); Console.WriteLine("Using the closest known version {0}.", Definition.GetVersionString(closestVersion)); version = closestVersion; } // Load stringIDs Console.Write("Reading stringIDs..."); var stringIdPath = Path.Combine(fileInfo.DirectoryName ?? "", "string_ids.dat"); var resolver = StringIDResolverFactory.Create(version); StringIDCache stringIds = null; try { using (var stream = File.OpenRead(stringIdPath)) stringIds = new StringIDCache(stream, resolver); } catch (IOException) { Console.WriteLine("Warning: unable to open string_ids.dat!"); Console.WriteLine("Commands which require stringID values will be unavailable."); } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message); ConsoleHistory.Dump("hott_*_string_ids_init.log"); return; } if (autoexecCommand == null && stringIds != null) { Console.WriteLine("{0} strings loaded.", stringIds.Strings.Count); Console.WriteLine(); } var info = new OpenTagCache { Cache = cache, CacheFile = fileInfo, StringIDs = stringIds, StringIDsFile = (stringIds != null) ? new FileInfo(stringIdPath) : null, Version = version, Serializer = new TagSerializer(version), Deserializer = new TagDeserializer(version), }; var tagNamesPath = "TagNames\\tagnames_" + Definition.GetVersionString(version) + ".csv"; if (File.Exists(tagNamesPath)) { using (var tagNamesStream = File.Open(tagNamesPath, FileMode.Open, FileAccess.Read)) { var reader = new StreamReader(tagNamesStream); while (!reader.EndOfStream) { var line = reader.ReadLine(); var separatorIndex = line.IndexOf(','); var indexString = line.Substring(2, separatorIndex - 2); int tagIndex; if (!int.TryParse(indexString, NumberStyles.HexNumber, null, out tagIndex)) { tagIndex = -1; } if (tagIndex < 0 || tagIndex >= cache.Tags.Count) { continue; } var nameString = line.Substring(separatorIndex + 1); if (nameString.Contains(" ")) { var lastSpaceIndex = nameString.LastIndexOf(' '); nameString = nameString.Substring(lastSpaceIndex + 1, nameString.Length - lastSpaceIndex - 1); } info.TagNames[tagIndex] = nameString; } reader.Close(); } } foreach (var tag in info.Cache.Tags) { if (tag != null && !info.TagNames.ContainsKey(tag.Index)) { info.TagNames[tag.Index] = $"0x{tag.Index:X4}"; } } // Create command context var contextStack = new CommandContextStack(); var tagsContext = TagCacheContextFactory.Create(contextStack, info); contextStack.Push(tagsContext); // If autoexecuting a command, just run it and return if (autoexecCommand != null) { if (!ExecuteCommand(contextStack.Context, autoexecCommand)) { Console.WriteLine("Unrecognized command: {0}", autoexecCommand[0]); } return; } Console.WriteLine("Enter \"help\" to list available commands. Enter \"exit\" to quit."); while (true) { // Read and parse a command Console.WriteLine(); Console.Write("{0}> ", contextStack.GetPath()); var commandLine = Console.ReadLine(); if (commandLine == null) { break; } string redirectFile; var commandArgs = ArgumentParser.ParseCommand(commandLine, out redirectFile); if (commandArgs.Count == 0) { continue; } // If "exit" or "quit" is given, pop the current context if (commandArgs[0] == "exit" || commandArgs[0] == "quit") { if (!contextStack.Pop()) { break; // No more contexts - quit } continue; } // Handle redirection var oldOut = Console.Out; StreamWriter redirectWriter = null; if (redirectFile != null) { redirectWriter = new StreamWriter(File.Open(redirectFile, FileMode.Create, FileAccess.Write)); Console.SetOut(redirectWriter); } // Try to execute it if (!ExecuteCommand(contextStack.Context, commandArgs)) { Console.WriteLine("Unrecognized command: {0}", commandArgs[0]); Console.WriteLine("Use \"help\" to list available commands."); } // Undo redirection if (redirectFile != null) { Console.SetOut(oldOut); redirectWriter.Dispose(); Console.WriteLine("Wrote output to {0}.", redirectFile); } } }
public void InvalidHistory() { Action create = () => { var x = new ConsoleHistory(0); }; create.ShouldThrow <ArgumentOutOfRangeException>(); }
void Awake() { // cache reference to ConsoleHistory m_consoleHistory = GetComponent <ConsoleHistory>(); }
private void ContextMenuCopyToClickBoardButton_Click(object sender, EventArgs e) { var r = ConsoleHistory.Aggregate(string.Empty, (current, line) => current + $"{line}\n"); Clipboard.SetText(r); }
static void Main(string[] args) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-US"); ConsoleHistory.Initialize(); // If there are extra arguments, use them to automatically execute a command List <string> autoexecCommand = null; if (args.Length > 1) { autoexecCommand = args.Skip(1).ToList(); } if (autoexecCommand == null) { Console.WriteLine($"Tag Tool [{Assembly.GetExecutingAssembly().GetName().Version}]"); Console.WriteLine(); Console.WriteLine("Please report any bugs and/or feature requests at"); Console.WriteLine("<https://github.com/TheGuardians-CI/TagTool/issues>."); Console.WriteLine(); } start: // Get the file path from the first argument // If no argument is given, load tags.dat var fileInfo = new FileInfo((args.Length > 0) ? args[0] : "tags.dat"); while (!fileInfo.Exists) { Console.WriteLine("Enter the path to 'tags.dat':"); Console.Write("> "); var tagCacheFile = Console.ReadLine(); switch (tagCacheFile.ToLower()) { case "restart": Console.WriteLine(); goto start; case "exit": case "quit": Console.WriteLine(); goto end; } //sometimes drag&drop files have quotes placed around them, remove the quotes tagCacheFile = tagCacheFile.Replace("\"", "").Replace("\'", ""); if (File.Exists(tagCacheFile)) { fileInfo = new FileInfo(tagCacheFile); } else { Console.WriteLine("Invalid path to 'tags.dat'!"); } Console.WriteLine(); } HaloOnlineCacheContext cacheContext = null; #if !DEBUG try { #endif cacheContext = new HaloOnlineCacheContext(fileInfo.Directory); #if !DEBUG } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message); Console.WriteLine("STACKTRACE: " + Environment.NewLine + e.StackTrace); ConsoleHistory.Dump("hott_*_init.log"); return; } #endif // Create command context var contextStack = new CommandContextStack(); var tagsContext = TagCacheContextFactory.Create(contextStack, cacheContext); contextStack.Push(tagsContext); // If autoexecuting a command, just run it and return if (autoexecCommand != null) { if (!ExecuteCommand(contextStack.Context, autoexecCommand)) { Console.WriteLine("Unrecognized command: {0}", autoexecCommand[0]); } return; } Console.WriteLine("Enter \"help\" to list available commands. Enter \"exit\" to quit."); while (true) { // Read and parse a command Console.WriteLine(); Console.Write("{0}> ", contextStack.GetPath()); Console.Title = $"TagTool {contextStack.GetPath()}>"; var commandLine = Console.ReadLine(); if (commandLine == null) { break; } var commandArgs = ArgumentParser.ParseCommand(commandLine, out string redirectFile); if (commandArgs.Count == 0) { continue; } switch (commandArgs[0].ToLower()) { case "restart": Console.WriteLine(); goto start; case "quit": Console.WriteLine(); goto end; case "exit": if (!contextStack.Pop()) { goto end; } continue; case "cyka": Console.WriteLine("blyat!"); continue; } // Handle redirection var oldOut = Console.Out; StreamWriter redirectWriter = null; if (redirectFile != null) { redirectWriter = new StreamWriter(File.Open(redirectFile, FileMode.Create, FileAccess.Write)); Console.SetOut(redirectWriter); } // Try to execute it if (!ExecuteCommand(contextStack.Context, commandArgs)) { Console.WriteLine("Unrecognized command: {0}", commandArgs[0]); Console.WriteLine("Use \"help\" to list available commands."); } // Undo redirection if (redirectFile != null) { Console.SetOut(oldOut); redirectWriter.Dispose(); Console.WriteLine("Wrote output to {0}.", redirectFile); } } end : return; }
static void Main(string[] args) { CultureInfo.DefaultThreadCurrentCulture = CultureInfo.GetCultureInfo("en-US"); ConsoleHistory.Initialize(); // If there are extra arguments, use them to automatically execute a command List <string> autoexecCommand = null; if (args.Length > 1) { autoexecCommand = args.Skip(1).ToList(); } if (autoexecCommand == null) { Console.WriteLine($"Tag Tool [{Assembly.GetExecutingAssembly().GetName().Version}]"); Console.WriteLine(); Console.WriteLine("Please report any bugs and/or feature requests at"); Console.WriteLine("<https://github.com/TheGuardians-CI/TagTool/issues>."); Console.WriteLine(); } start: // Get the file path from the first argument // If no argument is given, load tags.dat // legacy from older cache system where only HO caches could be loaded var fileInfo = new FileInfo((args.Length > 0) ? args[0] : "tags.dat"); while (!fileInfo.Exists) { Console.WriteLine("Enter the path to a Halo cache file (.map or tags.dat)':"); Console.Write("> "); var tagCacheFile = Console.ReadLine(); switch (tagCacheFile.ToLower()) { case "restart": Console.WriteLine(); goto start; case "exit": case "quit": Console.WriteLine(); goto end; } //sometimes drag&drop files have quotes placed around them, remove the quotes tagCacheFile = tagCacheFile.Replace("\"", "").Replace("\'", ""); if (File.Exists(tagCacheFile)) { fileInfo = new FileInfo(tagCacheFile); } else { Console.WriteLine("Invalid path to 'tags.dat'!"); } Console.WriteLine(); } GameCache gameCache = null; #if !DEBUG try { #endif gameCache = GameCache.Open(fileInfo); #if !DEBUG } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message); Console.WriteLine("STACKTRACE: " + Environment.NewLine + e.StackTrace); ConsoleHistory.Dump("hott_*_init.log"); return; } #endif // Create command context var contextStack = new CommandContextStack(); var tagsContext = TagCacheContextFactory.Create(contextStack, gameCache); contextStack.Push(tagsContext); var commandRunner = new CommandRunner(contextStack); // If autoexecuting a command, just run it and return if (autoexecCommand != null) { commandRunner.RunCommand(string.Join(" ", autoexecCommand), false); goto end; } Console.WriteLine("Enter \"help\" to list available commands. Enter \"exit\" to quit."); while (!commandRunner.EOF) { // Read and parse a command Console.WriteLine(); Console.Write("{0}> ", contextStack.GetPath()); Console.Title = $"TagTool {contextStack.GetPath()}>"; var line = Console.ReadLine(); if (line == "restart") { goto start; } commandRunner.RunCommand(line, false); } end : return; }