2019-02-26 08:44:17 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 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()
|
|
|
|
{
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
return "ComputerCraft ${version} (Test environment)";
|
2019-02-26 08:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IMount createResourceMount( String domain, String subPath )
|
|
|
|
{
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
return createMount( ComputerCraft.class, "data/" + domain + "/" + subPath, "main" );
|
2019-03-02 02:09:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InputStream createResourceFile( String domain, String subPath )
|
|
|
|
{
|
Update CC: Tweaked to 1.13
Look, I originally had this split into several commits, but lots of
other cleanups got mixed in. I then backported some of the cleanups to
1.12, did other tidy ups there, and eventually the web of merges was
unreadable.
Yes, this is a horrible mess, but it's still nicer than it was. Anyway,
changes:
- Flatten everything. For instance, there are now three instances of
BlockComputer, two BlockTurtle, ItemPocketComputer. There's also no
more BlockPeripheral (thank heavens) - there's separate block classes
for each peripheral type.
- Remove pretty much all legacy code. As we're breaking world
compatibility anyway, we can remove all the code to load worlds from
1.4 days.
- The command system is largely rewriten to take advantage of 1.13's
new system. It's very fancy!
- WidgetTerminal now uses Minecraft's "GUI listener" system.
- BREAKING CHANGE: All the codes in keys.lua are different, due to the
move to LWJGL 3. Hopefully this won't have too much of an impact.
I don't want to map to the old key codes on the Java side, as there
always ends up being small but slight inconsistencies. IMO it's
better to make a clean break - people should be using keys rather
than hard coding the constants anyway.
- commands.list now allows fetching sub-commands. The ROM has already
been updated to allow fancy usage such as commands.time.set("noon").
- Turtles, modems and cables can be waterlogged.
2019-04-02 12:27:27 +00:00
|
|
|
return ComputerCraft.class.getClassLoader().getResourceAsStream( "data/" + domain + "/" + subPath );
|
2019-03-02 02:09:14 +00:00
|
|
|
}
|
2019-02-26 08:44:17 +00:00
|
|
|
|
2019-03-02 02:09:14 +00:00
|
|
|
|
|
|
|
public static IMount createMount( Class<?> klass, String path, String fallback )
|
|
|
|
{
|
2019-06-07 23:28:03 +00:00
|
|
|
File file = getContainingFile( klass );
|
2019-02-26 08:44:17 +00:00
|
|
|
|
|
|
|
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();
|
2019-03-02 02:09:14 +00:00
|
|
|
wholeFile = new File( baseFile, "resources/" + fallback + "/" + path );
|
2019-02-26 08:44:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( !wholeFile.exists() ) throw new IllegalStateException( "Cannot find ROM mount at " + file );
|
|
|
|
|
|
|
|
return new FileMount( wholeFile, 0 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-07 23:28:03 +00:00
|
|
|
private static File getContainingFile( Class<?> klass )
|
2019-02-26 08:44:17 +00:00
|
|
|
{
|
2019-03-02 02:09:14 +00:00
|
|
|
String path = klass.getProtectionDomain().getCodeSource().getLocation().getPath();
|
2019-02-26 08:44:17 +00:00
|
|
|
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() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|