From a234b2472816000d665f3f6e5fe69898870b9f50 Mon Sep 17 00:00:00 2001 From: twanvl Date: Thu, 12 Jul 2007 17:37:08 +0000 Subject: [PATCH] Fixed error about enumeration of files in local_data_directory; It should now also find packages from *both* directories. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@559 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/gui/control/package_list.cpp | 40 +++++++------------- src/gui/preferences_window.cpp | 9 +++-- src/main.cpp | 4 -- src/util/io/package_manager.cpp | 64 ++++++++++++++++++++------------ src/util/io/package_manager.hpp | 8 ++-- 5 files changed, 63 insertions(+), 62 deletions(-) diff --git a/src/gui/control/package_list.cpp b/src/gui/control/package_list.cpp index 52ef2e0f..a876833c 100644 --- a/src/gui/control/package_list.cpp +++ b/src/gui/control/package_list.cpp @@ -10,6 +10,8 @@ #include #include +DECLARE_TYPEOF_COLLECTION(PackagedP); + // ----------------------------------------------------------------------------- : PackageList PackageList::PackageList(Window* parent, int id, int direction) @@ -58,32 +60,18 @@ void PackageList::showData(const String& pattern) { // clear packages.clear(); // find matching packages - String f = ::packages.findFirst(pattern); - while (!f.empty()) { - // try to open the package -// try { - PackagedP package = ::packages.openAny(f, true); - // open image - InputStreamP stream = package->openIconFile(); - Image img; - Bitmap bmp; - if (stream && img.LoadFile(*stream)) { - bmp = Bitmap(img); - } - // add to list - packages.push_back(PackageData(package, bmp)); -/* } - // If there are errors we don't add the package to the list - catch (Error e) { - handleError(e, false); - } catch (std::exception e) { - // we don't throw Exceptions ourselfs, so this is probably something serious - handleError(InternalError(String(csconv_(e.what()))), false); - } catch (...) { - handleError(InternalError(_("An unexpected exception occurred, \nplease save your work (use save as to so you don't overwrite things).\n And restart Magic Set Editor")), false); - }*/ - // Next package - f = wxFindNextFile(); + vector matching; + ::packages.findMatching(pattern, matching); + FOR_EACH(p, matching) { + // open image + InputStreamP stream = p->openIconFile(); + Image img; + Bitmap bmp; + if (stream && img.LoadFile(*stream)) { + bmp = Bitmap(img); + } + // add to list + packages.push_back(PackageData(p, bmp)); } // sort list sort(packages.begin(), packages.end(), ComparePackagePosHint()); diff --git a/src/gui/preferences_window.cpp b/src/gui/preferences_window.cpp index 7c0a72a6..9f8ce531 100644 --- a/src/gui/preferences_window.cpp +++ b/src/gui/preferences_window.cpp @@ -15,6 +15,8 @@ #include #include +DECLARE_TYPEOF_COLLECTION(PackagedP); + // ----------------------------------------------------------------------------- : Preferences pages // A page from the preferences dialog @@ -131,16 +133,15 @@ GlobalPreferencesPage::GlobalPreferencesPage(Window* parent) // init controls language = new wxComboBox(this, wxID_ANY, _(""), wxDefaultPosition, wxDefaultSize, 0, nullptr, wxCB_READONLY | wxCB_SORT); // set values - String f = ::packages.findFirst(_("*.mse-locale")); + vector locales; + ::packages.findMatching(_("*.mse-locale"), locales); int n = 0; - while (!f.empty()) { - PackagedP package = packages.openAny(f); + FOR_EACH(package, locales) { language->Append(package->name() + _(": ") + package->full_name, package.get()); if (settings.locale == package->name()) { language->SetSelection(n); } n++; - f = wxFindNextFile(); } // init sizer wxSizer* s = new wxBoxSizer(wxVERTICAL); diff --git a/src/main.cpp b/src/main.cpp index 36304566..70f71e9d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -86,13 +86,11 @@ bool MSE::OnInit() { // Show the symbol editor Window* wnd = new SymbolWindow(nullptr, argv[1]); wnd->Show(); - packages.destroy(); return true; } else if (f.GetExt() == _("mse-set") || f.GetExt() == _("mse") || f.GetExt() == _("set")) { // Show the set window Window* wnd = new SetWindow(nullptr, import_set(argv[1])); wnd->Show(); - packages.destroy(); return true; } else if (f.GetExt() == _("mse-installer")) { // Installer; install it @@ -102,7 +100,6 @@ bool MSE::OnInit() { } else if (arg == _("--symbol-editor")) { Window* wnd = new SymbolWindow(nullptr); wnd->Show(); - packages.destroy(); return true; } else if (arg == _("--create-installer")) { // create an installer @@ -147,7 +144,6 @@ bool MSE::OnInit() { // no command line arguments, or error, show welcome window (new WelcomeWindow())->Show(); - packages.destroy(); return true; } catch (const Error& e) { diff --git a/src/util/io/package_manager.cpp b/src/util/io/package_manager.cpp index b1d1f556..62895ed4 100644 --- a/src/util/io/package_manager.cpp +++ b/src/util/io/package_manager.cpp @@ -38,7 +38,6 @@ PackageManager packages; void PackageManager::init() { // determine data directory global_data_directory = wxStandardPaths::Get().GetDataDir(); - local_data_directory = wxStandardPaths::Get().GetUserDataDir(); // check if this is the actual data directory, especially during debugging, // the data may be higher up: // exe path = mse/build/debug/mse.exe @@ -53,51 +52,68 @@ void PackageManager::init() { } global_data_directory += _("/data"); // It's not an error for the local directory not to exist. + local_data_directory = wxStandardPaths::Get().GetUserDataDir(); local_data_directory += _("/data"); } PackagedP PackageManager::openAny(const String& name, bool just_header) { // Attempt to load local data first. String filename; - wxFileName* fn; + wxFileName fn; if (wxFileName(name).IsRelative()) { - fn = new wxFileName(local_data_directory + _("/") + name); - fn->Normalize(); - filename = fn->GetFullPath(); + // local data dir? + fn.Assign(local_data_directory + _("/") + name); + fn.Normalize(); + filename = fn.GetFullPath(); if (!wxFileExists(filename) && !wxDirExists(filename)) { - delete fn; - fn = new wxFileName(global_data_directory + _("/") + name); - fn->Normalize(); - filename = fn->GetFullPath(); + // global data dir + fn.Assign(global_data_directory + _("/") + name); + fn.Normalize(); + filename = fn.GetFullPath(); } } else { // Absolute filename - fn = new wxFileName(name); - fn->Normalize(); - filename = fn->GetFullPath(); + fn.Assign(name); + fn.Normalize(); + filename = fn.GetFullPath(); } // Is this package already loaded? PackagedP& p = loaded_packages[filename]; if (!p) { // load with the right type, based on extension - if (fn->GetExt() == _("mse-game")) p = new_intrusive(); - else if (fn->GetExt() == _("mse-style")) p = new_intrusive(); - else if (fn->GetExt() == _("mse-locale")) p = new_intrusive(); - else if (fn->GetExt() == _("mse-include")) p = new_intrusive(); - else if (fn->GetExt() == _("mse-symbol-font")) p = new_intrusive(); - else if (fn->GetExt() == _("mse-export-template")) p = new_intrusive(); + if (fn.GetExt() == _("mse-game")) p = new_intrusive(); + else if (fn.GetExt() == _("mse-style")) p = new_intrusive(); + else if (fn.GetExt() == _("mse-locale")) p = new_intrusive(); + else if (fn.GetExt() == _("mse-include")) p = new_intrusive(); + else if (fn.GetExt() == _("mse-symbol-font")) p = new_intrusive(); + else if (fn.GetExt() == _("mse-export-template")) p = new_intrusive(); else { - throw PackageError(_("Unrecognized package type: '") + fn->GetExt() + _("'\nwhile trying to open: ") + name); + throw PackageError(_("Unrecognized package type: '") + fn.GetExt() + _("'\nwhile trying to open: ") + name); } p->open(filename, just_header); } - delete fn; return p; } -String PackageManager::findFirst(const String& pattern) { - String file = wxFindFirstFile(local_data_directory + _("/") + pattern, 0); - return file.IsEmpty() ? wxFindFirstFile(global_data_directory + _("/") + pattern, 0) : file; +void PackageManager::findMatching(const String& pattern, vector& out) { + String file; + // first find local packages + if (wxDirExists(local_data_directory)) { + String file = wxFindFirstFile(local_data_directory + _("/") + pattern, 0); + while (!file.empty()) { + out.push_back(openAny(file, true)); + file = wxFindNextFile(); + } + } + // then global packages not already in the list + file = wxFindFirstFile(global_data_directory + _("/") + pattern, 0); + while (!file.empty()) { + PackagedP p = openAny(file, true); + if (find(out.begin(), out.end(), p) == out.end()) { + out.push_back(p); + } + file = wxFindNextFile(); + } } InputStreamP PackageManager::openFileFromPackage(const String& name) { @@ -114,8 +130,10 @@ InputStreamP PackageManager::openFileFromPackage(const String& name) { } bool PackageManager::checkDependency(const PackageDependency& dep, bool report_errors) { + // try local package String name = local_data_directory + _("/") + dep.package; if (!wxFileExists(name) && !wxDirExists(name)) { + // try global package name = global_data_directory + _("/") + dep.package; if (!wxFileExists(name) && !wxDirExists(name)) { handle_warning(_ERROR_1_("package not found", dep.package),false); diff --git a/src/util/io/package_manager.hpp b/src/util/io/package_manager.hpp index 7c39f083..641b3790 100644 --- a/src/util/io/package_manager.hpp +++ b/src/util/io/package_manager.hpp @@ -61,11 +61,9 @@ class PackageManager { */ PackagedP openAny(const String& name, bool just_header = false); - /// Find a package whos name matches a pattern - /** Find more using wxFindNextFile(). - * If no package is found returns an empty string. - */ - String findFirst(const String& pattern); + /// Find all packages that match a filename pattern, store them in out + /** Only reads the package headers */ + void findMatching(const String& pattern, vector& out); /// Open a file from a package, with a name encoded as "package/file" InputStreamP openFileFromPackage(const String& name);