<?php
/*
This is a tool written mostly for myself, it does not do any changes to the
source files you present it and it should not break anything on your
computer.

t2maptools - save & restore object names & cleanup .mis files for Tribes2
Copyright (C) 2003 Sprottlefreak
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
The License is also available online at
http://www.gnu.org/copyleft/gpl.html
*/
define ( "SAVEPATH", "/usr/web/telefrag.com/temp" );

function
hidden_input ($fname, $fval) {
  echo (
"<INPUT TYPE=HIDDEN NAME=\"$fname\" VALUE=\"$fval\">\n");
}

function
submit($desc) {
  echo (
"<INPUT TYPE=HIDDEN NAME='PHPSESSID' VALUE='" . session_id() . "'>\n");
  echo (
"<INPUT TYPE='SUBMIT' VALUE=\"$desc\">\n");
}
$lines_to_delete = array (
              
"damageTimeMS",
              
"deleteLastProjectile",
              
"inUse",
              
"lastDamagedBy",
              
"lastDamagedByTeam",
              
"lastProjectile",
              
"ObjectiveCompleted",
              
"prevTeam",
              
"ready",
              
"recharged",
              
"repairedBy",
              
"soiledByEnemyRepair",
              
"teamDamageStateOnZap",
              
"VehicleHercVehicle",
              
"wasDisabled",
              
"zappingSound",
              
"zapSound"
              
);


if (
$mode == "" OR $mode == "main" ) {
  print
"What is this? It's a small web utility to help Tribes2 mappers with a few annoying quirks of the Tribes2 map editor. It has the following features:<BR /><UL>\n";
  print
"<LI><B>Save NameTags</B> - By far the most annoying thing about the T2 editor is that it overwrites all your carefully named Objects with things like \\x01693 - this operation adds a dynamic field to your object saving your original name...</LI>\n";
  print
"<LI><B>Restore NameTags</B> - ...and this option restores your saved names from the specially named dynamic field over the cryptic \\x01693 lines - IF you saved them earlier.</LI>\n";
  print
"<LI><B>Delete garbage lines</B> - This deletes the following lines from your .mis file:<BR /><UL>";
  foreach (
$lines_to_delete as $ltd ) {
    print
"  <LI>$ltd</LI>\n";
  }
  print
"</UL>\n"; // end inner UL
  
print "</LI>\n"; // end LI 3
  
print "</UL>\n";
  print
"All operations are done on my webserver, presenting the changed .mis file in a textarea for you to copy - no changes are made to your local version (which I couldn't do even If I wanted, this being a web application ;-) ). So you can just try it, review the changes made and then save the output from the textarea to your mission file (after backing it up).<BR />";
  print
"I hope this is useful - comments go to (email) sprottlefreak -AT- nope.org<BR />\n";
  echo (
'<FORM ENCTYPE="multipart/form-data" ACTION="' . $PHP_SELF . '" METHOD=POST>');
  echo (
'Mission file to operate on: <input type="file" name="probe"><BR>');
  echo (
"Operation: <SELECT NAME='mode'>\n");
  print
" <OPTION VALUE='removelines'>Delete garbage lines</OPTION>\n";
  print
" <OPTION VALUE='savenames'>Save NameTags</OPTION>\n";
  print
" <OPTION VALUE='restorenames'>Restore NameTags</OPTION>\n";
  echo (
"</SELECT><BR><br>\n");
  
hidden_input ( "MAX_FILE_SIZE", "16000000" );
  
hidden_input ( "upload", "1" );
  print
"<INPUT TYPE=\"CHECKBOX\" NAME=\"wantfooter\" VALUE=\"yes\"> Add Debug Output to .mis file</INPUT><br>\n";
  print
"<INPUT TYPE=\"CHECKBOX\" NAME=\"removesprotsaves\" VALUE=\"yes\"> Remove my saved names from .mis file when deleting lines - do this before releasing your map</INPUT><br>\n";
  
submit("Execute selected operation");
  echo (
"</FORM>\n");
  print
"If you want to checkout the source code to this program, see <A
HREF=\""
. $PHP_SELF . "s\">the full source of this page</A><br>\n";
}

if (
$upload ) {
  
$upsize = $HTTP_POST_FILES['probe']['size'];
  
$origname = addslashes($HTTP_POST_FILES['probe']['name']);
  if ( (
$upsize>0) ) {
    
$filename = "mis_check_" . $REMOTE_ADDR . time() . ".mis";
    
move_uploaded_file($probe, SAVEPATH . "/$filename");
    
$operate_on = SAVEPATH . "/$filename";
  } else {
// error uploading pic
    
echo ("ERROR uploading .mis file");
    
$error = TRUE;
  }
}

if (
$error ){
  print
"cannot continue.";
  die();
}

if (
$mode == "removelines" ) {
  
// remove all lines with these fields
  
  // delete sprot's saves as well.
  
if ( $removesprotsaves == "yes" ) {
    
$lines_to_delete[] = "SprotSaved";
  }

  
$farr = file ( $operate_on );
  foreach (
$farr as $line ) {
    
$numlines++;
    
$work = trim ( $line );
    
$keepline = TRUE;
    foreach (
$lines_to_delete as $ltd ) {
      if (
substr ( $work, 0, strlen ( $ltd ) ) == $ltd ) {
        
// beginning of line is identical to a string to delete
        
$keepline = FALSE;
    
$outfooter .= "// deleted at line $numlines: $work\n";
        
$deletedlines++;
        break (
1);
      }
    }
    if (
$keepline ) $out.=  $line;
  }
  
  
$outfooter.=  "\n// SprotCleanLines removed $deletedlines lines\n";
}

if (
$mode == "savenames" ) {
  
$farr = file ( $operate_on );
  
$overwrittenlines = 0;
  for (
$i=0; $i < sizeof ( $farr ); $i++ ) {
    
$line = $farr[$i];
    
$numlines++;
    
$work = trim ( $line );
    
$isnameline = FALSE;
    
$was_overwritten = FALSE;
    unset (
$saveline );
    if (
substr ( $work, 0, 7 ) == "nameTag" ) {
      
// beginning of line is identical to a nameTag line
      // need to search inside the object block for the nameTag line
      
$objbegin = $i;
      while ( !
ereg ( ".+[a-zA-Z0-9]\) \{.*", $farr[$objbegin] ) ) {
    
$objbegin--;
      }
      
// after this, we got the object's beginning line
      
while ( trim ( $farr[$objbegin] ) != "};" ) {
    
// now walk through the block until we find the closing bracket
    // this needs to be done to find old backups & overwrite them
    
$workon = trim ( $farr[$objbegin] );
    if (
substr ( $workon, 0, 17 ) == "SprotSavednameTag" ) {
      
// found an old line - replace it. Since dynamic fields come AFTER the main fields we can just
      // change farr and be done with it ;-)
      
$farr[$objbegin] = str_replace ( "nameTag", "SprotSavednameTag", $line );
      
$was_overwritten = TRUE;
      
$outfooter.= "// saved (overwrote old) at line $objbegin: $work\n";
      
$namelines++;
      
$overwrittenlines++;
      
//      break(0);
    
}
    
$objbegin++;
      }
      if ( !
$was_overwritten ) {
    
// new line needs to be saved
    // nameTag = "Vehicle Pad";
    
$saveline = str_replace ( "nameTag", "SprotSavednameTag", $line );
    
$outfooter.= "// saved at line $numlines: $work\n";
    
$namelines++;
      }
    }
    
$out.=  $line;
    if (
strlen ( trim ( $saveline ) ) > 3 ) $out.=  "\n" . $saveline;
  }
  
$outfooter.=  "// " . date ( "Y-m-d H:i", time() ) . " CEST  -  SprotSaveNames saved $namelines names ($overwrittenlines overwritten)\n";
}  

if (
$mode == "restorenames" ) {
  
$farr = file ( $operate_on );
  for (
$i=0; $i < sizeof ( $farr ); $i++ ) {
    
$line = $farr[$i];
    
$numlines++;
    
$work = trim ( $line );
    
$isnameline = FALSE;
    if (
substr ( $work, 0, 10 ) == "SprotSaved" ) {
      
// potentially have a saved Name
      // need to search inside the object block for the nameTag line

      
$objbegin = $i;
      while ( !
ereg ( ".+[a-zA-Z0-9]\) \{.*", $farr[$objbegin] ) ) {
    
$objbegin--;
      }
      
// after this, we got the object's beginning line
      
while ( trim ( $farr[$objbegin] ) != "};" ) {
    
// now walk through the block until we find the closing bracket
    
    
$workon = trim ( $farr[$objbegin] );
    if (
substr ( $workon, 0, 7 ) == "nameTag" ) {
      
$outfooter.= "// restored at line " . ( $objbegin + 1 ) . ": was '" . trim ( $farr[$objbegin] ) . "' -&gt; set from backup to '";
      
$farr[$objbegin] = str_replace ( "SprotSaved", "", $line );
      
$outfooter.= trim ( $farr[$objbegin] ) . "'\n";
      
$namelines++;
      break (
1);
    }
    
$objbegin++;
      }
    }
    
$farr[$i]= str_replace ( "\n", "", $line );
  }
  
$out = implode ("\n", $farr);
  
$outfooter.= "\n";
  
$outfooter.= "// " . date ( "Y-m-d H:i", time() ) . " CEST  -  SprotSaveNames restored $namelines names\n";
}  

// display results
if ( $operate_on ) {
  print
"<H2>Results of '$mode' on '$origname'</H2>\n";
  print
"<FORM METHOD=\"POST\">\n";
  print
"<TEXTAREA WRAP=VIRTUAL ROWS=20 COLS=80>" . $out;
  if (
$wantfooter=="yes" ) {
    
// only add the footer debug into mis file when checkbox ticked
    
print $outfooter;
  }
  print
"</TEXTAREA>\n";
  print
"</FORM>\n";
  print
"<H3>Additional Info:</H3>\n";
  print
"<PRE>$outfooter</PRE>\n";
  
// cleanup
  
unlink ( $operate_on );
}

?>