addon/dep: Initial addon_dep type...

...used for representing add-on dependencies in the source code

This struct type is currently unused while some state tracking
functionality is moved around. It will work with the current
campaignd/pbl protocol first until the new syntax is discussed and later
finalized.
This commit is contained in:
Ignacio R. Morelle 2012-02-29 22:07:01 +00:00
parent b7f955cf78
commit 95cad6f34c
4 changed files with 243 additions and 0 deletions

View File

@ -315,6 +315,7 @@ set(wesnoth-main_SRC
about.cpp
actions.cpp
addon/client.cpp
addon/dep.cpp
addon/info.cpp
addon/manager.cpp
addon/manager_ui.cpp

View File

@ -155,6 +155,7 @@ wesnoth_sources = Split("""
about.cpp
actions.cpp
addon/client.cpp
addon/dep.cpp
addon/info.cpp
addon/manager.cpp
addon/manager_ui.cpp

108
src/addon/dep.cpp Normal file
View File

@ -0,0 +1,108 @@
/* $Id$ */
/*
Copyright (C) 2012 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "addon/dep.hpp"
#include "config.hpp"
namespace {
inline addon_dep::dep_type dep_type_str_to_id(const std::string& str)
{
if(str == "recommends") {
return addon_dep::recommends;
} else if(str == "conflicts") {
return addon_dep::conflicts;
} else {
return addon_dep::requires;
}
}
}
addon_dep::addon_dep(const addon_dep& o)
: id(o.id)
, type(o.type)
, dependants(o.dependants)
, version(o.version)
, version_type(o.version_type)
{
}
addon_dep::addon_dep(const std::string& dep)
: id(dep)
, type(requires)
, dependants()
, version()
, version_type(version_any)
{
}
addon_dep::addon_dep(const config& cfg, const std::string& type_str)
: id()
, type()
, dependants()
, version()
, version_type()
{
this->read(cfg, type_str);
}
addon_dep& addon_dep::operator=(const addon_dep& o)
{
if(&o != this) {
this->id = o.id;
this->type = o.type;
this->dependants = o.dependants;
this->version = o.version;
this->version_type = o.version_type;
}
return *this;
}
void addon_dep::read(const config& cfg, const std::string& type_str)
{
this->id = cfg["id"].str();
this->type = dep_type_str_to_id(type_str);
if(cfg.has_attribute("version_equals") && cfg["version_equals"].str() != "*") {
this->version_type = version_equals;
this->version = cfg["version_equals"].str();
}
else if(cfg.has_attribute("version_not_equals")) {
this->version_type = version_not_equals;
this->version = cfg["version_not_equals"].str();
}
else if(cfg.has_attribute("version_less_than")) {
this->version_type = version_less_than;
this->version = cfg["version_less_than"].str();
}
else if(cfg.has_attribute("version_less_than_or_equals")) {
this->version_type = version_less_than_or_equals;
this->version = cfg["version_less_than_or_equals"].str();
}
else if(cfg.has_attribute("version_greater_than")) {
this->version_type = version_greater_than;
this->version = cfg["version_greater_than"].str();
}
else if(cfg.has_attribute("version_greater_than_or_equals")) {
this->version_type = version_greater_than_or_equals;
this->version = cfg["version_greater_than_or_equals"].str();
}
else {
this->version_type = version_any;
this->version = version_info();
}
}

133
src/addon/dep.hpp Normal file
View File

@ -0,0 +1,133 @@
/* $Id$ */
/*
Copyright (C) 2012 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef ADDON_DEP_HPP_INCLUDED
#define ADDON_DEP_HPP_INCLUDED
#include "version.hpp"
#include <set>
class config;
/**
* Type for definining add-on dependencies.
*/
struct addon_dep
{
/** Representation of valid dependency levels. */
enum dep_type {
requires, /**< The add-on must be installed for dependants to work properly. */
conflicts, /**< The add-on must not be installed for dependants to work properly. */
recommends /**< The add-on is suggested to be installed for dependants to work better. */
};
/** Representation of valid version requirements. */
enum version_dep_type {
version_any, /**< Any @a version is accepted. */
version_equals, /**< The version must be exactly the specified @a version. */
version_not_equals, /**< The version must differ from the specified @a version. */
version_less_than, /**< The version must be less than the specified @a version. */
version_less_than_or_equals, /**< The version must be less than or equal to the specified @a version. */
version_greater_than, /**< The version must be greater than the specified @a version. */
version_greater_than_or_equals /**< The version must be greater than or equal to the specified @a version. */
};
//
// Fields
//
/** Add-on id that is depended upon. */
std::string id;
/** Dependency level. */
dep_type type;
/** List of known dependants, used in UI reports. */
std::set<std::string> dependants;
/** Add-on version that is depended upon. */
version_info version;
/** Dependency version class. */
version_dep_type version_type;
//
// Methods
//
/** Default constructor. */
addon_dep() :
id(), type(), dependants(), version(), version_type()
{}
/** Copy constructor. */
explicit addon_dep(const addon_dep& o);
/**
* Compatibility constructor used with the old .dependencies attribute.
*
* @todo Remove this once the new syntax is finalized and the server protocol changed.
*/
explicit addon_dep(const std::string& dep);
/**
* WML constructor using the @a read method.
*
* Sets attributes to reflect the specified WML node object.
*
* @param cfg The node object with attributes to read.
* @param type_str The original node key, e.g. "depends", "requires".
*/
addon_dep(const config& cfg, const std::string& type_str);
/** Copy operator. */
addon_dep& operator=(const addon_dep& o);
/**
* Sets attributes to reflect the specified WML node object.
*
* @param cfg The node object with attributes to read.
* @param type_str The original node key, e.g. "depends", "requires".
*/
void read(const config& cfg, const std::string& type_str);
};
inline bool operator<(const addon_dep& a, const addon_dep& b) {
return a.id < b.id;
}
inline bool operator==(const addon_dep& a, const addon_dep& b) {
return a.id == b.id;
}
inline bool operator>(const addon_dep& a, const addon_dep& b) {
return a.id > b.id;
}
inline bool operator<=(const addon_dep& a, const addon_dep& b) {
return a.id <= b.id;
}
inline bool operator!=(const addon_dep& a, const addon_dep& b) {
return a.id != b.id;
}
inline bool operator>=(const addon_dep& a, const addon_dep& b) {
return a.id >= b.id;
}
#endif