~ubuntu-branches/ubuntu/maverick/lordsawar/maverick

« back to all changes in this revision

Viewing changes to src/citysetlist.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barry deFreese
  • Date: 2010-04-10 09:29:33 UTC
  • mfrom: (1.1.9 upstream) (5.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100410092933-23uq4dxig30kmtcw
Tags: 0.1.8-1
* New upstream release.
* Add misc:Depends for -data package.
* Bump Standards Version to 3.8.4. (No changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
    s_instance = 0;
50
50
}
51
51
 
52
 
void Citysetlist::loadCitysets(std::list<std::string> citysets, bool p)
 
52
void Citysetlist::loadCitysets(std::list<std::string> citysets)
53
53
{
54
54
    for (std::list<std::string>::const_iterator i = citysets.begin(); 
55
55
         i != citysets.end(); i++)
56
56
      {
57
 
        if (loadCityset(*i, p) == true)
58
 
          {
59
 
            iterator it = end();
60
 
            it--;
61
 
            if (d_citysetids.find((*it)->getId()) != d_citysetids.end())
62
 
              {
63
 
                Cityset *c = (*d_citysetids.find((*it)->getId())).second;
64
 
                cerr << "Error!  cityset: `" << (*it)->getName() << 
65
 
                  "' has a duplicate cityset id with `" << File::getCityset(c) << 
66
 
                  "'.  Skipping." << endl;
67
 
                continue;
68
 
              }
69
 
            (*it)->setSubDir(*i);
70
 
            d_dirs[String::ucompose("%1 %2", (*it)->getName(), (*it)->getTileSize())] = *i;
71
 
            d_citysets[*i] = *it;
72
 
            d_citysetids[(*it)->getId()] = *it;
73
 
          }
 
57
        Cityset *cityset = loadCityset(*i);
 
58
        if (!cityset)
 
59
          continue;
 
60
 
 
61
        add(cityset);
74
62
      }
75
63
}
76
64
 
77
65
Citysetlist::Citysetlist()
78
66
{
79
67
    // load all citysets
80
 
    std::list<std::string> citysets = File::scanCitysets();
81
 
    loadCitysets(citysets, false);
82
 
    citysets = File::scanUserCitysets();
83
 
    loadCitysets(citysets, true);
 
68
    std::list<std::string> citysets = Cityset::scanSystemCollection();
 
69
    loadCitysets(citysets);
 
70
    citysets = Cityset::scanUserCollection();
 
71
    loadCitysets(citysets);
84
72
 
85
73
}
86
74
 
107
95
  return names;
108
96
}
109
97
 
110
 
bool Citysetlist::loadCityset(std::string name, bool p)
 
98
Cityset *Citysetlist::loadCityset(std::string name)
111
99
{
112
 
  debug("Loading cityset " <<name);
 
100
  debug("Loading cityset " <<File::get_basename(File::get_dirname(name)));
113
101
 
114
 
  Cityset *cityset = Cityset::create(name, p);
 
102
  Cityset *cityset = Cityset::create(name);
115
103
  if (!cityset)
116
 
    return false;
117
 
  if (d_citysetids.find(cityset->getId()) != d_citysetids.end())
118
 
    {
119
 
      Cityset *c = (*d_citysetids.find(cityset->getId())).second;
120
 
      cerr << "Error!  cityset: `" << cityset->getName() << 
121
 
        "' shares a duplicate cityset id with `" << File::getCityset(c) << 
 
104
    return NULL;
 
105
    
 
106
  if (cityset->validate() == false)
 
107
    {
 
108
      cerr << "Error!  cityset: `" << cityset->getName() << 
 
109
        "' is invalid.  Skipping." << endl;
 
110
      delete cityset;
 
111
      return NULL;
 
112
    }
 
113
 
 
114
  if (d_citysetids.find(cityset->getId()) != d_citysetids.end())
 
115
    {
 
116
      Cityset *c = (*d_citysetids.find(cityset->getId())).second;
 
117
      cerr << "Error!  cityset: `" << cityset->getName() << 
 
118
        "' shares a duplicate cityset id with `" << c->getConfigurationFile() 
 
119
        << "'.  Skipping." << endl;
 
120
      delete cityset;
 
121
      return NULL;
 
122
    }
 
123
 
 
124
  if (d_citysetids.find(cityset->getId()) != d_citysetids.end())
 
125
    {
 
126
      Cityset *c = (*d_citysetids.find(cityset->getId())).second;
 
127
      cerr << "Error!  cityset: `" << cityset->getName() << 
 
128
        "' has a duplicate cityset id with `" << c->getConfigurationFile() << 
122
129
        "'.  Skipping." << endl;
123
130
      delete cityset;
124
 
      return false;
 
131
      return NULL;
125
132
    }
 
133
  return cityset;
 
134
}
 
135
 
 
136
void Citysetlist::add(Cityset *cityset)
 
137
{
 
138
  std::string subdir = File::get_basename(cityset->getDirectory());
126
139
  push_back(cityset);
127
 
 
128
 
  return true;
 
140
  cityset->setSubDir(subdir);
 
141
  d_dirs[String::ucompose("%1 %2", cityset->getName(), cityset->getTileSize())] = subdir;
 
142
  d_citysets[subdir] = cityset;
 
143
  d_citysetids[cityset->getId()] = cityset;
129
144
}
130
145
 
131
146
void Citysetlist::getSizes(std::list<guint32> &sizes)
141
156
{
142
157
  return d_dirs[String::ucompose("%1 %2", name, tilesize)];
143
158
}
 
159
void Citysetlist::instantiateImages()
 
160
{
 
161
  for (iterator it = begin(); it != end(); it++)
 
162
    (*it)->instantiateImages();
 
163
}
 
164
void Citysetlist::uninstantiateImages()
 
165
{
 
166
  for (iterator it = begin(); it != end(); it++)
 
167
    (*it)->uninstantiateImages();
 
168
}
 
169
        
 
170
Cityset *Citysetlist::getCityset(guint32 id) 
 
171
 
172
  if (d_citysetids.find(id) == d_citysetids.end())
 
173
    return NULL;
 
174
  return d_citysetids[id];
 
175
}
 
176
        
 
177
Cityset *Citysetlist::getCityset(std::string dir) 
 
178
 
179
  if (d_citysets.find(dir) == d_citysets.end())
 
180
    return NULL;
 
181
  return d_citysets[dir];
 
182
}
 
183
bool Citysetlist::addToPersonalCollection(Cityset *cityset, std::string &new_subdir, guint32 &new_id)
 
184
{
 
185
  //do we already have this one?
 
186
  if (getCityset(cityset->getSubDir()) == getCityset(cityset->getId()) &&
 
187
      getCityset(cityset->getSubDir()) != NULL)
 
188
    {
 
189
      cityset->setDirectory(getCityset(cityset->getId())->getDirectory());
 
190
      return true;
 
191
    }
 
192
 
 
193
  //if the subdir conflicts with any other subdir, then change it.
 
194
  if (getCityset(cityset->getSubDir()) != NULL)
 
195
    {
 
196
      bool found = false;
 
197
      for (int count = 0; count < 100; count++)
 
198
        {
 
199
          new_subdir = String::ucompose("%1%2", cityset->getSubDir(), count);
 
200
          if (getCityset(new_subdir) == NULL)
 
201
            {
 
202
              found = true;
 
203
              break;
 
204
            }
 
205
        }
 
206
      if (found == false)
 
207
        return false;
 
208
      cityset->setSubDir(new_subdir);
 
209
    }
 
210
  else
 
211
    new_subdir = cityset->getSubDir();
 
212
 
 
213
  //if the id conflicts with any other id, then change it
 
214
  if (getCityset(cityset->getId()) != NULL)
 
215
    {
 
216
      new_id = Citysetlist::getNextAvailableId(cityset->getId());
 
217
      cityset->setId(new_id);
 
218
    }
 
219
  else
 
220
    new_id = cityset->getId();
 
221
 
 
222
  //make the directory where the cityset is going to live.
 
223
  std::string directory = 
 
224
    File::getUserCitysetDir() + cityset->getSubDir() + "/";
 
225
 
 
226
  if (File::create_dir(directory) == false)
 
227
    return false;
 
228
 
 
229
  //okay now we copy the image files into the new directory 
 
230
  std::list<std::string> files;
 
231
  cityset->getFilenames(files);
 
232
  for (std::list<std::string>::iterator it = files.begin(); it != files.end();
 
233
       it++)
 
234
    File::copy(cityset->getFile(*it), directory + *it);
 
235
 
 
236
  //save out the cityset file
 
237
  cityset->setDirectory(directory);
 
238
  XML_Helper helper(cityset->getConfigurationFile(), std::ios::out, false);
 
239
  cityset->save(&helper);
 
240
  helper.close();
 
241
  return true;
 
242
}
 
243
 
 
244
int Citysetlist::getNextAvailableId(int after)
 
245
{
 
246
  std::list<guint32> ids;
 
247
  std::list<std::string> citysets = Cityset::scanSystemCollection();
 
248
  //there might be IDs in invalid armysets.
 
249
  for (std::list<std::string>::const_iterator i = citysets.begin(); 
 
250
       i != citysets.end(); i++)
 
251
    {
 
252
      Cityset *cityset = Cityset::create(*i);
 
253
      if (cityset != NULL)
 
254
        {
 
255
          ids.push_back(cityset->getId());
 
256
          delete cityset;
 
257
        }
 
258
    }
 
259
  citysets = Cityset::scanUserCollection();
 
260
  for (std::list<std::string>::const_iterator i = citysets.begin(); 
 
261
       i != citysets.end(); i++)
 
262
    {
 
263
      Cityset *cityset = Cityset::create(*i);
 
264
      if (cityset != NULL)
 
265
        {
 
266
          ids.push_back(cityset->getId());
 
267
          delete cityset;
 
268
        }
 
269
    }
 
270
  for (guint32 i = after + 1; i < 1000000; i++)
 
271
    {
 
272
      if (find(ids.begin(), ids.end(), i) == ids.end())
 
273
        return i;
 
274
    }
 
275
  return -1;
 
276
}