Compare commits

...

19 Commits

Author SHA1 Message Date
Lupus590 44ed7456a8
Merge 8d3e1c5be9 into 929debd382 2024-04-25 11:09:55 +01:00
Jonathan Coates 929debd382
Don't build the webside on Windows/Mac
It seems to stall on Mac, and unlike Windows, I don't have access to a
machine to debug it :/.
2024-04-24 21:49:49 +01:00
Jonathan Coates 4980b7355d
Don't share CharsetDecoders across threads
Fixes #1803
2024-04-24 21:19:30 +01:00
Lupus590 8d3e1c5be9 Merge branch 'mc-1.20.x' into docs/startup_files 2023-10-10 18:08:12 +01:00
Lupus590 1d5463d2dd Merge branch 'mc-1.16.x' of https://github.com/SquidDev-CC/CC-Tweaked into docs/startup_files 2022-06-09 08:55:38 +01:00
Lupus590 e43314dd35 move mass config to top 2022-06-09 08:54:25 +01:00
Lupus590 67706cfb83 reword a bit 2022-06-09 08:49:53 +01:00
Lupus590 f835bb7f11 Move persistence stuff 2022-06-09 07:46:52 +01:00
Lupus590 f583df3392 add missing note header
off topic change
2022-06-09 07:46:20 +01:00
Lupus590 19e2afc6a6 Move case sensitivity to fs api page 2022-06-09 07:45:52 +01:00
Lupus590 8eb463b6ae Merge branch 'mc-1.16.x' of https://github.com/SquidDev-CC/CC-Tweaked into docs/startup_files 2022-06-07 16:11:56 +01:00
Lupus590 48fcd701ac Merge branch 'mc-1.16.x' of https://github.com/SquidDev-CC/CC-Tweaked into docs/startup_files 2022-06-07 02:07:52 +01:00
Lupus590 71409fac43 Merge branch 'mc-1.16.x' of https://github.com/SquidDev-CC/CC-Tweaked into docs/startup_files 2022-06-01 11:58:06 +01:00
Lupus590 3823202b63
turns on instead of boots
Co-authored-by: Jonathan Coates <git@squiddev.cc>
2022-05-05 17:37:41 +01:00
Lupus590 ae48f5dda9
Changes suggested by grammerly
there's a few more but these are the ones that I think are good changes
2022-04-29 01:24:02 +01:00
Lupus590 cd6fd8e6ca
fix another typo 2022-04-14 16:02:19 +01:00
Lupus590 f3935280d6
fix typos 2022-04-14 15:57:31 +01:00
Lupus590 89899f0ee2
fix typo 2022-04-14 14:34:27 +01:00
Lupus590 190c7c0221 document how to use startup files 2022-04-14 00:53:28 +01:00
4 changed files with 73 additions and 3 deletions

View File

@ -97,7 +97,7 @@ jobs:
- name: ⚒️ Build
run: |
./gradlew --configure-on-demand :core:assemble :web:assemble
./gradlew --configure-on-demand :core:assemble
- name: 🧪 Run tests
run: |

View File

@ -0,0 +1,62 @@
---
module: [kind=guide] startup_files
---
# Running code when a computer turns on
:::note Use case
You might be aware that CC computers restart when the chunk they are in reloads, startup files provides a way to have the CC computer run arbitrary code after it has finished turning on.
:::
:::note Mass configuration
Unless disabled in the server config, CC computers will look for startup files on disks in attached disk drives. This means that a freshly crafted computer can automatically load a program from that disk startup file.
Apply this to turtles with a nearby chest containing fuel and you can have a turtle factory that automatically programs the turtles after creating them.
:::
CC computers will look for a file or folder with a special name when it finishes loading. Also, CC has a fixed order in which it looks for these files and uses them.
## The magic names
All startup *files* have to be at the root of their drive, this drive is commonly the computer's internal storage, but can be a disk too.
```
startup
startup.lua
```
`startup` may be a file or a folder, the behavior of it changes depending on which it is. If it's a file then it is ran, if it's a folder then each file inside it is ran.
`startup.lua` has to be a file.
Note that these are all lower case, case matters sometimes so keep to the correct one - more info in a note on the @{fs} API page.
## Searching drives for startup files
If `shell.allow_disk_startup` is true (which it is by default), then the CC computer will look for disk drives that have at least one startup file, it only uses the first disk that fulfils these criteria. Disk drives are searched in the order they are found via `peripheral.getNames()`. It then runs the found startup files as described below.
If `shell.allow_startup` is true (which it is by default) and no disk startup file was used, then the computer will look for startup files in its internal storage. Valid files are run as described below.
### Running the startup files
If `startup` is a file then this file and *only* this file is run, even if a `startup.lua` exists on the same device.
If `startup` is not a file (either it's a folder or it doesn't exist) then `startup.lua` is run, assuming it exists. After running `startup.lua` (or not if it didn't exist), the CC computer then runs each file in the `startup` folder one after the other in the order that @{fs.list} returns them.
:::note Example
Let's say that I have a computer with a startup file on its internal storage, and is connected to six disk drives. Two of these drives have disks that have startup files, one has a disk with no files, one with files but none of them are startup files, one has a music disk (the kind that you can place in a jukebox), the remaining drive has no disk in it.
Both of the settings that affect startup are set to allow their respective startup modes.
I turn on my computer, it looks for disk drives. It so happens that the first one it finds is the empty one, since this drive has no disk it goes to the next drive it found. The second drive that it searches for startup files is the jukebox music disk, this one is also skipped as music disks don't have startup files. Third, it finds the disk drive containing the disk without files, it goes to the next drive. Forth is the disk with files but without startup files, it tries the next disk.
We are down to the two disks both with startup files, the computer finds one of them and searches it for startup files, it finds the files and runs them in the order above. When/if these startup files exit without shutting down the computer then the CC computer drops into the interactive prompt. The computer *didn't* look at the last disk drive and *didn't* use the startup files on its internal storage.
:::
:::note Computers don't remember what they were doing when the chunk unloads
You may have heard of the term "persistence", Computercraft computers do not have persistence. Startup files are currently the closest that CC has to persistence.
If you don't know what persistence is, if CC computers were persistent then they would remember what they were doing when the chunk they are in was unloaded, and resume running the task they were doing when the chunk gets reloaded.
However, Computercraft doesn't have persistence and thus restart as if they were just turned on. Mentioning turning on, they will remember that they were running and when the chunk reloads they will turn themselves on and start looking for startup files. Clever use of startup files and recording data to disk can somewhat circumvent the lack of built in persistence.
For most programs (usually running on computers), restarting from the beginning of the program is not an issue. However, turtles commonly have startup complications due to their ability to move. Unless you make sure that the turtle is in a fixed starting position every time (which is not always practical), your turtle programs will need some way to cope with the turtle starting in a different position - perhaps with the @{gps} API.
Incase it's not clear, Minecraft unloads all chunks (including force loaded chunks with chunk loaders) when the server restarts and when the single player world closes. So you may want startup files even if you are chunk loading your computers.
:::

View File

@ -52,6 +52,14 @@
* an incredibly large file) will fail. You can see a mount's capacity with {@link #getCapacity} and the remaining
* space with {@link #getFreeSpace}.
*
* :::note Case sensitivity
* CC inherits the case sensitivity of the real world OS that the Minecraft server is running on, so if you are used to
* Microsoft Windows' case insensitivity (e.g. `startup.lua` and `StartUp.Lua` are considered the same when used as
* file names) and play on a server (which will likely be running Linux which is case sensitive) then you should keep
* in mind that programs which work on your machine in singleplayer may not work correctly on the server if you are
* inconsistent with the case that you use for file names.
* :::
*
* @cc.module fs
*/
public class FSAPI implements ILuaAPI {

View File

@ -29,7 +29,7 @@
* @see dan200.computercraft.core.apis.HTTPAPI#websocket On how to open a websocket.
*/
public class WebsocketHandle {
private static final CharsetDecoder DECODER = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE);
private static final ThreadLocal<CharsetDecoder> DECODER = ThreadLocal.withInitial(() -> StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPLACE));
private final IAPIEnvironment environment;
private final String address;
@ -87,7 +87,7 @@ public final void send(Coerced<ByteBuffer> message, Optional<Boolean> binary) th
websocket.sendBinary(text);
} else {
try {
websocket.sendText(DECODER.decode(text).toString());
websocket.sendText(DECODER.get().decode(text).toString());
} catch (CharacterCodingException e) {
// This shouldn't happen, but worth mentioning.
throw new LuaException("Message is not valid UTF8");