[NodeMod Core]
/ .. / default
Class: default
Defined in: src/native/file.ts:74
File system utilities for Half-Life engine providing safe file operations with automatic memory management. Supports loading game resources, configuration files, and provides path utilities for Half-Life directory structure.
Example
// Load and read a configuration file
const config = nodemodCore.file.loadConfig('server.cfg');
if (config) {
console.log(`Max players\: ${config['maxplayers']}`);
}
// Check if a sound file exists
if (nodemodCore.file.isValidSoundFile('weapons/ak47-1.wav')) {
console.log('Sound file is available');
}
// Load file with automatic cleanup
const content = nodemodCore.file.readText('motd.txt');
if (content) {
console.log('MOTD\:', content);
}
// Watch a file for changes
const stopWatching = nodemodCore.file.watch('banned.cfg', (event) => {
console.log(`Banned list updated\: ${event.newSize} bytes`);
});
Constructors
Constructor
new default():
NodemodFile
Returns
NodemodFile
Properties
PATHS
static
PATHS:object
Defined in: src/native/file.ts:395
Common Half-Life server file paths.
CONFIG
CONFIG:
string
='server.cfg'
Main server configuration file
BANNED
BANNED:
string
='banned.cfg'
Banned players list
LISTIP
LISTIP:
string
='listip.cfg'
IP ban list
MAPCYCLE
MAPCYCLE:
string
='mapcycle.txt'
Map rotation cycle
MOTD
MOTD:
string
='motd.txt'
Message of the day
EXTENSIONS
static
EXTENSIONS:object
Defined in: src/native/file.ts:411
Valid file extensions for different resource types.
MODEL
MODEL:
string
[]
Model file extensions
SOUND
SOUND:
string
[]
Sound file extensions
MAP
MAP:
string
[]
Map file extensions
IMAGE
IMAGE:
string
[]
Image file extensions
SPRITE
SPRITE:
string
[]
Sprite file extensions
CONFIG
CONFIG:
string
[]
Configuration file extensions
Methods
load()
load(
filename
):null
|number
[]
Defined in: src/native/file.ts:93
Loads a file into memory and returns the raw buffer. Remember to call free() when done to prevent memory leaks.
Parameters
filename
string
Path to the file to load
Returns
null
| number
[]
Raw file buffer as number array, or null if loading failed
Example
const buffer = nodemodCore.file.load('maps/de_dust2.bsp');
if (buffer) {
console.log(`Loaded ${buffer.length} bytes`);
// Don't forget to free the buffer!
nodemodCore.file.free(buffer);
}
free()
free(
buffer
):void
Defined in: src/native/file.ts:104
Parameters
buffer
any
Returns
void
getSize()
getSize(
filename
):number
Defined in: src/native/file.ts:111
Parameters
filename
string
Returns
number
compareTime()
compareTime(
filename1
,filename2
):number
Defined in: src/native/file.ts:116
Parameters
filename1
string
filename2
string
Returns
number
exists()
exists(
filename
):boolean
Defined in: src/native/file.ts:122
Parameters
filename
string
Returns
boolean
loadWithCleanup()
loadWithCleanup<
T
>(filename
,callback
):null
|T
Defined in: src/native/file.ts:128
Type Parameters
T
T
Parameters
filename
string
callback
Returns
null
| T
readText()
readText(
filename
):null
|string
Defined in: src/native/file.ts:175
Reads a file as text with automatic memory management. Handles encoding detection and cleanup automatically.
Parameters
filename
string
Path to the text file to read
Returns
null
| string
File content as string, or null if reading failed
Example
// Read server configuration
const motd = nodemodCore.file.readText('motd.txt');
if (motd) {
console.log('Message of the Day\:', motd);
}
// Read map list
const mapList = nodemodCore.file.readText('mapcycle.txt');
if (mapList) {
const maps = mapList.split('\\n').filter(line => line.trim());
console.log('Available maps\:', maps);
}
getGameDir()
getGameDir():
string
Defined in: src/native/file.ts:189
Returns
string
joinPath()
joinPath(...
parts
):string
Defined in: src/native/file.ts:196
Parameters
parts
...string
[]
Returns
string
normalizePath()
normalizePath(
path
):string
Defined in: src/native/file.ts:200
Parameters
path
string
Returns
string
getExtension()
getExtension(
filename
):string
Defined in: src/native/file.ts:204
Parameters
filename
string
Returns
string
getBasename()
getBasename(
filename
):string
Defined in: src/native/file.ts:209
Parameters
filename
string
Returns
string
getDirectory()
getDirectory(
filename
):string
Defined in: src/native/file.ts:216
Parameters
filename
string
Returns
string
buildResourcePath()
buildResourcePath(
type
,name
):string
Defined in: src/native/file.ts:222
Parameters
type
string
name
string
Returns
string
resourceExists()
resourceExists(
type
,name
):boolean
Defined in: src/native/file.ts:236
Parameters
type
string
name
string
Returns
boolean
loadResource()
loadResource(
type
,name
):null
|number
[]
Defined in: src/native/file.ts:242
Parameters
type
string
name
string
Returns
null
| number
[]
loadMultiple()
loadMultiple(
filenames
):object
Defined in: src/native/file.ts:248
Parameters
filenames
string
[]
Returns
object
freeMultiple()
freeMultiple(
buffers
):void
Defined in: src/native/file.ts:263
Parameters
buffers
{[key
: string
]: number
[] | FileLoadResult
; } | number
[][]
Returns
void
isValidModelFile()
isValidModelFile(
filename
):boolean
Defined in: src/native/file.ts:278
Parameters
filename
string
Returns
boolean
isValidSoundFile()
isValidSoundFile(
filename
):boolean
Defined in: src/native/file.ts:283
Parameters
filename
string
Returns
boolean
isValidMapFile()
isValidMapFile(
filename
):boolean
Defined in: src/native/file.ts:288
Parameters
filename
string
Returns
boolean
loadConfig()
loadConfig(
filename
):null
| {[key
:string
]:string
; }
Defined in: src/native/file.ts:317
Loads and parses a configuration file with key=value format. Supports comments starting with // or # and handles empty lines.
Parameters
filename
string
Name of the configuration file
Returns
null
| {[key
: string
]: string
; }
Parsed configuration object, or null if loading failed
Example
// Load server configuration
const config = nodemodCore.file.loadConfig('server.cfg');
if (config) {
const maxPlayers = parseInt(config['maxplayers'] || '16');
const serverName = config['hostname'] || 'Unnamed Server';
console.log(`Server\: ${serverName}, Max Players\: ${maxPlayers}`);
}
// Load custom plugin config
const pluginConfig = nodemodCore.file.loadConfig('myplugin.cfg');
if (pluginConfig?.enabled === '1') {
console.log('Plugin is enabled');
}
watch()
watch(
filename
,callback
,interval
): () =>void
Defined in: src/native/file.ts:367
Monitors a file for changes using periodic polling. Returns a function to stop monitoring.
Parameters
filename
string
File to monitor
callback
Function called when file changes
interval
number
= 1000
Check interval in milliseconds (default: 1000)
Returns
Function to stop monitoring
():
void
Returns
void
Example
// Watch configuration file for changes
const stopWatching = nodemodCore.file.watch('server.cfg', (event) => {
console.log(`Config changed! Old size\: ${event.oldSize}, New size\: ${event.newSize}`);
// Reload configuration
const newConfig = nodemodCore.file.loadConfig('server.cfg');
});
// Stop watching after 10 minutes
setTimeout(() => {
stopWatching();
console.log('Stopped watching file');
}, 600000);