mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-02-04 13:19:13 +00:00
Adds the ability to load custom bios.lua files from resource packs
Computer now delegates to IComputerEnvironment which, by default, looks in the following locations: - Resouce pack files - The "debug" folder - The original ComputerCraft jar
This commit is contained in:
parent
94d701b1f7
commit
c0294e1534
@ -72,10 +72,10 @@ import net.minecraftforge.fml.common.network.FMLEventChannel;
|
|||||||
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
import net.minecraftforge.fml.common.network.NetworkRegistry;
|
||||||
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
|
import net.minecraftforge.fml.common.network.internal.FMLProxyPacket;
|
||||||
import net.minecraftforge.fml.relauncher.Side;
|
import net.minecraftforge.fml.relauncher.Side;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.*;
|
||||||
import java.io.IOException;
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
@ -83,6 +83,8 @@ import java.util.ArrayList;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
|
||||||
///////////////
|
///////////////
|
||||||
// UNIVERSAL //
|
// UNIVERSAL //
|
||||||
@ -875,6 +877,88 @@ public class ComputerCraft
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static InputStream getResourceFile( Class<?> modClass, String domain, String subPath )
|
||||||
|
{
|
||||||
|
// Start searching in possible locations
|
||||||
|
subPath = "assets/" + domain + "/" + subPath;
|
||||||
|
|
||||||
|
// Look in resource packs
|
||||||
|
File resourcePackDir = getResourcePackDir();
|
||||||
|
if( resourcePackDir.exists() && resourcePackDir.isDirectory() )
|
||||||
|
{
|
||||||
|
String[] resourcePacks = resourcePackDir.list();
|
||||||
|
for( String resourcePackPath : resourcePacks )
|
||||||
|
{
|
||||||
|
File resourcePack = new File( resourcePackDir, resourcePackPath );
|
||||||
|
if( resourcePack.isDirectory() )
|
||||||
|
{
|
||||||
|
// Mount a resource pack from a folder
|
||||||
|
File subResource = new File( resourcePack, subPath );
|
||||||
|
if( subResource.exists() && subResource.isFile() )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new FileInputStream( subResource );
|
||||||
|
}
|
||||||
|
catch( FileNotFoundException ignored )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ZipFile zipFile = null;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
final ZipFile zip = zipFile = new ZipFile( resourcePack );
|
||||||
|
ZipEntry entry = zipFile.getEntry( subPath );
|
||||||
|
if( entry != null )
|
||||||
|
{
|
||||||
|
// Return a custom InputStream which will close the original zip when finished.
|
||||||
|
return new FilterInputStream( zipFile.getInputStream( entry ) )
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException
|
||||||
|
{
|
||||||
|
super.close();
|
||||||
|
zip.close();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
IOUtils.closeQuietly( zipFile );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch( IOException e )
|
||||||
|
{
|
||||||
|
if( zipFile != null ) IOUtils.closeQuietly( zipFile );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look in debug dir
|
||||||
|
File codeDir = getDebugCodeDir( modClass );
|
||||||
|
if( codeDir != null )
|
||||||
|
{
|
||||||
|
File subResource = new File( codeDir, subPath );
|
||||||
|
if( subResource.exists() && subResource.isFile() )
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new FileInputStream( subResource );
|
||||||
|
}
|
||||||
|
catch( FileNotFoundException ignored )
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Look in class loader
|
||||||
|
return modClass.getClassLoader().getResourceAsStream( subPath );
|
||||||
|
}
|
||||||
|
|
||||||
private static File getContainingJar( Class<?> modClass )
|
private static File getContainingJar( Class<?> modClass )
|
||||||
{
|
{
|
||||||
String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
|
String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
|
||||||
|
@ -635,7 +635,7 @@ public class Computer
|
|||||||
InputStream biosStream;
|
InputStream biosStream;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
biosStream = Computer.class.getResourceAsStream( "/assets/computercraft/lua/bios.lua" );
|
biosStream = m_environment.createResourceFile( "computercraft", "lua/bios.lua" );
|
||||||
}
|
}
|
||||||
catch( Exception e )
|
catch( Exception e )
|
||||||
{
|
{
|
||||||
|
@ -8,6 +8,8 @@ package dan200.computercraft.core.computer;
|
|||||||
import dan200.computercraft.api.filesystem.IMount;
|
import dan200.computercraft.api.filesystem.IMount;
|
||||||
import dan200.computercraft.api.filesystem.IWritableMount;
|
import dan200.computercraft.api.filesystem.IWritableMount;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
public interface IComputerEnvironment
|
public interface IComputerEnvironment
|
||||||
{
|
{
|
||||||
int getDay();
|
int getDay();
|
||||||
@ -19,4 +21,5 @@ public interface IComputerEnvironment
|
|||||||
int assignNewID();
|
int assignNewID();
|
||||||
IWritableMount createSaveDirMount( String subPath, long capacity );
|
IWritableMount createSaveDirMount( String subPath, long capacity );
|
||||||
IMount createResourceMount( String domain, String subPath );
|
IMount createResourceMount( String domain, String subPath );
|
||||||
|
InputStream createResourceFile( String domain, String subPath );
|
||||||
}
|
}
|
||||||
|
@ -26,6 +26,8 @@ import net.minecraft.util.math.BlockPos;
|
|||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.fml.common.Loader;
|
import net.minecraftforge.fml.common.Loader;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class ServerComputer extends ServerTerminal
|
public class ServerComputer extends ServerTerminal
|
||||||
implements IComputer, IComputerEnvironment, INetworkedThing
|
implements IComputer, IComputerEnvironment, INetworkedThing
|
||||||
{
|
{
|
||||||
@ -309,6 +311,12 @@ public class ServerComputer extends ServerTerminal
|
|||||||
return ComputerCraftAPI.createResourceMount( ComputerCraft.class, domain, subPath );
|
return ComputerCraftAPI.createResourceMount( ComputerCraft.class, domain, subPath );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream createResourceFile( String domain, String subPath )
|
||||||
|
{
|
||||||
|
return ComputerCraft.getResourceFile( ComputerCraft.class, domain, subPath );
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public long getComputerSpaceLimit()
|
public long getComputerSpaceLimit()
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user