fix: add file handling functions for bot built-ins

Loading all waypoint data at once crashes on real hardware on various maps, narrowed it down to those maps with bigger waypoint files. Using a streamed file reader seems to work better but there might be a "better" fix to look at in the future.
This commit is contained in:
Michael Oliver
2026-06-13 11:12:45 +01:00
committed by mo
parent c00572ddba
commit e7f1a90440
2 changed files with 77 additions and 7 deletions
+58 -7
View File
@@ -87,6 +87,45 @@ BotBuiltinFileExists( file )
return false; return false;
} }
/*
Opens a file stream for reading.
*/
BotBuiltinOpenFile( file, mode )
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "openfile" ] ) )
{
return [[ level.bot_builtins[ "openfile" ] ]]( file, mode );
}
return -1;
}
/*
Reads the next line from the currently open file stream.
*/
BotBuiltinReadStream()
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "readstream" ] ) )
{
return [[ level.bot_builtins[ "readstream" ] ]]();
}
return undefined;
}
/*
Closes the currently open file stream.
*/
BotBuiltinCloseFile()
{
if ( isdefined( level.bot_builtins ) && isdefined( level.bot_builtins[ "closefile" ] ) )
{
return [[ level.bot_builtins[ "closefile" ] ]]();
}
return -1;
}
/* /*
Bot action, does a bot action Bot action, does a bot action
<client> botaction(<action string (+ or - then action like frag or smoke)>) <client> botaction(<action string (+ or - then action like frag or smoke)>)
@@ -1285,27 +1324,39 @@ readWpsFromFile( mapname )
{ {
return waypoints; return waypoints;
} }
openResult = BotBuiltinOpenFile( filename, "read" );
res = getWaypointLinesFromFile( filename ); if ( openResult <= 0 )
if ( !res.lines.size )
{ {
return waypoints; return waypoints;
} }
BotBuiltinPrintConsole( "Attempting to read waypoints from " + filename ); firstLine = BotBuiltinReadStream();
waypointCount = int( res.lines[ 0 ] ); if ( !isdefined( firstLine ) )
{
BotBuiltinCloseFile();
return waypoints;
}
waypointCount = int( firstLine );
for ( i = 1; i <= waypointCount; i++ ) for ( i = 1; i <= waypointCount; i++ )
{ {
tokens = strtok( res.lines[ i ], "," ); line = BotBuiltinReadStream();
if ( !isdefined( line ) )
{
break;
}
tokens = strtok( line, "," );
waypoint = parseTokensIntoWaypoint( tokens ); waypoint = parseTokensIntoWaypoint( tokens );
waypoints[ i - 1 ] = waypoint; waypoints[ i - 1 ] = waypoint;
} }
BotBuiltinCloseFile();
return waypoints; return waypoints;
} }
+19
View File
@@ -4,6 +4,9 @@ init()
level.bot_builtins[ "filewrite" ] = ::do_filewrite; level.bot_builtins[ "filewrite" ] = ::do_filewrite;
level.bot_builtins[ "fileread" ] = ::do_fileread; level.bot_builtins[ "fileread" ] = ::do_fileread;
level.bot_builtins[ "fileexists" ] = ::do_fileexists; level.bot_builtins[ "fileexists" ] = ::do_fileexists;
level.bot_builtins[ "openfile" ] = ::do_openfile;
level.bot_builtins[ "readstream" ] = ::do_readstream;
level.bot_builtins[ "closefile" ] = ::do_closefile;
level.bot_builtins[ "botaction" ] = ::do_botaction; level.bot_builtins[ "botaction" ] = ::do_botaction;
level.bot_builtins[ "botstop" ] = ::do_botstop; level.bot_builtins[ "botstop" ] = ::do_botstop;
level.bot_builtins[ "botmovement" ] = ::do_botmovement; level.bot_builtins[ "botmovement" ] = ::do_botmovement;
@@ -35,6 +38,22 @@ do_fileexists( file )
return fileexists( file ); return fileexists( file );
} }
do_openfile( file, mode )
{
file = "scriptdata/" + file;
return openfile( file, mode );
}
do_readstream()
{
return readstream();
}
do_closefile()
{
return closefile();
}
do_botaction( action ) do_botaction( action )
{ {
self botaction( action ); self botaction( action );