ISource2ModFiles Files, directories, KeyValues (Valve's KV1 text) and a streaming KeyValues reader.
Paths that are not absolute are under the game folder (csgo), as in SourceMod; ResolvePath says where one lands. A plugin that only needs to read and write its own files can as well use the C++ library -- these exist so plugins and SourcePawn scripts share one notion of where things are and one KeyValues.
KeyValues3 (the format CS2's own files use) is not here: the engine's KV3 (tier1/keyvalues3.h in the SDK) reads and writes it.
Interface id ISource2ModFiles001 (SOURCE2MOD_FILES_INTERFACE) · Global g_pSource2ModFiles · Header source2mod/ISource2ModFiles.h
using Source2ModDirCallback = std :: function < bool ( const char* name , bool isDirectory )>;
Called per directory entry; return false to stop.
virtual std :: string ResolvePath ( const char* path ) = 0 ;
A path as the other calls use it: absolute as given, else under the game folder.
Parameter Type pathconst char*
virtual bool FileExists ( const char* path ) = 0 ;
Parameter Type pathconst char*
virtual bool DirExists ( const char* path ) = 0 ;
Parameter Type pathconst char*
virtual int64_t FileSize ( const char* path ) = 0 ;
-1 when there is no such file.
Parameter Type pathconst char*
virtual int64_t FileTime ( const char* path ) = 0 ;
Seconds since 1970 of the last change; -1 when there is no such file.
Parameter Type pathconst char*
virtual bool ReadFile ( const char* path , std :: string & contents ) = 0 ;
The whole file. @return false when it cannot be read.
Parameter Type pathconst char*contentsstd::string&
virtual bool WriteFile ( const char* path , const void* data , size_t size , bool append = false ) = 0 ;
Creates or replaces (or appends to) a file; missing folders are made.
Parameter Type Default pathconst char*dataconst void*sizesize_tappendboolfalse
virtual bool DeleteFile ( const char* path ) = 0 ;
Parameter Type pathconst char*
virtual bool RenameFile ( const char* from , const char* to ) = 0 ;
Parameter Type fromconst char*toconst char*
virtual bool CreateDirectory ( const char* path ) = 0 ;
Makes a folder and those above it.
Parameter Type pathconst char*
virtual bool RemoveDirectory ( const char* path ) = 0 ;
Removes an empty folder.
Parameter Type pathconst char*
virtual bool ListDirectory ( const char* path , Source2ModDirCallback callback ) = 0 ;
The entries of a folder, without "." and "..". @return false when it cannot be read.
virtual ISource2ModKeyValues * CreateKeyValues ( const char* name ) = 0 ;
A new KeyValues tree with an empty root named @p name; Release() it.
Parameter Type nameconst char*
virtual ESource2ModKvError ParseKeyValues ( const char* text , ISource2ModKvListener * listener , int* line = nullptr , int* col = nullptr , bool escapes = true ) = 0 ;
Reads KeyValues text, calling @p listener per event.
virtual ESource2ModKvError ParseKeyValuesFile ( const char* path , ISource2ModKvListener * listener , int* line = nullptr , int* col = nullptr , bool escapes = true ) = 0 ;
virtual const char* KvErrorString ( ESource2ModKvError error ) = 0 ;
What an error means.
One key of a KeyValues tree: a section with subkeys, or a value.
Key arguments may be paths ("a/b/c"); names compare case-insensitively. A null or empty key means this key itself. Values convert on read (a string "5" reads as 5, an int as "5"). Pointers to keys stay valid until that key or one above it is removed, or the root is released.
virtual const char* GetName () = 0 ;
virtual void SetName ( const char* name ) = 0 ;
Parameter Type nameconst char*
virtual ISource2ModKeyValues * FindKey ( const char* key , bool create = false ) = 0 ;
A key under this one; nullptr when it is not there and @p create is false.
Parameter Type Default keyconst char*createboolfalse
virtual ISource2ModKeyValues * GetFirstSubKey () = 0 ;
The first / next key of any kind.
virtual ISource2ModKeyValues * GetNextKey () = 0 ;
virtual ISource2ModKeyValues * GetFirstTrueSubKey () = 0 ;
The first / next key that is a section.
virtual ISource2ModKeyValues * GetNextTrueSubKey () = 0 ;
virtual ISource2ModKeyValues * GetFirstValue () = 0 ;
The first / next key that is a value.
virtual ISource2ModKeyValues * GetNextValue () = 0 ;
virtual ESource2ModKvType GetDataType ( const char* key = nullptr ) = 0 ;
Parameter Type Default keyconst char*nullptr
virtual const char* GetString ( const char* key = nullptr , const char* def = "" ) = 0 ;
The value as text; valid until the key changes. @p def when there is none.
Parameter Type Default keyconst char*nullptrdefconst char*""
virtual int GetInt ( const char* key = nullptr , int def = 0 ) = 0 ;
Parameter Type Default keyconst char*nullptrdefint0
virtual float GetFloat ( const char* key = nullptr , float def = 0.0 f ) = 0 ;
Parameter Type Default keyconst char*nullptrdeffloat0.0f
virtual uint64_t GetUint64 ( const char* key = nullptr , uint64_t def = 0 ) = 0 ;
Parameter Type Default keyconst char*nullptrdefuint64_t0
virtual uint32_t GetColor ( const char* key = nullptr , uint32_t def = 0 ) = 0 ;
r | g << 8 | b << 16 | a << 24 (a "r g b a" string reads too).
Parameter Type Default keyconst char*nullptrdefuint32_t0
virtual void SetString ( const char* key , const char* value ) = 0 ;
Parameter Type keyconst char*valueconst char*
virtual void SetInt ( const char* key , int value ) = 0 ;
Parameter Type keyconst char*valueint
virtual void SetFloat ( const char* key , float value ) = 0 ;
Parameter Type keyconst char*valuefloat
virtual void SetUint64 ( const char* key , uint64_t value ) = 0 ;
Parameter Type keyconst char*valueuint64_t
virtual void SetColor ( const char* key , uint32_t rgba ) = 0 ;
Parameter Type keyconst char*rgbauint32_t
virtual bool RemoveKey ( const char* key ) = 0 ;
Removes a key under this one. @return false when it was not there.
Parameter Type keyconst char*
virtual void CopySubkeys ( ISource2ModKeyValues * from ) = 0 ;
Copies @p from's subkeys into this one; Merge replaces keys of the same name.
virtual void Merge ( ISource2ModKeyValues * from ) = 0 ;
virtual ISource2ModKeyValues * Clone () = 0 ;
A detached copy of this key and everything under it; Release() it.
virtual bool LoadFromFile ( const char* path , bool escapes = false ) = 0 ;
Replaces this key with the first section of a file / text (its name and subkeys). @return false on a missing file or bad text.
Parameter Type Default pathconst char*escapesboolfalse
virtual bool LoadFromString ( const char* text , bool escapes = false ) = 0 ;
Parameter Type Default textconst char*escapesboolfalse
virtual bool SaveToFile ( const char* path , bool escapes = false ) = 0 ;
Writes this key and everything under it in Valve's layout.
Parameter Type Default pathconst char*escapesboolfalse
virtual size_t SaveToString ( char* buffer , size_t maxlen , bool escapes = false ) = 0 ;
Parameter Type Default bufferchar*maxlensize_tescapesboolfalse
Returns size_t — The text's length; at most @p maxlen - 1 bytes are written.
virtual void Release () = 0 ;
Frees a tree from CreateKeyValues or Clone (only a root).
Gets the reader's events in order; return HALT to stop early.
virtual ESource2ModKvParseResult OnEnterSection ( const char* name , bool quoted );
Parameter Type nameconst char*quotedbool
virtual ESource2ModKvParseResult OnKeyValue ( const char* key , const char* value , bool keyQuoted , bool valueQuoted );
Parameter Type keyconst char*valueconst char*keyQuotedboolvalueQuotedbool
virtual ESource2ModKvParseResult OnLeaveSection ();
virtual ESource2ModKvParseResult OnRawLine ( const char* line , int lineNumber );
Parameter Type lineconst char*lineNumberint
virtual void OnEnd ( bool halted , bool failed );
Parameter Type haltedboolfailedbool
What a key holds (SourceMod's KvDataTypes numbering).
Name Value Description SOURCE2MOD_KV_NONE0a section: subkeys, no value SOURCE2MOD_KV_STRING1SOURCE2MOD_KV_INT2SOURCE2MOD_KV_FLOAT3SOURCE2MOD_KV_COLOR6SOURCE2MOD_KV_UINT647
enum ESource2ModKvParseResult
Name Value Description SOURCE2MOD_KVPARSE_CONTINUE0SOURCE2MOD_KVPARSE_HALTstop, not an error SOURCE2MOD_KVPARSE_HALT_FAILstop with SOURCE2MOD_KVERROR_CUSTOM
SourceMod's SMCError numbering.
Name Value Description SOURCE2MOD_KVERROR_OK0SOURCE2MOD_KVERROR_STREAM_OPENSOURCE2MOD_KVERROR_STREAM_ERRORSOURCE2MOD_KVERROR_CUSTOMSOURCE2MOD_KVERROR_INVALID_SECTION1SOURCE2MOD_KVERROR_INVALID_SECTION2{ without a name SOURCE2MOD_KVERROR_INVALID_SECTION3a lone token before } SOURCE2MOD_KVERROR_INVALID_SECTION4} without a section SOURCE2MOD_KVERROR_INVALID_SECTION5a section not closed SOURCE2MOD_KVERROR_INVALID_TOKENSan unterminated string, a lone token SOURCE2MOD_KVERROR_TOKEN_OVERFLOWSOURCE2MOD_KVERROR_INVALID_PROPERTY1a key/value outside any section