Merge pull request #427 from yueh/development
Fix potentially unclosed streams
This commit is contained in:
@@ -18,9 +18,11 @@
|
||||
|
||||
package appeng.core;
|
||||
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@@ -31,6 +33,8 @@ import java.util.Map.Entry;
|
||||
import java.util.UUID;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.nbt.CompressedStreamTools;
|
||||
@@ -40,6 +44,7 @@ import net.minecraftforge.common.DimensionManager;
|
||||
import net.minecraftforge.common.config.ConfigCategory;
|
||||
import net.minecraftforge.common.config.Configuration;
|
||||
import net.minecraftforge.common.config.Property;
|
||||
|
||||
import appeng.api.util.WorldCoord;
|
||||
import appeng.core.sync.network.NetworkHandler;
|
||||
import appeng.core.sync.packets.PacketNewStorageDimension;
|
||||
@@ -49,26 +54,26 @@ import appeng.me.GridStorage;
|
||||
import appeng.me.GridStorageSearch;
|
||||
import appeng.services.CompassService;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
public class WorldSettings extends Configuration
|
||||
{
|
||||
|
||||
private static final String SPAWNDATA_FOLDER = "spawndata";
|
||||
private static final String COMPASS_Folder = "compass";
|
||||
|
||||
private static WorldSettings instance;
|
||||
|
||||
long lastGridStorage = 0;
|
||||
int lastPlayer = 0;
|
||||
|
||||
private final File aeFolder;
|
||||
private final CompassService compass;
|
||||
|
||||
final File AEFolder;
|
||||
private long lastGridStorage = 0;
|
||||
private int lastPlayer = 0;
|
||||
|
||||
public WorldSettings( File aeFolder )
|
||||
{
|
||||
super( new File( aeFolder.getPath() + File.separatorChar + "settings.cfg" ) );
|
||||
AEFolder = aeFolder;
|
||||
|
||||
compass = new CompassService( AEFolder );
|
||||
this.aeFolder = aeFolder;
|
||||
this.compass = new CompassService( aeFolder );
|
||||
|
||||
for ( int dimID : get( "DimensionManager", "StorageCells", new int[0] ).getIntList() )
|
||||
{
|
||||
@@ -93,40 +98,44 @@ public class WorldSettings extends Configuration
|
||||
if ( !Thread.holdsLock( WorldSettings.class ) )
|
||||
throw new RuntimeException( "Invalid Request" );
|
||||
|
||||
File f = new File( AEFolder, "spawndata" + File.separatorChar + dim + "_" + (chunkX >> 4) + "_" + (chunkZ >> 4) + ".dat" );
|
||||
|
||||
if ( f.isFile() && f.exists() )
|
||||
{
|
||||
// open
|
||||
FileInputStream fis;
|
||||
try
|
||||
{
|
||||
fis = new FileInputStream( f );
|
||||
|
||||
NBTTagCompound data = null;
|
||||
File file = new File( aeFolder, SPAWNDATA_FOLDER + File.separatorChar + dim + "_" + ( chunkX >> 4 ) + "_" + ( chunkZ >> 4 ) + ".dat" );
|
||||
|
||||
if ( file.isFile() )
|
||||
{
|
||||
FileInputStream fileInputStream = null;
|
||||
|
||||
try
|
||||
{
|
||||
data = CompressedStreamTools.readCompressed( fis );
|
||||
fileInputStream = new FileInputStream( file );
|
||||
data = CompressedStreamTools.readCompressed( fileInputStream );
|
||||
}
|
||||
catch ( Throwable e )
|
||||
{
|
||||
data = new NBTTagCompound();
|
||||
AELog.error( e );
|
||||
}
|
||||
|
||||
fis.close();
|
||||
|
||||
return data;
|
||||
finally
|
||||
{
|
||||
if ( fileInputStream != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
fileInputStream.close();
|
||||
}
|
||||
catch (Throwable e)
|
||||
catch ( IOException e )
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
data = new NBTTagCompound();
|
||||
}
|
||||
|
||||
return new NBTTagCompound();
|
||||
return data;
|
||||
}
|
||||
|
||||
void writeSpawnData( int dim, int chunkX, int chunkZ, NBTTagCompound data )
|
||||
@@ -134,29 +143,33 @@ public class WorldSettings extends Configuration
|
||||
if ( !Thread.holdsLock( WorldSettings.class ) )
|
||||
throw new RuntimeException( "Invalid Request" );
|
||||
|
||||
File f = new File( AEFolder, "spawndata" + File.separatorChar + dim + "_" + (chunkX >> 4) + "_" + (chunkZ >> 4) + ".dat" );
|
||||
File file = new File( aeFolder, SPAWNDATA_FOLDER + File.separatorChar + dim + "_" + ( chunkX >> 4 ) + "_" + ( chunkZ >> 4 ) + ".dat" );
|
||||
FileOutputStream fileOutputStream = null;
|
||||
|
||||
try
|
||||
{
|
||||
// save
|
||||
FileOutputStream fos = new FileOutputStream( f );
|
||||
|
||||
try
|
||||
{
|
||||
CompressedStreamTools.writeCompressed( data, fos );
|
||||
fileOutputStream = new FileOutputStream( file );
|
||||
CompressedStreamTools.writeCompressed( data, fileOutputStream );
|
||||
}
|
||||
catch ( Throwable e )
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
|
||||
fos.close();
|
||||
finally
|
||||
{
|
||||
if ( fileOutputStream != null )
|
||||
{
|
||||
try
|
||||
{
|
||||
fileOutputStream.close();
|
||||
}
|
||||
catch (Throwable e)
|
||||
catch ( IOException e )
|
||||
{
|
||||
AELog.error( e );
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<NBTTagCompound> getNearByMeteorites( int dim, int chunkX, int chunkZ )
|
||||
{
|
||||
@@ -271,22 +284,19 @@ public class WorldSettings extends Configuration
|
||||
|
||||
File aeBaseFolder = new File( world.getPath() + File.separatorChar + "AE2" );
|
||||
|
||||
if ( !aeBaseFolder.exists() || !aeBaseFolder.isDirectory() )
|
||||
if ( !aeBaseFolder.mkdir() || !aeBaseFolder.exists() )
|
||||
if ( !aeBaseFolder.isDirectory() && !aeBaseFolder.mkdir() )
|
||||
{
|
||||
throw new RuntimeException( "Failed to create " + aeBaseFolder.getAbsolutePath() );
|
||||
}
|
||||
|
||||
File compass = new File( aeBaseFolder, "compass" );
|
||||
if ( !compass.exists() || !compass.isDirectory() )
|
||||
if ( !compass.mkdir() || !compass.exists() )
|
||||
File compass = new File( aeBaseFolder, COMPASS_Folder );
|
||||
if ( !compass.isDirectory() && !compass.mkdir() )
|
||||
{
|
||||
throw new RuntimeException( "Failed to create " + compass.getAbsolutePath() );
|
||||
}
|
||||
|
||||
File spawnData = new File( aeBaseFolder, "spawndata" );
|
||||
if ( !spawnData.exists() || !spawnData.isDirectory() )
|
||||
if ( !spawnData.mkdir() || !spawnData.exists() )
|
||||
File spawnData = new File( aeBaseFolder, SPAWNDATA_FOLDER );
|
||||
if ( !spawnData.isDirectory() && !spawnData.mkdir() )
|
||||
{
|
||||
throw new RuntimeException( "Failed to create " + spawnData.getAbsolutePath() );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user