-
Notifications
You must be signed in to change notification settings - Fork 193
Provide an API for registering custom APIs #491
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SquidDev
wants to merge
1
commit into
dan200:master
Choose a base branch
from
SquidDev-CC:feature/api-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/java/dan200/computercraft/api/filesystem/IFileSystem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package dan200.computercraft.api.filesystem; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Provides a mount of the entire computer's file system. | ||
| * | ||
| * This exists for use by various APIs - one should not attempt to mount it. | ||
| */ | ||
| public interface IFileSystem extends IWritableMount | ||
| { | ||
| /** | ||
| * Combine two paths together, reducing them into a normalised form. | ||
| * | ||
| * @param path The main path. | ||
| * @param child The path to append. | ||
| * @return The combined, normalised path. | ||
| */ | ||
| String combine( String path, String child ); | ||
|
|
||
| /** | ||
| * Copy files from one location to another. | ||
| * | ||
| * @param from The location to copy from. | ||
| * @param to The location to copy to. This should not exist. | ||
| * @throws IOException If the copy failed. | ||
| */ | ||
| void copy( String from, String to ) throws IOException; | ||
|
|
||
| /** | ||
| * Move files from one location to another. | ||
| * | ||
| * @param from The location to move from. | ||
| * @param to The location to move to. This should not exist. | ||
| * @throws IOException If the move failed. | ||
| */ | ||
| void move( String from, String to ) throws IOException; | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/main/java/dan200/computercraft/api/lua/IComputerSystem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package dan200.computercraft.api.lua; | ||
|
|
||
| import dan200.computercraft.api.filesystem.IFileSystem; | ||
| import dan200.computercraft.api.peripheral.IComputerAccess; | ||
|
|
||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * An interface passed to {@link ILuaAPIFactory} in order to provide additional information | ||
| * about a computer. | ||
| */ | ||
| public interface IComputerSystem extends IComputerAccess | ||
| { | ||
| /** | ||
| * Get the file system for this computer. | ||
| * | ||
| * @return The computer's file system, or {@code null} if it is not initialised. | ||
| */ | ||
| @Nullable | ||
| IFileSystem getFileSystem(); | ||
|
|
||
| /** | ||
| * Get the label for this computer | ||
| * | ||
| * @return This computer's label, or {@code null} if it is not set. | ||
| */ | ||
| @Nullable | ||
| String getLabel(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * This file is part of ComputerCraft - http://www.computercraft.info | ||
| * Copyright Daniel Ratcliffe, 2011-2016. Do not distribute without permission. | ||
| * Send enquiries to dratcliffe@gmail.com | ||
| */ | ||
|
|
||
| package dan200.computercraft.api.lua; | ||
|
|
||
| import dan200.computercraft.api.ComputerCraftAPI; | ||
|
|
||
| /** | ||
| * Represents a {@link ILuaObject} which is stored as a global variable on computer startup. | ||
| * | ||
| * Before implementing this interface, consider alternative methods of providing methods. It is generally preferred | ||
| * to use peripherals to provide functionality to users. | ||
| * | ||
| * @see ILuaAPIFactory | ||
| * @see ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory) | ||
| */ | ||
| public interface ILuaAPI extends ILuaObject | ||
| { | ||
| /** | ||
| * Get the globals this API will be assigned to. This will override any other global, so you should | ||
| * | ||
| * @return A list of globals this API will be assigned to. | ||
| */ | ||
| String[] getNames(); | ||
|
|
||
| /** | ||
| * Called when the computer is turned on. | ||
| * | ||
| * One should only interact with the file system. | ||
| */ | ||
| default void startup() { } | ||
|
|
||
| /** | ||
| * Called every time the computer is ticked. This can be used to process various. | ||
| */ | ||
| default void update() { } | ||
|
|
||
| /** | ||
| * Called when the computer is turned off or unloaded. | ||
| * | ||
| * This should reset the state of the object, disposing any remaining file handles, or other resources. | ||
| */ | ||
| default void shutdown() { } | ||
| } |
24 changes: 24 additions & 0 deletions
24
src/main/java/dan200/computercraft/api/lua/ILuaAPIFactory.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package dan200.computercraft.api.lua; | ||
|
|
||
| import dan200.computercraft.api.ComputerCraftAPI; | ||
|
|
||
| import javax.annotation.Nonnull; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Construct an {@link ILuaAPI} for a specific computer. | ||
| * | ||
| * @see ILuaAPI | ||
| * @see ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory) | ||
| */ | ||
| public interface ILuaAPIFactory | ||
| { | ||
| /** | ||
| * Create a new API instance for a given computer. | ||
| * | ||
| * @param computer The computer this API is for. | ||
| * @return The created API, or {@code null} if one should not be injected. | ||
| */ | ||
| @Nullable | ||
| ILuaAPI create( @Nonnull IComputerSystem computer ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should return a
booleanspecifying whether the API was actually registered?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm only really doing it this way because that's what all the other methods do. The only way it could thrown an exception is if the reflection fails, which shouldn't ever happen anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I was referring to
if( provider != null && !apiFactories.contains( provider ) )failing.