diff options
author | Chuck Miller <shadowmil@gmail.com> | 2009-12-19 04:40:42 -0500 |
---|---|---|
committer | Chuck Miller <shadowmil@gmail.com> | 2009-12-19 05:08:56 -0500 |
commit | cbc14c8a3c6614987d2331057e114d92336cbac0 (patch) | |
tree | a3693bd6ac6aca39a34a2d9a9f644873d63ecf32 /src | |
parent | 117cc13e863b788bfc8adef9468dba54c4909b9b (diff) | |
download | mana-cbc14c8a3c6614987d2331057e114d92336cbac0.tar.gz mana-cbc14c8a3c6614987d2331057e114d92336cbac0.tar.bz2 mana-cbc14c8a3c6614987d2331057e114d92336cbac0.tar.xz mana-cbc14c8a3c6614987d2331057e114d92336cbac0.zip |
Added support for resources.xml from update server... Also added option to download music optionally
I had to edit the XML wrapper a bit, basicilly its constructor can now take a optional
thrid arguement which will tell it to use a resman or open the file directly
Also I added fallback support for the old resouce2.txt so servers don't have to
upgrade if they do not want to
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/setup_audio.cpp | 14 | ||||
-rw-r--r-- | src/gui/setup_audio.h | 4 | ||||
-rw-r--r-- | src/gui/updatewindow.cpp | 137 | ||||
-rw-r--r-- | src/gui/updatewindow.h | 20 | ||||
-rw-r--r-- | src/main.cpp | 27 | ||||
-rw-r--r-- | src/net/download.cpp | 2 | ||||
-rw-r--r-- | src/utils/xml.cpp | 35 | ||||
-rw-r--r-- | src/utils/xml.h | 2 |
8 files changed, 186 insertions, 55 deletions
diff --git a/src/gui/setup_audio.cpp b/src/gui/setup_audio.cpp index 990a3ce8..5b1a86b9 100644 --- a/src/gui/setup_audio.cpp +++ b/src/gui/setup_audio.cpp @@ -38,7 +38,9 @@ Setup_Audio::Setup_Audio(): mMusicVolume((int)config.getValue("musicVolume", 60)), mSfxVolume((int)config.getValue("sfxVolume", 100)), mSoundEnabled(config.getValue("sound", 0)), + mDownloadEnabled(config.getValue("download-music", false)), mSoundCheckBox(new CheckBox(_("Sound"), mSoundEnabled)), + mDownloadMusicCheckBox(new CheckBox(_("Download music"), mDownloadEnabled)), mSfxSlider(new Slider(0, sound.getMaxVolume())), mMusicSlider(new Slider(0, sound.getMaxVolume())) { @@ -71,6 +73,7 @@ Setup_Audio::Setup_Audio(): place(1, 1, sfxLabel); place(0, 2, mMusicSlider); place(1, 2, musicLabel); + place(0, 3, mDownloadMusicCheckBox); setDimension(gcn::Rectangle(0, 0, 365, 280)); } @@ -78,11 +81,20 @@ Setup_Audio::Setup_Audio(): void Setup_Audio::apply() { mSoundEnabled = mSoundCheckBox->isSelected(); + mDownloadEnabled = mDownloadMusicCheckBox->isSelected(); mSfxVolume = (int) config.getValue("sfxVolume", 100); mMusicVolume = (int) config.getValue("musicVolume", 60); config.setValue("sound", mSoundEnabled); + // Display a message if user has selected to download music, + // And if downloadmusic is not already enabled + if (mDownloadEnabled && !config.getValue("download-music", false)) + { + new OkDialog(_("Notice"),_("You may have to restart your client if you want to download new music")); + } + config.setValue("download-music", mDownloadEnabled); + if (mSoundEnabled) { try @@ -104,6 +116,7 @@ void Setup_Audio::apply() void Setup_Audio::cancel() { mSoundCheckBox->setSelected(mSoundEnabled); + mDownloadMusicCheckBox->setSelected(mDownloadEnabled); sound.setSfxVolume(mSfxVolume); mSfxSlider->setValue(mSfxVolume); @@ -112,6 +125,7 @@ void Setup_Audio::cancel() mMusicSlider->setValue(mMusicVolume); config.setValue("sound", mSoundEnabled); + config.setValue("download-music", mDownloadEnabled); config.setValue("sfxVolume", mSfxVolume); config.setValue("musicVolume", mMusicVolume); } diff --git a/src/gui/setup_audio.h b/src/gui/setup_audio.h index b398a880..6cb3e1f3 100644 --- a/src/gui/setup_audio.h +++ b/src/gui/setup_audio.h @@ -40,9 +40,9 @@ class Setup_Audio : public SetupTab, public gcn::ActionListener private: int mMusicVolume, mSfxVolume; - bool mSoundEnabled; + bool mSoundEnabled, mDownloadEnabled; - gcn::CheckBox *mSoundCheckBox; + gcn::CheckBox *mSoundCheckBox, *mDownloadMusicCheckBox; gcn::Slider *mSfxSlider, *mMusicSlider; }; diff --git a/src/gui/updatewindow.cpp b/src/gui/updatewindow.cpp index afb87430..600c0bfd 100644 --- a/src/gui/updatewindow.cpp +++ b/src/gui/updatewindow.cpp @@ -40,30 +40,83 @@ #include "utils/gettext.h" #include "utils/stringutils.h" +#include "utils/xml.h" #include <iostream> +#include <fstream> /** - * Load the given file into a vector of strings. + * Load the given file into a vector of updateFiles. */ -std::vector<std::string> loadTextFile(const std::string &fileName) +std::vector<updateFile> loadXMLFile(const std::string &fileName) { - std::vector<std::string> lines; - std::ifstream fin(fileName.c_str()); + std::vector<updateFile> files; + XML::Document doc(fileName, false); + xmlNodePtr rootNode = doc.rootNode(); - if (!fin) { - logger->log("Couldn't load text file: %s", fileName.c_str()); - return lines; + if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "updates")) + { + logger->log("Error loading update file: %s", fileName.c_str()); + return files; } - std::string line; - - while (getline(fin, line)) - lines.push_back(line); + for_each_xml_child_node(fileNode, rootNode) + { + // Ignore all tags except for the "update" tags + if (!xmlStrEqual(fileNode->name, BAD_CAST "update")) + continue; + + updateFile file; + file.name = XML::getProperty(fileNode, "file", ""); + file.hash = XML::getProperty(fileNode, "hash", ""); + file.type = XML::getProperty(fileNode, "type", "data"); + file.desc = XML::getProperty(fileNode, "description", ""); + if (XML::getProperty(fileNode, "required", "yes") == "yes") + { + file.required = true; + } + else + { + file.required = false; + } + files.push_back(file); + } - return lines; + return files; } +std::vector<updateFile> loadTxtFile(const std::string &fileName) +{ + std::vector<updateFile> files; + std::ifstream fileHandler; + fileHandler.open(fileName.c_str(), std::ios::in); + + if (fileHandler.is_open()) + { + while (fileHandler.good()) + { + char name[256], hash[50]; + fileHandler.getline(name, 256, ' '); + fileHandler.getline(hash, 50); + + updateFile thisFile; + thisFile.name = name; + thisFile.hash = hash; + thisFile.type = "data"; + thisFile.required = true; + thisFile.desc = ""; + + files.push_back(thisFile); + } + } + else + { + logger->log("Error loading update file: %s", fileName.c_str()); + } + fileHandler.close(); + + return files; +} UpdaterWindow::UpdaterWindow(const std::string &updateHost, const std::string &updatesDir): @@ -80,7 +133,7 @@ UpdaterWindow::UpdaterWindow(const std::string &updateHost, mDownloadedBytes(0), mMemoryBuffer(NULL), mDownload(NULL), - mLineIndex(0) + mUpdateIndex(0) { mBrowserBox = new BrowserBox; mScrollArea = new ScrollArea(mBrowserBox); @@ -119,6 +172,7 @@ UpdaterWindow::UpdaterWindow(const std::string &updateHost, UpdaterWindow::~UpdaterWindow() { + loadUpdates(); if (mDownload) { mDownload->cancel(); @@ -300,8 +354,21 @@ void UpdaterWindow::download() mDownload->start(); } +void UpdaterWindow::loadUpdates() +{ + ResourceManager *resman = ResourceManager::getInstance(); + + for (mUpdateIndex = 0; mUpdateIndex < mUpdateFiles.size(); mUpdateIndex++) + { + resman->addToSearchPath(mUpdatesDir + "/" + mUpdateFiles[mUpdateIndex].name, false); + } +} + void UpdaterWindow::logic() { + const std::string xmlUpdateFile = "resources.xml"; + const std::string txtUpdateFile = "resources2.txt"; + // Update Scroll logic mScrollArea->logic(); @@ -342,7 +409,7 @@ void UpdaterWindow::logic() // Parse current memory buffer as news and dispose of the data loadNews(); - mCurrentFile = "resources2.txt"; + mCurrentFile = xmlUpdateFile; mStoreInMemory = false; mDownloadStatus = UPDATE_LIST; download(); // download() changes mDownloadComplete to false @@ -351,7 +418,25 @@ void UpdaterWindow::logic() case UPDATE_LIST: if (mDownloadComplete) { - mLines = loadTextFile(mUpdatesDir + "/resources2.txt"); + if (mCurrentFile == xmlUpdateFile) + { + mUpdateFiles = loadXMLFile(mUpdatesDir + "/" + xmlUpdateFile); + if (mUpdateFiles.size() == 0) + { + logger->log("Warning this server does not have a %s file falling back to %s",xmlUpdateFile.c_str(),txtUpdateFile.c_str()); + + // If the resources.xml file fails, fall back onto a older version + mCurrentFile = txtUpdateFile; + mStoreInMemory = false; + mDownloadStatus = UPDATE_LIST; + download(); + break; + } + } + else if (mCurrentFile == txtUpdateFile) + { + mUpdateFiles = loadTxtFile(mUpdatesDir + "/" + txtUpdateFile); + } mStoreInMemory = false; mDownloadStatus = UPDATE_RESOURCES; } @@ -359,12 +444,23 @@ void UpdaterWindow::logic() case UPDATE_RESOURCES: if (mDownloadComplete) { - if (mLineIndex < mLines.size()) + if (mUpdateIndex < mUpdateFiles.size()) { - std::stringstream line(mLines[mLineIndex]); - line >> mCurrentFile; + updateFile thisFile = mUpdateFiles[mUpdateIndex]; + if (!thisFile.required) + { + // This statement checks to see if the file type is music, and if download-music is true + // If it fails, this statement returns true, and results in not downloading the file + // Else it will ignore the break, and download the file. + if ( !(thisFile.type == "music" && config.getValue("download-music", false)) ) + { + mUpdateIndex++; + break; + } + } + mCurrentFile = thisFile.name; std::string checksum; - line >> checksum; + checksum = thisFile.hash; std::stringstream ss(checksum); ss >> std::hex >> mCurrentChecksum; @@ -378,9 +474,10 @@ void UpdaterWindow::logic() } else { + temp.close(); logger->log("%s already here", mCurrentFile.c_str()); } - mLineIndex++; + mUpdateIndex++; } else { diff --git a/src/gui/updatewindow.h b/src/gui/updatewindow.h index 8388b722..55415938 100644 --- a/src/gui/updatewindow.h +++ b/src/gui/updatewindow.h @@ -39,6 +39,16 @@ class Button; class ProgressBar; class ScrollArea; +struct updateFile +{ + public: + std::string name; + std::string hash; + std::string type; + bool required; + std::string desc; +}; + /** * Update progress window GUI * @@ -96,6 +106,12 @@ private: void download(); /** + * Loads the updates this window has gotten into the resource manager + */ + void loadUpdates(); + + + /** * A download callback for progress updates. */ static int updateProgress(void *ptr, DownloadStatus status, @@ -160,10 +176,10 @@ private: Net::Download *mDownload; /** List of files to download. */ - std::vector<std::string> mLines; + std::vector<updateFile> mUpdateFiles; /** Index of the file to be downloaded. */ - unsigned int mLineIndex; + unsigned int mUpdateIndex; gcn::Label *mLabel; /**< Progress bar caption. */ Button *mCancelButton; /**< Button to stop the update process. */ diff --git a/src/main.cpp b/src/main.cpp index 8caef783..cd5e7ba3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -627,27 +627,6 @@ static void parseOptions(int argc, char *argv[], Options &options) } } -/** - * Reads the file "{Updates Directory}/resources2.txt" and attempts to load - * each update mentioned in it. - */ -static void loadUpdates() -{ - if (updatesDir.empty()) return; - const std::string updatesFile = "/" + updatesDir + "/resources2.txt"; - ResourceManager *resman = ResourceManager::getInstance(); - std::vector<std::string> lines = resman->loadTextFile(updatesFile); - - for (unsigned int i = 0; i < lines.size(); ++i) - { - std::stringstream line(lines[i]); - std::string filename; - line >> filename; - resman->addToSearchPath(homeDir + "/" + updatesDir + "/" - + filename, false); - } -} - class AccountListener : public gcn::ActionListener { public: @@ -1041,11 +1020,7 @@ int main(int argc, char *argv[]) // we don't load any other files... if (options.dataPath.empty()) { - // Load the updates downloaded so far... - loadUpdates(); - - - // Also add customdata directory + // Add customdata directory ResourceManager::getInstance()->searchAndAddArchives( "customdata/", "zip", diff --git a/src/net/download.cpp b/src/net/download.cpp index 51034e0c..ecc36a0c 100644 --- a/src/net/download.cpp +++ b/src/net/download.cpp @@ -261,7 +261,7 @@ int Download::downloadThread(void *ptr) if (!d->mOptions.memoryWrite) { - // Don't check resources2.txt checksum + // Don't check resources.xml checksum if (d->mOptions.checkAdler) { unsigned long adler = fadler32(file); diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp index 7a6f75de..4d85e87f 100644 --- a/src/utils/xml.cpp +++ b/src/utils/xml.cpp @@ -25,14 +25,43 @@ #include "resources/resourcemanager.h" +#include <iostream> +#include <fstream> + namespace XML { - Document::Document(const std::string &filename): + Document::Document(const std::string &filename, bool useResman): mDoc(0) { int size; - ResourceManager *resman = ResourceManager::getInstance(); - char *data = (char*) resman->loadFile(filename.c_str(), size); + char *data; + if (useResman) + { + ResourceManager *resman = ResourceManager::getInstance(); + data = (char*) resman->loadFile(filename.c_str(), size); + } + else + { + std::ifstream file; + file.open(filename.c_str(), std::ios::in); + + if (file.is_open()) + { + // Get length of file + file.seekg (0, std::ios::end); + size = file.tellg(); + file.seekg(0, std::ios::beg); + + data = new char[size]; + + file.read(data, size); + file.close(); + } + else + { + logger->log("Error loading XML file %s", filename.c_str()); + } + } if (data) { mDoc = xmlParseMemory(data, size); diff --git a/src/utils/xml.h b/src/utils/xml.h index e7075279..441d2125 100644 --- a/src/utils/xml.h +++ b/src/utils/xml.h @@ -42,7 +42,7 @@ namespace XML * Constructor that attempts to load the given file through the * resource manager. Logs errors. */ - Document(const std::string &filename); + Document(const std::string &filename, bool useResman = true); /** * Constructor that attempts to load an XML document from memory. |