1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-07-13 15:32:53 +00:00
SquidDev a0e7c4a74c Add a little bit of source code checking to Gradle
- Adds a CheckStyle configuration which is pretty similar to CC's
   existing one.
 - Add the Gradle license plugin.
 - Ensure the existing source code is compatible with these additional
   checks.

See #239
2019-06-08 00:28:03 +01:00

160 lines
3.9 KiB
Java

/*
* 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
*/
package dan200.computercraft.core.computer;
import dan200.computercraft.ComputerCraft;
import dan200.computercraft.api.filesystem.IMount;
import dan200.computercraft.api.filesystem.IWritableMount;
import dan200.computercraft.core.filesystem.FileMount;
import dan200.computercraft.core.filesystem.JarMount;
import dan200.computercraft.core.filesystem.MemoryMount;
import net.minecraftforge.fml.common.Loader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
/**
* A very basic environment
*/
public class BasicEnvironment implements IComputerEnvironment
{
private final IWritableMount mount;
public BasicEnvironment()
{
this( new MemoryMount() );
}
public BasicEnvironment( IWritableMount mount )
{
this.mount = mount;
}
@Override
public int assignNewID()
{
return 0;
}
@Override
public IWritableMount createSaveDirMount( String path, long space )
{
return mount;
}
@Override
public int getDay()
{
return 0;
}
@Override
public double getTimeOfDay()
{
return 0;
}
@Override
public boolean isColour()
{
return true;
}
@Override
public long getComputerSpaceLimit()
{
return ComputerCraft.computerSpaceLimit;
}
@Override
public String getHostString()
{
return "ComputerCraft ${version} (Minecraft " + Loader.MC_VERSION + ")";
}
@Override
public IMount createResourceMount( String domain, String subPath )
{
return createMount( ComputerCraft.class, "assets/" + domain + "/" + subPath, "main" );
}
@Override
public InputStream createResourceFile( String domain, String subPath )
{
return ComputerCraft.class.getClassLoader().getResourceAsStream( "assets/" + domain + "/" + subPath );
}
public static IMount createMount( Class<?> klass, String path, String fallback )
{
File file = getContainingFile( klass );
if( file.isFile() )
{
try
{
return new JarMount( file, path );
}
catch( IOException e )
{
throw new UncheckedIOException( e );
}
}
else
{
File wholeFile = new File( file, path );
// If we don't exist, walk up the tree looking for resource folders
File baseFile = file;
while( baseFile != null && !wholeFile.exists() )
{
baseFile = baseFile.getParentFile();
wholeFile = new File( baseFile, "resources/" + fallback + "/" + path );
}
if( !wholeFile.exists() ) throw new IllegalStateException( "Cannot find ROM mount at " + file );
return new FileMount( wholeFile, 0 );
}
}
private static File getContainingFile( Class<?> klass )
{
String path = klass.getProtectionDomain().getCodeSource().getLocation().getPath();
int bangIndex = path.indexOf( "!" );
// Plain old file, so step up from dan200.computercraft.
if( bangIndex < 0 ) return new File( path );
path = path.substring( 0, bangIndex );
URL url;
try
{
url = new URL( path );
}
catch( MalformedURLException e )
{
throw new IllegalStateException( e );
}
try
{
return new File( url.toURI() );
}
catch( URISyntaxException e )
{
return new File( url.getPath() );
}
}
}