Skip to content

Commit

Permalink
First round of cleanup up the AddonManager a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Grumbel committed Aug 25, 2014
1 parent 8415881 commit 9216f1f
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 268 deletions.
103 changes: 49 additions & 54 deletions src/addon/addon.cpp
Expand Up @@ -29,93 +29,88 @@
std::string
Addon::get_md5() const
{
if (!installed) {
if (stored_md5 == "") { log_warning << "Add-on not installed and no stored MD5 available" << std::endl; }
if (!installed)
{
if (stored_md5.empty())
{
log_warning << "Add-on not installed and no stored MD5 available" << std::endl;
}
return stored_md5;
}

if (calculated_md5 != "") return calculated_md5;

if (installed_physfs_filename == "") throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");

// TODO: this does not work as expected for some files -- IFileStream seems to not always behave like an ifstream.
//IFileStream ifs(installed_physfs_filename);
//std::string md5 = MD5(ifs).hex_digest();

MD5 md5;
PHYSFS_file* file;
file = PHYSFS_openRead(installed_physfs_filename.c_str());
unsigned char buffer[1024];
while (true) {
PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
if (len <= 0) break;
md5.update(buffer, len);
else if (!calculated_md5.empty())
{
return calculated_md5;
}
else if (installed_physfs_filename.empty())
{
throw std::runtime_error("Tried to calculate MD5 of Add-on with unknown filename");
}
else
{
// TODO: this does not work as expected for some files -- IFileStream seems to not always behave like an ifstream.
//IFileStream ifs(installed_physfs_filename);
//std::string md5 = MD5(ifs).hex_digest();

MD5 md5;
PHYSFS_file* file;
file = PHYSFS_openRead(installed_physfs_filename.c_str());
unsigned char buffer[1024];
while (true) {
PHYSFS_sint64 len = PHYSFS_read(file, buffer, 1, sizeof(buffer));
if (len <= 0) break;
md5.update(buffer, len);
}
PHYSFS_close(file);

calculated_md5 = md5.hex_digest();
log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;

return calculated_md5;
}
PHYSFS_close(file);

calculated_md5 = md5.hex_digest();
log_debug << "MD5 of " << title << ": " << calculated_md5 << std::endl;

return calculated_md5;
}

void
Addon::parse(const Reader& lisp)
{
try {
try
{
lisp.get("kind", kind);
lisp.get("title", title);
lisp.get("author", author);
lisp.get("license", license);
lisp.get("http-url", http_url);
lisp.get("file", suggested_filename);
lisp.get("md5", stored_md5);
} catch(std::exception& e) {
}
catch(const std::exception& err)
{
std::stringstream msg;
msg << "Problem when parsing addoninfo: " << e.what();
msg << "Problem when parsing addoninfo: " << err.what();
throw std::runtime_error(msg.str());
}
}

void
Addon::parse(std::string fname)
Addon::parse(const std::string& fname)
{
try {
try
{
lisp::Parser parser;
const lisp::Lisp* root = parser.parse(fname);
const lisp::Lisp* addon = root->get_lisp("supertux-addoninfo");
if(!addon) throw std::runtime_error("file is not a supertux-addoninfo file.");
parse(*addon);
} catch(std::exception& e) {
}
catch(const std::exception& err)
{
std::stringstream msg;
msg << "Problem when reading addoninfo '" << fname << "': " << e.what();
msg << "Problem when reading addoninfo '" << fname << "': " << err.what();
throw std::runtime_error(msg.str());
}
}

void
Addon::write(lisp::Writer& writer) const
{
writer.start_list("supertux-addoninfo");
if (kind != "") writer.write("kind", kind);
if (title != "") writer.write("title", title);
if (author != "") writer.write("author", author);
if (license != "") writer.write("license", license);
if (http_url != "") writer.write("http-url", http_url);
if (suggested_filename != "") writer.write("file", suggested_filename);
if (stored_md5 != "") writer.write("md5", stored_md5);
writer.end_list("supertux-addoninfo");
}

void
Addon::write(std::string fname) const
{
lisp::Writer writer(fname);
write(writer);
}

bool
Addon::operator==(Addon addon2) const
Addon::operator==(const Addon& addon2) const
{
std::string s1 = this->get_md5();
std::string s2 = addon2.get_md5();
Expand Down
49 changes: 19 additions & 30 deletions src/addon/addon.hpp
Expand Up @@ -20,66 +20,51 @@
#include <string>

#include "util/reader_fwd.hpp"
#include "util/writer_fwd.hpp"

/**
* Represents an (available or installed) Add-on, e.g. a level set
*/
/** Represents an (available or installed) Add-on, e.g. a level set */
class Addon
{
public:
int id;
std::string kind;
std::string title;
std::string author;
std::string license;
std::string http_url;

/** filename suggested by addon author, e.g. "pak0.zip" */
std::string suggested_filename;

/** PhysFS filename on disk, e.g. "pak0.zip" */
std::string installed_physfs_filename;

/** complete path and filename on disk, e.g. "/home/sommer/.supertux2/pak0.zip" */
std::string installed_absolute_filename;

std::string stored_md5;
bool installed;
bool loaded;

/**
* Get MD5, based either on installed file's contents or stored value
*/
/** Get MD5, based either on installed file's contents or stored value */
std::string get_md5() const;

/**
* Read additional information from given contents of a (supertux-addoninfo ...) block
*/
/** Read additional information from given contents of a (supertux-addoninfo ...) block */
void parse(const Reader& lisp);

/**
* Read additional information from given file
*/
void parse(std::string fname);
/** Read additional information from given file */
void parse(const std::string& fname);

/**
* Writes out Add-on metainformation to a Lisp Writer
*/
void write(Writer& writer) const;

/**
* Writes out Add-on metainformation to a file
*/
void write(std::string fname) const;

/**
* Checks if Add-on is the same as given one.
* If available, checks MD5 sum, else relies on kind, author and title alone.
*/
bool operator==(Addon addon2) const;
/** Checks if Add-on is the same as given one. If available, checks
MD5 sum, else relies on kind, author and title alone. */
bool operator==(const Addon& addon2) const;

protected:
friend class AddonManager;

mutable std::string calculated_md5;

Addon() :
Addon(int id_) :
id(id_),
kind(),
title(),
author(),
Expand All @@ -93,6 +78,10 @@ class Addon
loaded(),
calculated_md5()
{};

private:
Addon(const Addon&) = delete;
Addon& operator=(const Addon&) = delete;
};

#endif
Expand Down

0 comments on commit 9216f1f

Please sign in to comment.