public void WriteJsonCommands(JsonCommandsfile commandsfile, string targetfile) { JsonSerializer serializer = new JsonSerializer(); using StreamWriter sw = new StreamWriter(@"" + root_folder + "/" + targetfile); using JsonWriter writer = new JsonTextWriter(sw) { Formatting = Formatting.Indented }; serializer.Serialize(writer, commandsfile); }
protected void LoadCustomCommands() { if (running_in_docker == true) { // search env for cmd_ foreach (System.Collections.DictionaryEntry env in Environment.GetEnvironmentVariables()) { string name = env.Key.ToString(); if (name.StartsWith("cmd_") == true) { string[] steps = env.Value.ToString().Split(new [] { "{-}" }, StringSplitOptions.None); name = name.Replace("cmd_", ""); name = name.ToLowerInvariant(); custom_commands.Add(name, steps); } } } else { // search for file called "commands.cmd" JsonCommandsfile LoadedCommands = new JsonCommandsfile { CustomCommands = new [] { "sayexample!!!say|||0~#~Hello{-}delay|||2500{-}say|||0~#~Bye" } }; string CommandsFile = "custom_commands.json"; SimpleIO io = new SimpleIO(); io.ChangeRoot(use_folder); if (SimpleIO.FileType(CommandsFile, "json") == true) { if (io.Exists(CommandsFile) == true) { string json = io.ReadFile(CommandsFile); if (json.Length > 0) { try { LoadedCommands = JsonConvert.DeserializeObject <JsonCommandsfile>(json); foreach (string loaded in LoadedCommands.CustomCommands) { string[] bits = loaded.Split(new [] { "!!!" }, StringSplitOptions.None); if (bits.Length == 2) { string[] steps = bits[1].Split(new [] { "{-}" }, StringSplitOptions.None); custom_commands.Add(bits[0].ToLowerInvariant(), steps); } } } catch { io.MarkOld(CommandsFile); io.WriteJsonCommands(LoadedCommands, CommandsFile); } } else { io.WriteJsonCommands(LoadedCommands, CommandsFile); } } else { io.WriteJsonCommands(LoadedCommands, CommandsFile); } } else { io.WriteJsonCommands(LoadedCommands, CommandsFile); } } if (custom_commands.Count > 0) { Info("Custom commands: " + custom_commands.Count.ToString()); } }