Go Back   RunUO - Ultima Online Emulation > RunUO > Core Modifications > Other

Other Cant find a category above, use this one! Core mods not listed above go here!

Reply
 
Thread Tools Display Modes
Old 02-07-2005, 08:26 PM   #1 (permalink)
UOT
Forum Expert
 
UOT's Avatar
 
Join Date: Oct 2002
Location: Naples,FL
Age: 32
Posts: 1,552
Smile Script Compiler to only recompile when changes are made

Even though this isn't really necessary since through out my tests it only takes around 10 seconds to compile the scripts anyway I thought I'd share this. This modification to Compile will check for newer or altered scripts (both .cs and .vb) throughout your scripts folder and cache the script file names to 2 files ScriptCountCS.cnt and ScriptCountVB.cnt to make a quicker comparison for the sebsequent loads. If it finds a Scripts.CS.dll or Scripts.VB.dll in your Scripts\Output \ folder it will first check to see if your script folder has any scripts (for shards that don't want to upload their scripts), if it does have scripts, it will check for newer files and it will load the compiled DLLs if no newer files are found. You can see the timing when you run the server in -debug mode. Even though I used plenty of exception handling and feel this code is safe: USE AT YOUR OWN RISK

Just replace the whole compile (bool debug) function
Code:
public static bool Compile( bool debug )
with the following code.
PHP Code:
        private static bool CacheScriptNames(string typebool debug)
        {
            
string[] files GetScriptstype );
            return 
CacheScriptNamestypefilesdebug );
        }
        
        private static 
bool CacheScriptNames(string typestring[] filesbool debug)
        {
            
DateTime dt DateTime.Now;
            
TimeSpan dt1 = new TimeSpan();
            
            if (
File.Existsstring.Format("ScriptCount{0}.cnt"type == "*.vb" "VB" "CS" )))
            {
                
int size;
                
string[] CachedScripts;
                
FileStream fs null;
                
BinaryReader r null;
                
                try
                {
                    
fs = new FileStreamstring.Format("ScriptCount{0}.cnt"type == "*.vb" "VB" "CS" ),FileMode.OpenFileAccess.Read  );
                    
= new BinaryReader(fs);
                    
size r.ReadInt32();
                    
CachedScripts = new string[size];
                    for (
int i=0;size;i++)
                    {
                        
CachedScripts[i] = r.ReadString();
                    }
                    
r.Close();
                    
fs.Close();
                    if (
debug)
                        
Console.WriteLine("You have {0} {1} script files cached."size.ToString(), type == "*.vb" "VB.NET" "C#" );
                }
                catch
                {
                    if (
!= null)
                        
r.Close();
                    if (
fs != null)
                        
fs.Close();
                    
                    
Console.WriteLine("Error loading script cache...Recreating cache files...Recompiling scripts...");
                    try
                    {
                        
fs = new FileStream(string.Format("ScriptCount{0}.cnt"type == "*.vb" "VB" "CS" ), FileMode.Create  );
                        
BinaryWriter w = new BinaryWriter(fs);
                        
w.Flush();
                        
w.Write( (int) files.Length );
                        for (
int i=0;files.Length;i++)
                        {
                            
w.Write(files[i]);
                        }
                        
w.Close();
                        
fs.Close();
                    }
                    catch (
Exception err)
                    {
                        
Console.WriteLine"An error occured caching files. Error: {0}"err.Message );
                        return 
false;
                    }
                    return 
false;
                }
                if (
size != files.Length)
                {
                    try
                    {
                        
fs = new FileStream(string.Format("ScriptCount{0}.cnt"type == "*.vb" "VB" "CS" ), FileMode.Create  );
                        
BinaryWriter w = new BinaryWriter(fs);
                        
w.Flush();
                        
w.Write( (int) files.Length );
                        for (
int i=0;files.Length;i++)
                        {
                            
w.Write(files[i]);
                        }
                        
w.Close();
                        
fs.Close();
                        if (
debug)
                        {
                            
Console.WriteLine("You now have {0} scripts and previously had {1} scripts."type == "*.vb" "VB" "CS" ,files.Length.ToString(), size.ToString());
                            
dt1 DateTime.Now dt;
                            
Console.WriteLine("Verified {0} scripts in {1}m{2}s.{3}ms."dt1.Minutes dt1.Seconds dt1.Milliseconds );
                        }
                    }
                    catch (
Exception err)
                    {
                        
Console.WriteLine"An error occured caching files. Error: {0}"err.Message );
                        return 
false;
                    }
                    return 
false;
                }
                else
                {
                    
bool Found false;
                    for ( 
int i=0files.Lengthi++ )
                    {
                        for (
int j=0CachedScripts.Lengthj++ )
                        {
                            if (
CachedScripts[j] == files[i])
                            {
                                
Found true;
                                break;
                            }
                        }
                        if (
Found)
                            
Found false;
                        else
                        {
                            if (
debug)
                            {
                                
dt1 DateTime.Now dt;
                                
Console.WriteLine("Verified scripts in {0}m{1}s.{2}ms."dt1.Minutes dt1.Seconds dt1.Milliseconds );
                            }
                            return 
false;
                        }
                    }
                }
                return 
true;
            }
            else
            {
                try
                {
                    
FileStream fs = new FileStream(string.Format("ScriptCount{0}.cnt"type == "*.vb" "VB" "CS" ), FileMode.Create );
                    
BinaryWriter w = new BinaryWriter(fs);
                    
w.Flush();
                    
w.Write( (int) files.Length );
                    for (
int i=0;files.Length;i++)
                    {
                        
w.Write(files[i]);
                    }
                    
w.Close();
                    
fs.Close();
                    if (
debug)
                    {
                        
dt1 DateTime.Now dt;
                        
Console.WriteLine("Verified scripts in {0}m{1}s.{2}ms."dt1.Minutes dt1.Seconds dt1.Milliseconds );
                    }
                }
                catch  ( 
Exception err )
                {
                    
Console.WriteLine"An Error Occurred. Skipping to Recompile... Error: {0}"err.ToString() );
                }
                return 
false;
            }
            return 
true;
        }
        
        private static 
DateTime GetNewest(string typebool debug)
        {
            
DateTime LastModScript DateTime.MinValue;
            
DateTime dt DateTime.Now;
            
TimeSpan dt1 = new TimeSpan();
            
            
string[] files GetScriptstype );
            
            if (
CacheScriptNames(typefilesdebug))
            {
                for (
int i 0;i<files.Length;i++)
                {
                    if (
LastModScript File.GetCreationTime (files[i]))
                        
LastModScript File.GetCreationTime (files[i]);
                    if(
LastModScript File.GetLastWriteTime (files[i]))
                        
LastModScript File.GetLastWriteTime(files[i]);
                }
                if (
debug)
                {
                    
dt1 DateTime.Now dt;
                    
Console.WriteLine("Verified scripts in {0}m{1}s.{2}ms."dt1.Minutes dt1.Seconds dt1.Milliseconds );
                }
                return 
LastModScript;
            }
            if (
debug)
            {
                
dt1 DateTime.Now dt;
                
Console.WriteLine("Verified scripts in {0}m{1}s.{2}ms."dt1.Minutes dt1.Seconds dt1.Milliseconds );
            }
            return 
DateTime.Now;
        }
        
        private static 
bool LoadScriptDLL(string pathstring typeout Assembly scriptsbool debug)
        {
            
scripts null;
            try
            {
                
scripts Assembly.LoadFilepath );
                return 
true;
            }
            catch ( 
Exception err )
            {
                
Console.WriteLine"Unable to load Scripts.{0}.dll. Compiling scripts now.. Error: {1}"type == "*.vb" "VB" "CS" err.ToString() );
                return 
false;
            }
            return 
false;
        }
        
        public static 
void CleanDir(string pathbool debug)
        {
            
DirectoryInfo di = new DirectoryInfo(path);
            
FileInfo[] fi di.GetFiles();
            for ( 
int i=0fi.Lengthi++ )
            {
                if (
debug)
                    
Console.Write"Trying to delete {0}"fi[i].Name );
                try
                {
                    
fi[i].Delete();
                    if (
debug)
                        
Console.WriteLine("...Deleted");
                }
                catch (
Exception err)
                {
                    if (
debug)
                        
Console.WriteLine("...Failed! Error: {0}"err.Message );
                }
            }
        }
        
        public static 
bool Compilebool debug )
        {
            
EnsureDirectory"Scripts/" );
            
EnsureDirectory"Scripts/Output/" );
            
            
DateTime dt DateTime.Now;
            
DateTime ScriptsDLL DateTime.MinValue;
            
DateTime LastModScript DateTime.MinValue;
            
            
string cspath Path.CombineCore.BaseDirectory, @"Scripts\Output\Scripts.CS.dll" );
            
string vbpath Path.CombineCore.BaseDirectory, @"Scripts\Output\Scripts.VB.dll" );
            
bool LoadDLL false;
            
bool Recompile false;
            
bool FoundDLL false;
            
int a 0;
            
Assembly vbscripts null;
            
Assembly csscripts null;
            
            if ( 
File.Exists(cspath) )
            {
                
FoundDLL true;
                
ScriptsDLL File.GetLastWriteTime(cspath);
                if (
debug)
                    
Console.WriteLine("Checking for C# scripts modified after {0}...."ScriptsDLL);
                
                
LastModScript GetNewest("*.cs",debug);
                if ( 
LastModScript ScriptsDLL)
                    
LoadDLL true;
                else
                    
LoadDLL false;
            }
            
            if ( 
File.Exists(vbpath) && LoadDLL )
            {
                
ScriptsDLL File.GetLastWriteTime(vbpath);
                if (
debug)
                    
Console.WriteLine("Checking for VB.NET scripts modified after {0}...."ScriptsDLL);
                
                
LastModScript GetNewest("*.vb",debug);
                if ( 
LastModScript ScriptsDLL)
                    
LoadDLL true;
                else
                    
LoadDLL false;
            }
            else if ( (
GetScripts"*.vb" )).Length )
            {
                
CacheScriptNames("*.vb"debug);
                
LoadDLL false;
            }
            
            if ( 
LoadDLL || ( FoundDLL && (GetScripts"*.cs" )).Length && (GetScripts"*.vb" )).Length 1) )
            {
                if (
debug)
                    
Console.WriteLine("Loading C# scripts .dll");
                
LoadDLL LoadScriptDLL(cspath,"*.cs",out csscriptsdebug);
                if ( 
File.Exists(vbpath) )
                {
                    if (
debug)
                        
Console.WriteLine("Loading VB.NET scripts .dll");
                    
LoadDLL LoadScriptDLL(vbpath,"*.vb",out vbscriptsdebug);
                }
                if ( 
csscripts == null || vbscripts == null )
                    
m_Assemblies = new Assembly[1];
                else
                    
m_Assemblies = new Assembly[2];
                if ( 
csscripts != null )
                    
m_Assemblies[a++] = csscripts;
                
                if ( 
vbscripts != null )
                    
m_Assemblies[a++] = vbscripts;
                
0;
                if (
debug)
                    
Console.WriteLine("No scripts modified after last compile or no scripts found. Loading pre-compiled scripts...");
            }
            
            if (!
LoadDLL)
            {
                if (
debug)
                    
Console.WriteLine("Unable to load script library. Compiling...");
                
CompilerResults csResults nullvbResults null;
                
                if ( 
vbscripts != null )
                    
                    
csscripts null;
                
CleanDir(Path.CombineCore.BaseDirectory, @"Scripts\Output"), debug);
                
                if ( 
m_AdditionalReferences.Count )
                    
m_AdditionalReferences.Clear();
                
                
csResults CompileCSScriptsdebug );
                
                if ( 
csResults == null || !csResults.Errors.HasErrors )
                    
vbResults CompileVBScriptsdebug );
                
                if ( ( 
csResults == null || !csResults.Errors.HasErrors ) && ( vbResults == null || !vbResults.Errors.HasErrors ) && ( vbResults != null || csResults != null ) )
                {
                    if ( 
csResults == null || vbResults == null )
                        
m_Assemblies = new Assembly[1];
                    else
                        
m_Assemblies = new Assembly[2];
                    
                    if ( 
csResults != null )
                        
m_Assemblies[a++] = csResults.CompiledAssembly;
                    
                    if ( 
vbResults != null )
                        
m_Assemblies[a++] = vbResults.CompiledAssembly;
                    
Recompile=true;
                }
                else
                    
Recompile=false;
            }
            if (
LoadDLL || Recompile)
            {
                if (
debug)
                {
                    
TimeSpan dt1 DateTime.Now dt;
                    
Console.WriteLine("Loaded in {0}m{1}s.{2}ms."dt1.Minutes dt1.Seconds dt1.Milliseconds );
                }
                
Console.Write"Scripts: Verifying..." );
                
Core.VerifySerialization();
                
Console.WriteLine"done ({0} items, {1} mobiles)"Core.ScriptItemsCore.ScriptMobiles );
                
                
ArrayList invoke = new ArrayList();
                try
                {
                    for (
a=0;a<m_Assemblies.Length;++a)
                    {
                        
Type[] types m_Assemblies[a].GetTypes();
                        
                        for ( 
int i 0types.Length; ++)
                        {
                            
MethodInfo m types[i].GetMethod"Configure"BindingFlags.Static | BindingFlags.Public );
                            
                            if ( 
!= null )
                                
invoke.Add);
                            
//m.Invoke( null, null );
                        
}
                    }
                    
                    
invoke.Sort( new CallPriorityComparer() );
                    
                    for ( 
int i 0invoke.Count; ++)
                        ((
MethodInfo)invoke[i]).Invokenullnull );
                    
                    
invoke.Clear();
                }
                catch
                {
                    return 
false;
                }
                
World.Load();
                
                for (
a=0;a<m_Assemblies.Length;++a)
                {
                    
Type[] types m_Assemblies[a].GetTypes();
                    
                    for ( 
int i 0types.Length; ++)
                    {
                        
MethodInfo m types[i].GetMethod"Initialize"BindingFlags.Static | BindingFlags.Public );
                        
                        if ( 
!= null )
                            
invoke.Add);
   &nbs