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.
//
2009-07-29 07:43:02 +02:00
// Aegisub Project http://www.aegisub.org/
2011-09-28 21:48:37 +02:00
# include "auto4_base.h"
2009-09-10 15:06:40 +02:00
# include "ass_file.h"
# include "ass_style.h"
2014-05-23 00:40:16 +02:00
# include "compat.h"
2011-09-28 21:47:40 +02:00
# include "dialog_progress.h"
2011-09-28 21:45:55 +02:00
# include "include/aegisub/context.h"
2013-01-07 02:50:09 +01:00
# include "options.h"
2009-09-10 15:06:40 +02:00
# include "string_codec.h"
2013-01-26 02:57:46 +01:00
# include "subs_controller.h"
2009-09-10 15:06:40 +02:00
2013-01-04 16:01:50 +01:00
# include <libaegisub/dispatch.h>
2014-05-29 00:19:05 +02:00
# include <libaegisub/format.h>
2013-01-04 16:01:50 +01:00
# include <libaegisub/fs.h>
# include <libaegisub/path.h>
2014-04-23 22:53:24 +02:00
# include <libaegisub/make_unique.h>
2015-01-11 17:11:22 +01:00
# include <libaegisub/split.h>
2013-01-04 16:01:50 +01:00
2013-04-30 15:16:59 +02:00
# include <boost/algorithm/string/replace.hpp>
2013-01-04 16:01:50 +01:00
# include <boost/algorithm/string/trim.hpp>
2013-07-09 00:47:55 +02:00
# include <future>
2013-01-04 16:01:50 +01:00
# include <wx/dcmemory.h>
# include <wx/log.h>
# include <wx/sizer.h>
# ifdef __WINDOWS__
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <libaegisub/charset_conv_win.h>
# endif
2006-12-28 22:18:35 +01:00
namespace Automation4 {
2013-01-04 16:01:50 +01:00
bool CalculateTextExtents ( AssStyle * style , std : : string const & 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
2014-07-11 16:11:10 +02:00
auto dc = CreateCompatibleDC ( nullptr ) ;
if ( ! dc ) return false ;
2006-12-28 22:18:35 +01:00
2014-07-11 16:11:10 +02:00
SetMapMode ( dc , MM_TEXT ) ;
LOGFONTW lf = { 0 } ;
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 ;
2014-07-08 18:21:13 +02:00
wcsncpy ( lf . lfFaceName , agi : : charset : : ConvertW ( style - > font ) . c_str ( ) , 31 ) ;
2006-12-28 22:18:35 +01:00
2014-07-11 16:11:10 +02:00
auto font = CreateFontIndirect ( & lf ) ;
if ( ! font ) return false ;
auto old_font = SelectObject ( dc , font ) ;
2012-03-25 06:05:06 +02:00
2013-01-04 16:01:50 +01:00
std : : wstring wtext ( agi : : charset : : ConvertW ( text ) ) ;
2007-01-15 18:02:39 +01:00
if ( spacing ! = 0 ) {
2006-12-28 22:18:35 +01:00
width = 0 ;
2013-01-04 16:01:50 +01:00
for ( auto c : wtext ) {
2014-07-11 16:11:10 +02:00
SIZE sz ;
GetTextExtentPoint32 ( dc , & c , 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 ;
}
2013-01-04 16:01:50 +01:00
}
else {
2014-07-11 16:11:10 +02:00
SIZE sz ;
GetTextExtentPoint32 ( dc , & wtext [ 0 ] , ( int ) wtext . size ( ) , & sz ) ;
2006-12-28 22:18:35 +01:00
width = sz . cx ;
height = sz . cy ;
}
TEXTMETRIC tm ;
2014-07-11 16:11:10 +02:00
GetTextMetrics ( dc , & tm ) ;
2006-12-28 22:18:35 +01:00
descent = tm . tmDescent ;
2014-07-11 16:11:10 +02:00
extlead = tm . tmExternalLeading ;
2006-12-28 22:18:35 +01:00
2014-07-11 16:11:10 +02:00
SelectObject ( dc , old_font ) ;
DeleteObject ( font ) ;
DeleteObject ( dc ) ;
2006-12-28 22:18:35 +01:00
# 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 ,
2013-01-04 16:01:50 +01:00
to_wx ( style - > font ) ,
2006-12-28 22:18:35 +01:00
wxFONTENCODING_SYSTEM ) ; // FIXME! make sure to get the right encoding here, make some translation table between windows and wx encodings
thedc . SetFont ( thefont ) ;
2013-01-04 16:01:50 +01:00
wxString wtext ( to_wx ( text ) ) ;
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?!
2013-01-04 16:01:50 +01:00
for ( auto const & wc : wtext ) {
2006-12-28 22:18:35 +01:00
int a , b , c , d ;
2013-01-04 16:01:50 +01:00
thedc . GetTextExtent ( wc , & a , & b , & c , & d ) ;
double scaling = fontsize / ( double ) ( b > 0 ? b : 1 ) ; // semi-workaround for missing OS/2 table data for scaling
2007-08-16 18:02:52 +02:00
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
2009-09-02 10:45:09 +02:00
wxCoord lwidth , lheight , ldescent , lextlead ;
2013-01-04 16:01:50 +01:00
thedc . GetTextExtent ( wtext , & lwidth , & lheight , & ldescent , & lextlead ) ;
double scaling = fontsize / ( double ) ( lheight > 0 ? lheight : 1 ) ; // semi-workaround for missing OS/2 table data for scaling
2007-08-16 18:02:52 +02:00
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 ;
}
2013-01-04 16:01:50 +01:00
ExportFilter : : ExportFilter ( std : : string const & name , std : : string const & description , int priority )
2011-09-28 21:48:47 +02:00
: AssExportFilter ( name , description , priority )
2006-12-28 22:18:35 +01:00
{
}
2013-01-04 16:01:50 +01:00
std : : string ExportFilter : : GetScriptSettingsIdentifier ( )
2007-04-04 02:01:17 +02:00
{
2014-05-22 03:32:42 +02:00
return inline_string_encode ( GetName ( ) ) ;
2007-04-04 02:01:17 +02:00
}
2011-09-28 21:48:47 +02:00
wxWindow * ExportFilter : : GetConfigDialogWindow ( wxWindow * parent , agi : : Context * c ) {
2014-04-23 01:34:18 +02:00
config_dialog = GenerateConfigDialog ( parent , c ) ;
2011-09-28 21:48:47 +02:00
2012-03-07 02:30:52 +01:00
if ( config_dialog ) {
2014-05-22 03:32:42 +02:00
std : : string const & val = c - > ass - > Properties . automation_settings [ GetScriptSettingsIdentifier ( ) ] ;
2011-09-28 21:48:47 +02:00
if ( ! val . empty ( ) )
2007-04-04 02:01:17 +02:00
config_dialog - > Unserialise ( val ) ;
2011-09-28 21:49:27 +02:00
return config_dialog - > CreateWindow ( parent ) ;
2006-12-28 22:18:35 +01:00
}
2013-11-21 18:13:36 +01:00
return nullptr ;
2011-09-28 21:48:47 +02:00
}
Note: This was done using a script! it's far from perfect but 95% of the work has been done already formatting-wise.
Document all functions, class, struct, union, enum, macro, variable, typedefs. This isn't the actual document in itself but empty documentation using any old documentation if it was there.
This was done using exuberant ctags to get tag info, then a TCL script to parse/remove old comments and convert them into Doxygen-style.
Some notes:
* Anything labeled 'DOCME' needs to be documented, @param and @return have been left blank as it would be annoying to delete the 'DOCME' from every one of those.
* Some multiline comments may have been munged into single line comments
* Leave the /// comments above global variables with a space, if they're harder to read then we'll be less likey to use them.
* Enum comments can go after the enumeration itself '[value] /// comment'
* include/aegisub/*.h haven't been converted yet, this will be done in a later commit
* Some documentation blocks are in the wrong place, in the .h when it should be in the .cpp, or vice versa.
See http://devel.aegisub.org/wiki/Doxygen for some details on Doxygen and a 'style guide'.
Originally committed to SVN as r3312.
2009-07-30 00:59:22 +02:00
2011-09-28 21:48:47 +02:00
void ExportFilter : : LoadSettings ( bool is_default , agi : : Context * c ) {
2014-05-22 03:32:42 +02:00
if ( config_dialog )
c - > ass - > Properties . automation_settings [ GetScriptSettingsIdentifier ( ) ] = config_dialog - > Serialise ( ) ;
2006-12-28 22:18:35 +01:00
}
// ProgressSink
2011-09-28 21:47:40 +02:00
ProgressSink : : ProgressSink ( agi : : ProgressSink * impl , BackgroundScriptRunner * bsr )
: impl ( impl )
, bsr ( bsr )
, trace_level ( OPT_GET ( " Automation/Trace Level " ) - > GetInt ( ) )
2006-12-28 22:18:35 +01:00
{
}
2011-09-28 21:49:27 +02:00
void ProgressSink : : ShowDialog ( ScriptDialog * config_dialog )
2006-12-28 22:18:35 +01:00
{
2013-01-04 16:01:50 +01:00
agi : : dispatch : : Main ( ) . Sync ( [ = ] {
2012-11-26 04:06:18 +01:00
wxDialog w ; // container dialog box
w . SetExtraStyle ( wxWS_EX_VALIDATE_RECURSIVELY ) ;
2013-01-04 16:01:50 +01:00
w . Create ( bsr - > GetParentWindow ( ) , - 1 , to_wx ( bsr - > GetTitle ( ) ) ) ;
2013-11-21 18:13:36 +01:00
auto s = new wxBoxSizer ( wxHORIZONTAL ) ; // sizer for putting contents in
2012-11-26 04:06:18 +01:00
wxWindow * ww = config_dialog - > CreateWindow ( & w ) ; // generate actual dialog contents
s - > Add ( ww , 0 , wxALL , 5 ) ; // add contents to dialog
w . SetSizerAndFit ( s ) ;
w . CenterOnParent ( ) ;
w . ShowModal ( ) ;
} ) ;
2006-12-28 22:18:35 +01:00
}
2012-02-22 05:17:16 +01:00
int ProgressSink : : ShowDialog ( wxDialog * dialog )
{
int ret = 0 ;
2013-01-04 16:01:50 +01:00
agi : : dispatch : : Main ( ) . Sync ( [ & ] { ret = dialog - > ShowModal ( ) ; } ) ;
2012-02-22 05:17:16 +01:00
return ret ;
}
2013-01-04 16:01:50 +01:00
BackgroundScriptRunner : : BackgroundScriptRunner ( wxWindow * parent , std : : string const & title )
: impl ( new DialogProgress ( parent , to_wx ( title ) ) )
2006-12-28 22:18:35 +01:00
{
}
2011-09-28 21:47:40 +02:00
BackgroundScriptRunner : : ~ BackgroundScriptRunner ( )
2006-12-28 22:18:35 +01:00
{
}
2012-09-25 01:35:27 +02:00
void BackgroundScriptRunner : : Run ( std : : function < void ( ProgressSink * ) > task )
2006-12-28 22:18:35 +01:00
{
2012-11-26 04:06:18 +01:00
impl - > Run ( [ & ] ( agi : : ProgressSink * ps ) {
ProgressSink aps ( ps , this ) ;
task ( & aps ) ;
2014-07-21 18:56:24 +02:00
} ) ;
2006-12-28 22:18:35 +01:00
}
2012-02-22 05:17:16 +01:00
wxWindow * BackgroundScriptRunner : : GetParentWindow ( ) const
{
return impl . get ( ) ;
}
2013-01-04 16:01:50 +01:00
std : : string BackgroundScriptRunner : : GetTitle ( ) const
2012-11-26 04:06:18 +01:00
{
2013-01-04 16:01:50 +01:00
return from_wx ( impl - > GetTitle ( ) ) ;
2012-11-26 04:06:18 +01:00
}
2006-12-28 22:18:35 +01:00
// Script
2013-01-04 16:01:50 +01:00
Script : : Script ( agi : : fs : : path const & filename )
2011-09-28 21:48:37 +02:00
: filename ( filename )
2006-12-28 22:18:35 +01:00
{
2013-01-04 16:01:50 +01:00
include_path . emplace_back ( filename . parent_path ( ) ) ;
std : : string include_paths = OPT_GET ( " Path/Automation/Include " ) - > GetString ( ) ;
2015-01-11 17:11:22 +01:00
for ( auto tok : agi : : Split ( include_paths , ' | ' ) ) {
auto path = config : : path - > Decode ( agi : : str ( tok ) ) ;
2013-01-04 16:01:50 +01:00
if ( path . is_absolute ( ) & & agi : : fs : : DirectoryExists ( path ) )
include_path . emplace_back ( std : : move ( path ) ) ;
2006-12-28 22:18:35 +01:00
}
}
// ScriptManager
2013-09-16 15:43:17 +02:00
void ScriptManager : : Add ( std : : unique_ptr < Script > script )
2006-12-28 22:18:35 +01:00
{
2011-09-28 21:48:58 +02:00
if ( find ( scripts . begin ( ) , scripts . end ( ) , script ) = = scripts . end ( ) )
2013-06-08 06:19:40 +02:00
scripts . emplace_back ( std : : move ( script ) ) ;
2011-09-28 21:49:56 +02:00
ScriptsChanged ( ) ;
2006-12-28 22:18:35 +01:00
}
void ScriptManager : : Remove ( Script * script )
{
2013-06-08 06:19:40 +02:00
auto i = find_if ( scripts . begin ( ) , scripts . end ( ) , [ & ] ( std : : unique_ptr < Script > const & s ) { return s . get ( ) = = script ; } ) ;
if ( i ! = scripts . end ( ) )
2011-09-28 21:48:58 +02:00
scripts . erase ( i ) ;
2011-09-28 21:49:56 +02:00
ScriptsChanged ( ) ;
2006-12-28 22:18:35 +01:00
}
2011-09-28 21:48:58 +02:00
void ScriptManager : : RemoveAll ( )
2006-12-28 22:18:35 +01:00
{
2013-06-08 06:19:40 +02:00
scripts . clear ( ) ;
2011-09-28 21:49:56 +02:00
ScriptsChanged ( ) ;
2006-12-28 22:18:35 +01:00
}
2011-10-25 21:02:01 +02:00
void ScriptManager : : Reload ( Script * script )
{
2013-06-08 06:19:40 +02:00
script - > Reload ( ) ;
ScriptsChanged ( ) ;
2011-10-25 21:02:01 +02:00
}
2011-09-28 21:48:47 +02:00
const std : : vector < cmd : : Command * > & ScriptManager : : GetMacros ( )
2006-12-28 22:18:35 +01:00
{
2007-01-03 05:31:17 +01:00
macros . clear ( ) ;
2013-06-08 06:19:40 +02:00
for ( auto & script : scripts ) {
2012-11-04 04:53:03 +01:00
std : : vector < cmd : : Command * > sfs = script - > GetMacros ( ) ;
2011-09-28 21:48:47 +02:00
copy ( sfs . begin ( ) , sfs . end ( ) , back_inserter ( macros ) ) ;
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
2013-11-21 18:13:36 +01:00
AutoloadScriptManager : : AutoloadScriptManager ( std : : string path )
: path ( std : : move ( path ) )
2006-12-28 22:18:35 +01:00
{
Reload ( ) ;
}
void AutoloadScriptManager : : Reload ( )
{
2013-06-08 06:19:40 +02:00
scripts . clear ( ) ;
2006-12-28 22:18:35 +01:00
2013-07-09 00:47:55 +02:00
std : : vector < std : : future < std : : unique_ptr < Script > > > script_futures ;
2006-12-28 22:18:35 +01:00
2015-01-11 17:11:22 +01:00
for ( auto tok : agi : : Split ( path , ' | ' ) ) {
auto dirname = config : : path - > Decode ( agi : : str ( tok ) ) ;
2013-01-04 16:01:50 +01:00
if ( ! agi : : fs : : DirectoryExists ( dirname ) ) continue ;
2007-06-10 03:49:11 +02:00
2013-07-09 00:47:55 +02:00
for ( auto filename : agi : : fs : : DirectoryIterator ( dirname , " *.* " ) )
script_futures . emplace_back ( std : : async ( std : : launch : : async , [ = ] {
return ScriptFactory : : CreateFromFile ( dirname / filename , false , false ) ;
} ) ) ;
}
int error_count = 0 ;
for ( auto & future : script_futures ) {
auto s = future . get ( ) ;
if ( s ) {
if ( ! s - > GetLoadedState ( ) ) + + error_count ;
scripts . emplace_back ( std : : move ( s ) ) ;
2006-12-28 22:18:35 +01:00
}
}
2012-02-22 21:47:24 +01:00
if ( error_count = = 1 ) {
wxLogWarning ( " A script in the Automation autoload directory failed to load. \n Please review the errors, fix them and use the Rescan Autoload Dir button in Automation Manager to load the scripts again. " ) ;
}
else if ( error_count > 1 ) {
wxLogWarning ( " Multiple scripts in the Automation autoload directory failed to load. \n Please review the errors, fix them and use the Rescan Autoload Dir button in Automation Manager to load the scripts again. " ) ;
2006-12-28 22:18:35 +01:00
}
2011-09-28 21:49:56 +02:00
ScriptsChanged ( ) ;
2006-12-28 22:18:35 +01:00
}
2011-09-28 21:45:55 +02:00
LocalScriptManager : : LocalScriptManager ( agi : : Context * c )
: context ( c )
2014-05-22 03:32:42 +02:00
, file_open_connection ( c - > subsController - > AddFileOpenListener ( & LocalScriptManager : : Reload , this ) )
2011-09-28 21:45:55 +02:00
{
2014-05-22 03:32:42 +02:00
AddScriptChangeListener ( & LocalScriptManager : : SaveLoadedList , this ) ;
2011-09-28 21:45:55 +02:00
}
2011-09-28 21:48:58 +02:00
void LocalScriptManager : : Reload ( )
{
2014-12-11 17:19:05 +01:00
bool was_empty = scripts . empty ( ) ;
2013-06-08 06:19:40 +02:00
scripts . clear ( ) ;
2011-09-28 21:45:55 +02:00
2014-05-22 03:32:42 +02:00
auto const & local_scripts = context - > ass - > Properties . automation_scripts ;
2012-03-09 01:23:30 +01:00
if ( local_scripts . empty ( ) ) {
2014-12-11 17:19:05 +01:00
if ( ! was_empty )
ScriptsChanged ( ) ;
2012-03-09 01:23:30 +01:00
return ;
}
2006-12-28 22:18:35 +01:00
2013-01-04 16:01:50 +01:00
auto autobasefn ( OPT_GET ( " Path/Automation/Base " ) - > GetString ( ) ) ;
2015-01-11 17:11:22 +01:00
for ( auto tok : agi : : Split ( local_scripts , ' | ' ) ) {
tok = boost : : trim_copy ( tok ) ;
if ( boost : : size ( tok ) = = 0 ) continue ;
char first_char = tok [ 0 ] ;
std : : string trimmed ( begin ( tok ) + 1 , end ( tok ) ) ;
2011-09-28 21:45:55 +02:00
2013-01-04 16:01:50 +01:00
agi : : fs : : path basepath ;
2011-09-28 21:45:55 +02:00
if ( first_char = = ' ~ ' ) {
2013-01-26 02:57:46 +01:00
basepath = context - > subsController - > Filename ( ) . parent_path ( ) ;
2011-09-28 21:45:55 +02:00
} else if ( first_char = = ' $ ' ) {
basepath = autobasefn ;
} else if ( first_char = = ' / ' ) {
} else {
wxLogWarning ( " Automation Script referenced with unknown location specifier character. \n Location specifier found: %c \n Filename specified: %s " ,
2013-01-04 16:01:50 +01:00
first_char , to_wx ( trimmed ) ) ;
2011-09-28 21:45:55 +02:00
continue ;
}
2013-01-04 16:01:50 +01:00
auto sfname = basepath / trimmed ;
if ( agi : : fs : : FileExists ( sfname ) )
2013-06-08 06:19:40 +02:00
scripts . emplace_back ( Automation4 : : ScriptFactory : : CreateFromFile ( sfname , true ) ) ;
2011-09-28 21:49:56 +02:00
else {
2011-09-28 21:45:55 +02:00
wxLogWarning ( " Automation Script referenced could not be found. \n Filename specified: %c%s \n Searched relative to: %s \n Resolved filename: %s " ,
2013-01-04 16:01:50 +01:00
first_char , to_wx ( trimmed ) , basepath . wstring ( ) , sfname . wstring ( ) ) ;
2011-09-28 21:45:55 +02:00
}
}
2011-09-28 21:49:56 +02:00
ScriptsChanged ( ) ;
2011-09-28 21:45:55 +02:00
}
2014-05-22 03:32:42 +02:00
void LocalScriptManager : : SaveLoadedList ( )
2011-09-28 21:48:58 +02:00
{
2011-09-28 21:45:55 +02:00
// Store Automation script data
// Algorithm:
// 1. If script filename has Automation Base Path as a prefix, the path is relative to that (ie. "$")
// 2. Otherwise try making it relative to the ass filename
// 3. If step 2 failed, or absolute path is shorter than path relative to ass, use absolute path ("/")
// 4. Otherwise, use path relative to ass ("~")
2013-01-04 16:01:50 +01:00
std : : string scripts_string ;
agi : : fs : : path autobasefn ( OPT_GET ( " Path/Automation/Base " ) - > GetString ( ) ) ;
2011-09-28 21:45:55 +02:00
2013-06-08 06:19:40 +02:00
for ( auto & script : GetScripts ( ) ) {
2012-11-04 04:53:03 +01:00
if ( ! scripts_string . empty ( ) )
2011-09-28 21:45:55 +02:00
scripts_string + = " | " ;
2013-01-04 16:01:50 +01:00
auto scriptfn ( script - > GetFilename ( ) . string ( ) ) ;
2015-08-22 03:17:48 +02:00
auto autobase_rel = context - > path - > MakeRelative ( scriptfn , autobasefn ) ;
auto assfile_rel = context - > path - > MakeRelative ( scriptfn , " ?script " ) ;
2011-09-28 21:45:55 +02:00
2013-01-04 16:01:50 +01:00
if ( autobase_rel . string ( ) . size ( ) < = scriptfn . size ( ) & & autobase_rel . string ( ) . size ( ) < = assfile_rel . string ( ) . size ( ) ) {
scriptfn = " $ " + autobase_rel . generic_string ( ) ;
} else if ( assfile_rel . string ( ) . size ( ) < = scriptfn . size ( ) & & assfile_rel . string ( ) . size ( ) < = autobase_rel . string ( ) . size ( ) ) {
scriptfn = " ~ " + assfile_rel . generic_string ( ) ;
2011-09-28 21:45:55 +02:00
} else {
2013-01-04 16:01:50 +01:00
scriptfn = " / " + script - > GetFilename ( ) . generic_string ( ) ;
2011-09-28 21:45:55 +02:00
}
scripts_string + = scriptfn ;
}
2014-05-22 03:32:42 +02:00
context - > ass - > Properties . automation_scripts = std : : move ( scripts_string ) ;
2011-09-28 21:45:55 +02:00
}
2006-12-28 22:18:35 +01:00
// ScriptFactory
2013-11-21 18:13:36 +01:00
ScriptFactory : : ScriptFactory ( std : : string engine_name , std : : string filename_pattern )
: engine_name ( std : : move ( engine_name ) )
, filename_pattern ( std : : move ( filename_pattern ) )
2006-12-28 22:18:35 +01:00
{
}
2013-09-16 15:43:17 +02:00
void ScriptFactory : : Register ( std : : unique_ptr < ScriptFactory > factory )
2006-12-28 22:18:35 +01:00
{
2012-04-27 21:08:17 +02:00
if ( find ( Factories ( ) . begin ( ) , Factories ( ) . end ( ) , factory ) ! = Factories ( ) . end ( ) )
2014-05-29 14:57:27 +02:00
throw agi : : InternalError ( " Automation 4: Attempt to register the same script factory multiple times. This should never happen. " ) ;
2006-12-29 00:03:26 +01:00
2013-06-08 06:19:40 +02:00
Factories ( ) . emplace_back ( std : : move ( factory ) ) ;
2006-12-28 22:18:35 +01:00
}
2013-06-08 06:19:40 +02:00
std : : unique_ptr < Script > ScriptFactory : : CreateFromFile ( agi : : fs : : path const & filename , bool complain_about_unrecognised , bool create_unknown )
2006-12-28 22:18:35 +01:00
{
2013-06-08 06:19:40 +02:00
for ( auto & factory : Factories ( ) ) {
auto s = factory - > Produce ( filename ) ;
2011-09-28 21:46:05 +02:00
if ( s ) {
2013-02-08 00:58:51 +01:00
if ( ! s - > GetLoadedState ( ) ) {
2013-09-21 21:10:37 +02:00
wxLogError ( _ ( " Failed to load Automation script '%s': \n %s " ) , filename . wstring ( ) , s - > GetDescription ( ) ) ;
2012-05-15 16:06:49 +02:00
}
2011-09-28 21:46:05 +02:00
return s ;
2007-01-15 23:19:50 +01:00
}
2007-06-10 03:49:11 +02:00
}
2011-09-28 21:46:05 +02:00
2013-02-08 00:58:51 +01:00
if ( complain_about_unrecognised ) {
2013-01-04 16:01:50 +01:00
wxLogError ( _ ( " The file was not recognised as an Automation script: %s " ) , filename . wstring ( ) ) ;
2012-05-15 16:06:49 +02:00
}
2011-09-28 21:46:05 +02:00
2014-04-23 22:53:24 +02:00
return create_unknown ? agi : : make_unique < UnknownScript > ( filename ) : nullptr ;
2008-01-18 04:45:43 +01:00
}
2013-06-08 06:19:40 +02:00
std : : vector < std : : unique_ptr < ScriptFactory > > & ScriptFactory : : Factories ( )
2006-12-28 22:18:35 +01:00
{
2013-06-08 06:19:40 +02:00
static std : : vector < std : : unique_ptr < ScriptFactory > > factories ;
2012-04-27 21:08:17 +02:00
return factories ;
}
2006-12-29 00:03:26 +01:00
2013-06-08 06:19:40 +02:00
const std : : vector < std : : unique_ptr < ScriptFactory > > & ScriptFactory : : GetFactories ( )
2012-04-27 21:08:17 +02:00
{
return Factories ( ) ;
2006-12-28 22:18:35 +01:00
}
2013-01-04 16:01:50 +01:00
std : : string ScriptFactory : : GetWildcardStr ( )
2011-09-28 21:46:05 +02:00
{
2013-01-04 16:01:50 +01:00
std : : string fnfilter , catchall ;
2013-06-08 06:19:40 +02:00
for ( auto & fact : Factories ( ) ) {
2011-09-28 21:46:05 +02:00
if ( fact - > GetEngineName ( ) . empty ( ) | | fact - > GetFilenamePattern ( ) . empty ( ) )
continue ;
2013-04-30 15:16:59 +02:00
std : : string filter ( fact - > GetFilenamePattern ( ) ) ;
boost : : replace_all ( filter , " , " , " ; " ) ;
2014-05-29 00:19:05 +02:00
fnfilter + = agi : : format ( " %s scripts (%s)|%s| " , fact - > GetEngineName ( ) , fact - > GetFilenamePattern ( ) , filter ) ;
2013-04-30 15:16:59 +02:00
catchall + = filter + " ; " ;
2011-09-28 21:46:05 +02:00
}
2013-01-04 16:01:50 +01:00
fnfilter + = from_wx ( _ ( " All Files " ) ) + " (*.*)|*.* " ;
2012-01-08 02:05:25 +01:00
2011-09-28 21:46:05 +02:00
if ( ! catchall . empty ( ) )
2013-01-04 16:01:50 +01:00
catchall . pop_back ( ) ;
2011-09-28 21:46:05 +02:00
2012-04-27 21:08:17 +02:00
if ( Factories ( ) . size ( ) > 1 )
2013-01-04 16:01:50 +01:00
fnfilter = from_wx ( _ ( " All Supported Formats " ) ) + " | " + catchall + " | " + fnfilter ;
2011-09-28 21:46:05 +02:00
return fnfilter ;
}
2014-05-23 00:40:16 +02:00
std : : string UnknownScript : : GetDescription ( ) const {
return from_wx ( _ ( " File was not recognized as a script " ) ) ;
}
2011-12-28 22:27:06 +01:00
}