View Single Post
Old 06-09-2008, 07:43 AM   #3 (permalink)
Kitchen_
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