Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vsfilesystem.cpp File Reference
#include <stdio.h>
#include <assert.h>
#include <stdarg.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include "config.h"
#include "configxml.h"
#include "vsfilesystem.h"
#include "vs_globals.h"
#include "vegastrike.h"
#include "common/common.h"
#include "galaxy_gen.h"
#include "pk3.h"
#include "gnuhash.h"

Go to the source code of this file.

Namespaces

 VSFileSystem
 

Constant Groups

 VSFileSystem
 

Macros

#define CASE(a)
 
#define CASE(a)
 
#define CASE(a)
 

Functions

int VSFS_DEBUG ()
 
void usedSerial (ObjSerial ser, bool used)
 
ObjSerial getUniqueSerial ()
 
string GetUnitDir (string filename)
 
int selectdirs (const struct dirent *entry)
 
int selectpk3s (const struct dirent *entry)
 
int selectbigpk3s (const struct dirent *entry)
 
void VSFileSystem::ChangeToProgramDirectory (char *argv0)
 
VSError VSFileSystem::CachedFileLookup (FileLookupCache &cache, const string &file, VSFileType type)
 
void VSFileSystem::DisplayType (VSFileSystem::VSFileType type)
 
void VSFileSystem::DisplayType (VSFileSystem::VSFileType type, std::ostream &ostr)
 
int VSFileSystem::GetReadBytes (char *fmt, va_list ap)
 
string VSFileSystem::HOMESUBDIR (".vegastrike")
 
std::string VSFileSystem::GetHashName (const std::string &name)
 
std::string VSFileSystem::GetHashName (const std::string &name, const Vector &scale, int faction)
 
std::string VSFileSystem::GetSharedMeshHashName (const std::string &name, const Vector &scale, int faction)
 
std::string VSFileSystem::GetSharedTextureHashName (const std::string &name)
 
std::string VSFileSystem::GetSharedSoundHashName (const std::string &name)
 
std::string VSFileSystem::MakeSharedPathReturnHome (const std::string &newpath)
 
std::string VSFileSystem::MakeSharedPath (const std::string &s)
 
std::string VSFileSystem::MakeSharedStarSysPath (const std::string &s)
 
std::string VSFileSystem::GetCorrectStarSysPath (const std::string &name, bool &autogenerated)
 
FILE * VSFileSystem::vs_open (const char *filename, const char *mode)
 
size_t VSFileSystem::vs_read (void *ptr, size_t size, size_t nmemb, FILE *fp)
 
size_t VSFileSystem::vs_write (const void *ptr, size_t size, size_t nmemb, FILE *fp)
 
void VSFileSystem::vs_close (FILE *fp)
 
int VSFileSystem::vs_fprintf (FILE *fp, const char *format,...)
 
void VSFileSystem::vs_dprintf (char level, const char *format,...)
 
int VSFileSystem::vs_fseek (FILE *fp, long offset, int whence)
 
long VSFileSystem::vs_ftell (FILE *fp)
 
void VSFileSystem::vs_rewind (FILE *fp)
 
bool VSFileSystem::vs_feof (FILE *fp)
 
long VSFileSystem::vs_getsize (FILE *fp)
 
void VSFileSystem::InitHomeDirectory ()
 
void VSFileSystem::InitDataDirectory ()
 
void VSFileSystem::LoadConfig (string subdir)
 
void VSFileSystem::InitMods ()
 
void VSFileSystem::InitPaths (string conf, string subdir)
 
void VSFileSystem::CreateDirectoryAbs (const char *filename)
 
void VSFileSystem::CreateDirectoryAbs (const string &filename)
 
void VSFileSystem::CreateDirectoryHome (const char *filename)
 
void VSFileSystem::CreateDirectoryHome (const string &filename)
 
void VSFileSystem::CreateDirectoryData (const char *filename)
 
void VSFileSystem::CreateDirectoryData (const string &filename)
 
bool VSFileSystem::DirectoryExists (const char *filename)
 
bool VSFileSystem::DirectoryExists (const string &filename)
 
int VSFileSystem::FileExists (const string &root, const char *filename, VSFileType type, bool lookinvolume)
 
int VSFileSystem::FileExists (const string &root, const string &filename, VSFileType type, bool lookinvolume)
 
int VSFileSystem::FileExistsData (const char *filename, VSFileType type)
 
int VSFileSystem::FileExistsData (const string &filename, VSFileType type)
 
int VSFileSystem::FileExistsHome (const char *filename, VSFileType type)
 
int VSFileSystem::FileExistsHome (const string &filename, VSFileType type)
 
VSError VSFileSystem::GetError (const char *str)
 
VSError VSFileSystem::LookForFile (const string &file, VSFileType type, VSFileMode mode)
 
VSError VSFileSystem::LookForFile (VSFile &f, VSFileType type, VSFileMode mode)
 
static void VSFileSystem::pathAppend (string &dest, string &suffix)
 
std::ostream & operator<< (std::ostream &ostr, VSFileSystem::VSError err)
 
std::ostream & operator<< (std::ostream &ostr, VSFileSystem::VSFileType type)
 
std::string nameof (VSFileSystem::VSFileType type)
 

Variables

char * CONFIGFILE
 
char pwd [65536]
 
VSVolumeType isin_bigvolumes = VSFSNone
 
string curmodpath = ""
 
ObjSerial serial_seed = 0
 
std::map< ObjSerial, boolusedSerials
 
string selectcurrentdir
 
std::string VSFileSystem::vegastrike_cwd
 
bool VSFileSystem::use_volumes
 
string VSFileSystem::volume_format
 
enum VSVolumeFormat VSFileSystem::q_volume_format
 
vector< vector< string > > VSFileSystem::SubDirectories
 
vector< string > VSFileSystem::Directories
 
vector< string > VSFileSystem::Rootdir
 
string VSFileSystem::sharedtextures
 
string VSFileSystem::sharedunits
 
string VSFileSystem::sharedsounds
 
string VSFileSystem::sharedmeshes
 
string VSFileSystem::sharedsectors
 
string VSFileSystem::sharedcockpits
 
string VSFileSystem::shareduniverse
 
string VSFileSystem::aidir
 
string VSFileSystem::sharedanims
 
string VSFileSystem::sharedvideos
 
string VSFileSystem::sharedsprites
 
string VSFileSystem::savedunitpath
 
string VSFileSystem::modname
 
string VSFileSystem::moddir
 
string VSFileSystem::datadir
 
string VSFileSystem::homedir
 
string VSFileSystem::config_file
 
string VSFileSystem::weapon_list
 
string VSFileSystem::universe_name
 
vector< string > VSFileSystem::current_path
 
vector< string > VSFileSystem::current_directory
 
vector< string > VSFileSystem::current_subdirectory
 
vector< VSFileType > VSFileSystem::current_type
 
vector< std::string > VSFileSystem::savedpwd
 
vector< std::string > VSFileSystem::curdir
 
vector< std::vector
< std::string > > 
VSFileSystem::savedcurdir
 
vector< intVSFileSystem::UseVolumes
 
string VSFileSystem::failed
 
vsUMap< string, CPK3 * > VSFileSystem::pk3_opened_files
 

Macro Definition Documentation

#define CASE (   a)
Value:
case a: \
ostr<<#a; break;

Definition at line 1965 of file vsfilesystem.cpp.

#define CASE (   a)
Value:
case a: \
ostr<<#a; break;

Definition at line 1965 of file vsfilesystem.cpp.

#define CASE (   a)
Value:
case a: \
return #a; break;

Definition at line 1965 of file vsfilesystem.cpp.

Function Documentation

ObjSerial getUniqueSerial ( )

Definition at line 61 of file vsfilesystem.cpp.

References fprintf, MAXSERIAL, serial_seed, SERVER, usedSerial(), and usedSerials.

Referenced by Mission::call_unit_launch(), UniverseUtil::ComputeSystemSerials(), Unit::EjectCargo(), and Unit::Fire().

62 {
63  int offset = (SERVER ? 2 : 1);
64  size_t maxserial=(1<<(sizeof(ObjSerial)*8));
65  if (usedSerials.size()>=maxserial/3) {
66  static bool firstBadness=true;
67  if (firstBadness) {
68  fprintf(stderr,"Error recycling used serial since serial map is completely full at size %d\n",(int)usedSerials.size());
69  firstBadness=false;
70  }
71  return ((rand()%(maxserial/3-1))+1)*3;//OFFSET..set to emergency zero
72  }
73  std::map< ObjSerial, bool >::const_iterator iter;
74  ObjSerial ret;
75  do {
77  ret = serial_seed+offset;
78  } while ( ( iter = usedSerials.find( ret ) ) != usedSerials.end()
79  && (*iter).second );
80  usedSerial( ret, true );
81  return ret;
82 }
string GetUnitDir ( string  filename)
std::string nameof ( VSFileSystem::VSFileType  type)

Definition at line 1999 of file vsfilesystem.cpp.

References VSFileSystem::AccountFile, VSFileSystem::AiFile, VSFileSystem::AnimFile, CASE, VSFileSystem::CockpitFile, VSFileSystem::CommFile, VSFileSystem::MeshFile, VSFileSystem::MissionFile, VSFileSystem::MusicFile, VSFileSystem::PythonFile, VSFileSystem::SaveFile, VSFileSystem::SoundFile, VSFileSystem::SystemFile, VSFileSystem::TextureFile, VSFileSystem::UnitFile, VSFileSystem::UniverseFile, VSFileSystem::UnknownFile, VSFileSystem::VSSpriteFile, and VSFileSystem::ZoneBuffer.

Referenced by VSFileSystem::FileExists().

2000 {
2001 #define CASE( a ) \
2002 case a: \
2003  return #a; break;
2004  switch (type)
2005  {
2007  CASE( VSFileSystem::SystemFile )
2008  CASE( VSFileSystem::CockpitFile )
2009  CASE( VSFileSystem::UnitFile )
2010  CASE( VSFileSystem::PythonFile )
2011  CASE( VSFileSystem::TextureFile )
2012  CASE( VSFileSystem::SoundFile )
2013  CASE( VSFileSystem::MeshFile )
2014  CASE( VSFileSystem::CommFile )
2015  CASE( VSFileSystem::AiFile )
2016  CASE( VSFileSystem::SaveFile )
2017  CASE( VSFileSystem::AnimFile )
2018  CASE( VSFileSystem::VSSpriteFile )
2019  CASE( VSFileSystem::MissionFile )
2020  CASE( VSFileSystem::MusicFile )
2021  CASE( VSFileSystem::AccountFile )
2022  CASE( VSFileSystem::ZoneBuffer )
2023  CASE( VSFileSystem::UnknownFile )
2024  default:
2025  return "VSFileSystem::<undefined VSFileType>";
2026 
2027  break;
2028  }
2029 #undef CASE
2030 }
std::ostream& operator<< ( std::ostream &  ostr,
VSFileSystem::VSError  err 
)

Definition at line 1968 of file vsfilesystem.cpp.

References VSFileSystem::BadFormat, CASE, VSFileSystem::DownloadInterrupted, VSFileSystem::EndOfFile, VSFileSystem::FileNotFound, VSFileSystem::IncompleteRead, VSFileSystem::IncompleteWrite, VSFileSystem::IsDirectory, VSFileSystem::LocalPermissionDenied, VSFileSystem::Ok, VSFileSystem::RemotePermissionDenied, VSFileSystem::Shared, VSFileSystem::SocketError, and VSFileSystem::Unspecified.

1969 {
1970  switch (err)
1971  {
1973  CASE( VSFileSystem::Ok )
1974  CASE( VSFileSystem::SocketError )
1975  CASE( VSFileSystem::FileNotFound )
1976  CASE( VSFileSystem::LocalPermissionDenied )
1977  CASE( VSFileSystem::RemotePermissionDenied )
1978  CASE( VSFileSystem::DownloadInterrupted )
1979  CASE( VSFileSystem::IncompleteWrite )
1980  CASE( VSFileSystem::IncompleteRead )
1981  CASE( VSFileSystem::EndOfFile )
1982  CASE( VSFileSystem::IsDirectory )
1983  CASE( VSFileSystem::BadFormat )
1984  CASE( VSFileSystem::Unspecified )
1985  default:
1986  ostr<<"VSFileSystem::<undefined VSError>";
1987  break;
1988  }
1989  return ostr;
1990 }
std::ostream& operator<< ( std::ostream &  ostr,
VSFileSystem::VSFileType  type 
)

Definition at line 1993 of file vsfilesystem.cpp.

References VSFileSystem::DisplayType().

1994 {
1995  VSFileSystem::DisplayType( type, ostr );
1996  return ostr;
1997 }
int selectbigpk3s ( const struct dirent *  entry)

Definition at line 123 of file vsfilesystem.cpp.

125 {
126  //If this is a regular file and we have ".pk3" in it
127  if ( ( string( entry->d_name ).find( "data.pk3" ) ) != std::string::npos )
128  return 1;
129  return 0;
130 }
int selectdirs ( const struct dirent *  entry)

Definition at line 91 of file vsfilesystem.cpp.

References selectcurrentdir.

Referenced by VSFileSystem::InitMods().

93 {
94  if (string( entry->d_name ) == "." && string( entry->d_name ) == "..")
95  return 0;
96  //Have to check if we have the full path or just relative (which would be a problem)
97  std::string tmp = selectcurrentdir+'/'+entry->d_name;
98  //cerr<<"Read directory entry : "<< tmp <<endl;
99  struct stat s;
100  if (stat( tmp.c_str(), &s ) < 0)
101  return 0;
102  if ( (s.st_mode&S_IFDIR) )
103  return 1;
104  return 0;
105 }
int selectpk3s ( const struct dirent *  entry)

Definition at line 110 of file vsfilesystem.cpp.

112 {
113  //If this is a regular file and we have ".pk3" in it
114  if ( ( string( entry->d_name ).find( ".pk3" ) ) != std::string::npos && ( string( entry->d_name ).find( "data" ) )
115  == std::string::npos )
116  return 1;
117  return 0;
118 }
void usedSerial ( ObjSerial  ser,
bool  used 
)

Definition at line 57 of file vsfilesystem.cpp.

References usedSerials.

Referenced by getUniqueSerial().

58 {
59  usedSerials[ser] = used;
60 }
int VSFS_DEBUG ( )

Definition at line 41 of file vsfilesystem.cpp.

References VegaConfig::getVariable(), XMLSupport::parse_int(), and vs_config.

Referenced by VSFileSystem::VSFile::Close(), VSFileSystem::FileExists(), VSFileSystem::LookForFile(), VSFileSystem::VSFile::OpenReadOnly(), VSFileSystem::VSFile::ReadLine(), and VSFileSystem::vs_open().

42 {
43  if (vs_config) {
44  static int vs_debug = XMLSupport::parse_int( vs_config->getVariable( "general", "debug_fs", "0" ) );
45  return vs_debug;
46  }
47  return 0;
48 }

Variable Documentation

char* CONFIGFILE
string curmodpath = ""

Definition at line 52 of file vsfilesystem.cpp.

Referenced by VSFileSystem::InitMods().

VSVolumeType isin_bigvolumes = VSFSNone

Definition at line 51 of file vsfilesystem.cpp.

Referenced by VSFileSystem::FileExists(), and VSFileSystem::LookForFile().

char pwd[65536]
string selectcurrentdir

Definition at line 86 of file vsfilesystem.cpp.

Referenced by VSFileSystem::InitMods(), and selectdirs().

ObjSerial serial_seed = 0
std::map< ObjSerial, bool > usedSerials

Definition at line 55 of file vsfilesystem.cpp.

Referenced by getUniqueSerial(), and usedSerial().