Commit 19441537 authored by twanvl's avatar twanvl

Fixed error about enumeration of files in local_data_directory; It should now...

Fixed error about enumeration of files in local_data_directory; It should now also find packages from *both* directories.
parent 1a9722fb
......@@ -10,6 +10,8 @@
#include <util/io/package_manager.hpp>
#include <util/alignment.hpp>
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<PackagedP> 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());
......
......@@ -15,6 +15,8 @@
#include <wx/filename.h>
#include <wx/notebook.h>
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<PackagedP> 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);
......
......@@ -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) {
......
......@@ -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<Game>();
else if (fn->GetExt() == _("mse-style")) p = new_intrusive<StyleSheet>();
else if (fn->GetExt() == _("mse-locale")) p = new_intrusive<Locale>();
else if (fn->GetExt() == _("mse-include")) p = new_intrusive<IncludePackage>();
else if (fn->GetExt() == _("mse-symbol-font")) p = new_intrusive<SymbolFont>();
else if (fn->GetExt() == _("mse-export-template")) p = new_intrusive<ExportTemplate>();
if (fn.GetExt() == _("mse-game")) p = new_intrusive<Game>();
else if (fn.GetExt() == _("mse-style")) p = new_intrusive<StyleSheet>();
else if (fn.GetExt() == _("mse-locale")) p = new_intrusive<Locale>();
else if (fn.GetExt() == _("mse-include")) p = new_intrusive<IncludePackage>();
else if (fn.GetExt() == _("mse-symbol-font")) p = new_intrusive<SymbolFont>();
else if (fn.GetExt() == _("mse-export-template")) p = new_intrusive<ExportTemplate>();
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<PackagedP>& 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);
......
......@@ -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<PackagedP>& out);
/// Open a file from a package, with a name encoded as "package/file"
InputStreamP openFileFromPackage(const String& name);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment