スクリプトを管理するクラス
示例#1
0
        public static void Main(string[] args)
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Thread.GetDomain().UnhandledException += new UnhandledExceptionEventHandler(Cadencii_UnhandledException);

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            // 引数を解釈
            parseArguments(args);
            if (mPrintVersion)
            {
                Console.Write(BAssemblyInfo.fileVersion);
                return;
            }
            string file = mPathVsq;

            Logger.setEnabled(false);
            string logfile = PortUtil.createTempFile() + ".txt";

            Logger.setPath(logfile);
#if DEBUG
            Logger.setEnabled(true);
#endif

#if !DEBUG
            try {
#endif

            // 言語設定を読み込み
            try {
                Messaging.loadMessages();
                // システムのデフォルトの言語を調べる.
                // EditorConfigのコンストラクタは,この判定を自動でやるのでそれを利用
                EditorConfig ec = new EditorConfig();
                Messaging.setLanguage(ec.Language);
            } catch (Exception ex) {
                Logger.write(typeof(FormMain) + ".ctor; ex=" + ex + "\n");
                serr.println("FormMain#.ctor; ex=" + ex);
            }

            // 開発版の場合の警告ダイアログ
            string str_minor = BAssemblyInfo.fileVersionMinor;
            int minor        = 0;
            try {
                minor = int.Parse(str_minor);
            } catch (Exception ex) {
            }

            /*if ((minor % 2) != 0) {
             *  AppManager.showMessageBox(
             *      PortUtil.formatMessage(
             *          _("Info: This is test version of Cadencii version {0}"),
             *          BAssemblyInfo.fileVersionMeasure + "." + (minor + 1)),
             *      "Cadencii_UTAU",
             *      cadencii.windows.forms.Utility.MSGBOX_DEFAULT_OPTION,
             *      cadencii.windows.forms.Utility.MSGBOX_INFORMATION_MESSAGE);
             * }*/

            // スプラッシュを表示するスレッドを開始
#if !MONO
            splashThread = new Thread(new ThreadStart(showSplash));
            splashThread.TrySetApartmentState(ApartmentState.STA);
            splashThread.Start();
#endif

            // AppManagerの初期化
            AppManager.init();

#if ENABLE_SCRIPT
            try {
                ScriptServer.reload();
                PaletteToolServer.init();
            } catch (Exception ex) {
                serr.println("Cadencii::Main; ex=" + ex);
                Logger.write(typeof(Cadencii) + ".Main; ex=" + ex + "\n");
            }
#endif
            AppManager.mMainWindowController = new FormMainController();
            AppManager.mMainWindow           = new FormMain(AppManager.mMainWindowController, file);
#if !MONO
            AppManager.mMainWindow.Load += mainWindow_Load;
#endif
            Application.Run(AppManager.mMainWindow);
#if !DEBUG
        }

        catch (Exception ex) {
            String            str_ex = getExceptionText(ex, 0);
            FormCompileResult dialog = new FormCompileResult(
                _("Failed to launch Cadencii. Please send the exception report to developer"),
                str_ex);
            dialog.Text = _("Error");
            dialog.ShowDialog();
            if (splash != null)
            {
                splash.Invoke(new Action(splash.Close));
            }
            Logger.write(typeof(Cadencii) + ".Main; ex=" + ex + "\n");
        }
#endif
        }
示例#2
0
        /// <summary>
        /// 指定されたファイルを読み込んでスクリプトをコンパイルします.
        /// </summary>
        /// <param name="file">スクリプトを発動するのに使用するコンテナを返します.</param>
        /// <returns></returns>
#if ENABLE_SCRIPT
        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);
        }