cadencii.PluginLoader.loadScript C# (CSharp) Method

loadScript() public method

指定されたファイルを読み込んでスクリプトをコンパイルします.
public loadScript ( String file ) : ScriptInvoker
file String スクリプトを発動するのに使用するコンテナを返します.
return ScriptInvoker
        public ScriptInvoker loadScript( String file )
        {
            ScriptInvoker ret = new ScriptInvoker();
            ret.ScriptFile = file;
            ret.fileTimestamp = PortUtil.getFileLastModified( file );
            // スクリプトの記述のうち、以下のリストに当てはまる部分は空文字に置換される
            string config_file = ScriptServer.configFileNameFromScriptFileName( file );
            string script = "";
            using ( StreamReader sr = new StreamReader( file ) ) {
                script += sr.ReadToEnd();
            }

            var code = createPluginCode( script );
            ret.ErrorMessage = "";

            List<string> errors = new List<string>();
            Assembly testAssembly = compileScript( code, errors );
            if ( testAssembly == null ) {
                ret.scriptDelegate = null;
                if ( errors.Count == 0 ) {
                    ret.ErrorMessage = "failed compiling";
                } else {
                    for ( int i = 0; i < errors.Count; i++ ) {
                        ret.ErrorMessage += errors[i] + "\r\n";
                    }
                }
                return ret;
            } else {
                foreach ( Type implemented in testAssembly.GetTypes() ) {
                    Object scriptDelegate = null;
                    ScriptDelegateGetDisplayName getDisplayNameDelegate = null;

                    MethodInfo get_displayname_delegate = implemented.GetMethod( "GetDisplayName", new Type[] { } );
                    if ( get_displayname_delegate != null && get_displayname_delegate.IsStatic && get_displayname_delegate.IsPublic ) {
                        if ( get_displayname_delegate.ReturnType.Equals( typeof( String ) ) ) {
                            getDisplayNameDelegate = (ScriptDelegateGetDisplayName)Delegate.CreateDelegate( typeof( ScriptDelegateGetDisplayName ), get_displayname_delegate );
                        }
                    }

                    MethodInfo tmi = implemented.GetMethod( "Edit", new Type[] { typeof( VsqFile ) } );
                    if ( tmi != null && tmi.IsStatic && tmi.IsPublic ) {
                        if ( tmi.ReturnType.Equals( typeof( bool ) ) ) {
                            scriptDelegate = (EditVsqScriptDelegate)Delegate.CreateDelegate( typeof( EditVsqScriptDelegate ), tmi );
                        } else if ( tmi.ReturnType.Equals( typeof( ScriptReturnStatus ) ) ) {
                            scriptDelegate = (EditVsqScriptDelegateWithStatus)Delegate.CreateDelegate( typeof( EditVsqScriptDelegateWithStatus ), tmi );
                        }
                    }
                    tmi = implemented.GetMethod( "Edit", new Type[] { typeof( VsqFileEx ) } );
                    if ( tmi != null && tmi.IsStatic && tmi.IsPublic ) {
                        if ( tmi.ReturnType.Equals( typeof( bool ) ) ) {
                            scriptDelegate = (EditVsqScriptDelegateEx)Delegate.CreateDelegate( typeof( EditVsqScriptDelegateEx ), tmi );
                        } else if ( tmi.ReturnType.Equals( typeof( ScriptReturnStatus ) ) ) {
                            scriptDelegate = (EditVsqScriptDelegateExWithStatus)Delegate.CreateDelegate( typeof( EditVsqScriptDelegateExWithStatus ), tmi );
                        }
                    }
                    if ( scriptDelegate != null ) {
                        ret.ScriptType = implemented;
                        ret.scriptDelegate = scriptDelegate;
                        ret.Serializer = new XmlStaticMemberSerializerEx( implemented );
                        ret.getDisplayNameDelegate = getDisplayNameDelegate;

                        if ( !File.Exists( config_file ) ) {
                            continue;
                        }

                        // 設定ファイルからDeserialize
                        System.IO.FileStream fs = null;
                        bool delete_when_exit = false;
                        try {
                            fs = new System.IO.FileStream( config_file, System.IO.FileMode.Open, System.IO.FileAccess.Read );
                            ret.Serializer.deserialize( fs );
                        } catch ( Exception ex ) {
                            serr.println( "Utility#loadScript; ex=" + ex );
                            Logger.write( typeof( Utility ) + ".loadScript; ex=" + ex + "\n" );
                            delete_when_exit = true;
                        } finally {
                            if ( fs != null ) {
                                try {
                                    fs.Close();
                                    if ( delete_when_exit ) {
                                        System.IO.File.Delete( config_file );
                                    }
                                } catch ( Exception ex2 ) {
                                    serr.println( "Utility#loadScript; ex2=" + ex2 );
                                    Logger.write( typeof( Utility ) + ".loadScritp; ex=" + ex2 + "\n" );
                                }
                            }
                        }
                    } else {
                        ret.ErrorMessage = _( "'Edit' Method not implemented" );
                    }
                }
            }
            return ret;
        }
#endif // ENABLE_SCRIPT

Usage Example

Exemplo n.º 1
0
 public void loadTest()
 {
     PluginLoader.cleanupUnusedAssemblyCache();
     var files =
         from file in PortUtil.listFiles( "./fixture/script", "" )
             where
                 file.EndsWith( ".cs" ) | file.EndsWith( ".txt" )
             select file;
     foreach ( var file in files ) {
         var loader = new PluginLoader();
         ScriptInvoker invoker = null;
         Assert.DoesNotThrow( () => { invoker = loader.loadScript( file ); } );
         Assert.IsNotNull( invoker );
         Console.Error.WriteLine( file + "\n" + invoker.ErrorMessage );
         Assert.IsNotNull( invoker.scriptDelegate );
     }
 }
All Usage Examples Of cadencii.PluginLoader::loadScript