File: ScriptServer.cs

package info (click to toggle)
cadencii 3.3.9%2Bsvn20110818.r1732-6
  • links: PTS
  • area: main
  • in suites: buster
  • size: 35,916 kB
  • sloc: cs: 160,836; java: 42,449; cpp: 7,605; ansic: 1,728; perl: 1,087; makefile: 236; php: 142; xml: 117; sh: 21
file content (259 lines) | stat: -rw-r--r-- 11,716 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#if ENABLE_SCRIPT
/*
 * ScriptServer.cs
 * Copyright © 2010-2011 kbinani
 *
 * This file is part of org.kbinani.cadencii.
 *
 * org.kbinani.cadencii is free software; you can redistribute it and/or
 * modify it under the terms of the GPLv3 License.
 *
 * org.kbinani.cadencii is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 */
using System;
using org.kbinani.apputil;
using org.kbinani.java.io;
using org.kbinani.java.util;

namespace org.kbinani.cadencii {
    using boolean = System.Boolean;

    /// <summary>
    /// スクリプトを管理するクラス
    /// </summary>
    public static class ScriptServer {
        private static TreeMap<String, ScriptInvoker> scripts = new TreeMap<String, ScriptInvoker>();

        /// <summary>
        /// 指定したIDのスクリプトを再読込みするか、または新規の場合読み込んで追加します。
        /// </summary>
        /// <param name="id"></param>
        public static void reload( String id ) {
            String dir = Utility.getScriptPath();
            String file = fsys.combine( dir, id );
#if DEBUG
            sout.println( "ScriptServer#reload; file=" + file + "; isFileExists(file)=" + fsys.isFileExists( file ) );
#endif
            if ( !fsys.isFileExists( file ) ) {
                return;
            }

            ScriptInvoker si = Utility.loadScript( file );
            scripts.put( id, si );
        }

        /// <summary>
        /// スクリプトを読み込み、コンパイルします。
        /// </summary>
        public static void reload() {
            // 拡張子がcs, txtのファイルを列挙
            String dir = Utility.getScriptPath();
            Vector<String> files = new Vector<String>();
            files.addAll( Arrays.asList( PortUtil.listFiles( dir, ".txt" ) ) );
            files.addAll( Arrays.asList( PortUtil.listFiles( dir, ".cs" ) ) );

            // 既存のスクリプトに無いまたは新しいやつはロード。
            Vector<String> added = new Vector<String>(); //追加または更新が行われたスクリプトのID
            foreach ( String file in files ) {
                String id = PortUtil.getFileName( file );
                double time = PortUtil.getFileLastModified( file );
                added.add( id );

                boolean loadthis = true;
                if ( scripts.containsKey( id ) ) {
                    double otime = scripts.get( id ).fileTimestamp;
                    if ( time <= otime ) {
                        // 前回コンパイルした時点でのスクリプトファイルよりも更新日が同じか古い。
                        loadthis = false;
                    }
                }

                // ロードする処理
                if ( !loadthis ) {
                    continue;
                }

                ScriptInvoker si = Utility.loadScript( file );
                scripts.put( id, si );
            }

            // 削除されたスクリプトがあれば登録を解除する
            boolean changed = true;
            while ( changed ) {
                changed = false;
                for ( Iterator<String> itr = scripts.keySet().iterator(); itr.hasNext(); ) {
                    String id = itr.next();
                    if ( !added.contains( id ) ) {
                        scripts.remove( id );
                        changed = true;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// スクリプトを実行します。
        /// </summary>
        /// <param name="evsd"></param>
        public static boolean invokeScript( String id, VsqFileEx vsq ) {
            ScriptInvoker script_invoker = null;
            if ( scripts.containsKey( id ) ) {
                script_invoker = scripts.get( id );
            } else {
                return false;
            }
            if ( script_invoker != null && script_invoker.scriptDelegate != null ) {
                try {
                    VsqFileEx work = (VsqFileEx)vsq.clone();
                    ScriptReturnStatus ret = ScriptReturnStatus.ERROR;
                    if ( script_invoker.scriptDelegate is EditVsqScriptDelegate ) {
                        boolean b_ret = ((EditVsqScriptDelegate)script_invoker.scriptDelegate).Invoke( work );
                        if ( b_ret ) {
                            ret = ScriptReturnStatus.EDITED;
                        } else {
                            ret = ScriptReturnStatus.ERROR;
                        }
                    } else if ( script_invoker.scriptDelegate is EditVsqScriptDelegateEx ) {
                        boolean b_ret = ((EditVsqScriptDelegateEx)script_invoker.scriptDelegate).Invoke( work );
                        if ( b_ret ) {
                            ret = ScriptReturnStatus.EDITED;
                        } else {
                            ret = ScriptReturnStatus.ERROR;
                        }
                    } else if ( script_invoker.scriptDelegate is EditVsqScriptDelegateWithStatus ) {
                        ret = ((EditVsqScriptDelegateWithStatus)script_invoker.scriptDelegate).Invoke( work );
                    } else if ( script_invoker.scriptDelegate is EditVsqScriptDelegateExWithStatus ) {
                        ret = ((EditVsqScriptDelegateExWithStatus)script_invoker.scriptDelegate).Invoke( work );
                    } else {
                        ret = ScriptReturnStatus.ERROR;
                    }
                    if ( ret == ScriptReturnStatus.ERROR ) {
                        AppManager.showMessageBox( _( "Script aborted" ), "Cadencii", org.kbinani.windows.forms.Utility.MSGBOX_DEFAULT_OPTION, org.kbinani.windows.forms.Utility.MSGBOX_INFORMATION_MESSAGE );
                    } else if ( ret == ScriptReturnStatus.EDITED ) {
                        CadenciiCommand run = VsqFileEx.generateCommandReplace( work );
                        AppManager.editHistory.register( vsq.executeCommand( run ) );
                    }
                    String config_file = configFileNameFromScriptFileName( script_invoker.ScriptFile );
                    FileOutputStream fs = null;
                    boolean delete_xml_when_exit = false; // xmlを消すときtrue
                    try {
                        fs = new FileOutputStream( config_file );
                        script_invoker.Serializer.serialize( fs, null );
                    } catch ( Exception ex ) {
                        serr.println( "AppManager#invokeScript; ex=" + ex );
                        delete_xml_when_exit = true;
                    } finally {
                        if ( fs != null ) {
                            try {
                                fs.close();
                                if ( delete_xml_when_exit ) {
                                    PortUtil.deleteFile( config_file );
                                }
                            } catch ( Exception ex2 ) {
                                serr.println( "AppManager#invokeScript; ex2=" + ex2 );
                            }
                        }
                    }
                    return (ret == ScriptReturnStatus.EDITED);
                } catch ( Exception ex ) {
                    AppManager.showMessageBox( _( "Script runtime error:" ) + " " + ex, _( "Error" ), org.kbinani.windows.forms.Utility.MSGBOX_DEFAULT_OPTION, org.kbinani.windows.forms.Utility.MSGBOX_INFORMATION_MESSAGE );
                    serr.println( "AppManager#invokeScript; ex=" + ex );
                }
            } else {
                AppManager.showMessageBox( _( "Script compilation failed." ), _( "Error" ), org.kbinani.windows.forms.Utility.MSGBOX_DEFAULT_OPTION, org.kbinani.windows.forms.Utility.MSGBOX_WARNING_MESSAGE );
            }
            return false;
        }

        /// <summary>
        /// スクリプトのファイル名から、そのスクリプトの設定ファイルの名前を決定します。
        /// </summary>
        /// <param name="script_file"></param>
        /// <returns></returns>
        public static String configFileNameFromScriptFileName( String script_file ) {
            String dir = fsys.combine( Utility.getApplicationDataPath(), "script" );
            if ( !fsys.isDirectoryExists( dir ) ) {
                PortUtil.createDirectory( dir );
            }
            return fsys.combine( dir, PortUtil.getFileNameWithoutExtension( script_file ) + ".config" );
        }

        private static String _( String id ) {
            return Messaging.getMessage( id );
        }

        /// <summary>
        /// 読み込まれたスクリプトのIDを順に返す反復子を取得します。
        /// </summary>
        /// <returns></returns>
        public static Iterator<String> getScriptIdIterator() {
            return scripts.keySet().iterator();
        }

        /// <summary>
        /// 指定したIDが示すスクリプトの、表示上の名称を取得します。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static String getDisplayName( String id ) {
            if ( scripts.containsKey( id ) ) {
                ScriptInvoker invoker = scripts.get( id );
                if ( invoker.getDisplayNameDelegate != null ) {
                    String ret = "";
                    try {
                        ret = invoker.getDisplayNameDelegate();
                    } catch ( Exception ex ) {
                        serr.println( "ScriptServer#getDisplayName; ex=" + ex );
                        ret = PortUtil.getFileNameWithoutExtension( id );
                    }
                    return ret;
                }
            }
            return PortUtil.getFileNameWithoutExtension( id );
        }

        /// <summary>
        /// 指定したIDが示すスクリプトの、コンパイルした時点でのソースコードの更新日を取得します。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static double getTimestamp( String id ) {
            if ( scripts.containsKey( id ) ) {
                return scripts.get( id ).fileTimestamp;
            } else {
                return 0;
            }
        }

        /// <summary>
        /// 指定したIDが示すスクリプトが利用可能かどうかを表すbool値を取得します。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static boolean isAvailable( String id ) {
            if ( scripts.containsKey( id ) ) {
                return scripts.get( id ).scriptDelegate != null;
            } else {
                return false;
            }
        }

        /// <summary>
        /// 指定したIDが示すスクリプトの、コンパイル時のメッセージを取得します。
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static String getCompileMessage( String id ) {
            if ( scripts.containsKey( id ) ) {
                return scripts.get( id ).ErrorMessage;
            } else {
                return "";
            }
        }
    }

}
#endif