More cleanup

project renaming
moved PluginImporter to SharedLibrary
config writer abstracted for plugins
This commit is contained in:
RaidMax 2017-06-12 17:47:31 -04:00
parent 0ef306a60c
commit 5d1c9bd218
36 changed files with 303 additions and 330 deletions

View File

@ -122,14 +122,12 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Commands.cs" />
<Compile Include="ServerConfig.cs" />
<Compile Include="Kayak.cs" />
<Compile Include="Logger.cs" />
<Compile Include="Main.cs" />
<Compile Include="Manager.cs" />
<Compile Include="PenaltyList.cs" />
<Compile Include="Plugins.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>

View File

@ -1,24 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text;
using SharedLibrary;
using SharedLibrary.Network;
using System.Threading.Tasks;
namespace IW4MAdmin
{
class Plugins : Command
{
public Plugins(String N, String D, String U, Player.Permission P, int args, bool nT) : base(N, D, U, P, args, nT) { }
public override async Task ExecuteAsync(Event E)
{
await E.Origin.Tell("^5Loaded Plugins:");
foreach (SharedLibrary.Interfaces.IPlugin P in PluginImporter.potentialPlugins)
{
await E.Origin.Tell(String.Format("^3{0} ^7[v^3{1}^7] by ^5{2}^7", P.Name, P.Version, P.Author));
}
}
}
}

View File

@ -42,7 +42,7 @@ namespace IW4MAdmin
NameValueCollection querySet = new NameValueCollection();
if (request.QueryString != null)
querySet = System.Web.HttpUtility.ParseQueryString(SharedLibrary.Utilities.removeNastyChars(request.QueryString));
querySet = System.Web.HttpUtility.ParseQueryString(SharedLibrary.Utilities.StripIllegalCharacters(request.QueryString));
querySet.Set("IP", IP);
SharedLibrary.HttpResponse requestedPage = WebService.GetPage(request.Path, querySet, request.Headers);

View File

@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Net;
using System.Threading;
using SharedLibrary;
using System.IO;
using SharedLibrary.Network;
using System.Threading.Tasks;
namespace IW4MAdmin
@ -17,12 +12,12 @@ namespace IW4MAdmin
{
static Manager Instance;
public List<Server> Servers { get; private set; }
List<AsyncStatus> TaskStatuses;
List<SharedLibrary.Helpers.AsyncStatus> TaskStatuses;
Database ClientDatabase;
Database AliasesDatabase;
SharedLibrary.Interfaces.IPenaltyList ClientPenalties;
List<Command> Commands;
List<MessageToken> MessageTokens;
List<SharedLibrary.Helpers.MessageToken> MessageTokens;
Kayak.IScheduler webServiceTask;
Thread WebThread;
public SharedLibrary.Interfaces.ILogger Logger { get; private set; }
@ -38,8 +33,8 @@ namespace IW4MAdmin
Logger = new Logger("Logs/IW4MAdmin.log");
Servers = new List<Server>();
Commands = new List<Command>();
TaskStatuses = new List<AsyncStatus>();
MessageTokens = new List<MessageToken>();
TaskStatuses = new List<SharedLibrary.Helpers.AsyncStatus>();
MessageTokens = new List<SharedLibrary.Helpers.MessageToken>();
ClientDatabase = new ClientsDB("Database/clients.rm");
AliasesDatabase = new AliasesDB("Database/aliases.rm");
@ -69,7 +64,7 @@ namespace IW4MAdmin
ServerConfig.Generate();
SharedLibrary.WebService.Init();
PluginImporter.Load();
SharedLibrary.Plugins.PluginImporter.Load(this);
foreach (var file in Configs)
{
@ -84,9 +79,12 @@ namespace IW4MAdmin
Servers.Add(ServerInstance);
// this way we can keep track of execution time and see if problems arise.
var Status = new AsyncStatus(ServerInstance, UPDATE_FREQUENCY);
var Status = new SharedLibrary.Helpers.AsyncStatus(ServerInstance, UPDATE_FREQUENCY);
TaskStatuses.Add(Status);
foreach (var Plugin in SharedLibrary.Plugins.PluginImporter.ActivePlugins)
await Plugin.OnLoadAsync(ServerInstance);
Logger.WriteVerbose($"Now monitoring {ServerInstance.Hostname}");
}
@ -114,7 +112,6 @@ namespace IW4MAdmin
Running = true;
}
public void Start()
{
while (Running)
@ -166,7 +163,7 @@ namespace IW4MAdmin
return Logger;
}
public IList<MessageToken> GetMessageTokens()
public IList<SharedLibrary.Helpers.MessageToken> GetMessageTokens()
{
return MessageTokens;
}

View File

@ -1,117 +0,0 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using SharedLibrary;
using SharedLibrary.Interfaces;
namespace IW4MAdmin
{
public class PluginImporter
{
public static List<Command> potentialCommands = new List<Command>();
public static List<IPlugin> potentialPlugins = new List<IPlugin>();
public static IPlugin webFront = null;
//private static AppDomain pluginDomain;
public static bool Load()
{
//pluginDomain = AppDomain.CreateDomain("Plugins");
string[] dllFileNames = null;
if (Directory.Exists(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins"))
dllFileNames = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins", "*.dll");
else
{
Manager.GetInstance().Logger.WriteWarning("Plugin folder does not exist!");
return false;
}
if (dllFileNames == null || dllFileNames.Length == 0)
{
Manager.GetInstance().Logger.WriteDebug("No plugins to load");
return true;
}
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
foreach (string dllFile in dllFileNames)
{
byte[] rawDLL = File.ReadAllBytes(dllFile); // because we want to update the plugin without restarting
Assembly assembly = Assembly.Load(rawDLL);
assemblies.Add(assembly);
}
int LoadedPlugins = 0;
int LoadedCommands = 0;
foreach (Assembly Plugin in assemblies)
{
if (Plugin != null)
{
Type[] types = Plugin.GetTypes();
foreach(Type assemblyType in types)
{
if (assemblyType.IsClass && assemblyType.BaseType.Name == "Command")
{
Object commandObject = Activator.CreateInstance(assemblyType);
Command newCommand = (Command)commandObject;
potentialCommands.Add(newCommand);
Manager.GetInstance().Logger.WriteDebug("Registered command \"" + newCommand.Name + "\"");
LoadedCommands++;
continue;
}
try
{
if (assemblyType.GetInterface("IPlugin", false) == null)
continue;
Object notifyObject = Activator.CreateInstance(assemblyType);
IPlugin newNotify = (IPlugin)notifyObject;
if (potentialPlugins.Find(x => x.Name == newNotify.Name) == null)
{
potentialPlugins.Add(newNotify);
newNotify.OnLoadAsync();
Manager.GetInstance().Logger.WriteDebug($"Loaded plugin \"{ newNotify.Name }\" [{newNotify.Version}]");
LoadedPlugins++;
}
}
catch (Exception E)
{
Manager.GetInstance().Logger.WriteWarning($"Could not load plugin {Plugin.Location} - {E.Message}");
}
}
}
}
Manager.GetInstance().Logger.WriteInfo($"Loaded {LoadedPlugins} plugins and registered {LoadedCommands} commands.");
return true;
}
/*
public static void Unload()
{
foreach (IPlugin P in potentialPlugins)
{
try
{
P.onUnload();
}
catch (Exception E)
{
Manager.GetInstance().mainLog.Write("There was an error unloading \"" + P.Name + "\" plugin", Log.Level.Debug);
Manager.GetInstance().mainLog.Write("Error Message: " + E.Message, Log.Level.Debug);
Manager.GetInstance().mainLog.Write("Error Trace: " + E.StackTrace, Log.Level.Debug);
continue;
}
}
potentialCommands = new List<Command>();
potentialPlugins = new List<IPlugin>();
AppDomain.Unload(pluginDomain);
}*/
}
}

View File

@ -313,7 +313,7 @@ namespace IW4MAdmin
{
await ProcessEvent(E);
foreach (SharedLibrary.Interfaces.IPlugin P in PluginImporter.potentialPlugins)
foreach (SharedLibrary.Interfaces.IPlugin P in SharedLibrary.Plugins.PluginImporter.ActivePlugins)
{
try
{
@ -386,7 +386,7 @@ namespace IW4MAdmin
if ((DateTime.Now - tickTime).TotalMilliseconds >= 1000)
{
// We don't want to await here, just in case user plugins are really slow :c
foreach (var Plugin in PluginImporter.potentialPlugins)
foreach (var Plugin in SharedLibrary.Plugins.PluginImporter.ActivePlugins)
#if !DEBUG
Plugin.OnTickAsync(this);
#else
@ -400,7 +400,7 @@ namespace IW4MAdmin
{
while (PlayerHistory.Count > 144) // 12 times a minute for 12 hours
PlayerHistory.Dequeue();
PlayerHistory.Enqueue(new PlayerHistory(lastCount, ClientNum));
PlayerHistory.Enqueue(new SharedLibrary.Helpers.PlayerHistory(lastCount, ClientNum));
playerCountStart = DateTime.Now;
}
@ -786,16 +786,8 @@ namespace IW4MAdmin
override public void InitializeTokens()
{
Manager.GetMessageTokens().Add(new MessageToken("TOTALPLAYERS", Manager.GetClientDatabase().TotalPlayers().ToString));
Manager.GetMessageTokens().Add(new MessageToken("VERSION", Program.Version.ToString));
}
override public void InitializeCommands()
{
foreach (Command C in PluginImporter.potentialCommands)
Manager.GetCommands().Add(C);
Manager.GetCommands().Add(new Plugins("plugins", "view all loaded plugins. syntax: !plugins", "p", Player.Permission.Administrator, 0, false));
Manager.GetMessageTokens().Add(new SharedLibrary.Helpers.MessageToken("TOTALPLAYERS", Manager.GetClientDatabase().TotalPlayers().ToString));
Manager.GetMessageTokens().Add(new SharedLibrary.Helpers.MessageToken("VERSION", Program.Version.ToString));
}
}
}

View File

@ -202,7 +202,7 @@ namespace IW4MAdmin
serverPort = S.GetPort(),
maxPlayers = S.MaxClients,
mapName = S.CurrentMap.Alias,
gameType = Utilities.gametypeLocalized(S.Gametype),
gameType = Utilities.GetLocalizedGametype(S.Gametype),
currentPlayers = S.GetPlayersAsList().Count,
chatHistory = S.ChatHistory,
players = new List<PlayerInfo>()
@ -406,7 +406,7 @@ namespace IW4MAdmin
adminName = admin.Name,
adminLevel = admin.Level.ToString(),
penaltyReason = p.Reason,
penaltyTime = SharedLibrary.Utilities.timePassed(p.When),
penaltyTime = SharedLibrary.Utilities.GetTimePassed(p.When),
penaltyType = p.BType.ToString(),
playerName = penalized.Name,
playerID = penalized.DatabaseID
@ -683,7 +683,7 @@ namespace IW4MAdmin
}
eachPlayer.playerConnections = pp.Connections;
eachPlayer.lastSeen = Utilities.timePassed(pp.LastConnection);
eachPlayer.lastSeen = Utilities.GetTimePassed(pp.LastConnection);
pInfo.Add(eachPlayer);
}

Binary file not shown.

View File

@ -1,9 +1,9 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26403.7
VisualStudioVersion = 15.0.26430.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IW4M ADMIN", "Admin\IW4M ADMIN.csproj", "{DD5DCDA2-51DB-4B1A-922F-5705546E6115}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Application", "Admin\Application.csproj", "{DD5DCDA2-51DB-4B1A-922F-5705546E6115}"
ProjectSection(ProjectDependencies) = postProject
{AF097E6B-48D5-4452-9CCF-0A81A21F341D} = {AF097E6B-48D5-4452-9CCF-0A81A21F341D}
{4785AB75-66F3-4391-985D-63A5A049A0FA} = {4785AB75-66F3-4391-985D-63A5A049A0FA}
@ -13,19 +13,19 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IW4M ADMIN", "Admin\IW4M AD
{D51EECEB-438A-47DA-870F-7D7B41BC24D6} = {D51EECEB-438A-47DA-870F-7D7B41BC24D6}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Stats Plugin", "Plugins\SimpleStats\Stats Plugin.csproj", "{4785AB75-66F3-4391-985D-63A5A049A0FA}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatsPlugin", "Plugins\SimpleStats\StatsPlugin.csproj", "{4785AB75-66F3-4391-985D-63A5A049A0FA}"
ProjectSection(ProjectDependencies) = postProject
{D51EECEB-438A-47DA-870F-7D7B41BC24D6} = {D51EECEB-438A-47DA-870F-7D7B41BC24D6}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SharedLibrary", "SharedLibrary\SharedLibrary.csproj", "{D51EECEB-438A-47DA-870F-7D7B41BC24D6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Welcome Plugin", "Plugins\Welcome\Welcome Plugin.csproj", "{AF097E6B-48D5-4452-9CCF-0A81A21F341D}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WelcomePlugin", "Plugins\Welcome\WelcomePlugin.csproj", "{AF097E6B-48D5-4452-9CCF-0A81A21F341D}"
ProjectSection(ProjectDependencies) = postProject
{D51EECEB-438A-47DA-870F-7D7B41BC24D6} = {D51EECEB-438A-47DA-870F-7D7B41BC24D6}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Votemap Plugin", "Plugins\VoteMap\Votemap Plugin.csproj", "{428D8EB9-ECA3-4A66-AA59-3A944378C33F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VotemapPlugin", "Plugins\VoteMap\VotemapPlugin.csproj", "{428D8EB9-ECA3-4A66-AA59-3A944378C33F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MessageboardPlugin", "Plugins\MessageBoard\MessageboardPlugin.csproj", "{E46C85BD-A99C-484E-BCCE-0F1831C5925E}"
EndProject

View File

@ -35,7 +35,7 @@ namespace EventAPI
{
StringBuilder s = new StringBuilder();
foreach (var S in Events.ActiveServers)
s.Append(String.Format("{0} has {1}/{4} players playing {2} on {3}\n", S.Hostname, S.GetPlayersAsList().Count, Utilities.gametypeLocalized(S.Gametype), S.CurrentMap.Name, S.MaxClients));
s.Append(String.Format("{0} has {1}/{4} players playing {2} on {3}\n", S.Hostname, S.GetPlayersAsList().Count, Utilities.GetLocalizedGametype(S.Gametype), S.CurrentMap.Name, S.MaxClients));
requestedEvent.Event = new RestEvent(RestEvent.EventType.STATUS, RestEvent.EventVersion.IW4MAdmin, s.ToString(), "Status", "", "");
requestedEvent.eventCount = 1;
}
@ -95,7 +95,7 @@ namespace EventAPI
}
}
public async Task OnLoadAsync()
public async Task OnLoadAsync(Server S)
{
APIEvents = new Queue<RestEvent>();
flaggedMessagesText = new List<string>();
@ -103,7 +103,7 @@ namespace EventAPI
WebService.PageList.Add(new EventsJSON());
}
public async Task OnUnloadAsync()
public async Task OnUnloadAsync(Server S)
{
APIEvents.Clear();
ActiveServers.Clear();

View File

@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SharedLibrary;
using SharedLibrary.Interfaces;
using SharedLibrary.Network;
using SharedLibrary.Helpers;
namespace Plugin
{
@ -21,9 +19,11 @@ namespace Plugin
public override async Task ExecuteAsync(Event E)
{
FastRestartPlugin.Config = new FastRestartConfig() { Enabled = true };
Serialize<FastRestartConfig>.Write($"config/fastrestartconfig_{E.Owner}.cfg", FastRestartPlugin.Config);
await E.Origin.Tell("Fast restarting is now enabled for this server");
var Config = new FastRestartConfig() { Enabled = true };
if (!new Configuration<FastRestartConfig>(E.Owner).Write(Config))
await E.Origin.Tell("Failed to save the configuration file for fast restart");
else
await E.Origin.Tell("Fast restarting is now enabled for this server");
}
}
@ -33,9 +33,11 @@ namespace Plugin
public override async Task ExecuteAsync(Event E)
{
FastRestartPlugin.Config = new FastRestartConfig() { Enabled = false };
Serialize<FastRestartConfig>.Write($"config/fastrestartconfig_{E.Owner}.cfg", FastRestartPlugin.Config);
await E.Origin.Tell("Fast restarting is now disabled for this server");
var Config = new FastRestartConfig() { Enabled = false };
if (!new Configuration<FastRestartConfig>(E.Owner).Write(Config))
await E.Origin.Tell("Failed to save the configuration file for fast restart");
else
await E.Origin.Tell("Fast restarting is now disabled for this server");
}
}
@ -43,7 +45,6 @@ namespace Plugin
{
bool MatchEnded;
DateTime MatchEndTime;
public static FastRestartConfig Config;
public string Name { get { return "Fast Restarter"; } }
@ -58,30 +59,24 @@ namespace Plugin
try
{
await S.GetDvarAsync<int>("scr_intermission_time");
Config = Serialize<FastRestartConfig>.Read($"config/fastrestartconfig_{E.Owner}.cfg");
}
catch (SharedLibrary.Exceptions.DvarException)
{
await S.ExecuteCommandAsync("set scr_intermission_time 20");
}
catch (SharedLibrary.Exceptions.SerializeException)
{
Config = new FastRestartConfig() { Enabled = false };
Serialize<FastRestartConfig>.Write($"config/fastrestartconfig_{E.Owner}.cfg", Config);
await S.SetDvarAsync("scr_intermission_time", 20);
}
}
}
public Task OnLoadAsync()
public async Task OnLoadAsync(Server S)
{
return null;
// this initializes the file if it doesn't exist already
new Configuration<FastRestartConfig>(S).Read();
}
public async Task OnTickAsync(Server S)
{
if (!Config.Enabled)
if (!new Configuration<FastRestartConfig>(S).Read().Enabled)
return;
MatchEnded = (await S.GetDvarAsync<int>("scr_gameended")).Value == 1;
@ -97,7 +92,7 @@ namespace Plugin
}
}
public Task OnUnloadAsync()
public Task OnUnloadAsync(Server S)
{
return null;
}

View File

@ -24,7 +24,7 @@ namespace MessageBoard
public Post(int id, int threadid, bool visible, string title, string content, User author, DateTime creationDate, DateTime updatedDate) : base(id, title, visible, content, 0, author, null, creationDate, updatedDate)
{
this.lastModificationString = SharedLibrary.Utilities.timePassed(creationDate);
this.lastModificationString = SharedLibrary.Utilities.GetTimePassed(creationDate);
this.threadid = threadid;
}
@ -118,7 +118,7 @@ namespace MessageBoard
this.threadCategory = threadCategory;
this.creationDate = creationDate;
this.updatedDate = updatedDate;
this.lastModificationString = SharedLibrary.Utilities.timePassed(updatedDate);
this.lastModificationString = SharedLibrary.Utilities.GetTimePassed(updatedDate);
this.visible = visible;
}

View File

@ -77,7 +77,7 @@ namespace MessageBoard
this.avatarURL = avatarURL;
this.posts = posts;
this.lastLoginString = SharedLibrary.Utilities.timePassed(lastLogin);
this.lastLoginString = SharedLibrary.Utilities.GetTimePassed(lastLogin);
}
public int getID()

View File

@ -133,12 +133,12 @@ namespace StatsPlugin
get { return "RaidMax"; }
}
public async Task OnLoadAsync()
public async Task OnLoadAsync(Server S)
{
statLists = new List<StatTracking>();
}
public async Task OnUnloadAsync()
public async Task OnUnloadAsync(Server S)
{
statLists.Clear();
}
@ -155,8 +155,8 @@ namespace StatsPlugin
statLists.Add(new StatTracking(S.GetPort()));
if (statLists.Count == 1)
{
S.Manager.GetMessageTokens().Add(new MessageToken("TOTALKILLS", GetTotalKills));
S.Manager.GetMessageTokens().Add(new MessageToken("TOTALPLAYTIME", GetTotalPlaytime));
S.Manager.GetMessageTokens().Add(new SharedLibrary.Helpers.MessageToken("TOTALKILLS", GetTotalKills));
S.Manager.GetMessageTokens().Add(new SharedLibrary.Helpers.MessageToken("TOTALPLAYTIME", GetTotalPlaytime));
}
}

View File

@ -205,12 +205,12 @@ namespace Votemap_Plugin
}
}
public async Task OnLoadAsync()
public async Task OnLoadAsync(Server S)
{
serverVotingList = new List<ServerVoting>();
}
public async Task OnUnloadAsync()
public async Task OnUnloadAsync(Server S)
{
serverVotingList.Clear();
}

View File

@ -13,6 +13,53 @@ namespace Welcome_Plugin
Dictionary<int, float> PlayerPings;
int PingAverageCount;
String TimesConnected(Player P)
{
int connection = P.Connections;
String Prefix = String.Empty;
if (connection % 10 > 3 || connection % 10 == 0 || (connection % 100 > 9 && connection % 100 < 19))
Prefix = "th";
else
{
switch (connection % 10)
{
case 1:
Prefix = "st";
break;
case 2:
Prefix = "nd";
break;
case 3:
Prefix = "rd";
break;
}
}
switch (connection)
{
case 0:
case 1:
return "first";
case 2:
return "second";
case 3:
return "third";
case 4:
return "fourth";
case 5:
return "fifth";
case 100:
return "One-Hundreth (amazing!)";
case 500:
return "you're really ^5dedicated ^7to this server! This is your ^5500th^7";
case 1000:
return "you deserve a medal. it's your ^11000th^7";
default:
return connection.ToString() + Prefix;
}
}
public string Author
{
get
@ -37,13 +84,13 @@ namespace Welcome_Plugin
}
}
public async Task OnLoadAsync()
public async Task OnLoadAsync(Server S)
{
PlayerPings = new Dictionary<int, float>();
PingAverageCount = 1;
}
public async Task OnUnloadAsync()
public async Task OnUnloadAsync(Server S)
{
PlayerPings.Clear();
PlayerPings = null;
@ -75,9 +122,9 @@ namespace Welcome_Plugin
Player newPlayer = E.Origin;
if (newPlayer.Level >= Player.Permission.Trusted && !E.Origin.Masked)
await E.Owner.Broadcast(Utilities.levelToColor(newPlayer.Level) + " ^5" + newPlayer.Name + " ^7has joined the server.");
await E.Owner.Broadcast(Utilities.ConvertLevelToColor(newPlayer.Level) + " ^5" + newPlayer.Name + " ^7has joined the server.");
await newPlayer.Tell($"Welcome ^5{newPlayer.Name}^7, this your ^5{newPlayer.TimesConnected()} ^7time connecting!");
await newPlayer.Tell($"Welcome ^5{newPlayer.Name}^7, this your ^5{TimesConnected(newPlayer)} ^7time connecting!");
if (newPlayer.Level == Player.Permission.Flagged)
await E.Owner.ToAdmins($"^1NOTICE: ^7Flagged player ^5{newPlayer.Name}^7 has joined!");

View File

@ -18,8 +18,7 @@ namespace SharedLibrary.Commands
}
class COwner : Command
{
{
public COwner(String N, String D, String U, Player.Permission P, int args, bool nT) : base(N, D, U, P, args, nT) { }
public override async Task ExecuteAsync(Event E)
@ -145,7 +144,7 @@ namespace SharedLibrary.Commands
public override async Task ExecuteAsync(Event E)
{
String You = String.Format("{0} [^3#{1}^7] {2} [^3@{3}^7] [{4}^7] IP: {5}", E.Origin.Name, E.Origin.ClientID, E.Origin.NetworkID, E.Origin.DatabaseID, SharedLibrary.Utilities.levelToColor(E.Origin.Level), E.Origin.IP);
String You = String.Format("{0} [^3#{1}^7] {2} [^3@{3}^7] [{4}^7] IP: {5}", E.Origin.Name, E.Origin.ClientID, E.Origin.NetworkID, E.Origin.DatabaseID, SharedLibrary.Utilities.ConvertLevelToColor(E.Origin.Level), E.Origin.IP);
await E.Origin.Tell(You);
}
}
@ -166,9 +165,9 @@ namespace SharedLibrary.Commands
continue;
if (P.Masked)
playerList.AppendFormat("[^3{0}^7]{3}[^3{1}^7] {2}", Utilities.levelToColor(Player.Permission.User), P.ClientID, P.Name, SharedLibrary.Utilities.getSpaces(Player.Permission.SeniorAdmin.ToString().Length - Player.Permission.User.ToString().Length));
playerList.AppendFormat("[^3{0}^7]{3}[^3{1}^7] {2}", Utilities.ConvertLevelToColor(Player.Permission.User), P.ClientID, P.Name, SharedLibrary.Utilities.GetSpaces(Player.Permission.SeniorAdmin.ToString().Length - Player.Permission.User.ToString().Length));
else
playerList.AppendFormat("[^3{0}^7]{3}[^3{1}^7] {2}", Utilities.levelToColor(P.Level), P.ClientID, P.Name, SharedLibrary.Utilities.getSpaces(Player.Permission.SeniorAdmin.ToString().Length - P.Level.ToString().Length));
playerList.AppendFormat("[^3{0}^7]{3}[^3{1}^7] {2}", Utilities.ConvertLevelToColor(P.Level), P.ClientID, P.Name, SharedLibrary.Utilities.GetSpaces(Player.Permission.SeniorAdmin.ToString().Length - P.Level.ToString().Length));
if (count == 2 || E.Owner.GetPlayersAsList().Count == 1)
{
@ -278,7 +277,7 @@ namespace SharedLibrary.Commands
return;
}
Player.Permission newPerm = Utilities.matchPermission(Utilities.RemoveWords(E.Data, 1));
Player.Permission newPerm = Utilities.MatchPermission(Utilities.RemoveWords(E.Data, 1));
if (newPerm == Player.Permission.Owner && E.Origin.Level != Player.Permission.Console)
newPerm = Player.Permission.Banned;
@ -333,9 +332,9 @@ namespace SharedLibrary.Commands
if (P != null && P.Level > Player.Permission.Flagged && !P.Masked)
{
if (E.Message[0] == '@')
await E.Owner.Broadcast(String.Format("[^3{0}^7] {1}", Utilities.levelToColor(P.Level), P.Name));
await E.Owner.Broadcast(String.Format("[^3{0}^7] {1}", Utilities.ConvertLevelToColor(P.Level), P.Name));
else
await E.Origin.Tell(String.Format("[^3{0}^7] {1}", Utilities.levelToColor(P.Level), P.Name));
await E.Origin.Tell(String.Format("[^3{0}^7] {1}", Utilities.ConvertLevelToColor(P.Level), P.Name));
}
}
}
@ -381,7 +380,7 @@ namespace SharedLibrary.Commands
foreach (Player P in db_players)
{
String mesg = String.Format("[^3{0}^7] [^3@{1}^7] - [{2}^7] - {3} | last seen {4} ago", P.Name, P.DatabaseID, SharedLibrary.Utilities.levelToColor(P.Level), P.IP, P.GetLastConnection());
String mesg = String.Format("[^3{0}^7] [^3@{1}^7] - [{2}^7] - {3} | last seen {4} ago", P.Name, P.DatabaseID, SharedLibrary.Utilities.ConvertLevelToColor(P.Level), P.IP, P.GetLastConnection());
await E.Origin.Tell(mesg);
}
}
@ -679,5 +678,19 @@ namespace SharedLibrary.Commands
await E.Origin.Tell("Successfully sent RCON command!");
}
}
class CPlugins : Command
{
public CPlugins(String N, String D, String U, Player.Permission P, int args, bool nT) : base(N, D, U, P, args, nT) { }
public override async Task ExecuteAsync(Event E)
{
await E.Origin.Tell("^5Loaded Plugins:");
foreach (var P in Plugins.PluginImporter.ActivePlugins)
{
await E.Origin.Tell(String.Format("^3{0} ^7[v^3{1}^7] by ^5{2}^7", P.Name, P.Version, P.Author));
}
}
}
}

View File

@ -509,7 +509,7 @@ namespace SharedLibrary
{
Dictionary<String, object> newPlayer = new Dictionary<String, object>
{
{ "Name", Utilities.removeNastyChars(P.Name) },
{ "Name", Utilities.StripIllegalCharacters(P.Name) },
{ "npID", P.NetworkID },
{ "Level", (int)P.Level },
{ "LastOffense", "" },
@ -546,7 +546,7 @@ namespace SharedLibrary
{
Dictionary<String, object> newBan = new Dictionary<String, object>
{
{ "Reason", Utilities.removeNastyChars(B.Reason) },
{ "Reason", Utilities.StripIllegalCharacters(B.Reason) },
{ "npID", B.OffenderID },
{ "bannedByID", B.PenaltyOriginID },
{ "IP", B.IP },
@ -656,7 +656,7 @@ namespace SharedLibrary
Dictionary<String, object> newPlayer = new Dictionary<String, object>
{
{ "Number", Alias.Number },
{ "NAMES", Utilities.removeNastyChars(String.Join(";", Alias.Names)) },
{ "NAMES", Utilities.StripIllegalCharacters(String.Join(";", Alias.Names)) },
{ "IPS", String.Join(";", Alias.IPS) }
};
Insert("ALIASES", newPlayer);

View File

@ -119,7 +119,7 @@ namespace SharedLibrary
{
Regex rgx = new Regex("[^a-zA-Z0-9 -! -_]");
string message = rgx.Replace(line[4], "");
return new Event(GType.Say, Utilities.removeNastyChars(message).StripColors(), SV.ParseClientFromString(line, 2), null, SV) { Message = Utilities.removeNastyChars(message).StripColors() };
return new Event(GType.Say, Utilities.StripIllegalCharacters(message).StripColors(), SV.ParseClientFromString(line, 2), null, SV) { Message = Utilities.StripIllegalCharacters(message).StripColors() };
}
if (eventType == ":")

View File

@ -5,7 +5,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace SharedLibrary
namespace SharedLibrary.Helpers
{
public sealed class AsyncStatus
{

View File

@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SharedLibrary.Helpers
{
public class Configuration<T> : Interfaces.Serialize<T>
{
string FilePostfix;
public Configuration(Server S)
{
FilePostfix = $"_{S.GetIP()}_{S.GetPort()}.cfg";
}
public T Read()
{
try
{
return Read();
}
catch (Exceptions.SerializeException)
{
return default(T);
}
}
public bool Write(T Data)
{
try
{
Write(Filename(), Data);
return true;
}
catch(Exceptions.SerializeException)
{
return false;
}
}
public override string Filename()
{
return $"config/{typeof(T).ToString()}_{FilePostfix}";
}
}
}

View File

@ -1,6 +1,6 @@
using System;
namespace SharedLibrary
namespace SharedLibrary.Helpers
{
public class MessageToken
{

View File

@ -1,6 +1,6 @@
using System;
namespace SharedLibrary
namespace SharedLibrary.Helpers
{
public class PlayerHistory
{

View File

@ -13,7 +13,7 @@ namespace SharedLibrary.Interfaces
IPenaltyList GetClientPenalties();
ClientsDB GetClientDatabase();
AliasesDB GetAliasesDatabase();
IList<MessageToken> GetMessageTokens();
IList<Helpers.MessageToken> GetMessageTokens();
IList<Player> GetActiveClients();
}
}

View File

@ -5,8 +5,8 @@ namespace SharedLibrary.Interfaces
{
public interface IPlugin
{
Task OnLoadAsync();
Task OnUnloadAsync();
Task OnLoadAsync(Server S);
Task OnUnloadAsync(Server S);
Task OnEventAsync(Event E, Server S);
Task OnTickAsync(Server S);

View File

@ -1,4 +1,5 @@
using System;
using SharedLibrary;
namespace SharedLibrary
{
@ -6,7 +7,7 @@ namespace SharedLibrary
{
public Penalty(Type BType, String Reas, String TargID, String From, DateTime time, String ip)
{
Reason = Reas.Replace("!","");
Reason = Reas.CleanChars().StripColors();
OffenderID = TargID;
PenaltyOriginID = From;
When = time;
@ -36,4 +37,18 @@ namespace SharedLibrary
public String IP { get; private set; }
public Type BType { get; private set; }
}
public class Report
{
public Report(Player T, Player O, String R)
{
Target = T;
Origin = O;
Reason = R;
}
public Player Target { get; private set; }
public Player Origin { get; private set; }
public String Reason { get; private set; }
}
}

View File

@ -124,7 +124,7 @@ namespace SharedLibrary
public String GetLastConnection()
{
return Utilities.timePassed(LastConnection);
return Utilities.GetTimePassed(LastConnection);
}
public void UpdateName(String n)

View File

@ -0,0 +1,78 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using SharedLibrary.Interfaces;
namespace SharedLibrary.Plugins
{
public class PluginImporter
{
public static List<Command> ActiveCommands = new List<Command>();
public static List<IPlugin> ActivePlugins = new List<IPlugin>();
public static bool Load(IManager Manager)
{
string[] dllFileNames = Directory.GetFiles(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\plugins", "*.dll");
if (dllFileNames.Length == 0)
{
Manager.GetLogger().WriteDebug("No plugins found to load");
return true;
}
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
foreach (string dllFile in dllFileNames)
{
byte[] rawDLL = File.ReadAllBytes(dllFile);
Assembly assembly = Assembly.Load(rawDLL);
assemblies.Add(assembly);
}
int LoadedPlugins = 0;
int LoadedCommands = 0;
foreach (Assembly Plugin in assemblies)
{
if (Plugin != null)
{
Type[] types = Plugin.GetTypes();
foreach (Type assemblyType in types)
{
if (assemblyType.IsClass && assemblyType.BaseType.Name == "Command")
{
Object commandObject = Activator.CreateInstance(assemblyType);
Command newCommand = (Command)commandObject;
ActiveCommands.Add(newCommand);
Manager.GetLogger().WriteDebug("Registered command \"" + newCommand.Name + "\"");
LoadedCommands++;
continue;
}
try
{
if (assemblyType.GetInterface("IPlugin", false) == null)
continue;
Object notifyObject = Activator.CreateInstance(assemblyType);
IPlugin newNotify = (IPlugin)notifyObject;
if (ActivePlugins.Find(x => x.Name == newNotify.Name) == null)
{
ActivePlugins.Add(newNotify);
Manager.GetLogger().WriteDebug($"Loaded plugin \"{ newNotify.Name }\" [{newNotify.Version}]");
LoadedPlugins++;
}
}
catch (Exception E)
{
Manager.GetLogger().WriteWarning($"Could not load plugin {Plugin.Location} - {E.Message}");
}
}
}
}
Manager.GetLogger().WriteInfo($"Loaded {LoadedPlugins} plugins and registered {LoadedCommands} commands.");
return true;
}
}
}

View File

@ -114,7 +114,7 @@ namespace SharedLibrary.Network
public static async Task SetDvarAsync(this Server server, string dvarName, object dvarValue)
{
await Task.FromResult(SendQuery(QueryType.DVAR, server, $"{dvarName} {dvarValue}"));
await Task.FromResult(SendQuery(QueryType.DVAR, server, $"set {dvarName} {dvarValue}"));
}
public static async Task<string[]> ExecuteCommandAsync(this Server server, string commandName)
@ -139,7 +139,6 @@ namespace SharedLibrary.Network
fixedRequest[i / 2] = initialRequestBytes[i];
return fixedRequest;
}
}
}

View File

@ -1,18 +0,0 @@
using System;
namespace SharedLibrary
{
public class Report
{
public Report(Player T, Player O, String R)
{
Target = T;
Origin = O;
Reason = R;
}
public Player Target { get; private set; }
public Player Origin { get; private set; }
public String Reason { get; private set; }
}
}

View File

@ -26,7 +26,7 @@ namespace SharedLibrary
Players = new List<Player>(new Player[18]);
events = new Queue<Event>();
Reports = new List<Report>();
PlayerHistory = new Queue<PlayerHistory>();
PlayerHistory = new Queue<SharedLibrary.Helpers.PlayerHistory>();
ChatHistory = new List<Chat>();
lastWebChat = DateTime.Now;
nextMessage = 0;
@ -71,6 +71,7 @@ namespace SharedLibrary
commands.Add(new CListAlias("alias", "get past aliases and ips of a player. syntax: !alias <player>", "known", Player.Permission.Moderator, 1, true));
commands.Add(new CExecuteRCON("rcon", "send rcon command to server. syntax: !rcon <command>", "rcon", Player.Permission.Owner, 1, false));
commands.Add(new CFindAllPlayers("findall", "find a player by their aliase(s). syntax: !findall <player>", "fa", Player.Permission.Moderator, 1, false));
commands.Add(new CPlugins("plugins", "view all loaded plugins. syntax: !plugins", "p", Player.Permission.Administrator, 0, false));
}
//Returns current server IP set by `net_ip` -- *STRING*
@ -375,7 +376,11 @@ namespace SharedLibrary
/// <summary>
/// Load up the built in commands
/// </summary>
abstract public void InitializeCommands();
public void InitializeCommands()
{
foreach (Command C in Plugins.PluginImporter.ActiveCommands)
Manager.GetCommands().Add(C);
}
//Objects
public Interfaces.IManager Manager { get; protected set; }
@ -389,7 +394,7 @@ namespace SharedLibrary
public int totalKills = 0;
public List<Report> Reports;
public List<Chat> ChatHistory;
public Queue<PlayerHistory> PlayerHistory { get; private set; }
public Queue<Helpers.PlayerHistory> PlayerHistory { get; private set; }
protected int ConnectionErrors;
protected DateTime LastPoll;

View File

@ -66,18 +66,19 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncStatus.cs" />
<Compile Include="Helpers\AsyncStatus.cs" />
<Compile Include="Commands\NativeCommands.cs" />
<Compile Include="Exceptions\CommandException.cs" />
<Compile Include="Exceptions\DvarException.cs" />
<Compile Include="Exceptions\NetworkException.cs" />
<Compile Include="Exceptions\SerializationException.cs" />
<Compile Include="Exceptions\ServerException.cs" />
<Compile Include="Helpers\Configuration.cs" />
<Compile Include="Interfaces\ILogger.cs" />
<Compile Include="Interfaces\IManager.cs" />
<Compile Include="Interfaces\IPenaltyList.cs" />
<Compile Include="Interfaces\ISerializable.cs" />
<Compile Include="MessageToken.cs" />
<Compile Include="Helpers\MessageToken.cs" />
<Compile Include="Penalty.cs" />
<Compile Include="Command.cs" />
<Compile Include="Database.cs" />
@ -85,12 +86,12 @@
<Compile Include="File.cs" />
<Compile Include="Dvar.cs" />
<Compile Include="Map.cs" />
<Compile Include="Miscellaneous.cs" />
<Compile Include="Helpers\PlayerHistory.cs" />
<Compile Include="Player.cs" />
<Compile Include="Interfaces\IPlugin.cs" />
<Compile Include="PluginImporter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RCON.cs" />
<Compile Include="Report.cs" />
<Compile Include="Server.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="WebService.cs" />

View File

@ -10,7 +10,7 @@ namespace SharedLibrary
public static class Utilities
{
//Get string with specified number of spaces -- really only for visual output
public static String getSpaces(int Num)
public static String GetSpaces(int Num)
{
String SpaceString = String.Empty;
while (Num > 0)
@ -22,12 +22,6 @@ namespace SharedLibrary
return SpaceString;
}
//Sleep for x amount of seconds
public static void Wait(double time)
{
Thread.Sleep((int)Math.Ceiling(time * 1000));
}
//Remove words from a space delimited string
public static String RemoveWords(this string str, int num)
{
@ -74,14 +68,11 @@ namespace SharedLibrary
return StatusPlayers;
}
public static Player.Permission matchPermission(String str)
public static Player.Permission MatchPermission(String str)
{
String lookingFor = str.ToLower();
#if REPZ_BUILD
for (Player.Permission Perm = Player.Permission.User; Perm <= Player.Permission.Owner; Perm++)
#else
for (Player.Permission Perm = Player.Permission.User; Perm < Player.Permission.Owner; Perm++)
#endif
{
if (lookingFor.Contains(Perm.ToString().ToLower()))
return Perm;
@ -90,12 +81,10 @@ namespace SharedLibrary
return Player.Permission.Banned;
}
public static String removeNastyChars(String str)
public static String StripIllegalCharacters(String str)
{
if (str != null)
{
return str.Replace("`", "").Replace("\\", "").Replace("\"", "").Replace("&quot;", "").Replace("&amp;", "&").Replace("\"", "''").Replace("'", "").Replace("?", "");
}
else
return String.Empty;
@ -130,7 +119,7 @@ namespace SharedLibrary
/// </summary>
/// <param name="level">Specified player level</param>
/// <returns></returns>
public static String levelToColor(Player.Permission level)
public static String ConvertLevelToColor(Player.Permission level)
{
switch (level)
{
@ -149,7 +138,7 @@ namespace SharedLibrary
}
}
public static String ProcessMessageToken(IList<MessageToken> tokens, String str)
public static String ProcessMessageToken(IList<Helpers.MessageToken> tokens, String str)
{
MatchCollection RegexMatches = Regex.Matches(str, @"\{\{[A-Z]+\}\}", RegexOptions.IgnoreCase);
foreach (Match M in RegexMatches)
@ -176,7 +165,7 @@ namespace SharedLibrary
/// </summary>
/// <param name="input">Shorthand gametype reported from server</param>
/// <returns></returns>
public static String gametypeLocalized(String input)
public static String GetLocalizedGametype(String input)
{
switch (input)
{
@ -232,7 +221,7 @@ namespace SharedLibrary
return datetime.ToString("yyyy-MM-dd H:mm:ss");
}
public static String timePassed(DateTime start)
public static String GetTimePassed(DateTime start)
{
TimeSpan Elapsed = DateTime.Now - start;
@ -259,52 +248,5 @@ namespace SharedLibrary
else
return "a very long time";
}
public static String TimesConnected(this Player P)
{
int connection = P.Connections;
String Prefix = String.Empty;
if (connection % 10 > 3 || connection % 10 == 0 || (connection % 100 > 9 && connection % 100 < 19))
Prefix = "th";
else
{
switch (connection % 10)
{
case 1:
Prefix = "st";
break;
case 2:
Prefix = "nd";
break;
case 3:
Prefix = "rd";
break;
}
}
switch (connection)
{
case 0:
case 1:
return "first";
case 2:
return "second";
case 3:
return "third";
case 4:
return "fourth";
case 5:
return "fifth";
case 100:
return "One-Hundreth (amazing!)";
case 500:
return "you're really ^5dedicated ^7to this server! This is your ^5500th^7";
case 1000:
return "you deserve a medal. it's your ^11000th^7";
default:
return connection.ToString() + Prefix;
}
}
}
}