Less usage of Convert class

This commit is contained in:
UnknownShadow200 2016-09-08 17:15:09 +10:00
parent 3f36bca185
commit de79a0e3e4
3 changed files with 43 additions and 89 deletions

View File

@ -42,7 +42,6 @@ namespace MCGalaxy {
public static void Load(string givenPath, bool skipSalt = false) { public static void Load(string givenPath, bool skipSalt = false) {
if (!skipSalt) GenerateSalt(); if (!skipSalt) GenerateSalt();
Server.s.Log(Server.salt);
oldPerms = new OldPerms(); oldPerms = new OldPerms();
if (PropertiesFile.Read(givenPath, ref oldPerms, LineProcessor)) if (PropertiesFile.Read(givenPath, ref oldPerms, LineProcessor))
Server.s.SettingsUpdate(); Server.s.SettingsUpdate();

View File

@ -186,7 +186,7 @@ namespace MCGalaxy {
List<Command> commands = new List<Command>(); List<Command> commands = new List<Command>();
foreach (Type t in lib.GetTypes()) { foreach (Type t in lib.GetTypes()) {
if (t.IsAbstract || t.IsInterface || !t.IsSubclassOf(typeof(Command)))continue; if (t.IsAbstract || t.IsInterface || !t.IsSubclassOf(typeof(Command))) continue;
object instance = Activator.CreateInstance(t); object instance = Activator.CreateInstance(t);
if (instance == null) { if (instance == null) {

View File

@ -21,7 +21,7 @@
*/ */
using System; using System;
using System.Collections; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Globalization; using System.Globalization;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
@ -44,7 +44,6 @@ namespace Sharkbite.Irc
private const string ActionModes = "+-"; private const string ActionModes = "+-";
private const string UserModes = "awiorOs"; private const string UserModes = "awiorOs";
private const string ChannelModes = "OohvaimnqpsrtklbeI"; private const string ChannelModes = "OohvaimnqpsrtklbeI";
private const string Space = " ";
internal static TraceSwitch IrcTrace = new TraceSwitch("IrcTraceSwitch", "Debug level for RFC2812 classes."); internal static TraceSwitch IrcTrace = new TraceSwitch("IrcTraceSwitch", "Debug level for RFC2812 classes.");
// Odd chars that IRC allows in nicknames // Odd chars that IRC allows in nicknames
@ -197,142 +196,98 @@ namespace Sharkbite.Irc
return true; return true;
} }
/// <summary> /// <summary> Convert a ModeAction into its RFC2812 character. </summary>
/// Convert a ModeAction into its RFC2812 character.
/// </summary>
/// <param name="action">The action enum.</param> /// <param name="action">The action enum.</param>
/// <returns>Either '+' or '-'.</returns> /// <returns>Either '+' or '-'.</returns>
public static char ModeActionToChar( ModeAction action ) public static char ModeActionToChar( ModeAction action ) {
{ return (char)((byte)action);
return Convert.ToChar( (byte) action, CultureInfo.InvariantCulture ) ;
} }
/// <summary> /// <summary> Converts the char received from the IRC server into its enum equivalent. </summary>
/// Converts the char received from the IRC server into
/// its enum equivalent.
/// </summary>
/// <param name="action">Either '+' or '-'.</param> /// <param name="action">Either '+' or '-'.</param>
/// <returns>An action enum.</returns> /// <returns>An action enum.</returns>
public static ModeAction CharToModeAction( char action ) public static ModeAction CharToModeAction( char action ) {
{ byte b = (byte)action;
byte b = Convert.ToByte( action, CultureInfo.InvariantCulture );
return (ModeAction) Enum.Parse( typeof( ModeAction), b.ToString( CultureInfo.InvariantCulture), false ); return (ModeAction) Enum.Parse( typeof( ModeAction), b.ToString( CultureInfo.InvariantCulture), false );
} }
/// <summary> /// <summary> Converts a UserMode into its RFC2812 character. </summary>
/// Converts a UserMode into its RFC2812 character.
/// </summary>
/// <param name="mode">The mode enum.</param> /// <param name="mode">The mode enum.</param>
/// <returns>The corresponding char.</returns> /// <returns>The corresponding char.</returns>
public static char UserModeToChar( UserMode mode ) public static char UserModeToChar( UserMode mode ) {
{ return (char)((byte)mode);
return Convert.ToChar( (byte) mode, CultureInfo.InvariantCulture ) ;
} }
/// <summary> /// <summary> Convert a string of UserModes characters to an array of UserMode enums. </summary>
/// Convert a string of UserModes characters to
/// an array of UserMode enums.
/// </summary>
/// <param name="modes">A string of UserMode chars from the IRC server.</param> /// <param name="modes">A string of UserMode chars from the IRC server.</param>
/// <returns>An array of UserMode enums. Charactres that are not from RFC2812 will be droppped.</returns> /// <returns>An array of UserMode enums. Charactres that are not from RFC2812 will be droppped.</returns>
public static UserMode[] UserModesToArray( string modes ) public static UserMode[] UserModesToArray( string modes ) {
{ List<UserMode> list = new List<UserMode>();
ArrayList list = new ArrayList(); for( int i = 0; i < modes.Length; i++ ) {
for( int i = 0; i < modes.Length; i++ ) if( IsValidModeChar( modes[i], UserModes ) ) {
{
if( IsValidModeChar( modes[i], UserModes ) )
{
list.Add( CharToUserMode( modes[i] )); list.Add( CharToUserMode( modes[i] ));
} }
} }
return (UserMode[]) list.ToArray( typeof(UserMode) ); return list.ToArray();
} }
/// <summary> /// <summary> Converts the char recived from the IRC server into its enum equivalent. </summary>
/// Converts the char recived from the IRC server into
/// its enum equivalent.
/// </summary>
/// <param name="mode">One of the IRC mode characters, e.g. 'a','i', etc...</param> /// <param name="mode">One of the IRC mode characters, e.g. 'a','i', etc...</param>
/// <returns>An mode enum.</returns> /// <returns>An mode enum.</returns>
public static UserMode CharToUserMode( char mode ) public static UserMode CharToUserMode( char mode ) {
{ byte b = (byte)mode;
byte b = Convert.ToByte( mode, CultureInfo.InvariantCulture );
return (UserMode) Enum.Parse( typeof( UserMode), b.ToString(CultureInfo.InvariantCulture), false ); return (UserMode) Enum.Parse( typeof( UserMode), b.ToString(CultureInfo.InvariantCulture), false );
} }
/// <summary> /// <summary> Convert a string of ChannelModes characters to an array of ChannelMode enums. </summary>
/// Convert a string of ChannelModes characters to
/// an array of ChannelMode enums.
/// </summary>
/// <param name="modes">A string of ChannelMode chars from the IRC server.</param> /// <param name="modes">A string of ChannelMode chars from the IRC server.</param>
/// <returns>An array of ChannelMode enums. Charactres that are not from RFC2812 will be droppped.</returns> /// <returns>An array of ChannelMode enums. Charactres that are not from RFC2812 will be droppped.</returns>
public static ChannelMode[] ChannelModesToArray( string modes ) public static ChannelMode[] ChannelModesToArray( string modes ) {
{ List<ChannelMode> list = new List<ChannelMode>();
ArrayList list = new ArrayList(); for( int i = 0; i < modes.Length; i++ ) {
for( int i = 0; i < modes.Length; i++ ) if( IsValidModeChar( modes[i], ChannelModes ) ) {
{
if( IsValidModeChar( modes[i], ChannelModes ) )
{
list.Add( CharToChannelMode( modes[i] )); list.Add( CharToChannelMode( modes[i] ));
} }
} }
return (ChannelMode[]) list.ToArray( typeof(ChannelMode) ); return list.ToArray();
} }
/// <summary> /// <summary> Converts a ChannelMode into its RFC2812 character. </summary>
/// Converts a ChannelMode into its RFC2812 character.
/// </summary>
/// <param name="mode">The mode enum.</param> /// <param name="mode">The mode enum.</param>
/// <returns>The corresponding char.</returns> /// <returns>The corresponding char.</returns>
public static char ChannelModeToChar( ChannelMode mode ) public static char ChannelModeToChar( ChannelMode mode ) {
{ return (char)((byte)mode);
return Convert.ToChar( (byte) mode, CultureInfo.InvariantCulture ) ;
} }
/// <summary>
/// Converts the char recived from the IRC server into /// <summary> Converts the char recived from the IRC server into its enum equivalent. </summary>
/// its enum equivalent.
/// </summary>
/// <param name="mode">One of the IRC mode characters, e.g. 'O','i', etc...</param> /// <param name="mode">One of the IRC mode characters, e.g. 'O','i', etc...</param>
/// <returns>An mode enum.</returns> /// <returns>An mode enum.</returns>
public static ChannelMode CharToChannelMode( char mode ) public static ChannelMode CharToChannelMode( char mode ) {
{ byte b = (byte)mode;
byte b = Convert.ToByte( mode, CultureInfo.InvariantCulture ); return (ChannelMode) Enum.Parse( typeof( ChannelMode ), b.ToString(CultureInfo.InvariantCulture), false );
return (ChannelMode) Enum.Parse( typeof( ChannelMode), b.ToString( CultureInfo.InvariantCulture), false );
} }
/// <summary> /// <summary> Converts a StatQuery enum value to its RFC2812 character. </summary>
/// Converts a StatQuery enum value to its RFC2812 character.
/// </summary>
/// <param name="query">The query enum.</param> /// <param name="query">The query enum.</param>
/// <returns>The corresponding char.</returns> /// <returns>The corresponding char.</returns>
public static char StatsQueryToChar( StatsQuery query ) public static char StatsQueryToChar( StatsQuery query ) {
{ return (char)((byte)query);
return Convert.ToChar( (byte) query, CultureInfo.InvariantCulture ) ;
} }
/// <summary> /// <summary> Converts the char recived from the IRC server into its enum equivalent. </summary>
/// Converts the char recived from the IRC server into
/// its enum equivalent.
/// </summary>
/// <param name="queryType">One of the IRC stats query characters, e.g. 'u','l', etc...</param> /// <param name="queryType">One of the IRC stats query characters, e.g. 'u','l', etc...</param>
/// <returns>An StatsQuery enum.</returns> /// <returns>An StatsQuery enum.</returns>
public static StatsQuery CharToStatsQuery( char queryType ) public static StatsQuery CharToStatsQuery( char queryType ) {
{ byte b = (byte)queryType;
byte b = Convert.ToByte( queryType, CultureInfo.InvariantCulture );
return (StatsQuery) Enum.Parse( typeof( StatsQuery), b.ToString(CultureInfo.InvariantCulture), false ); return (StatsQuery) Enum.Parse( typeof( StatsQuery), b.ToString(CultureInfo.InvariantCulture), false );
} }
private static bool IsValidModeChar( char c, string validList ) {
private static bool IsValidModeChar( char c, string validList )
{
return validList.IndexOf( c ) != -1; return validList.IndexOf( c ) != -1;
} }
private static bool ContainsSpace( string text ) private static bool ContainsSpace( string text ) {
{ return text.IndexOf( ' ' ) != -1;
return text.IndexOf( Space, 0, text.Length ) != -1;
} }
} }
} }