Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C#

C# C# Discussion

Reply
 
Thread Tools Display Modes
Old 05-20-2008, 11:45 AM   #1 (permalink)
Lurker
 
Join Date: May 2008
Posts: 3
Default RunUO Like Runtime Compiling

Ive been looking around google after seeing how RunUO Compiles its scripts at runtime, and altho ive found ways to compile say a single file, Ive not been able to find anyway to do something along the lines of how RunUO handel's it,

Has Anyone seen a guide or have any suggestions on how I would add similar functionality to my own applications?

Im not after someone to hold my hand and write a Step by Step guide, (unless some one does happen to have one :P)

But more a general howto/suggestions on how to acheive this.

Note: on another note im now off to go find the RunUO.exe source..
fusspawn is offline   Reply With Quote
Old 05-20-2008, 01:59 PM   #2 (permalink)
Forum Expert
 
mordero's Avatar
 
Join Date: Nov 2003
Location: Illinois, USA
Age: 22
Posts: 2,911
Default

Yeah I was gonna say, just look through the RunUO source and just walk through how it does it.

The site is: Revision 295: /devel
mordero is offline   Reply With Quote
Old 06-09-2008, 06:43 AM   #3 (permalink)
Forum Expert
 
Join Date: Jan 2008
Posts: 286
Default

I would look at ScriptCompiler.cs at the CompileCSSScripts method

Code:
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 )
            {
                Console.WriteLine( "no files found." );
                assembly = null;
                return true;
            }

            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 ) )
                            {
                                byte[] bytes = bin.ReadBytes( hashCode.Length );

                                if( bytes.Length == hashCode.Length )
                                {
                                    bool valid = true;

                                    for( int i = 0; i < bytes.Length; ++i )
                                    {
                                        if( bytes[i] != hashCode[i] )
                                        {
                                            valid = false;
                                            break;
                                        }
                                    }

                                    if( valid )
                                    {
                                        assembly = Assembly.LoadFrom( "Scripts/Output/Scripts.CS.dll" );

                                        if( !m_AdditionalReferences.Contains( assembly.Location ) )
                                        {
                                            m_AdditionalReferences.Add( assembly.Location );
                                        }

                                        Console.WriteLine( "done (cached)" );

                                        return true;
                                    }
                                }
                            }
                        }
                    }
                    catch
                    {
                    }
                }
            }

            DeleteFiles( "Scripts.CS*.dll" );

#if Framework_3_5
            using ( CSharpCodeProvider provider = new CSharpCodeProvider( new Dictionary<string, string>() { { "CompilerVersion", "v3.5" } } ) )
#else
            using ( CSharpCodeProvider provider = new CSharpCodeProvider() )
#endif
            {
                string path = GetUnusedPath( "Scripts.CS" );

                CompilerParameters parms = new CompilerParameters( GetReferenceAssemblies(), path, debug );

                string defines = GetDefines();

                if( defines != null )
                    parms.CompilerOptions = defines;

                if( Core.HaltOnWarning )
                    parms.WarningLevel = 4;

#if !MONO
                CompilerResults results = provider.CompileAssemblyFromFile( parms, files );
#else
                parms.CompilerOptions = String.Format( "{0} /nowarn:169,219,414 /recurse:Scripts/*.cs", parms.CompilerOptions );
                CompilerResults results = provider.CompileAssemblyFromFile( parms, "" );
#endif
                m_AdditionalReferences.Add( path );

                Display( results );

#if !MONO
                if( results.Errors.Count > 0 )
                {
                    assembly = null;
                    return false;
                }
#else
                if( results.Errors.Count > 0 ) {
                    foreach( CompilerError err in results.Errors ) {
                        if ( !err.IsWarning ) {
                            assembly = null;
                            return false;
                        }
                    }
                }
#endif


                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 ) )
                            {
                                bin.Write( hashCode, 0, hashCode.Length );
                            }
                        }
                    }
                    catch
                    {
                    }
                }

                assembly = results.CompiledAssembly;
                return true;
            }
        }
EDIT: Just realized this was a sort-of bump, oh well
__________________

Kitchen_ is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5