From 386e87d063c43fa5c47b6107ccdef0ef842a6ec1 Mon Sep 17 00:00:00 2001 From: ineed bots Date: Fri, 1 Dec 2023 15:23:40 -0600 Subject: [PATCH] Add perline reading/writing fileio again --- maps/mp/bots/_bot_utility.gsc | 111 ++++++++-------------------------- maps/mp/bots/_wp_editor.gsc | 12 +++- scripts/bots_adapter.gsc | 5 +- 3 files changed, 37 insertions(+), 91 deletions(-) diff --git a/maps/mp/bots/_bot_utility.gsc b/maps/mp/bots/_bot_utility.gsc index 5baca93..481040b 100644 --- a/maps/mp/bots/_bot_utility.gsc +++ b/maps/mp/bots/_bot_utility.gsc @@ -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++ ) + for ( line = BotBuiltinReadLine( f ); isDefined( line ); line = BotBuiltinReadLine( f ) ) { - c = names_str[i]; - - if ( c == "\n" ) - { - level.bot_names[level.bot_names.size] = line; - - line = ""; - continue; - } - - line += c; + level.bot_names[level.bot_names.size] = line; } + + 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,24 +1548,36 @@ 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 ); - for ( i = 1; i <= waypointCount; i++ ) + if ( isDefined( line ) ) { - tokens = tokenizeLine( res.lines[i], "," ); + waypointCount = int( line ); - waypoint = parseTokensIntoWaypoint( tokens ); + for ( i = 1; i <= waypointCount; i++ ) + { + line = BotBuiltinReadLine( f ); - waypoints[i - 1] = waypoint; + if ( !isDefined( line ) ) + break; + + tokens = strtok( line, "," ); + + waypoint = parseTokensIntoWaypoint( tokens ); + + waypoints[i - 1] = waypoint; + } } + BotBuiltinFileClose( f ); + return waypoints; } diff --git a/maps/mp/bots/_wp_editor.gsc b/maps/mp/bots/_wp_editor.gsc index b7ba3ff..1eb261c 100644 --- a/maps/mp/bots/_wp_editor.gsc +++ b/maps/mp/bots/_wp_editor.gsc @@ -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 ); diff --git a/scripts/bots_adapter.gsc b/scripts/bots_adapter.gsc index a5f2508..228c91c 100644 --- a/scripts/bots_adapter.gsc +++ b/scripts/bots_adapter.gsc @@ -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 )