From 058581126917d13d19db1f97730b6c1c7c0f7575 Mon Sep 17 00:00:00 2001 From: Belane Date: Wed, 14 Nov 2018 01:54:27 +0100 Subject: [PATCH 1/3] Help for Plugins Adds command line help for plugins --- ImportBlocks/ImportBlocks.cs | 2 ++ StatesDumper/StatesDumper.cs | 2 ++ 2 files changed, 4 insertions(+) diff --git a/ImportBlocks/ImportBlocks.cs b/ImportBlocks/ImportBlocks.cs index 4f039aa5a..0aae61a59 100644 --- a/ImportBlocks/ImportBlocks.cs +++ b/ImportBlocks/ImportBlocks.cs @@ -93,6 +93,8 @@ private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight) protected override bool OnMessage(object message) { if (!(message is string[] args)) return false; + if (args[0] == "plugin-help") + Console.Write("Import Blocks Commands:\n" + "\texport block[s] \n"); if (args.Length < 2) return false; if (args[0] != "export") return false; if (args[1] != "block" && args[1] != "blocks") return false; diff --git a/StatesDumper/StatesDumper.cs b/StatesDumper/StatesDumper.cs index 11f98ae21..8a64abd20 100644 --- a/StatesDumper/StatesDumper.cs +++ b/StatesDumper/StatesDumper.cs @@ -13,6 +13,8 @@ public class StatesDumper : Plugin protected override bool OnMessage(object message) { if (!(message is string[] args)) return false; + if (args[0] == "plugin-help") + Console.Write("States Dumper Commands:\n" + "\tdump storage \n"); if (args.Length < 2) return false; if (args[0] != "dump") return false; switch (args[1]) From 015e33d8b8ffde6d8fe0d0b4847f4881a100705a Mon Sep 17 00:00:00 2001 From: Belane Date: Wed, 14 Nov 2018 15:38:38 +0100 Subject: [PATCH 2/3] plugin-help to help plugin-name --- ImportBlocks/ImportBlocks.cs | 7 +++++-- StatesDumper/StatesDumper.cs | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/ImportBlocks/ImportBlocks.cs b/ImportBlocks/ImportBlocks.cs index 0aae61a59..8e29380ed 100644 --- a/ImportBlocks/ImportBlocks.cs +++ b/ImportBlocks/ImportBlocks.cs @@ -93,9 +93,12 @@ private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight) protected override bool OnMessage(object message) { if (!(message is string[] args)) return false; - if (args[0] == "plugin-help") - Console.Write("Import Blocks Commands:\n" + "\texport block[s] \n"); if (args.Length < 2) return false; + if (args[0] == "help" && args[1] == GetType().Name) + { + Console.Write("Import Blocks Commands:\n" + "\texport block[s] \n"); + return true; + } if (args[0] != "export") return false; if (args[1] != "block" && args[1] != "blocks") return false; if (args.Length >= 3 && uint.TryParse(args[2], out uint start)) diff --git a/StatesDumper/StatesDumper.cs b/StatesDumper/StatesDumper.cs index 8a64abd20..8fe86e9ae 100644 --- a/StatesDumper/StatesDumper.cs +++ b/StatesDumper/StatesDumper.cs @@ -13,9 +13,12 @@ public class StatesDumper : Plugin protected override bool OnMessage(object message) { if (!(message is string[] args)) return false; - if (args[0] == "plugin-help") - Console.Write("States Dumper Commands:\n" + "\tdump storage \n"); if (args.Length < 2) return false; + if (args[0] == "help" && args[1] == GetType().Name) + { + Console.Write("States Dumper Commands:\n" + "\tdump storage \n"); + return true; + } if (args[0] != "dump") return false; switch (args[1]) { From fb007c9e352172b61edab8d4690bbf1010e7e642 Mon Sep 17 00:00:00 2001 From: erikzhang Date: Fri, 16 Nov 2018 15:36:32 +0800 Subject: [PATCH 3/3] using switch case --- ImportBlocks/ImportBlocks.cs | 50 ++++++++++++++++++++++----------- StatesDumper/StatesDumper.cs | 54 +++++++++++++++++++++++------------- 2 files changed, 69 insertions(+), 35 deletions(-) diff --git a/ImportBlocks/ImportBlocks.cs b/ImportBlocks/ImportBlocks.cs index 8e29380ed..383a2431e 100644 --- a/ImportBlocks/ImportBlocks.cs +++ b/ImportBlocks/ImportBlocks.cs @@ -62,6 +62,13 @@ public ImportBlocks() }); } + private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight) + { + if (Settings.Default.MaxOnImportHeight == 0 || Settings.Default.MaxOnImportHeight >= currentImportBlockHeight) + return true; + return false; + } + private static IEnumerable GetBlocks(Stream stream, bool read_start = false) { using (BinaryReader r = new BinaryReader(stream)) @@ -83,24 +90,12 @@ private static IEnumerable GetBlocks(Stream stream, bool read_start = fal } } - private static bool CheckMaxOnImportHeight(uint currentImportBlockHeight) - { - if (Settings.Default.MaxOnImportHeight == 0 || Settings.Default.MaxOnImportHeight >= currentImportBlockHeight) - return true; - return false; - } - - protected override bool OnMessage(object message) + private bool OnExport(string[] args) { - if (!(message is string[] args)) return false; if (args.Length < 2) return false; - if (args[0] == "help" && args[1] == GetType().Name) - { - Console.Write("Import Blocks Commands:\n" + "\texport block[s] \n"); - return true; - } - if (args[0] != "export") return false; - if (args[1] != "block" && args[1] != "blocks") return false; + if (!string.Equals(args[1], "block", StringComparison.OrdinalIgnoreCase) + && !string.Equals(args[1], "blocks", StringComparison.OrdinalIgnoreCase)) + return false; if (args.Length >= 3 && uint.TryParse(args[2], out uint start)) { if (start > Blockchain.Singleton.Height) @@ -171,5 +166,28 @@ protected override bool OnMessage(object message) Console.WriteLine(); return true; } + + private bool OnHelp(string[] args) + { + if (args.Length < 2) return false; + if (!string.Equals(args[1], Name, StringComparison.OrdinalIgnoreCase)) + return false; + Console.Write($"{Name} Commands:\n" + "\texport block[s] \n"); + return true; + } + + protected override bool OnMessage(object message) + { + if (!(message is string[] args)) return false; + if (args.Length == 0) return false; + switch (args[0].ToLower()) + { + case "help": + return OnHelp(args); + case "export": + return OnExport(args); + } + return false; + } } } diff --git a/StatesDumper/StatesDumper.cs b/StatesDumper/StatesDumper.cs index 8fe86e9ae..fbe28a2d3 100644 --- a/StatesDumper/StatesDumper.cs +++ b/StatesDumper/StatesDumper.cs @@ -10,17 +10,40 @@ namespace Neo.Plugins { public class StatesDumper : Plugin { + private static void Dump(IEnumerable> states) + where TKey : ISerializable + where TValue : ISerializable + { + const string path = "dump.json"; + JArray array = new JArray(states.Select(p => + { + JObject state = new JObject(); + state["key"] = p.Key.ToArray().ToHexString(); + state["value"] = p.Value.ToArray().ToHexString(); + return state; + })); + File.WriteAllText(path, array.ToString()); + Console.WriteLine($"States ({array.Count}) have been dumped into file {path}"); + } + protected override bool OnMessage(object message) { if (!(message is string[] args)) return false; - if (args.Length < 2) return false; - if (args[0] == "help" && args[1] == GetType().Name) + if (args.Length == 0) return false; + switch (args[0].ToLower()) { - Console.Write("States Dumper Commands:\n" + "\tdump storage \n"); - return true; + case "help": + return OnHelp(args); + case "dump": + return OnDump(args); } - if (args[0] != "dump") return false; - switch (args[1]) + return false; + } + + private bool OnDump(string[] args) + { + if (args.Length < 2) return false; + switch (args[1].ToLower()) { case "storage": Dump(args.Length >= 3 @@ -32,20 +55,13 @@ protected override bool OnMessage(object message) } } - private static void Dump(IEnumerable> states) - where TKey : ISerializable - where TValue : ISerializable + private bool OnHelp(string[] args) { - const string path = "dump.json"; - JArray array = new JArray(states.Select(p => - { - JObject state = new JObject(); - state["key"] = p.Key.ToArray().ToHexString(); - state["value"] = p.Value.ToArray().ToHexString(); - return state; - })); - File.WriteAllText(path, array.ToString()); - Console.WriteLine($"States ({array.Count}) have been dumped into file {path}"); + if (args.Length < 2) return false; + if (!string.Equals(args[1], Name, StringComparison.OrdinalIgnoreCase)) + return false; + Console.Write($"{Name} Commands:\n" + "\tdump storage \n"); + return true; } } }