2006-12-28 22:18:35 +01:00
// Copyright (c) 2006, Niels Martin Hansen
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// * Neither the name of the Aegisub Group nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// -----------------------------------------------------------------------------
//
// AEGISUB
//
// Website: http://aegisub.cellosoft.com
// Contact: mailto:jiifurusu@gmail.com
//
2008-01-16 19:29:29 +01:00
2009-01-04 07:31:48 +01:00
# include "config.h"
2007-12-31 07:46:22 +01:00
# ifdef WITH_AUTOMATION
2006-12-28 22:18:35 +01:00
# include "auto4_base.h"
# include "ass_style.h"
# include "options.h"
2007-04-04 02:01:17 +02:00
# include "string_codec.h"
# include "ass_file.h"
2007-06-21 02:46:50 +02:00
# include "standard_paths.h"
2006-12-28 22:18:35 +01:00
# include <wx/filename.h>
# include <wx/dir.h>
# include <wx/dialog.h>
# include <wx/gauge.h>
# include <wx/button.h>
# include <wx/stattext.h>
# include <wx/thread.h>
# include <wx/sizer.h>
# include <wx/filefn.h>
# include <wx/tokenzr.h>
2007-09-12 01:22:26 +02:00
# include <wx/dcmemory.h>
# include <wx/msgdlg.h>
# include <wx/log.h>
2006-12-28 22:18:35 +01:00
# ifdef WIN32
# include <windows.h>
2007-01-10 02:36:05 +01:00
# include <tchar.h>
2006-12-28 22:18:35 +01:00
# else
# include <ft2build.h>
# include FT_FREETYPE_H
# endif
namespace Automation4 {
2007-01-15 18:02:39 +01:00
bool CalculateTextExtents ( AssStyle * style , wxString & text , double & width , double & height , double & descent , double & extlead )
2006-12-28 22:18:35 +01:00
{
width = height = descent = extlead = 0 ;
2007-01-15 18:02:39 +01:00
double fontsize = style - > fontsize * 64 ;
double spacing = style - > spacing * 64 ;
2006-12-28 22:18:35 +01:00
# ifdef WIN32
// This is almost copypasta from TextSub
HDC thedc = CreateCompatibleDC ( 0 ) ;
if ( ! thedc ) return false ;
SetMapMode ( thedc , MM_TEXT ) ;
2007-01-10 02:36:05 +01:00
LOGFONTW lf ;
2006-12-28 22:18:35 +01:00
ZeroMemory ( & lf , sizeof ( lf ) ) ;
2007-01-15 16:27:13 +01:00
lf . lfHeight = ( LONG ) fontsize ;
2006-12-28 22:18:35 +01:00
lf . lfWeight = style - > bold ? FW_BOLD : FW_NORMAL ;
lf . lfItalic = style - > italic ;
lf . lfUnderline = style - > underline ;
lf . lfStrikeOut = style - > strikeout ;
lf . lfCharSet = style - > encoding ;
lf . lfOutPrecision = OUT_TT_PRECIS ;
lf . lfClipPrecision = CLIP_DEFAULT_PRECIS ;
lf . lfQuality = ANTIALIASED_QUALITY ;
lf . lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE ;
2007-01-10 02:36:05 +01:00
_tcsncpy ( lf . lfFaceName , style - > font . c_str ( ) , 32 ) ;
2006-12-28 22:18:35 +01:00
HFONT thefont = CreateFontIndirect ( & lf ) ;
if ( ! thefont ) return false ;
SelectObject ( thedc , thefont ) ;
SIZE sz ;
size_t thetextlen = text . length ( ) ;
2007-01-10 02:36:05 +01:00
const TCHAR * thetext = text . wc_str ( ) ;
2007-01-15 18:02:39 +01:00
if ( spacing ! = 0 ) {
2006-12-28 22:18:35 +01:00
width = 0 ;
for ( unsigned int i = 0 ; i < thetextlen ; i + + ) {
GetTextExtentPoint32 ( thedc , & thetext [ i ] , 1 , & sz ) ;
2007-01-15 18:02:39 +01:00
width + = sz . cx + spacing ;
2006-12-28 22:18:35 +01:00
height = sz . cy ;
}
} else {
GetTextExtentPoint32 ( thedc , thetext , ( int ) thetextlen , & sz ) ;
width = sz . cx ;
height = sz . cy ;
}
// HACKISH FIX! This seems to work, but why? It shouldn't be needed?!?
2007-01-15 18:02:39 +01:00
//fontsize = style->fontsize;
//width = (int)(width * fontsize/height + 0.5);
//height = (int)(fontsize + 0.5);
2006-12-28 22:18:35 +01:00
TEXTMETRIC tm ;
GetTextMetrics ( thedc , & tm ) ;
descent = tm . tmDescent ;
extlead = tm . tmExternalLeading ;
DeleteObject ( thedc ) ;
DeleteObject ( thefont ) ;
# else // not WIN32
wxMemoryDC thedc ;
// fix fontsize to be 72 DPI
2007-01-15 18:02:39 +01:00
//fontsize = -FT_MulDiv((int)(fontsize+0.5), 72, thedc.GetPPI().y);
2006-12-28 22:18:35 +01:00
// now try to get a font!
// use the font list to get some caching... (chance is the script will need the same font very often)
// USING wxTheFontList SEEMS TO CAUSE BAD LEAKS!
//wxFont *thefont = wxTheFontList->FindOrCreateFont(
wxFont thefont (
2007-01-15 16:27:13 +01:00
( int ) fontsize ,
2006-12-28 22:18:35 +01:00
wxFONTFAMILY_DEFAULT ,
style - > italic ? wxFONTSTYLE_ITALIC : wxFONTSTYLE_NORMAL ,
style - > bold ? wxFONTWEIGHT_BOLD : wxFONTWEIGHT_NORMAL ,
style - > underline ,
style - > font ,
wxFONTENCODING_SYSTEM ) ; // FIXME! make sure to get the right encoding here, make some translation table between windows and wx encodings
thedc . SetFont ( thefont ) ;
2007-01-15 18:02:39 +01:00
if ( spacing ) {
2006-12-28 22:18:35 +01:00
// If there's inter-character spacing, kerning info must not be used, so calculate width per character
// NOTE: Is kerning actually done either way?!
2007-01-03 19:51:25 +01:00
for ( unsigned int i = 0 ; i < text . length ( ) ; i + + ) {
2006-12-28 22:18:35 +01:00
int a , b , c , d ;
2007-01-03 19:51:25 +01:00
thedc . GetTextExtent ( text [ i ] , & a , & b , & c , & d ) ;
2007-08-16 18:02:52 +02:00
double scaling = fontsize / ( double ) ( b > 0 ? b : 1 ) ; // semi-workaround for missing OS/2 table data for scaling
width + = ( a + spacing ) * scaling ;
height = b > height ? b * scaling : height ;
descent = c > descent ? c * scaling : descent ;
extlead = d > extlead ? d * scaling : extlead ;
2006-12-28 22:18:35 +01:00
}
} else {
// If the inter-character spacing should be zero, kerning info can (and must) be used, so calculate everything in one go
2007-01-24 03:35:09 +01:00
long lwidth , lheight , ldescent , lextlead ;
thedc . GetTextExtent ( text , & lwidth , & lheight , & ldescent , & lextlead ) ;
2007-08-16 18:02:52 +02:00
double scaling = fontsize / ( double ) ( lheight > 0 ? lheight : 1 ) ; // semi-workaround for missing OS/2 table data for scaling
width = lwidth * scaling ; height = lheight * scaling ; descent = ldescent * scaling ; extlead = lextlead * scaling ;
2006-12-28 22:18:35 +01:00
}
# endif
// Compensate for scaling
2007-01-15 18:02:39 +01:00
width = style - > scalex / 100 * width / 64 ;
2007-08-16 18:02:52 +02:00
height = style - > scaley / 100 * height / 64 ;
descent = style - > scaley / 100 * descent / 64 ;
extlead = style - > scaley / 100 * extlead / 64 ;
2006-12-28 22:18:35 +01:00
return true ;
}
// Feature
Feature : : Feature ( ScriptFeatureClass _featureclass , const wxString & _name )
: featureclass ( _featureclass )
, name ( _name )
{
// nothing to do
}
ScriptFeatureClass Feature : : GetClass ( ) const
{
return featureclass ;
}
FeatureMacro * Feature : : AsMacro ( )
{
if ( featureclass = = SCRIPTFEATURE_MACRO )
// For VS, remember to enable building with RTTI, otherwise dynamic_cast<> won't work
return dynamic_cast < FeatureMacro * > ( this ) ;
return 0 ;
}
FeatureFilter * Feature : : AsFilter ( )
{
if ( featureclass = = SCRIPTFEATURE_FILTER )
return dynamic_cast < FeatureFilter * > ( this ) ;
return 0 ;
}
FeatureSubtitleFormat * Feature : : AsSubFormat ( )
{
if ( featureclass = = SCRIPTFEATURE_SUBFORMAT )
return dynamic_cast < FeatureSubtitleFormat * > ( this ) ;
return 0 ;
}
const wxString & Feature : : GetName ( ) const
{
return name ;
}
// FeatureMacro
2007-01-03 05:31:17 +01:00
FeatureMacro : : FeatureMacro ( const wxString & _name , const wxString & _description )
2006-12-28 22:18:35 +01:00
: Feature ( SCRIPTFEATURE_MACRO , _name )
, description ( _description )
{
// nothing to do
}
const wxString & FeatureMacro : : GetDescription ( ) const
{
return description ;
}
// FeatureFilter
FeatureFilter : : FeatureFilter ( const wxString & _name , const wxString & _description , int _priority )
: Feature ( SCRIPTFEATURE_FILTER , _name )
, AssExportFilter ( )
, config_dialog ( 0 )
{
description = _description ; // from AssExportFilter
Register ( _name , _priority ) ;
}
FeatureFilter : : ~ FeatureFilter ( )
{
Unregister ( ) ;
}
2007-04-04 02:01:17 +02:00
wxString FeatureFilter : : GetScriptSettingsIdentifier ( )
{
return inline_string_encode ( wxString : : Format ( _T ( " Automation Settings %s " ) , GetName ( ) . c_str ( ) ) ) ;
}
2006-12-28 22:18:35 +01:00
wxWindow * FeatureFilter : : GetConfigDialogWindow ( wxWindow * parent ) {
if ( config_dialog ) {
delete config_dialog ;
config_dialog = 0 ;
}
2007-01-24 04:54:32 +01:00
if ( ( config_dialog = GenerateConfigDialog ( parent ) ) ! = NULL ) {
2007-04-04 02:01:17 +02:00
wxString val = AssFile : : top - > GetScriptInfo ( GetScriptSettingsIdentifier ( ) ) ;
if ( ! val . IsEmpty ( ) ) {
config_dialog - > Unserialise ( val ) ;
}
2006-12-28 22:18:35 +01:00
return config_dialog - > GetWindow ( parent ) ;
} else {
return 0 ;
}
}
void FeatureFilter : : LoadSettings ( bool IsDefault ) {
if ( config_dialog ) {
config_dialog - > ReadBack ( ) ;
2007-04-04 02:01:17 +02:00
wxString val = config_dialog - > Serialise ( ) ;
if ( ! val . IsEmpty ( ) ) {
AssFile : : top - > SetScriptInfo ( GetScriptSettingsIdentifier ( ) , val ) ;
}
2006-12-28 22:18:35 +01:00
}
}
// FeatureSubtitleFormat
FeatureSubtitleFormat : : FeatureSubtitleFormat ( const wxString & _name , const wxString & _extension )
: Feature ( SCRIPTFEATURE_SUBFORMAT , _name )
, extension ( _extension )
{
// nothing to do
}
const wxString & FeatureSubtitleFormat : : GetExtension ( ) const
{
return extension ;
}
bool FeatureSubtitleFormat : : CanWriteFile ( wxString filename )
{
return ! filename . Right ( extension . Length ( ) ) . CmpNoCase ( extension ) ;
}
bool FeatureSubtitleFormat : : CanReadFile ( wxString filename )
{
return ! filename . Right ( extension . Length ( ) ) . CmpNoCase ( extension ) ;
}
// ShowConfigDialogEvent
const wxEventType EVT_SHOW_CONFIG_DIALOG_t = wxNewEventType ( ) ;
// ScriptConfigDialog
wxWindow * ScriptConfigDialog : : GetWindow ( wxWindow * parent )
{
if ( win ) return win ;
return win = CreateWindow ( parent ) ;
}
void ScriptConfigDialog : : DeleteWindow ( )
{
if ( win ) delete win ;
win = 0 ;
}
2007-04-04 02:01:17 +02:00
wxString ScriptConfigDialog : : Serialise ( )
{
return _T ( " " ) ;
}
2006-12-28 22:18:35 +01:00
// ProgressSink
ProgressSink : : ProgressSink ( wxWindow * parent )
2008-01-24 11:33:32 +01:00
: wxDialog ( parent , - 1 , _T ( " Automation " ) , wxDefaultPosition , wxDefaultSize , wxBORDER_RAISED )
2007-04-04 22:42:44 +02:00
, debug_visible ( false )
, data_updated ( false )
2006-12-28 22:18:35 +01:00
, cancelled ( false )
, has_inited ( false )
, script_finished ( false )
{
// make the controls
progress_display = new wxGauge ( this , - 1 , 1000 , wxDefaultPosition , wxSize ( 300 , 20 ) ) ;
title_display = new wxStaticText ( this , - 1 , _T ( " " ) , wxDefaultPosition , wxDefaultSize , wxALIGN_CENTRE | wxST_NO_AUTORESIZE ) ;
task_display = new wxStaticText ( this , - 1 , _T ( " " ) , wxDefaultPosition , wxDefaultSize , wxALIGN_CENTRE | wxST_NO_AUTORESIZE ) ;
cancel_button = new wxButton ( this , wxID_CANCEL ) ;
debug_output = new wxTextCtrl ( this , - 1 , _T ( " " ) , wxDefaultPosition , wxSize ( 300 , 120 ) , wxTE_MULTILINE | wxTE_READONLY ) ;
// put it in a sizer
sizer = new wxBoxSizer ( wxVERTICAL ) ;
sizer - > Add ( title_display , 0 , wxEXPAND | wxALL , 5 ) ;
sizer - > Add ( progress_display , 0 , wxALL & ~ wxTOP , 5 ) ;
sizer - > Add ( task_display , 0 , wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
sizer - > Add ( cancel_button , 0 , wxALIGN_CENTER | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
sizer - > Add ( debug_output , 0 , wxEXPAND | wxLEFT | wxRIGHT | wxBOTTOM , 5 ) ;
sizer - > Show ( debug_output , false ) ;
// make the title a slightly larger font
wxFont title_font = title_display - > GetFont ( ) ;
int fontsize = title_font . GetPointSize ( ) ;
title_font . SetPointSize ( fontsize + fontsize / 4 + fontsize / 8 ) ;
title_font . SetWeight ( wxFONTWEIGHT_BOLD ) ;
title_display - > SetFont ( title_font ) ;
2007-01-07 08:10:01 +01:00
// Set up a timer to regularly update the status
// It doesn't need an event handler attached, as just a the timer in itself
// will ensure that the idle event is fired
update_timer = new wxTimer ( ) ;
update_timer - > Start ( 50 , false ) ;
2006-12-28 22:18:35 +01:00
sizer - > SetSizeHints ( this ) ;
SetSizer ( sizer ) ;
Center ( ) ;
2007-01-18 21:47:56 +01:00
// Init trace level
trace_level = Options . AsInt ( _T ( " Automation Trace Level " ) ) ;
2006-12-28 22:18:35 +01:00
}
ProgressSink : : ~ ProgressSink ( )
{
2007-01-07 08:10:01 +01:00
delete update_timer ;
2006-12-28 22:18:35 +01:00
}
void ProgressSink : : OnIdle ( wxIdleEvent & evt )
{
// The big glossy "update display" event
DoUpdateDisplay ( ) ;
if ( script_finished ) {
if ( ! debug_visible ) {
2007-02-02 20:40:50 +01:00
EndModal ( 0 ) ;
2006-12-28 22:18:35 +01:00
} else {
cancel_button - > Enable ( true ) ;
cancel_button - > SetLabel ( _ ( " Close " ) ) ;
SetProgress ( 100.0 ) ;
SetTask ( _ ( " Script completed " ) ) ;
}
}
}
void ProgressSink : : DoUpdateDisplay ( )
{
// If debug output isn't handled before the test for script_finished later,
// there might actually be some debug output but the debug_visible flag won't
// be set before the dialog closes itself.
wxMutexLocker lock ( data_mutex ) ;
2007-01-07 08:10:01 +01:00
if ( ! data_updated ) return ;
2006-12-28 22:18:35 +01:00
if ( ! pending_debug_output . IsEmpty ( ) ) {
if ( ! debug_visible ) {
sizer - > Show ( debug_output , true ) ;
Layout ( ) ;
sizer - > Fit ( this ) ;
debug_visible = true ;
}
* debug_output < < pending_debug_output ;
debug_output - > SetInsertionPointEnd ( ) ;
pending_debug_output = _T ( " " ) ;
}
progress_display - > SetValue ( ( int ) ( progress * 10 ) ) ;
task_display - > SetLabel ( task ) ;
title_display - > SetLabel ( title ) ;
2007-01-07 08:10:01 +01:00
data_updated = false ;
2006-12-28 22:18:35 +01:00
}
void ProgressSink : : SetProgress ( float _progress )
{
wxMutexLocker lock ( data_mutex ) ;
progress = _progress ;
2007-01-07 08:10:01 +01:00
data_updated = true ;
2006-12-28 22:18:35 +01:00
}
void ProgressSink : : SetTask ( const wxString & _task )
{
wxMutexLocker lock ( data_mutex ) ;
task = _task ;
2007-01-07 08:10:01 +01:00
data_updated = true ;
2006-12-28 22:18:35 +01:00
}
void ProgressSink : : SetTitle ( const wxString & _title )
{
wxMutexLocker lock ( data_mutex ) ;
title = _title ;
2007-01-07 08:10:01 +01:00
data_updated = true ;
2006-12-28 22:18:35 +01:00
}
void ProgressSink : : AddDebugOutput ( const wxString & msg )
{
wxMutexLocker lock ( data_mutex ) ;
pending_debug_output < < msg ;
2007-01-07 08:10:01 +01:00
data_updated = true ;
2006-12-28 22:18:35 +01:00
}
BEGIN_EVENT_TABLE ( ProgressSink , wxWindow )
EVT_INIT_DIALOG ( ProgressSink : : OnInit )
EVT_BUTTON ( wxID_CANCEL , ProgressSink : : OnCancel )
EVT_IDLE ( ProgressSink : : OnIdle )
EVT_SHOW_CONFIG_DIALOG ( ProgressSink : : OnConfigDialog )
END_EVENT_TABLE ( )
void ProgressSink : : OnInit ( wxInitDialogEvent & evt )
{
has_inited = true ;
}
void ProgressSink : : OnCancel ( wxCommandEvent & evt )
{
if ( ! script_finished ) {
cancelled = true ;
cancel_button - > Enable ( false ) ;
} else {
2007-02-02 20:40:50 +01:00
EndModal ( 0 ) ;
2006-12-28 22:18:35 +01:00
}
}
void ProgressSink : : OnConfigDialog ( ShowConfigDialogEvent & evt )
{
// assume we're in the GUI thread here
DoUpdateDisplay ( ) ;
if ( evt . config_dialog ) {
wxDialog * w = new wxDialog ( this , - 1 , title ) ; // container dialog box
wxBoxSizer * s = new wxBoxSizer ( wxHORIZONTAL ) ; // sizer for putting contents in
wxWindow * ww = evt . config_dialog - > GetWindow ( w ) ; // get/generate actual dialog contents
s - > Add ( ww , 0 , wxALL , 5 ) ; // add contents to dialog
w - > SetSizerAndFit ( s ) ;
w - > CenterOnParent ( ) ;
w - > ShowModal ( ) ;
evt . config_dialog - > ReadBack ( ) ;
evt . config_dialog - > DeleteWindow ( ) ;
delete w ;
} else {
wxMessageBox ( _T ( " Uh... no config dialog? " ) ) ;
}
// See note in auto4_base.h
if ( evt . sync_sema ) {
evt . sync_sema - > Post ( ) ;
}
}
// Script
Script : : Script ( const wxString & _filename )
: filename ( _filename )
, name ( _T ( " " ) )
, description ( _T ( " " ) )
, author ( _T ( " " ) )
, version ( _T ( " " ) )
, loaded ( false )
{
// copied from auto3
include_path . clear ( ) ;
include_path . EnsureFileAccessible ( filename ) ;
2007-06-10 03:49:11 +02:00
wxStringTokenizer toker ( Options . AsText ( _T ( " Automation Include Path " ) ) , _T ( " | " ) , wxTOKEN_STRTOK ) ;
2006-12-28 22:18:35 +01:00
while ( toker . HasMoreTokens ( ) ) {
// todo? make some error reporting here
2007-06-21 02:46:50 +02:00
wxFileName path ( StandardPaths : : DecodePath ( toker . GetNextToken ( ) ) ) ;
2006-12-28 22:18:35 +01:00
if ( ! path . IsOk ( ) ) continue ;
if ( path . IsRelative ( ) ) continue ;
if ( ! path . DirExists ( ) ) continue ;
include_path . Add ( path . GetLongPath ( ) ) ;
}
}
Script : : ~ Script ( )
{
for ( std : : vector < Feature * > : : iterator f = features . begin ( ) ; f ! = features . end ( ) ; + + f ) {
delete * f ;
}
}
2007-02-14 01:43:01 +01:00
wxString Script : : GetPrettyFilename ( ) const
{
wxFileName fn ( filename ) ;
return fn . GetFullName ( ) ;
}
2006-12-28 22:18:35 +01:00
const wxString & Script : : GetFilename ( ) const
{
return filename ;
}
const wxString & Script : : GetName ( ) const
{
return name ;
}
const wxString & Script : : GetDescription ( ) const
{
return description ;
}
const wxString & Script : : GetAuthor ( ) const
{
return author ;
}
const wxString & Script : : GetVersion ( ) const
{
return version ;
}
bool Script : : GetLoadedState ( ) const
{
return loaded ;
}
std : : vector < Feature * > & Script : : GetFeatures ( )
{
return features ;
}
// ScriptManager
ScriptManager : : ScriptManager ( )
{
// do nothing...?
}
ScriptManager : : ~ ScriptManager ( )
{
RemoveAll ( ) ;
}
void ScriptManager : : Add ( Script * script )
{
for ( std : : vector < Script * > : : iterator i = scripts . begin ( ) ; i ! = scripts . end ( ) ; + + i ) {
if ( script = = * i ) return ;
}
scripts . push_back ( script ) ;
}
void ScriptManager : : Remove ( Script * script )
{
for ( std : : vector < Script * > : : iterator i = scripts . begin ( ) ; i ! = scripts . end ( ) ; + + i ) {
if ( script = = * i ) {
delete * i ;
scripts . erase ( i ) ;
return ;
}
}
}
void ScriptManager : : RemoveAll ( )
{
for ( std : : vector < Script * > : : iterator i = scripts . begin ( ) ; i ! = scripts . end ( ) ; + + i ) {
delete * i ;
}
scripts . clear ( ) ;
}
const std : : vector < Script * > & ScriptManager : : GetScripts ( ) const
{
return scripts ;
}
2007-01-03 05:31:17 +01:00
const std : : vector < FeatureMacro * > & ScriptManager : : GetMacros ( )
2006-12-28 22:18:35 +01:00
{
2007-01-03 05:31:17 +01:00
macros . clear ( ) ;
2006-12-28 22:18:35 +01:00
for ( std : : vector < Script * > : : iterator i = scripts . begin ( ) ; i ! = scripts . end ( ) ; + + i ) {
std : : vector < Feature * > & sfs = ( * i ) - > GetFeatures ( ) ;
for ( std : : vector < Feature * > : : iterator j = sfs . begin ( ) ; j ! = sfs . end ( ) ; + + j ) {
FeatureMacro * m = dynamic_cast < FeatureMacro * > ( * j ) ;
if ( ! m ) continue ;
2007-01-03 05:31:17 +01:00
macros . push_back ( m ) ;
2006-12-28 22:18:35 +01:00
}
}
2007-01-03 05:31:17 +01:00
return macros ;
2006-12-28 22:18:35 +01:00
}
// AutoloadScriptManager
AutoloadScriptManager : : AutoloadScriptManager ( const wxString & _path )
: path ( _path )
{
Reload ( ) ;
}
void AutoloadScriptManager : : Reload ( )
{
RemoveAll ( ) ;
int error_count = 0 ;
2007-06-10 03:49:11 +02:00
wxStringTokenizer tok ( path , _T ( " | " ) , wxTOKEN_STRTOK ) ;
while ( tok . HasMoreTokens ( ) ) {
wxDir dir ;
2007-06-21 02:46:50 +02:00
wxString dirname = StandardPaths : : DecodePath ( tok . GetNextToken ( ) ) ;
2007-06-10 03:49:11 +02:00
if ( ! dir . Exists ( dirname ) ) {
2007-06-21 02:46:50 +02:00
//wxLogWarning(_T("A directory was specified in the Automation autoload path, but it doesn't exist: %s"), dirname.c_str());
2007-06-10 03:49:11 +02:00
continue ;
2006-12-28 22:18:35 +01:00
}
2007-06-10 03:49:11 +02:00
if ( ! dir . Open ( dirname ) ) {
2007-06-21 02:46:50 +02:00
//wxLogWarning(_T("Failed to open a directory in the Automation autoload path: %s"), dirname.c_str());
2007-06-10 03:49:11 +02:00
continue ;
2006-12-28 22:18:35 +01:00
}
2007-06-10 03:49:11 +02:00
wxString fn ;
2007-06-21 02:46:50 +02:00
wxFileName script_path ( dirname + _T ( " / " ) , _T ( " " ) ) ;
2007-06-10 03:49:11 +02:00
bool more = dir . GetFirst ( & fn , wxEmptyString , wxDIR_FILES ) ;
while ( more ) {
script_path . SetName ( fn ) ;
try {
2008-01-18 04:45:43 +01:00
wxString fullpath = script_path . GetFullPath ( ) ;
if ( ScriptFactory : : CanHandleScriptFormat ( fullpath ) ) {
Script * s = ScriptFactory : : CreateFromFile ( fullpath , true ) ;
Add ( s ) ;
if ( ! s - > GetLoadedState ( ) ) error_count + + ;
}
2007-06-10 03:49:11 +02:00
}
catch ( const wchar_t * e ) {
error_count + + ;
wxLogError ( _T ( " Error loading Automation script: %s \n %s " ) , fn . c_str ( ) , e ) ;
}
catch ( . . . ) {
error_count + + ;
wxLogError ( _T ( " Error loading Automation script: %s \n Unknown error. " ) , fn . c_str ( ) ) ;
}
more = dir . GetNext ( & fn ) ;
2006-12-28 22:18:35 +01:00
}
}
2007-06-10 03:49:11 +02:00
if ( error_count > 0 ) {
2006-12-28 22:18:35 +01:00
wxLogWarning ( _T ( " One or more scripts placed in the Automation autoload directory failed to load \n Please review the errors above, correct them and use the Reload Autoload dir button in Automation Manager to attempt loading the scripts again. " ) ) ;
}
}
// ScriptFactory
2006-12-29 00:03:26 +01:00
std : : vector < ScriptFactory * > * ScriptFactory : : factories = 0 ;
2006-12-28 22:18:35 +01:00
const wxString & ScriptFactory : : GetEngineName ( ) const
{
return engine_name ;
}
const wxString & ScriptFactory : : GetFilenamePattern ( ) const
{
return filename_pattern ;
}
void ScriptFactory : : Register ( ScriptFactory * factory )
{
2006-12-29 00:03:26 +01:00
if ( ! factories )
factories = new std : : vector < ScriptFactory * > ( ) ;
for ( std : : vector < ScriptFactory * > : : iterator i = factories - > begin ( ) ; i ! = factories - > end ( ) ; + + i ) {
2006-12-28 22:18:35 +01:00
if ( * i = = factory ) {
2007-06-10 03:49:11 +02:00
throw _T ( " Automation 4: Attempt to register the same script factory multiple times. This should never happen. " ) ;
2006-12-28 22:18:35 +01:00
}
}
2006-12-29 00:03:26 +01:00
factories - > push_back ( factory ) ;
2006-12-28 22:18:35 +01:00
}
void ScriptFactory : : Unregister ( ScriptFactory * factory )
{
2006-12-29 00:03:26 +01:00
if ( ! factories )
factories = new std : : vector < ScriptFactory * > ( ) ;
for ( std : : vector < ScriptFactory * > : : iterator i = factories - > begin ( ) ; i ! = factories - > end ( ) ; + + i ) {
2006-12-28 22:18:35 +01:00
if ( * i = = factory ) {
2006-12-29 00:03:26 +01:00
factories - > erase ( i ) ;
2006-12-28 22:18:35 +01:00
return ;
}
}
}
2007-06-10 03:49:11 +02:00
Script * ScriptFactory : : CreateFromFile ( const wxString & filename , bool log_errors )
2006-12-28 22:18:35 +01:00
{
2006-12-29 00:03:26 +01:00
if ( ! factories )
factories = new std : : vector < ScriptFactory * > ( ) ;
for ( std : : vector < ScriptFactory * > : : iterator i = factories - > begin ( ) ; i ! = factories - > end ( ) ; + + i ) {
2007-01-15 23:19:50 +01:00
try {
Script * s = ( * i ) - > Produce ( filename ) ;
2007-06-10 03:49:11 +02:00
if ( s ) {
if ( ! s - > GetLoadedState ( ) & & log_errors ) {
wxLogError ( _ ( " An Automation script failed to load. File name: '%s', error reported: " ) , filename . c_str ( ) ) ;
wxLogError ( s - > GetDescription ( ) ) ;
}
return s ;
}
2007-01-15 23:19:50 +01:00
}
catch ( Script * e ) {
// This was the wrong script factory, but it throwing a Script object means it did know what to do about the file
// Use this script object
return e ;
}
2006-12-28 22:18:35 +01:00
}
2007-06-10 03:49:11 +02:00
if ( log_errors ) {
wxLogWarning ( _ ( " The file was not recognised as an Automation script: %s " ) , filename . c_str ( ) ) ;
}
2006-12-28 22:18:35 +01:00
return new UnknownScript ( filename ) ;
}
2008-01-18 04:45:43 +01:00
bool ScriptFactory : : CanHandleScriptFormat ( const wxString & filename )
{
// Just make this always return true to bitch about unknown script formats in autoload
if ( ! factories )
factories = new std : : vector < ScriptFactory * > ( ) ;
for ( std : : vector < ScriptFactory * > : : iterator i = factories - > begin ( ) ; i ! = factories - > end ( ) ; + + i ) {
wxString pattern = ( * i ) - > GetFilenamePattern ( ) ;
if ( filename . Matches ( pattern ) ) return true ;
}
return false ;
}
2006-12-28 22:18:35 +01:00
const std : : vector < ScriptFactory * > & ScriptFactory : : GetFactories ( )
{
2006-12-29 00:03:26 +01:00
if ( ! factories )
factories = new std : : vector < ScriptFactory * > ( ) ;
return * factories ;
2006-12-28 22:18:35 +01:00
}
// UnknownScript
UnknownScript : : UnknownScript ( const wxString & filename )
: Script ( filename )
{
wxFileName fn ( filename ) ;
name = fn . GetName ( ) ;
description = _ ( " File was not recognized as a script " ) ;
loaded = false ;
}
} ;
2007-12-31 07:46:22 +01:00
# endif // WITH_AUTOMATION