Fixes code according to modern code style. Fixes a few expression bugs.

This commit is contained in:
Kamron Batman 2018-09-15 09:58:51 -07:00
parent 89eea25e5f
commit 970fd563b2
3324 changed files with 441118 additions and 433755 deletions

View file

@ -4,48 +4,49 @@ using System.Threading;
namespace Server.Engines.MyRunUO
{
public class Config
{
// Is MyRunUO enabled?
public static bool Enabled = false;
public class Config
{
// Details required for database connection string
public const string DatabaseDriver = "{MySQL ODBC 5.2w Driver}";
public const string DatabaseServer = "localhost";
public const string DatabaseName = "MyRunUO";
public const string DatabaseUserID = "username";
// Details required for database connection string
public const string DatabaseDriver = "{MySQL ODBC 5.2w Driver}";
public const string DatabaseServer = "localhost";
public const string DatabaseName = "MyRunUO";
public const string DatabaseUserID = "username";
public const string DatabasePassword = "password";
public const string DatabasePassword = "password";
// Should the database use transactions? This is recommended
public static bool UseTransactions = true;
// Is MyRunUO enabled?
public static bool Enabled = false;
// Use optimized table loading techniques? (LOAD DATA INFILE)
public static bool LoadDataInFile = true;
// Should the database use transactions? This is recommended
public static bool UseTransactions = true;
// This must be enabled if the database server is on a remote machine.
public static bool DatabaseNonLocal = ( DatabaseServer != "localhost" );
// Use optimized table loading techniques? (LOAD DATA INFILE)
public static bool LoadDataInFile = true;
// Text encoding used
public static Encoding EncodingIO = Encoding.ASCII;
// This must be enabled if the database server is on a remote machine.
public static bool DatabaseNonLocal = DatabaseServer != "localhost";
// Database communication is done in a separate thread. This value is the 'priority' of that thread, or, how much CPU it will try to use
public static ThreadPriority DatabaseThreadPriority = ThreadPriority.BelowNormal;
// Text encoding used
public static Encoding EncodingIO = Encoding.ASCII;
// Any character with an AccessLevel equal to or higher than this will not be displayed
public static AccessLevel HiddenAccessLevel = AccessLevel.Counselor;
// Database communication is done in a separate thread. This value is the 'priority' of that thread, or, how much CPU it will try to use
public static ThreadPriority DatabaseThreadPriority = ThreadPriority.BelowNormal;
// Export character database every 30 minutes
public static TimeSpan CharacterUpdateInterval = TimeSpan.FromMinutes( 30.0 );
// Any character with an AccessLevel equal to or higher than this will not be displayed
public static AccessLevel HiddenAccessLevel = AccessLevel.Counselor;
// Export online list database every 5 minutes
public static TimeSpan StatusUpdateInterval = TimeSpan.FromMinutes( 5.0 );
// Export character database every 30 minutes
public static TimeSpan CharacterUpdateInterval = TimeSpan.FromMinutes(30.0);
public static string CompileConnectionString()
{
string connectionString =
$"DRIVER={DatabaseDriver};SERVER={DatabaseServer};DATABASE={DatabaseName};UID={DatabaseUserID};PASSWORD={DatabasePassword};";
// Export online list database every 5 minutes
public static TimeSpan StatusUpdateInterval = TimeSpan.FromMinutes(5.0);
return connectionString;
}
}
public static string CompileConnectionString()
{
string connectionString =
$"DRIVER={DatabaseDriver};SERVER={DatabaseServer};DATABASE={DatabaseName};UID={DatabaseUserID};PASSWORD={DatabasePassword};";
return connectionString;
}
}
}

View file

@ -1,206 +1,243 @@
using System;
using System.Threading;
using System.Collections;
using System.Data.Odbc;
using System.Threading;
namespace Server.Engines.MyRunUO
{
public class DatabaseCommandQueue
{
private Queue m_Queue;
private ManualResetEvent m_Sync;
private Thread m_Thread;
public class DatabaseCommandQueue
{
private string m_CompletionString;
private string m_ConnectionString;
private Queue m_Queue;
private ManualResetEvent m_Sync;
private Thread m_Thread;
private string m_CompletionString;
private string m_ConnectionString;
public DatabaseCommandQueue(string completionString, string threadName) : this(Config.CompileConnectionString(),
completionString, threadName)
{
}
public bool HasCompleted { get; private set; }
public DatabaseCommandQueue(string connectionString, string completionString, string threadName)
{
m_CompletionString = completionString;
m_ConnectionString = connectionString;
public void Enqueue( object obj )
{
lock ( m_Queue.SyncRoot )
{
m_Queue.Enqueue( obj );
try{ m_Sync.Set(); }
catch{}
}
}
m_Queue = Queue.Synchronized(new Queue());
public DatabaseCommandQueue( string completionString, string threadName ) : this( Config.CompileConnectionString(), completionString, threadName )
{
}
m_Queue.Enqueue(null); // signal connect
public DatabaseCommandQueue( string connectionString, string completionString, string threadName )
{
m_CompletionString = completionString;
m_ConnectionString = connectionString;
/*m_Queue.Enqueue( "DELETE FROM myrunuo_characters" );
m_Queue.Enqueue( "DELETE FROM myrunuo_characters_layers" );
m_Queue.Enqueue( "DELETE FROM myrunuo_characters_skills" );
m_Queue.Enqueue( "DELETE FROM myrunuo_guilds" );
m_Queue.Enqueue( "DELETE FROM myrunuo_guilds_wars" );*/
m_Queue = Queue.Synchronized( new Queue() );
m_Sync = new ManualResetEvent(true);
m_Queue.Enqueue( null ); // signal connect
m_Thread = new Thread(Thread_Start);
m_Thread.Name = threadName; //"MyRunUO Database Command Queue";
m_Thread.Priority = Config.DatabaseThreadPriority;
m_Thread.Start();
}
/*m_Queue.Enqueue( "DELETE FROM myrunuo_characters" );
m_Queue.Enqueue( "DELETE FROM myrunuo_characters_layers" );
m_Queue.Enqueue( "DELETE FROM myrunuo_characters_skills" );
m_Queue.Enqueue( "DELETE FROM myrunuo_guilds" );
m_Queue.Enqueue( "DELETE FROM myrunuo_guilds_wars" );*/
public bool HasCompleted{ get; private set; }
m_Sync = new ManualResetEvent( true );
public void Enqueue(object obj)
{
lock (m_Queue.SyncRoot)
{
m_Queue.Enqueue(obj);
try
{
m_Sync.Set();
}
catch
{
}
}
}
m_Thread = new Thread( Thread_Start );
m_Thread.Name = threadName;//"MyRunUO Database Command Queue";
m_Thread.Priority = Config.DatabaseThreadPriority;
m_Thread.Start();
}
private void Thread_Start()
{
bool connected = false;
private void Thread_Start()
{
bool connected = false;
OdbcConnection connection = null;
OdbcCommand command = null;
OdbcTransaction transact = null;
OdbcConnection connection = null;
OdbcCommand command = null;
OdbcTransaction transact = null;
DateTime start = DateTime.UtcNow;
DateTime start = DateTime.UtcNow;
bool shouldWriteException = true;
bool shouldWriteException = true;
while (true)
{
m_Sync.WaitOne();
while ( true )
{
m_Sync.WaitOne();
while (m_Queue.Count > 0)
try
{
object obj = m_Queue.Dequeue();
while ( m_Queue.Count > 0 )
{
try
{
object obj = m_Queue.Dequeue();
if (obj == null)
{
if (connected)
{
if (transact != null)
try
{
transact.Commit();
}
catch (Exception commitException)
{
Console.WriteLine("MyRunUO: Exception caught when committing transaction");
Console.WriteLine(commitException);
if ( obj == null )
{
if ( connected )
{
if ( transact != null )
{
try{ transact.Commit(); }
catch ( Exception commitException )
{
Console.WriteLine( "MyRunUO: Exception caught when committing transaction" );
Console.WriteLine( commitException );
try
{
transact.Rollback();
Console.WriteLine("MyRunUO: Transaction has been rolled back");
}
catch (Exception rollbackException)
{
Console.WriteLine("MyRunUO: Exception caught when rolling back transaction");
Console.WriteLine(rollbackException);
}
}
try
{
transact.Rollback();
Console.WriteLine( "MyRunUO: Transaction has been rolled back" );
}
catch ( Exception rollbackException )
{
Console.WriteLine( "MyRunUO: Exception caught when rolling back transaction" );
Console.WriteLine( rollbackException );
}
}
}
try
{
connection.Close();
}
catch
{
}
try{ connection.Close(); }
catch{}
try
{
connection.Dispose();
}
catch
{
}
try{ connection.Dispose(); }
catch{}
try
{
command.Dispose();
}
catch
{
}
try{ command.Dispose(); }
catch{}
try
{
m_Sync.Close();
}
catch
{
}
try{ m_Sync.Close(); }
catch{}
Console.WriteLine(m_CompletionString, (DateTime.UtcNow - start).TotalSeconds);
HasCompleted = true;
Console.WriteLine( m_CompletionString, (DateTime.UtcNow - start).TotalSeconds );
HasCompleted = true;
return;
}
return;
}
try
{
connected = true;
connection = new OdbcConnection(m_ConnectionString);
connection.Open();
command = connection.CreateCommand();
try
{
connected = true;
connection = new OdbcConnection( m_ConnectionString );
connection.Open();
command = connection.CreateCommand();
if (Config.UseTransactions)
{
transact = connection.BeginTransaction();
command.Transaction = transact;
}
}
catch (Exception e)
{
try
{
transact?.Rollback();
}
catch
{
}
if ( Config.UseTransactions )
{
transact = connection.BeginTransaction();
command.Transaction = transact;
}
}
catch ( Exception e )
{
try
{
transact?.Rollback();
}
catch{}
try
{
connection?.Close();
}
catch
{
}
try
{
connection?.Close();
}
catch{}
try
{
connection?.Dispose();
}
catch
{
}
try
{
connection?.Dispose();
}
catch{}
try
{
command?.Dispose();
}
catch
{
}
try
{
command?.Dispose();
}
catch{}
try
{
m_Sync.Close();
}
catch
{
}
try{ m_Sync.Close(); }
catch{}
Console.WriteLine("MyRunUO: Unable to connect to the database");
Console.WriteLine(e);
HasCompleted = true;
return;
}
}
else if (obj is string s)
{
command.CommandText = s;
command.ExecuteNonQuery();
}
else
{
string[] parms = (string[])obj;
Console.WriteLine( "MyRunUO: Unable to connect to the database" );
Console.WriteLine( e );
HasCompleted = true;
return;
}
}
else if ( obj is string s )
{
command.CommandText = s;
command.ExecuteNonQuery();
}
else
{
string[] parms = (string[])obj;
command.CommandText = parms[0];
command.CommandText = parms[0];
if (command.ExecuteScalar() == null)
{
command.CommandText = parms[1];
command.ExecuteNonQuery();
}
}
}
catch (Exception e)
{
if (shouldWriteException)
{
Console.WriteLine("MyRunUO: Exception caught in database thread");
Console.WriteLine(e);
shouldWriteException = false;
}
}
if ( command.ExecuteScalar() == null )
{
command.CommandText = parms[1];
command.ExecuteNonQuery();
}
}
}
catch ( Exception e )
{
if ( shouldWriteException )
{
Console.WriteLine( "MyRunUO: Exception caught in database thread" );
Console.WriteLine( e );
shouldWriteException = false;
}
}
}
lock ( m_Queue.SyncRoot )
{
if ( m_Queue.Count == 0 )
m_Sync.Reset();
}
}
}
}
}
lock (m_Queue.SyncRoot)
{
if (m_Queue.Count == 0)
m_Sync.Reset();
}
}
}
}
}

View file

@ -2,89 +2,86 @@ using System.Collections;
namespace Server.Engines.MyRunUO
{
public class LayerComparer : IComparer
{
private static Layer PlateArms = (Layer)255;
private static Layer ChainTunic = (Layer)254;
private static Layer LeatherShorts = (Layer)253;
public class LayerComparer : IComparer
{
private static Layer PlateArms = (Layer)255;
private static Layer ChainTunic = (Layer)254;
private static Layer LeatherShorts = (Layer)253;
private static Layer[] m_DesiredLayerOrder = {
Layer.Cloak,
Layer.Bracelet,
Layer.Ring,
Layer.Shirt,
Layer.Pants,
Layer.InnerLegs,
Layer.Shoes,
LeatherShorts,
Layer.Arms,
Layer.InnerTorso,
LeatherShorts,
PlateArms,
Layer.MiddleTorso,
Layer.OuterLegs,
Layer.Neck,
Layer.Waist,
Layer.Gloves,
Layer.OuterTorso,
Layer.OneHanded,
Layer.TwoHanded,
Layer.FacialHair,
Layer.Hair,
Layer.Helm,
Layer.Talisman
};
private static Layer[] m_DesiredLayerOrder =
{
Layer.Cloak,
Layer.Bracelet,
Layer.Ring,
Layer.Shirt,
Layer.Pants,
Layer.InnerLegs,
Layer.Shoes,
LeatherShorts,
Layer.Arms,
Layer.InnerTorso,
LeatherShorts,
PlateArms,
Layer.MiddleTorso,
Layer.OuterLegs,
Layer.Neck,
Layer.Waist,
Layer.Gloves,
Layer.OuterTorso,
Layer.OneHanded,
Layer.TwoHanded,
Layer.FacialHair,
Layer.Hair,
Layer.Helm,
Layer.Talisman
};
public static int[] TranslationTable { get; }
public static readonly IComparer Instance = new LayerComparer();
static LayerComparer()
{
TranslationTable = new int[256];
static LayerComparer()
{
TranslationTable = new int[256];
for ( int i = 0; i < m_DesiredLayerOrder.Length; ++i )
TranslationTable[(int)m_DesiredLayerOrder[i]] = m_DesiredLayerOrder.Length - i;
}
for (int i = 0; i < m_DesiredLayerOrder.Length; ++i)
TranslationTable[(int)m_DesiredLayerOrder[i]] = m_DesiredLayerOrder.Length - i;
}
public static bool IsValid( Item item )
{
return ( TranslationTable[(int)item.Layer] > 0 );
}
public static int[] TranslationTable{ get; }
public static readonly IComparer Instance = new LayerComparer();
public int Compare(object x, object y)
{
Item a = (Item)x;
Item b = (Item)y;
public LayerComparer()
{
}
Layer aLayer = a.Layer;
Layer bLayer = b.Layer;
public Layer Fix( int itemID, Layer oldLayer )
{
if ( itemID == 0x1410 || itemID == 0x1417 ) // platemail arms
return PlateArms;
aLayer = Fix(a.ItemID, aLayer);
bLayer = Fix(b.ItemID, bLayer);
if ( itemID == 0x13BF || itemID == 0x13C4 ) // chainmail tunic
return ChainTunic;
return TranslationTable[(int)bLayer] - TranslationTable[(int)aLayer];
}
if ( itemID == 0x1C08 || itemID == 0x1C09 ) // leather skirt
return LeatherShorts;
public static bool IsValid(Item item)
{
return TranslationTable[(int)item.Layer] > 0;
}
if ( itemID == 0x1C00 || itemID == 0x1C01 ) // leather shorts
return LeatherShorts;
public Layer Fix(int itemID, Layer oldLayer)
{
if (itemID == 0x1410 || itemID == 0x1417) // platemail arms
return PlateArms;
return oldLayer;
}
if (itemID == 0x13BF || itemID == 0x13C4) // chainmail tunic
return ChainTunic;
public int Compare( object x, object y )
{
Item a = (Item)x;
Item b = (Item)y;
if (itemID == 0x1C08 || itemID == 0x1C09) // leather skirt
return LeatherShorts;
Layer aLayer = a.Layer;
Layer bLayer = b.Layer;
if (itemID == 0x1C00 || itemID == 0x1C01) // leather shorts
return LeatherShorts;
aLayer = Fix( a.ItemID, aLayer );
bLayer = Fix( b.ItemID, bLayer );
return TranslationTable[(int)bLayer] - TranslationTable[(int)aLayer];
}
}
return oldLayer;
}
}
}

File diff suppressed because it is too large Load diff

View file

@ -5,68 +5,69 @@ using Server.Network;
namespace Server.Engines.MyRunUO
{
public class MyRunUOStatus
{
public static void Initialize()
{
if ( Config.Enabled )
{
Timer.DelayCall( TimeSpan.FromSeconds( 20.0 ), Config.StatusUpdateInterval, Begin );
public class MyRunUOStatus
{
private static DatabaseCommandQueue m_Command;
CommandSystem.Register( "UpdateWebStatus", AccessLevel.Administrator, UpdateWebStatus_OnCommand );
}
}
public static void Initialize()
{
if (Config.Enabled)
{
Timer.DelayCall(TimeSpan.FromSeconds(20.0), Config.StatusUpdateInterval, Begin);
[Usage( "UpdateWebStatus" )]
[Description( "Starts the process of updating the MyRunUO online status database." )]
public static void UpdateWebStatus_OnCommand( CommandEventArgs e )
{
if ( m_Command == null || m_Command.HasCompleted )
{
Begin();
e.Mobile.SendMessage( "Web status update process has been started." );
}
else
{
e.Mobile.SendMessage( "Web status database is already being updated." );
}
}
CommandSystem.Register("UpdateWebStatus", AccessLevel.Administrator, UpdateWebStatus_OnCommand);
}
}
private static DatabaseCommandQueue m_Command;
[Usage("UpdateWebStatus")]
[Description("Starts the process of updating the MyRunUO online status database.")]
public static void UpdateWebStatus_OnCommand(CommandEventArgs e)
{
if (m_Command == null || m_Command.HasCompleted)
{
Begin();
e.Mobile.SendMessage("Web status update process has been started.");
}
else
{
e.Mobile.SendMessage("Web status database is already being updated.");
}
}
public static void Begin()
{
if ( m_Command != null && !m_Command.HasCompleted )
return;
public static void Begin()
{
if (m_Command != null && !m_Command.HasCompleted)
return;
DateTime start = DateTime.UtcNow;
Console.WriteLine( "MyRunUO: Updating status database" );
DateTime start = DateTime.UtcNow;
Console.WriteLine("MyRunUO: Updating status database");
try
{
m_Command = new DatabaseCommandQueue( "MyRunUO: Status database updated in {0:F1} seconds", "MyRunUO Status Database Thread" );
try
{
m_Command = new DatabaseCommandQueue("MyRunUO: Status database updated in {0:F1} seconds",
"MyRunUO Status Database Thread");
m_Command.Enqueue( "DELETE FROM myrunuo_status" );
m_Command.Enqueue("DELETE FROM myrunuo_status");
List<NetState> online = NetState.Instances;
List<NetState> online = NetState.Instances;
for ( int i = 0; i < online.Count; ++i )
{
NetState ns = online[i];
Mobile mob = ns.Mobile;
for (int i = 0; i < online.Count; ++i)
{
NetState ns = online[i];
Mobile mob = ns.Mobile;
if ( mob != null )
m_Command.Enqueue(
$"INSERT INTO myrunuo_status (char_id) VALUES ({mob.Serial.Value.ToString()})");
}
}
catch ( Exception e )
{
Console.WriteLine( "MyRunUO: Error updating status database" );
Console.WriteLine( e );
}
if (mob != null)
m_Command.Enqueue(
$"INSERT INTO myrunuo_status (char_id) VALUES ({mob.Serial.Value.ToString()})");
}
}
catch (Exception e)
{
Console.WriteLine("MyRunUO: Error updating status database");
Console.WriteLine(e);
}
m_Command?.Enqueue( null );
}
}
m_Command?.Enqueue(null);
}
}
}