From ac662477b978bb3d5d0a8b2385e5a74328ab5a9c Mon Sep 17 00:00:00 2001 From: Niels Martin Hansen Date: Sun, 10 Jun 2007 01:49:11 +0000 Subject: [PATCH] Support multiple Automation autoload dirs. (I hope. As usual, not tested.) Originally committed to SVN as r1219. --- aegisub/auto4_base.cpp | 71 ++++++++++++++++++++++------------- aegisub/auto4_base.h | 2 +- aegisub/dialog_automation.cpp | 2 +- aegisub/frame_main.cpp | 2 +- 4 files changed, 47 insertions(+), 30 deletions(-) diff --git a/aegisub/auto4_base.cpp b/aegisub/auto4_base.cpp index e0910317b..b44e3dcd0 100644 --- a/aegisub/auto4_base.cpp +++ b/aegisub/auto4_base.cpp @@ -512,7 +512,7 @@ namespace Automation4 { // copied from auto3 include_path.clear(); include_path.EnsureFileAccessible(filename); - wxStringTokenizer toker(Options.AsText(_T("Automation Include Path")), _T("|"), false); + wxStringTokenizer toker(Options.AsText(_T("Automation Include Path")), _T("|"), wxTOKEN_STRTOK); while (toker.HasMoreTokens()) { // todo? make some error reporting here wxFileName path(toker.GetNextToken()); @@ -641,37 +641,45 @@ namespace Automation4 { void AutoloadScriptManager::Reload() { - wxDir dir; - if (!dir.Exists(path)) { - return; - } - if (!dir.Open(path)) { - return; - } - RemoveAll(); int error_count = 0; - wxString fn; - wxFileName script_path(path, _T("")); - bool more = dir.GetFirst(&fn, wxEmptyString, wxDIR_FILES); - while (more) { - script_path.SetName(fn); - try { - Add(ScriptFactory::CreateFromFile(script_path.GetFullPath())); + wxStringTokenizer tok(path, _T("|"), wxTOKEN_STRTOK); + while (tok.HasMoreTokens()) { + wxDir dir; + wxString dirname = tok.GetNextToken(); + if (!dir.Exists(dirname)) { + wxLogWarning(_T("A directory was specified in the Automation autoload path, but it doesn't exist: %s"), dirname.c_str()); + continue; } - catch (const wchar_t *e) { - error_count++; - wxLogError(_T("Error loading Automation script: %s\n%s"), fn.c_str(), e); + if (!dir.Open(dirname)) { + wxLogWarning(_T("Failed to open a directory in the Automation autoload path: %s"), dirname.c_str()); + continue; } - catch (...) { - error_count++; - wxLogError(_T("Error loading Automation script: %s\nUnknown error."), fn.c_str()); + + wxString fn; + wxFileName script_path(path, _T("")); + bool more = dir.GetFirst(&fn, wxEmptyString, wxDIR_FILES); + while (more) { + script_path.SetName(fn); + try { + Script *s = ScriptFactory::CreateFromFile(script_path.GetFullPath(), true); + Add(s); + if (!s->GetLoadedState()) error_count++; + } + 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\nUnknown error."), fn.c_str()); + } + more = dir.GetNext(&fn); } - more = dir.GetNext(&fn); } - if (error_count) { + if (error_count > 0) { wxLogWarning(_T("One or more scripts placed in the Automation autoload directory failed to load\nPlease review the errors above, correct them and use the Reload Autoload dir button in Automation Manager to attempt loading the scripts again.")); } } @@ -699,7 +707,7 @@ namespace Automation4 { for (std::vector::iterator i = factories->begin(); i != factories->end(); ++i) { if (*i == factory) { - throw _T("Automation 4: Attempt to register the same script factory multiple times."); + throw _T("Automation 4: Attempt to register the same script factory multiple times. This should never happen."); } } factories->push_back(factory); @@ -718,7 +726,7 @@ namespace Automation4 { } } - Script* ScriptFactory::CreateFromFile(const wxString &filename) + Script* ScriptFactory::CreateFromFile(const wxString &filename, bool log_errors) { if (!factories) factories = new std::vector(); @@ -726,7 +734,13 @@ namespace Automation4 { for (std::vector::iterator i = factories->begin(); i != factories->end(); ++i) { try { Script *s = (*i)->Produce(filename); - if (s) return s; + 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; + } } 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 @@ -734,6 +748,9 @@ namespace Automation4 { return e; } } + if (log_errors) { + wxLogWarning(_("The file was not recognised as an Automation script: %s"), filename.c_str()); + } return new UnknownScript(filename); } diff --git a/aegisub/auto4_base.h b/aegisub/auto4_base.h index 202fe7df5..e8d9f975f 100644 --- a/aegisub/auto4_base.h +++ b/aegisub/auto4_base.h @@ -345,7 +345,7 @@ namespace Automation4 { static void Register(ScriptFactory *factory); static void Unregister(ScriptFactory *factory); - static Script* CreateFromFile(const wxString &filename); + static Script* CreateFromFile(const wxString &filename, bool log_errors); static const std::vector& GetFactories(); }; diff --git a/aegisub/dialog_automation.cpp b/aegisub/dialog_automation.cpp index 48f4345d2..d2c15fa43 100644 --- a/aegisub/dialog_automation.cpp +++ b/aegisub/dialog_automation.cpp @@ -203,7 +203,7 @@ void DialogAutomation::OnAdd(wxCommandEvent &evt) try { ExtraScriptInfo ei; - ei.script = Automation4::ScriptFactory::CreateFromFile(fname); + ei.script = Automation4::ScriptFactory::CreateFromFile(fname, false); local_manager->Add(ei.script); ei.is_global = false; AddScript(ei); diff --git a/aegisub/frame_main.cpp b/aegisub/frame_main.cpp index fb078ea46..af8abefed 100644 --- a/aegisub/frame_main.cpp +++ b/aegisub/frame_main.cpp @@ -847,7 +847,7 @@ void FrameMain::SynchronizeProject(bool fromSubs) { sfname.MakeAbsolute(basepath); if (sfname.FileExists()) { sfnames = sfname.GetFullPath(); - local_scripts->Add(Automation4::ScriptFactory::CreateFromFile(sfnames)); + local_scripts->Add(Automation4::ScriptFactory::CreateFromFile(sfnames, true)); } else { wxLogWarning(_T("Automation Script referenced could not be found.\nFilename specified: %s%s\nSearched relative to: %s\nResolved filename: %s"), sfnamel.c_str(), sfnames.c_str(), basepath.c_str(), sfname.GetFullPath().c_str());