Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
Fixed console command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
KarlOfDuty committed Jul 18, 2022
1 parent c76d46d commit dd46603
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 31 deletions.
10 changes: 2 additions & 8 deletions SCPDiscordPlugin/BotListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public BotListener(SCPDiscord plugin)
plugin.Error("Connection to bot lost.");

else
plugin.Error("Couldnt parse incoming packet!\n" + e.ToString());
plugin.Error("Couldn't parse incoming packet!\n" + e.ToString());

return;
}
Expand All @@ -51,13 +51,7 @@ public BotListener(SCPDiscord plugin)
break;

case MessageWrapper.MessageOneofCase.ConsoleCommand:
string[] words = data.ConsoleCommand.Command.Split(' ');
string response = plugin.ConsoleCommand(plugin.PluginManager.Server, words[0], words.Skip(1).ToArray());
Dictionary<string, string> variables = new Dictionary<string, string>
{
{ "feedback", response }
};
plugin.SendMessageByID(data.ConsoleCommand.ChannelID, "botresponses.consolecommandfeedback", variables);
plugin.sync.ScheduleConsoleCommand(data.ConsoleCommand);
break;

case MessageWrapper.MessageOneofCase.RoleResponse:
Expand Down
4 changes: 1 addition & 3 deletions SCPDiscordPlugin/NetworkSystem.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using Google.Protobuf;
using SCPDiscord.EventListeners;
using SCPDiscord.Interface;
using Smod2.API;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand Down Expand Up @@ -172,7 +170,7 @@ private static bool SendMessage(MessageWrapper message)
{
if (message == null)
{
plugin.Error("Tried to send message but it was null.");
plugin.Error("Tried to send message but it was null. " + new StackTrace());
return true;
}

Expand Down
44 changes: 24 additions & 20 deletions SCPDiscordPlugin/SCPDiscord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace SCPDiscord
name = "SCPDiscord",
description = "SCP:SL - Discord bridge.",
id = "karlofduty.scpdiscord",
version = "2.0.4",
version = "2.0.5",
SmodMajor = 3,
SmodMinor = 10,
SmodRevision = 1
Expand All @@ -35,6 +35,8 @@ public class SCPDiscord : Plugin
{
public readonly Stopwatch serverStartTime = new Stopwatch();

internal SynchronousExecutor sync;

internal static SCPDiscord plugin;

public bool roundStarted = false;
Expand All @@ -57,6 +59,8 @@ public override void Register()
AddEventHandlers(new TeamEventListener(this), Priority.LAST);
AddEventHandlers(new SyncPlayerRole(), Priority.LAST);

sync = new SynchronousExecutor(this);
AddEventHandlers(sync, Priority.NORMAL);

AddConfig(new Smod2.Config.ConfigSetting("max_players", 20, true, "Gets the max players without reserved slots."));
AddConfig(new Smod2.Config.ConfigSetting("scpdiscord_config_global", false, true, "Whether or not the config should be placed in the global config directory."));
Expand Down Expand Up @@ -84,15 +88,15 @@ public override void OnEnable()
AddCommand("scpd_grantvanillarank", new GrantVanillaRankCommand());

SetUpFileSystem();
this.roleSync = new RoleSync(this);
roleSync = new RoleSync(this);
LoadConfig();
if (this.Server.Port == Config.GetInt("bot.port"))
{
this.Error("ERROR: Server is running on the same port as the plugin, aborting...");
this.Disable();
Error("ERROR: Server is running on the same port as the plugin, aborting...");
Disable();
}
Language.Reload();

new Thread(() => new StartNetworkSystem(plugin)).Start();

GetMaxPlayers();
Expand Down Expand Up @@ -140,13 +144,13 @@ public void SetUpFileSystem()

if (!File.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml"))
{
this.Info("Config file '" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml' does not exist, creating...");
Info("Config file '" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml' does not exist, creating...");
File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml", Encoding.UTF8.GetString(Resources.config));
}

if (!File.Exists(FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_rolesync_global")) + "SCPDiscord/rolesync.json"))
{
plugin.Info("Config file rolesync.json does not exist, creating...");
Info("Config file rolesync.json does not exist, creating...");
File.WriteAllText(FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_rolesync_global")) + "SCPDiscord/rolesync.json", "[]");
}
}
Expand All @@ -159,27 +163,27 @@ public void LoadConfig()
try
{
Config.Reload(plugin);
this.Info("Successfully loaded config '" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml'.");
Info("Successfully loaded config '" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml'.");
}
catch (Exception e)
{
if (e is DirectoryNotFoundException)
{
this.Error("Config directory not found.");
Error("Config directory not found.");
}
else if (e is UnauthorizedAccessException)
{
this.Error("Primary language file access denied.");
Error("Primary language file access denied.");
}
else if (e is FileNotFoundException)
{
this.Error("'" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml' was not found.");
Error("'" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml' was not found.");
}
else if (e is JsonReaderException || e is YamlException)
{
this.Error("'" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml' formatting error.");
Error("'" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml' formatting error.");
}
this.Error("Error reading config file '" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml'. Aborting startup." + e);
Error("Error reading config file '" + FileManager.GetAppFolder(true, !GetConfigBool("scpdiscord_config_global")) + "SCPDiscord/config.yml'. Aborting startup." + e);
Disable();
}
}
Expand All @@ -193,7 +197,7 @@ public override void OnDisable()
{
shutdown = true;
NetworkSystem.Disconnect();
this.Info("SCPDiscord disabled.");
Info("SCPDiscord disabled.");
}

/// <summary>
Expand All @@ -204,47 +208,47 @@ public void Verbose(string message)
{
if (Config.GetBool("settings.verbose"))
{
plugin.Info(message);
Info(message);
}
}

public void VerboseWarn(string message)
{
if (Config.GetBool("settings.verbose"))
{
plugin.Warn(message);
Warn(message);
}
}

public void VerboseError(string message)
{
if (Config.GetBool("settings.verbose"))
{
plugin.Error(message);
Error(message);
}
}

public new void Debug(string message)
{
if (Config.GetBool("settings.debug"))
{
plugin.Info(message);
Info(message);
}
}

public void DebugWarn(string message)
{
if (Config.GetBool("settings.debug"))
{
plugin.Warn(message);
Warn(message);
}
}

public void DebugError(string message)
{
if (Config.GetBool("settings.debug"))
{
plugin.Error(message);
Error(message);
}
}

Expand Down
1 change: 1 addition & 0 deletions SCPDiscordPlugin/SCPDiscordPlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
<Compile Include="Commands\UnsyncCommand.cs" />
<Compile Include="Commands\VerboseCommand.cs" />
<Compile Include="Commands\ValidateCommand.cs" />
<Compile Include="SynchronousExecutor.cs" />
<Compile Include="Utilities.cs" />
</ItemGroup>
<ItemGroup>
Expand Down
39 changes: 39 additions & 0 deletions SCPDiscordPlugin/SynchronousExecutor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using SCPDiscord.Interface;
using Smod2.EventHandlers;
using Smod2.Events;

namespace SCPDiscord
{
public class SynchronousExecutor : IEventHandlerFixedUpdate
{
private readonly SCPDiscord plugin;
private readonly ConcurrentQueue<ConsoleCommand> queuedCommands = new ConcurrentQueue<ConsoleCommand>();

public SynchronousExecutor(SCPDiscord pl)
{
plugin = pl;
}

public void ScheduleConsoleCommand(ConsoleCommand command)
{
queuedCommands.Enqueue(command);
}

public void OnFixedUpdate(FixedUpdateEvent ev)
{
while(queuedCommands.TryDequeue(out ConsoleCommand command))
{
string[] words = command.Command.Split(' ');
string response = plugin.ConsoleCommand(plugin.PluginManager.Server, words[0], words.Skip(1).ToArray());
Dictionary<string, string> variables = new Dictionary<string, string>
{
{ "feedback", response }
};
plugin.SendMessageByID(command.ChannelID, "botresponses.consolecommandfeedback", variables);
}
}
}
}

0 comments on commit dd46603

Please sign in to comment.