Finally got precompiled headers to work.

Now all C++ files need to #include <util/prec.hpp>
 That is why all .cpp files are touched by this commit

Many changes to installers and update checking:
     - the window is now called PackagesWindow, in a new source file
     - update checking is now independent from the PackagesWindow. For update checking only a list of package versions are needed (vector<PackageDependency>). This is much less information to download at each startup.
     - the list of available packages is now a list of available Installers, since an installer can contain multiple packages.
     - moved the logic of dependency checking etc. to data/installer
     - moved the actual installation to util/io/package_manager
     - moved directory iteration/creation logic to util/file_utils
     - added PackageDirectory: the local and global package directory now have their own object (was part of PackageManager)
     - added PackageVersion: for detecting if a package has been modified after it was installed.
     - added PackageDescription: description/header of a package. Basicly the same as what Packaged provides.
     - added DownloadableInstaller: where to find an insaller, what does it contain?
     - added InstallablePackage: brining it all together: installer, package, status, action.

Current status: the insaller is currently broken in a few places, more on that soon.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@792 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
twanvl
2007-12-29 18:30:41 +00:00
parent 361488b4fe
commit d2196eea09
182 changed files with 2508 additions and 809 deletions
+328 -50
View File
@@ -6,66 +6,58 @@
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <util/io/package_manager.hpp>
#include <util/error.hpp>
#include <util/file_utils.hpp>
#include <data/game.hpp>
#include <data/stylesheet.hpp>
#include <data/symbol_font.hpp>
#include <data/locale.hpp>
#include <data/export_template.hpp>
#include <data/installer.hpp>
#include <wx/stdpaths.h>
#include <wx/wfstream.h>
// ----------------------------------------------------------------------------- : PackageManager
DECLARE_TYPEOF_COLLECTION(InstallablePackageP);
DECLARE_TYPEOF_COLLECTION(PackageVersion::FileInfo);
// ----------------------------------------------------------------------------- : PackageManager : in memory
PackageManager packages;
void PackageManager::init() {
// determine data directory
global_data_directory = wxStandardPaths::Get().GetDataDir();
// check if this is the actual data directory, especially during debugging,
// the data may be higher up:
// exe path = mse/build/debug/mse.exe
// data path = mse/data
while (!wxDirExists(global_data_directory + _("/data"))) {
String d = global_data_directory;
global_data_directory = wxPathOnly(global_data_directory);
if (d == global_data_directory) {
// we are at the root -> 'data' not found anywhere in the path -> fatal error
throw Error(_("The global MSE data files can not be found, there should be a directory called 'data' with these files. The expected directory to find it in was ") + wxStandardPaths::Get().GetDataDir());
}
}
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");
local.init(true);
global.init(false);
}
void PackageManager::destroy() {
loaded_packages.clear();
}
void PackageManager::reset() {
loaded_packages.clear();
}
PackagedP PackageManager::openAny(const String& name, bool just_header) {
PackagedP PackageManager::openAny(const String& name_, bool just_header) {
String name = trim(name_);
// Attempt to load local data first.
String filename;
wxFileName fn;
if (wxFileName(name).IsRelative()) {
// local data dir?
fn.Assign(local_data_directory + _("/") + name);
fn.Normalize();
filename = fn.GetFullPath();
filename = normalize_filename(local.name(name));
if (!wxFileExists(filename) && !wxDirExists(filename)) {
// global data dir
fn.Assign(global_data_directory + _("/") + name);
fn.Normalize();
filename = fn.GetFullPath();
filename = normalize_filename(global.name(name));
}
} else { // Absolute filename
fn.Assign(name);
fn.Normalize();
filename = fn.GetFullPath();
filename = normalize_filename(name);
}
// Is this package already loaded?
PackagedP& p = loaded_packages[filename];
if (!p) {
// load with the right type, based on extension
wxFileName fn(filename);
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>();
@@ -76,22 +68,21 @@ PackagedP PackageManager::openAny(const String& name, bool just_header) {
throw PackageError(_("Unrecognized package type: '") + fn.GetExt() + _("'\nwhile trying to open: ") + name);
}
p->open(filename, just_header);
} else if (!just_header) {
p->loadFully();
}
return p;
}
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();
}
String file = local.findFirstMatching(pattern);
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);
file = global.findFirstMatching(pattern);
while (!file.empty()) {
PackagedP p = openAny(file, true);
if (find(out.begin(), out.end(), p) == out.end()) {
@@ -121,17 +112,21 @@ InputStreamP PackageManager::openFileFromPackage(Packaged*& package, const Strin
throw FileNotFoundError(name, _("No package name specified, use '/package/filename'"));
}
// ----------------------------------------------------------------------------- : PackageManager : on disk
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)) {
if (report_errors)
handle_warning(_ERROR_1_("package not found", dep.package),false);
return false;
// mse package?
if (dep.package == mse_package) {
if (app_version < dep.version) {
handle_warning(_ERROR_3_("package out of date", _("Magic Set Editor"), app_version.toString(), dep.version.toString()),false);
}
return true;
}
// does the package exist?
if (!local.exists(dep.package) && !global.exists(dep.package)) {
if (report_errors)
handle_warning(_ERROR_1_("package not found", dep.package),false);
return false;
}
PackagedP package = openAny(dep.package, true);
if (package->version < dep.version) {
@@ -141,7 +136,290 @@ bool PackageManager::checkDependency(const PackageDependency& dep, bool report_e
}
return true;
}
void PackageManager::destroy() {
loaded_packages.clear();
bool PackageManager::installedVersion(const String& package_name, Version& version_out) {
if (package_name == mse_package) {
version_out = app_version;
return true;
} else {
if (!local.exists(package_name) && !global.exists(package_name)) return false;
PackagedP package = openAny(package_name, true);
version_out = package->version;
return true;
}
}
void PackageManager::installedPackages(vector<InstallablePackageP>& packages) {
// from directories
vector<InstallablePackageP> more_packages;
global.installedPackages(packages);
local.installedPackages(more_packages);
merge(packages, more_packages);
// the magic appliation package
packages.push_back(mse_installable_package());
}
void PackageManager::install(const InstallablePackage& package) {
bool install_local = package.action & PACKAGE_LOCAL;
(install_local ? local : global).install(package);
}
// ----------------------------------------------------------------------------- : PackageDirectory
void PackageDirectory::init(bool local) {
is_local = local;
if (local) {
init(wxStandardPaths::Get().GetUserDataDir() + _("/data"));
} else {
// determine data directory
String dir = wxStandardPaths::Get().GetDataDir();
// check if this is the actual data directory, especially during debugging,
// the data may be higher up:
// exe path = mse/build/debug/mse.exe
// data path = mse/data
while (!wxDirExists(dir + _("/data"))) {
String d = dir;
dir = wxPathOnly(dir);
if (d == dir) {
// we are at the root -> 'data' not found anywhere in the path -> fatal error
throw Error(_("The global MSE data files can not be found, there should be a directory called 'data' with these files. The expected place to find it in was ") + wxStandardPaths::Get().GetDataDir());
}
}
init(dir + _("/data"));
}
}
void PackageDirectory::init(const String& dir) {
directory = dir;
}
String PackageDirectory::name(const String& name) const {
return directory + _("/") + name;
}
bool PackageDirectory::exists(const String& filename) const {
String fn = name(filename);
return wxFileExists(fn) || wxDirExists(fn);
}
String PackageDirectory::findFirstMatching(const String& pattern) const {
if (!wxDirExists(directory)) return String();
return wxFindFirstFile(directory + _("/") + pattern, 0);
}
bool compare_name(const PackageVersionP& a, const PackageVersionP& b) {
return a->name < b->name;
}
void PackageDirectory::installedPackages(vector<InstallablePackageP>& packages_out) {
loadDatabase();
// find all package files
vector<String> in_dir;
for (String s = findFirstMatching(_("*.mse-*")) ; !s.empty() ; s = wxFindNextFile()) {
size_t pos = s.find_last_of(_("/\\"));
if (pos != String::npos) s = s.substr(pos+1);
// TODO : check for valid package names
in_dir.push_back(s);
}
sort(in_dir.begin(), in_dir.end());
// merge with package database
bool db_changed = false;
vector<PackageVersionP>::const_iterator it1 = packages.begin();
vector<String>::const_iterator it2 = in_dir.begin();
while (it2 != in_dir.end()) {
if (it1 == packages.end() || (*it1)->name > *it2) {
// add new package to db
try {
PackagedP pack = ::packages.openAny(*it2, true);
db_changed = true;
PackageVersionP ver(new PackageVersion(
is_local ? PackageVersion::STATUS_LOCAL : PackageVersion::STATUS_GLOBAL));
ver->check_status(*pack);
packages_out.push_back(new_intrusive2<InstallablePackage>(ver, new_intrusive1<PackageDescription>(*pack)));
} catch (const Error&) {}
++it2;
} else if ((*it1)->name < *it2) {
// delete package from db
db_changed = true;
++it1;
} else {
// ok, a package already in the db
try {
PackagedP pack = ::packages.openAny(*it2, true);
(*it1)->check_status(*pack);
packages_out.push_back(new_intrusive2<InstallablePackage>(*it1, new_intrusive1<PackageDescription>(*pack)));
} catch (const Error&) { db_changed = true; }
++it1, ++it2;
}
}
if (it1 != packages.end()) db_changed = true;
// has the database of installed packages changed?
if (db_changed) {
packages.clear();
FOR_EACH(p, packages_out) {
if (p->installed) packages.push_back(p->installed);
}
saveDatabase();
}
}
IMPLEMENT_REFLECTION(PackageDirectory) {
REFLECT(packages);
}
void PackageDirectory::loadDatabase() {
if (!packages.empty()) return;
String filename = databaseFile();
if (wxFileExists(filename)) {
// packages file not existing is not an error
shared_ptr<wxFileInputStream> file = new_shared1<wxFileInputStream>(filename);
if (!file->Ok()) return; // failure is not an error
Reader reader(file, nullptr, filename);
reader.handle_greedy(*this);
sort(packages.begin(), packages.end(), compare_name);
}
}
void PackageDirectory::saveDatabase() {
Writer writer(new_shared1<wxFileOutputStream>(databaseFile()));
writer.handle(*this);
}
String PackageDirectory::databaseFile() {
return name(_("packages"));
}
// ----------------------------------------------------------------------------- : PackageDirectory : installing
bool PackageDirectory::install(const InstallablePackage& package) {
String n = name(package.description->name);
if (package.action & PACKAGE_REMOVE) {
remove_file_or_dir(n);
} else if ((package.action & PACKAGE_UPGRADE) || (package.action & PACKAGE_INSTALL)) {
remove_file_or_dir(n + _(".new"));
bool ok = actual_install(package, n + _(".new"));
if (!ok) return false;
move_ignored_files(n, n + _(".new"));
remove_file_or_dir(n);
rename_file_or_dir(n + _(".new"), n);
}
return true;
}
bool PackageDirectory::actual_install(const InstallablePackage& package, const String& install_dir) {
String name = package.description->name;
if (!package.installer->installer) {
handle_warning(_("Installer not found for package: ") + name);
return false;
}
Installer& installer = *package.installer->installer;
// install files
const Packaged::FileInfos& file_infos = installer.getFileInfos();
for (Packaged::FileInfos::const_iterator it = file_infos.begin() ; it != file_infos.end() ; ++it) {
String file = it->first;
if (!is_substr(file,0,name)) continue; // not the right package
// correct filename
file = install_dir + file.substr(name.length());
create_parent_dirs(file);
// copy file
InputStreamP is = installer.openIn(file);
wxFileOutputStream os (install_dir + _("/") + file);
if (!os.IsOk()) {
int act = wxMessageBox(_ERROR_1_("cannot create file", file), _TITLE_("cannot create file"), wxICON_ERROR | wxYES_NO);
if (act == wxNO) return false;
}
os.Write(*is);
}
// update package database
return true;
}
// ----------------------------------------------------------------------------- : PackageVersion
template <> void Writer::handle(const PackageVersion::FileInfo& f) {
if (f.status == PackageVersion::FILE_DELETED) {
handle(_("D ") + f.file);
} else {
handle(format_string(_("%s%s %s"),
f.status == PackageVersion::FILE_ADDED ? _("A")
: f.status == PackageVersion::FILE_MODIFIED ? _("M") : _(""),
f.time.Format(_("%Y%m%dT%H%M%S")),
f.file));
}
}
template <> void Reader::handle(PackageVersion::FileInfo& f) {
String s; handle(s);
// read status
if (s.size() < 2) {f.status = PackageVersion::FILE_ADDED; return; }
f.status = s.GetChar(0) == _('M') ? PackageVersion::FILE_MODIFIED
: s.GetChar(0) == _('A') ? PackageVersion::FILE_ADDED
: s.GetChar(0) == _('D') ? PackageVersion::FILE_DELETED
: PackageVersion::FILE_UNCHANGED;
if (f.status == PackageVersion::FILE_DELETED) {
if (s.GetChar(1) != _(' ')) {f.status = PackageVersion::FILE_ADDED; return; }
f.file = s.substr(2);
return;
} else if (f.status != PackageVersion::FILE_UNCHANGED) {
s = s.substr(1);
}
if (s.size() < 8+1+6+1) {f.status = PackageVersion::FILE_ADDED; return; }
if (s.GetChar(8+1+6) != _(' ')) {f.status = PackageVersion::FILE_ADDED; return; } // invalid format
// read time, filename
f.time.ParseFormat(s, _("%Y%m%dT%H%M%S"));
f.file = s.substr(8+1+6+1);
}
IMPLEMENT_REFLECTION_NO_SCRIPT(PackageVersion) {
REFLECT_NO_SCRIPT(name);
REFLECT_NO_SCRIPT(version);
REFLECT_NO_SCRIPT(status);
REFLECT_NO_SCRIPT(files);
}
void PackageVersion::check_status(Packaged& package) {
status &= ~STATUS_MODIFIED;
if (!(status & STATUS_BLESSED)) status |= STATUS_MODIFIED;
name = package.relativeFilename();
version = package.version;
// Merge our files list with the list from the package
vector<FileInfo> new_files;
Package::FileInfos fis = package.getFileInfos();
Package::FileInfos::const_iterator it1 = fis.begin();
vector<FileInfo>::iterator it2 = files.begin();
//% size_t it2 = 0, size = files.size();
//% bool need_sort = false;
while(it1 != fis.end() || it2 != files.end()) {
if (it1 != fis.end() && it2 != files.end() && it1->first == it2->file) {
DateTime mtime = package.modificationTime(*it1);
if (mtime != it2->time) {
it2->time = mtime;
it2->status = FILE_MODIFIED;
new_files.push_back(*it2);
status |= STATUS_MODIFIED;
}
++it1; ++it2;
} else if (it1 != fis.end() && (it2 == files.end() || it1->first < it2->file)) {
// this is a new file
DateTime mtime = package.modificationTime(*it1);
new_files.push_back(FileInfo(it1->first, mtime, FILE_ADDED));
status |= STATUS_MODIFIED;
++it1;
} else {
// this file is no longer in the package, it was deleted
if (it2->status != FILE_ADDED) {
it2->status = FILE_DELETED;
new_files.push_back(*it2);
status |= STATUS_MODIFIED;
}
++it2;
}
}
swap(files,new_files);
}
inline bool is_deleted(PackageVersion::FileInfo f) {
return f.status == PackageVersion::FILE_DELETED;
}
void PackageVersion::bless() {
files.erase(remove_if(files.begin(),files.end(),is_deleted),files.end());
FOR_EACH(f,files) {
f.status = FILE_UNCHANGED;
}
status &= ~STATUS_MODIFIED;
status |= STATUS_BLESSED;
}