Clover coverage report -
Coverage timestamp: Sun Oct 12 2003 22:57:21 PDT
file stats: LOC: 615   Methods: 21
NCLOC: 301   Classes: 9
 
 Source file Conditionals Statements Methods TOTAL
Version2Update.java 0% 0% 0% 0%
coverage
 1   
 /*
 2   
 
 3   
 VRMoo Server - Virtual Reality Object Oriented MUD Server
 4   
 Copyright (C) 2001 - 2003  VRMoo Development Team
 5   
 
 6   
 
 7   
 This program is free software; you can redistribute it and/or modify
 8   
 it under the terms of the GNU General Public License as published by
 9   
 the Free Software Foundation; either version 2 of the License, or
 10   
 (at your option) any later version.
 11   
 
 12   
 This program is distributed in the hope that it will be useful,
 13   
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   
 GNU General Public License for more details.
 16   
 
 17   
 You should have received a copy of the GNU General Public License
 18   
 along with this program; if not, write to the Free Software
 19   
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20   
 
 21   
 
 22   
 For information about VRMoo and its authors, please visit the website:
 23   
 http://www.vrmoo.org/
 24   
 
 25   
 */
 26   
 
 27   
 package org.vrmoo.server.data.update;
 28   
 
 29   
 import java.io.BufferedReader;
 30   
 import java.io.File;
 31   
 import java.io.IOException;
 32   
 import java.io.PrintWriter;
 33   
 import java.util.ArrayList;
 34   
 import java.util.List;
 35   
 
 36   
 import org.vrmoo.common.exception.VRMooParseException;
 37   
 import org.vrmoo.common.util.FileUtility;
 38   
 import org.vrmoo.common.util.ParserUtility;
 39   
 
 40   
 /**
 41   
  * Update the VRMoo Server data directory from version 2 to version 3.
 42   
  *
 43   
  * @author Jeff Weston
 44   
  */
 45   
 public class Version2Update extends Update
 46   
 {
 47   
     /**
 48   
      * Update the VRMoo Server data directory from version 2 to version 3.
 49   
      *
 50   
      * @throws IOException           for any IO errors that occur
 51   
      * @throws VRMooParseException   for any parsing errors that occur
 52   
      */
 53  0
     public void update( ) throws IOException, VRMooParseException
 54   
     {
 55  0
         System.out.println(
 56   
                 "Updating data directory from version 2 to version 3." );
 57   
 
 58  0
         File worldsDir = new File( "data/worlds" );
 59  0
         FileUtility.ensureDirectoryExists( worldsDir );
 60   
 
 61  0
         File[ ] files = worldsDir.listFiles( );
 62  0
         for ( int i = 0; i < files.length; i++ )
 63   
         {
 64  0
             if ( files[ i ].isDirectory( ) )
 65   
             {
 66  0
                 handleObjects( files[ i ].getName( ) );
 67   
             }
 68   
         }
 69   
 
 70  0
         PrintWriter printWriter = FileUtility.getPrintWriter(
 71   
                 "data/version.dat" );
 72  0
         printWriter.println( 3 );
 73  0
         printWriter.close( );
 74   
     }
 75   
 
 76   
     /**
 77   
      * Update the object definitions for the specified world.
 78   
      *
 79   
      * @param worldName   the world to update
 80   
      *
 81   
      * @throws IOException   for any IO errors that occur
 82   
      * @throws VRMooParseException   for any parsing errors that occur
 83   
      */
 84  0
     private void handleObjects( String worldName )
 85   
             throws IOException, VRMooParseException
 86   
     {
 87  0
         List lines = new ArrayList( );
 88  0
         BufferedReader reader = FileUtility.getBufferedReader(
 89   
                 "data/worlds/" + worldName + "/objects.dat" );
 90   
 
 91  0
         String line = reader.readLine( );
 92   
 
 93  0
         while ( line != null )
 94   
         {
 95  0
             ObjectDataUpdater objectData = new ObjectDataUpdater( line );
 96  0
             lines.add( objectData.toEncodedString( ) );
 97  0
             line = reader.readLine( );
 98   
         }
 99  0
         reader.close( );
 100   
 
 101  0
         PrintWriter writer = FileUtility.getPrintWriter(
 102   
                 "data/worlds/" + worldName + "/objects.dat" );
 103   
 
 104  0
         for ( int i = 0; i < lines.size( ); i++ )
 105   
         {
 106  0
             writer.println( lines.get( i ) );
 107   
         }
 108  0
         writer.close( );
 109   
     }
 110   
 
 111   
     /**
 112   
      * Helper class for parsing the old version of an object definition and
 113   
      * outputing the new version.
 114   
      */
 115   
     private class ObjectDataUpdater
 116   
     {
 117   
         /**
 118   
          * The name of the object definition.
 119   
          */
 120   
         private String name;
 121   
 
 122   
         /**
 123   
          * The quad data of the object definition.
 124   
          */
 125   
         private QuadArrayDataUpdater quadArrayData;
 126   
 
 127   
         /**
 128   
          * The triangle data of the object definition.
 129   
          */
 130   
         private TriangleArrayDataUpdater triangleArrayData;
 131   
 
 132   
         /**
 133   
          * Parse object data from an encoded string.
 134   
          *
 135   
          * @param data   the data to parse
 136   
          *
 137   
          * @throws VRMooParseException   for any parsing errors that occur
 138   
          */
 139  0
         public ObjectDataUpdater( String data ) throws VRMooParseException
 140   
         {
 141  0
             int nameStart = data.indexOf( "\"" );
 142  0
             int nameEnd   = data.indexOf( "\"", nameStart + 1 );
 143   
 
 144  0
             if ( ( nameStart != -1 ) &&
 145   
                  ( nameEnd   != -1 ) )
 146   
             {
 147  0
                 name = data.substring( nameStart + 1, nameEnd );
 148   
             }
 149   
             else
 150   
             {
 151  0
                 throw new VRMooParseException( "Unable to parse object name." );
 152   
             }
 153   
 
 154  0
             int quadStart       = data.indexOf( "{"                );
 155  0
             int quadEnd         = data.indexOf( "}", quadStart     );
 156  0
             int triangleStart   = data.indexOf( "{", quadEnd       );
 157  0
             int triangleEnd     = data.indexOf( "}", triangleStart );
 158   
 
 159  0
             if ( ( quadStart       != -1 ) &&
 160   
                  ( quadEnd         != -1 ) &&
 161   
                  ( triangleStart   != -1 ) &&
 162   
                  ( triangleEnd     != -1 ) )
 163   
             {
 164  0
                 String quad = data.substring( quadStart + 1,
 165   
                                               quadEnd );
 166   
 
 167  0
                 String triangle = data.substring( triangleStart + 1,
 168   
                                                   triangleEnd );
 169   
 
 170  0
                 quadArrayData     = new QuadArrayDataUpdater    ( quad       );
 171  0
                 triangleArrayData = new TriangleArrayDataUpdater( triangle   );
 172   
             }
 173   
             else
 174   
             {
 175  0
                 throw new VRMooParseException( "Unable to parse object data." );
 176   
             }
 177   
         }
 178   
 
 179   
         /**
 180   
          * Get a string with the object definition encoded in it.
 181   
          *
 182   
          * @return the encoded data of the object definition
 183   
          */
 184  0
         public String toEncodedString( )
 185   
         {
 186  0
             return "\"" + name + "\", " +
 187   
                    "{ " + quadArrayData.toEncodedString( )     + " }, " +
 188   
                    "{ " + triangleArrayData.toEncodedString( ) + " }";
 189   
         }
 190   
     }
 191   
 
 192   
     /**
 193   
      * Helper class for parsing the old version of a data array and outputing
 194   
      * the new version.
 195   
      */
 196   
     private abstract class ArrayDataUpdater
 197   
     {
 198   
         /**
 199   
          * The array of data being managed by this class.
 200   
          */
 201   
         private List dataList;
 202   
 
 203   
         /**
 204   
          * Parse array data from an encoded string.
 205   
          *
 206   
          * @param data   the data to parse
 207   
          *
 208   
          * @throws VRMooParseException   for any parsing errors that occur
 209   
          */
 210  0
         public ArrayDataUpdater( String data ) throws VRMooParseException
 211   
         {
 212  0
             data = data.trim( );
 213  0
             dataList = new ArrayList( );
 214  0
             if ( data.indexOf( getDataType( ) ) != 0 )
 215   
             {
 216  0
                 throw new VRMooParseException(
 217   
                         "Invalid data type encountered. " +
 218   
                         "Expected " + getDataType( ) + "." );
 219   
             }
 220  0
             parseArray( data.substring( data.indexOf( ":" )  + 1 ) );
 221   
         }
 222   
 
 223   
         /**
 224   
          * Get a string with the array data encoded in it.
 225   
          *
 226   
          * @return the encoded data of the array
 227   
          */
 228  0
         public String toEncodedString( )
 229   
         {
 230  0
             StringBuffer result = new StringBuffer( );
 231  0
             result.append( getDataType( ) );
 232  0
             result.append( ": " );
 233   
 
 234  0
             for ( int i = 0; i < dataList.size( ); i++ )
 235   
             {
 236  0
                 ItemDataUpdater itemData =
 237   
                         ( ItemDataUpdater ) dataList.get( i );
 238  0
                 if ( i != 0 )
 239   
                 {
 240  0
                     result.append( ", " );
 241   
                 }
 242  0
                 result.append( "[ " );
 243  0
                 result.append( itemData.toEncodedString( ) );
 244  0
                 result.append( " ]" );
 245   
             }
 246  0
             return result.toString( );
 247   
         }
 248   
 
 249   
         /**
 250   
          * Abstract method for determining what type of data a subclass of this
 251   
          * class is managing.
 252   
          *
 253   
          * @return the type of data the class is managing
 254   
          */
 255   
         protected abstract String getDataType(  );
 256   
 
 257   
         /**
 258   
          * Abstract method for parsing one item of the array.
 259   
          *
 260   
          * @param data   the data to parse
 261   
          *
 262   
          * @return the parsed item
 263   
          *
 264   
          * @throws VRMooParseException   for any parsing errors that occur
 265   
          */
 266   
         protected abstract ItemDataUpdater parseItem( String data )
 267   
                 throws VRMooParseException;
 268   
 
 269   
         /**
 270   
          * Parses the actual array of data from an encoded string.
 271   
          *
 272   
          * @param data   the data to parse
 273   
          *
 274   
          * @throws VRMooParseException   for any parsing errors that occur
 275   
          */
 276  0
         private void parseArray( String data ) throws VRMooParseException
 277   
         {
 278  0
             data = data.trim( );
 279  0
             int itemEnd = data.indexOf( "]" );
 280  0
             while ( ( data.indexOf( "[" ) == 0 ) && ( itemEnd != -1 ) )
 281   
             {
 282  0
                 String item = data.substring( 1, itemEnd );
 283  0
                 int nextIndex = data.indexOf( "[", itemEnd );
 284  0
                 if ( nextIndex != -1 )
 285   
                 {
 286  0
                     data = data.substring( nextIndex ).trim( );
 287   
                 }
 288   
                 else
 289   
                 {
 290  0
                     data = "";
 291   
                 }
 292  0
                 itemEnd = data.indexOf( "]" );
 293   
 
 294  0
                 dataList.add( parseItem( item ) );
 295   
             }
 296   
         }
 297   
     }
 298   
 
 299   
     /**
 300   
      * Helper class for parsing the old version of a quad array and outputing
 301   
      * the new version.
 302   
      */
 303   
     private class QuadArrayDataUpdater extends ArrayDataUpdater
 304   
     {
 305   
         /**
 306   
          * Parse quad data from an encoded string.
 307   
          *
 308   
          * @param data   the data to parse
 309   
          *
 310   
          * @throws VRMooParseException   for any parsing errors that occur
 311   
          */
 312  0
         public QuadArrayDataUpdater( String data ) throws VRMooParseException
 313   
         {
 314  0
             super( data );
 315   
         }
 316   
 
 317   
         /**
 318   
          * Specifies what data type that this subclass of
 319   
          * <code>ArrayDataUpdater</code> is handling.
 320   
          *
 321   
          * @return the type of data the class is managing
 322   
          */
 323  0
         protected String getDataType(  )
 324   
         {
 325  0
             return "QuadArray";
 326   
         }
 327   
 
 328   
         /**
 329   
          * Parse one quad item of the array.
 330   
          *
 331   
          * @param data   the data to parse
 332   
          *
 333   
          * @return the parsed item
 334   
          *
 335   
          * @throws VRMooParseException   for any parsing errors that occur
 336   
          */
 337  0
         protected ItemDataUpdater parseItem( String data )
 338   
                 throws VRMooParseException
 339   
         {
 340  0
             return new QuadDataUpdater( data );
 341   
         }
 342   
     }
 343   
 
 344   
     /**
 345   
      * Helper class for parsing the old version of a triangle array and
 346   
      * outputing the new version.
 347   
      */
 348   
     private class TriangleArrayDataUpdater extends ArrayDataUpdater
 349   
     {
 350   
         /**
 351   
          * Parse triangle data from an encoded string.
 352   
          *
 353   
          * @param data   the data to parse
 354   
          *
 355   
          * @throws VRMooParseException   for any parsing errors that occur
 356   
          */
 357  0
         public TriangleArrayDataUpdater( String data )
 358   
                 throws VRMooParseException
 359   
         {
 360  0
             super( data );
 361   
         }
 362   
 
 363   
         /**
 364   
          * Specifies what data type that this subclass of
 365   
          * <code>ArrayDataUpdater</code> is handling.
 366   
          *
 367   
          * @return the type of data the class is managing
 368   
          */
 369  0
         protected String getDataType(  )
 370   
         {
 371  0
             return "TriangleArray";
 372   
         }
 373   
 
 374   
         /**
 375   
          * Parse one triangle item of the array.
 376   
          *
 377   
          * @param data   the data to parse
 378   
          *
 379   
          * @return the parsed item
 380   
          *
 381   
          * @throws VRMooParseException   for any parsing errors that occur
 382   
          */
 383  0
         protected ItemDataUpdater parseItem( String data )
 384   
                 throws VRMooParseException
 385   
         {
 386  0
             return new TriangleDataUpdater( data );
 387   
         }
 388   
     }
 389   
 
 390   
     /**
 391   
      * Helper class for parsing the old version of a single item and outputing
 392   
      * the new version.
 393   
      */
 394   
     private abstract class ItemDataUpdater
 395   
     {
 396   
         /**
 397   
          * The vertices representing the item.
 398   
          */
 399   
         private final VertexDataUpdater[ ] vertices =
 400   
                 new VertexDataUpdater[ getVertexCount( ) ];
 401   
 
 402   
         /**
 403   
          * The appearance name used for the item.
 404   
          */
 405   
         private String appearanceName;
 406   
 
 407   
         /**
 408   
          * Parse an item from an encoded string.
 409   
          *
 410   
          * @param data   the data to parse
 411   
          *
 412   
          * @throws VRMooParseException   for any parsing errors that occur
 413   
          */
 414  0
         public ItemDataUpdater( String data ) throws VRMooParseException
 415   
         {
 416  0
             int idStart = data.indexOf( "\"" );
 417  0
             int idEnd   = data.indexOf( "\"", idStart + 1 );
 418   
 
 419  0
             if ( ( idStart != -1 ) &&
 420   
                  ( idEnd   != -1 ) )
 421   
             {
 422  0
                 appearanceName = data.substring( idStart + 1, idEnd );
 423   
             }
 424   
             else
 425   
             {
 426  0
                 throw new VRMooParseException(
 427   
                         "Unable to parse appearance ID." );
 428   
             }
 429   
 
 430  0
             data = data.trim( );
 431  0
             int vertexCount = 0;
 432  0
             int vertexEnd = data.indexOf( ")" );
 433  0
             while ( ( data.indexOf( "(" ) == 0 ) && ( vertexEnd != -1 ) )
 434   
             {
 435  0
                 if ( vertexCount > getVertexCount( ) )
 436   
                 {
 437  0
                     throw new VRMooParseException( "Too many vertices." );
 438   
                 }
 439   
 
 440  0
                 String vertex = data.substring( 1, vertexEnd );
 441  0
                 int nextIndex = data.indexOf( "(", vertexEnd );
 442  0
                 if ( nextIndex != -1 )
 443   
                 {
 444  0
                     data = data.substring( nextIndex ).trim( );
 445   
                 }
 446   
                 else
 447   
                 {
 448  0
                     data = "";
 449   
                 }
 450  0
                 vertexEnd = data.indexOf( ")" );
 451   
 
 452  0
                 vertices[ vertexCount++ ] = new VertexDataUpdater( vertex );
 453   
             }
 454  0
             if ( vertexCount < getVertexCount( ) )
 455   
             {
 456  0
                 throw new VRMooParseException( "Too few vertices." );
 457   
             }
 458   
         }
 459   
 
 460   
         /**
 461   
          * Get a string with the item data encoded in it.
 462   
          *
 463   
          * @return the encoded data of the item
 464   
          */
 465  0
         public String toEncodedString( )
 466   
         {
 467  0
             StringBuffer result = new StringBuffer( );
 468   
 
 469  0
             for ( int i = 0; i < getVertexCount( ); i++ )
 470   
             {
 471  0
                 VertexDataUpdater vertexData = vertices[ i ];
 472  0
                 if ( i != 0 )
 473   
                 {
 474  0
                     result.append( ", " );
 475   
                 }
 476  0
                 result.append( vertexData.toEncodedString( ) );
 477   
             }
 478  0
             result.append( ", \"" );
 479  0
             result.append( appearanceName );
 480  0
             result.append( "\"" );
 481  0
             return result.toString( );
 482   
         }
 483   
 
 484   
         /**
 485   
          * Abstract method for determining the number of vertices that
 486   
          * subclasses of this class are using.
 487   
          *
 488   
          * @return the number of vertices the class is usiing
 489   
          */
 490   
         protected abstract int getVertexCount( );
 491   
     }
 492   
 
 493   
     /**
 494   
      * Helper class for parsing the old version of a single quad and outputing
 495   
      * the new version.
 496   
      */
 497   
     private class QuadDataUpdater extends ItemDataUpdater
 498   
     {
 499   
         /**
 500   
          * Parse a quad from an encoded string.
 501   
          *
 502   
          * @param data   the data to parse
 503   
          *
 504   
          * @throws VRMooParseException   for any parsing errors that occur
 505   
          */
 506  0
         public QuadDataUpdater( String data ) throws VRMooParseException
 507   
         {
 508  0
             super( data );
 509   
         }
 510   
 
 511   
         /**
 512   
          * Specifies the number of vertices that this subclass of
 513   
          * <code>ItemDataUpdater</code> is using.
 514   
          *
 515   
          * @return the number of vertices the class is using
 516   
          */
 517  0
         protected int getVertexCount( )
 518   
         {
 519  0
             return 4;
 520   
         }
 521   
     }
 522   
 
 523   
     /**
 524   
      * Helper class for parsing the old version of a single triangle and
 525   
      * outputing the new version.
 526   
      */
 527   
     private class TriangleDataUpdater extends ItemDataUpdater
 528   
     {
 529   
         /**
 530   
          * Parse a triangle from an encoded string.
 531   
          *
 532   
          * @param data   the data to parse
 533   
          *
 534   
          * @throws VRMooParseException   for any parsing errors that occur
 535   
          */
 536  0
         public TriangleDataUpdater( String data ) throws VRMooParseException
 537   
         {
 538  0
             super( data );
 539   
         }
 540   
 
 541   
         /**
 542   
          * Specifies the number of vertices that this subclass of
 543   
          * <code>ItemDataUpdater</code> is using.
 544   
          *
 545   
          * @return the number of vertices the class is using
 546   
          */
 547  0
         protected int getVertexCount( )
 548   
         {
 549  0
             return 3;
 550   
         }
 551   
     }
 552   
 
 553   
     /**
 554   
      * Helper class for parsing the old version of a vertex and outputing the
 555   
      * new version. Adds a hard coded normal of (0.0, 1.0, 0.0) to every Vertex
 556   
      * during the update.
 557   
      */
 558   
     private class VertexDataUpdater
 559   
     {
 560   
         /**
 561   
          * Contains the X coordinate of the point data.
 562   
          */
 563   
         private float pointX;
 564   
 
 565   
         /**
 566   
          * Contains the Y coordinate of the point data.
 567   
          */
 568   
         private float pointY;
 569   
 
 570   
         /**
 571   
          * Contains the Z coordinate of the point data.
 572   
          */
 573   
         private float pointZ;
 574   
 
 575   
         /**
 576   
          * Contans the X coordinate of the texture coordinates for this point.
 577   
          */
 578   
         private float texCoordX;
 579   
 
 580   
         /**
 581   
          * Contans the Y coordinate of the texture coordinates for this point.
 582   
          */
 583   
         private float texCoordY;
 584   
 
 585   
         /**
 586   
          * Parse a vertex from an encoded string.
 587   
          *
 588   
          * @param data   the data to parse
 589   
          *
 590   
          * @throws VRMooParseException   for any parsing errors that occur
 591   
          */
 592  0
         public VertexDataUpdater( String data ) throws VRMooParseException
 593   
         {
 594  0
             float f[ ] = ParserUtility.parseFloatArray( data, 5 );
 595  0
             pointX    = f[ 0 ];
 596  0
             pointY    = f[ 1 ];
 597  0
             pointZ    = f[ 2 ];
 598  0
             texCoordX = f[ 3 ];
 599  0
             texCoordY = f[ 4 ];
 600   
         }
 601   
 
 602   
         /**
 603   
          * Get a string with the vertex data encoded in it.
 604   
          *
 605   
          * @return the encoded data of the item
 606   
          */
 607  0
         public String toEncodedString( )
 608   
         {
 609  0
             return "( " + pointX + ", " + pointY + ", " + pointZ + ", " +
 610   
                     texCoordX + ", " + texCoordY + ", " +
 611   
                     "0.0, 1.0, 0.0 )";
 612   
         }
 613   
     }
 614   
 }
 615