1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
/*
* This file is part of NumptyPhysics
* Copyright (C) 2008 Tim Edmonds
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
*/
#include <cstring>
#include <sys/types.h>
#include <dirent.h>
#include "Levels.h"
#include "ZipFile.h"
#include "Config.h"
#include "Os.h"
using namespace std;
static const char MISC_COLLECTION[] = "My Levels";
static int rankFromPath( const string& p, int defaultrank=9999 )
{
if (p==MISC_COLLECTION) {
return 10000;
}
const char *c = p.data();
size_t i = p.rfind(Os::pathSep);
if ( i != string::npos ) {
c += i+1;
if ( *c=='L' || *c == 'C' ){
c++;
int rank=0;
while ( *c>='0' && *c<='9' ) {
rank = rank*10 + (*c)-'0';
c++;
}
return rank;
} else {
c++;
}
}
return defaultrank;
}
std::string nameFromPath(const std::string& path)
{
// TODO extract name from collection manifest
std::string name;
size_t i = path.rfind(Os::pathSep);
if ( i != string::npos ) {
i++;
} else {
i = 0;
}
if (path[i] == 'C') i++;
if (path[i] == 'L') i++;
while (path[i] >= '0' && path[i] <= '9') i++;
if (path[i] == '_') i++;
size_t e = path.rfind('.');
name = path.substr(i,e-i);
for (i=0; i<name.size(); i++) {
if (name[i]=='-' || name[i]=='_' || name[i]=='.') {
name[i] = ' ';
}
}
return name;
}
Levels::Levels( int numFiles, const char** names )
: m_numLevels(0)
{
for ( int d=0;d<numFiles;d++ ) {
addPath( names[d] );
}
}
bool Levels::addPath( const char* path )
{
int len = strlen( path );
if ( strcasecmp( path+len-4, ".npz" )==0 ) {
scanCollection( string(path), rankFromPath(path) );
} else if ( strcasecmp( path+len-4, ".nph" )==0
|| strcasecmp( path+len-4, ".npd" )==0) {
addLevel( path, rankFromPath(path) );
} else {
DIR *dir = opendir( path );
if ( dir ) {
struct dirent* entry;
while ( (entry = readdir( dir )) != NULL ) {
if ( entry->d_name[0] != '.' ) {
string full( path );
full += "/";
full += entry->d_name;
//DANGER - recursion may not halt for linked dirs
addPath( full.c_str() );
}
}
closedir( dir );
} else {
//printf("bogus level path %s\n",path);
}
}
return true;
}
bool Levels::addLevel( const string& file, int rank, int index )
{
return addLevel( getCollection(MISC_COLLECTION), file, rank, index );
}
bool Levels::addLevel( Collection* collection,
const string& file, int rank, int index )
{
LevelDesc *e = new LevelDesc( file, rank, index );
for ( int i=0; i<collection->levels.size(); i++ ) {
if ( collection->levels[i]->file == file
&& collection->levels[i]->index == index ) {
//printf("addLevel %s already present!\n",file.c_str());
return false;
} else if ( collection->levels[i]->rank > rank ) {
//printf("insert level %s+%d at %d\n",file.c_str(),index,i);
collection->levels.insert(i,e);
m_numLevels++;
return true;
}
}
collection->levels.append( e );
//printf("add level %s+%d as %s[%d]\n",file.c_str(),index,
// collection->file.c_str(), collection->levels.size());
m_numLevels++;
return true;
}
Levels::Collection* Levels::getCollection( const std::string& file )
{
for (int i=0; i<m_collections.size(); i++) {
if (m_collections[i]->file == file) {
return m_collections[i];
}
}
Collection *c = new Collection();
//fprintf(stderr,"New Collection %s\n",file.c_str());
c->file = file;
c->name = file;
c->rank = rankFromPath(file);
for (int i=0; i<m_collections.size(); i++) {
if (m_collections[i]->rank > c->rank) {
m_collections.insert(i,c);
return c;
}
}
m_collections.append(c);
return c;
}
bool Levels::scanCollection( const std::string& file, int rank )
{
try {
ZipFile zf(file);
Collection *collection = getCollection(file);
//printf("found collection %s with %d levels\n",file.c_str(),zf.numEntries());
for ( int i=0; i<zf.numEntries(); i++ ) {
addLevel( collection, file, rankFromPath(zf.entryName(i),rank), i );
}
} catch (...) {
fprintf(stderr,"invalid collection %s\n",file.c_str());
}
return false;
}
int Levels::numLevels()
{
return m_numLevels;
}
int Levels::load( int i, unsigned char* buf, int bufLen )
{
int l = 0;
LevelDesc *lev = findLevel(i);
if (lev) {
if ( lev->index >= 0 ) {
ZipFile zf( lev->file.c_str() );
if ( lev->index < zf.numEntries() ) {
unsigned char* d = zf.extract( lev->index, &l);
if ( d && l <= bufLen ) {
memcpy( buf, d, l );
}
}
} else {
FILE *f = fopen( lev->file.c_str(), "rt" );
if ( f ) {
l = fread( buf, 1, bufLen, f );
fclose(f);
}
}
return l;
}
throw "invalid level index";
}
std::string Levels::levelName( int i, bool pretty )
{
std::string s = "end";
LevelDesc *lev = findLevel(i);
if (lev) {
if ( lev->index >= 0 ) {
ZipFile zf( lev->file.c_str() );
s = zf.entryName( lev->index );
} else {
s = lev->file;
}
} else {
s = "err";
}
return pretty ? nameFromPath(s) : s;
}
int Levels::numCollections()
{
return m_collections.size();
}
int Levels::collectionFromLevel( int i, int *indexInCol )
{
if (i < m_numLevels) {
for ( int c=0; c<m_collections.size(); c++ ) {
if ( i >= m_collections[c]->levels.size() ) {
i -= m_collections[c]->levels.size();
} else {
if (indexInCol) *indexInCol = i;
return c;
}
}
}
return -1;
}
std::string Levels::collectionName( int i, bool pretty )
{
if (i>=0 && i<numCollections()) {
if (pretty) {
return nameFromPath(m_collections[i]->name);
} else {
return m_collections[i]->name;
}
}
return "Bad Collection ID";
}
int Levels::collectionSize(int c)
{
if (c>=0 && c<numCollections()) {
return m_collections[c]->levels.size();
}
return 0;
}
int Levels::collectionLevel(int c, int i)
{
if (c>=0 && c<numCollections()) {
if (i>=0 && i<m_collections[c]->levels.size()) {
int l = i;
for (int j=0; j<c; j++) {
l += m_collections[j]->levels.size();
}
return l;
}
}
return 0;
}
std::string Levels::demoPath(int l)
{
int c = collectionFromLevel(l);
std::string path = Config::userDataDir() + Os::pathSep
+ "Recordings" + Os::pathSep
+ collectionName(c,false);
if (path.substr(path.length()-4) == ".npz") {
path.resize(path.length()-4);
}
return path;
}
std::string Levels::demoName(int l)
{
std::string name = levelName(l,false);
size_t sep = name.rfind(Os::pathSep);
if (sep != std::string::npos) {
name = name.substr(sep);
}
if (name.substr(name.length()-4) == ".nph") {
name.resize(name.length()-4);
}
return demoPath(l) + Os::pathSep + name + ".npd";
}
bool Levels::hasDemo(int l)
{
return OS->exists(demoName(l));
}
Levels::LevelDesc* Levels::findLevel( int i )
{
if (i < m_numLevels) {
for ( int c=0; c<m_collections.size(); c++ ) {
if ( i >= m_collections[c]->levels.size() ) {
//fprintf(stderr,"index %d not in c%d (size=%d)\n",i,c,m_collections[c]->levels.size());
i -= m_collections[c]->levels.size();
} else {
return m_collections[c]->levels[i];
}
}
}
return NULL;
}
int Levels::findLevel( const char *file )
{
int index = 0;
for ( int c=0; c<m_collections.size(); c++ ) {
for ( int i=0; i<m_collections[c]->levels.size(); i++ ) {
if ( m_collections[c]->levels[i]->file == file ) {
return index + i;
}
}
index += m_collections[c]->levels.size();
}
return -1;
}
|