From 7e778045b8b4eaed46a8daf1ba7f5241c3abe84a Mon Sep 17 00:00:00 2001
From: Ivan Savenko <saven.ivan@gmail.com>
Date: Thu, 9 Aug 2012 10:32:50 +0000
Subject: [PATCH] - removed no longer used CSndHandler. Transition to new fs is
 finished - moved TownHandler::requirements to CBuilding class

---
 client/CMakeLists.txt    |   1 -
 client/CMusicHandler.cpp |   1 -
 client/CSndHandler.cpp   | 159 ---------------------------------------
 client/CSndHandler.h     |  66 ----------------
 client/CVideoHandler.cpp |   1 -
 client/CVideoHandler.h   |   4 -
 client/Makefile.am       |   2 -
 client/Makefile.in       |  19 +----
 config/filesystem.json   |   2 +-
 lib/CBuildingHandler.cpp |  23 +++++-
 lib/CBuildingHandler.h   |   3 +-
 lib/CTownHandler.cpp     |  10 ---
 lib/CTownHandler.h       |   3 +-
 lib/IGameCallback.cpp    |   6 +-
 14 files changed, 30 insertions(+), 270 deletions(-)
 delete mode 100644 client/CSndHandler.cpp
 delete mode 100644 client/CSndHandler.h

diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt
index d98495464..9729fabba 100644
--- a/client/CMakeLists.txt
+++ b/client/CMakeLists.txt
@@ -34,7 +34,6 @@ set(client_SRCS
         CMusicHandler.cpp
         CPlayerInterface.cpp
         CPreGame.cpp
-        CSndHandler.cpp
         CSpellWindow.cpp
         CVideoHandler.cpp
         CQuestLog.cpp
diff --git a/client/CMusicHandler.cpp b/client/CMusicHandler.cpp
index dae3a4b10..b560983f6 100644
--- a/client/CMusicHandler.cpp
+++ b/client/CMusicHandler.cpp
@@ -2,7 +2,6 @@
 #include <boost/bimap.hpp>
 #include <SDL_mixer.h>
 
-#include "CSndHandler.h"
 #include "CMusicHandler.h"
 #include "../lib/CCreatureHandler.h"
 #include "../lib/CSpellHandler.h"
diff --git a/client/CSndHandler.cpp b/client/CSndHandler.cpp
deleted file mode 100644
index a1341b8ba..000000000
--- a/client/CSndHandler.cpp
+++ /dev/null
@@ -1,159 +0,0 @@
-#include "StdInc.h"
-#include <boost/iostreams/device/mapped_file.hpp>
-#include <SDL_endian.h>
-#include "CSndHandler.h"
-
-/*
- * CSndHandler.cpp, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
-
-/* Media file are kept in container files. We map these files in
- * memory, parse them and create an index of them to easily retrieve
- * the data+size of the objects. */
-
-CMediaHandler::~CMediaHandler()
-{
-	std::vector<boost::iostreams::mapped_file_source *>::iterator it;
-
-	entries.clear();
-	fimap.clear();
-	
-	for (it=mfiles.begin() ; it < mfiles.end(); it++ ) {
-		(*it)->close();
-		delete *it;
-	}
-}
-
-boost::iostreams::mapped_file_source *CMediaHandler::add_file(std::string fname, bool important /*= true*/)
-{
-	boost::iostreams::mapped_file_source *mfile;
-
-	try //c-tor of mapped_file_source throws exception on failure
-	{
-		mfile = new boost::iostreams::mapped_file_source(fname);
-		
-		if (!mfile->is_open()) //just in case
-			throw std::runtime_error("Cannot open " + fname + ": !mfile->is_open()");
-	} 
-	catch(std::exception &e)
-	{
-		if(important)
-			tlog1 << "Cannot open " << fname  << ": " << e.what() << std::endl;
-		throw;
-	}
-
-
-	mfiles.push_back(mfile);
-	return mfile;
-}
-
-void CMediaHandler::extract(int index, std::string dstfile) //saves selected file
-{
-	std::ofstream out(dstfile.c_str(),std::ios_base::binary);
-	Entry &entry = entries[index];
-
-	out.write(entry.data, entry.size);
-	out.close();
-}
-
-void CMediaHandler::extract(std::string srcfile, std::string dstfile, bool caseSens) //saves selected file
-{
-	srcfile.erase(srcfile.find_last_of('.'));
-	if (caseSens)
-	{
-		for (size_t i=0;i<entries.size();++i)
-		{
-			if (entries[i].name==srcfile)
-				extract(i,dstfile);
-		}
-	}
-	else
-	{
-		std::transform(srcfile.begin(),srcfile.end(),srcfile.begin(),tolower);
-		for (size_t i=0;i<entries.size();++i)
-		{
-			if (entries[i].name==srcfile)
-			{
-				std::string por = entries[i].name;
-				std::transform(por.begin(),por.end(),por.begin(),tolower);
-				if (por==srcfile)
-					extract(i,dstfile);
-			}
-		}
-	}
-}
-
-const char * CMediaHandler::extract (int index, int & size)
-{
-	Entry &entry = entries[index];
-
-	size = entry.size;
-	return entry.data;
-}
-
-const char * CMediaHandler::extract (std::string srcName, int &size)
-{
-	int index;
-	size_t dotPos = srcName.find_last_of('.');
-	if (dotPos != std::string::npos)
-		srcName.erase(dotPos);
-
-	std::map<std::string, int>::iterator fit;
-	if ((fit = fimap.find(srcName)) != fimap.end())
-	{
-		index = fit->second;
-		return this->extract(index, size);
-	}
-	size = 0;
-	return NULL;
-}
-
-void CVidHandler::add_file(std::string fname)
-{
-	boost::iostreams::mapped_file_source *mfile = NULL;
-	try
-	{
-		mfile = CMediaHandler::add_file(fname);
-	}
-	catch(...)
-	{
-		return;
-	}
-
-	if(mfile->size() < 48)
-	{
-		tlog1 << fname << " doesn't contain needed data!\n";
-		return;
-	}
-	const ui8 *data = (const ui8 *)mfile->data();
-	ui32 numFiles = SDL_SwapLE32(*(Uint32 *)&data[0]);
-	struct videoEntry *ve = (struct videoEntry *)&data[4];
-
-	for (ui32 i=0; i<numFiles; i++, ve++)
-	{
-		Entry entry;
-
-		entry.name = ve->filename;
-		entry.offset = SDL_SwapLE32(ve->offset);
-		entry.name.erase(entry.name.find_last_of('.'));
-
-		// There is no size, so check where the next file is
-		if (i == numFiles - 1) {
-			entry.size = mfile->size() - entry.offset;
-		} else {
-			struct videoEntry *ve_next = ve+1;
-
-			entry.size = SDL_SwapLE32(ve_next->offset) - entry.offset;
-		}
-		entry.data = mfile->data() + entry.offset;
-
-		entries.push_back(entry);
-		fimap[entry.name] = i;
-	}
-}
diff --git a/client/CSndHandler.h b/client/CSndHandler.h
deleted file mode 100644
index 940192603..000000000
--- a/client/CSndHandler.h
+++ /dev/null
@@ -1,66 +0,0 @@
-#pragma once
-
-#include <iosfwd>
-#include <SDL_stdinc.h>
-
-
-/*
- * CSndHandler.h, part of VCMI engine
- *
- * Authors: listed in file AUTHORS in main folder
- *
- * License: GNU General Public License v2.0 or later
- * Full text of license available in license.txt file, in main folder
- *
- */
-
-namespace boost 
-{ 
-	namespace iostreams 
-	{
-		class mapped_file_source;
-	}
-}
-
-struct MemberFile
-{
-	std::ifstream * ifs;
-	int length;
-};
-
-// video entry structure in catalog
-struct videoEntry
-{
-	char filename[40];
-	Uint32 offset;				/* little endian */
-};
-
-class CMediaHandler
-{
-protected:
-	struct Entry
-	{
-		std::string name;
-		ui32 size;
-		ui32 offset;
-		const char *data;
-	};
-
-	std::vector<boost::iostreams::mapped_file_source *> mfiles;
-	boost::iostreams::mapped_file_source *add_file(std::string fname, bool important = true); //if not important, then we don't print warning when the file is missing
-
-public:
-	std::vector<Entry> entries;
-	std::map<std::string, int> fimap; // map of file and index
-	~CMediaHandler(); //d-tor
-	void extract(std::string srcfile, std::string dstfile, bool caseSens=true); //saves selected file
-	const char * extract (std::string srcfile, int & size); //return selecte file data, NULL if file doesn't exist
-	void extract(int index, std::string dstfile); //saves selected file
-	const char * extract (int index, int & size); //return selecte file - NIE TESTOWANE
-};
-
-class CVidHandler: public CMediaHandler
-{
-public:
-	void add_file(std::string fname);
-};
diff --git a/client/CVideoHandler.cpp b/client/CVideoHandler.cpp
index 05dadbb01..7744cbc0a 100644
--- a/client/CVideoHandler.cpp
+++ b/client/CVideoHandler.cpp
@@ -1,6 +1,5 @@
 #include "StdInc.h"
 #include <SDL.h>
-#include "CSndHandler.h"
 #include "CVideoHandler.h"
 
 #include "UIFramework/CGuiHandler.h"
diff --git a/client/CVideoHandler.h b/client/CVideoHandler.h
index b7cd484d7..fd2caffdc 100644
--- a/client/CVideoHandler.h
+++ b/client/CVideoHandler.h
@@ -1,8 +1,5 @@
 #pragma once
 
-
-#include "CSndHandler.h"
-
 struct SDL_Surface;
 
 
@@ -227,7 +224,6 @@ public:
 
 #else
 
-#include "CSndHandler.h"
 #include <SDL.h>
 #include <SDL_video.h>
 #if SDL_VERSION_ATLEAST(1,3,0)
diff --git a/client/Makefile.am b/client/Makefile.am
index aa250417c..47e7fb072 100644
--- a/client/Makefile.am
+++ b/client/Makefile.am
@@ -75,8 +75,6 @@ vcmiclient_SOURCES =  \
 	CPlayerInterface.h \
 	CPreGame.cpp \
 	CPreGame.h \
-	CSndHandler.cpp \
-	CSndHandler.h \
 	CSoundBase.h \
 	CSpellWindow.cpp \
 	CSpellWindow.h \
diff --git a/client/Makefile.in b/client/Makefile.in
index 753f7fc18..ceff7c3fe 100644
--- a/client/Makefile.in
+++ b/client/Makefile.in
@@ -86,7 +86,7 @@ am_vcmiclient_OBJECTS = vcmiclient-CCallback.$(OBJEXT) \
 	vcmiclient-Client.$(OBJEXT) vcmiclient-CMessage.$(OBJEXT) \
 	vcmiclient-CMT.$(OBJEXT) vcmiclient-CMusicHandler.$(OBJEXT) \
 	vcmiclient-CPlayerInterface.$(OBJEXT) \
-	vcmiclient-CPreGame.$(OBJEXT) vcmiclient-CSndHandler.$(OBJEXT) \
+	vcmiclient-CPreGame.$(OBJEXT) \
 	vcmiclient-CSpellWindow.$(OBJEXT) \
 	vcmiclient-CVideoHandler.$(OBJEXT) \
 	vcmiclient-CQuestLog.$(OBJEXT) vcmiclient-Graphics.$(OBJEXT) \
@@ -380,8 +380,6 @@ vcmiclient_SOURCES = \
 	CPlayerInterface.h \
 	CPreGame.cpp \
 	CPreGame.h \
-	CSndHandler.cpp \
-	CSndHandler.h \
 	CSoundBase.h \
 	CSpellWindow.cpp \
 	CSpellWindow.h \
@@ -513,7 +511,6 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiclient-CPlayerInterface.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiclient-CPreGame.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiclient-CQuestLog.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiclient-CSndHandler.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiclient-CSpellWindow.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiclient-CVideoHandler.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vcmiclient-Client.Po@am__quote@
@@ -937,20 +934,6 @@ vcmiclient-CPreGame.obj: CPreGame.cpp
 @AMDEP_TRUE@@am__fastdepCXX_FALSE@	DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
 @am__fastdepCXX_FALSE@	$(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiclient_CXXFLAGS) $(CXXFLAGS) -c -o vcmiclient-CPreGame.obj `if test -f 'CPreGame.cpp'; then $(CYGPATH_W) 'CPreGame.cpp'; else $(CYGPATH_W) '$(srcdir)/CPreGame.cpp'; fi`
 
-vcmiclient-CSndHandler.o: CSndHandler.cpp
-@am__fastdepCXX_TRUE@	$(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiclient_CXXFLAGS) $(CXXFLAGS) -MT vcmiclient-CSndHandler.o -MD -MP -MF $(DEPDIR)/vcmiclient-CSndHandler.Tpo -c -o vcmiclient-CSndHandler.o `test -f 'CSndHandler.cpp' || echo '$(srcdir)/'`CSndHandler.cpp
-@am__fastdepCXX_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/vcmiclient-CSndHandler.Tpo $(DEPDIR)/vcmiclient-CSndHandler.Po
-@AMDEP_TRUE@@am__fastdepCXX_FALSE@	$(AM_V_CXX)source='CSndHandler.cpp' object='vcmiclient-CSndHandler.o' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCXX_FALSE@	DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCXX_FALSE@	$(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiclient_CXXFLAGS) $(CXXFLAGS) -c -o vcmiclient-CSndHandler.o `test -f 'CSndHandler.cpp' || echo '$(srcdir)/'`CSndHandler.cpp
-
-vcmiclient-CSndHandler.obj: CSndHandler.cpp
-@am__fastdepCXX_TRUE@	$(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiclient_CXXFLAGS) $(CXXFLAGS) -MT vcmiclient-CSndHandler.obj -MD -MP -MF $(DEPDIR)/vcmiclient-CSndHandler.Tpo -c -o vcmiclient-CSndHandler.obj `if test -f 'CSndHandler.cpp'; then $(CYGPATH_W) 'CSndHandler.cpp'; else $(CYGPATH_W) '$(srcdir)/CSndHandler.cpp'; fi`
-@am__fastdepCXX_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/vcmiclient-CSndHandler.Tpo $(DEPDIR)/vcmiclient-CSndHandler.Po
-@AMDEP_TRUE@@am__fastdepCXX_FALSE@	$(AM_V_CXX)source='CSndHandler.cpp' object='vcmiclient-CSndHandler.obj' libtool=no @AMDEPBACKSLASH@
-@AMDEP_TRUE@@am__fastdepCXX_FALSE@	DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
-@am__fastdepCXX_FALSE@	$(AM_V_CXX@am__nodep@)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiclient_CXXFLAGS) $(CXXFLAGS) -c -o vcmiclient-CSndHandler.obj `if test -f 'CSndHandler.cpp'; then $(CYGPATH_W) 'CSndHandler.cpp'; else $(CYGPATH_W) '$(srcdir)/CSndHandler.cpp'; fi`
-
 vcmiclient-CSpellWindow.o: CSpellWindow.cpp
 @am__fastdepCXX_TRUE@	$(AM_V_CXX)$(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(vcmiclient_CXXFLAGS) $(CXXFLAGS) -MT vcmiclient-CSpellWindow.o -MD -MP -MF $(DEPDIR)/vcmiclient-CSpellWindow.Tpo -c -o vcmiclient-CSpellWindow.o `test -f 'CSpellWindow.cpp' || echo '$(srcdir)/'`CSpellWindow.cpp
 @am__fastdepCXX_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/vcmiclient-CSpellWindow.Tpo $(DEPDIR)/vcmiclient-CSpellWindow.Po
diff --git a/config/filesystem.json b/config/filesystem.json
index 6d6b8a01b..d6b69930f 100644
--- a/config/filesystem.json
+++ b/config/filesystem.json
@@ -42,7 +42,7 @@
 		],
 		"CONFIG/":
 		[
-			{"type" : "dir",  "path" : "GLOBAL/Config",} // separate to avoid overwriting global resources
+			{"type" : "dir",  "path" : "GLOBAL/Config",}, // separate to avoid overwriting global resources
 			{"type" : "dir",  "path" : "LOCAL/Config", "writeable": true}
 		],
 		"MAPS/":
diff --git a/lib/CBuildingHandler.cpp b/lib/CBuildingHandler.cpp
index 2d0f0a5f4..4e553d02f 100644
--- a/lib/CBuildingHandler.cpp
+++ b/lib/CBuildingHandler.cpp
@@ -124,9 +124,30 @@ void CBuildingHandler::loadBuildings()
 			}
 			row_num ++;
 		}
-
 		assert (row_num == 5);
 	}
+
+	// Buildings dependencies. Which building depend on which other building.
+	const JsonNode buildingsConf(ResourceID("config/buildings.json"));
+
+	// Iterate for each city type
+	int townID = 0;
+	BOOST_FOREACH(const JsonNode &town_node, buildingsConf["town_type"].Vector())
+	{
+		BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector())
+		{
+			int id = node["id"].Float();
+			CBuilding * build = buildings[townID][id];
+			if (build)
+			{
+				BOOST_FOREACH(const JsonNode &building, node["requires"].Vector())
+				{
+					build->requirements.insert(building.Float());
+				}
+			}
+		}
+		townID++;
+	}
 }
 
 CBuildingHandler::~CBuildingHandler()
diff --git a/lib/CBuildingHandler.h b/lib/CBuildingHandler.h
index 1823189ee..0047ae07f 100644
--- a/lib/CBuildingHandler.h
+++ b/lib/CBuildingHandler.h
@@ -22,13 +22,14 @@ public:
 	TResources resources;
 	std::string name;
 	std::string description;
+	std::set<int> requirements; //set of required buildings
 
 	const std::string &Name() const;
 	const std::string &Description() const;
 
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
-		h & tid & bid & resources & name & description;
+		h & tid & bid & resources & name & description & requirements;
 	}
 	CBuilding(int TID = -1, int BID = -1);
 };
diff --git a/lib/CTownHandler.cpp b/lib/CTownHandler.cpp
index d312a2d33..24822ea2f 100644
--- a/lib/CTownHandler.cpp
+++ b/lib/CTownHandler.cpp
@@ -110,16 +110,6 @@ void CTownHandler::loadStructures()
 			level ++;
 		}
 
-		// Buildings dependencies. Which building depend on which other building.
-		requirements.resize(GameConstants::F_NUMBER);
-		BOOST_FOREACH(const JsonNode &node, town_node["building_requirements"].Vector()) {
-			std::set<int> &requires = requirements[townID][node["id"].Float()];
-
-			BOOST_FOREACH(const JsonNode &building, node["requires"].Vector()) {
-				requires.insert(building.Float());
-			}
-		}
-
 		// Misc.
 		towns[townID].mageLevel = town_node["mage_guild"].Float();
 		towns[townID].primaryRes  = town_node["primary_resource"].Float();
diff --git a/lib/CTownHandler.h b/lib/CTownHandler.h
index 01599f100..a06cc88ff 100644
--- a/lib/CTownHandler.h
+++ b/lib/CTownHandler.h
@@ -64,7 +64,6 @@ class DLL_LINKAGE CTownHandler
 public:
 	std::vector<CTown> towns;
 	std::vector<std::map<int, Structure*> > structures; // <town ID, <structure ID, structure>>
-	std::vector<std::map<int,std::set<int> > > requirements; //requirements[town_id][structure_id] -> set of required buildings
 
 	CTownHandler(); //c-tor
 	~CTownHandler(); //d-tor
@@ -72,7 +71,7 @@ public:
 
 	template <typename Handler> void serialize(Handler &h, const int version)
 	{
-		h & towns & requirements;
+		h & towns;
 		if(!h.saving)
 			loadStructures();
 	}
diff --git a/lib/IGameCallback.cpp b/lib/IGameCallback.cpp
index a1e0ca0ab..a13d131e8 100644
--- a/lib/IGameCallback.cpp
+++ b/lib/IGameCallback.cpp
@@ -1014,7 +1014,7 @@ std::set<int> CGameInfoCallback::getBuildingRequiments( const CGTownInstance *t,
 
 	std::set<int> used;
 	used.insert(ID);
-	std::set<int> reqs = VLC->townh->requirements[t->subID][ID];
+	std::set<int> reqs = VLC->buildh->buildings[t->subID][ID]->requirements;
 
 	while(true)
 	{
@@ -1025,8 +1025,8 @@ std::set<int> CGameInfoCallback::getBuildingRequiments( const CGTownInstance *t,
 			{
 				used.insert(*i);
 				for(
-					std::set<int>::iterator j=VLC->townh->requirements[t->subID][*i].begin();
-					j!=VLC->townh->requirements[t->subID][*i].end();
+					std::set<int>::iterator j=VLC->buildh->buildings[t->subID][*i]->requirements.begin();
+					j!= VLC->buildh->buildings[t->subID][*i]->requirements.end();
 				j++)
 				{
 					reqs.insert(*j);//creating full list of requirements