IFileSystem: Difference between revisions
|  (include) | MOoEyThEcOw (talk | contribs)  No edit summary | ||
| Line 17: | Line 17: | ||
| ==Create Directory== | ==Create Directory== | ||
| To create a directory you need to know two things; the '''relative path''' containing the new directory name and '''Path ID''' where the path will be created — called a Path ID because it does not appear to be possible to create a directory using anything other than a search path id. | To create a directory you need to know two things; the '''relative path''' (Can include folders, i.e. scripts/weapon.txt) containing the new directory name and '''Path ID''' where the path will be created — called a Path ID because it does not appear to be possible to create a directory using anything other than a search path id. | ||
| ===Default Path Ids=== | ===Default Path Ids=== | ||
| Line 117: | Line 117: | ||
| So in this example we open <code>mylog.log</code> in '''Append''' mode.  Then we print the line <code>Logging A Test Line...</code> and close the file. | So in this example we open <code>mylog.log</code> in '''Append''' mode.  Then we print the line <code>Logging A Test Line...</code> and close the file. | ||
|  FileHandle_t File; //file wer're reading | |||
|  File = filesystem->Open( "test/test.txt", "r", "MOD"); //opens a pre-made file in the MOD/test directory called test.txt | |||
|  int size = filesystem->Size(File); //gets the size of the file | |||
|  char *Raw = new char[ size + 1 ]; //makes a pointer array 1 bigger to sign | |||
|  filesystem->Read( Raw, size, File); // reads into the pointer array, the size of the file charecters, from the file | |||
|  Raw[size] = 0; //signs | |||
|  Msg(Raw); //outputs to the command line | |||
| Toss this little bit of code into a function called by a ConCommand and it can spit out the contents of a file. | |||
| ==Search Files== | ==Search Files== | ||
Revision as of 12:07, 18 May 2008
Here you will find all the information you ever wanted to know about the File System in Source: how to open files, how to read files, how to create new directories and how to find a file on the local system.
Include
#include "Filesystem.h"
My Game Directory
This does not deal specifically with the filesystem, but you might find this useful for your project.
//Server char * pGameDir = new char[1024]; engine->GetGameDir(pGameDir, 1024); //Client const char *pGameDir = engine->GetGameDirectory();
Create Directory
To create a directory you need to know two things; the relative path (Can include folders, i.e. scripts/weapon.txt) containing the new directory name and Path ID where the path will be created — called a Path ID because it does not appear to be possible to create a directory using anything other than a search path id.
Default Path Ids
- DEFAULT_WRITE_PATH
- MOD
- GAME
- GAMEBIN
char * relpath = new char[1024]; Q_snprintf(relpath, 1024, "stuff\\%s", "newdirectory"); filesystem->CreateDirHierarchy( relpath, "MOD" );
This code will create a newdirectory in your stuff directory, which if does not exist will be created along with newdirectory inside of your MOD directory.
Delete Directory
To remove a directory, first make sure to remove all the files in that directory. Then you can call this code:
#include "direct.h"
char *toRemove = new char[1096]; toRemove[0] = 0; #ifndef CLIENT_DLL char * pGameDir = new char[1024]; engine->GetGameDir(pGameDir, 1024); #else const char *pGameDir = engine->GetGameDirectory(); #endif Q_strcat(toRemove, pGameDir); //concatenate the rest of the directory name here _rmdir(toRemove);
Copy File
 Warning:Untested
Warning:Untestedbool CopyFile(const char *from, const char *to)
{
	Assert(from);
	Assert(to);
	CUtlBuffer buf;
	// Read in file to copy
	if(filesystem->ReadFile(from, "MOD", buf))
		return filesystem->WriteFile(to, "MOD", buf); // Write out copy
	return false;
}
File I/O
I/O Modes
| r | Open a text file for reading | 
| w | Create a text file for writing | 
| a | Append to a text file | 
| rb | Open a binary file for reading | 
| wb | Create a binary file for writing | 
| ab | Append to a binary file | 
| r+ | Open a text file for read/write | 
| w+ | Create a text file for read/write | 
| a+ | Append to or create a text file for read/write | 
| rb+ | Open a binary file for read/write | 
| wb+ | Create a binary file for read/write | 
| ab+ | Append to or create a binary file for read/write | 
FileHandle_t fh;
fh = filesystem->Open( "mylog.log", "a", "MOD");
if (fh)
{
    filesystem->FPrintf(fh, "%s", "Logging A Test Line...");  
    filesystem->Close(fh);
}
or
FileHandle_t fh;
fh = filesystem->Open( "mylog.log", "a", "MOD");
if (fh)
{
    char* text = "Logging A Test Line...";
    filesystem->Write(text, Q_strlen(text), fh);  
    filesystem->Close(fh);
}
So in this example we open mylog.log in Append mode.  Then we print the line Logging A Test Line... and close the file.
FileHandle_t File; //file wer're reading File = filesystem->Open( "test/test.txt", "r", "MOD"); //opens a pre-made file in the MOD/test directory called test.txt int size = filesystem->Size(File); //gets the size of the file char *Raw = new char[ size + 1 ]; //makes a pointer array 1 bigger to sign filesystem->Read( Raw, size, File); // reads into the pointer array, the size of the file charecters, from the file Raw[size] = 0; //signs Msg(Raw); //outputs to the command line
Toss this little bit of code into a function called by a ConCommand and it can spit out the contents of a file.
Search Files
To iterate through a list of files in a given Path ID you would do the following.
FileFindHandle_t findHandle;
const char *pFilename = filesystem->FindFirstEx( "*.*", "MOD", &findHandle );
while ( pFilename != NULL )
{
    pFilename = filesystem->FindNext( findHandle );
}
filesystem->FindClose( findHandle );
*.* is the wildcard to match the files against.  "MOD" is the Path ID.
The value in pFilename for each filesystem->FindNext is nothing but the file name (i.e. for maps/d1_trainstation_01.bsp, pFilename would be d1_trainstation_01)
Mount Steam Application Content
The following code segment should be added to both CHLClient::Init in src\cl_dll\cdll_client_int.cpp and CServerGameDLL::DLLInit in src\dlls\GameInterface.cpp after filesystem is initialized.
	filesystem->AddSearchPath("path", "GAME");
	if(filesystem->MountSteamContent(-id)==FILESYSTEM_MOUNT_FAILED)
		return false;
path should be retrieved from Game Name Abbreviations
id should be retrieved from Steam Application IDs
- is a negative sign (Subtract) and is required to mount content properly.