mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-10-22 17:37:38 +00:00
Move file transfer API to the code library
This is useful for emulators, which might want to emulate the event.
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
// SPDX-FileCopyrightText: 2022 The CC: Tweaked Developers
|
||||
//
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dan200.computercraft.core.apis.transfer;
|
||||
|
||||
import dan200.computercraft.api.lua.LuaFunction;
|
||||
import dan200.computercraft.core.apis.handles.BinaryReadableHandle;
|
||||
import dan200.computercraft.core.apis.handles.ByteBufferChannel;
|
||||
import dan200.computercraft.core.methods.ObjectSource;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* A binary file handle that has been transferred to this computer.
|
||||
* <p>
|
||||
* This inherits all methods of {@link BinaryReadableHandle binary file handles}, meaning you can use the standard
|
||||
* {@link BinaryReadableHandle#read(Optional) read functions} to access the contents of the file.
|
||||
*
|
||||
* @cc.module [kind=event] file_transfer.TransferredFile
|
||||
* @see BinaryReadableHandle
|
||||
*/
|
||||
public class TransferredFile implements ObjectSource {
|
||||
private final String name;
|
||||
private final BinaryReadableHandle handle;
|
||||
|
||||
public TransferredFile(String name, ByteBuffer contents) {
|
||||
this.name = name;
|
||||
handle = BinaryReadableHandle.of(new ByteBufferChannel(contents));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of this file being transferred.
|
||||
*
|
||||
* @return The file's name.
|
||||
*/
|
||||
@LuaFunction
|
||||
public final String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Object> getExtra() {
|
||||
return Collections.singleton(handle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
// SPDX-FileCopyrightText: 2022 The CC: Tweaked Developers
|
||||
//
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
|
||||
package dan200.computercraft.core.apis.transfer;
|
||||
|
||||
import dan200.computercraft.api.lua.LuaFunction;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
/**
|
||||
* A list of files that have been transferred to this computer.
|
||||
*
|
||||
* @cc.module [kind=event] file_transfer.TransferredFiles
|
||||
*/
|
||||
public class TransferredFiles {
|
||||
public static final String EVENT = "file_transfer";
|
||||
|
||||
private final AtomicBoolean consumed = new AtomicBoolean(false);
|
||||
private final Runnable onConsumed;
|
||||
|
||||
private final List<TransferredFile> files;
|
||||
|
||||
public TransferredFiles(List<TransferredFile> files, Runnable onConsumed) {
|
||||
this.files = files;
|
||||
this.onConsumed = onConsumed;
|
||||
}
|
||||
|
||||
/**
|
||||
* All the files that are being transferred to this computer.
|
||||
*
|
||||
* @return The list of files.
|
||||
*/
|
||||
@LuaFunction
|
||||
public final List<TransferredFile> getFiles() {
|
||||
consumed();
|
||||
return files;
|
||||
}
|
||||
|
||||
private void consumed() {
|
||||
if (consumed.getAndSet(true)) return;
|
||||
onConsumed.run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user