From 4be7c65471513a77238d9a2aa7917f4a29d65fd3 Mon Sep 17 00:00:00 2001 From: Splamy Date: Fri, 17 Aug 2018 20:44:39 +0200 Subject: [PATCH] Fixed settings for simple strings --- TS3AudioBot/CommandSystem/Ast/AstValue.cs | 5 ++++ TS3AudioBot/CommandSystem/CommandParser.cs | 33 ++++++++++++++++------ TS3AudioBot/Config/ConfigEnumerable.cs | 1 + TS3AudioBot/Config/ConfigPart.cs | 1 + TS3AudioBot/Config/ConfigValue.cs | 1 + TS3AudioBot/Helper/IJsonConfig.cs | 4 +++ TS3AudioBot/MainCommands.cs | 28 ++++++------------ TS3Client/Full/Ts3FullClient.cs | 2 +- 8 files changed, 47 insertions(+), 28 deletions(-) diff --git a/TS3AudioBot/CommandSystem/Ast/AstValue.cs b/TS3AudioBot/CommandSystem/Ast/AstValue.cs index 4e782ae7..3ec5b6fa 100644 --- a/TS3AudioBot/CommandSystem/Ast/AstValue.cs +++ b/TS3AudioBot/CommandSystem/Ast/AstValue.cs @@ -16,6 +16,11 @@ internal class AstValue : AstNode public override AstType Type => AstType.Value; public string Value { get; set; } + public void BuildValue() + { + Value = FullRequest.Substring(Position, Length); + } + public override void Write(StringBuilder strb, int depth) => strb.Space(depth).Append(Value); } } diff --git a/TS3AudioBot/CommandSystem/CommandParser.cs b/TS3AudioBot/CommandSystem/CommandParser.cs index 830817ad..7805ac05 100644 --- a/TS3AudioBot/CommandSystem/CommandParser.cs +++ b/TS3AudioBot/CommandSystem/CommandParser.cs @@ -57,7 +57,9 @@ public static AstNode ParseCommandRequest(string request, char commandChar = Def strPtr.SkipChar(delimeterChar); if (strPtr.End) + { build = BuildStatus.End; + } else { switch (strPtr.Char) @@ -66,20 +68,28 @@ public static AstNode ParseCommandRequest(string request, char commandChar = Def build = BuildStatus.ParseQuotedString; //goto case BuildStatus.ParseQuotedString; break; + case '(': if (!strPtr.HasNext) + { build = BuildStatus.ParseFreeString; + } else if (strPtr.IsNext(commandChar)) { strPtr.Next('('); build = BuildStatus.ParseCommand; } else + { build = BuildStatus.ParseFreeString; + } break; + case ')': if (comAst.Count <= 0) + { build = BuildStatus.End; + } else { comAst.Pop(); @@ -88,6 +98,7 @@ public static AstNode ParseCommandRequest(string request, char commandChar = Def } strPtr.Next(); break; + default: build = BuildStatus.ParseFreeString; break; @@ -96,8 +107,6 @@ public static AstNode ParseCommandRequest(string request, char commandChar = Def break; case BuildStatus.ParseFreeString: - strb.Clear(); - var valFreeAst = new AstValue(); using (strPtr.TrackNode(valFreeAst)) { @@ -106,11 +115,12 @@ public static AstNode ParseCommandRequest(string request, char commandChar = Def if ((strPtr.Char == '(' && strPtr.HasNext && strPtr.IsNext(commandChar)) || strPtr.Char == ')' || strPtr.Char == delimeterChar) + { break; - strb.Append(strPtr.Char); + } } } - valFreeAst.Value = strb.ToString(); + valFreeAst.BuildValue(); buildCom = comAst.Peek(); buildCom.Parameter.Add(valFreeAst); build = BuildStatus.SelectParam; @@ -127,14 +137,21 @@ public static AstNode ParseCommandRequest(string request, char commandChar = Def bool escaped = false; for (; !strPtr.End; strPtr.Next()) { - if (strPtr.Char == '\\') escaped = true; + if (strPtr.Char == '\\') + { + escaped = true; + } else if (strPtr.Char == '"') { - if (escaped) strb.Length--; + if (escaped) { strb.Length--; } else { strPtr.Next(); break; } escaped = false; } - else escaped = false; + else + { + escaped = false; + } + strb.Append(strPtr.Char); } } @@ -208,7 +225,7 @@ public NodeTracker TrackNode(AstNode node) astnode.Position = index; astnode.Length = 0; } - return (curTrack = new NodeTracker(this)); + return curTrack = new NodeTracker(this); } private void UntrackNode() diff --git a/TS3AudioBot/Config/ConfigEnumerable.cs b/TS3AudioBot/Config/ConfigEnumerable.cs index e1ccc62e..3a6211d8 100644 --- a/TS3AudioBot/Config/ConfigEnumerable.cs +++ b/TS3AudioBot/Config/ConfigEnumerable.cs @@ -20,6 +20,7 @@ public abstract class ConfigEnumerable : ConfigPart protected virtual TomlTable.TableTypes TableType { get => TomlTable.TableTypes.Default; } public TomlTable TomlObject { get; set; } + public override bool ExpectsString => false; public override void FromToml(TomlObject tomlObject) { diff --git a/TS3AudioBot/Config/ConfigPart.cs b/TS3AudioBot/Config/ConfigPart.cs index 1908722b..92e5894a 100644 --- a/TS3AudioBot/Config/ConfigPart.cs +++ b/TS3AudioBot/Config/ConfigPart.cs @@ -34,6 +34,7 @@ protected ConfigPart(string key) Key = key; } + public abstract bool ExpectsString { get; } public abstract void FromToml(TomlObject tomlObject); public abstract void ToToml(bool writeDefaults, bool writeDocumentation); public abstract void Derive(ConfigPart derived); diff --git a/TS3AudioBot/Config/ConfigValue.cs b/TS3AudioBot/Config/ConfigValue.cs index 836e4dad..eec3f2ed 100644 --- a/TS3AudioBot/Config/ConfigValue.cs +++ b/TS3AudioBot/Config/ConfigValue.cs @@ -19,6 +19,7 @@ namespace TS3AudioBot.Config [DebuggerDisplay("{Key}:{Value}")] public class ConfigValue : ConfigPart { + public override bool ExpectsString => typeof(T) == typeof(string); private ConfigValue backingValue; private bool hasValue = false; public T Default { get; } diff --git a/TS3AudioBot/Helper/IJsonConfig.cs b/TS3AudioBot/Helper/IJsonConfig.cs index 3d4f9175..1ff08df8 100644 --- a/TS3AudioBot/Helper/IJsonConfig.cs +++ b/TS3AudioBot/Helper/IJsonConfig.cs @@ -16,6 +16,7 @@ namespace TS3AudioBot.Helper public interface IJsonSerializable { + bool ExpectsString { get; } void ToJson(JsonWriter writer); E FromJson(JsonReader reader); } @@ -24,6 +25,9 @@ public static class JsonSerializableExtensions { public static E FromJson(this IJsonSerializable jsonConfig, string json) { + if (jsonConfig.ExpectsString) + json = JsonConvert.SerializeObject(json); + var sr = new StringReader(json); using (var reader = new JsonTextReader(sr)) { diff --git a/TS3AudioBot/MainCommands.cs b/TS3AudioBot/MainCommands.cs index 0d98d5b3..09269be4 100644 --- a/TS3AudioBot/MainCommands.cs +++ b/TS3AudioBot/MainCommands.cs @@ -817,15 +817,11 @@ public static void CommandListPlay(PlaylistManager playlistManager, PlayManager { var plist = AutoGetPlaylist(session, invoker); - if (!index.HasValue || (index.Value >= 0 && index.Value < plist.Count)) - { - playlistManager.PlayFreelist(plist); - playlistManager.Index = index ?? 0; - } - else - { + if (index.HasValue && (index.Value < 0 || index.Value >= plist.Count)) throw new CommandException(strings.error_playlist_item_index_out_of_range, CommandExceptionReason.CommandError); - } + + playlistManager.PlayFreelist(plist); + playlistManager.Index = index ?? 0; var item = playlistManager.Current(); if (item != null) @@ -891,9 +887,7 @@ public static JsonArray CommandListShow(PlaylistManager playlistMa [Command("next")] public static void CommandNext(PlayManager playManager, InvokerData invoker) - { - playManager.Next(invoker).UnwrapThrow(); - } + => playManager.Next(invoker).UnwrapThrow(); [Command("pm")] public static string CommandPm(CallerInfo caller, InvokerData invoker) @@ -1340,20 +1334,16 @@ public static void CommandUnsubscribe(IVoiceTarget targetManager, InvokerData in [Command("unsubscribe channel")] public static void CommandUnsubscribeChannel(IVoiceTarget targetManager, InvokerData invoker, ulong? channel = null) { - var subChan = channel ?? invoker.ChannelId ?? 0; - if (subChan != 0) - targetManager.WhisperChannelUnsubscribe(subChan, false); + var subChan = channel ?? invoker.ChannelId; + if (subChan.HasValue) + targetManager.WhisperChannelUnsubscribe(subChan.Value, false); } [Command("unsubscribe temporary")] public static void CommandUnsubscribeTemporary(IVoiceTarget targetManager) => targetManager.ClearTemporary(); [Command("version")] - public static JsonValue CommandVersion() - { - var data = SystemData.AssemblyData; - return new JsonValue(data, data.ToLongString()); - } + public static JsonValue CommandVersion() => new JsonValue(SystemData.AssemblyData, d => d.ToLongString()); [Command("volume")] [Usage("", "A new volume level between 0 and 100.")] diff --git a/TS3Client/Full/Ts3FullClient.cs b/TS3Client/Full/Ts3FullClient.cs index df526146..5487feac 100644 --- a/TS3Client/Full/Ts3FullClient.cs +++ b/TS3Client/Full/Ts3FullClient.cs @@ -269,7 +269,7 @@ partial void ProcessEachInitServer(InitServer initServer) partial void ProcessEachPluginCommand(PluginCommand cmd) { if (cmd.Name == "cliententerview" && cmd.Data == "version") - SendPluginCommand("cliententerview", "TAB", PluginTargetMode.Client); + SendPluginCommand("cliententerview", "TAB", PluginTargetMode.Server); } partial void ProcessEachCommandError(CommandError error)