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

Types

Source2ModDirCallback

using Source2ModDirCallback = std::function<bool(const char* name, bool isDirectory)>;

Called per directory entry; return false to stop.

Methods

ResolvePath

virtual std::string ResolvePath(const char* path) = 0;

A path as the other calls use it: absolute as given, else under the game folder.

ParameterType
pathconst char*

FileExists

virtual bool FileExists(const char* path) = 0;
ParameterType
pathconst char*

DirExists

virtual bool DirExists(const char* path) = 0;
ParameterType
pathconst char*

FileSize

virtual int64_t FileSize(const char* path) = 0;

-1 when there is no such file.

ParameterType
pathconst char*

FileTime

virtual int64_t FileTime(const char* path) = 0;

Seconds since 1970 of the last change; -1 when there is no such file.

ParameterType
pathconst char*

ReadFile

virtual bool ReadFile(const char* path, std::string& contents) = 0;

The whole file. @return false when it cannot be read.

ParameterType
pathconst char*
contentsstd::string&

WriteFile

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.

ParameterTypeDefault
pathconst char*
dataconst void*
sizesize_t
appendboolfalse

DeleteFile

virtual bool DeleteFile(const char* path) = 0;
ParameterType
pathconst char*

RenameFile

virtual bool RenameFile(const char* from, const char* to) = 0;
ParameterType
fromconst char*
toconst char*

CreateDirectory

virtual bool CreateDirectory(const char* path) = 0;

Makes a folder and those above it.

ParameterType
pathconst char*

RemoveDirectory

virtual bool RemoveDirectory(const char* path) = 0;

Removes an empty folder.

ParameterType
pathconst char*

ListDirectory

virtual bool ListDirectory(const char* path, Source2ModDirCallback callback) = 0;

The entries of a folder, without "." and "..". @return false when it cannot be read.

ParameterType
pathconst char*
callbackSource2ModDirCallback

CreateKeyValues

virtual ISource2ModKeyValues* CreateKeyValues(const char* name) = 0;

A new KeyValues tree with an empty root named @p name; Release() it.

ParameterType
nameconst char*

ParseKeyValues

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.

ParameterTypeDefault
textconst char*
listenerISource2ModKvListener*
lineint*nullptr
colint*nullptr
escapesbooltrue

ParseKeyValuesFile

virtual ESource2ModKvError ParseKeyValuesFile(const char* path, ISource2ModKvListener* listener, int* line = nullptr, int* col = nullptr, bool escapes = true) = 0;
ParameterTypeDefault
pathconst char*
listenerISource2ModKvListener*
lineint*nullptr
colint*nullptr
escapesbooltrue

KvErrorString

virtual const char* KvErrorString(ESource2ModKvError error) = 0;

What an error means.

ParameterType
errorESource2ModKvError

ISource2ModKeyValues

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.

GetName

virtual const char* GetName() = 0;

SetName

virtual void SetName(const char* name) = 0;
ParameterType
nameconst char*

FindKey

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.

ParameterTypeDefault
keyconst char*
createboolfalse

GetFirstSubKey

virtual ISource2ModKeyValues* GetFirstSubKey() = 0;

The first / next key of any kind.

GetNextKey

virtual ISource2ModKeyValues* GetNextKey() = 0;

GetFirstTrueSubKey

virtual ISource2ModKeyValues* GetFirstTrueSubKey() = 0;

The first / next key that is a section.

GetNextTrueSubKey

virtual ISource2ModKeyValues* GetNextTrueSubKey() = 0;

GetFirstValue

virtual ISource2ModKeyValues* GetFirstValue() = 0;

The first / next key that is a value.

GetNextValue

virtual ISource2ModKeyValues* GetNextValue() = 0;

GetDataType

virtual ESource2ModKvType GetDataType(const char* key = nullptr) = 0;
ParameterTypeDefault
keyconst char*nullptr

GetString

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.

ParameterTypeDefault
keyconst char*nullptr
defconst char*""

GetInt

virtual int GetInt(const char* key = nullptr, int def = 0) = 0;
ParameterTypeDefault
keyconst char*nullptr
defint0

GetFloat

virtual float GetFloat(const char* key = nullptr, float def = 0.0f) = 0;
ParameterTypeDefault
keyconst char*nullptr
deffloat0.0f

GetUint64

virtual uint64_t GetUint64(const char* key = nullptr, uint64_t def = 0) = 0;
ParameterTypeDefault
keyconst char*nullptr
defuint64_t0

GetColor

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).

ParameterTypeDefault
keyconst char*nullptr
defuint32_t0

SetString

virtual void SetString(const char* key, const char* value) = 0;
ParameterType
keyconst char*
valueconst char*

SetInt

virtual void SetInt(const char* key, int value) = 0;
ParameterType
keyconst char*
valueint

SetFloat

virtual void SetFloat(const char* key, float value) = 0;
ParameterType
keyconst char*
valuefloat

SetUint64

virtual void SetUint64(const char* key, uint64_t value) = 0;
ParameterType
keyconst char*
valueuint64_t

SetColor

virtual void SetColor(const char* key, uint32_t rgba) = 0;
ParameterType
keyconst char*
rgbauint32_t

RemoveKey

virtual bool RemoveKey(const char* key) = 0;

Removes a key under this one. @return false when it was not there.

ParameterType
keyconst char*

CopySubkeys

virtual void CopySubkeys(ISource2ModKeyValues* from) = 0;

Copies @p from's subkeys into this one; Merge replaces keys of the same name.

ParameterType
fromISource2ModKeyValues*

Merge

virtual void Merge(ISource2ModKeyValues* from) = 0;
ParameterType
fromISource2ModKeyValues*

Clone

virtual ISource2ModKeyValues* Clone() = 0;

A detached copy of this key and everything under it; Release() it.

LoadFromFile

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.

ParameterTypeDefault
pathconst char*
escapesboolfalse

LoadFromString

virtual bool LoadFromString(const char* text, bool escapes = false) = 0;
ParameterTypeDefault
textconst char*
escapesboolfalse

SaveToFile

virtual bool SaveToFile(const char* path, bool escapes = false) = 0;

Writes this key and everything under it in Valve's layout.

ParameterTypeDefault
pathconst char*
escapesboolfalse

SaveToString

virtual size_t SaveToString(char* buffer, size_t maxlen, bool escapes = false) = 0;
ParameterTypeDefault
bufferchar*
maxlensize_t
escapesboolfalse

Returns size_t — The text's length; at most @p maxlen - 1 bytes are written.

Release

virtual void Release() = 0;

Frees a tree from CreateKeyValues or Clone (only a root).

ISource2ModKvListener

Gets the reader's events in order; return HALT to stop early.

OnStart

virtual void OnStart();

OnEnterSection

virtual ESource2ModKvParseResult OnEnterSection(const char* name, bool quoted);
ParameterType
nameconst char*
quotedbool

OnKeyValue

virtual ESource2ModKvParseResult OnKeyValue(const char* key, const char* value, bool keyQuoted, bool valueQuoted);
ParameterType
keyconst char*
valueconst char*
keyQuotedbool
valueQuotedbool

OnLeaveSection

virtual ESource2ModKvParseResult OnLeaveSection();

OnRawLine

virtual ESource2ModKvParseResult OnRawLine(const char* line, int lineNumber);
ParameterType
lineconst char*
lineNumberint

OnEnd

virtual void OnEnd(bool halted, bool failed);
ParameterType
haltedbool
failedbool

Enums

ESource2ModKvType

enum ESource2ModKvType

What a key holds (SourceMod's KvDataTypes numbering).

NameValueDescription
SOURCE2MOD_KV_NONE0a section: subkeys, no value
SOURCE2MOD_KV_STRING1
SOURCE2MOD_KV_INT2
SOURCE2MOD_KV_FLOAT3
SOURCE2MOD_KV_COLOR6
SOURCE2MOD_KV_UINT647

ESource2ModKvParseResult

enum ESource2ModKvParseResult
NameValueDescription
SOURCE2MOD_KVPARSE_CONTINUE0
SOURCE2MOD_KVPARSE_HALTstop, not an error
SOURCE2MOD_KVPARSE_HALT_FAILstop with SOURCE2MOD_KVERROR_CUSTOM

ESource2ModKvError

enum ESource2ModKvError

SourceMod's SMCError numbering.

NameValueDescription
SOURCE2MOD_KVERROR_OK0
SOURCE2MOD_KVERROR_STREAM_OPEN
SOURCE2MOD_KVERROR_STREAM_ERROR
SOURCE2MOD_KVERROR_CUSTOM
SOURCE2MOD_KVERROR_INVALID_SECTION1
SOURCE2MOD_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_OVERFLOW
SOURCE2MOD_KVERROR_INVALID_PROPERTY1a key/value outside any section

On this page