Skip to content

Commit

Permalink
Fixed settings for simple strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Splamy committed Aug 17, 2018
1 parent 06e4bc4 commit 4be7c65
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 28 deletions.
5 changes: 5 additions & 0 deletions TS3AudioBot/CommandSystem/Ast/AstValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
33 changes: 25 additions & 8 deletions TS3AudioBot/CommandSystem/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -88,6 +98,7 @@ public static AstNode ParseCommandRequest(string request, char commandChar = Def
}
strPtr.Next();
break;

default:
build = BuildStatus.ParseFreeString;
break;
Expand All @@ -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))
{
Expand All @@ -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;
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions TS3AudioBot/Config/ConfigEnumerable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
1 change: 1 addition & 0 deletions TS3AudioBot/Config/ConfigPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions TS3AudioBot/Config/ConfigValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace TS3AudioBot.Config
[DebuggerDisplay("{Key}:{Value}")]
public class ConfigValue<T> : ConfigPart
{
public override bool ExpectsString => typeof(T) == typeof(string);
private ConfigValue<T> backingValue;
private bool hasValue = false;
public T Default { get; }
Expand Down
4 changes: 4 additions & 0 deletions TS3AudioBot/Helper/IJsonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace TS3AudioBot.Helper

public interface IJsonSerializable
{
bool ExpectsString { get; }
void ToJson(JsonWriter writer);
E<string> FromJson(JsonReader reader);
}
Expand All @@ -24,6 +25,9 @@ public static class JsonSerializableExtensions
{
public static E<string> FromJson(this IJsonSerializable jsonConfig, string json)
{
if (jsonConfig.ExpectsString)
json = JsonConvert.SerializeObject(json);

var sr = new StringReader(json);
using (var reader = new JsonTextReader(sr))
{
Expand Down
28 changes: 9 additions & 19 deletions TS3AudioBot/MainCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -891,9 +887,7 @@ public static JsonArray<PlaylistItem> 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)
Expand Down Expand Up @@ -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<BuildData> CommandVersion()
{
var data = SystemData.AssemblyData;
return new JsonValue<BuildData>(data, data.ToLongString());
}
public static JsonValue<BuildData> CommandVersion() => new JsonValue<BuildData>(SystemData.AssemblyData, d => d.ToLongString());

[Command("volume")]
[Usage("<level>", "A new volume level between 0 and 100.")]
Expand Down
2 changes: 1 addition & 1 deletion TS3Client/Full/Ts3FullClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4be7c65

Please sign in to comment.