2019-01-01 01:10:18 +00:00
|
|
|
/*
|
|
|
|
* This file is part of ComputerCraft - http://www.computercraft.info
|
|
|
|
* Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
|
|
|
|
* Send enquiries to dratcliffe@gmail.com
|
|
|
|
*/
|
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
package dan200.computercraft.shared.util;
|
|
|
|
|
2017-05-13 21:26:35 +00:00
|
|
|
import dan200.computercraft.ComputerCraft;
|
2017-05-08 18:55:51 +00:00
|
|
|
|
2017-05-01 13:32:39 +00:00
|
|
|
import java.io.*;
|
2019-01-11 11:33:05 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2017-05-01 13:32:39 +00:00
|
|
|
|
2019-03-29 21:21:39 +00:00
|
|
|
public final class IDAssigner
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
2017-05-01 14:48:44 +00:00
|
|
|
private IDAssigner()
|
|
|
|
{
|
|
|
|
}
|
2018-12-23 17:46:58 +00:00
|
|
|
|
2019-01-12 18:20:16 +00:00
|
|
|
public static int getNextIDFromDirectory( String path )
|
|
|
|
{
|
|
|
|
return getNextIDFromDirectory( new File( ComputerCraft.getWorldDir(), path ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public static int getNextIDFromFile( String path )
|
|
|
|
{
|
|
|
|
return getNextIDFromFile( new File( ComputerCraft.getWorldDir(), path ) );
|
|
|
|
}
|
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
public static int getNextIDFromDirectory( File dir )
|
|
|
|
{
|
|
|
|
return getNextID( dir, true );
|
|
|
|
}
|
2018-12-23 17:46:58 +00:00
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
public static int getNextIDFromFile( File file )
|
|
|
|
{
|
|
|
|
return getNextID( file, false );
|
|
|
|
}
|
2018-12-23 17:46:58 +00:00
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
private static int getNextID( File location, boolean directory )
|
|
|
|
{
|
|
|
|
// Determine where to locate ID file
|
2019-03-29 21:21:39 +00:00
|
|
|
File lastIdFile;
|
2017-05-01 14:48:44 +00:00
|
|
|
if( directory )
|
|
|
|
{
|
|
|
|
location.mkdirs();
|
2019-03-29 21:21:39 +00:00
|
|
|
lastIdFile = new File( location, "lastid.txt" );
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
location.getParentFile().mkdirs();
|
2019-03-29 21:21:39 +00:00
|
|
|
lastIdFile = location;
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
2018-12-23 17:46:58 +00:00
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
// Try to determine the id
|
|
|
|
int id = 0;
|
2019-03-29 21:21:39 +00:00
|
|
|
if( !lastIdFile.exists() )
|
2017-05-01 14:48:44 +00:00
|
|
|
{
|
|
|
|
// If an ID file doesn't exist, determine it from the file structure
|
|
|
|
if( directory && location.exists() && location.isDirectory() )
|
|
|
|
{
|
|
|
|
String[] contents = location.list();
|
2017-05-06 23:42:00 +00:00
|
|
|
for( String content : contents )
|
2017-05-01 14:48:44 +00:00
|
|
|
{
|
2017-05-06 23:42:00 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
int number = Integer.parseInt( content );
|
2017-05-01 14:48:44 +00:00
|
|
|
id = Math.max( number + 1, id );
|
2017-05-06 23:42:00 +00:00
|
|
|
}
|
|
|
|
catch( NumberFormatException e )
|
|
|
|
{
|
2017-05-13 21:26:35 +00:00
|
|
|
ComputerCraft.log.error( "Unexpected file '" + content + "' in '" + location.getAbsolutePath() + "'", e );
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If an ID file does exist, parse the file to get the ID string
|
2017-05-06 23:42:00 +00:00
|
|
|
String idString;
|
2017-05-01 14:48:44 +00:00
|
|
|
try
|
|
|
|
{
|
2019-03-29 21:21:39 +00:00
|
|
|
FileInputStream in = new FileInputStream( lastIdFile );
|
|
|
|
InputStreamReader isr = new InputStreamReader( in, StandardCharsets.UTF_8 );
|
2017-06-12 20:08:35 +00:00
|
|
|
try( BufferedReader br = new BufferedReader( isr ) )
|
2017-05-01 13:32:39 +00:00
|
|
|
{
|
|
|
|
idString = br.readLine();
|
|
|
|
}
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
2019-03-29 21:21:39 +00:00
|
|
|
ComputerCraft.log.error( "Cannot open ID file '" + lastIdFile + "'", e );
|
2017-05-01 14:48:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
id = Integer.parseInt( idString ) + 1;
|
|
|
|
}
|
|
|
|
catch( NumberFormatException e )
|
|
|
|
{
|
2019-03-29 21:21:39 +00:00
|
|
|
ComputerCraft.log.error( "Cannot parse ID file '" + lastIdFile + "', perhaps it is corrupt?", e );
|
2017-05-01 14:48:44 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2018-12-23 17:46:58 +00:00
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
// Write the lastID file out with the new value
|
|
|
|
try
|
|
|
|
{
|
2019-03-29 21:21:39 +00:00
|
|
|
try( BufferedWriter out = new BufferedWriter( new FileWriter( lastIdFile, false ) ) )
|
|
|
|
{
|
|
|
|
out.write( Integer.toString( id ) );
|
|
|
|
out.newLine();
|
|
|
|
}
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
|
|
|
catch( IOException e )
|
|
|
|
{
|
2019-03-29 21:21:39 +00:00
|
|
|
ComputerCraft.log.error( "An error occurred while trying to create the computer folder. Please check you have relevant permissions.", e );
|
2017-05-01 14:48:44 +00:00
|
|
|
}
|
2018-12-23 17:46:58 +00:00
|
|
|
|
2017-05-01 14:48:44 +00:00
|
|
|
return id;
|
|
|
|
}
|
2017-05-01 13:32:39 +00:00
|
|
|
}
|