Completely remove used of m_index (i.e. the addons_manager
does not have a concept of a current addon anymore). Note that downloading addons still crashes. git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/trunk@7186 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
@@ -41,30 +41,33 @@ public:
|
||||
Addon() {};
|
||||
/** Initialises the object from an XML node. */
|
||||
Addon(const XMLNode &xml, bool installed=false);
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the name of the addon. */
|
||||
const std::string& getName() const {return m_name; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the type of the addon. */
|
||||
const std::string& getType() const {return m_type; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the filename of the addon. */
|
||||
const std::string& getFile() const {return m_file; }
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the name of the icon of this addon. */
|
||||
const std::string& getIcon() const {return m_icon; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the name of the addon. */
|
||||
const std::string& getDescription() const {return m_description; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns if the addon is installed. */
|
||||
bool isInstalled() const {return m_installed; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the installed version of an addon. */
|
||||
int getInstalledVersion() const {return m_installed_version; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the latest version of this addon.
|
||||
* m_version>m_installed_version if a newer version is available
|
||||
* online. */
|
||||
int getVersion() const {return m_version; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the version as string. */
|
||||
std::string getVersionAsStr() const
|
||||
{
|
||||
@@ -72,16 +75,26 @@ public:
|
||||
os << m_version;
|
||||
return os.str();
|
||||
} // getVersionAsStr
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Returns the ID of this addon. */
|
||||
const std::string& getId() const {return m_id; }
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** True if this addon needs to be updated. */
|
||||
bool needsUpdate() const
|
||||
{
|
||||
return getInstalledVersion() < getVersion();
|
||||
}
|
||||
// --------------------------------------------------------------------
|
||||
// ------------------------------------------------------------------------
|
||||
/** Marks this addon to be installed. If the addon is marked as being
|
||||
* installed, it also updates the installed version number to be the
|
||||
* same as currently available version number. */
|
||||
void setInstalled(bool state)
|
||||
{
|
||||
m_installed = state;
|
||||
if(state)
|
||||
m_installed_version = m_version;
|
||||
} // setInstalled
|
||||
// ------------------------------------------------------------------------
|
||||
}; // Addon
|
||||
|
||||
|
||||
|
||||
@@ -45,8 +45,6 @@ AddonsManager* addons_manager = 0;
|
||||
*/
|
||||
AddonsManager::AddonsManager() : m_state(STATE_INIT)
|
||||
{
|
||||
m_index = -1;
|
||||
|
||||
m_file_installed = file_manager->getConfigDir()
|
||||
+ "/" + "addons_installed.xml";
|
||||
loadInstalledAddons();
|
||||
@@ -128,8 +126,6 @@ void AddonsManager::loadInstalledAddons()
|
||||
if(!xml)
|
||||
return;
|
||||
|
||||
int old_index = m_index;
|
||||
|
||||
for(unsigned int i=0; i<xml->getNumNodes(); i++)
|
||||
{
|
||||
const XMLNode *node=xml->getNode(i);
|
||||
@@ -142,11 +138,11 @@ void AddonsManager::loadInstalledAddons()
|
||||
node->get("id", &id );
|
||||
node->get("name", &name );
|
||||
node->get("version", &version);
|
||||
|
||||
if(getAddon(id))
|
||||
int index = getAddonIndex(id);
|
||||
if(index>0)
|
||||
{
|
||||
m_addons_list[m_index].m_installed = true;
|
||||
m_addons_list[m_index].m_installed_version = version;
|
||||
m_addons_list[index].m_installed = true;
|
||||
m_addons_list[index].m_installed_version = version;
|
||||
std::cout << "[Addons] An addon is already installed: "
|
||||
<< id << std::endl;
|
||||
}
|
||||
@@ -159,38 +155,8 @@ void AddonsManager::loadInstalledAddons()
|
||||
} // for i <= xml->getNumNodes()
|
||||
|
||||
delete xml;
|
||||
m_index = old_index;
|
||||
} // loadInstalledAddons
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
bool AddonsManager::select(std::string name)
|
||||
{
|
||||
//the unsigned is to remove the compiler warnings, maybe it is a bad idea ?
|
||||
for(unsigned int i = 0; i < m_addons_list.size(); i++)
|
||||
{
|
||||
if(m_addons_list[i].m_name == name)
|
||||
{
|
||||
m_index = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} // select
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
bool AddonsManager::selectId(std::string id)
|
||||
{
|
||||
for(unsigned int i = 0; i < m_addons_list.size(); i++)
|
||||
{
|
||||
if(m_addons_list[i].m_id == id)
|
||||
{
|
||||
m_index = i;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} // selectId
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
/** Returns an addon with a given id. Raises an assertion if the id is not
|
||||
* found!
|
||||
@@ -216,42 +182,14 @@ int AddonsManager::getAddonIndex(const std::string &id) const
|
||||
} // getAddonIndex
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
std::string AddonsManager::getIdAsStr() const
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << m_addons_list[m_index].m_id;
|
||||
return os.str();
|
||||
} // getIdAsStr
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
int AddonsManager::getInstalledVersion() const
|
||||
{
|
||||
if(m_addons_list[m_index].m_installed)
|
||||
return m_addons_list[m_index].m_installed_version;
|
||||
return 0;
|
||||
} // getInstalledVersion
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
std::string AddonsManager::getInstalledVersionAsStr() const
|
||||
{
|
||||
if(m_addons_list[m_index].m_installed)
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << m_addons_list[m_index].m_installed_version;
|
||||
return os.str();
|
||||
}
|
||||
return "";
|
||||
} // getInstalledVersionAsStr
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void AddonsManager::install()
|
||||
void AddonsManager::install(const Addon &addon)
|
||||
{
|
||||
//download of the addons file
|
||||
|
||||
m_str_state = "Downloading...";
|
||||
|
||||
std::string file = "file/" + m_addons_list[m_index].m_file;
|
||||
network_http->downloadFileAsynchron(file, m_addons_list[m_index].m_name);
|
||||
std::string file = "file/" + addon.getFile();
|
||||
network_http->downloadFileAsynchron(file, addon.getName());
|
||||
//FIXME , &m_download_state);
|
||||
bool success=true;
|
||||
if (!success)
|
||||
@@ -261,15 +199,13 @@ void AddonsManager::install()
|
||||
return;
|
||||
}
|
||||
|
||||
file_manager->checkAndCreateDirForAddons(m_addons_list[m_index].m_name,
|
||||
m_addons_list[m_index].m_type + "s/");
|
||||
file_manager->checkAndCreateDirForAddons(addon.getName(),
|
||||
addon.getType()+ "s/");
|
||||
|
||||
//extract the zip in the addons folder called like the addons name
|
||||
std::string dest_file =file_manager->getAddonsDir() + "/" + "data" + "/" +
|
||||
m_addons_list[m_index].m_type + "s/" +
|
||||
m_addons_list[m_index].m_name + "/" ;
|
||||
std::string from = file_manager->getConfigDir() + "/"
|
||||
+ m_addons_list[m_index].m_name;
|
||||
std::string dest_file = file_manager->getAddonsDir() + "/"
|
||||
+ addon.getType()+ "s/" + addon.getName() + "/" ;
|
||||
std::string from = file_manager->getConfigDir() + "/" + addon.getName();
|
||||
std::string to = dest_file;
|
||||
|
||||
m_str_state = "Unzip the addons...";
|
||||
@@ -283,8 +219,9 @@ void AddonsManager::install()
|
||||
return;
|
||||
}
|
||||
|
||||
m_addons_list[m_index].m_installed = true;
|
||||
m_addons_list[m_index].m_installed_version = m_addons_list[m_index].m_version;
|
||||
int index = getAddonIndex(addon.getId());
|
||||
assert(index>=0 && index < (int)m_addons_list.size());
|
||||
m_addons_list[index].setInstalled(true);
|
||||
|
||||
m_str_state = "Reloading kart list...";
|
||||
saveInstalled();
|
||||
@@ -323,16 +260,20 @@ void AddonsManager::saveInstalled()
|
||||
} // saveInstalled
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
void AddonsManager::uninstall()
|
||||
void AddonsManager::uninstall(const Addon &addon)
|
||||
{
|
||||
std::cout << "[Addons] Uninstalling <"
|
||||
<< m_addons_list[m_index].m_name << ">\n";
|
||||
<< addon.getName() << ">\n";
|
||||
|
||||
// addon is a const reference, and to avoid removing the const, we
|
||||
// find the proper index again to modify the installed state
|
||||
int index = getAddonIndex(addon.getId());
|
||||
assert(index>=0 && index < (int)m_addons_list.size());
|
||||
m_addons_list[index].setInstalled(false);
|
||||
|
||||
m_addons_list[m_index].m_installed = false;
|
||||
//write the xml file with the informations about installed karts
|
||||
std::string dest_file = file_manager->getAddonsDir() + "/" + "data" + "/" +
|
||||
m_addons_list[m_index].m_type + "s/" +
|
||||
m_addons_list[m_index].m_name + "/";
|
||||
std::string dest_file = file_manager->getAddonsDir() + "/"
|
||||
+ addon.getType()+ "s/" + addon.getName()+ "/";
|
||||
|
||||
//remove the addons directory
|
||||
file_manager->removeDirectory(dest_file.c_str());
|
||||
|
||||
@@ -32,7 +32,6 @@ class AddonsManager
|
||||
{
|
||||
private:
|
||||
std::vector<Addon> m_addons_list;
|
||||
int m_index;
|
||||
std::string m_file_installed;
|
||||
void saveInstalled();
|
||||
void loadInstalledAddons();
|
||||
@@ -65,38 +64,12 @@ public:
|
||||
/** Get all the selected addon parameters. */
|
||||
const Addon &getAddons() const;
|
||||
|
||||
/** Select an addon with it name. */
|
||||
bool select(std::string);
|
||||
|
||||
/** Select an addon with it id. */
|
||||
bool selectId(std::string);
|
||||
|
||||
/** Get the version of the selected addon as a string. */
|
||||
std::string getVersionAsStr() const;
|
||||
|
||||
/** Get the installed version of the selected addon. */
|
||||
int getInstalledVersion() const;
|
||||
std::string getInstalledVersionAsStr() const;
|
||||
|
||||
/** Get the installed version of the selected addon. */
|
||||
std::string getIdAsStr() const;
|
||||
|
||||
/** Get the description of the selected addons. */
|
||||
const std::string &getDescription() const
|
||||
{ return m_addons_list[m_index].m_description; };
|
||||
|
||||
const std::string &getType() const
|
||||
{ return m_addons_list[m_index].m_type; };
|
||||
/** Install or upgrade the selected addon. */
|
||||
void install();
|
||||
void install(const Addon &addon);
|
||||
|
||||
/** Uninstall the selected addon. This method will remove all the
|
||||
* directory of the addon.*/
|
||||
void uninstall();
|
||||
|
||||
/** Get the state of the addon: if it is installed or not.*/
|
||||
bool isInstalled() const
|
||||
{ return m_addons_list[m_index].m_installed; };
|
||||
void uninstall(const Addon &addon);
|
||||
|
||||
int getDownloadState();
|
||||
|
||||
|
||||
@@ -198,11 +198,11 @@ void * AddonsLoading::startInstall(void* pthis)
|
||||
AddonsLoading * obj = (AddonsLoading*)pthis;
|
||||
if(!obj->m_addon.isInstalled() || obj->m_addon.needsUpdate())
|
||||
{
|
||||
addons_manager->install();
|
||||
addons_manager->install(obj->m_addon);
|
||||
}
|
||||
else
|
||||
{
|
||||
addons_manager->uninstall();
|
||||
addons_manager->uninstall(obj->m_addon);
|
||||
}
|
||||
pthread_mutex_lock(&(obj->m_mutex_can_install));
|
||||
obj->m_can_install = true;
|
||||
|
||||
Reference in New Issue
Block a user