Add perline reading/writing fileio again

This commit is contained in:
ineed bots 2023-12-01 15:23:40 -06:00
parent dc58bf328a
commit 386e87d063
3 changed files with 37 additions and 91 deletions

View File

@ -1465,34 +1465,6 @@ FrontLinesWaypoints()
return waypoints;
}
/*
Tokenizes a string (strtok has limits...) (only one char tok)
*/
tokenizeLine( line, tok )
{
tokens = [];
token = "";
for ( i = 0; i < line.size; i++ )
{
c = line[i];
if ( c == tok )
{
tokens[tokens.size] = token;
token = "";
continue;
}
token += c;
}
tokens[tokens.size] = token;
return tokens;
}
/*
Parses tokens into a waypoint obj
*/
@ -1541,26 +1513,16 @@ getABotName()
if ( BotBuiltinFileExists( filename ) )
{
names_str = BotBuiltinFileRead( filename );
f = BotBuiltinFileOpen( filename, "read" );
if ( isDefined( names_str ) )
if ( f > 0 )
{
line = "";
for ( i = 0; i < names_str.size; i++ )
{
c = names_str[i];
if ( c == "\n" )
for ( line = BotBuiltinReadLine( f ); isDefined( line ); line = BotBuiltinReadLine( f ) )
{
level.bot_names[level.bot_names.size] = line;
line = "";
continue;
}
line += c;
}
BotBuiltinFileClose( f );
}
}
}
@ -1575,41 +1537,6 @@ getABotName()
return name;
}
/*
Returns an array of each line
*/
getWaypointLinesFromFile( filename )
{
result = spawnStruct();
result.lines = [];
waypointStr = BotBuiltinFileRead( filename );
if ( !isDefined( waypointStr ) )
return result;
line = "";
for ( i = 0; i < waypointStr.size; i++ )
{
c = waypointStr[i];
if ( c == "\n" )
{
result.lines[result.lines.size] = line;
line = "";
continue;
}
line += c;
}
result.lines[result.lines.size] = line;
return result;
}
/*
Read from file a csv, and returns an array of waypoints
*/
@ -1621,23 +1548,35 @@ readWpsFromFile( mapname )
if ( !BotBuiltinFileExists( filename ) )
return waypoints;
res = getWaypointLinesFromFile( filename );
f = BotBuiltinFileOpen( filename, "read" );
if ( !res.lines.size )
if ( f < 1 )
return waypoints;
BotBuiltinPrintConsole( "Attempting to read waypoints from " + filename );
waypointCount = int( res.lines[0] );
line = BotBuiltinReadLine( f );
if ( isDefined( line ) )
{
waypointCount = int( line );
for ( i = 1; i <= waypointCount; i++ )
{
tokens = tokenizeLine( res.lines[i], "," );
line = BotBuiltinReadLine( f );
if ( !isDefined( line ) )
break;
tokens = strtok( line, "," );
waypoint = parseTokensIntoWaypoint( tokens );
waypoints[i - 1] = waypoint;
}
}
BotBuiltinFileClose( f );
return waypoints;
}

View File

@ -315,7 +315,10 @@ watchSaveWaypointsCommand()
PrintLn( "********* Start Bot Warfare WPDump *********" );
PrintLn( level.waypointCount );
BotBuiltinFileWrite( filename, level.waypointCount + "\n", "write" );
f = BotBuiltinFileOpen( filename, "write" );
if ( f > 0 )
BotBuiltinWriteLine( f, level.waypointCount );
for ( i = 0; i < level.waypointCount; i++ )
{
@ -342,9 +345,14 @@ watchSaveWaypointsCommand()
str += ",";
PrintLn( str );
BotBuiltinFileWrite( filename, str + "\n", "append" );
if ( f > 0 )
BotBuiltinWriteLine( f, str );
}
if ( f > 0 )
BotBuiltinFileClose( f );
PrintLn( "\n\n\n\n\n\n" );
self iprintln( "Saved!!! to " + filename );

View File

@ -90,10 +90,9 @@ do_fs_fopen( file, mode )
return FS_FOpen( file, mode );
}
do_fs_fclose( file )
do_fs_fclose( fh )
{
file = "scriptdata/" + file;
FS_FClose( file );
FS_FClose( fh );
}
do_fs_readline( fh )