-nocache option added

BaseCreatures skillcap increased to accommodate skills over 100
Cliloc fix
This commit is contained in:
asayre 2006-06-18 05:17:31 +00:00
parent 720c1caaa3
commit c89964aed6
4 changed files with 504 additions and 269 deletions

View file

@ -273,7 +273,7 @@ namespace Server.Engines.Plants
else
{
// a decorative ~1_COLOR~ ~2_TYPE~ plant
list.Add( 1070973, string.Format( "#{0}\t#{1}", title, typeInfo.Name ) );
list.Add( hueInfo.IsBright() ? 1074267 : 1070973, string.Format( "#{0}\t#{1}", title, typeInfo.Name ) );
}
}
else if ( m_PlantStatus >= PlantStatus.Seed )

View file

@ -197,11 +197,18 @@ namespace Server.SkillHandlers
}
public static void AdjustSkillCaps( BaseCreature bc )
{
int totalCapIncrease = 0;
for ( int i = 0; i < bc.Skills.Length; ++i )
{
if ( bc.Skills[i].Base > bc.Skills[i].Cap )
if( bc.Skills[i].Base > bc.Skills[i].Cap )
{
totalCapIncrease += (bc.Skills[i].BaseFixedPoint - bc.Skills[i].CapFixedPoint);
bc.Skills[i].Cap = bc.Skills[i].Base;
}
}
bc.SkillsCap += totalCapIncrease;
}
private class InternalTimer : Timer

View file

@ -33,10 +33,12 @@ using Server.Accounting;
using Server.Gumps;
using Server.Network;
namespace Server {
namespace Server
{
public delegate void Slice();
public class Core {
public class Core
{
private static bool m_Crashed;
private static Thread timerThread;
private static string m_BaseDirectory;
@ -47,6 +49,7 @@ namespace Server {
private static Thread m_Thread;
private static bool m_Service;
private static bool m_Debug;
private static bool m_Cache = true;
private static MultiTextWriter m_MultiConOut;
private static bool m_Profiling;
@ -55,32 +58,37 @@ namespace Server {
private static MessagePump m_MessagePump;
public static MessagePump MessagePump {
public static MessagePump MessagePump
{
get { return m_MessagePump; }
set { m_MessagePump = value; }
}
public static Slice Slice;
public static bool Profiling {
public static bool Profiling
{
get { return m_Profiling; }
set {
if ( m_Profiling == value )
set
{
if( m_Profiling == value )
return;
m_Profiling = value;
if ( m_ProfileStart > DateTime.MinValue )
if( m_ProfileStart > DateTime.MinValue )
m_ProfileTime += DateTime.Now - m_ProfileStart;
m_ProfileStart = ( m_Profiling ? DateTime.Now : DateTime.MinValue );
m_ProfileStart = (m_Profiling ? DateTime.Now : DateTime.MinValue);
}
}
public static TimeSpan ProfileTime {
get {
if ( m_ProfileStart > DateTime.MinValue )
return m_ProfileTime + ( DateTime.Now - m_ProfileStart );
public static TimeSpan ProfileTime
{
get
{
if( m_ProfileStart > DateTime.MinValue )
return m_ProfileTime + (DateTime.Now - m_ProfileStart);
return m_ProfileTime;
}
@ -94,7 +102,7 @@ namespace Server {
public static Thread Thread { get { return m_Thread; } }
public static MultiTextWriter MultiConsoleOut { get { return m_MultiConOut; } }
public static readonly bool Is64Bit = ( IntPtr.Size == 8 );
public static readonly bool Is64Bit = (IntPtr.Size == 8);
private static bool m_MultiProcessor;
private static int m_ProcessorCount;
@ -102,16 +110,18 @@ namespace Server {
public static bool MultiProcessor { get { return m_MultiProcessor; } set { m_MultiProcessor = value; } }
public static int ProcessorCount { get { return m_ProcessorCount; } set { m_ProcessorCount = value; } }
public static string FindDataFile( string path ) {
if ( m_DataDirectories.Count == 0 )
public static string FindDataFile( string path )
{
if( m_DataDirectories.Count == 0 )
throw new InvalidOperationException( "Attempted to FindDataFile before DataDirectories list has been filled." );
string fullPath = null;
for ( int i = 0; i < m_DataDirectories.Count; ++i ) {
for( int i = 0; i < m_DataDirectories.Count; ++i )
{
fullPath = Path.Combine( m_DataDirectories[i], path );
if ( File.Exists( fullPath ) )
if( File.Exists( fullPath ) )
break;
fullPath = null;
@ -120,41 +130,52 @@ namespace Server {
return fullPath;
}
public static string FindDataFile( string format, params object[] args ) {
public static string FindDataFile( string format, params object[] args )
{
return FindDataFile( String.Format( format, args ) );
}
#region Expansions
private static Expansion m_Expansion;
public static Expansion Expansion {
public static Expansion Expansion
{
get { return m_Expansion; }
set { m_Expansion = value; }
}
public static bool AOS {
get {
public static bool AOS
{
get
{
return m_Expansion >= Expansion.AOS;
}
}
public static bool SE {
get {
public static bool SE
{
get
{
return m_Expansion >= Expansion.SE;
}
}
public static bool ML {
get {
public static bool ML
{
get
{
return m_Expansion >= Expansion.ML;
}
}
#endregion
public static string ExePath {
get {
if ( m_ExePath == null ) {
public static string ExePath
{
get
{
if( m_ExePath == null )
{
m_ExePath = Assembly.Location;
//m_ExePath = Process.GetCurrentProcess().MainModule.FileName;
}
@ -163,15 +184,21 @@ namespace Server {
}
}
public static string BaseDirectory {
get {
if ( m_BaseDirectory == null ) {
try {
public static string BaseDirectory
{
get
{
if( m_BaseDirectory == null )
{
try
{
m_BaseDirectory = ExePath;
if ( m_BaseDirectory.Length > 0 )
if( m_BaseDirectory.Length > 0 )
m_BaseDirectory = Path.GetDirectoryName( m_BaseDirectory );
} catch {
}
catch
{
m_BaseDirectory = "";
}
}
@ -180,30 +207,40 @@ namespace Server {
}
}
private static void CurrentDomain_UnhandledException( object sender, UnhandledExceptionEventArgs e ) {
private static void CurrentDomain_UnhandledException( object sender, UnhandledExceptionEventArgs e )
{
Console.WriteLine( e.IsTerminating ? "Error:" : "Warning:" );
Console.WriteLine( e.ExceptionObject );
if ( e.IsTerminating ) {
if( e.IsTerminating )
{
m_Crashed = true;
bool close = false;
try {
try
{
CrashedEventArgs args = new CrashedEventArgs( e.ExceptionObject as Exception );
EventSink.InvokeCrashed( args );
close = args.Close;
} catch {
}
catch
{
}
if ( !close && !m_Service ) {
try {
for ( int i = 0; i < m_MessagePump.Listeners.Length; i++ ) {
if( !close && !m_Service )
{
try
{
for( int i = 0; i < m_MessagePump.Listeners.Length; i++ )
{
m_MessagePump.Listeners[i].Dispose();
}
} catch {
}
catch
{
}
Console.WriteLine( "This exception is fatal, press return to exit" );
@ -215,11 +252,12 @@ namespace Server {
}
#if !MONO
private enum ConsoleEventType {
private enum ConsoleEventType
{
CTRL_C_EVENT,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_LOGOFF_EVENT=5,
CTRL_SHUTDOWN_EVENT
}
@ -229,15 +267,17 @@ namespace Server {
[System.Runtime.InteropServices.DllImport( "Kernel32.dll" )]
private static extern bool SetConsoleCtrlHandler( ConsoleEventHandler callback, bool add );
private static bool OnConsoleEvent( ConsoleEventType type ) {
if ( World.Saving )
private static bool OnConsoleEvent( ConsoleEventType type )
{
if( World.Saving )
return true;
return false;
}
#endif
private static void CurrentDomain_ProcessExit( object sender, EventArgs e ) {
private static void CurrentDomain_ProcessExit( object sender, EventArgs e )
{
HandleClosed();
}
@ -248,64 +288,79 @@ namespace Server {
private static int m_CycleIndex;
private static float[] m_CyclesPerSecond = new float[100];
public static float CyclesPerSecond {
get { return m_CyclesPerSecond[Math.Max( 0, ( m_CycleIndex - 1 ) % m_CyclesPerSecond.Length )]; }
public static float CyclesPerSecond
{
get { return m_CyclesPerSecond[Math.Max( 0, (m_CycleIndex - 1) % m_CyclesPerSecond.Length )]; }
}
public static float AverageCPS {
get {
public static float AverageCPS
{
get
{
float t = 0.0f;
int c = 0;
for ( int i = 0; i < m_CycleIndex && i < m_CyclesPerSecond.Length; ++i ) {
for( int i = 0; i < m_CycleIndex && i < m_CyclesPerSecond.Length; ++i )
{
t += m_CyclesPerSecond[i];
++c;
}
return ( t / Math.Max( c, 1 ) );
return (t / Math.Max( c, 1 ));
}
}
private static void HandleClosed() {
if ( m_Closing )
private static void HandleClosed()
{
if( m_Closing )
return;
m_Closing = true;
Console.Write( "Exiting..." );
if ( !m_Crashed )
if( !m_Crashed )
EventSink.InvokeShutdown( new ShutdownEventArgs() );
if ( timerThread.ThreadState != System.Threading.ThreadState.Unstarted )
if( timerThread.ThreadState != System.Threading.ThreadState.Unstarted )
timerThread.Join();
Console.WriteLine( "done" );
}
public static void Main( string[] args ) {
public static void Main( string[] args )
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler( CurrentDomain_UnhandledException );
AppDomain.CurrentDomain.ProcessExit += new EventHandler( CurrentDomain_ProcessExit );
for ( int i = 0; i < args.Length; ++i ) {
if ( Insensitive.Equals( args[i], "-debug" ) )
for( int i = 0; i < args.Length; ++i )
{
if( Insensitive.Equals( args[i], "-debug" ) )
m_Debug = true;
else if ( Insensitive.Equals( args[i], "-service" ) )
else if( Insensitive.Equals( args[i], "-service" ) )
m_Service = true;
else if ( Insensitive.Equals( args[i], "-profile" ) )
else if( Insensitive.Equals( args[i], "-profile" ) )
Profiling = true;
else if( Insensitive.Equals( args[i], "-nocache" ) )
m_Cache = false;
}
try {
if ( m_Service ) {
if ( !Directory.Exists( "Logs" ) )
try
{
if( m_Service )
{
if( !Directory.Exists( "Logs" ) )
Directory.CreateDirectory( "Logs" );
Console.SetOut( m_MultiConOut = new MultiTextWriter( Console.Out, new FileLogger( "Logs/Console.log" ) ) );
} else {
}
else
{
Console.SetOut( m_MultiConOut = new MultiTextWriter( Console.Out ) );
}
} catch {
}
catch
{
}
#if !MONO
@ -317,10 +372,10 @@ namespace Server {
m_Process = Process.GetCurrentProcess();
m_Assembly = Assembly.GetEntryAssembly();
if ( m_Thread != null )
if( m_Thread != null )
m_Thread.Name = "Core Thread";
if ( BaseDirectory.Length > 0 )
if( BaseDirectory.Length > 0 )
Directory.SetCurrentDirectory( BaseDirectory );
Timer.TimerThread ttObj = new Timer.TimerThread();
@ -335,24 +390,25 @@ namespace Server {
string s = Arguments;
if ( s.Length > 0 )
if( s.Length > 0 )
Console.WriteLine( "Core: Running with arguments: {0}", s );
int processorCount = Environment.ProcessorCount;
if ( processorCount > 1 )
if( processorCount > 1 )
m_MultiProcessor = true;
if ( m_MultiProcessor || Is64Bit )
if( m_MultiProcessor || Is64Bit )
Console.WriteLine( "Core: Optimizing for {0} {2}processor{1}", processorCount, processorCount == 1 ? "" : "s", Is64Bit ? "64-bit " : "" );
m_ProcessorCount = processorCount;
while ( !ScriptCompiler.Compile( m_Debug ) ) {
while( !ScriptCompiler.Compile( m_Debug, m_Cache ) )
{
Console.WriteLine( "Scripts: One or more scripts failed to compile or no script files were found." );
Console.WriteLine( " - Press return to exit, or R to try again." );
if ( Console.ReadKey( true ).Key != ConsoleKey.R )
if( Console.ReadKey( true ).Key != ConsoleKey.R )
return;
}
@ -360,22 +416,24 @@ namespace Server {
timerThread.Start();
for ( int i = 0; i < Map.AllMaps.Count; ++i )
for( int i = 0; i < Map.AllMaps.Count; ++i )
Map.AllMaps[i].Tiles.Force();
NetState.Initialize();
EventSink.InvokeServerStarted();
try {
try
{
DateTime now, last = DateTime.Now;
const int sampleInterval = 100;
const float ticksPerSecond = ( float ) ( TimeSpan.TicksPerSecond * sampleInterval );
const float ticksPerSecond = (float)(TimeSpan.TicksPerSecond * sampleInterval);
int sample = 0;
while ( !m_Closing ) {
while( !m_Closing )
{
Thread.Sleep( m_MultiProcessor ? 0 : 1 );
Mobile.ProcessDeltaQueue();
@ -387,44 +445,53 @@ namespace Server {
NetState.FlushAll();
NetState.ProcessDisposedQueue();
if ( Slice != null )
if( Slice != null )
Slice();
if ( ( ++sample % sampleInterval ) == 0 ) {
if( (++sample % sampleInterval) == 0 )
{
now = DateTime.Now;
m_CyclesPerSecond[m_CycleIndex++ % m_CyclesPerSecond.Length] =
ticksPerSecond / ( now.Ticks - last.Ticks );
ticksPerSecond / (now.Ticks - last.Ticks);
last = now;
}
}
} catch ( Exception e ) {
}
catch( Exception e )
{
CurrentDomain_UnhandledException( null, new UnhandledExceptionEventArgs( e, true ) );
}
if ( timerThread.IsAlive )
if( timerThread.IsAlive )
timerThread.Abort();
}
public static string Arguments {
get {
public static string Arguments
{
get
{
StringBuilder sb = new StringBuilder();
if ( Core.Debug )
if( Core.Debug )
Utility.Seperate( sb, "-debug", " " );
if ( Core.Service )
if( Core.Service )
Utility.Seperate( sb, "-service", " " );
if ( Core.Profiling )
if( Core.Profiling )
Utility.Seperate( sb, "-profile", " " );
if( !m_Cache )
Utility.Seperate( sb, "-nocache", " " );
return sb.ToString();
}
}
private static int m_GlobalMaxUpdateRange = 24;
public static int GlobalMaxUpdateRange {
public static int GlobalMaxUpdateRange
{
get { return m_GlobalMaxUpdateRange; }
set { m_GlobalMaxUpdateRange = value; }
}
@ -434,34 +501,39 @@ namespace Server {
public static int ScriptItems { get { return m_ItemCount; } }
public static int ScriptMobiles { get { return m_MobileCount; } }
public static void VerifySerialization() {
public static void VerifySerialization()
{
m_ItemCount = 0;
m_MobileCount = 0;
VerifySerialization( Assembly.GetCallingAssembly() );
for ( int a = 0; a < ScriptCompiler.Assemblies.Length; ++a )
for( int a = 0; a < ScriptCompiler.Assemblies.Length; ++a )
VerifySerialization( ScriptCompiler.Assemblies[a] );
}
private static void VerifySerialization( Assembly a ) {
if ( a == null )
private static void VerifySerialization( Assembly a )
{
if( a == null )
return;
Type[] ctorTypes = new Type[] { typeof( Serial ) };
foreach ( Type t in a.GetTypes() ) {
foreach( Type t in a.GetTypes() )
{
bool isItem = t.IsSubclassOf( typeof( Item ) );
if ( isItem || t.IsSubclassOf( typeof( Mobile ) ) ) {
if ( isItem )
if( isItem || t.IsSubclassOf( typeof( Mobile ) ) )
{
if( isItem )
++m_ItemCount;
else
++m_MobileCount;
bool warned = false;
try {
try
{
/*
if( isItem && t.IsPublic && !t.IsAbstract )
@ -478,40 +550,46 @@ namespace Server {
}
*/
if ( t.GetConstructor( ctorTypes ) == null ) {
if ( !warned )
if( t.GetConstructor( ctorTypes ) == null )
{
if( !warned )
Console.WriteLine( "Warning: {0}", t );
warned = true;
Console.WriteLine( " - No serialization constructor" );
}
if ( t.GetMethod( "Serialize", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly ) == null ) {
if ( !warned )
if( t.GetMethod( "Serialize", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly ) == null )
{
if( !warned )
Console.WriteLine( "Warning: {0}", t );
warned = true;
Console.WriteLine( " - No Serialize() method" );
}
if ( t.GetMethod( "Deserialize", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly ) == null ) {
if ( !warned )
if( t.GetMethod( "Deserialize", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly ) == null )
{
if( !warned )
Console.WriteLine( "Warning: {0}", t );
warned = true;
Console.WriteLine( " - No Deserialize() method" );
}
if ( warned )
if( warned )
Console.WriteLine();
} catch {
}
catch
{
}
}
}
}
}
public class FileLogger : TextWriter, IDisposable {
public class FileLogger : TextWriter, IDisposable
{
private string m_FileName;
private bool m_NewLine;
public const string DateFormat = "[MMMM dd hh:mm:ss.f tt]: ";
@ -519,20 +597,26 @@ namespace Server {
public string FileName { get { return m_FileName; } }
public FileLogger( string file )
: this( file, false ) {
: this( file, false )
{
}
public FileLogger( string file, bool append ) {
public FileLogger( string file, bool append )
{
m_FileName = file;
using ( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read ) ) ) {
using( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, append ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.Read ) ) )
{
writer.WriteLine( ">>>Logging started on {0}.", DateTime.Now.ToString( "f" ) ); //f = Tuesday, April 10, 2001 3:51 PM
}
m_NewLine = true;
}
public override void Write( char ch ) {
using ( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, FileMode.Append, FileAccess.Write, FileShare.Read ) ) ) {
if ( m_NewLine ) {
public override void Write( char ch )
{
using( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, FileMode.Append, FileAccess.Write, FileShare.Read ) ) )
{
if( m_NewLine )
{
writer.Write( DateTime.Now.ToString( DateFormat ) );
m_NewLine = false;
}
@ -540,9 +624,12 @@ namespace Server {
}
}
public override void Write( string str ) {
using ( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, FileMode.Append, FileAccess.Write, FileShare.Read ) ) ) {
if ( m_NewLine ) {
public override void Write( string str )
{
using( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, FileMode.Append, FileAccess.Write, FileShare.Read ) ) )
{
if( m_NewLine )
{
writer.Write( DateTime.Now.ToString( DateFormat ) );
m_NewLine = false;
}
@ -550,53 +637,64 @@ namespace Server {
}
}
public override void WriteLine( string line ) {
using ( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, FileMode.Append, FileAccess.Write, FileShare.Read ) ) ) {
if ( m_NewLine )
public override void WriteLine( string line )
{
using( StreamWriter writer = new StreamWriter( new FileStream( m_FileName, FileMode.Append, FileAccess.Write, FileShare.Read ) ) )
{
if( m_NewLine )
writer.Write( DateTime.Now.ToString( DateFormat ) );
writer.WriteLine( line );
m_NewLine = true;
}
}
public override System.Text.Encoding Encoding {
public override System.Text.Encoding Encoding
{
get { return System.Text.Encoding.Default; }
}
}
public class MultiTextWriter : TextWriter {
public class MultiTextWriter : TextWriter
{
private List<TextWriter> m_Streams;
public MultiTextWriter( params TextWriter[] streams ) {
public MultiTextWriter( params TextWriter[] streams )
{
m_Streams = new List<TextWriter>( streams );
if ( m_Streams.Count < 0 )
if( m_Streams.Count < 0 )
throw new ArgumentException( "You must specify at least one stream." );
}
public void Add( TextWriter tw ) {
public void Add( TextWriter tw )
{
m_Streams.Add( tw );
}
public void Remove( TextWriter tw ) {
public void Remove( TextWriter tw )
{
m_Streams.Remove( tw );
}
public override void Write( char ch ) {
for ( int i = 0; i < m_Streams.Count; i++ )
public override void Write( char ch )
{
for( int i = 0; i < m_Streams.Count; i++ )
m_Streams[i].Write( ch );
}
public override void WriteLine( string line ) {
for ( int i = 0; i < m_Streams.Count; i++ )
public override void WriteLine( string line )
{
for( int i = 0; i < m_Streams.Count; i++ )
m_Streams[i].WriteLine( line );
}
public override void WriteLine( string line, params object[] args ) {
public override void WriteLine( string line, params object[] args )
{
WriteLine( String.Format( line, args ) );
}
public override Encoding Encoding {
public override Encoding Encoding
{
get { return Encoding.Default; }
}
}

View file

@ -26,35 +26,45 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Reflection;
using System.Security.Cryptography;
using Microsoft.CSharp;
using Microsoft.VisualBasic;
namespace Server {
public class ScriptCompiler {
namespace Server
{
public class ScriptCompiler
{
private static Assembly[] m_Assemblies;
public static Assembly[] Assemblies {
get {
public static Assembly[] Assemblies
{
get
{
return m_Assemblies;
}
set {
set
{
m_Assemblies = value;
}
}
private static List<string> m_AdditionalReferences = new List<string>();
public static string[] GetReferenceAssemblies() {
public static string[] GetReferenceAssemblies()
{
List<string> list = new List<string>();
string path = Path.Combine( Core.BaseDirectory, "Data/Assemblies.cfg" );
if ( File.Exists( path ) ) {
using ( StreamReader ip = new StreamReader( path ) ) {
if( File.Exists( path ) )
{
using( StreamReader ip = new StreamReader( path ) )
{
string line;
while ( ( line = ip.ReadLine() ) != null ) {
if ( line.Length > 0 && !line.StartsWith( "#" ) )
while( (line = ip.ReadLine()) != null )
{
if( line.Length > 0 && !line.StartsWith( "#" ) )
list.Add( line );
}
}
@ -67,7 +77,8 @@ namespace Server {
return list.ToArray();
}
public static string GetDefines() {
public static string GetDefines()
{
StringBuilder sb = null;
#if MONO
@ -75,16 +86,17 @@ namespace Server {
#endif
//These two defines are legacy, ie, depreciated.
if ( Core.Is64Bit )
if( Core.Is64Bit )
AppendDefine( ref sb, "x64" );
AppendDefine( ref sb, "Framework_2_0" );
return ( sb == null ? null : sb.ToString() );
return (sb == null ? null : sb.ToString());
}
public static void AppendDefine( ref StringBuilder sb, string define ) {
if ( sb == null )
public static void AppendDefine( ref StringBuilder sb, string define )
{
if( sb == null )
sb = new StringBuilder();
else
sb.Append( ';' );
@ -92,14 +104,18 @@ namespace Server {
sb.Append( define );
}
private static byte[] GetHashCode( string compiledFile, string[] scriptFiles, bool debug ) {
using ( MemoryStream ms = new MemoryStream() ) {
using ( BinaryWriter bin = new BinaryWriter( ms ) ) {
private static byte[] GetHashCode( string compiledFile, string[] scriptFiles, bool debug )
{
using( MemoryStream ms = new MemoryStream() )
{
using( BinaryWriter bin = new BinaryWriter( ms ) )
{
FileInfo fileInfo = new FileInfo( compiledFile );
bin.Write( fileInfo.LastWriteTimeUtc.Ticks );
foreach ( string scriptFile in scriptFiles ) {
foreach( string scriptFile in scriptFiles )
{
fileInfo = new FileInfo( scriptFile );
bin.Write( fileInfo.LastWriteTimeUtc.Ticks );
@ -109,50 +125,69 @@ namespace Server {
ms.Position = 0;
using ( System.Security.Cryptography.SHA1 sha1 = System.Security.Cryptography.SHA1.Create() ) {
using( SHA1 sha1 = SHA1.Create() )
{
return sha1.ComputeHash( ms );
}
}
}
}
public static bool CompileCSScripts( out Assembly assembly ) {
return CompileCSScripts( false, out assembly );
public static bool CompileCSScripts( out Assembly assembly )
{
return CompileCSScripts( false, true, out assembly );
}
public static bool CompileCSScripts( bool debug, out Assembly assembly ) {
public static bool CompileCSScripts( bool debug, out Assembly assembly )
{
return CompileCSScripts( debug, true, out assembly );
}
public static bool CompileCSScripts( bool debug, bool cache, out Assembly assembly )
{
Console.Write( "Scripts: Compiling C# scripts..." );
string[] files = GetScripts( "*.cs" );
if ( files.Length == 0 ) {
if( files.Length == 0 )
{
Console.WriteLine( "no files found." );
assembly = null;
return true;
}
if ( File.Exists( "Scripts/Output/Scripts.CS.dll" ) ) {
if ( File.Exists( "Scripts/Output/Scripts.CS.hash" ) ) {
try {
if( File.Exists( "Scripts/Output/Scripts.CS.dll" ) )
{
if( cache && File.Exists( "Scripts/Output/Scripts.CS.hash" ) )
{
try
{
byte[] hashCode = GetHashCode( "Scripts/Output/Scripts.CS.dll", files, debug );
using ( FileStream fs = new FileStream( "Scripts/Output/Scripts.CS.hash", FileMode.Open, FileAccess.Read, FileShare.Read ) ) {
using ( BinaryReader bin = new BinaryReader( fs ) ) {
using( FileStream fs = new FileStream( "Scripts/Output/Scripts.CS.hash", FileMode.Open, FileAccess.Read, FileShare.Read ) )
{
using( BinaryReader bin = new BinaryReader( fs ) )
{
byte[] bytes = bin.ReadBytes( hashCode.Length );
if ( bytes.Length == hashCode.Length ) {
if( bytes.Length == hashCode.Length )
{
bool valid = true;
for ( int i = 0; i < bytes.Length; ++i ) {
if ( bytes[i] != hashCode[i] ) {
for( int i = 0; i < bytes.Length; ++i )
{
if( bytes[i] != hashCode[i] )
{
valid = false;
break;
}
}
if ( valid ) {
if( valid )
{
assembly = Assembly.LoadFrom( "Scripts/Output/Scripts.CS.dll" );
if ( !m_AdditionalReferences.Contains( assembly.Location ) ) {
if( !m_AdditionalReferences.Contains( assembly.Location ) )
{
m_AdditionalReferences.Add( assembly.Location );
}
@ -163,21 +198,24 @@ namespace Server {
}
}
}
} catch {
}
catch
{
}
}
}
DeleteFiles( "Scripts.CS*.dll" );
using ( CSharpCodeProvider provider = new CSharpCodeProvider() ) {
using( CSharpCodeProvider provider = new CSharpCodeProvider() )
{
string path = GetUnusedPath( "Scripts.CS" );
CompilerParameters parms = new CompilerParameters( GetReferenceAssemblies(), path, debug );
string defines = GetDefines();
if ( defines != null )
if( defines != null )
parms.CompilerOptions = string.Format( "/D:{0}", defines );
CompilerResults results = provider.CompileAssemblyFromFile( parms, files );
@ -185,21 +223,28 @@ namespace Server {
Display( results );
if ( results.Errors.Count > 0 ) {
if( results.Errors.Count > 0 )
{
assembly = null;
return false;
}
if ( Path.GetFileName( path ) == "Scripts.CS.dll" ) {
try {
if( cache && Path.GetFileName( path ) == "Scripts.CS.dll" )
{
try
{
byte[] hashCode = GetHashCode( path, files, debug );
using ( FileStream fs = new FileStream( "Scripts/Output/Scripts.CS.hash", FileMode.Create, FileAccess.Write, FileShare.None ) ) {
using ( BinaryWriter bin = new BinaryWriter( fs ) ) {
using( FileStream fs = new FileStream( "Scripts/Output/Scripts.CS.hash", FileMode.Create, FileAccess.Write, FileShare.None ) )
{
using( BinaryWriter bin = new BinaryWriter( fs ) )
{
bin.Write( hashCode, 0, hashCode.Length );
}
}
} catch {
}
catch
{
}
}
@ -208,43 +253,61 @@ namespace Server {
}
}
public static bool CompileVBScripts( out Assembly assembly ) {
public static bool CompileVBScripts( out Assembly assembly )
{
return CompileVBScripts( false, out assembly );
}
public static bool CompileVBScripts( bool debug, out Assembly assembly ) {
public static bool CompileVBScripts( bool debug, out Assembly assembly )
{
return CompileVBScripts( debug, true, out assembly );
}
public static bool CompileVBScripts( bool debug, bool cache, out Assembly assembly )
{
Console.Write( "Scripts: Compiling VB.NET scripts..." );
string[] files = GetScripts( "*.vb" );
if ( files.Length == 0 ) {
if( files.Length == 0 )
{
Console.WriteLine( "no files found." );
assembly = null;
return true;
}
if ( File.Exists( "Scripts/Output/Scripts.VB.dll" ) ) {
if ( File.Exists( "Scripts/Output/Scripts.VB.hash" ) ) {
if( File.Exists( "Scripts/Output/Scripts.VB.dll" ) )
{
if( cache && File.Exists( "Scripts/Output/Scripts.VB.hash" ) )
{
byte[] hashCode = GetHashCode( "Scripts/Output/Scripts.VB.dll", files, debug );
try {
using ( FileStream fs = new FileStream( "Scripts/Output/Scripts.VB.hash", FileMode.Open, FileAccess.Read, FileShare.Read ) ) {
using ( BinaryReader bin = new BinaryReader( fs ) ) {
try
{
using( FileStream fs = new FileStream( "Scripts/Output/Scripts.VB.hash", FileMode.Open, FileAccess.Read, FileShare.Read ) )
{
using( BinaryReader bin = new BinaryReader( fs ) )
{
byte[] bytes = bin.ReadBytes( hashCode.Length );
if ( bytes.Length == hashCode.Length ) {
if( bytes.Length == hashCode.Length )
{
bool valid = true;
for ( int i = 0; i < bytes.Length; ++i ) {
if ( bytes[i] != hashCode[i] ) {
for( int i = 0; i < bytes.Length; ++i )
{
if( bytes[i] != hashCode[i] )
{
valid = false;
break;
}
}
if ( valid ) {
if( valid )
{
assembly = Assembly.LoadFrom( "Scripts/Output/Scripts.VB.dll" );
if ( !m_AdditionalReferences.Contains( assembly.Location ) ) {
if( !m_AdditionalReferences.Contains( assembly.Location ) )
{
m_AdditionalReferences.Add( assembly.Location );
}
@ -255,21 +318,24 @@ namespace Server {
}
}
}
} catch {
}
catch
{
}
}
}
DeleteFiles( "Scripts.VB*.dll" );
using ( VBCodeProvider provider = new VBCodeProvider() ) {
using( VBCodeProvider provider = new VBCodeProvider() )
{
string path = GetUnusedPath( "Scripts.VB" );
CompilerParameters parms = new CompilerParameters( GetReferenceAssemblies(), path, debug );
string defines = GetDefines();
if ( defines != null )
if( defines != null )
parms.CompilerOptions = String.Format( "/D:{0}", defines );
CompilerResults results = provider.CompileAssemblyFromFile( parms, files );
@ -277,21 +343,28 @@ namespace Server {
Display( results );
if ( results.Errors.Count > 0 ) {
if( results.Errors.Count > 0 )
{
assembly = null;
return false;
}
if ( Path.GetFileName( path ) == "Scripts.VB.dll" ) {
try {
if( cache && Path.GetFileName( path ) == "Scripts.VB.dll" )
{
try
{
byte[] hashCode = GetHashCode( path, files, debug );
using ( FileStream fs = new FileStream( "Scripts/Output/Scripts.VB.hash", FileMode.Create, FileAccess.Write, FileShare.None ) ) {
using ( BinaryWriter bin = new BinaryWriter( fs ) ) {
using( FileStream fs = new FileStream( "Scripts/Output/Scripts.VB.hash", FileMode.Create, FileAccess.Write, FileShare.None ) )
{
using( BinaryWriter bin = new BinaryWriter( fs ) )
{
bin.Write( hashCode, 0, hashCode.Length );
}
}
} catch {
}
catch
{
}
}
@ -300,24 +373,27 @@ namespace Server {
}
}
public static void Display( CompilerResults results ) {
if ( results.Errors.Count > 0 ) {
public static void Display( CompilerResults results )
{
if( results.Errors.Count > 0 )
{
Dictionary<string, List<CompilerError>> errors = new Dictionary<string, List<CompilerError>>( results.Errors.Count, StringComparer.OrdinalIgnoreCase );
Dictionary<string, List<CompilerError>> warnings = new Dictionary<string, List<CompilerError>>( results.Errors.Count, StringComparer.OrdinalIgnoreCase );
foreach ( CompilerError e in results.Errors ) {
Dictionary<string, List<CompilerError>> table = ( e.IsWarning ? warnings : errors );
foreach( CompilerError e in results.Errors )
{
Dictionary<string, List<CompilerError>> table = (e.IsWarning ? warnings : errors);
List<CompilerError> list = null;
table.TryGetValue( e.FileName, out list );
if ( list == null )
if( list == null )
table[e.FileName] = list = new List<CompilerError>();
list.Add( e );
}
if ( errors.Count > 0 )
if( errors.Count > 0 )
Console.WriteLine( "failed ({0} errors, {1} warnings)", errors.Count, warnings.Count );
else
Console.WriteLine( "done ({0} errors, {1} warnings)", errors.Count, warnings.Count );
@ -327,10 +403,11 @@ namespace Server {
Utility.PushColor( ConsoleColor.Yellow );
if ( warnings.Count > 0 )
if( warnings.Count > 0 )
Console.WriteLine( "Warnings:" );
foreach ( KeyValuePair<string, List<CompilerError>> kvp in warnings ) {
foreach( KeyValuePair<string, List<CompilerError>> kvp in warnings )
{
string fileName = kvp.Key;
List<CompilerError> list = kvp.Value;
@ -341,7 +418,7 @@ namespace Server {
Utility.PushColor( ConsoleColor.DarkYellow );
foreach ( CompilerError e in list )
foreach( CompilerError e in list )
Console.WriteLine( " {0}: Line {1}: {3}", e.ErrorNumber, e.Line, e.Column, e.ErrorText );
Utility.PopColor();
@ -351,10 +428,11 @@ namespace Server {
Utility.PushColor( ConsoleColor.Red );
if ( errors.Count > 0 )
if( errors.Count > 0 )
Console.WriteLine( "Errors:" );
foreach ( KeyValuePair<string, List<CompilerError>> kvp in errors ) {
foreach( KeyValuePair<string, List<CompilerError>> kvp in errors )
{
string fileName = kvp.Key;
List<CompilerError> list = kvp.Value;
@ -365,79 +443,106 @@ namespace Server {
Utility.PushColor( ConsoleColor.DarkRed );
foreach ( CompilerError e in list )
foreach( CompilerError e in list )
Console.WriteLine( " {0}: Line {1}: {3}", e.ErrorNumber, e.Line, e.Column, e.ErrorText );
Utility.PopColor();
}
Utility.PopColor();
} else {
}
else
{
Console.WriteLine( "done (0 errors, 0 warnings)" );
}
}
public static string GetUnusedPath( string name ) {
public static string GetUnusedPath( string name )
{
string path = Path.Combine( Core.BaseDirectory, String.Format( "Scripts/Output/{0}.dll", name ) );
for ( int i = 2; File.Exists( path ) && i <= 1000; ++i )
for( int i = 2; File.Exists( path ) && i <= 1000; ++i )
path = Path.Combine( Core.BaseDirectory, String.Format( "Scripts/Output/{0}.{1}.dll", name, i ) );
return path;
}
public static void DeleteFiles( string mask ) {
try {
public static void DeleteFiles( string mask )
{
try
{
string[] files = Directory.GetFiles( Path.Combine( Core.BaseDirectory, "Scripts/Output" ), mask );
foreach ( string file in files ) {
try { File.Delete( file ); } catch { }
foreach( string file in files )
{
try { File.Delete( file ); }
catch { }
}
} catch {
}
catch
{
}
}
private delegate CompilerResults Compiler( bool debug );
private void LoadScriptedAssembly( List<Assembly> assemblies, string fileName, Compiler compiler, bool debug ) {
private void LoadScriptedAssembly( List<Assembly> assemblies, string fileName, Compiler compiler, bool debug )
{
if ( File.Exists( fileName ) ) {
if( File.Exists( fileName ) )
{
}
}
public static bool Compile() {
public static bool Compile()
{
return Compile( false );
}
public static bool Compile( bool debug ) {
public static bool Compile( bool debug )
{
return Compile( debug, true );
}
public static bool Compile( bool debug, bool cache )
{
EnsureDirectory( "Scripts/" );
EnsureDirectory( "Scripts/Output/" );
if ( m_AdditionalReferences.Count > 0 )
if( m_AdditionalReferences.Count > 0 )
m_AdditionalReferences.Clear();
List<Assembly> assemblies = new List<Assembly>();
Assembly assembly;
if ( CompileCSScripts( debug, out assembly ) ) {
if ( assembly != null ) {
if( CompileCSScripts( debug, cache, out assembly ) )
{
if( assembly != null )
{
assemblies.Add( assembly );
}
} else {
}
else
{
return false;
}
if ( CompileVBScripts( debug, out assembly ) ) {
if ( assembly != null ) {
if( CompileVBScripts( debug, cache, out assembly ) )
{
if( assembly != null )
{
assemblies.Add( assembly );
}
} else {
}
else
{
return false;
}
if ( assemblies.Count == 0 ) {
if( assemblies.Count == 0 )
{
return false;
}
@ -449,20 +554,22 @@ namespace Server {
List<MethodInfo> invoke = new List<MethodInfo>();
for ( int a = 0; a < m_Assemblies.Length; ++a ) {
for( int a = 0; a < m_Assemblies.Length; ++a )
{
Type[] types = m_Assemblies[a].GetTypes();
for ( int i = 0; i < types.Length; ++i ) {
for( int i = 0; i < types.Length; ++i )
{
MethodInfo m = types[i].GetMethod( "Configure", BindingFlags.Static | BindingFlags.Public );
if ( m != null )
if( m != null )
invoke.Add( m );
}
}
invoke.Sort( new CallPriorityComparer() );
for ( int i = 0; i < invoke.Count; ++i )
for( int i = 0; i < invoke.Count; ++i )
invoke[i].Invoke( null, null );
invoke.Clear();
@ -470,20 +577,22 @@ namespace Server {
Region.Load();
World.Load();
for ( int a = 0; a < m_Assemblies.Length; ++a ) {
for( int a = 0; a < m_Assemblies.Length; ++a )
{
Type[] types = m_Assemblies[a].GetTypes();
for ( int i = 0; i < types.Length; ++i ) {
for( int i = 0; i < types.Length; ++i )
{
MethodInfo m = types[i].GetMethod( "Initialize", BindingFlags.Static | BindingFlags.Public );
if ( m != null )
if( m != null )
invoke.Add( m );
}
}
invoke.Sort( new CallPriorityComparer() );
for ( int i = 0; i < invoke.Count; ++i )
for( int i = 0; i < invoke.Count; ++i )
invoke[i].Invoke( null, null );
return true;
@ -492,9 +601,11 @@ namespace Server {
private static Dictionary<Assembly, TypeCache> m_TypeCaches = new Dictionary<Assembly, TypeCache>();
private static TypeCache m_NullCache;
public static TypeCache GetTypeCache( Assembly asm ) {
if ( asm == null ) {
if ( m_NullCache == null )
public static TypeCache GetTypeCache( Assembly asm )
{
if( asm == null )
{
if( m_NullCache == null )
m_NullCache = new TypeCache( null );
return m_NullCache;
@ -503,52 +614,58 @@ namespace Server {
TypeCache c = null;
m_TypeCaches.TryGetValue( asm, out c );
if ( c == null )
if( c == null )
m_TypeCaches[asm] = c = new TypeCache( asm );
return c;
}
public static Type FindTypeByFullName( string fullName ) {
public static Type FindTypeByFullName( string fullName )
{
return FindTypeByFullName( fullName, true );
}
public static Type FindTypeByFullName( string fullName, bool ignoreCase ) {
public static Type FindTypeByFullName( string fullName, bool ignoreCase )
{
Type type = null;
for ( int i = 0; type == null && i < m_Assemblies.Length; ++i )
for( int i = 0; type == null && i < m_Assemblies.Length; ++i )
type = GetTypeCache( m_Assemblies[i] ).GetTypeByFullName( fullName, ignoreCase );
if ( type == null )
if( type == null )
type = GetTypeCache( Core.Assembly ).GetTypeByFullName( fullName, ignoreCase );
return type;
}
public static Type FindTypeByName( string name ) {
public static Type FindTypeByName( string name )
{
return FindTypeByName( name, true );
}
public static Type FindTypeByName( string name, bool ignoreCase ) {
public static Type FindTypeByName( string name, bool ignoreCase )
{
Type type = null;
for ( int i = 0; type == null && i < m_Assemblies.Length; ++i )
for( int i = 0; type == null && i < m_Assemblies.Length; ++i )
type = GetTypeCache( m_Assemblies[i] ).GetTypeByName( name, ignoreCase );
if ( type == null )
if( type == null )
type = GetTypeCache( Core.Assembly ).GetTypeByName( name, ignoreCase );
return type;
}
public static void EnsureDirectory( string dir ) {
public static void EnsureDirectory( string dir )
{
string path = Path.Combine( Core.BaseDirectory, dir );
if ( !Directory.Exists( path ) )
if( !Directory.Exists( path ) )
Directory.CreateDirectory( path );
}
public static string[] GetScripts( string filter ) {
public static string[] GetScripts( string filter )
{
List<string> list = new List<string>();
GetScripts( list, Path.Combine( Core.BaseDirectory, "Scripts" ), filter );
@ -556,15 +673,17 @@ namespace Server {
return list.ToArray();
}
public static void GetScripts( List<string> list, string path, string filter ) {
foreach ( string dir in Directory.GetDirectories( path ) )
public static void GetScripts( List<string> list, string path, string filter )
{
foreach( string dir in Directory.GetDirectories( path ) )
GetScripts( list, dir, filter );
list.AddRange( Directory.GetFiles( path, filter ) );
}
}
public class TypeCache {
public class TypeCache
{
private Type[] m_Types;
private TypeTable m_Names, m_FullNames;
@ -572,16 +691,19 @@ namespace Server {
public TypeTable Names { get { return m_Names; } }
public TypeTable FullNames { get { return m_FullNames; } }
public Type GetTypeByName( string name, bool ignoreCase ) {
public Type GetTypeByName( string name, bool ignoreCase )
{
return m_Names.Get( name, ignoreCase );
}
public Type GetTypeByFullName( string fullName, bool ignoreCase ) {
public Type GetTypeByFullName( string fullName, bool ignoreCase )
{
return m_FullNames.Get( fullName, ignoreCase );
}
public TypeCache( Assembly asm ) {
if ( asm == null )
public TypeCache( Assembly asm )
{
if( asm == null )
m_Types = Type.EmptyTypes;
else
m_Types = asm.GetTypes();
@ -591,20 +713,24 @@ namespace Server {
Type typeofTypeAliasAttribute = typeof( TypeAliasAttribute );
for ( int i = 0; i < m_Types.Length; ++i ) {
for( int i = 0; i < m_Types.Length; ++i )
{
Type type = m_Types[i];
m_Names.Add( type.Name, type );
m_FullNames.Add( type.FullName, type );
if ( type.IsDefined( typeofTypeAliasAttribute, false ) ) {
if( type.IsDefined( typeofTypeAliasAttribute, false ) )
{
object[] attrs = type.GetCustomAttributes( typeofTypeAliasAttribute, false );
if ( attrs != null && attrs.Length > 0 ) {
if( attrs != null && attrs.Length > 0 )
{
TypeAliasAttribute attr = attrs[0] as TypeAliasAttribute;
if ( attr != null ) {
for ( int j = 0; j < attr.Aliases.Length; ++j )
if( attr != null )
{
for( int j = 0; j < attr.Aliases.Length; ++j )
m_FullNames.Add( attr.Aliases[j], type );
}
}
@ -613,18 +739,21 @@ namespace Server {
}
}
public class TypeTable {
public class TypeTable
{
private Dictionary<string, Type> m_Sensitive, m_Insensitive;
public void Add( string key, Type type ) {
public void Add( string key, Type type )
{
m_Sensitive[key] = type;
m_Insensitive[key] = type;
}
public Type Get( string key, bool ignoreCase ) {
public Type Get( string key, bool ignoreCase )
{
Type t = null;
if ( ignoreCase )
if( ignoreCase )
m_Insensitive.TryGetValue( key, out t );
else
m_Sensitive.TryGetValue( key, out t );
@ -632,7 +761,8 @@ namespace Server {
return t;
}
public TypeTable( int capacity ) {
public TypeTable( int capacity )
{
m_Sensitive = new Dictionary<string, Type>( capacity );
m_Insensitive = new Dictionary<string, Type>( capacity, StringComparer.OrdinalIgnoreCase );
}