|
11 | 11 | import dan200.computercraft.api.peripheral.IComputerAccess; |
12 | 12 | import dan200.computercraft.api.peripheral.IPeripheral; |
13 | 13 | import dan200.computercraft.shared.computer.blocks.AbstractComputerBlockEntity; |
| 14 | +import dan200.computercraft.shared.computer.core.ServerComputer; |
14 | 15 | import net.minecraft.block.entity.BlockEntity; |
15 | 16 | import net.minecraft.util.math.Direction; |
16 | 17 | import org.windclan.embeddedcomputer.storage.harddrive.HardDrivePeripheral; |
17 | 18 | import org.jetbrains.annotations.Nullable; |
18 | 19 | import org.windclan.embeddedcomputer.embedded.block.EmbeddedComputerBlockEntity; |
19 | 20 |
|
| 21 | +import java.nio.ByteBuffer; |
| 22 | +import java.nio.channels.SeekableByteChannel; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.nio.file.OpenOption; |
| 25 | +import java.nio.file.StandardOpenOption; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Scanner; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +import static dan200.computercraft.shared.pocket.items.PocketComputerItem.getServerComputer; |
20 | 31 | import static java.util.Objects.isNull; |
21 | 32 | import static org.windclan.embeddedcomputer.main.log; |
22 | 33 |
|
@@ -57,17 +68,66 @@ public final void reboot() { |
57 | 68 | if (!isNull(comp1)) comp1.reboot(); |
58 | 69 | } |
59 | 70 | @LuaFunction(mainThread = true) |
60 | | - public final void format() { |
| 71 | + public final boolean format() { |
61 | 72 | var comp1 = getServerComp(); |
| 73 | + WritableMount mnt; |
62 | 74 | if (!isNull(comp1)) { |
63 | 75 | try { |
64 | | - WritableMount fs = ComputerCraftAPI.createSaveDirMount(comp1.getLevel().getServer(), "computer/" + comp1.getID(), 100); |
| 76 | + mnt = comp1.createRootMount(); |
| 77 | + if (mnt.exists(".LOCKED")) { |
| 78 | + return false; |
| 79 | + } |
65 | 80 | try { |
66 | | - fs.delete("/"); |
67 | | - }catch(Exception ignored){} |
68 | | - comp1.reboot(); |
69 | | - }catch(Exception ignored){ |
70 | | - log.info(ignored.getMessage(),ignored.fillInStackTrace()); |
| 81 | + mnt.delete("/"); |
| 82 | + comp1.reboot(); |
| 83 | + return true; |
| 84 | + }catch(Exception ignored){ |
| 85 | + comp1.reboot(); |
| 86 | + return false; |
| 87 | + } |
| 88 | + }catch(Exception ex){ |
| 89 | + log.info(ex.getMessage(),ex.fillInStackTrace()); |
| 90 | + return false; |
| 91 | + } |
| 92 | + } |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + @LuaFunction(mainThread = true) |
| 97 | + public final void unlock(String pass1) { |
| 98 | + ServerComputer comp1 = getServerComp(); |
| 99 | + if (!isNull(comp)) { |
| 100 | + WritableMount mnt; |
| 101 | + SeekableByteChannel root = null; |
| 102 | + Scanner scan = null; |
| 103 | + try { |
| 104 | + mnt = comp1.createRootMount(); |
| 105 | + if (mnt.exists(".LOCKED")) { |
| 106 | + String pass = ""; |
| 107 | + root = comp1.createRootMount().openForRead(".LOCKED"); |
| 108 | + scan = new Scanner(root); |
| 109 | + while (scan.hasNext()) { |
| 110 | + pass+=scan.next(); |
| 111 | + } |
| 112 | + scan.close(); |
| 113 | + if (!pass.equals(pass1)) { |
| 114 | + root.close(); |
| 115 | + return; |
| 116 | + } |
| 117 | + mnt.delete(".LOCKED"); |
| 118 | + return; |
| 119 | + } |
| 120 | + } catch (Exception ex) { |
| 121 | + log.warn(ex.toString()); |
| 122 | + if (!isNull(root)) { |
| 123 | + try { |
| 124 | + root.close(); |
| 125 | + } catch (Exception ignored) {} |
| 126 | + } |
| 127 | + if (!isNull(scan)) { |
| 128 | + scan.close(); |
| 129 | + } |
| 130 | + return; |
71 | 131 | } |
72 | 132 | } |
73 | 133 | } |
|
0 commit comments