00001 /* 00002 This file is part of Advanced Strategic Command; http://www.asc-hq.de 00003 Copyright (C) 1994-2010 Martin Bickel and Marc Schellenberger 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; see the file COPYING. If not, write to the 00017 Free Software Foundation, Inc., 59 Temple Place, Suite 330, 00018 Boston, MA 02111-1307 USA 00019 */ 00020 00021 #include "packagemanager.h" 00022 #include "package.h" 00023 #include "packagerepository.h" 00024 00025 00026 static const int packageDataStreamVersion = 1; 00027 00028 void PackageData::read ( tnstream& stream ) 00029 { 00030 int v = stream.readInt(); 00031 if ( v != packageDataStreamVersion ) 00032 throw tinvalidversion ( "PackageData", packageDataStreamVersion, v ); 00033 00034 int size = stream.readInt(); 00035 for ( int i = 0; i < size; ++i ) { 00036 ASCString s = stream.readString(); 00037 Package* p = new Package(); 00038 p->read( stream ); 00039 packages[s] = p; 00040 } 00041 } 00042 00043 00044 void PackageData::write ( tnstream& stream ) const 00045 { 00046 stream.writeInt( packageDataStreamVersion ); 00047 stream.writeInt( packages.size() ); 00048 for ( Packages::const_iterator i = packages.begin(); i != packages.end(); ++i ) { 00049 stream.writeString( i->first ); 00050 i->second->write( stream ); 00051 } 00052 } 00053 00054 00055 void PackageManager::checkGame( GameMap* game ) 00056 { 00057 if( game == NULL || game->packageData == NULL ) 00058 return; 00059 00060 for ( PackageData::Packages::const_iterator i = game->packageData->packages.begin(); i != game->packageData->packages.end(); ++i ) 00061 packageRepository.checkPackageDependency( i->second, game->packageData ); 00062 } 00063 00064 void PackageManager::processContainer( const ContainerBase* container, std::set<ASCString>& archives ) 00065 { 00066 archives.insert( container->baseType->archive ); 00067 for ( ContainerBase::Production::const_iterator i = container->getProduction().begin(); i != container->getProduction().end(); ++i ) 00068 if ( *i ) { 00069 archives.insert( (*i)->archive ); 00070 } 00071 } 00072 00073 00074 void PackageManager::storeData( const GameMap* game ) 00075 { 00076 if( game->packageData == NULL ) 00077 game->packageData = new PackageData(); 00078 00079 std::set<ASCString> archives; 00080 00081 for ( int y = 0; y < game->ysize; ++y ) 00082 for ( int x = 0; x < game->xsize; ++x ) { 00083 const MapField* fld = game->getField( x,y ); 00084 archives.insert( fld->typ->terraintype->archive ); 00085 00086 for ( MapField::ObjectContainer::const_iterator o = fld->objects.begin(); o != fld->objects.end(); ++o ) 00087 archives.insert( o->typ->archive ); 00088 } 00089 for ( int p = 0; p <= 8; ++p ) { 00090 const Player& pl = game->getPlayer( p ); 00091 for ( Player::VehicleList::const_iterator i = pl.vehicleList.begin(); i != pl.vehicleList.end(); ++i ) 00092 processContainer( *i , archives ); 00093 00094 for ( Player::BuildingList::const_iterator i = pl.buildingList.begin(); i != pl.buildingList.end(); ++i ) 00095 processContainer( *i , archives ); 00096 } 00097 00098 game->packageData->packages.clear(); 00099 for ( PackageRepository::const_iterator i = packageRepository.begin(); i != packageRepository.end(); ++i ) { 00100 if ( archives.find( (*i)->archive ) != archives.end() ) 00101 game->packageData->packages[ (*i)->name ] = *i; 00102 } 00103 00104 } 00105
1.5.1