File: PaletteToolServer.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 (180 lines) | stat: -rw-r--r-- 8,307 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
#if ENABLE_SCRIPT
/*
 * PaletteToolServer.cs
 * Copyright © 2009-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 System.CodeDom.Compiler;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using org.kbinani.apputil;
using org.kbinani.java.util;
using org.kbinani.vsq;
using org.kbinani.xml;

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

    /// <summary>
    /// パレットツールを一元管理するクラス
    /// </summary>
    public static class PaletteToolServer {
        /// <summary>
        /// 読み込まれたパレットツールのコレクション
        /// </summary>
        public static TreeMap<String, Object> loadedTools = new TreeMap<String, Object>();

        /// <summary>
        /// パレットツールを読み込みます
        /// </summary>
        public static void init() {
            String path = Utility.getToolPath();
            if ( !Directory.Exists( path ) ) {
                return;
            }

            FileInfo[] files = new DirectoryInfo( path ).GetFiles( "*.txt" );
            foreach ( FileInfo file in files ) {
                String code = "";
                StreamReader sr = null;
                try {
                    sr = new StreamReader( file.FullName );
                    code += sr.ReadToEnd();
                } catch ( Exception ex ) {
                    Logger.write( typeof( PaletteToolServer ) + ".init; ex=" + ex + "\n" );
                } finally {
                    if ( sr != null ) {
                        try {
                            sr.Close();
                        } catch ( Exception ex2 ) {
                            Logger.write( typeof( PaletteToolServer ) + ".init; ex=" + ex2 + "\n" );
                        }
                    }
                }

                Assembly asm = null;
                Vector<String> errors = new Vector<String>();
                try {
                    asm = Utility.compileScript( code, errors );
                } catch ( Exception ex ) {
                    serr.println( "PaletteToolServer#init; ex=" + ex );
                    asm = null;
                }
                if ( asm == null ) {
                    continue;
                }

                if ( asm == null ) {
                    continue;
                }

                foreach ( Type t in asm.GetTypes() ) {
                    if ( t.IsClass && t.IsPublic && !t.IsAbstract && t.GetInterface( typeof( IPaletteTool ).FullName ) != null ) {
                        try {
#if DEBUG
                            AppManager.debugWriteLine( "t.FullName=" + t.FullName );
#endif
                            Object instance = asm.CreateInstance( t.FullName );
                            String dir = Path.Combine( Utility.getApplicationDataPath(), "tool" );
                            String cfg = Path.GetFileNameWithoutExtension( file.FullName ) + ".config";
                            String config = Path.Combine( dir, cfg );
                            if ( fsys.isFileExists( config ) ) {
                                XmlStaticMemberSerializer xsms = new XmlStaticMemberSerializer( instance.GetType() );
                                FileStream fs = null;
                                boolean errorOnDeserialize = false;
                                try {
                                    fs = new FileStream( config, FileMode.Open, FileAccess.Read );
                                    try {
                                        xsms.Deserialize( fs );
                                    } catch ( Exception ex ) {
                                        errorOnDeserialize = true;
                                        serr.println( "PaletteToolServer#init; ex=" + ex );
                                    }
                                } catch ( Exception ex ) {
                                    serr.println( "PaletteToolServer#init; ex=" + ex );
                                } finally {
                                    if ( fs != null ) {
                                        try {
                                            fs.Close();
                                        } catch ( Exception ex2 ) {
                                            serr.println( "PaletteToolServer#init; ex2=" + ex2 );
                                        }
                                    }
                                }
                                if ( errorOnDeserialize ) {
                                    try {
                                        PortUtil.deleteFile( config );
                                    } catch ( Exception ex ) {
                                        serr.println( "PaletteToolServer#init; ex=" + ex );
                                    }
                                }
                            }
                            String id = Path.GetFileNameWithoutExtension( file.FullName );
                            loadedTools.put( id, instance );
                        } catch ( Exception ex ) {
                            serr.println( "PlaetteToolServer#init; ex=" + ex );
                        }
                    }
                }
            }
        }

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

        /// <summary>
        /// パレットツールを実行します
        /// </summary>
        /// <param name="id">実行するパレットツールのID</param>
        /// <param name="track">編集対象のトラック番号</param>
        /// <param name="vsq_event_intrenal_ids">編集対象のInternalIDのリスト</param>
        /// <param name="button">パレットツールが押し下げられた時のマウスボタンの種類</param>
        /// <returns>パレットツールによって編集が加えられた場合true。そうでなければfalse(パレットツールがエラーを起こした場合も含む)。</returns>
        public static boolean invokePaletteTool( String id, int track, int[] vsq_event_intrenal_ids, MouseButtons button ) {
            if ( loadedTools.containsKey( id ) ) {
                VsqFileEx vsq = AppManager.getVsqFile();
                VsqTrack item = (VsqTrack)vsq.Track.get( track ).clone();
                Object objPal = loadedTools.get( id );
                if ( objPal == null ) {
                    return false;
                }
                if ( !(objPal is IPaletteTool) ) {
                    return false;
                }
                IPaletteTool pal = (IPaletteTool)objPal;
                boolean edited = false;
                try {
                    edited = pal.edit( item, vsq_event_intrenal_ids, button );
                } catch ( Exception ex ) {
                    AppManager.showMessageBox(
                        PortUtil.formatMessage( _( "Palette tool '{0}' reported an error.\nPlease copy the exception text and report it to developper." ), id ),
                        "Error",
                        org.kbinani.windows.forms.Utility.MSGBOX_DEFAULT_OPTION,
                        org.kbinani.windows.forms.Utility.MSGBOX_ERROR_MESSAGE );
                    serr.println( typeof( PaletteToolServer ) + ".invokePaletteTool; ex=" + ex );
                    edited = false;
                }
                if ( edited ) {
                    CadenciiCommand run = VsqFileEx.generateCommandTrackReplace( track, item, vsq.AttachedCurves.get( track - 1 ) );
                    AppManager.editHistory.register( vsq.executeCommand( run ) );
                }
                return edited;
            } else {
                return false;
            }
        }
    }

}
#endif