+Archive* AddPk3Dir( const char* fullpath ){
+ if ( g_numDirs == VFS_MAXDIRS ) return 0;
+
+ strncpy( g_strDirs[g_numDirs], fullpath, PATH_MAX );
+ g_strDirs[g_numDirs][PATH_MAX] = '\0';
+ g_numDirs++;
+
+ {
+ archive_entry_t entry;
+ entry.name = fullpath;
+ entry.archive = OpenArchive( fullpath );
+ entry.is_pakfile = false;
+ g_archives.push_back( entry );
+
+ return entry.archive;
+ }
+}
+
+// for Daemon DPK vfs
+
+Archive* AddDpkDir( const char* fullpath ){
+ return AddPk3Dir( fullpath );
+}
+
+struct pakfile_path_t
+{
+ CopiedString fullpath; // full pak dir or pk3dir name
+ bool is_pakfile; // defines is it .pk3dir or .pk3 file
+};
+
+typedef std::pair<CopiedString, pakfile_path_t> PakfilePathsKV;
+typedef std::map<CopiedString, pakfile_path_t> PakfilePaths; // key must have no extension, only name
+
+static PakfilePaths g_pakfile_paths;
+
+void AddDpkPak( const char* name, const char* fullpath, bool is_pakfile ){
+ pakfile_path_t pakfile_path;
+ pakfile_path.fullpath = fullpath;
+ pakfile_path.is_pakfile = is_pakfile;
+ g_pakfile_paths.insert( PakfilePathsKV( name, pakfile_path ) );
+}
+
+// takes name without ext, returns without ext
+static const char* GetLatestDpkPakVersion( const char* name ){
+ const char* maxversion = 0;
+ const char* result = 0;
+ const char* pakname;
+ const char* pakversion;
+ int namelen = string_length( name );
+
+ for ( PakfilePaths::iterator i = g_pakfile_paths.begin(); i != g_pakfile_paths.end(); ++i )
+ {
+ pakname = i->first.c_str();
+ if ( strncmp( pakname, name, namelen ) != 0 || pakname[namelen] != '_' ) continue;
+ pakversion = pakname + (namelen + 1);
+ if ( maxversion == 0 || DpkPakVersionCmp( pakversion, maxversion ) > 0 ){
+ maxversion = pakversion;
+ result = pakname;
+ }
+ }
+ return result;
+}
+
+// release string after using
+static char* GetCurrentMapDpkPakName(){
+ char* mapdir;
+ char* mapname;
+ int mapnamelen;
+ char* result = 0;
+
+ mapname = string_clone( GlobalRadiant().getMapName() );
+ mapnamelen = string_length( mapname );
+
+ mapdir = strrchr( mapname, '/' );
+ if ( mapdir ) {
+ mapdir -= 12;
+ if ( strncmp( mapdir, ".dpkdir/maps/", 13 ) == 0 ) {
+ *mapdir = '\0';
+ mapdir = strrchr( mapname, '/' );
+ if ( mapdir ) mapdir++;
+ else mapdir = mapname;
+ result = string_clone( mapdir );
+ }
+ }
+
+ string_release( mapname, mapnamelen );
+ return result;
+
+}
+
+// prevent loading duplicates or circular references
+static Archives g_loaded_dpk_paks;
+
+// actual pak adding on initialise, deferred from InitDirectory
+// Daemon DPK filesystem doesn't need load all paks it finds
+static void LoadDpkPakWithDeps( const char* pakname ){
+ const char* und = strrchr( pakname, '_' );
+ if ( !und ) pakname = GetLatestDpkPakVersion( pakname );
+ if ( !pakname || g_loaded_dpk_paks.find( pakname ) != g_loaded_dpk_paks.end() ) return;
+
+ PakfilePaths::iterator i = g_pakfile_paths.find( pakname );
+ if ( i == g_pakfile_paths.end() ) return;
+
+ Archive* arc;
+ if ( i->second.is_pakfile ){
+ arc = InitPakFile( FileSystemQ3API_getArchiveModules(), i->second.fullpath.c_str() );
+ } else {
+ arc = AddDpkDir( i->second.fullpath.c_str() );
+ }
+ g_loaded_dpk_paks.insert( pakname );
+
+ ArchiveTextFile* depsFile = arc->openTextFile( "DEPS" );
+ if ( !depsFile ) return;
+
+ {
+ TextLinesInputStream<TextInputStream> istream = depsFile->getInputStream();
+
+ CopiedString line;
+ char *p_name;
+ char *p_version;
+ while ( line = istream.readLine(), string_length( line.c_str() ) ) {
+ if ( !DpkReadDepsLine( line.c_str(), &p_name, &p_version ) ) continue;
+ if ( !p_version ) {
+ const char* p_latest = GetLatestDpkPakVersion( p_name );
+ if ( p_latest ) LoadDpkPakWithDeps( p_latest );
+ } else {
+ int len = string_length( p_name ) + string_length( p_version ) + 1;
+ char* p_pakname = string_new( len );
+ sprintf( p_pakname, "%s_%s", p_name, p_version );
+ LoadDpkPakWithDeps( p_pakname );
+ string_release( p_pakname, len );
+ }
+ string_release( p_name, string_length( p_name ) );
+ if ( p_version ) string_release( p_version, string_length( p_version ) );
+ }
+ }
+
+ depsFile->release();
+}
+
+// end for Daemon DPK vfs
+