mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-02-03 12:49:11 +00:00
Clean up ApiWrapper a little
We now wrap every Api with an ApiWrapper, rather than making it pretend to be an ILuaApi.
This commit is contained in:
parent
55510c42db
commit
43744b0e85
@ -6,40 +6,34 @@ package dan200.computercraft.core.computer;
|
|||||||
|
|
||||||
import dan200.computercraft.api.lua.ILuaAPI;
|
import dan200.computercraft.api.lua.ILuaAPI;
|
||||||
|
|
||||||
/**
|
import javax.annotation.Nullable;
|
||||||
* A wrapper for {@link ILuaAPI}s which cleans up after a {@link ComputerSystem} when the computer is shutdown.
|
|
||||||
*/
|
|
||||||
final class ApiWrapper implements ILuaAPI {
|
|
||||||
private final ILuaAPI delegate;
|
|
||||||
private final ComputerSystem system;
|
|
||||||
|
|
||||||
ApiWrapper(ILuaAPI delegate, ComputerSystem system) {
|
/**
|
||||||
this.delegate = delegate;
|
* A wrapper for {@link ILuaAPI}s which optionally manages the lifecycle of a {@link ComputerSystem}.
|
||||||
|
*/
|
||||||
|
final class ApiWrapper {
|
||||||
|
private final ILuaAPI api;
|
||||||
|
private final @Nullable ComputerSystem system;
|
||||||
|
|
||||||
|
ApiWrapper(ILuaAPI api, @Nullable ComputerSystem system) {
|
||||||
|
this.api = api;
|
||||||
this.system = system;
|
this.system = system;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String[] getNames() {
|
|
||||||
return delegate.getNames();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void startup() {
|
public void startup() {
|
||||||
delegate.startup();
|
api.startup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update() {
|
public void update() {
|
||||||
delegate.update();
|
api.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
delegate.shutdown();
|
api.shutdown();
|
||||||
system.unmountAll();
|
if (system != null) system.unmountAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ILuaAPI getDelegate() {
|
public ILuaAPI api() {
|
||||||
return delegate;
|
return api;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,7 +61,7 @@ final class ComputerExecutor {
|
|||||||
private final Computer computer;
|
private final Computer computer;
|
||||||
private final ComputerEnvironment computerEnvironment;
|
private final ComputerEnvironment computerEnvironment;
|
||||||
private final MetricsObserver metrics;
|
private final MetricsObserver metrics;
|
||||||
private final List<ILuaAPI> apis = new ArrayList<>();
|
private final List<ApiWrapper> apis = new ArrayList<>();
|
||||||
private final ComputerThread scheduler;
|
private final ComputerThread scheduler;
|
||||||
private final MethodSupplier<LuaMethod> luaMethods;
|
private final MethodSupplier<LuaMethod> luaMethods;
|
||||||
final TimeoutState timeout;
|
final TimeoutState timeout;
|
||||||
@ -177,12 +177,12 @@ final class ComputerExecutor {
|
|||||||
var environment = computer.getEnvironment();
|
var environment = computer.getEnvironment();
|
||||||
|
|
||||||
// Add all default APIs to the loaded list.
|
// Add all default APIs to the loaded list.
|
||||||
apis.add(new TermAPI(environment));
|
addApi(new TermAPI(environment));
|
||||||
apis.add(new RedstoneAPI(environment));
|
addApi(new RedstoneAPI(environment));
|
||||||
apis.add(new FSAPI(environment));
|
addApi(new FSAPI(environment));
|
||||||
apis.add(new PeripheralAPI(environment, context.peripheralMethods()));
|
addApi(new PeripheralAPI(environment, context.peripheralMethods()));
|
||||||
apis.add(new OSAPI(environment));
|
addApi(new OSAPI(environment));
|
||||||
if (CoreConfig.httpEnabled) apis.add(new HTTPAPI(environment));
|
if (CoreConfig.httpEnabled) addApi(new HTTPAPI(environment));
|
||||||
|
|
||||||
// Load in the externally registered APIs.
|
// Load in the externally registered APIs.
|
||||||
for (var factory : context.apiFactories()) {
|
for (var factory : context.apiFactories()) {
|
||||||
@ -207,7 +207,7 @@ final class ComputerExecutor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void addApi(ILuaAPI api) {
|
void addApi(ILuaAPI api) {
|
||||||
apis.add(api);
|
apis.add(new ApiWrapper(api, null));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -385,7 +385,7 @@ final class ComputerExecutor {
|
|||||||
try (var bios = biosStream) {
|
try (var bios = biosStream) {
|
||||||
return luaFactory.create(new MachineEnvironment(
|
return luaFactory.create(new MachineEnvironment(
|
||||||
new LuaContext(computer), metrics, timeout,
|
new LuaContext(computer), metrics, timeout,
|
||||||
() -> apis.stream().map(api -> api instanceof ApiWrapper wrapper ? wrapper.getDelegate() : api).iterator(),
|
() -> apis.stream().map(ApiWrapper::api).iterator(),
|
||||||
luaMethods,
|
luaMethods,
|
||||||
computer.getGlobalEnvironment().getHostString()
|
computer.getGlobalEnvironment().getHostString()
|
||||||
), bios);
|
), bios);
|
||||||
|
Loading…
Reference in New Issue
Block a user