mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-11-03 23:22:59 +00:00 
			
		
		
		
	Compare commits
	
		
			103 Commits
		
	
	
		
			v1.80pr1.1
			...
			v1.12.2-1.
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					6be330ae8d | ||
| 
						 | 
					4569af2130 | ||
| 
						 | 
					765c31315a | ||
| 
						 | 
					0e191e42a0 | ||
| 
						 | 
					ca34b2a1b8 | ||
| 
						 | 
					1fd0b40776 | ||
| 
						 | 
					2965fb666f | ||
| 
						 | 
					390575ab4d | ||
| 
						 | 
					e4ef92ca2d | ||
| 
						 | 
					9bf586b018 | ||
| 
						 | 
					173ea72001 | ||
| 
						 | 
					1230cabcb0 | ||
| 
						 | 
					6ed03e1fcd | ||
| 
						 | 
					c4b371b124 | ||
| 
						 | 
					a600213b00 | ||
| 
						 | 
					7799b8d4cb | ||
| 
						 | 
					245bf26480 | ||
| 
						 | 
					5d05205d69 | ||
| 
						 | 
					853e2622a1 | ||
| 
						 | 
					d0bf9e9cd7 | ||
| 
						 | 
					7a7951ae68 | ||
| 
						 | 
					bd28955c8e | ||
| 
						 | 
					e46f09a939 | ||
| 
						 | 
					71b1f8138d | ||
| 
						 | 
					1d82a1c98c | ||
| 
						 | 
					b5f60f3f11 | ||
| 
						 | 
					259665d9f1 | ||
| 
						 | 
					ba823bae13 | ||
| 
						 | 
					1290a4402c | ||
| 
						 | 
					379076a5e2 | ||
| 
						 | 
					d12bdf50d8 | ||
| 
						 | 
					cbfd5aeeee | ||
| 
						 | 
					41429bdc0b | ||
| 
						 | 
					54b9966feb | ||
| 
						 | 
					105c66127c | ||
| 
						 | 
					765ad0bd3f | ||
| 
						 | 
					dd05478483 | ||
| 
						 | 
					5d028dea39 | ||
| 
						 | 
					629c51d260 | ||
| 
						 | 
					9ea57961af | ||
| 
						 | 
					07b9b1c9c7 | ||
| 
						 | 
					5b942ff9c1 | ||
| 
						 | 
					7b5a918941 | ||
| 
						 | 
					47721bf76b | ||
| 
						 | 
					35ce0974cd | ||
| 
						 | 
					52e1906d42 | ||
| 
						 | 
					eaf24a3ceb | ||
| 
						 | 
					62760e371e | ||
| 
						 | 
					e154e11186 | ||
| 
						 | 
					72d079ef61 | ||
| 
						 | 
					0bfb7049b0 | ||
| 
						 | 
					f7cb526793 | ||
| 
						 | 
					e34e833d3d | ||
| 
						 | 
					a125a19728 | ||
| 
						 | 
					b3e6a53868 | ||
| 
						 | 
					218f8e53bb | ||
| 
						 | 
					d02575528b | ||
| 
						 | 
					c78adb2cdc | ||
| 
						 | 
					3e28f79ce9 | ||
| 
						 | 
					67af7a698b | ||
| 
						 | 
					06e76f9b15 | ||
| 
						 | 
					6d383d005c | ||
| 
						 | 
					c373583723 | ||
| 
						 | 
					f1d10809d5 | ||
| 
						 | 
					474f571798 | ||
| 
						 | 
					fb9c125ab8 | ||
| 
						 | 
					162fb37421 | ||
| 
						 | 
					d953f031f0 | ||
| 
						 | 
					7fde89ad95 | ||
| 
						 | 
					bd04a93ffb | ||
| 
						 | 
					e2bfaafe28 | ||
| 
						 | 
					1fb3d16b89 | ||
| 
						 | 
					35645b3d93 | ||
| 
						 | 
					a4cd1fe77d | ||
| 
						 | 
					4145914024 | ||
| 
						 | 
					6bd11a5e4a | ||
| 
						 | 
					46fa798797 | ||
| 
						 | 
					70a226207e | ||
| 
						 | 
					257a35f3ed | ||
| 
						 | 
					af01b9514b | ||
| 
						 | 
					070fd1f2ff | ||
| 
						 | 
					fb59da2b06 | ||
| 
						 | 
					11e4d0de82 | ||
| 
						 | 
					e46ab1e267 | ||
| 
						 | 
					d6e0f368df | ||
| 
						 | 
					9f2884bc0f | ||
| 
						 | 
					18d468e887 | ||
| 
						 | 
					63f6735bb8 | ||
| 
						 | 
					ab6f0ccd16 | ||
| 
						 | 
					ae0f093e73 | ||
| 
						 | 
					e5f988e3fe | ||
| 
						 | 
					12e82afad2 | ||
| 
						 | 
					6c2db93cbd | ||
| 
						 | 
					d5edbe700b | ||
| 
						 | 
					86ad43c3ab | ||
| 
						 | 
					f450c0156b | ||
| 
						 | 
					8abcfcb4ac | ||
| 
						 | 
					f3cace1d03 | ||
| 
						 | 
					e1e5e898ab | ||
| 
						 | 
					3aa3852ff6 | ||
| 
						 | 
					709a6329c7 | ||
| 
						 | 
					c9f05a2939 | ||
| 
						 | 
					e41377f862 | 
							
								
								
									
										5
									
								
								.github/ISSUE_TEMPLATE/bug_report.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.github/ISSUE_TEMPLATE/bug_report.md
									
									
									
									
										vendored
									
									
								
							@@ -6,11 +6,10 @@ about: Report some misbehaviour in the mod
 | 
			
		||||
 | 
			
		||||
<!--
 | 
			
		||||
## Before reporting
 | 
			
		||||
 - Search for the bug both here and [on the ComputerCraft issues page](https://github.com/dan200/ComputerCraft/issues?utf8=%E2%9C%93&q=is%3Aissue+)
 | 
			
		||||
 - If possible, try to reproduce on vanilla ComputerCraft. If it still occurs, [report on the ComputerCraft repo](https://github.com/dan200/ComputerCraft/issues/new) instead.
 | 
			
		||||
 - Search for the bug on the issue tracker. Make sure to look at closed issues too!
 | 
			
		||||
-->
 | 
			
		||||
 | 
			
		||||
## Useful information to include:
 | 
			
		||||
 - Minecraft version
 | 
			
		||||
 - CC: Tweaked version
 | 
			
		||||
 - Detailed reproduction steps!** Sometimes I can spot a bug pretty easily, but often it's much more obscure. Anything you can give which will help reproduce it means it'll get fixed quicker.
 | 
			
		||||
 - Detailed reproduction steps: sometimes I can spot a bug pretty easily, but often it's much more obscure. The more information I have to help reproduce it, the quicker it'll get fixed.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										7
									
								
								.github/ISSUE_TEMPLATE/feature_request.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										7
									
								
								.github/ISSUE_TEMPLATE/feature_request.md
									
									
									
									
										vendored
									
									
								
							@@ -6,10 +6,9 @@ about: Suggest an idea or improvement
 | 
			
		||||
 | 
			
		||||
<!--
 | 
			
		||||
## Before reporting
 | 
			
		||||
 - Search for the suggestion both here and [on the ComputerCraft issues page](https://github.com/dan200/ComputerCraft/issues?utf8=%E2%9C%93&q=is%3Aissue+). It's possible someone's suggested it before!
 | 
			
		||||
 - Unless something is specific to CC:Tweaked, try to [suggest them on the ComputerCraft repo](https://github.com/dan200/ComputerCraft/issues/new). There's a lot more people watching it, so it allows the wider community to contribute.
 | 
			
		||||
 - Search for the suggestion here. It's possible someone's suggested it before!
 | 
			
		||||
-->
 | 
			
		||||
 | 
			
		||||
## Useful information to include:
 | 
			
		||||
 - Explanation of how the feature/change chould work.
 | 
			
		||||
 - Some rationale/use case for a feature. I'd like to keep CC:T as minimal
 | 
			
		||||
 - Explanation of how the feature/change should work.
 | 
			
		||||
 - Some rationale/use case for a feature. I'd like to keep CC:T as minimal as possible, so I like have a solid justification for each feature.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										9
									
								
								.github/pull_request_template.md
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										9
									
								
								.github/pull_request_template.md
									
									
									
									
										vendored
									
									
								
							@@ -1,9 +0,0 @@
 | 
			
		||||
<!--
 | 
			
		||||
Unless this feature is specific to CC:Tweaked, try to [target the original ComputerCraft repo](https://github.com/dan200/ComputerCraft/) instead. There's a lot more people watching it, so it allows the wider community to contribute.
 | 
			
		||||
-->
 | 
			
		||||
 | 
			
		||||
## Useful information to include:
 | 
			
		||||
 - Brief explanation of the changes you've made.
 | 
			
		||||
 - Rationale of why this change has been made/reasoning behind it.
 | 
			
		||||
 | 
			
		||||
The more information you can provide, the easier it is to review something now _and_ to see why a change was made, when the code needs updating in the future.
 | 
			
		||||
							
								
								
									
										19
									
								
								LICENSE-luaj
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								LICENSE-luaj
									
									
									
									
									
								
							@@ -1,19 +0,0 @@
 | 
			
		||||
Copyright (c) 2007 LuaJ. All rights reserved.
 | 
			
		||||
 | 
			
		||||
Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
			
		||||
of this software and associated documentation files (the "Software"), to deal
 | 
			
		||||
in the Software without restriction, including without limitation the rights
 | 
			
		||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 | 
			
		||||
copies of the Software, and to permit persons to whom the Software is
 | 
			
		||||
furnished to do so, subject to the following conditions:
 | 
			
		||||
 | 
			
		||||
The above copyright notice and this permission notice shall be included in
 | 
			
		||||
all copies or substantial portions of the Software.
 | 
			
		||||
 | 
			
		||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | 
			
		||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | 
			
		||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 | 
			
		||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | 
			
		||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 | 
			
		||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 | 
			
		||||
THE SOFTWARE.
 | 
			
		||||
							
								
								
									
										69
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										69
									
								
								README.md
									
									
									
									
									
								
							@@ -1,35 +1,35 @@
 | 
			
		||||
# 
 | 
			
		||||
[](https://travis-ci.org/SquidDev-CC/CC-Tweaked)
 | 
			
		||||
 | 
			
		||||
CC: Tweaked is a fork of ComputerCraft which aims to provide earlier access to the more experimental and in-development
 | 
			
		||||
features of the mod. For a more stable experience, I recommend checking out the
 | 
			
		||||
[original mod](https://github.com/dan200/ComputerCraft).
 | 
			
		||||
CC: Tweaked is a fork of [ComputerCraft](https://github.com/dan200/ComputerCraft), adding programmable computers,
 | 
			
		||||
turtles and more to Minecraft.
 | 
			
		||||
 | 
			
		||||
## What?
 | 
			
		||||
CC: Tweaked (or CC:T for short) does not aim to create a competing fork of ComputerCraft, nor am I planning to take it
 | 
			
		||||
in in a vastly different direction to the original mod. In fact, CC:T aims to be a nurturing ground for various
 | 
			
		||||
features, with a pull request against the original mod being the end goal.
 | 
			
		||||
ComputerCraft has always held a fond place in my heart: it's the mod which really got me into Minecraft, and it's the
 | 
			
		||||
mod which has kept me playing it for many years. However, development of the original mod has slowed, as the original
 | 
			
		||||
developers have had less time to work on the mod, and moved onto other projects and commitments.
 | 
			
		||||
 | 
			
		||||
CC:T also includes many pull requests from the community which have not yet been merged, offering a large number
 | 
			
		||||
of additional bug fixes and features over the original mod.
 | 
			
		||||
CC:Tweaked (or CC:T for short) is an attempt to continue ComputerCraft's legacy. It's not intended to be a competitor
 | 
			
		||||
to CC, nor do I want to take it in a vastly different direction to the original mod. Instead, CC:T focuses on making the
 | 
			
		||||
ComputerCraft experience as _solid_ as possible, ironing out any wrinkles that may have developed over time.
 | 
			
		||||
 | 
			
		||||
## Features
 | 
			
		||||
CC: Tweaked contains all the features of the latest alpha, as well as numerous fixes, performance improvements and
 | 
			
		||||
several additional features. I'd recommend checking out [the releases page](https://github.com/SquidDev-CC/CC-Tweaked/releases)
 | 
			
		||||
to see the full changes, but here's a couple of the more interesting changes:
 | 
			
		||||
CC: Tweaked contains all the features of the latest version of ComputerCraft, as well as numerous fixes, performance
 | 
			
		||||
improvements and several nifty additions. I'd recommend checking out [the releases page](https://github.com/SquidDev-CC/CC-Tweaked/releases)
 | 
			
		||||
to see the full set of changes, but here's a couple of the more interesting additions:
 | 
			
		||||
 | 
			
		||||
 - Replace LuaJ with Cobalt.
 | 
			
		||||
 - Allow running multiple computers at the same time.
 | 
			
		||||
 - Websocket support in the HTTP library.
 | 
			
		||||
 - Wired modems and cables act more like multiparts.
 | 
			
		||||
 - Add map-like rendering for pocket computers and printed pages/books.
 | 
			
		||||
 - Adds the `/computercraft` command, offering various diagnostic tools for server owners. This allows operators to
 | 
			
		||||
   track which computers are hogging resources, turn on and shutdown multiple computers at once and interact with
 | 
			
		||||
   computers remotely.
 | 
			
		||||
 - Add full-block wired modems, allowing one to wrap non-solid peripherals (such as turtles, or chests if Plethora is
 | 
			
		||||
 - Improvements to the `http` library, including websockets, support for other HTTP methods (`PUT`, `DELETE`, etc...)
 | 
			
		||||
   and configurable limits on HTTP usage.
 | 
			
		||||
 - Full-block wired modems, allowing one to wrap non-solid peripherals (such as turtles, or chests if Plethora is
 | 
			
		||||
   installed).
 | 
			
		||||
 - Extended binary file handles. They support file seeking, and reading new lines, allowing full (and accurate)
 | 
			
		||||
   emulation of the standard Lua `io` library.
 | 
			
		||||
 - Pocket computers can be held like maps, allowing you to view the screen without entering a GUI.
 | 
			
		||||
 - Printed pages and books can be placed in item frames and held like maps.
 | 
			
		||||
 - Several profiling and administration tools for server owners, via the `/computercraft` command. This allows operators
 | 
			
		||||
   to track which computers are hogging resources, turn on and shutdown multiple computers at once and interact with
 | 
			
		||||
   computers remotely.
 | 
			
		||||
 - Closer emulation of standard Lua, adding the `debug` and `io` libraries. This also enables seeking within binary
 | 
			
		||||
   files, meaning you don't need to read large files into memory.
 | 
			
		||||
 - Allow running multiple computers on multiple threads, reducing latency on worlds with many computers.
 | 
			
		||||
 | 
			
		||||
## Relation to CCTweaks?
 | 
			
		||||
This mod has nothing to do with CCTweaks, though there is no denying the name is a throwback to it. That being said,
 | 
			
		||||
@@ -37,13 +37,30 @@ several features have been included, such as full block modems, the Cobalt runti
 | 
			
		||||
computers.
 | 
			
		||||
 | 
			
		||||
## Contributing
 | 
			
		||||
Any contribution is welcome, be that using the mod, reporting bugs or contributing code. If you do wish to contribute
 | 
			
		||||
code, do consider submitting it to the ComputerCraft repository first.
 | 
			
		||||
 | 
			
		||||
That being said, in order to start helping develop CC:T, you'll need to follow these steps:
 | 
			
		||||
Any contribution is welcome, be that using the mod, reporting bugs or contributing code. In order to start helping
 | 
			
		||||
develop CC:T, you'll need to follow these steps:
 | 
			
		||||
 | 
			
		||||
 - **Clone the repository:** `git clone https://github.com/SquidDev-CC/CC-Tweaked.git && cd CC-Tweaked`
 | 
			
		||||
 - **Setup Forge:** `./gradlew setupDecompWorkspace`
 | 
			
		||||
 - **Test your changes:** `./gradlew runClient` (or run the `GradleStart` class from your IDE).
 | 
			
		||||
 | 
			
		||||
If you want to run CC:T in a normal Minecraft instance, run `./gradlew build` and copy the `.jar` from `build/libs`.
 | 
			
		||||
 | 
			
		||||
## Using
 | 
			
		||||
If you want to depend on CC:Tweaked, we have a maven repo. However, you should be wary that some functionality is only
 | 
			
		||||
exposed by CC:T's API and not vanilla ComputerCraft. If you wish to support all variations of ComputerCraft, I recommend
 | 
			
		||||
using [cc.crzd.me's maven](https://cc.crzd.me/maven/) instead.
 | 
			
		||||
 | 
			
		||||
```groovy
 | 
			
		||||
dependencies {
 | 
			
		||||
  maven { url 'https://squiddev.cc/maven/' }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
dependencies {
 | 
			
		||||
  implementation "org.squiddev:cc-tweaked-${mc_version}:${cct_version}"
 | 
			
		||||
}
 | 
			
		||||
```
 | 
			
		||||
 | 
			
		||||
You should also be careful to only use classes within the `dan200.computercraft.api` package. Non-API classes are
 | 
			
		||||
subject to change at any point. If you depend on functionality outside the API, file an issue, and we can look into
 | 
			
		||||
exposing more features.
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										198
									
								
								build.gradle
									
									
									
									
									
								
							
							
						
						
									
										198
									
								
								build.gradle
									
									
									
									
									
								
							@@ -9,8 +9,10 @@ buildscript {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    dependencies {
 | 
			
		||||
        classpath 'com.google.code.gson:gson:2.8.1'
 | 
			
		||||
        classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
 | 
			
		||||
        classpath 'org.ajoberstar:gradle-git:1.6.0'
 | 
			
		||||
        classpath 'net.sf.proguard:proguard-gradle:6.1.0beta1'
 | 
			
		||||
        classpath 'org.ajoberstar.grgit:grgit-gradle:3.0.0'
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -23,35 +25,37 @@ apply plugin: 'org.ajoberstar.grgit'
 | 
			
		||||
apply plugin: 'maven-publish'
 | 
			
		||||
apply plugin: 'maven'
 | 
			
		||||
 | 
			
		||||
version = "1.80pr1.14"
 | 
			
		||||
version = mod_version
 | 
			
		||||
 | 
			
		||||
group = "org.squiddev"
 | 
			
		||||
archivesBaseName = "cc-tweaked"
 | 
			
		||||
archivesBaseName = "cc-tweaked-${mc_version}"
 | 
			
		||||
 | 
			
		||||
minecraft {
 | 
			
		||||
    version = "1.12.2-14.23.4.2749"
 | 
			
		||||
    version = "${mc_version}-${forge_version}"
 | 
			
		||||
    runDir = "run"
 | 
			
		||||
    replace '${version}', project.version
 | 
			
		||||
    replace '${version}', mod_version
 | 
			
		||||
 | 
			
		||||
    // the mappings can be changed at any time, and must be in the following format.
 | 
			
		||||
    // snapshot_YYYYMMDD   snapshot are built nightly.
 | 
			
		||||
    // stable_#            stables are built at the discretion of the MCP team.
 | 
			
		||||
    // Use non-default mappings at your own risk. they may not allways work.
 | 
			
		||||
    // simply re-run your setup task after changing the mappings to update your workspace.
 | 
			
		||||
    mappings = "snapshot_20180724"
 | 
			
		||||
    // makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
 | 
			
		||||
    mappings = mappings_version
 | 
			
		||||
    makeObfSourceJar = false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
repositories {
 | 
			
		||||
    maven {
 | 
			
		||||
        name = "JEI"
 | 
			
		||||
        url  = "http://dvs1.progwml6.com/files/maven"
 | 
			
		||||
        name "JEI"
 | 
			
		||||
        url "http://dvs1.progwml6.com/files/maven"
 | 
			
		||||
    }
 | 
			
		||||
    maven {
 | 
			
		||||
        name = "squiddev"
 | 
			
		||||
        url = "https://dl.bintray.com/squiddev/maven"
 | 
			
		||||
        name "SquidDev"
 | 
			
		||||
        url "https://squiddev.cc/maven"
 | 
			
		||||
    }
 | 
			
		||||
    ivy {
 | 
			
		||||
        name "Charset"
 | 
			
		||||
        artifactPattern "https://asie.pl/files/mods/Charset/LibOnly/[module]-[revision](-[classifier]).[ext]"
 | 
			
		||||
    }
 | 
			
		||||
    maven {
 | 
			
		||||
        name "Amadornes"
 | 
			
		||||
        url "http://maven.amadornes.com/"
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ivy { artifactPattern "https://asie.pl/files/mods/Charset/LibOnly/[module]-[revision](-[classifier]).[ext]" }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
configurations {
 | 
			
		||||
@@ -61,14 +65,16 @@ configurations {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
dependencies {
 | 
			
		||||
    deobfProvided "mezz.jei:jei_1.12.2:4.8.5.159:api"
 | 
			
		||||
    deobfProvided "mezz.jei:jei_1.12.2:4.15.0.269:api"
 | 
			
		||||
    deobfProvided "pl.asie:Charset-Lib:0.5.4.6"
 | 
			
		||||
    deobfProvided "MCMultiPart2:MCMultiPart:2.5.3"
 | 
			
		||||
 | 
			
		||||
    runtime "mezz.jei:jei_1.12.2:4.8.5.159"
 | 
			
		||||
    runtime "mezz.jei:jei_1.12.2:4.15.0.269"
 | 
			
		||||
 | 
			
		||||
    shade 'org.squiddev:Cobalt:0.4.0'
 | 
			
		||||
    shade 'org.squiddev:Cobalt:0.5.0-SNAPSHOT'
 | 
			
		||||
 | 
			
		||||
    testCompile 'junit:junit:4.11'
 | 
			
		||||
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.1.0'
 | 
			
		||||
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.1.0'
 | 
			
		||||
 | 
			
		||||
    deployerJars "org.apache.maven.wagon:wagon-ssh:3.0.0"
 | 
			
		||||
}
 | 
			
		||||
@@ -84,39 +90,91 @@ jar {
 | 
			
		||||
        attributes('FMLAT': 'computercraft_at.cfg')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    into("docs", { from (javadoc.destinationDir) })
 | 
			
		||||
 | 
			
		||||
    into("api", { from (sourceSets.main.allSource) {
 | 
			
		||||
    from (sourceSets.main.allSource) {
 | 
			
		||||
        include "dan200/computercraft/api/**/*.java"
 | 
			
		||||
    }})
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    from configurations.shade.collect { it.isDirectory() ? it : zipTree(it) }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
import java.nio.charset.StandardCharsets
 | 
			
		||||
import java.nio.file.*
 | 
			
		||||
import java.util.zip.*
 | 
			
		||||
 | 
			
		||||
import com.google.gson.GsonBuilder
 | 
			
		||||
import com.google.gson.JsonElement
 | 
			
		||||
import org.ajoberstar.grgit.Grgit
 | 
			
		||||
import proguard.gradle.ProGuardTask
 | 
			
		||||
 | 
			
		||||
task proguard(type: ProGuardTask, dependsOn: jar) {
 | 
			
		||||
    description "Removes unused shadowed classes from the jar"
 | 
			
		||||
    group "compact"
 | 
			
		||||
 | 
			
		||||
    injars jar.archivePath
 | 
			
		||||
    outjars "${jar.archivePath.absolutePath.replace(".jar", "")}-min.jar"
 | 
			
		||||
 | 
			
		||||
    // Add the main runtime jar and all non-shadowed dependencies
 | 
			
		||||
    libraryjars "${System.getProperty('java.home')}/lib/rt.jar"
 | 
			
		||||
    doFirst {
 | 
			
		||||
        sourceSets.main.compileClasspath
 | 
			
		||||
            .filter { !it.name.contains("Cobalt") }
 | 
			
		||||
            .each { libraryjars it }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // We want to avoid as much obfuscation as possible. We're only doing this to shrink code size.
 | 
			
		||||
    dontobfuscate; dontoptimize; keepattributes; keepparameternames
 | 
			
		||||
 | 
			
		||||
    // Proguard will remove directories by default, but that breaks JarMount.
 | 
			
		||||
    keepdirectories 'assets/computercraft/lua**'
 | 
			
		||||
 | 
			
		||||
    // Preserve ComputerCraft classes - we only want to strip shadowed files.
 | 
			
		||||
    keep 'class dan200.computercraft.** { *; }'
 | 
			
		||||
 | 
			
		||||
    // Preserve the constructors in Cobalt library class, as we init them via reflection
 | 
			
		||||
    keepclassmembers 'class org.squiddev.cobalt.lib.** { <init>(...); }'
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
task proguardMove(dependsOn: proguard) {
 | 
			
		||||
    description "Replace the original jar with the minified version"
 | 
			
		||||
    group "compact"
 | 
			
		||||
 | 
			
		||||
    doLast {
 | 
			
		||||
        Files.move(
 | 
			
		||||
            file("${jar.archivePath.absolutePath.replace(".jar", "")}-min.jar").toPath(),
 | 
			
		||||
            file(jar.archivePath).toPath(),
 | 
			
		||||
            StandardCopyOption.REPLACE_EXISTING
 | 
			
		||||
        )
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
reobfJar.dependsOn proguardMove
 | 
			
		||||
 | 
			
		||||
processResources {
 | 
			
		||||
    inputs.property "version", project.version
 | 
			
		||||
    inputs.property "mcversion", project.minecraft.version
 | 
			
		||||
    inputs.property "version", mod_version
 | 
			
		||||
    inputs.property "mcversion", mc_version
 | 
			
		||||
 | 
			
		||||
    def grgit = Grgit.open(dir: '.')
 | 
			
		||||
    inputs.property "commithash", grgit.head().id
 | 
			
		||||
 | 
			
		||||
    def blacklist = ['GitHub', 'dan200', 'Daniel Ratcliffe']
 | 
			
		||||
    def hash = 'none'
 | 
			
		||||
    Set<String> contributors = []
 | 
			
		||||
    try {
 | 
			
		||||
        def grgit = Grgit.open(dir: '.')
 | 
			
		||||
        hash = grgit.head().id
 | 
			
		||||
 | 
			
		||||
    grgit.log().each {
 | 
			
		||||
        if (!blacklist.contains(it.author.name)) contributors.add(it.author.name)
 | 
			
		||||
        if (!blacklist.contains(it.committer.name)) contributors.add(it.committer.name)
 | 
			
		||||
    }
 | 
			
		||||
        def blacklist = ['GitHub', 'dan200', 'Daniel Ratcliffe']
 | 
			
		||||
        grgit.log().each {
 | 
			
		||||
            if (!blacklist.contains(it.author.name)) contributors.add(it.author.name)
 | 
			
		||||
            if (!blacklist.contains(it.committer.name)) contributors.add(it.committer.name)
 | 
			
		||||
        }
 | 
			
		||||
    } catch(Exception ignored) { }
 | 
			
		||||
 | 
			
		||||
    inputs.property "commithash", hash
 | 
			
		||||
 | 
			
		||||
    from(sourceSets.main.resources.srcDirs) {
 | 
			
		||||
        include 'mcmod.info'
 | 
			
		||||
        include 'assets/computercraft/lua/rom/help/credits.txt'
 | 
			
		||||
 | 
			
		||||
        expand 'version':project.version,
 | 
			
		||||
               'mcversion':project.minecraft.version,
 | 
			
		||||
               'gitcontributors':contributors.sort(false, String.CASE_INSENSITIVE_ORDER).join('\n')
 | 
			
		||||
        expand 'version': mod_version,
 | 
			
		||||
               'mcversion': mc_version,
 | 
			
		||||
               'gitcontributors': contributors.sort(false, String.CASE_INSENSITIVE_ORDER).join('\n')
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    from(sourceSets.main.resources.srcDirs) {
 | 
			
		||||
@@ -125,13 +183,54 @@ processResources {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
task compressJson(dependsOn: extractAnnotationsJar) {
 | 
			
		||||
    group "compact"
 | 
			
		||||
    description "Minifies all JSON files, stripping whitespace"
 | 
			
		||||
 | 
			
		||||
    def jarPath = file(jar.archivePath)
 | 
			
		||||
 | 
			
		||||
    def tempPath = File.createTempFile("input", ".jar", temporaryDir)
 | 
			
		||||
    tempPath.deleteOnExit()
 | 
			
		||||
 | 
			
		||||
    def gson = new GsonBuilder().create()
 | 
			
		||||
 | 
			
		||||
    doLast {
 | 
			
		||||
        // Copy over all files in the current jar to the new one, running json files from GSON. As pretty printing
 | 
			
		||||
        // is turned off, they should be minified.
 | 
			
		||||
        new ZipFile(jarPath).withCloseable { inJar ->
 | 
			
		||||
            new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(tempPath))).withCloseable { outJar ->
 | 
			
		||||
                inJar.entries().each { entry ->
 | 
			
		||||
                    if(entry.directory) {
 | 
			
		||||
                        outJar.putNextEntry(entry)
 | 
			
		||||
                    } else if(!entry.name.endsWith(".json")) {
 | 
			
		||||
                        outJar.putNextEntry(entry)
 | 
			
		||||
                        inJar.getInputStream(entry).withCloseable { outJar << it }
 | 
			
		||||
                    } else {
 | 
			
		||||
                        ZipEntry newEntry = new ZipEntry(entry.name)
 | 
			
		||||
                        newEntry.setTime(entry.time)
 | 
			
		||||
                        outJar.putNextEntry(newEntry)
 | 
			
		||||
 | 
			
		||||
                        def element = inJar.getInputStream(entry).withCloseable { gson.fromJson(it.newReader("UTF8"), JsonElement.class) }
 | 
			
		||||
                        outJar.write(gson.toJson(element).getBytes(StandardCharsets.UTF_8))
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // And replace the original jar again
 | 
			
		||||
        Files.move(tempPath.toPath(), jarPath.toPath(), StandardCopyOption.REPLACE_EXISTING)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
assemble.dependsOn compressJson
 | 
			
		||||
 | 
			
		||||
curseforge {
 | 
			
		||||
    apiKey = project.hasProperty('curseForgeApiKey') ? project.curseForgeApiKey : ''
 | 
			
		||||
    project {
 | 
			
		||||
        id = '282001'
 | 
			
		||||
        releaseType = 'beta'
 | 
			
		||||
        changelog = "Release notes can be found on the GitHub repository (https://github.com/SquidDev-CC/CC-Tweaked/releases/tag/v${project.version})."
 | 
			
		||||
        releaseType = 'release'
 | 
			
		||||
        changelog = "Release notes can be found on the GitHub repository (https://github.com/SquidDev-CC/CC-Tweaked/releases/tag/v${mc_version}-${mod_version})."
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -159,22 +258,22 @@ uploadArchives {
 | 
			
		||||
                pom.project {
 | 
			
		||||
                    name 'CC: Tweaked'
 | 
			
		||||
                    packaging 'jar'
 | 
			
		||||
                    description 'A fork of ComputerCraft which aims to provide earlier access to the more experimental and in-development features of the mod.'
 | 
			
		||||
                    description 'CC: Tweaked is a fork of ComputerCraft, adding programmable computers, turtles and more to Minecraft.'
 | 
			
		||||
                    url 'https://github.com/SquidDev-CC/CC-Tweaked'
 | 
			
		||||
 | 
			
		||||
                    scm {
 | 
			
		||||
                        url 'https://github.com/dan200/ComputerCraft.git'
 | 
			
		||||
                        url 'https://github.com/SquidDev-CC/CC-Tweaked.git'
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    issueManagement {
 | 
			
		||||
                        system 'github'
 | 
			
		||||
                        url 'https://github.com/dan200/ComputerCraft/issues'
 | 
			
		||||
                        url 'https://github.com/SquidDev-CC/CC-Tweaked/issues'
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    licenses {
 | 
			
		||||
                        license {
 | 
			
		||||
                            name 'ComputerCraft Public License, Version 1.0'
 | 
			
		||||
                            url 'https://github.com/dan200/ComputerCraft/blob/master/LICENSE'
 | 
			
		||||
                            url 'https://github.com/SquidDev-CC/CC-Tweaked/blob/master/LICENSE'
 | 
			
		||||
                            distribution 'repo'
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
@@ -188,9 +287,16 @@ uploadArchives {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
test {
 | 
			
		||||
    useJUnitPlatform()
 | 
			
		||||
    testLogging {
 | 
			
		||||
        events "passed", "skipped", "failed"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
gradle.projectsEvaluated {
 | 
			
		||||
    tasks.withType(JavaCompile) {
 | 
			
		||||
        options.compilerArgs << "-Xlint"
 | 
			
		||||
        options.compilerArgs << "-Xlint" << "-Xlint:-processing" << "-Werror"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										2491
									
								
								codeInspectionSettings.xml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										2491
									
								
								codeInspectionSettings.xml
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							
							
								
								
									
										7
									
								
								gradle.properties
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								gradle.properties
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
			
		||||
# Mod properties
 | 
			
		||||
mod_version=1.82.1
 | 
			
		||||
 | 
			
		||||
# Minecraft properties
 | 
			
		||||
mc_version=1.12.2
 | 
			
		||||
forge_version=14.23.4.2749
 | 
			
		||||
mappings_version=snapshot_20180724
 | 
			
		||||
							
								
								
									
										2
									
								
								gradle/wrapper/gradle-wrapper.properties
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								gradle/wrapper/gradle-wrapper.properties
									
									
									
									
										vendored
									
									
								
							@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
 | 
			
		||||
distributionPath=wrapper/dists
 | 
			
		||||
zipStoreBase=GRADLE_USER_HOME
 | 
			
		||||
zipStorePath=wrapper/dists
 | 
			
		||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.3-bin.zip
 | 
			
		||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-bin.zip
 | 
			
		||||
 
 | 
			
		||||
@@ -1 +1 @@
 | 
			
		||||
rootProject.name = 'cc-tweaked'
 | 
			
		||||
rootProject.name = "cc-tweaked-${mc_version}"
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										30
									
								
								src/main/java/dan200/computercraft/CCTweaked.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								src/main/java/dan200/computercraft/CCTweaked.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft;
 | 
			
		||||
 | 
			
		||||
import net.minecraftforge.fml.common.Mod;
 | 
			
		||||
import net.minecraftforge.fml.common.network.NetworkCheckHandler;
 | 
			
		||||
import net.minecraftforge.fml.relauncher.Side;
 | 
			
		||||
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A stub mod for CC: Tweaked. This doesn't have any functionality (everything of note is done in
 | 
			
		||||
 * {@link ComputerCraft}), but people may depend on this if they require CC: Tweaked functionality.
 | 
			
		||||
 */
 | 
			
		||||
@Mod(
 | 
			
		||||
    modid = "cctweaked", name = ComputerCraft.NAME, version = ComputerCraft.VERSION,
 | 
			
		||||
    acceptableRemoteVersions = "*"
 | 
			
		||||
)
 | 
			
		||||
public class CCTweaked
 | 
			
		||||
{
 | 
			
		||||
    @NetworkCheckHandler
 | 
			
		||||
    public boolean onNetworkConnect( Map<String, String> mods, Side side )
 | 
			
		||||
    {
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -24,18 +24,15 @@ import dan200.computercraft.api.turtle.event.TurtleAction;
 | 
			
		||||
import dan200.computercraft.core.apis.AddressPredicate;
 | 
			
		||||
import dan200.computercraft.core.apis.ApiFactories;
 | 
			
		||||
import dan200.computercraft.core.apis.http.websocket.Websocket;
 | 
			
		||||
import dan200.computercraft.core.computer.MainThread;
 | 
			
		||||
import dan200.computercraft.core.filesystem.ComboMount;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileMount;
 | 
			
		||||
import dan200.computercraft.core.filesystem.JarMount;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.core.tracking.Tracking;
 | 
			
		||||
import dan200.computercraft.shared.*;
 | 
			
		||||
import dan200.computercraft.shared.computer.blocks.BlockCommandComputer;
 | 
			
		||||
import dan200.computercraft.shared.computer.blocks.BlockComputer;
 | 
			
		||||
import dan200.computercraft.shared.computer.blocks.TileComputer;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ClientComputerRegistry;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ServerComputer;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ServerComputerRegistry;
 | 
			
		||||
import dan200.computercraft.shared.computer.items.ItemCommandComputer;
 | 
			
		||||
import dan200.computercraft.shared.computer.items.ItemComputer;
 | 
			
		||||
@@ -43,24 +40,19 @@ import dan200.computercraft.shared.media.items.ItemDiskExpanded;
 | 
			
		||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
 | 
			
		||||
import dan200.computercraft.shared.media.items.ItemPrintout;
 | 
			
		||||
import dan200.computercraft.shared.media.items.ItemTreasureDisk;
 | 
			
		||||
import dan200.computercraft.shared.network.NetworkHandler;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.common.BlockPeripheral;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.common.ItemPeripheral;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.diskdrive.TileDiskDrive;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.modem.wired.BlockCable;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.modem.wired.BlockWiredModemFull;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.modem.wired.ItemCable;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.modem.wireless.BlockAdvancedModem;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.modem.wireless.ItemAdvancedModem;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.modem.wireless.WirelessNetwork;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.printer.TilePrinter;
 | 
			
		||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
 | 
			
		||||
import dan200.computercraft.shared.pocket.peripherals.PocketModem;
 | 
			
		||||
import dan200.computercraft.shared.pocket.peripherals.PocketSpeaker;
 | 
			
		||||
import dan200.computercraft.shared.proxy.ICCTurtleProxy;
 | 
			
		||||
import dan200.computercraft.shared.proxy.IComputerCraftProxy;
 | 
			
		||||
import dan200.computercraft.shared.proxy.ComputerCraftProxyCommon;
 | 
			
		||||
import dan200.computercraft.shared.turtle.blocks.BlockTurtle;
 | 
			
		||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
 | 
			
		||||
import dan200.computercraft.shared.turtle.items.ItemTurtleAdvanced;
 | 
			
		||||
import dan200.computercraft.shared.turtle.items.ItemTurtleLegacy;
 | 
			
		||||
import dan200.computercraft.shared.turtle.items.ItemTurtleNormal;
 | 
			
		||||
@@ -76,7 +68,6 @@ import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.server.MinecraftServer;
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import net.minecraft.util.EnumFacing;
 | 
			
		||||
import net.minecraft.util.EnumHand;
 | 
			
		||||
import net.minecraft.util.math.BlockPos;
 | 
			
		||||
import net.minecraft.world.IBlockAccess;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
@@ -95,27 +86,20 @@ import java.net.URL;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.EnumSet;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.zip.ZipEntry;
 | 
			
		||||
import java.util.zip.ZipFile;
 | 
			
		||||
 | 
			
		||||
@Mod(
 | 
			
		||||
    modid = ComputerCraft.MOD_ID, name = "CC: Tweaked", version = "${version}",
 | 
			
		||||
    modid = ComputerCraft.MOD_ID, name = ComputerCraft.NAME, version = ComputerCraft.VERSION,
 | 
			
		||||
    guiFactory = "dan200.computercraft.client.gui.GuiConfigCC$Factory",
 | 
			
		||||
    dependencies = "required:forge@[14.23.4.2746,)"
 | 
			
		||||
)
 | 
			
		||||
public class ComputerCraft
 | 
			
		||||
{
 | 
			
		||||
    public static final String MOD_ID = "computercraft";
 | 
			
		||||
 | 
			
		||||
    // GUI IDs
 | 
			
		||||
    public static final int diskDriveGUIID = 100;
 | 
			
		||||
    public static final int computerGUIID = 101;
 | 
			
		||||
    public static final int printerGUIID = 102;
 | 
			
		||||
    public static final int turtleGUIID = 103;
 | 
			
		||||
    // ComputerCraftEdu uses ID 104
 | 
			
		||||
    public static final int printoutGUIID = 105;
 | 
			
		||||
    public static final int pocketComputerGUIID = 106;
 | 
			
		||||
    public static final int viewComputerGUIID = 110;
 | 
			
		||||
    static final String VERSION = "${version}";
 | 
			
		||||
    static final String NAME = "CC: Tweaked";
 | 
			
		||||
 | 
			
		||||
    // Configuration options
 | 
			
		||||
    public static final String[] DEFAULT_HTTP_WHITELIST = new String[] { "*" };
 | 
			
		||||
@@ -133,9 +117,12 @@ public class ComputerCraft
 | 
			
		||||
    public static boolean disable_lua51_features = false;
 | 
			
		||||
    public static String default_computer_settings = "";
 | 
			
		||||
    public static boolean debug_enable = true;
 | 
			
		||||
    public static int computer_threads = 1;
 | 
			
		||||
    public static boolean logPeripheralErrors = false;
 | 
			
		||||
 | 
			
		||||
    public static int computer_threads = 1;
 | 
			
		||||
    public static long maxMainGlobalTime = TimeUnit.MILLISECONDS.toNanos( 10 );
 | 
			
		||||
    public static long maxMainComputerTime = TimeUnit.MILLISECONDS.toNanos( 5 );
 | 
			
		||||
 | 
			
		||||
    public static boolean http_enable = true;
 | 
			
		||||
    public static boolean http_websocket_enable = true;
 | 
			
		||||
    public static AddressPredicate http_whitelist = new AddressPredicate( DEFAULT_HTTP_WHITELIST );
 | 
			
		||||
@@ -172,7 +159,7 @@ public class ComputerCraft
 | 
			
		||||
    public static final int terminalHeight_pocketComputer = 20;
 | 
			
		||||
 | 
			
		||||
    // Blocks and Items
 | 
			
		||||
    public static class Blocks
 | 
			
		||||
    public static final class Blocks
 | 
			
		||||
    {
 | 
			
		||||
        public static BlockComputer computer;
 | 
			
		||||
        public static BlockCommandComputer commandComputer;
 | 
			
		||||
@@ -187,7 +174,7 @@ public class ComputerCraft
 | 
			
		||||
        public static BlockWiredModemFull wiredModemFull;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class Items
 | 
			
		||||
    public static final class Items
 | 
			
		||||
    {
 | 
			
		||||
        public static ItemComputer computer;
 | 
			
		||||
        public static ItemCommandComputer commandComputer;
 | 
			
		||||
@@ -210,7 +197,7 @@ public class ComputerCraft
 | 
			
		||||
        public static ItemBlock wiredModemFull;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class TurtleUpgrades
 | 
			
		||||
    public static final class TurtleUpgrades
 | 
			
		||||
    {
 | 
			
		||||
        public static TurtleModem wirelessModem;
 | 
			
		||||
        public static TurtleModem advancedModem;
 | 
			
		||||
@@ -224,7 +211,7 @@ public class ComputerCraft
 | 
			
		||||
        public static TurtleHoe diamondHoe;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class PocketUpgrades
 | 
			
		||||
    public static final class PocketUpgrades
 | 
			
		||||
    {
 | 
			
		||||
        public static PocketModem wirelessModem;
 | 
			
		||||
        public static PocketModem advancedModem;
 | 
			
		||||
@@ -235,7 +222,8 @@ public class ComputerCraft
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static class Upgrades {
 | 
			
		||||
    public static final class Upgrades
 | 
			
		||||
    {
 | 
			
		||||
        public static TurtleModem advancedModem;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -253,20 +241,14 @@ public class ComputerCraft
 | 
			
		||||
    public static List<IPeripheralProvider> peripheralProviders = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    // Implementation
 | 
			
		||||
    @Mod.Instance( value = ComputerCraft.MOD_ID )
 | 
			
		||||
    @Mod.Instance( ComputerCraft.MOD_ID )
 | 
			
		||||
    public static ComputerCraft instance;
 | 
			
		||||
 | 
			
		||||
    @SidedProxy(
 | 
			
		||||
        clientSide = "dan200.computercraft.client.proxy.ComputerCraftProxyClient",
 | 
			
		||||
        serverSide = "dan200.computercraft.shared.proxy.ComputerCraftProxyCommon"
 | 
			
		||||
    )
 | 
			
		||||
    private static IComputerCraftProxy proxy;
 | 
			
		||||
 | 
			
		||||
    @SidedProxy(
 | 
			
		||||
        clientSide = "dan200.computercraft.client.proxy.CCTurtleProxyClient",
 | 
			
		||||
        serverSide = "dan200.computercraft.shared.proxy.CCTurtleProxyCommon"
 | 
			
		||||
    )
 | 
			
		||||
    private static ICCTurtleProxy turtleProxy;
 | 
			
		||||
    private static ComputerCraftProxyCommon proxy;
 | 
			
		||||
 | 
			
		||||
    @Mod.EventHandler
 | 
			
		||||
    public void preInit( FMLPreInitializationEvent event )
 | 
			
		||||
@@ -276,24 +258,19 @@ public class ComputerCraft
 | 
			
		||||
        // Load config
 | 
			
		||||
        Config.load( event.getSuggestedConfigurationFile() );
 | 
			
		||||
 | 
			
		||||
        // Setup network
 | 
			
		||||
        NetworkHandler.setup();
 | 
			
		||||
 | 
			
		||||
        proxy.preInit();
 | 
			
		||||
        turtleProxy.preInit();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Mod.EventHandler
 | 
			
		||||
    public void init( FMLInitializationEvent event )
 | 
			
		||||
    {
 | 
			
		||||
        proxy.init();
 | 
			
		||||
        turtleProxy.init();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Mod.EventHandler
 | 
			
		||||
    public void onServerStarting( FMLServerStartingEvent event )
 | 
			
		||||
    {
 | 
			
		||||
        proxy.initServer( event.getServer() );
 | 
			
		||||
        ComputerCraftProxyCommon.initServer( event.getServer() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Mod.EventHandler
 | 
			
		||||
@@ -303,6 +280,7 @@ public class ComputerCraft
 | 
			
		||||
        {
 | 
			
		||||
            ComputerCraft.serverComputerRegistry.reset();
 | 
			
		||||
            WirelessNetwork.resetNetworks();
 | 
			
		||||
            MainThread.reset();
 | 
			
		||||
            Tracking.reset();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -314,65 +292,14 @@ public class ComputerCraft
 | 
			
		||||
        {
 | 
			
		||||
            ComputerCraft.serverComputerRegistry.reset();
 | 
			
		||||
            WirelessNetwork.resetNetworks();
 | 
			
		||||
            MainThread.reset();
 | 
			
		||||
            Tracking.reset();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static String getVersion()
 | 
			
		||||
    {
 | 
			
		||||
        return "${version}";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void openDiskDriveGUI( EntityPlayer player, TileDiskDrive drive )
 | 
			
		||||
    {
 | 
			
		||||
        BlockPos pos = drive.getPos();
 | 
			
		||||
        player.openGui( ComputerCraft.instance, ComputerCraft.diskDriveGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void openComputerGUI( EntityPlayer player, TileComputer computer )
 | 
			
		||||
    {
 | 
			
		||||
        BlockPos pos = computer.getPos();
 | 
			
		||||
        player.openGui( ComputerCraft.instance, ComputerCraft.computerGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void openPrinterGUI( EntityPlayer player, TilePrinter printer )
 | 
			
		||||
    {
 | 
			
		||||
        BlockPos pos = printer.getPos();
 | 
			
		||||
        player.openGui( ComputerCraft.instance, ComputerCraft.printerGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void openTurtleGUI( EntityPlayer player, TileTurtle turtle )
 | 
			
		||||
    {
 | 
			
		||||
        BlockPos pos = turtle.getPos();
 | 
			
		||||
        player.openGui( instance, ComputerCraft.turtleGUIID, player.getEntityWorld(), pos.getX(), pos.getY(), pos.getZ() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void openPrintoutGUI( EntityPlayer player, EnumHand hand )
 | 
			
		||||
    {
 | 
			
		||||
        player.openGui( ComputerCraft.instance, ComputerCraft.printoutGUIID, player.getEntityWorld(), hand.ordinal(), 0, 0 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void openPocketComputerGUI( EntityPlayer player, EnumHand hand )
 | 
			
		||||
    {
 | 
			
		||||
        player.openGui( ComputerCraft.instance, ComputerCraft.pocketComputerGUIID, player.getEntityWorld(), hand.ordinal(), 0, 0 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void openComputerGUI( EntityPlayer player, ServerComputer computer )
 | 
			
		||||
    {
 | 
			
		||||
        ComputerFamily family = computer.getFamily();
 | 
			
		||||
        int width = 0, height = 0;
 | 
			
		||||
        Terminal terminal = computer.getTerminal();
 | 
			
		||||
        if( terminal != null )
 | 
			
		||||
        {
 | 
			
		||||
            width = terminal.getWidth();
 | 
			
		||||
            height = terminal.getHeight();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Pack useful terminal information into the various coordinate bits.
 | 
			
		||||
        // These are extracted in ComputerCraftProxyCommon.getClientGuiElement
 | 
			
		||||
        player.openGui( ComputerCraft.instance, ComputerCraft.viewComputerGUIID, player.getEntityWorld(),
 | 
			
		||||
            computer.getInstanceID(), family.ordinal(), (width & 0xFFFF) << 16 | (height & 0xFFFF)
 | 
			
		||||
        );
 | 
			
		||||
        return VERSION;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static File getBaseDir()
 | 
			
		||||
@@ -385,16 +312,6 @@ public class ComputerCraft
 | 
			
		||||
        return new File( getBaseDir(), "resourcepacks" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static boolean canPlayerUseCommands( EntityPlayer player )
 | 
			
		||||
    {
 | 
			
		||||
        MinecraftServer server = player.getServer();
 | 
			
		||||
        if( server != null )
 | 
			
		||||
        {
 | 
			
		||||
            return server.getPlayerList().canSendCommands( player.getGameProfile() );
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static void registerPermissionProvider( ITurtlePermissionProvider provider )
 | 
			
		||||
    {
 | 
			
		||||
@@ -641,7 +558,7 @@ public class ComputerCraft
 | 
			
		||||
    private static File getContainingJar( Class<?> modClass )
 | 
			
		||||
    {
 | 
			
		||||
        String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
 | 
			
		||||
        int bangIndex = path.indexOf( "!" );
 | 
			
		||||
        int bangIndex = path.indexOf( '!' );
 | 
			
		||||
        if( bangIndex >= 0 )
 | 
			
		||||
        {
 | 
			
		||||
            path = path.substring( 0, bangIndex );
 | 
			
		||||
@@ -672,7 +589,7 @@ public class ComputerCraft
 | 
			
		||||
    private static File getDebugCodeDir( Class<?> modClass )
 | 
			
		||||
    {
 | 
			
		||||
        String path = modClass.getProtectionDomain().getCodeSource().getLocation().getPath();
 | 
			
		||||
        int bangIndex = path.indexOf( "!" );
 | 
			
		||||
        int bangIndex = path.indexOf( '!' );
 | 
			
		||||
        return bangIndex >= 0 ? null : new File( new File( path ).getParentFile(), "../.." );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -729,5 +646,12 @@ public class ComputerCraft
 | 
			
		||||
    {
 | 
			
		||||
        return Peripherals.getPeripheral( world, pos, side );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static boolean canPlayerUseCommands( EntityPlayer player )
 | 
			
		||||
    {
 | 
			
		||||
        MinecraftServer server = player.getServer();
 | 
			
		||||
        return server != null && server.getPlayerList().canSendCommands( player.getGameProfile() );
 | 
			
		||||
    }
 | 
			
		||||
    //endregion
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -4,7 +4,7 @@
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.shared.turtle.upgrades;
 | 
			
		||||
package dan200.computercraft.api;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
 | 
			
		||||
import dan200.computercraft.api.turtle.TurtleUpgradeType;
 | 
			
		||||
@@ -15,6 +15,11 @@ import net.minecraft.util.ResourceLocation;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A base class for {@link ITurtleUpgrade}s.
 | 
			
		||||
 *
 | 
			
		||||
 * One does not have to use this, but it does provide a convenient template.
 | 
			
		||||
 */
 | 
			
		||||
public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade
 | 
			
		||||
{
 | 
			
		||||
    private final ResourceLocation id;
 | 
			
		||||
@@ -23,7 +28,7 @@ public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade
 | 
			
		||||
    private final String adjective;
 | 
			
		||||
    private final ItemStack stack;
 | 
			
		||||
 | 
			
		||||
    public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, ItemStack stack )
 | 
			
		||||
    protected AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, ItemStack stack )
 | 
			
		||||
    {
 | 
			
		||||
        this.id = id;
 | 
			
		||||
        this.legacyId = legacyId;
 | 
			
		||||
@@ -32,27 +37,27 @@ public abstract class AbstractTurtleUpgrade implements ITurtleUpgrade
 | 
			
		||||
        this.stack = stack;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Item item )
 | 
			
		||||
    protected AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Item item )
 | 
			
		||||
    {
 | 
			
		||||
        this( id, legacyId, type, adjective, new ItemStack( item ) );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Block block )
 | 
			
		||||
    protected AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, String adjective, Block block )
 | 
			
		||||
    {
 | 
			
		||||
        this( id, legacyId, type, adjective, new ItemStack( block ) );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, ItemStack stack )
 | 
			
		||||
    protected AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, ItemStack stack )
 | 
			
		||||
    {
 | 
			
		||||
        this( id, legacyId, type, "upgrade." + id + ".adjective", stack );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, Item item )
 | 
			
		||||
    protected AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, Item item )
 | 
			
		||||
    {
 | 
			
		||||
        this( id, legacyId, type, new ItemStack( item ) );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, Block block )
 | 
			
		||||
    protected AbstractTurtleUpgrade( ResourceLocation id, int legacyId, TurtleUpgradeType type, Block block )
 | 
			
		||||
    {
 | 
			
		||||
        this( id, legacyId, type, new ItemStack( block ) );
 | 
			
		||||
    }
 | 
			
		||||
@@ -173,8 +173,8 @@ public final class ComputerCraftAPI
 | 
			
		||||
     * Registers a peripheral provider to convert blocks into {@link IPeripheral} implementations.
 | 
			
		||||
     *
 | 
			
		||||
     * @param provider The peripheral provider to register.
 | 
			
		||||
     * @see dan200.computercraft.api.peripheral.IPeripheral
 | 
			
		||||
     * @see dan200.computercraft.api.peripheral.IPeripheralProvider
 | 
			
		||||
     * @see IPeripheral
 | 
			
		||||
     * @see IPeripheralProvider
 | 
			
		||||
     */
 | 
			
		||||
    public static void registerPeripheralProvider( @Nonnull IPeripheralProvider provider )
 | 
			
		||||
    {
 | 
			
		||||
@@ -198,7 +198,7 @@ public final class ComputerCraftAPI
 | 
			
		||||
     * this during the load() method of your mod.
 | 
			
		||||
     *
 | 
			
		||||
     * @param upgrade The turtle upgrade to register.
 | 
			
		||||
     * @see dan200.computercraft.api.turtle.ITurtleUpgrade
 | 
			
		||||
     * @see ITurtleUpgrade
 | 
			
		||||
     */
 | 
			
		||||
    public static void registerTurtleUpgrade( @Nonnull ITurtleUpgrade upgrade )
 | 
			
		||||
    {
 | 
			
		||||
@@ -223,7 +223,7 @@ public final class ComputerCraftAPI
 | 
			
		||||
     * Registers a bundled redstone provider to provide bundled redstone output for blocks.
 | 
			
		||||
     *
 | 
			
		||||
     * @param provider The bundled redstone provider to register.
 | 
			
		||||
     * @see dan200.computercraft.api.redstone.IBundledRedstoneProvider
 | 
			
		||||
     * @see IBundledRedstoneProvider
 | 
			
		||||
     */
 | 
			
		||||
    public static void registerBundledRedstoneProvider( @Nonnull IBundledRedstoneProvider provider )
 | 
			
		||||
    {
 | 
			
		||||
@@ -249,7 +249,7 @@ public final class ComputerCraftAPI
 | 
			
		||||
     * @param side  The side to extract the bundled redstone output from.
 | 
			
		||||
     * @return If there is a block capable of emitting bundled redstone at the location, it's signal (0-65535) will be returned.
 | 
			
		||||
     * If there is no block capable of emitting bundled redstone at the location, -1 will be returned.
 | 
			
		||||
     * @see dan200.computercraft.api.redstone.IBundledRedstoneProvider
 | 
			
		||||
     * @see IBundledRedstoneProvider
 | 
			
		||||
     */
 | 
			
		||||
    public static int getBundledRedstoneOutput( @Nonnull World world, @Nonnull BlockPos pos, @Nonnull EnumFacing side )
 | 
			
		||||
    {
 | 
			
		||||
@@ -272,7 +272,7 @@ public final class ComputerCraftAPI
 | 
			
		||||
     * Registers a media provider to provide {@link IMedia} implementations for Items
 | 
			
		||||
     *
 | 
			
		||||
     * @param provider The media provider to register.
 | 
			
		||||
     * @see dan200.computercraft.api.media.IMediaProvider
 | 
			
		||||
     * @see IMediaProvider
 | 
			
		||||
     */
 | 
			
		||||
    public static void registerMediaProvider( @Nonnull IMediaProvider provider )
 | 
			
		||||
    {
 | 
			
		||||
@@ -294,7 +294,7 @@ public final class ComputerCraftAPI
 | 
			
		||||
     * Registers a permission provider to restrict where turtles can move or build.
 | 
			
		||||
     *
 | 
			
		||||
     * @param provider The turtle permission provider to register.
 | 
			
		||||
     * @see dan200.computercraft.api.permissions.ITurtlePermissionProvider
 | 
			
		||||
     * @see ITurtlePermissionProvider
 | 
			
		||||
     * @deprecated Prefer using {@link dan200.computercraft.api.turtle.event.TurtleBlockEvent} or the standard Forge events.
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
@@ -481,7 +481,7 @@ public final class ComputerCraftAPI
 | 
			
		||||
            }
 | 
			
		||||
            catch( Exception e )
 | 
			
		||||
            {
 | 
			
		||||
                System.out.println( "ComputerCraftAPI: ComputerCraft not found." );
 | 
			
		||||
                System.err.println( "ComputerCraftAPI: ComputerCraft not found." );
 | 
			
		||||
            }
 | 
			
		||||
            finally
 | 
			
		||||
            {
 | 
			
		||||
@@ -498,7 +498,7 @@ public final class ComputerCraftAPI
 | 
			
		||||
        }
 | 
			
		||||
        catch( NoSuchMethodException e )
 | 
			
		||||
        {
 | 
			
		||||
            System.out.println( "ComputerCraftAPI: ComputerCraft method " + name + " not found." );
 | 
			
		||||
            System.err.println( "ComputerCraftAPI: ComputerCraft method " + name + " not found." );
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -17,6 +17,7 @@ import javax.annotation.Nullable;
 | 
			
		||||
 * @see ILuaAPI
 | 
			
		||||
 * @see ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory)
 | 
			
		||||
 */
 | 
			
		||||
@FunctionalInterface
 | 
			
		||||
public interface ILuaAPIFactory
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
 
 | 
			
		||||
@@ -32,7 +32,12 @@ public interface ILuaContext
 | 
			
		||||
     *                              intercepted, or the computer will leak memory and end up in a broken state.
 | 
			
		||||
     */
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    Object[] pullEvent( @Nullable String filter ) throws LuaException, InterruptedException;
 | 
			
		||||
    default Object[] pullEvent( @Nullable String filter ) throws LuaException, InterruptedException
 | 
			
		||||
    {
 | 
			
		||||
        Object[] results = pullEventRaw( filter );
 | 
			
		||||
        if( results.length >= 1 && results[0].equals( "terminate" ) ) throw new LuaException( "Terminated", 0 );
 | 
			
		||||
        return results;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The same as {@link #pullEvent(String)}, except "terminated" events are ignored. Only use this if you want to
 | 
			
		||||
@@ -47,7 +52,10 @@ public interface ILuaContext
 | 
			
		||||
     * @see #pullEvent(String)
 | 
			
		||||
     */
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    Object[] pullEventRaw( @Nullable String filter ) throws InterruptedException;
 | 
			
		||||
    default Object[] pullEventRaw( @Nullable String filter ) throws InterruptedException
 | 
			
		||||
    {
 | 
			
		||||
        return yield( new Object[] { filter } );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Yield the current coroutine with some arguments until it is resumed. This method is exactly equivalent to
 | 
			
		||||
 
 | 
			
		||||
@@ -7,6 +7,7 @@
 | 
			
		||||
package dan200.computercraft.api.media;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.filesystem.IMount;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.util.SoundEvent;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
@@ -16,7 +17,9 @@ import javax.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents an item that can be placed in a disk drive and used by a Computer.
 | 
			
		||||
 * Implement this interface on your Item class to allow it to be used in the drive.
 | 
			
		||||
 *
 | 
			
		||||
 * Implement this interface on your {@link Item} class to allow it to be used in the drive. Alternatively, register
 | 
			
		||||
 * a {@link IMediaProvider}.
 | 
			
		||||
 */
 | 
			
		||||
public interface IMedia
 | 
			
		||||
{
 | 
			
		||||
@@ -43,7 +46,7 @@ public interface IMedia
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * If this disk represents an item with audio (like a record), get the readable name of the audio track. ie:
 | 
			
		||||
     * "Jonathon Coulton - Still Alive"
 | 
			
		||||
     * "Jonathan Coulton - Still Alive"
 | 
			
		||||
     *
 | 
			
		||||
     * @param stack The {@link ItemStack} to modify.
 | 
			
		||||
     * @return The name, or null if this item does not represent an item with audio.
 | 
			
		||||
@@ -74,7 +77,7 @@ public interface IMedia
 | 
			
		||||
     * @param world The world in which the item and disk drive reside.
 | 
			
		||||
     * @return The mount, or null if this item does not represent an item with data. If the mount returned also
 | 
			
		||||
     * implements {@link dan200.computercraft.api.filesystem.IWritableMount}, it will mounted using mountWritable()
 | 
			
		||||
     * @see dan200.computercraft.api.filesystem.IMount
 | 
			
		||||
     * @see IMount
 | 
			
		||||
     * @see dan200.computercraft.api.filesystem.IWritableMount
 | 
			
		||||
     * @see dan200.computercraft.api.ComputerCraftAPI#createSaveDirMount(World, String, long)
 | 
			
		||||
     * @see dan200.computercraft.api.ComputerCraftAPI#createResourceMount(Class, String, String)
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@ public interface IMediaProvider
 | 
			
		||||
     * Produce an IMedia implementation from an ItemStack.
 | 
			
		||||
     *
 | 
			
		||||
     * @param stack The stack from which to extract the media information.
 | 
			
		||||
     * @return An IMedia implementation, or null if the item is not something you wish to handle
 | 
			
		||||
     * @return An {@link IMedia} implementation, or {@code null} if the item is not something you wish to handle
 | 
			
		||||
     * @see dan200.computercraft.api.ComputerCraftAPI#registerMediaProvider(IMediaProvider)
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
 
 | 
			
		||||
@@ -9,6 +9,8 @@ package dan200.computercraft.api.peripheral;
 | 
			
		||||
import dan200.computercraft.api.ComputerCraftAPI;
 | 
			
		||||
import dan200.computercraft.api.filesystem.IMount;
 | 
			
		||||
import dan200.computercraft.api.filesystem.IWritableMount;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaContext;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaTask;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
@@ -146,7 +148,7 @@ public interface IComputerAccess
 | 
			
		||||
     *
 | 
			
		||||
     *                  You may supply {@code null} to indicate that no arguments are to be supplied.
 | 
			
		||||
     * @throws RuntimeException If the peripheral has been detached.
 | 
			
		||||
     * @see dan200.computercraft.api.peripheral.IPeripheral#callMethod
 | 
			
		||||
     * @see IPeripheral#callMethod
 | 
			
		||||
     */
 | 
			
		||||
    void queueEvent( @Nonnull String event, @Nullable Object[] arguments );
 | 
			
		||||
 | 
			
		||||
@@ -179,8 +181,8 @@ public interface IComputerAccess
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get a reachable peripheral with the given attachement name. This is a equivalent to
 | 
			
		||||
     * {@link #getAvailablePeripherals()}{@code .get(name)}, though may be more performant.
 | 
			
		||||
     * Get a reachable peripheral with the given attachment name. This is a equivalent to
 | 
			
		||||
     * {@link #getAvailablePeripherals()}{@code .get(name)}, though may be more efficient.
 | 
			
		||||
     *
 | 
			
		||||
     * @param name The peripheral's attached name
 | 
			
		||||
     * @return The reachable peripheral, or {@code null} if none can be found.
 | 
			
		||||
@@ -191,4 +193,23 @@ public interface IComputerAccess
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get a {@link IWorkMonitor} for tasks your peripheral might execute on the main (server) thread.
 | 
			
		||||
     *
 | 
			
		||||
     * This should be used to ensure your peripheral integrates with ComputerCraft's monitoring and limiting of how much
 | 
			
		||||
     * server time each computer consumes. You should not need to use this if you use
 | 
			
		||||
     * {@link ILuaContext#issueMainThreadTask(ILuaTask)} - this is intended for mods with their own system for running
 | 
			
		||||
     * work on the main thread.
 | 
			
		||||
     *
 | 
			
		||||
     * Please note that the returned implementation is <em>not</em> thread-safe, and should only be used from the main
 | 
			
		||||
     * thread.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The work monitor for the main thread, or {@code null} if this computer does not have one.
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    default IWorkMonitor getMainThreadMonitor()
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -41,8 +41,8 @@ public interface IPeripheral
 | 
			
		||||
     * This is called when a lua program on an attached computer calls {@code peripheral.call()} with
 | 
			
		||||
     * one of the methods exposed by {@link #getMethodNames()}.
 | 
			
		||||
     *
 | 
			
		||||
     * Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
 | 
			
		||||
     * when interacting with Minecraft objects.
 | 
			
		||||
     * Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe when interacting
 | 
			
		||||
     * with Minecraft objects.
 | 
			
		||||
     *
 | 
			
		||||
     * @param computer  The interface to the computer that is making the call. Remember that multiple
 | 
			
		||||
     *                  computers can be attached to a peripheral at once.
 | 
			
		||||
@@ -75,20 +75,21 @@ public interface IPeripheral
 | 
			
		||||
    Object[] callMethod( @Nonnull IComputerAccess computer, @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Is called when canAttachToSide has returned true, and a computer is attaching to the peripheral.
 | 
			
		||||
     * Is called when when a computer is attaching to the peripheral.
 | 
			
		||||
     *
 | 
			
		||||
     * This will occur when a peripheral is placed next to an active computer, when a computer is turned on next to a
 | 
			
		||||
     * peripheral, or when a turtle travels into a square next to a peripheral.
 | 
			
		||||
     * peripheral, when a turtle travels into a square next to a peripheral, or when a wired modem adjacent to this
 | 
			
		||||
     * peripheral is does any of the above.
 | 
			
		||||
     *
 | 
			
		||||
     * Between calls to attach() and detach(), the attached computer can make method calls on the peripheral using
 | 
			
		||||
     * Between calls to attach and {@link #detach}, the attached computer can make method calls on the peripheral using
 | 
			
		||||
     * {@code peripheral.call()}. This method can be used to keep track of which computers are attached to the
 | 
			
		||||
     * peripheral, or to take action when attachment occurs.
 | 
			
		||||
     *
 | 
			
		||||
     * Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
 | 
			
		||||
     * when interacting with Minecraft objects.
 | 
			
		||||
     * Be aware that will be called from both the server thread and ComputerCraft Lua thread, and so must be thread-safe
 | 
			
		||||
     * and reentrant.
 | 
			
		||||
     *
 | 
			
		||||
     * @param computer The interface to the computer that is being attached. Remember that multiple
 | 
			
		||||
     *                 computers can be attached to a peripheral at once.
 | 
			
		||||
     * @param computer The interface to the computer that is being attached. Remember that multiple computers can be
 | 
			
		||||
     *                 attached to a peripheral at once.
 | 
			
		||||
     * @see #detach
 | 
			
		||||
     */
 | 
			
		||||
    default void attach( @Nonnull IComputerAccess computer )
 | 
			
		||||
@@ -96,19 +97,21 @@ public interface IPeripheral
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Is called when a computer is detaching from the peripheral.
 | 
			
		||||
     * Called when a computer is detaching from the peripheral.
 | 
			
		||||
     *
 | 
			
		||||
     * This will occur when a computer shuts down, when the peripheral is removed while attached to computers,
 | 
			
		||||
     * or when a turtle moves away from a square attached to a peripheral. This method can be used to keep track of
 | 
			
		||||
     * which computers are attached to the peripheral, or to take action when detachment
 | 
			
		||||
     * occurs.
 | 
			
		||||
     * This will occur when a computer shuts down, when the peripheral is removed while attached to computers, when a
 | 
			
		||||
     * turtle moves away from a block attached to a peripheral, or when a wired modem adjacent to this peripheral is
 | 
			
		||||
     * detached.
 | 
			
		||||
     *
 | 
			
		||||
     * Be aware that this will be called from the ComputerCraft Lua thread, and must be thread-safe
 | 
			
		||||
     * when interacting with Minecraft objects.
 | 
			
		||||
     * This method can be used to keep track of which computers are attached to the peripheral, or to take action when
 | 
			
		||||
     * detachment occurs.
 | 
			
		||||
     *
 | 
			
		||||
     * @param computer The interface to the computer that is being detached. Remember that multiple
 | 
			
		||||
     *                 computers can be attached to a peripheral at once.
 | 
			
		||||
     * @see #detach
 | 
			
		||||
     * Be aware that this will be called from both the server and ComputerCraft Lua thread, and must be thread-safe
 | 
			
		||||
     * and reentrant.
 | 
			
		||||
     *
 | 
			
		||||
     * @param computer The interface to the computer that is being detached. Remember that multiple computers can be
 | 
			
		||||
     *                 attached to a peripheral at once.
 | 
			
		||||
     * @see #attach
 | 
			
		||||
     */
 | 
			
		||||
    default void detach( @Nonnull IComputerAccess computer )
 | 
			
		||||
    {
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.api.peripheral;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
import net.minecraft.util.EnumFacing;
 | 
			
		||||
import net.minecraft.util.math.BlockPos;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
@@ -16,6 +17,8 @@ import javax.annotation.Nullable;
 | 
			
		||||
/**
 | 
			
		||||
 * This interface is used to create peripheral implementations for blocks.
 | 
			
		||||
 *
 | 
			
		||||
 * If you have a {@link TileEntity} which acts as a peripheral, you may alternatively implement {@link IPeripheralTile}.
 | 
			
		||||
 *
 | 
			
		||||
 * @see dan200.computercraft.api.ComputerCraftAPI#registerPeripheralProvider(IPeripheralProvider)
 | 
			
		||||
 */
 | 
			
		||||
@FunctionalInterface
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,32 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of the public ComputerCraft API - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. This API may be redistributed unmodified and in full only.
 | 
			
		||||
 * For help using the API, and posting your mods, visit the forums at computercraft.info.
 | 
			
		||||
 */
 | 
			
		||||
package dan200.computercraft.api.peripheral;
 | 
			
		||||
 | 
			
		||||
import net.minecraft.util.EnumFacing;
 | 
			
		||||
import net.minecraft.util.math.BlockPos;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A {@link net.minecraft.tileentity.TileEntity} which may act as a peripheral.
 | 
			
		||||
 *
 | 
			
		||||
 * If you need more complex capabilities (such as handling TEs not belonging to your mod), you should use
 | 
			
		||||
 * {@link IPeripheralProvider}.
 | 
			
		||||
 */
 | 
			
		||||
public interface IPeripheralTile
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the peripheral on the given {@code side}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param side The side to get the peripheral from.
 | 
			
		||||
     * @return A peripheral, or {@code null} if there is not a peripheral here.
 | 
			
		||||
     * @see IPeripheralProvider#getPeripheral(World, BlockPos, EnumFacing)
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    IPeripheral getPeripheral( @Nonnull EnumFacing side );
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,78 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.api.peripheral;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Monitors "work" associated with a computer, keeping track of how much a computer has done, and ensuring every
 | 
			
		||||
 * computer receives a fair share of any processing time.
 | 
			
		||||
 *
 | 
			
		||||
 * This is primarily intended for work done by peripherals on the main thread (such as on a tile entity's tick), but
 | 
			
		||||
 * could be used for other purposes (such as complex computations done on another thread).
 | 
			
		||||
 *
 | 
			
		||||
 * Before running a task, one should call {@link #canWork()} to determine if the computer is currently allowed to
 | 
			
		||||
 * execute work. If that returns true, you should execute the task and use {@link #trackWork(long, TimeUnit)} to inform
 | 
			
		||||
 * the monitor how long that task took.
 | 
			
		||||
 *
 | 
			
		||||
 * Alternatively, use {@link #runWork(Runnable)} to run and keep track of work.
 | 
			
		||||
 *
 | 
			
		||||
 * @see IComputerAccess#getMainThreadMonitor()
 | 
			
		||||
 */
 | 
			
		||||
public interface IWorkMonitor
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * If the owning computer is currently allowed to execute work.
 | 
			
		||||
     *
 | 
			
		||||
     * @return If we can execute work right now.
 | 
			
		||||
     */
 | 
			
		||||
    boolean canWork();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * If the owning computer is currently allowed to execute work, and has ample time to do so.
 | 
			
		||||
     *
 | 
			
		||||
     * This is effectively a more restrictive form of {@link #canWork()}. One should use that in order to determine if
 | 
			
		||||
     * you may do an initial piece of work, and shouldWork to determine if any additional task may be performed.
 | 
			
		||||
     *
 | 
			
		||||
     * @return If we should execute work right now.
 | 
			
		||||
     */
 | 
			
		||||
    boolean shouldWork();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Inform the monitor how long some piece of work took to execute.
 | 
			
		||||
     *
 | 
			
		||||
     * @param time The time some task took to run
 | 
			
		||||
     * @param unit The unit that {@code time} was measured in.
 | 
			
		||||
     */
 | 
			
		||||
    void trackWork( long time, @Nonnull TimeUnit unit );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Run a task if possible, and inform the monitor of how long it took.
 | 
			
		||||
     *
 | 
			
		||||
     * @param runnable The task to run.
 | 
			
		||||
     * @return If the task was actually run (namely, {@link #canWork()} returned {@code true}).
 | 
			
		||||
     */
 | 
			
		||||
    default boolean runWork( @Nonnull Runnable runnable )
 | 
			
		||||
    {
 | 
			
		||||
        Objects.requireNonNull( runnable, "runnable should not be null" );
 | 
			
		||||
        if( !canWork() ) return false;
 | 
			
		||||
 | 
			
		||||
        long start = System.nanoTime();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            runnable.run();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            trackWork( System.nanoTime() - start, TimeUnit.NANOSECONDS );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -4,14 +4,18 @@
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.shared.pocket.peripherals;
 | 
			
		||||
package dan200.computercraft.api.pocket;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.pocket.IPocketUpgrade;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.util.ResourceLocation;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A base class for {@link IPocketUpgrade}s.
 | 
			
		||||
 *
 | 
			
		||||
 * One does not have to use this, but it does provide a convenient template.
 | 
			
		||||
 */
 | 
			
		||||
public abstract class AbstractPocketUpgrade implements IPocketUpgrade
 | 
			
		||||
{
 | 
			
		||||
    private final ResourceLocation id;
 | 
			
		||||
@@ -24,10 +24,22 @@ public interface IPocketAccess
 | 
			
		||||
     * Gets the entity holding this item.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The holding entity. This may be {@code null}.
 | 
			
		||||
     * @deprecated Use {@link #getValidEntity()} where possible.
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    Entity getEntity();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Gets the entity holding this item with additional safety checks.
 | 
			
		||||
     *
 | 
			
		||||
     * This must be called on the server thread.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The holding entity, or {@code null} if none exists.
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    Entity getValidEntity();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the colour of this pocket computer as a RGB number.
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ import javax.annotation.Nullable;
 | 
			
		||||
/**
 | 
			
		||||
 * Additional peripherals for pocket computers.
 | 
			
		||||
 *
 | 
			
		||||
 * This is similar to {@link dan200.computercraft.api.turtle.ITurtleUpgrade}.
 | 
			
		||||
 * This is similar to {@link ITurtleUpgrade}.
 | 
			
		||||
 */
 | 
			
		||||
public interface IPocketUpgrade
 | 
			
		||||
{
 | 
			
		||||
@@ -54,6 +54,9 @@ public interface IPocketUpgrade
 | 
			
		||||
     * pocket computer which holds this upgrade. This item stack is also used to determine the upgrade given by
 | 
			
		||||
     * {@code pocket.equip()}/{@code pocket.unequip()}.
 | 
			
		||||
     *
 | 
			
		||||
     * Ideally this should be constant over a session. It is recommended that you cache
 | 
			
		||||
     * the item too, in order to prevent constructing it every time the method is called.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The item stack used for crafting. This can be {@link ItemStack#EMPTY} if crafting is disabled.
 | 
			
		||||
     */
 | 
			
		||||
    @Nonnull
 | 
			
		||||
 
 | 
			
		||||
@@ -79,6 +79,9 @@ public interface ITurtleUpgrade
 | 
			
		||||
     * with to create a turtle which holds this upgrade. This item stack is also used
 | 
			
		||||
     * to determine the upgrade given by {@code turtle.equip()}
 | 
			
		||||
     *
 | 
			
		||||
     * Ideally this should be constant over a session. It is recommended that you cache
 | 
			
		||||
     * the item too, in order to prevent constructing it every time the method is called.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The item stack to craft with, or {@link ItemStack#EMPTY} if it cannot be crafted.
 | 
			
		||||
     */
 | 
			
		||||
    @Nonnull
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,6 @@ import net.minecraft.util.math.BlockPos;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
import net.minecraftforge.common.util.FakePlayer;
 | 
			
		||||
import net.minecraftforge.event.world.BlockEvent;
 | 
			
		||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
@@ -37,7 +36,6 @@ import java.util.Objects;
 | 
			
		||||
 * Be aware that some events (such as {@link TurtleInventoryEvent}) do not necessarily interact
 | 
			
		||||
 * with a block, simply objects within that block space.
 | 
			
		||||
 */
 | 
			
		||||
@Cancelable
 | 
			
		||||
public abstract class TurtleBlockEvent extends TurtlePlayerEvent
 | 
			
		||||
{
 | 
			
		||||
    private final World world;
 | 
			
		||||
@@ -84,7 +82,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
 | 
			
		||||
     *
 | 
			
		||||
     * @see TurtleAction#DIG
 | 
			
		||||
     */
 | 
			
		||||
    @Cancelable
 | 
			
		||||
    public static class Dig extends TurtleBlockEvent
 | 
			
		||||
    {
 | 
			
		||||
        private final IBlockState block;
 | 
			
		||||
@@ -142,7 +139,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
 | 
			
		||||
     *
 | 
			
		||||
     * @see TurtleAction#MOVE
 | 
			
		||||
     */
 | 
			
		||||
    @Cancelable
 | 
			
		||||
    public static class Move extends TurtleBlockEvent
 | 
			
		||||
    {
 | 
			
		||||
        public Move( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos )
 | 
			
		||||
@@ -156,7 +152,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
 | 
			
		||||
     *
 | 
			
		||||
     * @see TurtleAction#PLACE
 | 
			
		||||
     */
 | 
			
		||||
    @Cancelable
 | 
			
		||||
    public static class Place extends TurtleBlockEvent
 | 
			
		||||
    {
 | 
			
		||||
        private final ItemStack stack;
 | 
			
		||||
@@ -188,7 +183,6 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
 | 
			
		||||
     *
 | 
			
		||||
     * @see TurtleAction#INSPECT
 | 
			
		||||
     */
 | 
			
		||||
    @Cancelable
 | 
			
		||||
    public static class Inspect extends TurtleBlockEvent
 | 
			
		||||
    {
 | 
			
		||||
        private final IBlockState state;
 | 
			
		||||
@@ -229,7 +223,7 @@ public abstract class TurtleBlockEvent extends TurtlePlayerEvent
 | 
			
		||||
        /**
 | 
			
		||||
         * Add new information to the inspection result. Note this will override fields with the same name.
 | 
			
		||||
         *
 | 
			
		||||
         * @param newData The data to add. Note all values should be convertable to Lua (see
 | 
			
		||||
         * @param newData The data to add. Note all values should be convertible to Lua (see
 | 
			
		||||
         *                {@link dan200.computercraft.api.peripheral.IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}).
 | 
			
		||||
         */
 | 
			
		||||
        public void addData( @Nonnull Map<String, ?> newData )
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,74 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.api.turtle.event;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaContext;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IComputerAccess;
 | 
			
		||||
import dan200.computercraft.api.turtle.ITurtleAccess;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Fired when a turtle gathers data on an item in its inventory.
 | 
			
		||||
 *
 | 
			
		||||
 * You may prevent items being inspected, or add additional information to the result. Be aware that this is fired on
 | 
			
		||||
 * the computer thread, and so any operations on it must be thread safe.
 | 
			
		||||
 *
 | 
			
		||||
 * @see TurtleAction#INSPECT_ITEM
 | 
			
		||||
 */
 | 
			
		||||
public class TurtleInspectItemEvent extends TurtleActionEvent
 | 
			
		||||
{
 | 
			
		||||
    private final ItemStack stack;
 | 
			
		||||
    private final Map<String, Object> data;
 | 
			
		||||
 | 
			
		||||
    public TurtleInspectItemEvent( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack, @Nonnull Map<String, Object> data )
 | 
			
		||||
    {
 | 
			
		||||
        super( turtle, TurtleAction.INSPECT_ITEM );
 | 
			
		||||
 | 
			
		||||
        Objects.requireNonNull( stack, "stack cannot be null" );
 | 
			
		||||
        Objects.requireNonNull( data, "data cannot be null" );
 | 
			
		||||
        this.stack = stack;
 | 
			
		||||
        this.data = data;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The item which is currently being inspected.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The item stack which is being inspected. This should <b>not</b> be modified.
 | 
			
		||||
     */
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public ItemStack getStack()
 | 
			
		||||
    {
 | 
			
		||||
        return stack;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the "inspection data" from this item, which will be returned to the user.
 | 
			
		||||
     *
 | 
			
		||||
     * @return This items's inspection data.
 | 
			
		||||
     */
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public Map<String, Object> getData()
 | 
			
		||||
    {
 | 
			
		||||
        return data;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Add new information to the inspection result. Note this will override fields with the same name.
 | 
			
		||||
     *
 | 
			
		||||
     * @param newData The data to add. Note all values should be convertible to Lua (see
 | 
			
		||||
     *                {@link dan200.computercraft.api.peripheral.IPeripheral#callMethod(IComputerAccess, ILuaContext, int, Object[])}).
 | 
			
		||||
     */
 | 
			
		||||
    public void addData( @Nonnull Map<String, ?> newData )
 | 
			
		||||
    {
 | 
			
		||||
        Objects.requireNonNull( newData, "newData cannot be null" );
 | 
			
		||||
        data.putAll( newData );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -11,7 +11,6 @@ import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.util.math.BlockPos;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
import net.minecraftforge.common.util.FakePlayer;
 | 
			
		||||
import net.minecraftforge.fml.common.eventhandler.Cancelable;
 | 
			
		||||
import net.minecraftforge.items.IItemHandler;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
@@ -21,7 +20,6 @@ import java.util.Objects;
 | 
			
		||||
/**
 | 
			
		||||
 * Fired when a turtle attempts to interact with an inventory.
 | 
			
		||||
 */
 | 
			
		||||
@Cancelable
 | 
			
		||||
public abstract class TurtleInventoryEvent extends TurtleBlockEvent
 | 
			
		||||
{
 | 
			
		||||
    private final IItemHandler handler;
 | 
			
		||||
@@ -48,7 +46,6 @@ public abstract class TurtleInventoryEvent extends TurtleBlockEvent
 | 
			
		||||
     *
 | 
			
		||||
     * @see TurtleAction#SUCK
 | 
			
		||||
     */
 | 
			
		||||
    @Cancelable
 | 
			
		||||
    public static class Suck extends TurtleInventoryEvent
 | 
			
		||||
    {
 | 
			
		||||
        public Suck( @Nonnull ITurtleAccess turtle, @Nonnull FakePlayer player, @Nonnull World world, @Nonnull BlockPos pos, @Nullable IItemHandler handler )
 | 
			
		||||
@@ -62,7 +59,6 @@ public abstract class TurtleInventoryEvent extends TurtleBlockEvent
 | 
			
		||||
     *
 | 
			
		||||
     * @see TurtleAction#DROP
 | 
			
		||||
     */
 | 
			
		||||
    @Cancelable
 | 
			
		||||
    public static class Drop extends TurtleInventoryEvent
 | 
			
		||||
    {
 | 
			
		||||
        private final ItemStack stack;
 | 
			
		||||
@@ -78,13 +74,12 @@ public abstract class TurtleInventoryEvent extends TurtleBlockEvent
 | 
			
		||||
        /**
 | 
			
		||||
         * The item which will be inserted into the inventory/dropped on the ground.
 | 
			
		||||
         *
 | 
			
		||||
         * Note that this is a copy of the original stack, and so should not be modified, as that will have no effect.
 | 
			
		||||
         *
 | 
			
		||||
         * @return The item stack which will be dropped.
 | 
			
		||||
         * @return The item stack which will be dropped. This should <b>not</b> be modified.
 | 
			
		||||
         */
 | 
			
		||||
        @Nonnull
 | 
			
		||||
        public ItemStack getStack()
 | 
			
		||||
        {
 | 
			
		||||
            return stack.copy();
 | 
			
		||||
            return stack;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,91 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.api.turtle.event;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.turtle.ITurtleAccess;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Fired when a turtle attempts to refuel from an item.
 | 
			
		||||
 *
 | 
			
		||||
 * One may use {@link #setCanceled(boolean, String)} to prevent refueling from this specific item. Additionally, you
 | 
			
		||||
 * may use {@link #setHandler(Handler)} to register a custom fuel provider.
 | 
			
		||||
 */
 | 
			
		||||
public class TurtleRefuelEvent extends TurtleActionEvent
 | 
			
		||||
{
 | 
			
		||||
    private final ItemStack stack;
 | 
			
		||||
    private Handler handler;
 | 
			
		||||
 | 
			
		||||
    public TurtleRefuelEvent( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack )
 | 
			
		||||
    {
 | 
			
		||||
        super( turtle, TurtleAction.REFUEL );
 | 
			
		||||
 | 
			
		||||
        Objects.requireNonNull( turtle, "turtle cannot be null" );
 | 
			
		||||
        this.stack = stack;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the stack we are attempting to refuel from.
 | 
			
		||||
     *
 | 
			
		||||
     * Do not modify the returned stack - all modifications should be done within the {@link Handler}.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The stack to refuel from.
 | 
			
		||||
     */
 | 
			
		||||
    public ItemStack getStack()
 | 
			
		||||
    {
 | 
			
		||||
        return stack;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the refuel handler for this stack.
 | 
			
		||||
     *
 | 
			
		||||
     * @return The refuel handler, or {@code null} if none has currently been set.
 | 
			
		||||
     * @see #setHandler(Handler)
 | 
			
		||||
     */
 | 
			
		||||
    @Nullable
 | 
			
		||||
    public Handler getHandler()
 | 
			
		||||
    {
 | 
			
		||||
        return handler;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Set the refuel handler for this stack.
 | 
			
		||||
     *
 | 
			
		||||
     * You should call this if you can actually refuel from this item, and ideally only if there are no existing
 | 
			
		||||
     * handlers.
 | 
			
		||||
     *
 | 
			
		||||
     * @param handler The new refuel handler.
 | 
			
		||||
     * @see #getHandler()
 | 
			
		||||
     */
 | 
			
		||||
    public void setHandler( @Nullable Handler handler )
 | 
			
		||||
    {
 | 
			
		||||
        this.handler = handler;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Handles refuelling a turtle from a specific item.
 | 
			
		||||
     */
 | 
			
		||||
    @FunctionalInterface
 | 
			
		||||
    public interface Handler
 | 
			
		||||
    {
 | 
			
		||||
        /**
 | 
			
		||||
         * Refuel a turtle using an item.
 | 
			
		||||
         *
 | 
			
		||||
         * @param turtle The turtle to refuel.
 | 
			
		||||
         * @param stack  The stack to refuel with.
 | 
			
		||||
         * @param slot   The slot the stack resides within. This may be used to modify the inventory afterwards.
 | 
			
		||||
         * @param limit  The maximum number of refuel operations to perform. This will often correspond to the number of
 | 
			
		||||
         *               items to consume.
 | 
			
		||||
         * @return The amount of fuel gained.
 | 
			
		||||
         */
 | 
			
		||||
        int refuel( @Nonnull ITurtleAccess turtle, @Nonnull ItemStack stack, int slot, int limit );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -7,6 +7,11 @@
 | 
			
		||||
package dan200.computercraft.client;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.client.render.TurtleModelLoader;
 | 
			
		||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
 | 
			
		||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
 | 
			
		||||
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
 | 
			
		||||
import dan200.computercraft.shared.util.Colour;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.renderer.ItemMeshDefinition;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.IBakedModel;
 | 
			
		||||
@@ -17,6 +22,7 @@ import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
 | 
			
		||||
import net.minecraft.item.Item;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.util.ResourceLocation;
 | 
			
		||||
import net.minecraftforge.client.event.ColorHandlerEvent;
 | 
			
		||||
import net.minecraftforge.client.event.ModelBakeEvent;
 | 
			
		||||
import net.minecraftforge.client.event.ModelRegistryEvent;
 | 
			
		||||
import net.minecraftforge.client.event.TextureStitchEvent;
 | 
			
		||||
@@ -33,9 +39,9 @@ import javax.annotation.Nonnull;
 | 
			
		||||
 * Registers textures and models for items.
 | 
			
		||||
 */
 | 
			
		||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
 | 
			
		||||
public class ClientRegistry
 | 
			
		||||
public final class ClientRegistry
 | 
			
		||||
{
 | 
			
		||||
    private static final String[] TURTLE_UPGRADES = {
 | 
			
		||||
    private static final String[] EXTRA_MODELS = new String[] {
 | 
			
		||||
        "turtle_modem_off_left",
 | 
			
		||||
        "turtle_modem_on_left",
 | 
			
		||||
        "turtle_modem_off_right",
 | 
			
		||||
@@ -48,11 +54,18 @@ public class ClientRegistry
 | 
			
		||||
        "advanced_turtle_modem_on_right",
 | 
			
		||||
        "turtle_speaker_upgrade_left",
 | 
			
		||||
        "turtle_speaker_upgrade_right",
 | 
			
		||||
 | 
			
		||||
        "turtle_white",
 | 
			
		||||
        "turtle_elf_overlay",
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    private ClientRegistry() {}
 | 
			
		||||
 | 
			
		||||
    @SubscribeEvent
 | 
			
		||||
    public static void registerModels( ModelRegistryEvent event )
 | 
			
		||||
    {
 | 
			
		||||
        ModelLoaderRegistry.registerLoader( TurtleModelLoader.INSTANCE );
 | 
			
		||||
 | 
			
		||||
        // Register item models
 | 
			
		||||
        registerUniversalItemModel( ComputerCraft.Items.computer, "computer" );
 | 
			
		||||
        registerItemModel( ComputerCraft.Items.commandComputer, 0, "command_computer" );
 | 
			
		||||
@@ -79,35 +92,59 @@ public class ClientRegistry
 | 
			
		||||
        registerItemModel( ComputerCraft.Items.printout, 1, "pages" );
 | 
			
		||||
        registerItemModel( ComputerCraft.Items.printout, 2, "book" );
 | 
			
		||||
 | 
			
		||||
        String[] extraTurtleModels = new String[] { "turtle", "turtle_advanced", "turtle_white", "turtle_elf_overlay" };
 | 
			
		||||
        registerUniversalItemModel( ComputerCraft.Items.turtle, "turtle_dynamic", extraTurtleModels );
 | 
			
		||||
        registerUniversalItemModel( ComputerCraft.Items.turtleExpanded, "turtle_dynamic", extraTurtleModels );
 | 
			
		||||
        registerUniversalItemModel( ComputerCraft.Items.turtleAdvanced, "turtle_dynamic", extraTurtleModels );
 | 
			
		||||
        registerUniversalItemModel( ComputerCraft.Items.turtle, "turtle" );
 | 
			
		||||
        registerUniversalItemModel( ComputerCraft.Items.turtleExpanded, "turtle" );
 | 
			
		||||
        registerUniversalItemModel( ComputerCraft.Items.turtleAdvanced, "turtle_advanced" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SubscribeEvent
 | 
			
		||||
    public static void onTextureStitchEvent( TextureStitchEvent.Pre event )
 | 
			
		||||
    {
 | 
			
		||||
        // Load all textures for upgrades
 | 
			
		||||
        // Load all textures for the extra models
 | 
			
		||||
        TextureMap map = event.getMap();
 | 
			
		||||
        for( String upgrade : TURTLE_UPGRADES )
 | 
			
		||||
        for( String upgrade : EXTRA_MODELS )
 | 
			
		||||
        {
 | 
			
		||||
            IModel model = ModelLoaderRegistry.getModelOrMissing( new ResourceLocation( "computercraft", "block/" + upgrade ) );
 | 
			
		||||
            for( ResourceLocation texture : model.getTextures() )
 | 
			
		||||
            {
 | 
			
		||||
                map.registerSprite( texture );
 | 
			
		||||
            }
 | 
			
		||||
            for( ResourceLocation texture : model.getTextures() ) map.registerSprite( texture );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SubscribeEvent
 | 
			
		||||
    public static void onModelBakeEvent( ModelBakeEvent event )
 | 
			
		||||
    {
 | 
			
		||||
        // Load all upgrade models
 | 
			
		||||
        for( String upgrade : TURTLE_UPGRADES )
 | 
			
		||||
        {
 | 
			
		||||
            loadBlockModel( event, upgrade );
 | 
			
		||||
        }
 | 
			
		||||
        // Load all extra models
 | 
			
		||||
        for( String model : EXTRA_MODELS ) loadBlockModel( event, model );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SubscribeEvent
 | 
			
		||||
    public static void onItemColours( ColorHandlerEvent.Item event )
 | 
			
		||||
    {
 | 
			
		||||
        event.getItemColors().registerItemColorHandler(
 | 
			
		||||
            ( stack, layer ) -> layer == 1 ? ((ItemDiskLegacy) stack.getItem()).getColour( stack ) : 0xFFFFFF,
 | 
			
		||||
            ComputerCraft.Items.disk, ComputerCraft.Items.diskExpanded
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        event.getItemColors().registerItemColorHandler( ( stack, layer ) -> {
 | 
			
		||||
            switch( layer )
 | 
			
		||||
            {
 | 
			
		||||
                case 0:
 | 
			
		||||
                default:
 | 
			
		||||
                    return 0xFFFFFF;
 | 
			
		||||
                case 1: // Frame colour
 | 
			
		||||
                    return ComputerCraft.Items.pocketComputer.getColour( stack );
 | 
			
		||||
                case 2: // Light colour
 | 
			
		||||
                {
 | 
			
		||||
                    int light = ItemPocketComputer.getLightState( stack );
 | 
			
		||||
                    return light == -1 ? Colour.Black.getHex() : light;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }, ComputerCraft.Items.pocketComputer );
 | 
			
		||||
 | 
			
		||||
        // Setup turtle colours
 | 
			
		||||
        event.getItemColors().registerItemColorHandler(
 | 
			
		||||
            ( stack, tintIndex ) -> tintIndex == 0 ? ((ItemTurtleBase) stack.getItem()).getColour( stack ) : 0xFFFFFF,
 | 
			
		||||
            ComputerCraft.Blocks.turtle, ComputerCraft.Blocks.turtleExpanded, ComputerCraft.Blocks.turtleAdvanced
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void registerItemModel( Item item, int damage, String name )
 | 
			
		||||
@@ -118,18 +155,10 @@ public class ClientRegistry
 | 
			
		||||
        ModelLoader.setCustomModelResourceLocation( item, damage, res );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static void registerUniversalItemModel( Item item, String mainModel, String... extraModels )
 | 
			
		||||
    private static void registerUniversalItemModel( Item item, String mainModel )
 | 
			
		||||
    {
 | 
			
		||||
        ResourceLocation mainLocation = new ResourceLocation( ComputerCraft.MOD_ID, mainModel );
 | 
			
		||||
 | 
			
		||||
        ResourceLocation[] modelLocations = new ResourceLocation[extraModels.length + 1];
 | 
			
		||||
        modelLocations[0] = mainLocation;
 | 
			
		||||
        for( int i = 0; i < extraModels.length; i++ )
 | 
			
		||||
        {
 | 
			
		||||
            modelLocations[i + 1] = new ResourceLocation( ComputerCraft.MOD_ID, extraModels[i] );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        ModelBakery.registerItemVariants( item, modelLocations );
 | 
			
		||||
        ModelBakery.registerItemVariants( item, mainLocation );
 | 
			
		||||
 | 
			
		||||
        final ModelResourceLocation mainModelLocation = new ModelResourceLocation( mainLocation, "inventory" );
 | 
			
		||||
        ModelLoader.setCustomMeshDefinition( item, new ItemMeshDefinition()
 | 
			
		||||
 
 | 
			
		||||
@@ -13,12 +13,14 @@ import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.gui.FontRenderer;
 | 
			
		||||
import net.minecraft.client.gui.GuiNewChat;
 | 
			
		||||
import net.minecraft.client.gui.GuiUtilRenderComponents;
 | 
			
		||||
import net.minecraft.util.math.MathHelper;
 | 
			
		||||
import net.minecraft.util.text.ITextComponent;
 | 
			
		||||
import net.minecraft.util.text.TextFormatting;
 | 
			
		||||
import org.apache.commons.lang3.StringUtils;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public class ClientTableFormatter implements TableFormatter
 | 
			
		||||
{
 | 
			
		||||
@@ -26,7 +28,7 @@ public class ClientTableFormatter implements TableFormatter
 | 
			
		||||
 | 
			
		||||
    private static Int2IntOpenHashMap lastHeights = new Int2IntOpenHashMap();
 | 
			
		||||
 | 
			
		||||
    private FontRenderer renderer()
 | 
			
		||||
    private static FontRenderer renderer()
 | 
			
		||||
    {
 | 
			
		||||
        return Minecraft.getMinecraft().fontRenderer;
 | 
			
		||||
    }
 | 
			
		||||
@@ -62,7 +64,13 @@ public class ClientTableFormatter implements TableFormatter
 | 
			
		||||
    @Override
 | 
			
		||||
    public void writeLine( int id, ITextComponent component )
 | 
			
		||||
    {
 | 
			
		||||
        Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessageWithOptionalDeletion( component, id );
 | 
			
		||||
        Minecraft mc = Minecraft.getMinecraft();
 | 
			
		||||
        GuiNewChat chat = mc.ingameGUI.getChatGUI();
 | 
			
		||||
 | 
			
		||||
        // Trim the text if it goes over the allowed length
 | 
			
		||||
        int maxWidth = MathHelper.floor( chat.getChatWidth() / chat.getChatScale() );
 | 
			
		||||
        List<ITextComponent> list = GuiUtilRenderComponents.splitText( component, maxWidth, mc.fontRenderer, false, false );
 | 
			
		||||
        if( !list.isEmpty() ) chat.printChatMessageWithOptionalDeletion( list.get( 0 ), id );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -19,7 +19,7 @@ import org.lwjgl.opengl.GL11;
 | 
			
		||||
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
 | 
			
		||||
public class FixedWidthFontRenderer
 | 
			
		||||
public final class FixedWidthFontRenderer
 | 
			
		||||
{
 | 
			
		||||
    private static final ResourceLocation FONT = new ResourceLocation( "computercraft", "textures/gui/term_font.png" );
 | 
			
		||||
    public static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/term_background.png" );
 | 
			
		||||
@@ -93,7 +93,7 @@ public class FixedWidthFontRenderer
 | 
			
		||||
 | 
			
		||||
    private boolean isGreyScale( int colour )
 | 
			
		||||
    {
 | 
			
		||||
        return (colour == 0 || colour == 15 || colour == 7 || colour == 8);
 | 
			
		||||
        return colour == 0 || colour == 15 || colour == 7 || colour == 8;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void drawStringBackgroundPart( int x, int y, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale, Palette p )
 | 
			
		||||
 
 | 
			
		||||
@@ -80,12 +80,6 @@ public class GuiComputer extends GuiContainer
 | 
			
		||||
        Keyboard.enableRepeatEvents( false );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean doesGuiPauseGame()
 | 
			
		||||
    {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void updateScreen()
 | 
			
		||||
    {
 | 
			
		||||
@@ -144,7 +138,7 @@ public class GuiComputer extends GuiContainer
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void drawScreen( int mouseX, int mouseY, float f )
 | 
			
		||||
    public void drawScreen( int mouseX, int mouseY, float partialTicks )
 | 
			
		||||
    {
 | 
			
		||||
        // Work out where to draw
 | 
			
		||||
        int startX = (width - m_terminal.getWidth()) / 2;
 | 
			
		||||
@@ -156,7 +150,7 @@ public class GuiComputer extends GuiContainer
 | 
			
		||||
        drawDefaultBackground();
 | 
			
		||||
 | 
			
		||||
        // Draw terminal
 | 
			
		||||
        m_terminal.draw( this.mc, startX, startY, mouseX, mouseY );
 | 
			
		||||
        m_terminal.draw( mc, startX, startY, mouseX, mouseY );
 | 
			
		||||
 | 
			
		||||
        // Draw a border around the terminal
 | 
			
		||||
        GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
 | 
			
		||||
@@ -164,20 +158,14 @@ public class GuiComputer extends GuiContainer
 | 
			
		||||
        {
 | 
			
		||||
            case Normal:
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                this.mc.getTextureManager().bindTexture( BACKGROUND );
 | 
			
		||||
                mc.getTextureManager().bindTexture( BACKGROUND );
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            case Advanced:
 | 
			
		||||
            {
 | 
			
		||||
                this.mc.getTextureManager().bindTexture( BACKGROUND_ADVANCED );
 | 
			
		||||
                mc.getTextureManager().bindTexture( BACKGROUND_ADVANCED );
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            case Command:
 | 
			
		||||
            {
 | 
			
		||||
                this.mc.getTextureManager().bindTexture( BACKGROUND_COMMAND );
 | 
			
		||||
                mc.getTextureManager().bindTexture( BACKGROUND_COMMAND );
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        drawTexturedModalRect( startX - 12, startY - 12, 12, 28, 12, 12 );
 | 
			
		||||
 
 | 
			
		||||
@@ -22,10 +22,8 @@ public class GuiConfigCC extends GuiConfig
 | 
			
		||||
        super( parentScreen, Config.getConfigElements(), ComputerCraft.MOD_ID, false, false, "CC: Tweaked" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class Factory
 | 
			
		||||
        implements IModGuiFactory
 | 
			
		||||
    public static class Factory implements IModGuiFactory
 | 
			
		||||
    {
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void initialize( Minecraft minecraft )
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
@@ -25,21 +25,19 @@ public class GuiDiskDrive extends GuiContainer
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void drawGuiContainerForegroundLayer( int par1, int par2 )
 | 
			
		||||
    protected void drawGuiContainerForegroundLayer( int mouseX, int mouseY )
 | 
			
		||||
    {
 | 
			
		||||
        String title = m_container.getDiskDrive().getDisplayName().getUnformattedText();
 | 
			
		||||
        fontRenderer.drawString( title, (xSize - fontRenderer.getStringWidth( title )) / 2, 6, 0x404040 );
 | 
			
		||||
        fontRenderer.drawString( I18n.format( "container.inventory" ), 8, (ySize - 96) + 2, 0x404040 );
 | 
			
		||||
        fontRenderer.drawString( I18n.format( "container.inventory" ), 8, ySize - 96 + 2, 0x404040 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float f, int i, int j )
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
 | 
			
		||||
    {
 | 
			
		||||
        GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
 | 
			
		||||
        this.mc.getTextureManager().bindTexture( BACKGROUND );
 | 
			
		||||
        int l = (width - xSize) / 2;
 | 
			
		||||
        int i1 = (height - ySize) / 2;
 | 
			
		||||
        drawTexturedModalRect( l, i1, 0, 0, xSize, ySize );
 | 
			
		||||
        mc.getTextureManager().bindTexture( BACKGROUND );
 | 
			
		||||
        drawTexturedModalRect( guiLeft, guiTop, 0, 0, xSize, ySize );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -8,6 +8,7 @@ package dan200.computercraft.client.gui;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
 | 
			
		||||
import dan200.computercraft.shared.pocket.items.ItemPocketComputer;
 | 
			
		||||
 | 
			
		||||
public class GuiPocketComputer extends GuiComputer
 | 
			
		||||
{
 | 
			
		||||
@@ -16,7 +17,7 @@ public class GuiPocketComputer extends GuiComputer
 | 
			
		||||
        super(
 | 
			
		||||
            container,
 | 
			
		||||
            ComputerCraft.Items.pocketComputer.getFamily( container.getStack() ),
 | 
			
		||||
            ComputerCraft.Items.pocketComputer.createClientComputer( container.getStack() ),
 | 
			
		||||
            ItemPocketComputer.createClientComputer( container.getStack() ),
 | 
			
		||||
            ComputerCraft.terminalWidth_pocketComputer,
 | 
			
		||||
            ComputerCraft.terminalHeight_pocketComputer
 | 
			
		||||
        );
 | 
			
		||||
 
 | 
			
		||||
@@ -16,36 +16,30 @@ public class GuiPrinter extends GuiContainer
 | 
			
		||||
{
 | 
			
		||||
    private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/printer.png" );
 | 
			
		||||
 | 
			
		||||
    private final ContainerPrinter m_container;
 | 
			
		||||
    private final ContainerPrinter container;
 | 
			
		||||
 | 
			
		||||
    public GuiPrinter( ContainerPrinter container )
 | 
			
		||||
    {
 | 
			
		||||
        super( container );
 | 
			
		||||
        m_container = container;
 | 
			
		||||
        this.container = container;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void drawGuiContainerForegroundLayer( int par1, int par2 )
 | 
			
		||||
    protected void drawGuiContainerForegroundLayer( int mouseX, int mouseY )
 | 
			
		||||
    {
 | 
			
		||||
        String title = m_container.getPrinter().getDisplayName().getUnformattedText();
 | 
			
		||||
        String title = container.getPrinter().getDisplayName().getUnformattedText();
 | 
			
		||||
        fontRenderer.drawString( title, (xSize - fontRenderer.getStringWidth( title )) / 2, 6, 0x404040 );
 | 
			
		||||
        fontRenderer.drawString( I18n.format( "container.inventory" ), 8, (ySize - 96) + 2, 0x404040 );
 | 
			
		||||
        fontRenderer.drawString( I18n.format( "container.inventory" ), 8, ySize - 96 + 2, 0x404040 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float f, int i, int j )
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
 | 
			
		||||
    {
 | 
			
		||||
        GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
 | 
			
		||||
        this.mc.getTextureManager().bindTexture( BACKGROUND );
 | 
			
		||||
        int startX = (width - xSize) / 2;
 | 
			
		||||
        int startY = (height - ySize) / 2;
 | 
			
		||||
        drawTexturedModalRect( startX, startY, 0, 0, xSize, ySize );
 | 
			
		||||
        mc.getTextureManager().bindTexture( BACKGROUND );
 | 
			
		||||
        drawTexturedModalRect( guiLeft, guiTop, 0, 0, xSize, ySize );
 | 
			
		||||
 | 
			
		||||
        boolean printing = m_container.isPrinting();
 | 
			
		||||
        if( printing )
 | 
			
		||||
        {
 | 
			
		||||
            drawTexturedModalRect( startX + 34, startY + 21, 176, 0, 25, 45 );
 | 
			
		||||
        }
 | 
			
		||||
        if( container.isPrinting() ) drawTexturedModalRect( guiLeft + 34, guiTop + 21, 176, 0, 25, 45 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -29,6 +29,8 @@ public class GuiPrintout extends GuiContainer
 | 
			
		||||
    {
 | 
			
		||||
        super( container );
 | 
			
		||||
 | 
			
		||||
        ySize = Y_SIZE;
 | 
			
		||||
 | 
			
		||||
        String[] text = ItemPrintout.getText( container.getStack() );
 | 
			
		||||
        m_text = new TextBuffer[text.length];
 | 
			
		||||
        for( int i = 0; i < m_text.length; i++ ) m_text[i] = new TextBuffer( text[i] );
 | 
			
		||||
@@ -42,12 +44,6 @@ public class GuiPrintout extends GuiContainer
 | 
			
		||||
        m_book = ItemPrintout.getType( container.getStack() ) == ItemPrintout.Type.Book;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean doesGuiPauseGame()
 | 
			
		||||
    {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void keyTyped( char c, int k ) throws IOException
 | 
			
		||||
    {
 | 
			
		||||
@@ -73,41 +69,35 @@ public class GuiPrintout extends GuiContainer
 | 
			
		||||
        int mouseWheelChange = Mouse.getEventDWheel();
 | 
			
		||||
        if( mouseWheelChange < 0 )
 | 
			
		||||
        {
 | 
			
		||||
            // Up
 | 
			
		||||
            // Scroll up goes to the next page
 | 
			
		||||
            if( m_page < m_pages - 1 ) m_page++;
 | 
			
		||||
        }
 | 
			
		||||
        else if( mouseWheelChange > 0 )
 | 
			
		||||
        {
 | 
			
		||||
            // Down
 | 
			
		||||
            // Scroll down goes to the previous page
 | 
			
		||||
            if( m_page > 0 ) m_page--;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void drawGuiContainerForegroundLayer( int par1, int par2 )
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float var1, int var2, int var3 )
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void drawScreen( int mouseX, int mouseY, float f )
 | 
			
		||||
    {
 | 
			
		||||
        // Draw background
 | 
			
		||||
        zLevel = zLevel - 1;
 | 
			
		||||
        drawDefaultBackground();
 | 
			
		||||
        zLevel = zLevel + 1;
 | 
			
		||||
 | 
			
		||||
        // Draw the printout
 | 
			
		||||
        GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
 | 
			
		||||
 | 
			
		||||
        int startY = (height - Y_SIZE) / 2;
 | 
			
		||||
        int startX = (width - X_SIZE) / 2;
 | 
			
		||||
        drawBorder( guiLeft, guiTop, zLevel, m_page, m_pages, m_book );
 | 
			
		||||
        drawText( guiLeft + X_TEXT_MARGIN, guiTop + Y_TEXT_MARGIN, ItemPrintout.LINES_PER_PAGE * m_page, m_text, m_colours );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
        drawBorder( startX, startY, zLevel, m_page, m_pages, m_book );
 | 
			
		||||
        drawText( startX + X_TEXT_MARGIN, startY + Y_TEXT_MARGIN, ItemPrintout.LINES_PER_PAGE * m_page, m_text, m_colours );
 | 
			
		||||
    @Override
 | 
			
		||||
    public void drawScreen( int mouseX, int mouseY, float partialTicks )
 | 
			
		||||
    {
 | 
			
		||||
        // We must take the background further back in order to not overlap with our printed pages.
 | 
			
		||||
        zLevel--;
 | 
			
		||||
        drawDefaultBackground();
 | 
			
		||||
        zLevel++;
 | 
			
		||||
 | 
			
		||||
        super.drawScreen( mouseX, mouseY, partialTicks );
 | 
			
		||||
        renderHoveredToolTip( mouseX, mouseY );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@ import java.io.IOException;
 | 
			
		||||
 | 
			
		||||
public class GuiTurtle extends GuiContainer
 | 
			
		||||
{
 | 
			
		||||
    private static final ResourceLocation BACKGROUND = new ResourceLocation( "computercraft", "textures/gui/turtle.png" );
 | 
			
		||||
    private static final ResourceLocation BACKGROUND_NORMAL = new ResourceLocation( "computercraft", "textures/gui/turtle.png" );
 | 
			
		||||
    private static final ResourceLocation BACKGROUND_ADVANCED = new ResourceLocation( "computercraft", "textures/gui/turtle_advanced.png" );
 | 
			
		||||
 | 
			
		||||
    private ContainerTurtle m_container;
 | 
			
		||||
@@ -50,8 +50,8 @@ public class GuiTurtle extends GuiContainer
 | 
			
		||||
        super.initGui();
 | 
			
		||||
        Keyboard.enableRepeatEvents( true );
 | 
			
		||||
        m_terminalGui = new WidgetTerminal(
 | 
			
		||||
            (width - xSize) / 2 + 8,
 | 
			
		||||
            (height - ySize) / 2 + 8,
 | 
			
		||||
            guiLeft + 8,
 | 
			
		||||
            guiTop + 8,
 | 
			
		||||
            ComputerCraft.terminalWidth_turtle,
 | 
			
		||||
            ComputerCraft.terminalHeight_turtle,
 | 
			
		||||
            () -> m_computer,
 | 
			
		||||
@@ -98,8 +98,8 @@ public class GuiTurtle extends GuiContainer
 | 
			
		||||
    public void handleMouseInput() throws IOException
 | 
			
		||||
    {
 | 
			
		||||
        super.handleMouseInput();
 | 
			
		||||
        int x = Mouse.getEventX() * this.width / mc.displayWidth;
 | 
			
		||||
        int y = this.height - Mouse.getEventY() * this.height / mc.displayHeight - 1;
 | 
			
		||||
        int x = Mouse.getEventX() * width / mc.displayWidth;
 | 
			
		||||
        int y = height - Mouse.getEventY() * height / mc.displayHeight - 1;
 | 
			
		||||
        m_terminalGui.handleMouseInput( x, y );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -112,34 +112,29 @@ public class GuiTurtle extends GuiContainer
 | 
			
		||||
 | 
			
		||||
    protected void drawSelectionSlot( boolean advanced )
 | 
			
		||||
    {
 | 
			
		||||
        int x = (width - xSize) / 2;
 | 
			
		||||
        int y = (height - ySize) / 2;
 | 
			
		||||
 | 
			
		||||
        // Draw selection slot
 | 
			
		||||
        int slot = m_container.getSelectedSlot();
 | 
			
		||||
        if( slot >= 0 )
 | 
			
		||||
        {
 | 
			
		||||
            GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
 | 
			
		||||
            int slotX = (slot % 4);
 | 
			
		||||
            int slotY = (slot / 4);
 | 
			
		||||
            this.mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND );
 | 
			
		||||
            drawTexturedModalRect( x + m_container.m_turtleInvStartX - 2 + slotX * 18, y + m_container.m_playerInvStartY - 2 + slotY * 18, 0, 217, 24, 24 );
 | 
			
		||||
            int slotX = slot % 4;
 | 
			
		||||
            int slotY = slot / 4;
 | 
			
		||||
            mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL );
 | 
			
		||||
            drawTexturedModalRect( guiLeft + m_container.m_turtleInvStartX - 2 + slotX * 18, guiTop + m_container.m_playerInvStartY - 2 + slotY * 18, 0, 217, 24, 24 );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float f, int mouseX, int mouseY )
 | 
			
		||||
    protected void drawGuiContainerBackgroundLayer( float partialTicks, int mouseX, int mouseY )
 | 
			
		||||
    {
 | 
			
		||||
        // Draw term
 | 
			
		||||
        boolean advanced = (m_family == ComputerFamily.Advanced);
 | 
			
		||||
        boolean advanced = m_family == ComputerFamily.Advanced;
 | 
			
		||||
        m_terminalGui.draw( Minecraft.getMinecraft(), 0, 0, mouseX, mouseY );
 | 
			
		||||
 | 
			
		||||
        // Draw border/inventory
 | 
			
		||||
        GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
 | 
			
		||||
        this.mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND );
 | 
			
		||||
        int x = (width - xSize) / 2;
 | 
			
		||||
        int y = (height - ySize) / 2;
 | 
			
		||||
        drawTexturedModalRect( x, y, 0, 0, xSize, ySize );
 | 
			
		||||
        mc.getTextureManager().bindTexture( advanced ? BACKGROUND_ADVANCED : BACKGROUND_NORMAL );
 | 
			
		||||
        drawTexturedModalRect( guiLeft, guiTop, 0, 0, xSize, ySize );
 | 
			
		||||
 | 
			
		||||
        drawSelectionSlot( advanced );
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -31,23 +31,23 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
 | 
			
		||||
    private final IComputerContainer m_computer;
 | 
			
		||||
 | 
			
		||||
    private float m_terminateTimer;
 | 
			
		||||
    private float m_rebootTimer;
 | 
			
		||||
    private float m_shutdownTimer;
 | 
			
		||||
    private float m_terminateTimer = 0.0f;
 | 
			
		||||
    private float m_rebootTimer = 0.0f;
 | 
			
		||||
    private float m_shutdownTimer = 0.0f;
 | 
			
		||||
 | 
			
		||||
    private int m_lastClickButton;
 | 
			
		||||
    private int m_lastClickX;
 | 
			
		||||
    private int m_lastClickY;
 | 
			
		||||
    private int m_lastClickButton = -1;
 | 
			
		||||
    private int m_lastClickX = -1;
 | 
			
		||||
    private int m_lastClickY = -1;
 | 
			
		||||
 | 
			
		||||
    private boolean m_focus;
 | 
			
		||||
    private boolean m_allowFocusLoss;
 | 
			
		||||
    private boolean m_focus = false;
 | 
			
		||||
    private boolean m_allowFocusLoss = true;
 | 
			
		||||
 | 
			
		||||
    private int m_leftMargin;
 | 
			
		||||
    private int m_rightMargin;
 | 
			
		||||
    private int m_topMargin;
 | 
			
		||||
    private int m_bottomMargin;
 | 
			
		||||
 | 
			
		||||
    private ArrayList<Integer> m_keysDown;
 | 
			
		||||
    private final ArrayList<Integer> m_keysDown = new ArrayList<>();
 | 
			
		||||
 | 
			
		||||
    public WidgetTerminal( int x, int y, int termWidth, int termHeight, IComputerContainer computer, int leftMargin, int rightMargin, int topMargin, int bottomMargin )
 | 
			
		||||
    {
 | 
			
		||||
@@ -58,23 +58,11 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
        );
 | 
			
		||||
 | 
			
		||||
        m_computer = computer;
 | 
			
		||||
        m_terminateTimer = 0.0f;
 | 
			
		||||
        m_rebootTimer = 0.0f;
 | 
			
		||||
        m_shutdownTimer = 0.0f;
 | 
			
		||||
 | 
			
		||||
        m_lastClickButton = -1;
 | 
			
		||||
        m_lastClickX = -1;
 | 
			
		||||
        m_lastClickY = -1;
 | 
			
		||||
 | 
			
		||||
        m_focus = false;
 | 
			
		||||
        m_allowFocusLoss = true;
 | 
			
		||||
 | 
			
		||||
        m_leftMargin = leftMargin;
 | 
			
		||||
        m_rightMargin = rightMargin;
 | 
			
		||||
        m_topMargin = topMargin;
 | 
			
		||||
        m_bottomMargin = bottomMargin;
 | 
			
		||||
 | 
			
		||||
        m_keysDown = new ArrayList<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setAllowFocusLoss( boolean allowFocusLoss )
 | 
			
		||||
@@ -94,9 +82,9 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
                String clipboard = GuiScreen.getClipboardString();
 | 
			
		||||
                if( clipboard != null )
 | 
			
		||||
                {
 | 
			
		||||
                    // Clip to the first occurance of \r or \n
 | 
			
		||||
                    int newLineIndex1 = clipboard.indexOf( "\r" );
 | 
			
		||||
                    int newLineIndex2 = clipboard.indexOf( "\n" );
 | 
			
		||||
                    // Clip to the first occurrence of \r or \n
 | 
			
		||||
                    int newLineIndex1 = clipboard.indexOf( '\r' );
 | 
			
		||||
                    int newLineIndex2 = clipboard.indexOf( '\n' );
 | 
			
		||||
                    if( newLineIndex1 >= 0 && newLineIndex2 >= 0 )
 | 
			
		||||
                    {
 | 
			
		||||
                        clipboard = clipboard.substring( 0, Math.min( newLineIndex1, newLineIndex2 ) );
 | 
			
		||||
@@ -122,9 +110,7 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        // Queue the "paste" event
 | 
			
		||||
                        queueEvent( "paste", new Object[] {
 | 
			
		||||
                            clipboard
 | 
			
		||||
                        } );
 | 
			
		||||
                        queueEvent( "paste", new Object[] { clipboard } );
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                return true;
 | 
			
		||||
@@ -143,18 +129,15 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Queue the "key" event
 | 
			
		||||
                    queueEvent( "key", new Object[] {
 | 
			
		||||
                        key, repeat
 | 
			
		||||
                    } );
 | 
			
		||||
                    IComputer computer = m_computer.getComputer();
 | 
			
		||||
                    if( computer != null ) computer.keyDown( key, repeat );
 | 
			
		||||
                    handled = true;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if( (ch >= 32 && ch <= 126) || (ch >= 160 && ch <= 255) ) // printable chars in byte range
 | 
			
		||||
                {
 | 
			
		||||
                    // Queue the "char" event
 | 
			
		||||
                    queueEvent( "char", new Object[] {
 | 
			
		||||
                        Character.toString( ch )
 | 
			
		||||
                    } );
 | 
			
		||||
                    queueEvent( "char", new Object[] { Character.toString( ch ) } );
 | 
			
		||||
                    handled = true;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@@ -189,9 +172,7 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
                        charX = Math.min( Math.max( charX, 0 ), term.getWidth() - 1 );
 | 
			
		||||
                        charY = Math.min( Math.max( charY, 0 ), term.getHeight() - 1 );
 | 
			
		||||
 | 
			
		||||
                        computer.queueEvent( "mouse_click", new Object[] {
 | 
			
		||||
                            button + 1, charX + 1, charY + 1
 | 
			
		||||
                        } );
 | 
			
		||||
                        computer.mouseClick( button + 1, charX + 1, charY + 1 );
 | 
			
		||||
 | 
			
		||||
                        m_lastClickButton = button;
 | 
			
		||||
                        m_lastClickX = charX;
 | 
			
		||||
@@ -222,9 +203,8 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
                if( m_focus )
 | 
			
		||||
                {
 | 
			
		||||
                    // Queue the "key_up" event
 | 
			
		||||
                    queueEvent( "key_up", new Object[] {
 | 
			
		||||
                        key
 | 
			
		||||
                    } );
 | 
			
		||||
                    IComputer computer = m_computer.getComputer();
 | 
			
		||||
                    if( computer != null ) computer.keyUp( key );
 | 
			
		||||
                    handled = true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
@@ -251,12 +231,7 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
 | 
			
		||||
                if( m_lastClickButton >= 0 && !Mouse.isButtonDown( m_lastClickButton ) )
 | 
			
		||||
                {
 | 
			
		||||
                    if( m_focus )
 | 
			
		||||
                    {
 | 
			
		||||
                        computer.queueEvent( "mouse_up", new Object[] {
 | 
			
		||||
                            m_lastClickButton + 1, charX + 1, charY + 1
 | 
			
		||||
                        } );
 | 
			
		||||
                    }
 | 
			
		||||
                    if( m_focus ) computer.mouseUp( m_lastClickButton + 1, charX + 1, charY + 1 );
 | 
			
		||||
                    m_lastClickButton = -1;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@@ -270,22 +245,16 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
                {
 | 
			
		||||
                    if( wheelChange < 0 )
 | 
			
		||||
                    {
 | 
			
		||||
                        computer.queueEvent( "mouse_scroll", new Object[] {
 | 
			
		||||
                            1, charX + 1, charY + 1
 | 
			
		||||
                        } );
 | 
			
		||||
                        computer.mouseScroll( 1, charX + 1, charY + 1 );
 | 
			
		||||
                    }
 | 
			
		||||
                    else if( wheelChange > 0 )
 | 
			
		||||
                    {
 | 
			
		||||
                        computer.queueEvent( "mouse_scroll", new Object[] {
 | 
			
		||||
                            -1, charX + 1, charY + 1
 | 
			
		||||
                        } );
 | 
			
		||||
                        computer.mouseScroll( -1, charX + 1, charY + 1 );
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if( m_lastClickButton >= 0 && (charX != m_lastClickX || charY != m_lastClickY) )
 | 
			
		||||
                    {
 | 
			
		||||
                        computer.queueEvent( "mouse_drag", new Object[] {
 | 
			
		||||
                            m_lastClickButton + 1, charX + 1, charY + 1
 | 
			
		||||
                        } );
 | 
			
		||||
                        computer.mouseDrag( m_lastClickButton + 1, charX + 1, charY + 1 );
 | 
			
		||||
                        m_lastClickX = charX;
 | 
			
		||||
                        m_lastClickY = charY;
 | 
			
		||||
                    }
 | 
			
		||||
@@ -305,11 +274,8 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
            {
 | 
			
		||||
                if( m_terminateTimer < TERMINATE_TIME )
 | 
			
		||||
                {
 | 
			
		||||
                    m_terminateTimer = m_terminateTimer + 0.05f;
 | 
			
		||||
                    if( m_terminateTimer >= TERMINATE_TIME )
 | 
			
		||||
                    {
 | 
			
		||||
                        queueEvent( "terminate" );
 | 
			
		||||
                    }
 | 
			
		||||
                    m_terminateTimer += 0.05f;
 | 
			
		||||
                    if( m_terminateTimer >= TERMINATE_TIME ) queueEvent( "terminate" );
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
@@ -322,14 +288,11 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
            {
 | 
			
		||||
                if( m_rebootTimer < TERMINATE_TIME )
 | 
			
		||||
                {
 | 
			
		||||
                    m_rebootTimer = m_rebootTimer + 0.05f;
 | 
			
		||||
                    m_rebootTimer += 0.05f;
 | 
			
		||||
                    if( m_rebootTimer >= TERMINATE_TIME )
 | 
			
		||||
                    {
 | 
			
		||||
                        IComputer computer = m_computer.getComputer();
 | 
			
		||||
                        if( computer != null )
 | 
			
		||||
                        {
 | 
			
		||||
                            computer.reboot();
 | 
			
		||||
                        }
 | 
			
		||||
                        if( computer != null ) computer.reboot();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
@@ -343,14 +306,11 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
            {
 | 
			
		||||
                if( m_shutdownTimer < TERMINATE_TIME )
 | 
			
		||||
                {
 | 
			
		||||
                    m_shutdownTimer = m_shutdownTimer + 0.05f;
 | 
			
		||||
                    m_shutdownTimer += 0.05f;
 | 
			
		||||
                    if( m_shutdownTimer >= TERMINATE_TIME )
 | 
			
		||||
                    {
 | 
			
		||||
                        IComputer computer = m_computer.getComputer();
 | 
			
		||||
                        if( computer != null )
 | 
			
		||||
                        {
 | 
			
		||||
                            computer.shutdown();
 | 
			
		||||
                        }
 | 
			
		||||
                        if( computer != null ) computer.shutdown();
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
@@ -377,7 +337,7 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
        {
 | 
			
		||||
            // Draw the screen contents
 | 
			
		||||
            IComputer computer = m_computer.getComputer();
 | 
			
		||||
            Terminal terminal = (computer != null) ? computer.getTerminal() : null;
 | 
			
		||||
            Terminal terminal = computer != null ? computer.getTerminal() : null;
 | 
			
		||||
            if( terminal != null )
 | 
			
		||||
            {
 | 
			
		||||
                // Draw the terminal
 | 
			
		||||
@@ -455,18 +415,12 @@ public class WidgetTerminal extends Widget
 | 
			
		||||
    private void queueEvent( String event )
 | 
			
		||||
    {
 | 
			
		||||
        IComputer computer = m_computer.getComputer();
 | 
			
		||||
        if( computer != null )
 | 
			
		||||
        {
 | 
			
		||||
            computer.queueEvent( event );
 | 
			
		||||
        }
 | 
			
		||||
        if( computer != null ) computer.queueEvent( event );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void queueEvent( String event, Object[] args )
 | 
			
		||||
    {
 | 
			
		||||
        IComputer computer = m_computer.getComputer();
 | 
			
		||||
        if( computer != null )
 | 
			
		||||
        {
 | 
			
		||||
            computer.queueEvent( event, args );
 | 
			
		||||
        }
 | 
			
		||||
        if( computer != null ) computer.queueEvent( event, args );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,74 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.client.proxy;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
 | 
			
		||||
import dan200.computercraft.client.render.TurtleSmartItemModel;
 | 
			
		||||
import dan200.computercraft.shared.proxy.CCTurtleProxyCommon;
 | 
			
		||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
 | 
			
		||||
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
 | 
			
		||||
import net.minecraft.client.resources.IReloadableResourceManager;
 | 
			
		||||
import net.minecraft.client.resources.IResourceManager;
 | 
			
		||||
import net.minecraftforge.client.event.ModelBakeEvent;
 | 
			
		||||
import net.minecraftforge.common.MinecraftForge;
 | 
			
		||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
 | 
			
		||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
 | 
			
		||||
 | 
			
		||||
public class CCTurtleProxyClient extends CCTurtleProxyCommon
 | 
			
		||||
{
 | 
			
		||||
    @Override
 | 
			
		||||
    public void preInit()
 | 
			
		||||
    {
 | 
			
		||||
        super.preInit();
 | 
			
		||||
        MinecraftForge.EVENT_BUS.register( new ForgeHandlers() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void init()
 | 
			
		||||
    {
 | 
			
		||||
        super.init();
 | 
			
		||||
 | 
			
		||||
        // Setup turtle colours
 | 
			
		||||
        Minecraft.getMinecraft().getItemColors().registerItemColorHandler( ( stack, tintIndex ) -> {
 | 
			
		||||
            if( tintIndex == 0 )
 | 
			
		||||
            {
 | 
			
		||||
                ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
 | 
			
		||||
                int colour = turtle.getColour( stack );
 | 
			
		||||
                if( colour != -1 ) return colour;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return 0xFFFFFF;
 | 
			
		||||
        }, ComputerCraft.Blocks.turtle, ComputerCraft.Blocks.turtleExpanded, ComputerCraft.Blocks.turtleAdvanced );
 | 
			
		||||
 | 
			
		||||
        // Setup renderers
 | 
			
		||||
        ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static class ForgeHandlers
 | 
			
		||||
    {
 | 
			
		||||
        private final TurtleSmartItemModel m_turtleSmartItemModel = new TurtleSmartItemModel();
 | 
			
		||||
 | 
			
		||||
        ForgeHandlers()
 | 
			
		||||
        {
 | 
			
		||||
            IResourceManager resourceManager = Minecraft.getMinecraft().getResourceManager();
 | 
			
		||||
            if( resourceManager instanceof IReloadableResourceManager )
 | 
			
		||||
            {
 | 
			
		||||
                ((IReloadableResourceManager) resourceManager).registerReloadListener( m_turtleSmartItemModel );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @SubscribeEvent
 | 
			
		||||
        public void onModelBakeEvent( ModelBakeEvent event )
 | 
			
		||||
        {
 | 
			
		||||
            event.getModelRegistry().putObject( new ModelResourceLocation( "computercraft:turtle_dynamic", "inventory" ), m_turtleSmartItemModel );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -9,25 +9,19 @@ package dan200.computercraft.client.proxy;
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.client.render.TileEntityCableRenderer;
 | 
			
		||||
import dan200.computercraft.client.render.TileEntityMonitorRenderer;
 | 
			
		||||
import dan200.computercraft.client.render.TileEntityTurtleRenderer;
 | 
			
		||||
import dan200.computercraft.shared.command.CommandCopy;
 | 
			
		||||
import dan200.computercraft.shared.media.items.ItemDiskLegacy;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.modem.wired.TileCable;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.monitor.ClientMonitor;
 | 
			
		||||
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
 | 
			
		||||
import dan200.computercraft.shared.proxy.ComputerCraftProxyCommon;
 | 
			
		||||
import dan200.computercraft.shared.util.Colour;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.renderer.color.IItemColor;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import dan200.computercraft.shared.turtle.blocks.TileTurtle;
 | 
			
		||||
import net.minecraftforge.client.ClientCommandHandler;
 | 
			
		||||
import net.minecraftforge.event.world.WorldEvent;
 | 
			
		||||
import net.minecraftforge.fml.client.registry.ClientRegistry;
 | 
			
		||||
import net.minecraftforge.fml.common.Mod;
 | 
			
		||||
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
 | 
			
		||||
import net.minecraftforge.fml.relauncher.Side;
 | 
			
		||||
import net.minecraftforge.fml.relauncher.SideOnly;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
 | 
			
		||||
public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
 | 
			
		||||
{
 | 
			
		||||
@@ -45,40 +39,14 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
 | 
			
		||||
    {
 | 
			
		||||
        super.init();
 | 
			
		||||
 | 
			
		||||
        Minecraft mc = Minecraft.getMinecraft();
 | 
			
		||||
 | 
			
		||||
        // Setup
 | 
			
		||||
        mc.getItemColors().registerItemColorHandler( new DiskColorHandler( ComputerCraft.Items.disk ), ComputerCraft.Items.disk );
 | 
			
		||||
        mc.getItemColors().registerItemColorHandler( new DiskColorHandler( ComputerCraft.Items.diskExpanded ), ComputerCraft.Items.diskExpanded );
 | 
			
		||||
 | 
			
		||||
        mc.getItemColors().registerItemColorHandler( ( stack, layer ) -> {
 | 
			
		||||
            switch( layer )
 | 
			
		||||
            {
 | 
			
		||||
                case 0:
 | 
			
		||||
                default:
 | 
			
		||||
                    return 0xFFFFFF;
 | 
			
		||||
                case 1:
 | 
			
		||||
                {
 | 
			
		||||
                    // Frame colour
 | 
			
		||||
                    int colour = ComputerCraft.Items.pocketComputer.getColour( stack );
 | 
			
		||||
                    return colour == -1 ? 0xFFFFFF : colour;
 | 
			
		||||
                }
 | 
			
		||||
                case 2:
 | 
			
		||||
                {
 | 
			
		||||
                    // Light colour
 | 
			
		||||
                    int colour = ComputerCraft.Items.pocketComputer.getLightState( stack );
 | 
			
		||||
                    return colour == -1 ? Colour.Black.getHex() : colour;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }, ComputerCraft.Items.pocketComputer );
 | 
			
		||||
 | 
			
		||||
        // Setup renderers
 | 
			
		||||
        ClientRegistry.bindTileEntitySpecialRenderer( TileMonitor.class, new TileEntityMonitorRenderer() );
 | 
			
		||||
        ClientRegistry.bindTileEntitySpecialRenderer( TileCable.class, new TileEntityCableRenderer() );
 | 
			
		||||
        ClientRegistry.bindTileEntitySpecialRenderer( TileTurtle.class, new TileEntityTurtleRenderer() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
 | 
			
		||||
    public static class ForgeHandlers
 | 
			
		||||
    public static final class ForgeHandlers
 | 
			
		||||
    {
 | 
			
		||||
        @SubscribeEvent
 | 
			
		||||
        public static void onWorldUnload( WorldEvent.Unload event )
 | 
			
		||||
@@ -90,20 +58,5 @@ public class ComputerCraftProxyClient extends ComputerCraftProxyCommon
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @SideOnly( Side.CLIENT )
 | 
			
		||||
    private static class DiskColorHandler implements IItemColor
 | 
			
		||||
    {
 | 
			
		||||
        private final ItemDiskLegacy disk;
 | 
			
		||||
 | 
			
		||||
        private DiskColorHandler( ItemDiskLegacy disk )
 | 
			
		||||
        {
 | 
			
		||||
            this.disk = disk;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public int colorMultiplier( @Nonnull ItemStack stack, int layer )
 | 
			
		||||
        {
 | 
			
		||||
            return layer == 0 ? 0xFFFFFF : disk.getColour( stack );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -57,7 +57,7 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
 | 
			
		||||
    @Override
 | 
			
		||||
    protected void renderItem( ItemStack stack )
 | 
			
		||||
    {
 | 
			
		||||
        // Setup various transformations. Note that these are partially adapated from the corresponding method
 | 
			
		||||
        // Setup various transformations. Note that these are partially adapted from the corresponding method
 | 
			
		||||
        // in ItemRenderer
 | 
			
		||||
        GlStateManager.disableLighting();
 | 
			
		||||
 | 
			
		||||
@@ -65,8 +65,7 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
 | 
			
		||||
        GlStateManager.rotate( 180f, 0f, 0f, 1f );
 | 
			
		||||
        GlStateManager.scale( 0.5, 0.5, 0.5 );
 | 
			
		||||
 | 
			
		||||
        ItemPocketComputer pocketComputer = ComputerCraft.Items.pocketComputer;
 | 
			
		||||
        ClientComputer computer = pocketComputer.createClientComputer( stack );
 | 
			
		||||
        ClientComputer computer = ItemPocketComputer.createClientComputer( stack );
 | 
			
		||||
 | 
			
		||||
        {
 | 
			
		||||
            // First render the background item. We use the item's model rather than a direct texture as this ensures
 | 
			
		||||
@@ -90,9 +89,9 @@ public final class ItemPocketRenderer extends ItemMapLikeRenderer
 | 
			
		||||
            GlStateManager.blendFunc( GlStateManager.SourceFactor.SRC_ALPHA, GlStateManager.DestFactor.ONE_MINUS_SRC_ALPHA );
 | 
			
		||||
            GlStateManager.color( 1.0F, 1.0F, 1.0F, 1.0F );
 | 
			
		||||
 | 
			
		||||
            IBakedModel bakedmodel = renderItem.getItemModelWithOverrides( stack, null, null );
 | 
			
		||||
            bakedmodel = ForgeHooksClient.handleCameraTransforms( bakedmodel, ItemCameraTransforms.TransformType.GUI, false );
 | 
			
		||||
            renderItem.renderItem( stack, bakedmodel );
 | 
			
		||||
            IBakedModel baked = renderItem.getItemModelWithOverrides( stack, null, null );
 | 
			
		||||
            baked = ForgeHooksClient.handleCameraTransforms( baked, ItemCameraTransforms.TransformType.GUI, false );
 | 
			
		||||
            renderItem.renderItem( stack, baked );
 | 
			
		||||
 | 
			
		||||
            GlStateManager.disableAlpha();
 | 
			
		||||
            GlStateManager.disableRescaleNormal();
 | 
			
		||||
 
 | 
			
		||||
@@ -23,7 +23,7 @@ import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAG
 | 
			
		||||
import static dan200.computercraft.shared.media.items.ItemPrintout.LINE_MAX_LENGTH;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Emulates map and item-frame rendering for prinouts
 | 
			
		||||
 * Emulates map and item-frame rendering for printouts
 | 
			
		||||
 */
 | 
			
		||||
@Mod.EventBusSubscriber( modid = ComputerCraft.MOD_ID, value = Side.CLIENT )
 | 
			
		||||
public final class ItemPrintoutRenderer extends ItemMapLikeRenderer
 | 
			
		||||
 
 | 
			
		||||
@@ -180,7 +180,7 @@ public final class ModelTransformer
 | 
			
		||||
     *
 | 
			
		||||
     * This also provides the ability to swap vertices through {@link #swap(int, int)} to allow reordering.
 | 
			
		||||
     */
 | 
			
		||||
    private static class BakedQuadBuilder implements IVertexConsumer
 | 
			
		||||
    private static final class BakedQuadBuilder implements IVertexConsumer
 | 
			
		||||
    {
 | 
			
		||||
        private final VertexFormat format;
 | 
			
		||||
 | 
			
		||||
@@ -195,7 +195,7 @@ public final class ModelTransformer
 | 
			
		||||
        private BakedQuadBuilder( VertexFormat format )
 | 
			
		||||
        {
 | 
			
		||||
            this.format = format;
 | 
			
		||||
            this.vertexData = new int[format.getSize()];
 | 
			
		||||
            vertexData = new int[format.getSize()];
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nonnull
 | 
			
		||||
@@ -208,7 +208,7 @@ public final class ModelTransformer
 | 
			
		||||
        @Override
 | 
			
		||||
        public void setQuadTint( int tint )
 | 
			
		||||
        {
 | 
			
		||||
            this.quadTint = tint;
 | 
			
		||||
            quadTint = tint;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -12,6 +12,8 @@ import dan200.computercraft.shared.util.Palette;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.renderer.BufferBuilder;
 | 
			
		||||
import net.minecraft.client.renderer.GlStateManager;
 | 
			
		||||
import net.minecraft.client.renderer.GlStateManager.DestFactor;
 | 
			
		||||
import net.minecraft.client.renderer.GlStateManager.SourceFactor;
 | 
			
		||||
import net.minecraft.client.renderer.Tessellator;
 | 
			
		||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
 | 
			
		||||
import net.minecraft.util.ResourceLocation;
 | 
			
		||||
@@ -20,7 +22,7 @@ import org.lwjgl.opengl.GL11;
 | 
			
		||||
import static dan200.computercraft.client.gui.FixedWidthFontRenderer.FONT_HEIGHT;
 | 
			
		||||
import static dan200.computercraft.shared.media.items.ItemPrintout.LINES_PER_PAGE;
 | 
			
		||||
 | 
			
		||||
public class PrintoutRenderer
 | 
			
		||||
public final class PrintoutRenderer
 | 
			
		||||
{
 | 
			
		||||
    private static final ResourceLocation BG = new ResourceLocation( "computercraft", "textures/gui/printout.png" );
 | 
			
		||||
    private static final double BG_SIZE = 256.0;
 | 
			
		||||
@@ -58,6 +60,8 @@ public class PrintoutRenderer
 | 
			
		||||
    private static final int COVER_Y = Y_SIZE;
 | 
			
		||||
    private static final int COVER_X = X_SIZE + 4 * X_FOLD_SIZE;
 | 
			
		||||
 | 
			
		||||
    private PrintoutRenderer() {}
 | 
			
		||||
 | 
			
		||||
    public static void drawText( int x, int y, int start, TextBuffer[] text, TextBuffer[] colours )
 | 
			
		||||
    {
 | 
			
		||||
        FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
 | 
			
		||||
@@ -73,6 +77,7 @@ public class PrintoutRenderer
 | 
			
		||||
        GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
 | 
			
		||||
        GlStateManager.enableBlend();
 | 
			
		||||
        GlStateManager.enableTexture2D();
 | 
			
		||||
        GlStateManager.tryBlendFuncSeparate( SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ZERO );
 | 
			
		||||
 | 
			
		||||
        FixedWidthFontRenderer fontRenderer = FixedWidthFontRenderer.instance();
 | 
			
		||||
 | 
			
		||||
@@ -87,6 +92,7 @@ public class PrintoutRenderer
 | 
			
		||||
        GlStateManager.color( 1.0f, 1.0f, 1.0f, 1.0f );
 | 
			
		||||
        GlStateManager.enableBlend();
 | 
			
		||||
        GlStateManager.enableTexture2D();
 | 
			
		||||
        GlStateManager.tryBlendFuncSeparate( SourceFactor.SRC_ALPHA, DestFactor.ONE_MINUS_SRC_ALPHA, SourceFactor.ONE, DestFactor.ZERO );
 | 
			
		||||
 | 
			
		||||
        Minecraft.getMinecraft().getTextureManager().bindTexture( BG );
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -53,7 +53,7 @@ public final class RenderOverlayCable
 | 
			
		||||
        state = state.getActualState( world, pos );
 | 
			
		||||
 | 
			
		||||
        event.setCanceled( true );
 | 
			
		||||
        PeripheralType type = ComputerCraft.Blocks.cable.getPeripheralType( state );
 | 
			
		||||
        PeripheralType type = BlockCable.getPeripheralType( state );
 | 
			
		||||
 | 
			
		||||
        GlStateManager.enableBlend();
 | 
			
		||||
        GlStateManager.tryBlendFuncSeparate( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, 1, 0 );
 | 
			
		||||
 
 | 
			
		||||
@@ -61,11 +61,11 @@ public class TileEntityCableRenderer extends TileEntitySpecialRenderer<TileCable
 | 
			
		||||
        state = state.getActualState( world, pos );
 | 
			
		||||
        if( te.getPeripheralType() != PeripheralType.Cable && WorldUtil.isVecInsideInclusive( CableBounds.getModemBounds( state ), hit.hitVec.subtract( pos.getX(), pos.getY(), pos.getZ() ) ) )
 | 
			
		||||
        {
 | 
			
		||||
            state = block.getDefaultState().withProperty( BlockCable.Properties.MODEM, state.getValue( BlockCable.Properties.MODEM ) );
 | 
			
		||||
            state = block.getDefaultState().withProperty( BlockCable.MODEM, state.getValue( BlockCable.MODEM ) );
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            state = state.withProperty( BlockCable.Properties.MODEM, BlockCableModemVariant.None );
 | 
			
		||||
            state = state.withProperty( BlockCable.MODEM, BlockCableModemVariant.None );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        IBakedModel model = mc.getBlockRendererDispatcher().getModelForState( state );
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void renderMonitorAt( TileMonitor monitor, double posX, double posY, double posZ, float f, int i )
 | 
			
		||||
    private static void renderMonitorAt( TileMonitor monitor, double posX, double posY, double posZ, float f, int i )
 | 
			
		||||
    {
 | 
			
		||||
        // Render from the origin monitor
 | 
			
		||||
        ClientMonitor originTerminal = monitor.getClientMonitor();
 | 
			
		||||
@@ -78,7 +78,7 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
 | 
			
		||||
            GlStateManager.rotate( pitch, 1.0f, 0.0f, 0.0f );
 | 
			
		||||
            GlStateManager.translate(
 | 
			
		||||
                -0.5 + TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN,
 | 
			
		||||
                (origin.getHeight() - 0.5) - (TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN),
 | 
			
		||||
                origin.getHeight() - 0.5 - (TileMonitor.RENDER_BORDER + TileMonitor.RENDER_MARGIN),
 | 
			
		||||
                0.5
 | 
			
		||||
            );
 | 
			
		||||
            double xSize = origin.getWidth() - 2.0 * (TileMonitor.RENDER_MARGIN + TileMonitor.RENDER_BORDER);
 | 
			
		||||
 
 | 
			
		||||
@@ -14,23 +14,20 @@ import dan200.computercraft.shared.util.Holiday;
 | 
			
		||||
import dan200.computercraft.shared.util.HolidayUtil;
 | 
			
		||||
import net.minecraft.block.state.IBlockState;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.gui.FontRenderer;
 | 
			
		||||
import net.minecraft.client.renderer.BufferBuilder;
 | 
			
		||||
import net.minecraft.client.renderer.EntityRenderer;
 | 
			
		||||
import net.minecraft.client.renderer.GlStateManager;
 | 
			
		||||
import net.minecraft.client.renderer.Tessellator;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.BakedQuad;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.IBakedModel;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.ModelManager;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
 | 
			
		||||
import net.minecraft.client.renderer.entity.RenderManager;
 | 
			
		||||
import net.minecraft.client.renderer.texture.TextureMap;
 | 
			
		||||
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
 | 
			
		||||
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
 | 
			
		||||
import net.minecraft.client.renderer.vertex.VertexFormat;
 | 
			
		||||
import net.minecraft.util.EnumFacing;
 | 
			
		||||
import net.minecraft.util.ResourceLocation;
 | 
			
		||||
import net.minecraft.util.math.BlockPos;
 | 
			
		||||
import net.minecraft.util.math.RayTraceResult;
 | 
			
		||||
import net.minecraft.util.math.Vec3d;
 | 
			
		||||
import net.minecraftforge.client.ForgeHooksClient;
 | 
			
		||||
import net.minecraftforge.client.model.pipeline.LightUtil;
 | 
			
		||||
@@ -48,9 +45,9 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
    private static final ModelResourceLocation ELF_OVERLAY_MODEL = new ModelResourceLocation( "computercraft:turtle_elf_overlay", "inventory" );
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void render( TileTurtle tileEntity, double posX, double posY, double posZ, float f, int i, float f2 )
 | 
			
		||||
    public void render( TileTurtle tileEntity, double posX, double posY, double posZ, float partialTicks, int breaking, float f2 )
 | 
			
		||||
    {
 | 
			
		||||
        if( tileEntity != null ) renderTurtleAt( tileEntity, posX, posY, posZ, f, i );
 | 
			
		||||
        if( tileEntity != null ) renderTurtleAt( tileEntity, posX, posY, posZ, partialTicks );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static ModelResourceLocation getTurtleModel( ComputerFamily family, boolean coloured )
 | 
			
		||||
@@ -65,7 +62,7 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static ModelResourceLocation getTurtleOverlayModel( ComputerFamily family, ResourceLocation overlay, boolean christmas )
 | 
			
		||||
    public static ModelResourceLocation getTurtleOverlayModel( ResourceLocation overlay, boolean christmas )
 | 
			
		||||
    {
 | 
			
		||||
        if( overlay != null )
 | 
			
		||||
        {
 | 
			
		||||
@@ -81,26 +78,30 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double posZ, float f, int i )
 | 
			
		||||
    private void renderTurtleAt( TileTurtle turtle, double posX, double posY, double posZ, float partialTicks )
 | 
			
		||||
    {
 | 
			
		||||
        IBlockState state = turtle.getWorld().getBlockState( turtle.getPos() );
 | 
			
		||||
        // Render the label
 | 
			
		||||
        String label = turtle.createProxy().getLabel();
 | 
			
		||||
        if( label != null && rendererDispatcher.cameraHitResult != null && turtle.getPos().equals( rendererDispatcher.cameraHitResult.getBlockPos() ) )
 | 
			
		||||
        {
 | 
			
		||||
            setLightmapDisabled( true );
 | 
			
		||||
            EntityRenderer.drawNameplate(
 | 
			
		||||
                getFontRenderer(), label,
 | 
			
		||||
                (float) posX + 0.5F, (float) posY + 1.2F, (float) posZ + 0.5F, 0,
 | 
			
		||||
                rendererDispatcher.entityYaw, rendererDispatcher.entityPitch, false, false
 | 
			
		||||
            );
 | 
			
		||||
            setLightmapDisabled( false );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        GlStateManager.pushMatrix();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            IBlockState state = turtle.getWorld().getBlockState( turtle.getPos() );
 | 
			
		||||
            // Setup the transform
 | 
			
		||||
            Vec3d offset;
 | 
			
		||||
            float yaw;
 | 
			
		||||
            offset = turtle.getRenderOffset( f );
 | 
			
		||||
            yaw = turtle.getRenderYaw( f );
 | 
			
		||||
            Vec3d offset = turtle.getRenderOffset( partialTicks );
 | 
			
		||||
            float yaw = turtle.getRenderYaw( partialTicks );
 | 
			
		||||
            GlStateManager.translate( posX + offset.x, posY + offset.y, posZ + offset.z );
 | 
			
		||||
 | 
			
		||||
            // Render the label
 | 
			
		||||
            String label = turtle.createProxy().getLabel();
 | 
			
		||||
            if( label != null )
 | 
			
		||||
            {
 | 
			
		||||
                renderLabel( turtle.getAccess().getPosition(), label );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Render the turtle
 | 
			
		||||
            GlStateManager.translate( 0.5f, 0.5f, 0.5f );
 | 
			
		||||
            GlStateManager.rotate( 180.0f - yaw, 0.0f, 1.0f, 0.0f );
 | 
			
		||||
@@ -112,18 +113,14 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
            }
 | 
			
		||||
            GlStateManager.translate( -0.5f, -0.5f, -0.5f );
 | 
			
		||||
            // Render the turtle
 | 
			
		||||
            int colour;
 | 
			
		||||
            ComputerFamily family;
 | 
			
		||||
            ResourceLocation overlay;
 | 
			
		||||
            colour = turtle.getColour();
 | 
			
		||||
            family = turtle.getFamily();
 | 
			
		||||
            overlay = turtle.getOverlay();
 | 
			
		||||
            int colour = turtle.getColour();
 | 
			
		||||
            ComputerFamily family = turtle.getFamily();
 | 
			
		||||
            ResourceLocation overlay = turtle.getOverlay();
 | 
			
		||||
 | 
			
		||||
            renderModel( state, getTurtleModel( family, colour != -1 ), colour == -1 ? null : new int[] { colour } );
 | 
			
		||||
 | 
			
		||||
            // Render the overlay
 | 
			
		||||
            ModelResourceLocation overlayModel = getTurtleOverlayModel(
 | 
			
		||||
                family,
 | 
			
		||||
                overlay,
 | 
			
		||||
                HolidayUtil.getCurrentHoliday() == Holiday.Christmas
 | 
			
		||||
            );
 | 
			
		||||
@@ -144,8 +141,8 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Render the upgrades
 | 
			
		||||
            renderUpgrade( state, turtle, TurtleSide.Left, f );
 | 
			
		||||
            renderUpgrade( state, turtle, TurtleSide.Right, f );
 | 
			
		||||
            renderUpgrade( state, turtle, TurtleSide.Left, partialTicks );
 | 
			
		||||
            renderUpgrade( state, turtle, TurtleSide.Right, partialTicks );
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
@@ -154,7 +151,7 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void renderUpgrade( IBlockState state, TileTurtle turtle, TurtleSide side, float f )
 | 
			
		||||
    private static void renderUpgrade( IBlockState state, TileTurtle turtle, TurtleSide side, float f )
 | 
			
		||||
    {
 | 
			
		||||
        ITurtleUpgrade upgrade = turtle.getUpgrade( side );
 | 
			
		||||
        if( upgrade != null )
 | 
			
		||||
@@ -187,14 +184,14 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void renderModel( IBlockState state, ModelResourceLocation modelLocation, int[] tints )
 | 
			
		||||
    private static void renderModel( IBlockState state, ModelResourceLocation modelLocation, int[] tints )
 | 
			
		||||
    {
 | 
			
		||||
        Minecraft mc = Minecraft.getMinecraft();
 | 
			
		||||
        ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
 | 
			
		||||
        renderModel( state, modelManager.getModel( modelLocation ), tints );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void renderModel( IBlockState state, IBakedModel model, int[] tints )
 | 
			
		||||
    private static void renderModel( IBlockState state, IBakedModel model, int[] tints )
 | 
			
		||||
    {
 | 
			
		||||
        Minecraft mc = Minecraft.getMinecraft();
 | 
			
		||||
        Tessellator tessellator = Tessellator.getInstance();
 | 
			
		||||
@@ -206,7 +203,7 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void renderQuads( Tessellator tessellator, List<BakedQuad> quads, int[] tints )
 | 
			
		||||
    private static void renderQuads( Tessellator tessellator, List<BakedQuad> quads, int[] tints )
 | 
			
		||||
    {
 | 
			
		||||
        BufferBuilder buffer = tessellator.getBuffer();
 | 
			
		||||
        VertexFormat format = DefaultVertexFormats.ITEM;
 | 
			
		||||
@@ -232,72 +229,4 @@ public class TileEntityTurtleRenderer extends TileEntitySpecialRenderer<TileTurt
 | 
			
		||||
        }
 | 
			
		||||
        tessellator.draw();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void renderLabel( BlockPos position, String label )
 | 
			
		||||
    {
 | 
			
		||||
        Minecraft mc = Minecraft.getMinecraft();
 | 
			
		||||
        RayTraceResult mop = mc.objectMouseOver;
 | 
			
		||||
        if( mop != null && mop.typeOfHit == RayTraceResult.Type.BLOCK && mop.getBlockPos().equals( position ) )
 | 
			
		||||
        {
 | 
			
		||||
            RenderManager renderManager = mc.getRenderManager();
 | 
			
		||||
            FontRenderer fontrenderer = renderManager.getFontRenderer();
 | 
			
		||||
            float scale = 0.016666668F * 1.6f;
 | 
			
		||||
 | 
			
		||||
            GlStateManager.pushMatrix();
 | 
			
		||||
            GlStateManager.disableLighting();
 | 
			
		||||
            GlStateManager.enableBlend();
 | 
			
		||||
            GlStateManager.blendFunc( GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA );
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                GlStateManager.translate( 0.5f, 1.25f, 0.5f );
 | 
			
		||||
                GlStateManager.rotate( -renderManager.playerViewY, 0.0F, 1.0F, 0.0F );
 | 
			
		||||
                GlStateManager.rotate( renderManager.playerViewX, 1.0F, 0.0F, 0.0F );
 | 
			
		||||
                GlStateManager.scale( -scale, -scale, scale );
 | 
			
		||||
 | 
			
		||||
                int yOffset = 0;
 | 
			
		||||
                int xOffset = fontrenderer.getStringWidth( label ) / 2;
 | 
			
		||||
 | 
			
		||||
                // Draw background
 | 
			
		||||
                GlStateManager.depthMask( false );
 | 
			
		||||
                GlStateManager.disableDepth();
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    // Quad
 | 
			
		||||
                    GlStateManager.disableTexture2D();
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        Tessellator tessellator = Tessellator.getInstance();
 | 
			
		||||
                        BufferBuilder renderer = tessellator.getBuffer();
 | 
			
		||||
                        renderer.begin( GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR );
 | 
			
		||||
                        renderer.pos( -xOffset - 1, -1 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
 | 
			
		||||
                        renderer.pos( -xOffset - 1, 8 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
 | 
			
		||||
                        renderer.pos( xOffset + 1, 8 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
 | 
			
		||||
                        renderer.pos( xOffset + 1, -1 + yOffset, 0.0D ).color( 0.0F, 0.0F, 0.0F, 0.25F ).endVertex();
 | 
			
		||||
                        tessellator.draw();
 | 
			
		||||
                    }
 | 
			
		||||
                    finally
 | 
			
		||||
                    {
 | 
			
		||||
                        GlStateManager.enableTexture2D();
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Text
 | 
			
		||||
                    fontrenderer.drawString( label, -fontrenderer.getStringWidth( label ) / 2, yOffset, 0x20ffffff );
 | 
			
		||||
                }
 | 
			
		||||
                finally
 | 
			
		||||
                {
 | 
			
		||||
                    GlStateManager.enableDepth();
 | 
			
		||||
                    GlStateManager.depthMask( true );
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // Draw foreground text
 | 
			
		||||
                fontrenderer.drawString( label, -fontrenderer.getStringWidth( label ) / 2, yOffset, -1 );
 | 
			
		||||
            }
 | 
			
		||||
            finally
 | 
			
		||||
            {
 | 
			
		||||
                GlStateManager.disableBlend();
 | 
			
		||||
                GlStateManager.enableLighting();
 | 
			
		||||
                GlStateManager.popMatrix();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,121 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.client.render;
 | 
			
		||||
 | 
			
		||||
import com.google.common.collect.ImmutableMap;
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.IBakedModel;
 | 
			
		||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
 | 
			
		||||
import net.minecraft.client.renderer.vertex.VertexFormat;
 | 
			
		||||
import net.minecraft.client.resources.IResourceManager;
 | 
			
		||||
import net.minecraft.util.ResourceLocation;
 | 
			
		||||
import net.minecraftforge.client.model.ICustomModelLoader;
 | 
			
		||||
import net.minecraftforge.client.model.IModel;
 | 
			
		||||
import net.minecraftforge.client.model.ModelLoaderRegistry;
 | 
			
		||||
import net.minecraftforge.common.model.IModelState;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.function.Function;
 | 
			
		||||
 | 
			
		||||
public final class TurtleModelLoader implements ICustomModelLoader
 | 
			
		||||
{
 | 
			
		||||
    private static final ResourceLocation NORMAL_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle" );
 | 
			
		||||
    private static final ResourceLocation ADVANCED_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/advanced_turtle" );
 | 
			
		||||
    private static final ResourceLocation COLOUR_TURTLE_MODEL = new ResourceLocation( ComputerCraft.MOD_ID, "block/turtle_white" );
 | 
			
		||||
 | 
			
		||||
    public static final TurtleModelLoader INSTANCE = new TurtleModelLoader();
 | 
			
		||||
 | 
			
		||||
    private TurtleModelLoader()
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onResourceManagerReload( @Nonnull IResourceManager manager )
 | 
			
		||||
    {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean accepts( @Nonnull ResourceLocation name )
 | 
			
		||||
    {
 | 
			
		||||
        return name.getNamespace().equals( ComputerCraft.MOD_ID )
 | 
			
		||||
            && (name.getPath().equals( "turtle" ) || name.getPath().equals( "turtle_advanced" ));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public IModel loadModel( @Nonnull ResourceLocation name ) throws Exception
 | 
			
		||||
    {
 | 
			
		||||
        if( name.getNamespace().equals( ComputerCraft.MOD_ID ) )
 | 
			
		||||
        {
 | 
			
		||||
            IModel colourModel = ModelLoaderRegistry.getModel( COLOUR_TURTLE_MODEL );
 | 
			
		||||
            switch( name.getPath() )
 | 
			
		||||
            {
 | 
			
		||||
                case "turtle":
 | 
			
		||||
                    return new TurtleModel( ModelLoaderRegistry.getModel( NORMAL_TURTLE_MODEL ), colourModel );
 | 
			
		||||
                case "turtle_advanced":
 | 
			
		||||
                    return new TurtleModel( ModelLoaderRegistry.getModel( ADVANCED_TURTLE_MODEL ), colourModel );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new IllegalStateException( "Loader does not accept " + name );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static final class TurtleModel implements IModel
 | 
			
		||||
    {
 | 
			
		||||
        private final IModel family;
 | 
			
		||||
        private final IModel colour;
 | 
			
		||||
 | 
			
		||||
        private TurtleModel( IModel family, IModel colour )
 | 
			
		||||
        {
 | 
			
		||||
            this.family = family;
 | 
			
		||||
            this.colour = colour;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nonnull
 | 
			
		||||
        @Override
 | 
			
		||||
        public IBakedModel bake( @Nonnull IModelState state, @Nonnull VertexFormat format, @Nonnull Function<ResourceLocation, TextureAtlasSprite> function )
 | 
			
		||||
        {
 | 
			
		||||
            return new TurtleSmartItemModel(
 | 
			
		||||
                family.bake( state, format, function ),
 | 
			
		||||
                colour.bake( state, format, function )
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private TurtleModel copy( IModel family, IModel colour )
 | 
			
		||||
        {
 | 
			
		||||
            return this.family == family && this.colour == colour ? this : new TurtleModel( family, colour );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nonnull
 | 
			
		||||
        @Override
 | 
			
		||||
        public IModel smoothLighting( boolean value )
 | 
			
		||||
        {
 | 
			
		||||
            return copy( family.smoothLighting( value ), colour.smoothLighting( value ) );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nonnull
 | 
			
		||||
        @Override
 | 
			
		||||
        public IModel gui3d( boolean value )
 | 
			
		||||
        {
 | 
			
		||||
            return copy( family.gui3d( value ), colour.gui3d( value ) );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nonnull
 | 
			
		||||
        @Override
 | 
			
		||||
        public IModel uvlock( boolean value )
 | 
			
		||||
        {
 | 
			
		||||
            return copy( family.uvlock( value ), colour.uvlock( value ) );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Nonnull
 | 
			
		||||
        @Override
 | 
			
		||||
        public IModel retexture( ImmutableMap<String, String> textures )
 | 
			
		||||
        {
 | 
			
		||||
            return copy( family.retexture( textures ), colour.retexture( textures ) );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -17,7 +17,7 @@ import net.minecraft.util.EnumFacing;
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.vecmath.Matrix4f;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.EnumMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
 | 
			
		||||
@@ -30,8 +30,8 @@ public class TurtleMultiModel implements IBakedModel
 | 
			
		||||
    private final Matrix4f m_leftUpgradeTransform;
 | 
			
		||||
    private final IBakedModel m_rightUpgradeModel;
 | 
			
		||||
    private final Matrix4f m_rightUpgradeTransform;
 | 
			
		||||
    private List<BakedQuad> m_generalQuads;
 | 
			
		||||
    private Map<EnumFacing, List<BakedQuad>> m_faceQuads;
 | 
			
		||||
    private List<BakedQuad> m_generalQuads = null;
 | 
			
		||||
    private Map<EnumFacing, List<BakedQuad>> m_faceQuads = new EnumMap<>( EnumFacing.class );
 | 
			
		||||
 | 
			
		||||
    public TurtleMultiModel( IBakedModel baseModel, IBakedModel overlayModel, Matrix4f generalTransform, IBakedModel leftUpgradeModel, Matrix4f leftUpgradeTransform, IBakedModel rightUpgradeModel, Matrix4f rightUpgradeTransform )
 | 
			
		||||
    {
 | 
			
		||||
@@ -43,8 +43,6 @@ public class TurtleMultiModel implements IBakedModel
 | 
			
		||||
        m_rightUpgradeModel = rightUpgradeModel;
 | 
			
		||||
        m_rightUpgradeTransform = rightUpgradeTransform;
 | 
			
		||||
        m_generalTransform = generalTransform;
 | 
			
		||||
        m_generalQuads = null;
 | 
			
		||||
        m_faceQuads = new HashMap<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,6 @@ package dan200.computercraft.client.render;
 | 
			
		||||
import com.google.common.base.Objects;
 | 
			
		||||
import dan200.computercraft.api.turtle.ITurtleUpgrade;
 | 
			
		||||
import dan200.computercraft.api.turtle.TurtleSide;
 | 
			
		||||
import dan200.computercraft.shared.computer.core.ComputerFamily;
 | 
			
		||||
import dan200.computercraft.shared.turtle.items.ItemTurtleBase;
 | 
			
		||||
import dan200.computercraft.shared.util.Holiday;
 | 
			
		||||
import dan200.computercraft.shared.util.HolidayUtil;
 | 
			
		||||
@@ -17,15 +16,11 @@ import net.minecraft.block.state.IBlockState;
 | 
			
		||||
import net.minecraft.client.Minecraft;
 | 
			
		||||
import net.minecraft.client.renderer.block.model.*;
 | 
			
		||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
 | 
			
		||||
import net.minecraft.client.resources.IResourceManager;
 | 
			
		||||
import net.minecraft.entity.EntityLivingBase;
 | 
			
		||||
import net.minecraft.item.ItemStack;
 | 
			
		||||
import net.minecraft.util.EnumFacing;
 | 
			
		||||
import net.minecraft.util.ResourceLocation;
 | 
			
		||||
import net.minecraft.world.World;
 | 
			
		||||
import net.minecraftforge.client.resource.IResourceType;
 | 
			
		||||
import net.minecraftforge.client.resource.ISelectiveResourceReloadListener;
 | 
			
		||||
import net.minecraftforge.client.resource.VanillaResourceType;
 | 
			
		||||
import org.apache.commons.lang3.tuple.Pair;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
@@ -34,9 +29,8 @@ import javax.vecmath.Matrix4f;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.function.Predicate;
 | 
			
		||||
 | 
			
		||||
public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceReloadListener
 | 
			
		||||
public class TurtleSmartItemModel implements IBakedModel
 | 
			
		||||
{
 | 
			
		||||
    private static final Matrix4f s_identity, s_flip;
 | 
			
		||||
 | 
			
		||||
@@ -53,17 +47,15 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
 | 
			
		||||
    private static class TurtleModelCombination
 | 
			
		||||
    {
 | 
			
		||||
        public final ComputerFamily m_family;
 | 
			
		||||
        public final boolean m_colour;
 | 
			
		||||
        public final ITurtleUpgrade m_leftUpgrade;
 | 
			
		||||
        public final ITurtleUpgrade m_rightUpgrade;
 | 
			
		||||
        public final ResourceLocation m_overlay;
 | 
			
		||||
        public final boolean m_christmas;
 | 
			
		||||
        public final boolean m_flip;
 | 
			
		||||
        final boolean m_colour;
 | 
			
		||||
        final ITurtleUpgrade m_leftUpgrade;
 | 
			
		||||
        final ITurtleUpgrade m_rightUpgrade;
 | 
			
		||||
        final ResourceLocation m_overlay;
 | 
			
		||||
        final boolean m_christmas;
 | 
			
		||||
        final boolean m_flip;
 | 
			
		||||
 | 
			
		||||
        public TurtleModelCombination( ComputerFamily family, boolean colour, ITurtleUpgrade leftUpgrade, ITurtleUpgrade rightUpgrade, ResourceLocation overlay, boolean christmas, boolean flip )
 | 
			
		||||
        TurtleModelCombination( boolean colour, ITurtleUpgrade leftUpgrade, ITurtleUpgrade rightUpgrade, ResourceLocation overlay, boolean christmas, boolean flip )
 | 
			
		||||
        {
 | 
			
		||||
            m_family = family;
 | 
			
		||||
            m_colour = colour;
 | 
			
		||||
            m_leftUpgrade = leftUpgrade;
 | 
			
		||||
            m_rightUpgrade = rightUpgrade;
 | 
			
		||||
@@ -79,8 +71,7 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
            if( !(other instanceof TurtleModelCombination) ) return false;
 | 
			
		||||
 | 
			
		||||
            TurtleModelCombination otherCombo = (TurtleModelCombination) other;
 | 
			
		||||
            return otherCombo.m_family == m_family &&
 | 
			
		||||
                otherCombo.m_colour == m_colour &&
 | 
			
		||||
            return otherCombo.m_colour == m_colour &&
 | 
			
		||||
                otherCombo.m_leftUpgrade == m_leftUpgrade &&
 | 
			
		||||
                otherCombo.m_rightUpgrade == m_rightUpgrade &&
 | 
			
		||||
                Objects.equal( otherCombo.m_overlay, m_overlay ) &&
 | 
			
		||||
@@ -92,8 +83,7 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
        public int hashCode()
 | 
			
		||||
        {
 | 
			
		||||
            final int prime = 31;
 | 
			
		||||
            int result = 1;
 | 
			
		||||
            result = prime * result + m_family.hashCode();
 | 
			
		||||
            int result = 0;
 | 
			
		||||
            result = prime * result + (m_colour ? 1 : 0);
 | 
			
		||||
            result = prime * result + (m_leftUpgrade != null ? m_leftUpgrade.hashCode() : 0);
 | 
			
		||||
            result = prime * result + (m_rightUpgrade != null ? m_rightUpgrade.hashCode() : 0);
 | 
			
		||||
@@ -104,14 +94,18 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private final IBakedModel familyModel;
 | 
			
		||||
    private final IBakedModel colourModel;
 | 
			
		||||
 | 
			
		||||
    private HashMap<TurtleModelCombination, IBakedModel> m_cachedModels;
 | 
			
		||||
    private ItemOverrideList m_overrides;
 | 
			
		||||
    private final TurtleModelCombination m_defaultCombination;
 | 
			
		||||
 | 
			
		||||
    public TurtleSmartItemModel()
 | 
			
		||||
    public TurtleSmartItemModel( IBakedModel familyModel, IBakedModel colourModel )
 | 
			
		||||
    {
 | 
			
		||||
        this.familyModel = familyModel;
 | 
			
		||||
        this.colourModel = colourModel;
 | 
			
		||||
 | 
			
		||||
        m_cachedModels = new HashMap<>();
 | 
			
		||||
        m_defaultCombination = new TurtleModelCombination( ComputerFamily.Normal, false, null, null, null, false, false );
 | 
			
		||||
        m_overrides = new ItemOverrideList( new ArrayList<>() )
 | 
			
		||||
        {
 | 
			
		||||
            @Nonnull
 | 
			
		||||
@@ -119,7 +113,6 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
            public IBakedModel handleItemState( @Nonnull IBakedModel originalModel, @Nonnull ItemStack stack, @Nullable World world, @Nullable EntityLivingBase entity )
 | 
			
		||||
            {
 | 
			
		||||
                ItemTurtleBase turtle = (ItemTurtleBase) stack.getItem();
 | 
			
		||||
                ComputerFamily family = turtle.getFamily( stack );
 | 
			
		||||
                int colour = turtle.getColour( stack );
 | 
			
		||||
                ITurtleUpgrade leftUpgrade = turtle.getUpgrade( stack, TurtleSide.Left );
 | 
			
		||||
                ITurtleUpgrade rightUpgrade = turtle.getUpgrade( stack, TurtleSide.Right );
 | 
			
		||||
@@ -127,17 +120,11 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
                boolean christmas = HolidayUtil.getCurrentHoliday() == Holiday.Christmas;
 | 
			
		||||
                String label = turtle.getLabel( stack );
 | 
			
		||||
                boolean flip = label != null && (label.equals( "Dinnerbone" ) || label.equals( "Grumm" ));
 | 
			
		||||
                TurtleModelCombination combo = new TurtleModelCombination( family, colour != -1, leftUpgrade, rightUpgrade, overlay, christmas, flip );
 | 
			
		||||
                if( m_cachedModels.containsKey( combo ) )
 | 
			
		||||
                {
 | 
			
		||||
                    return m_cachedModels.get( combo );
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    IBakedModel model = buildModel( combo );
 | 
			
		||||
                    m_cachedModels.put( combo, model );
 | 
			
		||||
                    return model;
 | 
			
		||||
                }
 | 
			
		||||
                TurtleModelCombination combo = new TurtleModelCombination( colour != -1, leftUpgrade, rightUpgrade, overlay, christmas, flip );
 | 
			
		||||
 | 
			
		||||
                IBakedModel model = m_cachedModels.get( combo );
 | 
			
		||||
                if( model == null ) m_cachedModels.put( combo, model = buildModel( combo ) );
 | 
			
		||||
                return model;
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
@@ -149,23 +136,17 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
        return m_overrides;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void onResourceManagerReload( @Nonnull IResourceManager resourceManager, @Nonnull Predicate<IResourceType> resourcePredicate )
 | 
			
		||||
    {
 | 
			
		||||
        if( resourcePredicate.test( VanillaResourceType.MODELS ) ) m_cachedModels.clear();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private IBakedModel buildModel( TurtleModelCombination combo )
 | 
			
		||||
    {
 | 
			
		||||
        Minecraft mc = Minecraft.getMinecraft();
 | 
			
		||||
        ModelManager modelManager = mc.getRenderItem().getItemModelMesher().getModelManager();
 | 
			
		||||
        ModelResourceLocation baseModelLocation = TileEntityTurtleRenderer.getTurtleModel( combo.m_family, combo.m_colour );
 | 
			
		||||
        ModelResourceLocation overlayModelLocation = TileEntityTurtleRenderer.getTurtleOverlayModel( combo.m_family, combo.m_overlay, combo.m_christmas );
 | 
			
		||||
        IBakedModel baseModel = modelManager.getModel( baseModelLocation );
 | 
			
		||||
        IBakedModel overlayModel = (overlayModelLocation != null) ? modelManager.getModel( overlayModelLocation ) : null;
 | 
			
		||||
        ModelResourceLocation overlayModelLocation = TileEntityTurtleRenderer.getTurtleOverlayModel( combo.m_overlay, combo.m_christmas );
 | 
			
		||||
 | 
			
		||||
        IBakedModel baseModel = combo.m_colour ? colourModel : familyModel;
 | 
			
		||||
        IBakedModel overlayModel = overlayModelLocation != null ? modelManager.getModel( overlayModelLocation ) : null;
 | 
			
		||||
        Matrix4f transform = combo.m_flip ? s_flip : s_identity;
 | 
			
		||||
        Pair<IBakedModel, Matrix4f> leftModel = (combo.m_leftUpgrade != null) ? combo.m_leftUpgrade.getModel( null, TurtleSide.Left ) : null;
 | 
			
		||||
        Pair<IBakedModel, Matrix4f> rightModel = (combo.m_rightUpgrade != null) ? combo.m_rightUpgrade.getModel( null, TurtleSide.Right ) : null;
 | 
			
		||||
        Pair<IBakedModel, Matrix4f> leftModel = combo.m_leftUpgrade != null ? combo.m_leftUpgrade.getModel( null, TurtleSide.Left ) : null;
 | 
			
		||||
        Pair<IBakedModel, Matrix4f> rightModel = combo.m_rightUpgrade != null ? combo.m_rightUpgrade.getModel( null, TurtleSide.Right ) : null;
 | 
			
		||||
        if( leftModel != null && rightModel != null )
 | 
			
		||||
        {
 | 
			
		||||
            return new TurtleMultiModel( baseModel, overlayModel, transform, leftModel.getLeft(), leftModel.getRight(), rightModel.getLeft(), rightModel.getRight() );
 | 
			
		||||
@@ -184,38 +165,36 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // These should not be called:
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public List<BakedQuad> getQuads( IBlockState state, EnumFacing facing, long rand )
 | 
			
		||||
    {
 | 
			
		||||
        return getDefaultModel().getQuads( state, facing, rand );
 | 
			
		||||
        return familyModel.getQuads( state, facing, rand );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isAmbientOcclusion()
 | 
			
		||||
    {
 | 
			
		||||
        return getDefaultModel().isAmbientOcclusion();
 | 
			
		||||
        return familyModel.isAmbientOcclusion();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isGui3d()
 | 
			
		||||
    {
 | 
			
		||||
        return getDefaultModel().isGui3d();
 | 
			
		||||
        return familyModel.isGui3d();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isBuiltInRenderer()
 | 
			
		||||
    {
 | 
			
		||||
        return getDefaultModel().isBuiltInRenderer();
 | 
			
		||||
        return familyModel.isBuiltInRenderer();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public TextureAtlasSprite getParticleTexture()
 | 
			
		||||
    {
 | 
			
		||||
        return getDefaultModel().getParticleTexture();
 | 
			
		||||
        return familyModel.getParticleTexture();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
@@ -223,18 +202,7 @@ public class TurtleSmartItemModel implements IBakedModel, ISelectiveResourceRelo
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public ItemCameraTransforms getItemCameraTransforms()
 | 
			
		||||
    {
 | 
			
		||||
        return getDefaultModel().getItemCameraTransforms();
 | 
			
		||||
        return familyModel.getItemCameraTransforms();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private IBakedModel getDefaultModel()
 | 
			
		||||
    {
 | 
			
		||||
        IBakedModel model = m_cachedModels.get( m_defaultCombination );
 | 
			
		||||
        if( model == null )
 | 
			
		||||
        {
 | 
			
		||||
            model = buildModel( m_defaultCombination );
 | 
			
		||||
            m_cachedModels.put( m_defaultCombination, model );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return model;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -20,7 +20,7 @@ import java.util.regex.Pattern;
 | 
			
		||||
 */
 | 
			
		||||
public class AddressPredicate
 | 
			
		||||
{
 | 
			
		||||
    private static class HostRange
 | 
			
		||||
    private static final class HostRange
 | 
			
		||||
    {
 | 
			
		||||
        private final byte[] min;
 | 
			
		||||
        private final byte[] max;
 | 
			
		||||
@@ -69,7 +69,10 @@ public class AddressPredicate
 | 
			
		||||
                }
 | 
			
		||||
                catch( NumberFormatException e )
 | 
			
		||||
                {
 | 
			
		||||
                    ComputerCraft.log.warn( "Cannot parse CIDR size from {} ({})", filter, prefixSizeStr );
 | 
			
		||||
                    ComputerCraft.log.error(
 | 
			
		||||
                        "Malformed http whitelist/blacklist entry '{}': Cannot extract size of CIDR mask from '{}'.",
 | 
			
		||||
                        filter, prefixSizeStr
 | 
			
		||||
                    );
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@@ -80,7 +83,10 @@ public class AddressPredicate
 | 
			
		||||
                }
 | 
			
		||||
                catch( IllegalArgumentException e )
 | 
			
		||||
                {
 | 
			
		||||
                    ComputerCraft.log.warn( "Cannot parse IP address from {} ({})", filter, addressStr );
 | 
			
		||||
                    ComputerCraft.log.error(
 | 
			
		||||
                        "Malformed http whitelist/blacklist entry '{}': Cannot extract IP address from '{}'.",
 | 
			
		||||
                        filter, prefixSizeStr
 | 
			
		||||
                    );
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,8 +10,7 @@ import com.google.common.base.Preconditions;
 | 
			
		||||
import dan200.computercraft.api.filesystem.IMount;
 | 
			
		||||
import dan200.computercraft.api.filesystem.IWritableMount;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IComputerAccess;
 | 
			
		||||
import dan200.computercraft.core.computer.Computer;
 | 
			
		||||
import dan200.computercraft.core.computer.IComputerOwned;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IWorkMonitor;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystem;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystemException;
 | 
			
		||||
 | 
			
		||||
@@ -21,22 +20,22 @@ import java.util.HashSet;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
public abstract class ComputerAccess implements IComputerAccess, IComputerOwned
 | 
			
		||||
public abstract class ComputerAccess implements IComputerAccess
 | 
			
		||||
{
 | 
			
		||||
    private final IAPIEnvironment m_environment;
 | 
			
		||||
    private final Set<String> m_mounts = new HashSet<>();
 | 
			
		||||
 | 
			
		||||
    protected ComputerAccess( IAPIEnvironment m_environment )
 | 
			
		||||
    protected ComputerAccess( IAPIEnvironment environment )
 | 
			
		||||
    {
 | 
			
		||||
        this.m_environment = m_environment;
 | 
			
		||||
        this.m_environment = environment;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void unmountAll()
 | 
			
		||||
    {
 | 
			
		||||
        FileSystem fileSystem = m_environment.getFileSystem();
 | 
			
		||||
        for( String m_mount : m_mounts )
 | 
			
		||||
        for( String mount : m_mounts )
 | 
			
		||||
        {
 | 
			
		||||
            fileSystem.unmount( m_mount );
 | 
			
		||||
            fileSystem.unmount( mount );
 | 
			
		||||
        }
 | 
			
		||||
        m_mounts.clear();
 | 
			
		||||
    }
 | 
			
		||||
@@ -128,9 +127,9 @@ public abstract class ComputerAccess implements IComputerAccess, IComputerOwned
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    @Override
 | 
			
		||||
    public Computer getComputer()
 | 
			
		||||
    public IWorkMonitor getMainThreadMonitor()
 | 
			
		||||
    {
 | 
			
		||||
        return m_environment.getComputer();
 | 
			
		||||
        return m_environment.getMainThreadMonitor();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private String findFreeLocation( String desiredLoc )
 | 
			
		||||
 
 | 
			
		||||
@@ -34,9 +34,9 @@ public class FSAPI implements ILuaAPI
 | 
			
		||||
    private IAPIEnvironment m_env;
 | 
			
		||||
    private FileSystem m_fileSystem;
 | 
			
		||||
 | 
			
		||||
    public FSAPI( IAPIEnvironment _env )
 | 
			
		||||
    public FSAPI( IAPIEnvironment env )
 | 
			
		||||
    {
 | 
			
		||||
        m_env = _env;
 | 
			
		||||
        m_env = env;
 | 
			
		||||
        m_fileSystem = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -353,10 +353,8 @@ public class FSAPI implements ILuaAPI
 | 
			
		||||
                return new Object[] { FileSystem.getDirectory( path ) };
 | 
			
		||||
            }
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                assert (false);
 | 
			
		||||
                assert false;
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -199,9 +199,7 @@ public class HTTPAPI implements ILuaAPI
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -209,14 +207,14 @@ public class HTTPAPI implements ILuaAPI
 | 
			
		||||
    private static HttpHeaders getHeaders( @Nonnull Map<?, ?> headerTable ) throws LuaException
 | 
			
		||||
    {
 | 
			
		||||
        HttpHeaders headers = new DefaultHttpHeaders();
 | 
			
		||||
        for( Object key : headerTable.keySet() )
 | 
			
		||||
        for( Map.Entry<?, ?> entry : headerTable.entrySet() )
 | 
			
		||||
        {
 | 
			
		||||
            Object value = headerTable.get( key );
 | 
			
		||||
            if( key instanceof String && value instanceof String )
 | 
			
		||||
            Object value = entry.getValue();
 | 
			
		||||
            if( entry.getKey() instanceof String && value instanceof String )
 | 
			
		||||
            {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    headers.add( (String) key, value );
 | 
			
		||||
                    headers.add( (String) entry.getKey(), value );
 | 
			
		||||
                }
 | 
			
		||||
                catch( IllegalArgumentException e )
 | 
			
		||||
                {
 | 
			
		||||
 
 | 
			
		||||
@@ -7,27 +7,38 @@
 | 
			
		||||
package dan200.computercraft.core.apis;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.peripheral.IPeripheral;
 | 
			
		||||
import dan200.computercraft.core.computer.Computer;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IWorkMonitor;
 | 
			
		||||
import dan200.computercraft.core.computer.IComputerEnvironment;
 | 
			
		||||
import dan200.computercraft.core.computer.IComputerOwned;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystem;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.core.tracking.TrackingField;
 | 
			
		||||
 | 
			
		||||
public interface IAPIEnvironment extends IComputerOwned
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
public interface IAPIEnvironment
 | 
			
		||||
{
 | 
			
		||||
    String[] SIDE_NAMES = new String[] {
 | 
			
		||||
        "bottom", "top", "back", "front", "right", "left",
 | 
			
		||||
    };
 | 
			
		||||
 | 
			
		||||
    int SIDE_COUNT = 6;
 | 
			
		||||
 | 
			
		||||
    @FunctionalInterface
 | 
			
		||||
    interface IPeripheralChangeListener
 | 
			
		||||
    {
 | 
			
		||||
        void onPeripheralChanged( int side, IPeripheral newPeripheral );
 | 
			
		||||
        void onPeripheralChanged( int side, @Nullable IPeripheral newPeripheral );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    Computer getComputer();
 | 
			
		||||
 | 
			
		||||
    int getComputerID();
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    IComputerEnvironment getComputerEnvironment();
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    IWorkMonitor getMainThreadMonitor();
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    Terminal getTerminal();
 | 
			
		||||
 | 
			
		||||
    FileSystem getFileSystem();
 | 
			
		||||
@@ -50,17 +61,18 @@ public interface IAPIEnvironment extends IComputerOwned
 | 
			
		||||
 | 
			
		||||
    int getBundledInput( int side );
 | 
			
		||||
 | 
			
		||||
    void setPeripheralChangeListener( IPeripheralChangeListener listener );
 | 
			
		||||
    void setPeripheralChangeListener( @Nullable IPeripheralChangeListener listener );
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    IPeripheral getPeripheral( int side );
 | 
			
		||||
 | 
			
		||||
    String getLabel();
 | 
			
		||||
 | 
			
		||||
    void setLabel( String label );
 | 
			
		||||
    void setLabel( @Nullable String label );
 | 
			
		||||
 | 
			
		||||
    void addTrackingChange( TrackingField field, long change );
 | 
			
		||||
    void addTrackingChange( @Nonnull TrackingField field, long change );
 | 
			
		||||
 | 
			
		||||
    default void addTrackingChange( TrackingField field )
 | 
			
		||||
    default void addTrackingChange( @Nonnull TrackingField field )
 | 
			
		||||
    {
 | 
			
		||||
        addTrackingChange( field, 1 );
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -39,7 +39,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class Alarm implements Comparable<Alarm>
 | 
			
		||||
    private static class Alarm implements Comparable<Alarm>
 | 
			
		||||
    {
 | 
			
		||||
        public final double m_time;
 | 
			
		||||
        public final int m_day;
 | 
			
		||||
@@ -110,7 +110,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
            {
 | 
			
		||||
                Map.Entry<Integer, Timer> entry = it.next();
 | 
			
		||||
                Timer timer = entry.getValue();
 | 
			
		||||
                timer.m_ticksLeft = timer.m_ticksLeft - 1;
 | 
			
		||||
                timer.m_ticksLeft--;
 | 
			
		||||
                if( timer.m_ticksLeft <= 0 )
 | 
			
		||||
                {
 | 
			
		||||
                    // Queue the "timer" event
 | 
			
		||||
@@ -198,7 +198,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
 | 
			
		||||
    private int getDayForCalendar( Calendar c )
 | 
			
		||||
    {
 | 
			
		||||
        GregorianCalendar g = (c instanceof GregorianCalendar) ? (GregorianCalendar) c : new GregorianCalendar();
 | 
			
		||||
        GregorianCalendar g = c instanceof GregorianCalendar ? (GregorianCalendar) c : new GregorianCalendar();
 | 
			
		||||
        int year = c.get( Calendar.YEAR );
 | 
			
		||||
        int day = 0;
 | 
			
		||||
        for( int y = 1970; y < year; y++ )
 | 
			
		||||
@@ -219,12 +219,9 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
    {
 | 
			
		||||
        switch( method )
 | 
			
		||||
        {
 | 
			
		||||
            case 0:
 | 
			
		||||
            {
 | 
			
		||||
                // queueEvent
 | 
			
		||||
            case 0: // queueEvent
 | 
			
		||||
                queueLuaEvent( getString( args, 0 ), trimArray( args, 1 ) );
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 1:
 | 
			
		||||
            {
 | 
			
		||||
                // startTimer
 | 
			
		||||
@@ -245,29 +242,20 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
                }
 | 
			
		||||
                synchronized( m_alarms )
 | 
			
		||||
                {
 | 
			
		||||
                    int day = (time > m_time) ? m_day : (m_day + 1);
 | 
			
		||||
                    int day = time > m_time ? m_day : m_day + 1;
 | 
			
		||||
                    m_alarms.put( m_nextAlarmToken, new Alarm( time, day ) );
 | 
			
		||||
                    return new Object[] { m_nextAlarmToken++ };
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            case 3:
 | 
			
		||||
            {
 | 
			
		||||
                // shutdown
 | 
			
		||||
            case 3: // shutdown
 | 
			
		||||
                m_apiEnvironment.shutdown();
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 4:
 | 
			
		||||
            {
 | 
			
		||||
                // reboot
 | 
			
		||||
            case 4: // reboot
 | 
			
		||||
                m_apiEnvironment.reboot();
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 5:
 | 
			
		||||
            case 6:
 | 
			
		||||
            {
 | 
			
		||||
                // computerID/getComputerID
 | 
			
		||||
            case 6: // computerID/getComputerID
 | 
			
		||||
                return new Object[] { getComputerID() };
 | 
			
		||||
            }
 | 
			
		||||
            case 7:
 | 
			
		||||
            {
 | 
			
		||||
                // setComputerLabel
 | 
			
		||||
@@ -286,19 +274,16 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
                }
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 10:
 | 
			
		||||
            {
 | 
			
		||||
                // clock
 | 
			
		||||
            case 10: // clock
 | 
			
		||||
                synchronized( m_timers )
 | 
			
		||||
                {
 | 
			
		||||
                    return new Object[] { m_clock * 0.05 };
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            case 11:
 | 
			
		||||
            {
 | 
			
		||||
                // time
 | 
			
		||||
                String param = optString( args, 0, "ingame" );
 | 
			
		||||
                switch( param )
 | 
			
		||||
                switch( param.toLowerCase( Locale.ROOT ) )
 | 
			
		||||
                {
 | 
			
		||||
                    case "utc":
 | 
			
		||||
                    {
 | 
			
		||||
@@ -326,7 +311,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
            {
 | 
			
		||||
                // day
 | 
			
		||||
                String param = optString( args, 0, "ingame" );
 | 
			
		||||
                switch( param )
 | 
			
		||||
                switch( param.toLowerCase( Locale.ROOT ) )
 | 
			
		||||
                {
 | 
			
		||||
                    case "utc":
 | 
			
		||||
                    {
 | 
			
		||||
@@ -356,10 +341,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
                int token = getInt( args, 0 );
 | 
			
		||||
                synchronized( m_timers )
 | 
			
		||||
                {
 | 
			
		||||
                    if( m_timers.containsKey( token ) )
 | 
			
		||||
                    {
 | 
			
		||||
                        m_timers.remove( token );
 | 
			
		||||
                    }
 | 
			
		||||
                    m_timers.remove( token );
 | 
			
		||||
                }
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
@@ -369,10 +351,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
                int token = getInt( args, 0 );
 | 
			
		||||
                synchronized( m_alarms )
 | 
			
		||||
                {
 | 
			
		||||
                    if( m_alarms.containsKey( token ) )
 | 
			
		||||
                    {
 | 
			
		||||
                        m_alarms.remove( token );
 | 
			
		||||
                    }
 | 
			
		||||
                    m_alarms.remove( token );
 | 
			
		||||
                }
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
@@ -380,7 +359,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
            {
 | 
			
		||||
                // epoch
 | 
			
		||||
                String param = optString( args, 0, "ingame" );
 | 
			
		||||
                switch( param )
 | 
			
		||||
                switch( param.toLowerCase( Locale.ROOT ) )
 | 
			
		||||
                {
 | 
			
		||||
                    case "utc":
 | 
			
		||||
                    {
 | 
			
		||||
@@ -407,9 +386,7 @@ public class OSAPI implements ILuaAPI
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -12,9 +12,6 @@ import dan200.computercraft.api.lua.ILuaAPI;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaContext;
 | 
			
		||||
import dan200.computercraft.api.lua.LuaException;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IPeripheral;
 | 
			
		||||
import dan200.computercraft.core.computer.Computer;
 | 
			
		||||
import dan200.computercraft.core.computer.ComputerThread;
 | 
			
		||||
import dan200.computercraft.core.computer.ITask;
 | 
			
		||||
import dan200.computercraft.core.tracking.TrackingField;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
@@ -47,8 +44,8 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
 | 
			
		||||
 | 
			
		||||
            m_type = peripheral.getType();
 | 
			
		||||
            m_methods = peripheral.getMethodNames();
 | 
			
		||||
            assert (m_type != null);
 | 
			
		||||
            assert (m_methods != null);
 | 
			
		||||
            assert m_type != null;
 | 
			
		||||
            assert m_methods != null;
 | 
			
		||||
 | 
			
		||||
            m_methodMap = new HashMap<>();
 | 
			
		||||
            for( int i = 0; i < m_methods.length; i++ )
 | 
			
		||||
@@ -231,9 +228,9 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
 | 
			
		||||
    private final PeripheralWrapper[] m_peripherals;
 | 
			
		||||
    private boolean m_running;
 | 
			
		||||
 | 
			
		||||
    public PeripheralAPI( IAPIEnvironment _environment )
 | 
			
		||||
    public PeripheralAPI( IAPIEnvironment environment )
 | 
			
		||||
    {
 | 
			
		||||
        m_environment = _environment;
 | 
			
		||||
        m_environment = environment;
 | 
			
		||||
        m_environment.setPeripheralChangeListener( this );
 | 
			
		||||
 | 
			
		||||
        m_peripherals = new PeripheralWrapper[6];
 | 
			
		||||
@@ -256,68 +253,24 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
 | 
			
		||||
            {
 | 
			
		||||
                // Queue a detachment
 | 
			
		||||
                final PeripheralWrapper wrapper = m_peripherals[side];
 | 
			
		||||
                ComputerThread.queueTask( new ITask()
 | 
			
		||||
                {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public Computer getOwner()
 | 
			
		||||
                    {
 | 
			
		||||
                        return m_environment.getComputer();
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public void execute()
 | 
			
		||||
                    {
 | 
			
		||||
                        synchronized( m_peripherals )
 | 
			
		||||
                        {
 | 
			
		||||
                            if( wrapper.isAttached() )
 | 
			
		||||
                            {
 | 
			
		||||
                                wrapper.detach();
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }, null );
 | 
			
		||||
                if( wrapper.isAttached() ) wrapper.detach();
 | 
			
		||||
 | 
			
		||||
                // Queue a detachment event
 | 
			
		||||
                m_environment.queueEvent( "peripheral_detach", new Object[] { Computer.s_sideNames[side] } );
 | 
			
		||||
                m_environment.queueEvent( "peripheral_detach", new Object[] { IAPIEnvironment.SIDE_NAMES[side] } );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Assign the new peripheral
 | 
			
		||||
            if( newPeripheral != null )
 | 
			
		||||
            {
 | 
			
		||||
                m_peripherals[side] = new PeripheralWrapper( newPeripheral, Computer.s_sideNames[side] );
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                m_peripherals[side] = null;
 | 
			
		||||
            }
 | 
			
		||||
            m_peripherals[side] = newPeripheral == null ? null
 | 
			
		||||
                : new PeripheralWrapper( newPeripheral, IAPIEnvironment.SIDE_NAMES[side] );
 | 
			
		||||
 | 
			
		||||
            if( m_peripherals[side] != null )
 | 
			
		||||
            {
 | 
			
		||||
                // Queue an attachment
 | 
			
		||||
                final PeripheralWrapper wrapper = m_peripherals[side];
 | 
			
		||||
                ComputerThread.queueTask( new ITask()
 | 
			
		||||
                {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public Computer getOwner()
 | 
			
		||||
                    {
 | 
			
		||||
                        return m_environment.getComputer();
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public void execute()
 | 
			
		||||
                    {
 | 
			
		||||
                        synchronized( m_peripherals )
 | 
			
		||||
                        {
 | 
			
		||||
                            if( m_running && !wrapper.isAttached() )
 | 
			
		||||
                            {
 | 
			
		||||
                                wrapper.attach();
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }, null );
 | 
			
		||||
                if( m_running && !wrapper.isAttached() ) wrapper.attach();
 | 
			
		||||
 | 
			
		||||
                // Queue an attachment event
 | 
			
		||||
                m_environment.queueEvent( "peripheral", new Object[] { Computer.s_sideNames[side] } );
 | 
			
		||||
                m_environment.queueEvent( "peripheral", new Object[] { IAPIEnvironment.SIDE_NAMES[side] } );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -341,10 +294,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
 | 
			
		||||
            for( int i = 0; i < 6; i++ )
 | 
			
		||||
            {
 | 
			
		||||
                PeripheralWrapper wrapper = m_peripherals[i];
 | 
			
		||||
                if( wrapper != null && !wrapper.isAttached() )
 | 
			
		||||
                {
 | 
			
		||||
                    wrapper.attach();
 | 
			
		||||
                }
 | 
			
		||||
                if( wrapper != null && !wrapper.isAttached() ) wrapper.attach();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
@@ -472,9 +422,7 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
 | 
			
		||||
                throw new LuaException( "No peripheral attached" );
 | 
			
		||||
            }
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -483,9 +431,9 @@ public class PeripheralAPI implements ILuaAPI, IAPIEnvironment.IPeripheralChange
 | 
			
		||||
    private int parseSide( Object[] args ) throws LuaException
 | 
			
		||||
    {
 | 
			
		||||
        String side = getString( args, 0 );
 | 
			
		||||
        for( int n = 0; n < Computer.s_sideNames.length; n++ )
 | 
			
		||||
        for( int n = 0; n < IAPIEnvironment.SIDE_NAMES.length; n++ )
 | 
			
		||||
        {
 | 
			
		||||
            if( side.equals( Computer.s_sideNames[n] ) )
 | 
			
		||||
            if( side.equals( IAPIEnvironment.SIDE_NAMES[n] ) )
 | 
			
		||||
            {
 | 
			
		||||
                return n;
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,7 +9,6 @@ package dan200.computercraft.core.apis;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaAPI;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaContext;
 | 
			
		||||
import dan200.computercraft.api.lua.LuaException;
 | 
			
		||||
import dan200.computercraft.core.computer.Computer;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
@@ -65,9 +64,9 @@ public class RedstoneAPI implements ILuaAPI
 | 
			
		||||
            {
 | 
			
		||||
                // getSides
 | 
			
		||||
                Map<Object, Object> table = new HashMap<>();
 | 
			
		||||
                for( int i = 0; i < Computer.s_sideNames.length; i++ )
 | 
			
		||||
                for( int i = 0; i < IAPIEnvironment.SIDE_NAMES.length; i++ )
 | 
			
		||||
                {
 | 
			
		||||
                    table.put( i + 1, Computer.s_sideNames[i] );
 | 
			
		||||
                    table.put( i + 1, IAPIEnvironment.SIDE_NAMES[i] );
 | 
			
		||||
                }
 | 
			
		||||
                return new Object[] { table };
 | 
			
		||||
            }
 | 
			
		||||
@@ -117,7 +116,7 @@ public class RedstoneAPI implements ILuaAPI
 | 
			
		||||
                int side = parseSide( args );
 | 
			
		||||
                int mask = getInt( args, 1 );
 | 
			
		||||
                int input = m_environment.getBundledInput( side );
 | 
			
		||||
                return new Object[] { ((input & mask) == mask) };
 | 
			
		||||
                return new Object[] { (input & mask) == mask };
 | 
			
		||||
            }
 | 
			
		||||
            case 8:
 | 
			
		||||
            case 9:
 | 
			
		||||
@@ -147,18 +146,16 @@ public class RedstoneAPI implements ILuaAPI
 | 
			
		||||
                return new Object[] { m_environment.getInput( side ) };
 | 
			
		||||
            }
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private int parseSide( Object[] args ) throws LuaException
 | 
			
		||||
    private static int parseSide( Object[] args ) throws LuaException
 | 
			
		||||
    {
 | 
			
		||||
        String side = getString( args, 0 );
 | 
			
		||||
        for( int n = 0; n < Computer.s_sideNames.length; n++ )
 | 
			
		||||
        for( int n = 0; n < IAPIEnvironment.SIDE_NAMES.length; n++ )
 | 
			
		||||
        {
 | 
			
		||||
            if( side.equals( Computer.s_sideNames[n] ) )
 | 
			
		||||
            if( side.equals( IAPIEnvironment.SIDE_NAMES[n] ) )
 | 
			
		||||
            {
 | 
			
		||||
                return n;
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -11,6 +11,7 @@ import dan200.computercraft.api.lua.ILuaContext;
 | 
			
		||||
import dan200.computercraft.api.lua.LuaException;
 | 
			
		||||
import dan200.computercraft.core.computer.IComputerEnvironment;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.shared.util.Colour;
 | 
			
		||||
import dan200.computercraft.shared.util.Palette;
 | 
			
		||||
import org.apache.commons.lang3.ArrayUtils;
 | 
			
		||||
 | 
			
		||||
@@ -23,10 +24,10 @@ public class TermAPI implements ILuaAPI
 | 
			
		||||
    private final Terminal m_terminal;
 | 
			
		||||
    private final IComputerEnvironment m_environment;
 | 
			
		||||
 | 
			
		||||
    public TermAPI( IAPIEnvironment _environment )
 | 
			
		||||
    public TermAPI( IAPIEnvironment environment )
 | 
			
		||||
    {
 | 
			
		||||
        m_terminal = _environment.getTerminal();
 | 
			
		||||
        m_environment = _environment.getComputerEnvironment();
 | 
			
		||||
        m_terminal = environment.getTerminal();
 | 
			
		||||
        m_environment = environment.getComputerEnvironment();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -65,6 +66,8 @@ public class TermAPI implements ILuaAPI
 | 
			
		||||
            "setPaletteColor",
 | 
			
		||||
            "getPaletteColour",
 | 
			
		||||
            "getPaletteColor",
 | 
			
		||||
            "nativePaletteColour",
 | 
			
		||||
            "nativePaletteColor",
 | 
			
		||||
            "getCursorBlink",
 | 
			
		||||
        };
 | 
			
		||||
    }
 | 
			
		||||
@@ -108,16 +111,7 @@ public class TermAPI implements ILuaAPI
 | 
			
		||||
            case 0:
 | 
			
		||||
            {
 | 
			
		||||
                // write
 | 
			
		||||
                String text;
 | 
			
		||||
                if( args.length > 0 && args[0] != null )
 | 
			
		||||
                {
 | 
			
		||||
                    text = args[0].toString();
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    text = "";
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                String text = args.length > 0 && args[0] != null ? args[0].toString() : "";
 | 
			
		||||
                synchronized( m_terminal )
 | 
			
		||||
                {
 | 
			
		||||
                    m_terminal.write( text );
 | 
			
		||||
@@ -178,24 +172,18 @@ public class TermAPI implements ILuaAPI
 | 
			
		||||
                }
 | 
			
		||||
                return new Object[] { width, height };
 | 
			
		||||
            }
 | 
			
		||||
            case 6:
 | 
			
		||||
            {
 | 
			
		||||
                // clear
 | 
			
		||||
            case 6: // clear
 | 
			
		||||
                synchronized( m_terminal )
 | 
			
		||||
                {
 | 
			
		||||
                    m_terminal.clear();
 | 
			
		||||
                }
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 7:
 | 
			
		||||
            {
 | 
			
		||||
                // clearLine
 | 
			
		||||
            case 7: // clearLine
 | 
			
		||||
                synchronized( m_terminal )
 | 
			
		||||
                {
 | 
			
		||||
                    m_terminal.clearLine();
 | 
			
		||||
                }
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 8:
 | 
			
		||||
            case 9:
 | 
			
		||||
            {
 | 
			
		||||
@@ -219,23 +207,14 @@ public class TermAPI implements ILuaAPI
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 12:
 | 
			
		||||
            case 13:
 | 
			
		||||
            {
 | 
			
		||||
                // isColour/isColor
 | 
			
		||||
            case 13: // isColour/isColor
 | 
			
		||||
                return new Object[] { m_environment.isColour() };
 | 
			
		||||
            }
 | 
			
		||||
            case 14:
 | 
			
		||||
            case 15:
 | 
			
		||||
            {
 | 
			
		||||
                // getTextColour/getTextColor
 | 
			
		||||
            case 15: // getTextColour/getTextColor
 | 
			
		||||
                return encodeColour( m_terminal.getTextColour() );
 | 
			
		||||
            }
 | 
			
		||||
            case 16:
 | 
			
		||||
            case 17:
 | 
			
		||||
            {
 | 
			
		||||
                // getBackgroundColour/getBackgroundColor
 | 
			
		||||
            case 17: // getBackgroundColour/getBackgroundColor
 | 
			
		||||
                return encodeColour( m_terminal.getBackgroundColour() );
 | 
			
		||||
            }
 | 
			
		||||
            case 18:
 | 
			
		||||
            {
 | 
			
		||||
                // blit
 | 
			
		||||
@@ -289,12 +268,23 @@ public class TermAPI implements ILuaAPI
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case 23:
 | 
			
		||||
            case 24:
 | 
			
		||||
            {
 | 
			
		||||
                // nativePaletteColour/nativePaletteColor
 | 
			
		||||
                int colour = 15 - parseColour( args );
 | 
			
		||||
                Colour c = Colour.fromInt( colour );
 | 
			
		||||
 | 
			
		||||
                float[] rgb = c.getRGB();
 | 
			
		||||
 | 
			
		||||
                Object[] rgbObj = new Object[rgb.length];
 | 
			
		||||
                for( int i = 0; i < rgbObj.length; ++i ) rgbObj[i] = rgb[i];
 | 
			
		||||
                return rgbObj;
 | 
			
		||||
            }
 | 
			
		||||
            case 25:
 | 
			
		||||
                // getCursorBlink
 | 
			
		||||
                return new Object[] { m_terminal.getCursorBlink() };
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -6,13 +6,11 @@
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.apis.handles;
 | 
			
		||||
 | 
			
		||||
import com.google.common.base.Preconditions;
 | 
			
		||||
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.nio.ByteBuffer;
 | 
			
		||||
import java.nio.channels.ClosedChannelException;
 | 
			
		||||
import java.nio.channels.NonWritableChannelException;
 | 
			
		||||
import java.nio.channels.SeekableByteChannel;
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * A seekable, readable byte channel which is backed by a simple byte array.
 | 
			
		||||
@@ -30,10 +28,10 @@ public class ArrayByteChannel implements SeekableByteChannel
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int read( ByteBuffer destination ) throws IOException
 | 
			
		||||
    public int read( ByteBuffer destination ) throws ClosedChannelException
 | 
			
		||||
    {
 | 
			
		||||
        if( closed ) throw new ClosedChannelException();
 | 
			
		||||
        Preconditions.checkNotNull( destination, "destination" );
 | 
			
		||||
        Objects.requireNonNull( destination, "destination" );
 | 
			
		||||
 | 
			
		||||
        if( position >= backing.length ) return -1;
 | 
			
		||||
 | 
			
		||||
@@ -44,21 +42,21 @@ public class ArrayByteChannel implements SeekableByteChannel
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int write( ByteBuffer src ) throws IOException
 | 
			
		||||
    public int write( ByteBuffer src ) throws ClosedChannelException
 | 
			
		||||
    {
 | 
			
		||||
        if( closed ) throw new ClosedChannelException();
 | 
			
		||||
        throw new NonWritableChannelException();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public long position() throws IOException
 | 
			
		||||
    public long position() throws ClosedChannelException
 | 
			
		||||
    {
 | 
			
		||||
        if( closed ) throw new ClosedChannelException();
 | 
			
		||||
        return position;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public SeekableByteChannel position( long newPosition ) throws IOException
 | 
			
		||||
    public SeekableByteChannel position( long newPosition ) throws ClosedChannelException
 | 
			
		||||
    {
 | 
			
		||||
        if( closed ) throw new ClosedChannelException();
 | 
			
		||||
        if( newPosition < 0 || newPosition > Integer.MAX_VALUE )
 | 
			
		||||
@@ -70,14 +68,14 @@ public class ArrayByteChannel implements SeekableByteChannel
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public long size() throws IOException
 | 
			
		||||
    public long size() throws ClosedChannelException
 | 
			
		||||
    {
 | 
			
		||||
        if( closed ) throw new ClosedChannelException();
 | 
			
		||||
        return backing.length;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public SeekableByteChannel truncate( long size ) throws IOException
 | 
			
		||||
    public SeekableByteChannel truncate( long size ) throws ClosedChannelException
 | 
			
		||||
    {
 | 
			
		||||
        if( closed ) throw new ClosedChannelException();
 | 
			
		||||
        throw new NonWritableChannelException();
 | 
			
		||||
 
 | 
			
		||||
@@ -38,8 +38,8 @@ public class BinaryReadableHandle extends HandleGeneric
 | 
			
		||||
    public BinaryReadableHandle( ReadableByteChannel channel, Closeable closeable )
 | 
			
		||||
    {
 | 
			
		||||
        super( closeable );
 | 
			
		||||
        this.m_reader = channel;
 | 
			
		||||
        this.m_seekable = asSeekable( channel );
 | 
			
		||||
        m_reader = channel;
 | 
			
		||||
        m_seekable = asSeekable( channel );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public BinaryReadableHandle( ReadableByteChannel channel )
 | 
			
		||||
@@ -169,27 +169,42 @@ public class BinaryReadableHandle extends HandleGeneric
 | 
			
		||||
                {
 | 
			
		||||
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
 | 
			
		||||
 | 
			
		||||
                    boolean readAnything = false;
 | 
			
		||||
                    boolean readAnything = false, readRc = false;
 | 
			
		||||
                    while( true )
 | 
			
		||||
                    {
 | 
			
		||||
                        single.clear();
 | 
			
		||||
                        int r = m_reader.read( single );
 | 
			
		||||
                        if( r == -1 ) break;
 | 
			
		||||
                        int read = m_reader.read( single );
 | 
			
		||||
                        if( read <= 0 )
 | 
			
		||||
                        {
 | 
			
		||||
                            // Nothing else to read, and we saw no \n. Return the array. If we saw a \r, then add it
 | 
			
		||||
                            // back.
 | 
			
		||||
                            if( readRc ) stream.write( '\r' );
 | 
			
		||||
                            return readAnything ? new Object[] { stream.toByteArray() } : null;
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        readAnything = true;
 | 
			
		||||
                        byte b = single.get( 0 );
 | 
			
		||||
                        if( b == '\n' )
 | 
			
		||||
 | 
			
		||||
                        byte chr = single.get( 0 );
 | 
			
		||||
                        if( chr == '\n' )
 | 
			
		||||
                        {
 | 
			
		||||
                            if( withTrailing ) stream.write( b );
 | 
			
		||||
                            break;
 | 
			
		||||
                            if( withTrailing )
 | 
			
		||||
                            {
 | 
			
		||||
                                if( readRc ) stream.write( '\r' );
 | 
			
		||||
                                stream.write( chr );
 | 
			
		||||
                            }
 | 
			
		||||
                            return new Object[] { stream.toByteArray() };
 | 
			
		||||
                        }
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            stream.write( b );
 | 
			
		||||
                            // We want to skip \r\n, but obviously need to include cases where \r is not followed by \n.
 | 
			
		||||
                            // Note, this behaviour is non-standard compliant (strictly speaking we should have no
 | 
			
		||||
                            // special logic for \r), but we preserve compatibility with EncodedReadableHandle and
 | 
			
		||||
                            // previous behaviour of the io library.
 | 
			
		||||
                            if( readRc ) stream.write( '\r' );
 | 
			
		||||
                            readRc = chr == '\r';
 | 
			
		||||
                            if( !readRc ) stream.write( chr );
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    return readAnything ? new Object[] { stream.toByteArray() } : null;
 | 
			
		||||
                }
 | 
			
		||||
                catch( IOException e )
 | 
			
		||||
                {
 | 
			
		||||
 
 | 
			
		||||
@@ -32,8 +32,8 @@ public class BinaryWritableHandle extends HandleGeneric
 | 
			
		||||
    public BinaryWritableHandle( WritableByteChannel channel, Closeable closeable )
 | 
			
		||||
    {
 | 
			
		||||
        super( closeable );
 | 
			
		||||
        this.m_writer = channel;
 | 
			
		||||
        this.m_seekable = asSeekable( channel );
 | 
			
		||||
        m_writer = channel;
 | 
			
		||||
        m_seekable = asSeekable( channel );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public BinaryWritableHandle( WritableByteChannel channel )
 | 
			
		||||
 
 | 
			
		||||
@@ -32,7 +32,7 @@ public class EncodedReadableHandle extends HandleGeneric
 | 
			
		||||
    public EncodedReadableHandle( @Nonnull BufferedReader reader, @Nonnull Closeable closable )
 | 
			
		||||
    {
 | 
			
		||||
        super( closable );
 | 
			
		||||
        this.m_reader = reader;
 | 
			
		||||
        m_reader = reader;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public EncodedReadableHandle( @Nonnull BufferedReader reader )
 | 
			
		||||
@@ -84,7 +84,7 @@ public class EncodedReadableHandle extends HandleGeneric
 | 
			
		||||
                checkOpen();
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    StringBuilder result = new StringBuilder( "" );
 | 
			
		||||
                    StringBuilder result = new StringBuilder();
 | 
			
		||||
                    String line = m_reader.readLine();
 | 
			
		||||
                    while( line != null )
 | 
			
		||||
                    {
 | 
			
		||||
 
 | 
			
		||||
@@ -27,7 +27,7 @@ public class EncodedWritableHandle extends HandleGeneric
 | 
			
		||||
    public EncodedWritableHandle( @Nonnull BufferedWriter writer, @Nonnull Closeable closable )
 | 
			
		||||
    {
 | 
			
		||||
        super( closable );
 | 
			
		||||
        this.m_writer = writer;
 | 
			
		||||
        m_writer = writer;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public EncodedWritableHandle( @Nonnull BufferedWriter writer )
 | 
			
		||||
 
 | 
			
		||||
@@ -26,7 +26,7 @@ public abstract class HandleGeneric implements ILuaObject
 | 
			
		||||
 | 
			
		||||
    protected HandleGeneric( @Nonnull Closeable closable )
 | 
			
		||||
    {
 | 
			
		||||
        this.m_closable = closable;
 | 
			
		||||
        m_closable = closable;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    protected void checkOpen() throws LuaException
 | 
			
		||||
@@ -46,7 +46,7 @@ public abstract class HandleGeneric implements ILuaObject
 | 
			
		||||
     *
 | 
			
		||||
     * @param channel The channel to seek in
 | 
			
		||||
     * @param args    The Lua arguments to process, like Lua's {@code file:seek}.
 | 
			
		||||
     * @return The new position of the file, or null if some error occured.
 | 
			
		||||
     * @return The new position of the file, or null if some error occurred.
 | 
			
		||||
     * @throws LuaException If the arguments were invalid
 | 
			
		||||
     * @see <a href="https://www.lua.org/manual/5.1/manual.html#pdf-file:seek">{@code file:seek} in the Lua manual.</a>
 | 
			
		||||
     */
 | 
			
		||||
 
 | 
			
		||||
@@ -31,7 +31,7 @@ public class CheckUrl extends Resource<CheckUrl>
 | 
			
		||||
        super( limiter );
 | 
			
		||||
        this.environment = environment;
 | 
			
		||||
        this.address = address;
 | 
			
		||||
        this.host = uri.getHost();
 | 
			
		||||
        host = uri.getHost();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void run()
 | 
			
		||||
 
 | 
			
		||||
@@ -140,6 +140,6 @@ public abstract class Resource<T extends Resource<T>> implements Closeable
 | 
			
		||||
    public static void cleanup()
 | 
			
		||||
    {
 | 
			
		||||
        Reference<?> reference;
 | 
			
		||||
        while( (reference = QUEUE.poll()) != null ) ((CloseReference) reference).resource.close();
 | 
			
		||||
        while( (reference = QUEUE.poll()) != null ) ((CloseReference<?>) reference).resource.close();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -32,7 +32,7 @@ public class ResourceGroup<T extends Resource<T>>
 | 
			
		||||
 | 
			
		||||
    public ResourceGroup()
 | 
			
		||||
    {
 | 
			
		||||
        this.limit = ZERO;
 | 
			
		||||
        limit = ZERO;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void startup()
 | 
			
		||||
 
 | 
			
		||||
@@ -67,12 +67,12 @@ public class HttpRequest extends Resource<HttpRequest>
 | 
			
		||||
        super( limiter );
 | 
			
		||||
        this.environment = environment;
 | 
			
		||||
        this.address = address;
 | 
			
		||||
        this.postBuffer = postText != null
 | 
			
		||||
        postBuffer = postText != null
 | 
			
		||||
            ? Unpooled.wrappedBuffer( postText.getBytes( StandardCharsets.UTF_8 ) )
 | 
			
		||||
            : Unpooled.buffer( 0 );
 | 
			
		||||
        this.headers = headers;
 | 
			
		||||
        this.binary = binary;
 | 
			
		||||
        this.redirects = new AtomicInteger( followRedirects ? MAX_REDIRECTS : 0 );
 | 
			
		||||
        redirects = new AtomicInteger( followRedirects ? MAX_REDIRECTS : 0 );
 | 
			
		||||
 | 
			
		||||
        if( postText != null )
 | 
			
		||||
        {
 | 
			
		||||
@@ -113,6 +113,7 @@ public class HttpRequest extends Resource<HttpRequest>
 | 
			
		||||
    {
 | 
			
		||||
        // Validate the URL
 | 
			
		||||
        if( url.getScheme() == null ) throw new HTTPRequestException( "Must specify http or https" );
 | 
			
		||||
        if( url.getHost() == null ) throw new HTTPRequestException( "URL malformed" );
 | 
			
		||||
 | 
			
		||||
        String scheme = url.getScheme().toLowerCase( Locale.ROOT );
 | 
			
		||||
        if( !scheme.equalsIgnoreCase( "http" ) && !scheme.equalsIgnoreCase( "https" ) )
 | 
			
		||||
 
 | 
			
		||||
@@ -37,7 +37,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler<HttpOb
 | 
			
		||||
    /**
 | 
			
		||||
     * Same as {@link io.netty.handler.codec.MessageAggregator}.
 | 
			
		||||
     */
 | 
			
		||||
    private static final int DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS = 1024;
 | 
			
		||||
    private static final int DEFAULT_MAX_COMPOSITE_BUFFER_COMPONENTS = 1024;
 | 
			
		||||
 | 
			
		||||
    private static final byte[] EMPTY_BYTES = new byte[0];
 | 
			
		||||
 | 
			
		||||
@@ -147,7 +147,7 @@ public final class HttpRequestHandler extends SimpleChannelInboundHandler<HttpOb
 | 
			
		||||
 | 
			
		||||
            if( responseBody == null )
 | 
			
		||||
            {
 | 
			
		||||
                responseBody = ctx.alloc().compositeBuffer( DEFAULT_MAX_COMPOSITEBUFFER_COMPONENTS );
 | 
			
		||||
                responseBody = ctx.alloc().compositeBuffer( DEFAULT_MAX_COMPOSITE_BUFFER_COMPONENTS );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            ByteBuf partial = content.content();
 | 
			
		||||
 
 | 
			
		||||
@@ -95,7 +95,7 @@ public class Websocket extends Resource<Websocket>
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                uri = new URI( "ws://" + uri.toString() );
 | 
			
		||||
                uri = new URI( "ws://" + uri );
 | 
			
		||||
            }
 | 
			
		||||
            catch( URISyntaxException e )
 | 
			
		||||
            {
 | 
			
		||||
@@ -161,7 +161,10 @@ public class Websocket extends Resource<Websocket>
 | 
			
		||||
                    }
 | 
			
		||||
                } )
 | 
			
		||||
                .remoteAddress( socketAddress )
 | 
			
		||||
                .connect();
 | 
			
		||||
                .connect()
 | 
			
		||||
                .addListener( c -> {
 | 
			
		||||
                    if( !c.isSuccess() ) failure( c.cause().getMessage() );
 | 
			
		||||
                } );
 | 
			
		||||
 | 
			
		||||
            // Do an additional check for cancellation
 | 
			
		||||
            checkClosed();
 | 
			
		||||
@@ -183,7 +186,7 @@ public class Websocket extends Resource<Websocket>
 | 
			
		||||
 | 
			
		||||
        WebsocketHandle handle = new WebsocketHandle( this, channel );
 | 
			
		||||
        environment().queueEvent( SUCCESS_EVENT, new Object[] { address, handle } );
 | 
			
		||||
        this.websocketHandle = createOwnerReference( handle );
 | 
			
		||||
        websocketHandle = createOwnerReference( handle );
 | 
			
		||||
 | 
			
		||||
        checkClosed();
 | 
			
		||||
    }
 | 
			
		||||
@@ -213,7 +216,7 @@ public class Websocket extends Resource<Websocket>
 | 
			
		||||
        executorFuture = closeFuture( executorFuture );
 | 
			
		||||
        connectFuture = closeChannel( connectFuture );
 | 
			
		||||
 | 
			
		||||
        WeakReference<WebsocketHandle> websocketHandleRef = this.websocketHandle;
 | 
			
		||||
        WeakReference<WebsocketHandle> websocketHandleRef = websocketHandle;
 | 
			
		||||
        WebsocketHandle websocketHandle = websocketHandleRef == null ? null : websocketHandleRef.get();
 | 
			
		||||
        if( websocketHandle != null ) IoUtil.closeQuietly( websocketHandle );
 | 
			
		||||
        this.websocketHandle = null;
 | 
			
		||||
 
 | 
			
		||||
@@ -83,6 +83,11 @@ public class WebsocketHandler extends SimpleChannelInboundHandler<Object>
 | 
			
		||||
            CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
 | 
			
		||||
            websocket.close( closeFrame.statusCode(), closeFrame.reasonText() );
 | 
			
		||||
        }
 | 
			
		||||
        else if( frame instanceof PingWebSocketFrame )
 | 
			
		||||
        {
 | 
			
		||||
            frame.content().retain();
 | 
			
		||||
            ctx.channel().writeAndFlush( new PongWebSocketFrame( frame.content() ) );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -108,6 +113,13 @@ public class WebsocketHandler extends SimpleChannelInboundHandler<Object>
 | 
			
		||||
            message = "Could not connect";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        websocket.failure( message );
 | 
			
		||||
        if( handshaker.isHandshakeComplete() )
 | 
			
		||||
        {
 | 
			
		||||
            websocket.close( -1, message );
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            websocket.failure( message );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,68 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaAPI;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaContext;
 | 
			
		||||
import dan200.computercraft.api.lua.LuaException;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
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;
 | 
			
		||||
        this.system = system;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String[] getNames()
 | 
			
		||||
    {
 | 
			
		||||
        return delegate.getNames();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void startup()
 | 
			
		||||
    {
 | 
			
		||||
        delegate.startup();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void update()
 | 
			
		||||
    {
 | 
			
		||||
        delegate.update();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void shutdown()
 | 
			
		||||
    {
 | 
			
		||||
        delegate.shutdown();
 | 
			
		||||
        system.unmountAll();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public String[] getMethodNames()
 | 
			
		||||
    {
 | 
			
		||||
        return delegate.getMethodNames();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    @Override
 | 
			
		||||
    public Object[] callMethod( @Nonnull ILuaContext context, int method, @Nonnull Object[] arguments ) throws LuaException, InterruptedException
 | 
			
		||||
    {
 | 
			
		||||
        return delegate.callMethod( context, method, arguments );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@@ -0,0 +1,668 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.api.filesystem.IMount;
 | 
			
		||||
import dan200.computercraft.api.filesystem.IWritableMount;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaAPI;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
 | 
			
		||||
import dan200.computercraft.core.apis.*;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystem;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystemException;
 | 
			
		||||
import dan200.computercraft.core.lua.CobaltLuaMachine;
 | 
			
		||||
import dan200.computercraft.core.lua.ILuaMachine;
 | 
			
		||||
import dan200.computercraft.core.lua.MachineResult;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.core.tracking.Tracking;
 | 
			
		||||
import dan200.computercraft.shared.util.Colour;
 | 
			
		||||
import dan200.computercraft.shared.util.IoUtil;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.util.ArrayDeque;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Queue;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicReference;
 | 
			
		||||
import java.util.concurrent.locks.ReentrantLock;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The main task queue and executor for a single computer. This handles turning on and off a computer, as well as
 | 
			
		||||
 * running events.
 | 
			
		||||
 *
 | 
			
		||||
 * When the computer is instructed to turn on or off, or handle an event, we queue a task and register this to be
 | 
			
		||||
 * executed on the {@link ComputerThread}. Note, as we may be starting many events in a single tick, the external
 | 
			
		||||
 * cannot lock on anything which may be held for a long time.
 | 
			
		||||
 *
 | 
			
		||||
 * The executor is effectively composed of two separate queues. Firstly, we have a "single element" queue
 | 
			
		||||
 * {@link #command} which determines which state the computer should transition too. This is set by
 | 
			
		||||
 * {@link #queueStart()} and {@link #queueStop(boolean, boolean)}.
 | 
			
		||||
 *
 | 
			
		||||
 * When a computer is on, we simply push any events onto to the {@link #eventQueue}.
 | 
			
		||||
 *
 | 
			
		||||
 * Both queues are run from the {@link #work()} method, which tries to execute a command if one exists, or resumes the
 | 
			
		||||
 * machine with an event otherwise.
 | 
			
		||||
 *
 | 
			
		||||
 * One final responsibility for the executor is calling {@link ILuaAPI#update()} every tick, via the {@link #tick()}
 | 
			
		||||
 * method. This should only be called when the computer is actually on ({@link #isOn}).
 | 
			
		||||
 */
 | 
			
		||||
final class ComputerExecutor
 | 
			
		||||
{
 | 
			
		||||
    private static final int QUEUE_LIMIT = 256;
 | 
			
		||||
 | 
			
		||||
    private static IMount romMount;
 | 
			
		||||
    private static final Object romMountLock = new Object();
 | 
			
		||||
 | 
			
		||||
    private final Computer computer;
 | 
			
		||||
    private final List<ILuaAPI> apis = new ArrayList<>();
 | 
			
		||||
    final TimeoutState timeout = new TimeoutState();
 | 
			
		||||
 | 
			
		||||
    private FileSystem fileSystem;
 | 
			
		||||
 | 
			
		||||
    private ILuaMachine machine;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether the computer is currently on. This is set to false when a shutdown starts, or when turning on completes
 | 
			
		||||
     * (but just before the Lua machine is started).
 | 
			
		||||
     *
 | 
			
		||||
     * @see #isOnLock
 | 
			
		||||
     */
 | 
			
		||||
    private volatile boolean isOn = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The lock to acquire when you need to modify the "on state" of a computer.
 | 
			
		||||
     *
 | 
			
		||||
     * We hold this lock when running any command, and attempt to hold it when updating APIs. This ensures you don't
 | 
			
		||||
     * update APIs while also starting/stopping them.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #isOn
 | 
			
		||||
     * @see #tick()
 | 
			
		||||
     * @see #turnOn()
 | 
			
		||||
     * @see #shutdown()
 | 
			
		||||
     */
 | 
			
		||||
    private final ReentrantLock isOnLock = new ReentrantLock();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A lock used for any changes to {@link #eventQueue}, {@link #command} or {@link #onComputerQueue}. This will be
 | 
			
		||||
     * used on the main thread, so locks should be kept as brief as possible.
 | 
			
		||||
     */
 | 
			
		||||
    private final Object queueLock = new Object();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Determines if this executor is present within {@link ComputerThread}.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #queueLock
 | 
			
		||||
     * @see #enqueue()
 | 
			
		||||
     * @see #afterWork()
 | 
			
		||||
     */
 | 
			
		||||
    volatile boolean onComputerQueue = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The amount of time this computer has used on a theoretical machine which shares work evenly amongst computers.
 | 
			
		||||
     *
 | 
			
		||||
     * @see ComputerThread
 | 
			
		||||
     */
 | 
			
		||||
    long virtualRuntime = 0;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The last time at which we updated {@link #virtualRuntime}.
 | 
			
		||||
     *
 | 
			
		||||
     * @see ComputerThread
 | 
			
		||||
     */
 | 
			
		||||
    long vRuntimeStart;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The command that {@link #work()} should execute on the computer thread.
 | 
			
		||||
     *
 | 
			
		||||
     * One sets the command with {@link #queueStart()} and {@link #queueStop(boolean, boolean)}. Neither of these will
 | 
			
		||||
     * queue a new event if there is an existing one in the queue.
 | 
			
		||||
     *
 | 
			
		||||
     * Note, if command is not {@code null}, then some command is scheduled to be executed. Otherwise it is not
 | 
			
		||||
     * currently in the queue (or is currently being executed).
 | 
			
		||||
     */
 | 
			
		||||
    private volatile StateCommand command;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The queue of events which should be executed when this computer is on.
 | 
			
		||||
     *
 | 
			
		||||
     * Note, this should be empty if this computer is off - it is cleared on shutdown and when turning on again.
 | 
			
		||||
     */
 | 
			
		||||
    private final Queue<Event> eventQueue = new ArrayDeque<>( 4 );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether we interrupted an event and so should resume it instead of executing another task.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #work()
 | 
			
		||||
     * @see #resumeMachine(String, Object[])
 | 
			
		||||
     */
 | 
			
		||||
    private boolean interruptedEvent = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether this executor has been closed, and will no longer accept any incoming commands or events.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #queueStop(boolean, boolean)
 | 
			
		||||
     */
 | 
			
		||||
    private boolean closed;
 | 
			
		||||
 | 
			
		||||
    private IWritableMount rootMount;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The thread the executor is running on. This is non-null when performing work. We use this to ensure we're only
 | 
			
		||||
     * doing one bit of work at one time.
 | 
			
		||||
     *
 | 
			
		||||
     * @see ComputerThread
 | 
			
		||||
     */
 | 
			
		||||
    final AtomicReference<Thread> executingThread = new AtomicReference<>();
 | 
			
		||||
 | 
			
		||||
    ComputerExecutor( Computer computer )
 | 
			
		||||
    {
 | 
			
		||||
        // Ensure the computer thread is running as required.
 | 
			
		||||
        ComputerThread.start();
 | 
			
		||||
 | 
			
		||||
        this.computer = computer;
 | 
			
		||||
 | 
			
		||||
        Environment environment = computer.getEnvironment();
 | 
			
		||||
 | 
			
		||||
        // Add all default APIs to the loaded list.
 | 
			
		||||
        apis.add( new TermAPI( environment ) );
 | 
			
		||||
        apis.add( new RedstoneAPI( environment ) );
 | 
			
		||||
        apis.add( new FSAPI( environment ) );
 | 
			
		||||
        apis.add( new PeripheralAPI( environment ) );
 | 
			
		||||
        apis.add( new OSAPI( environment ) );
 | 
			
		||||
        if( ComputerCraft.http_enable ) apis.add( new HTTPAPI( environment ) );
 | 
			
		||||
 | 
			
		||||
        // Load in the externally registered APIs.
 | 
			
		||||
        for( ILuaAPIFactory factory : ApiFactories.getAll() )
 | 
			
		||||
        {
 | 
			
		||||
            ComputerSystem system = new ComputerSystem( environment );
 | 
			
		||||
            ILuaAPI api = factory.create( system );
 | 
			
		||||
            if( api != null ) apis.add( new ApiWrapper( api, system ) );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    boolean isOn()
 | 
			
		||||
    {
 | 
			
		||||
        return isOn;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    FileSystem getFileSystem()
 | 
			
		||||
    {
 | 
			
		||||
        return fileSystem;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    Computer getComputer()
 | 
			
		||||
    {
 | 
			
		||||
        return computer;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void addApi( ILuaAPI api )
 | 
			
		||||
    {
 | 
			
		||||
        apis.add( api );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Schedule this computer to be started if not already on.
 | 
			
		||||
     */
 | 
			
		||||
    void queueStart()
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            // We should only schedule a start if we're not currently on and there's turn on.
 | 
			
		||||
            if( closed || isOn || command != null ) return;
 | 
			
		||||
 | 
			
		||||
            command = StateCommand.TURN_ON;
 | 
			
		||||
            enqueue();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Schedule this computer to be stopped if not already on.
 | 
			
		||||
     *
 | 
			
		||||
     * @param reboot Reboot the computer after stopping
 | 
			
		||||
     * @param close  Close the computer after stopping.
 | 
			
		||||
     * @see #closed
 | 
			
		||||
     */
 | 
			
		||||
    void queueStop( boolean reboot, boolean close )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( closed ) return;
 | 
			
		||||
            closed = close;
 | 
			
		||||
 | 
			
		||||
            StateCommand newCommand = reboot ? StateCommand.REBOOT : StateCommand.SHUTDOWN;
 | 
			
		||||
 | 
			
		||||
            // We should only schedule a stop if we're currently on and there's no shutdown pending.
 | 
			
		||||
            if( !isOn || command != null )
 | 
			
		||||
            {
 | 
			
		||||
                // If we're closing, set the command just in case.
 | 
			
		||||
                if( close ) command = newCommand;
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            command = newCommand;
 | 
			
		||||
            enqueue();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Abort this whole computer due to a timeout. This will immediately destroy the Lua machine,
 | 
			
		||||
     * and then schedule a shutdown.
 | 
			
		||||
     */
 | 
			
		||||
    void abort()
 | 
			
		||||
    {
 | 
			
		||||
        ILuaMachine machine = this.machine;
 | 
			
		||||
        if( machine != null ) machine.close();
 | 
			
		||||
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( closed ) return;
 | 
			
		||||
            command = StateCommand.ABORT;
 | 
			
		||||
            if( isOn ) enqueue();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Queue an event if the computer is on
 | 
			
		||||
     *
 | 
			
		||||
     * @param event The event's name
 | 
			
		||||
     * @param args  The event's arguments
 | 
			
		||||
     */
 | 
			
		||||
    void queueEvent( @Nonnull String event, @Nullable Object[] args )
 | 
			
		||||
    {
 | 
			
		||||
        // Events should be skipped if we're not on.
 | 
			
		||||
        if( !isOn ) return;
 | 
			
		||||
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            // And if we've got some command in the pipeline, then don't queue events - they'll
 | 
			
		||||
            // probably be disposed of anyway.
 | 
			
		||||
            // We also limit the number of events which can be queued.
 | 
			
		||||
            if( closed || command != null || eventQueue.size() >= QUEUE_LIMIT ) return;
 | 
			
		||||
 | 
			
		||||
            eventQueue.offer( new Event( event, args ) );
 | 
			
		||||
            enqueue();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Add this executor to the {@link ComputerThread} if not already there.
 | 
			
		||||
     */
 | 
			
		||||
    private void enqueue()
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( !onComputerQueue ) ComputerThread.queue( this );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Update the internals of the executor.
 | 
			
		||||
     */
 | 
			
		||||
    void tick()
 | 
			
		||||
    {
 | 
			
		||||
        if( isOn && isOnLock.tryLock() )
 | 
			
		||||
        {
 | 
			
		||||
            // This horrific structure means we don't try to update APIs while the state is being changed
 | 
			
		||||
            // (and so they may be running startup/shutdown).
 | 
			
		||||
            // We use tryLock here, as it has minimal delay, and it doesn't matter if we miss an advance at the
 | 
			
		||||
            // beginning or end of a computer's lifetime.
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                if( isOn )
 | 
			
		||||
                {
 | 
			
		||||
                    // Advance our APIs.
 | 
			
		||||
                    for( ILuaAPI api : apis ) api.update();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            finally
 | 
			
		||||
            {
 | 
			
		||||
                isOnLock.unlock();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private IMount getRomMount()
 | 
			
		||||
    {
 | 
			
		||||
        if( romMount != null ) return romMount;
 | 
			
		||||
 | 
			
		||||
        synchronized( romMountLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( romMount != null ) return romMount;
 | 
			
		||||
            return romMount = computer.getComputerEnvironment().createResourceMount( "computercraft", "lua/rom" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    IWritableMount getRootMount()
 | 
			
		||||
    {
 | 
			
		||||
        if( rootMount == null )
 | 
			
		||||
        {
 | 
			
		||||
            rootMount = computer.getComputerEnvironment().createSaveDirMount(
 | 
			
		||||
                "computer/" + computer.assignID(),
 | 
			
		||||
                computer.getComputerEnvironment().getComputerSpaceLimit()
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
        return rootMount;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private FileSystem createFileSystem()
 | 
			
		||||
    {
 | 
			
		||||
        FileSystem filesystem = null;
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            filesystem = new FileSystem( "hdd", getRootMount() );
 | 
			
		||||
 | 
			
		||||
            IMount romMount = getRomMount();
 | 
			
		||||
            if( romMount == null )
 | 
			
		||||
            {
 | 
			
		||||
                displayFailure( "Cannot mount ROM", null );
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            filesystem.mount( "rom", "rom", romMount );
 | 
			
		||||
            return filesystem;
 | 
			
		||||
        }
 | 
			
		||||
        catch( FileSystemException e )
 | 
			
		||||
        {
 | 
			
		||||
            if( filesystem != null ) filesystem.close();
 | 
			
		||||
            ComputerCraft.log.error( "Cannot mount computer filesystem", e );
 | 
			
		||||
 | 
			
		||||
            displayFailure( "Cannot mount computer system", null );
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private ILuaMachine createLuaMachine()
 | 
			
		||||
    {
 | 
			
		||||
        // Load the bios resource
 | 
			
		||||
        InputStream biosStream = null;
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            biosStream = computer.getComputerEnvironment().createResourceFile( "computercraft", "lua/bios.lua" );
 | 
			
		||||
        }
 | 
			
		||||
        catch( Exception ignored )
 | 
			
		||||
        {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if( biosStream == null )
 | 
			
		||||
        {
 | 
			
		||||
            displayFailure( "Error loading bios.lua", null );
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Create the lua machine
 | 
			
		||||
        ILuaMachine machine = new CobaltLuaMachine( computer, timeout );
 | 
			
		||||
 | 
			
		||||
        // Add the APIs
 | 
			
		||||
        for( ILuaAPI api : apis ) machine.addAPI( api );
 | 
			
		||||
 | 
			
		||||
        // Start the machine running the bios resource
 | 
			
		||||
        MachineResult result = machine.loadBios( biosStream );
 | 
			
		||||
        IoUtil.closeQuietly( biosStream );
 | 
			
		||||
 | 
			
		||||
        if( result.isError() )
 | 
			
		||||
        {
 | 
			
		||||
            machine.close();
 | 
			
		||||
            displayFailure( "Error loading bios.lua", result.getMessage() );
 | 
			
		||||
            return null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return machine;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void turnOn() throws InterruptedException
 | 
			
		||||
    {
 | 
			
		||||
        isOnLock.lockInterruptibly();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            // Reset the terminal and event queue
 | 
			
		||||
            computer.getTerminal().reset();
 | 
			
		||||
            interruptedEvent = false;
 | 
			
		||||
            synchronized( queueLock )
 | 
			
		||||
            {
 | 
			
		||||
                eventQueue.clear();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Init filesystem
 | 
			
		||||
            if( (fileSystem = createFileSystem()) == null )
 | 
			
		||||
            {
 | 
			
		||||
                shutdown();
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Init APIs
 | 
			
		||||
            for( ILuaAPI api : apis ) api.startup();
 | 
			
		||||
 | 
			
		||||
            // Init lua
 | 
			
		||||
            if( (machine = createLuaMachine()) == null )
 | 
			
		||||
            {
 | 
			
		||||
                shutdown();
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Initialisation has finished, so let's mark ourselves as on.
 | 
			
		||||
            isOn = true;
 | 
			
		||||
            computer.markChanged();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            isOnLock.unlock();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Now actually start the computer, now that everything is set up.
 | 
			
		||||
        resumeMachine( null, null );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void shutdown() throws InterruptedException
 | 
			
		||||
    {
 | 
			
		||||
        isOnLock.lockInterruptibly();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            isOn = false;
 | 
			
		||||
            interruptedEvent = false;
 | 
			
		||||
            synchronized( queueLock )
 | 
			
		||||
            {
 | 
			
		||||
                eventQueue.clear();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Shutdown Lua machine
 | 
			
		||||
            if( machine != null )
 | 
			
		||||
            {
 | 
			
		||||
                machine.close();
 | 
			
		||||
                machine = null;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            // Shutdown our APIs
 | 
			
		||||
            for( ILuaAPI api : apis ) api.shutdown();
 | 
			
		||||
 | 
			
		||||
            // Unload filesystem
 | 
			
		||||
            if( fileSystem != null )
 | 
			
		||||
            {
 | 
			
		||||
                fileSystem.close();
 | 
			
		||||
                fileSystem = null;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            computer.getEnvironment().resetOutput();
 | 
			
		||||
            computer.markChanged();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            isOnLock.unlock();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Called before calling {@link #work()}, setting up any important state.
 | 
			
		||||
     */
 | 
			
		||||
    void beforeWork()
 | 
			
		||||
    {
 | 
			
		||||
        vRuntimeStart = System.nanoTime();
 | 
			
		||||
        timeout.startTimer();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Called after executing {@link #work()}.
 | 
			
		||||
     *
 | 
			
		||||
     * @return If we have more work to do.
 | 
			
		||||
     */
 | 
			
		||||
    boolean afterWork()
 | 
			
		||||
    {
 | 
			
		||||
        if( interruptedEvent )
 | 
			
		||||
        {
 | 
			
		||||
            timeout.pauseTimer();
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            timeout.stopTimer();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Tracking.addTaskTiming( getComputer(), timeout.nanoCurrent() );
 | 
			
		||||
 | 
			
		||||
        if( interruptedEvent ) return true;
 | 
			
		||||
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( eventQueue.isEmpty() && command == null ) return onComputerQueue = false;
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The main worker function, called by {@link ComputerThread}.
 | 
			
		||||
     *
 | 
			
		||||
     * This either executes a {@link StateCommand} or attempts to run an event
 | 
			
		||||
     *
 | 
			
		||||
     * @throws InterruptedException If various locks could not be acquired.
 | 
			
		||||
     * @see #command
 | 
			
		||||
     * @see #eventQueue
 | 
			
		||||
     */
 | 
			
		||||
    void work() throws InterruptedException
 | 
			
		||||
    {
 | 
			
		||||
        if( interruptedEvent )
 | 
			
		||||
        {
 | 
			
		||||
            interruptedEvent = false;
 | 
			
		||||
            if( machine != null )
 | 
			
		||||
            {
 | 
			
		||||
                resumeMachine( null, null );
 | 
			
		||||
                return;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        StateCommand command;
 | 
			
		||||
        Event event = null;
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            command = this.command;
 | 
			
		||||
            this.command = null;
 | 
			
		||||
 | 
			
		||||
            // If we've no command, pull something from the event queue instead.
 | 
			
		||||
            if( command == null )
 | 
			
		||||
            {
 | 
			
		||||
                if( !isOn )
 | 
			
		||||
                {
 | 
			
		||||
                    // We're not on and had no command, but we had work queued. This should never happen, so clear
 | 
			
		||||
                    // the event queue just in case.
 | 
			
		||||
                    eventQueue.clear();
 | 
			
		||||
                    return;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                event = eventQueue.poll();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if( command != null )
 | 
			
		||||
        {
 | 
			
		||||
            switch( command )
 | 
			
		||||
            {
 | 
			
		||||
                case TURN_ON:
 | 
			
		||||
                    if( isOn ) return;
 | 
			
		||||
                    turnOn();
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case SHUTDOWN:
 | 
			
		||||
 | 
			
		||||
                    if( !isOn ) return;
 | 
			
		||||
                    computer.getTerminal().reset();
 | 
			
		||||
                    shutdown();
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case REBOOT:
 | 
			
		||||
                    if( !isOn ) return;
 | 
			
		||||
                    computer.getTerminal().reset();
 | 
			
		||||
                    shutdown();
 | 
			
		||||
 | 
			
		||||
                    computer.turnOn();
 | 
			
		||||
                    break;
 | 
			
		||||
 | 
			
		||||
                case ABORT:
 | 
			
		||||
                    if( !isOn ) return;
 | 
			
		||||
                    displayFailure( "Error running computer", TimeoutState.ABORT_MESSAGE );
 | 
			
		||||
                    shutdown();
 | 
			
		||||
                    break;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else if( event != null )
 | 
			
		||||
        {
 | 
			
		||||
            resumeMachine( event.name, event.args );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void displayFailure( String message, String extra )
 | 
			
		||||
    {
 | 
			
		||||
        Terminal terminal = computer.getTerminal();
 | 
			
		||||
        boolean colour = computer.getComputerEnvironment().isColour();
 | 
			
		||||
        terminal.reset();
 | 
			
		||||
 | 
			
		||||
        // Display our primary error message
 | 
			
		||||
        if( colour ) terminal.setTextColour( 15 - Colour.Red.ordinal() );
 | 
			
		||||
        terminal.write( message );
 | 
			
		||||
 | 
			
		||||
        if( extra != null )
 | 
			
		||||
        {
 | 
			
		||||
            // Display any additional information. This generally comes from the Lua Machine, such as compilation or
 | 
			
		||||
            // runtime errors.
 | 
			
		||||
            terminal.setCursorPos( 0, terminal.getCursorY() + 1 );
 | 
			
		||||
            terminal.write( extra );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // And display our generic "CC may be installed incorrectly" message.
 | 
			
		||||
        terminal.setCursorPos( 0, terminal.getCursorY() + 1 );
 | 
			
		||||
        if( colour ) terminal.setTextColour( 15 - Colour.White.ordinal() );
 | 
			
		||||
        terminal.write( "ComputerCraft may be installed incorrectly" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void resumeMachine( String event, Object[] args ) throws InterruptedException
 | 
			
		||||
    {
 | 
			
		||||
        MachineResult result = machine.handleEvent( event, args );
 | 
			
		||||
        interruptedEvent = result.isPause();
 | 
			
		||||
        if( !result.isError() ) return;
 | 
			
		||||
 | 
			
		||||
        displayFailure( "Error running computer", result.getMessage() );
 | 
			
		||||
        shutdown();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private enum StateCommand
 | 
			
		||||
    {
 | 
			
		||||
        TURN_ON,
 | 
			
		||||
        SHUTDOWN,
 | 
			
		||||
        REBOOT,
 | 
			
		||||
        ABORT,
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static final class Event
 | 
			
		||||
    {
 | 
			
		||||
        final String name;
 | 
			
		||||
        final Object[] args;
 | 
			
		||||
 | 
			
		||||
        private Event( String name, Object[] args )
 | 
			
		||||
        {
 | 
			
		||||
            this.name = name;
 | 
			
		||||
            this.args = args;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,58 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.filesystem.IFileSystem;
 | 
			
		||||
import dan200.computercraft.api.lua.IComputerSystem;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaAPIFactory;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IComputerAccess;
 | 
			
		||||
import dan200.computercraft.core.apis.ComputerAccess;
 | 
			
		||||
import dan200.computercraft.core.apis.IAPIEnvironment;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystem;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Implementation of {@link IComputerAccess}/{@link IComputerSystem} for usage by externally registered APIs.
 | 
			
		||||
 *
 | 
			
		||||
 * @see dan200.computercraft.api.ComputerCraftAPI#registerAPIFactory(ILuaAPIFactory)
 | 
			
		||||
 * @see ILuaAPIFactory
 | 
			
		||||
 * @see ApiWrapper
 | 
			
		||||
 */
 | 
			
		||||
public class ComputerSystem extends ComputerAccess implements IComputerSystem
 | 
			
		||||
{
 | 
			
		||||
    private final IAPIEnvironment m_environment;
 | 
			
		||||
 | 
			
		||||
    ComputerSystem( IAPIEnvironment environment )
 | 
			
		||||
    {
 | 
			
		||||
        super( environment );
 | 
			
		||||
        this.m_environment = environment;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getAttachmentName()
 | 
			
		||||
    {
 | 
			
		||||
        return "computer";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    @Override
 | 
			
		||||
    public IFileSystem getFileSystem()
 | 
			
		||||
    {
 | 
			
		||||
        FileSystem fs = m_environment.getFileSystem();
 | 
			
		||||
        return fs == null ? null : fs.getMountWrapper();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getLabel()
 | 
			
		||||
    {
 | 
			
		||||
        return m_environment.getLabel();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -7,152 +7,351 @@
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.core.tracking.Tracking;
 | 
			
		||||
import dan200.computercraft.shared.util.ThreadUtils;
 | 
			
		||||
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.WeakHashMap;
 | 
			
		||||
import java.util.concurrent.BlockingQueue;
 | 
			
		||||
import java.util.concurrent.LinkedBlockingQueue;
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.util.TreeSet;
 | 
			
		||||
import java.util.concurrent.ThreadFactory;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicReference;
 | 
			
		||||
import java.util.concurrent.locks.Condition;
 | 
			
		||||
import java.util.concurrent.locks.LockSupport;
 | 
			
		||||
import java.util.concurrent.locks.ReentrantLock;
 | 
			
		||||
 | 
			
		||||
public class ComputerThread
 | 
			
		||||
import static dan200.computercraft.core.computer.TimeoutState.ABORT_TIMEOUT;
 | 
			
		||||
import static dan200.computercraft.core.computer.TimeoutState.TIMEOUT;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Responsible for running all tasks from a {@link Computer}.
 | 
			
		||||
 *
 | 
			
		||||
 * This is split into two components: the {@link TaskRunner}s, which pull an executor from the queue and execute it, and
 | 
			
		||||
 * a single {@link Monitor} which observes all runners and kills them if they have not been terminated by
 | 
			
		||||
 * {@link TimeoutState#isSoftAborted()}.
 | 
			
		||||
 *
 | 
			
		||||
 * Computers are executed using a priority system, with those who have spent less time executing having a higher
 | 
			
		||||
 * priority than those hogging the thread. This, combined with {@link TimeoutState#isPaused()} means we can reduce the
 | 
			
		||||
 * risk of badly behaved computers stalling execution for everyone else.
 | 
			
		||||
 *
 | 
			
		||||
 * This is done using an implementation of Linux's Completely Fair Scheduler. When a computer executes, we compute what
 | 
			
		||||
 * share of execution time it has used (time executed/number of tasks). We then pick the computer who has the least
 | 
			
		||||
 * "virtual execution time" (aka {@link ComputerExecutor#virtualRuntime}).
 | 
			
		||||
 *
 | 
			
		||||
 * When adding a computer to the queue, we make sure its "virtual runtime" is at least as big as the smallest runtime.
 | 
			
		||||
 * This means that adding computers which have slept a lot do not then have massive priority over everyone else. See
 | 
			
		||||
 * {@link #queue(ComputerExecutor)} for how this is implemented.
 | 
			
		||||
 *
 | 
			
		||||
 * In reality, it's unlikely that more than a few computers are waiting to execute at once, so this will not have much
 | 
			
		||||
 * effect unless you have a computer hogging execution time. However, it is pretty effective in those situations.
 | 
			
		||||
 *
 | 
			
		||||
 * @see TimeoutState For how hard timeouts are handled.
 | 
			
		||||
 * @see ComputerExecutor For how computers actually do execution.
 | 
			
		||||
 */
 | 
			
		||||
public final class ComputerThread
 | 
			
		||||
{
 | 
			
		||||
    private static final int QUEUE_LIMIT = 256;
 | 
			
		||||
    /**
 | 
			
		||||
     * How often the computer thread monitor should run, in milliseconds
 | 
			
		||||
     *
 | 
			
		||||
     * @see Monitor
 | 
			
		||||
     */
 | 
			
		||||
    private static final int MONITOR_WAKEUP = 100;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lock used for modifications to the object
 | 
			
		||||
     * The target latency between executing two tasks on a single machine.
 | 
			
		||||
     *
 | 
			
		||||
     * An average tick takes 50ms, and so we ideally need to have handled a couple of events within that window in order
 | 
			
		||||
     * to have a perceived low latency.
 | 
			
		||||
     */
 | 
			
		||||
    private static final Object s_stateLock = new Object();
 | 
			
		||||
    private static final long DEFAULT_LATENCY = TimeUnit.MILLISECONDS.toNanos( 50 );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lock for various task operations
 | 
			
		||||
     * The minimum value that {@link #DEFAULT_LATENCY} can have when scaled.
 | 
			
		||||
     *
 | 
			
		||||
     * From statistics gathered on SwitchCraft, almost all machines will execute under 15ms, 75% under 1.5ms, with the
 | 
			
		||||
     * mean being about 3ms. Most computers shouldn't be too impacted with having such a short period to execute in.
 | 
			
		||||
     */
 | 
			
		||||
    private static final Object s_taskLock = new Object();
 | 
			
		||||
    private static final long DEFAULT_MIN_PERIOD = TimeUnit.MILLISECONDS.toNanos( 5 );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Map of objects to task list
 | 
			
		||||
     * The maximum number of tasks before we have to start scaling latency linearly.
 | 
			
		||||
     */
 | 
			
		||||
    private static final WeakHashMap<Object, BlockingQueue<ITask>> s_computerTaskQueues = new WeakHashMap<>();
 | 
			
		||||
    private static final long LATENCY_MAX_TASKS = DEFAULT_LATENCY / DEFAULT_MIN_PERIOD;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Lock used for modifications to the array of current threads.
 | 
			
		||||
     */
 | 
			
		||||
    private static final Object threadLock = new Object();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether the computer thread system is currently running
 | 
			
		||||
     */
 | 
			
		||||
    private static volatile boolean running = false;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The current task manager.
 | 
			
		||||
     */
 | 
			
		||||
    private static Thread monitor;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The array of current runners, and their owning threads.
 | 
			
		||||
     */
 | 
			
		||||
    private static TaskRunner[] runners;
 | 
			
		||||
 | 
			
		||||
    private static long latency;
 | 
			
		||||
    private static long minPeriod;
 | 
			
		||||
 | 
			
		||||
    private static final ReentrantLock computerLock = new ReentrantLock();
 | 
			
		||||
 | 
			
		||||
    private static final Condition hasWork = computerLock.newCondition();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Active queues to execute
 | 
			
		||||
     */
 | 
			
		||||
    private static final BlockingQueue<BlockingQueue<ITask>> s_computerTasksActive = new LinkedBlockingQueue<>();
 | 
			
		||||
    private static final Set<BlockingQueue<ITask>> s_computerTasksActiveSet = new HashSet<>();
 | 
			
		||||
    private static final TreeSet<ComputerExecutor> computerQueue = new TreeSet<>( ( a, b ) -> {
 | 
			
		||||
        if( a == b ) return 0; // Should never happen, but let's be consistent here
 | 
			
		||||
 | 
			
		||||
        long at = a.virtualRuntime, bt = b.virtualRuntime;
 | 
			
		||||
        if( at == bt ) return Integer.compare( a.hashCode(), b.hashCode() );
 | 
			
		||||
        return at < bt ? -1 : 1;
 | 
			
		||||
    } );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The default object for items which don't have an owner
 | 
			
		||||
     * The minimum {@link ComputerExecutor#virtualRuntime} time on the tree.
 | 
			
		||||
     */
 | 
			
		||||
    private static final Object s_defaultOwner = new Object();
 | 
			
		||||
    private static long minimumVirtualRuntime = 0;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether the thread is stopped or should be stopped
 | 
			
		||||
     */
 | 
			
		||||
    private static boolean s_stopped = false;
 | 
			
		||||
    private static final ThreadFactory monitorFactory = ThreadUtils.factory( "Computer-Monitor" );
 | 
			
		||||
    private static final ThreadFactory runnerFactory = ThreadUtils.factory( "Computer-Runner" );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The thread tasks execute on
 | 
			
		||||
     */
 | 
			
		||||
    private static Thread[] s_threads = null;
 | 
			
		||||
 | 
			
		||||
    private static final ThreadFactory s_ManagerFactory = ThreadUtils.factory( "Computer-Manager" );
 | 
			
		||||
    private static final ThreadFactory s_RunnerFactory = ThreadUtils.factory( "Computer-Runner" );
 | 
			
		||||
    private ComputerThread() {}
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Start the computer thread
 | 
			
		||||
     */
 | 
			
		||||
    public static void start()
 | 
			
		||||
    static void start()
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( s_stateLock )
 | 
			
		||||
        synchronized( threadLock )
 | 
			
		||||
        {
 | 
			
		||||
            s_stopped = false;
 | 
			
		||||
            if( s_threads == null || s_threads.length != ComputerCraft.computer_threads )
 | 
			
		||||
            running = true;
 | 
			
		||||
            if( monitor == null || !monitor.isAlive() ) (monitor = monitorFactory.newThread( new Monitor() )).start();
 | 
			
		||||
 | 
			
		||||
            if( runners == null )
 | 
			
		||||
            {
 | 
			
		||||
                s_threads = new Thread[ComputerCraft.computer_threads];
 | 
			
		||||
                // TODO: Change the runners length on config reloads
 | 
			
		||||
                runners = new TaskRunner[ComputerCraft.computer_threads];
 | 
			
		||||
 | 
			
		||||
                // latency and minPeriod are scaled by 1 + floor(log2(threads)). We can afford to execute tasks for
 | 
			
		||||
                // longer when executing on more than one thread.
 | 
			
		||||
                long factor = 64 - Long.numberOfLeadingZeros( runners.length );
 | 
			
		||||
                latency = DEFAULT_LATENCY * factor;
 | 
			
		||||
                minPeriod = DEFAULT_MIN_PERIOD * factor;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            for( int i = 0; i < s_threads.length; i++ )
 | 
			
		||||
            for( int i = 0; i < runners.length; i++ )
 | 
			
		||||
            {
 | 
			
		||||
                Thread thread = s_threads[i];
 | 
			
		||||
                if( thread == null || !thread.isAlive() )
 | 
			
		||||
                TaskRunner runner = runners[i];
 | 
			
		||||
                if( runner == null || runner.owner == null || !runner.owner.isAlive() )
 | 
			
		||||
                {
 | 
			
		||||
                    (s_threads[i] = s_ManagerFactory.newThread( new TaskExecutor() )).start();
 | 
			
		||||
                    // Mark the old runner as dead, just in case.
 | 
			
		||||
                    if( runner != null ) runner.running = false;
 | 
			
		||||
                    // And start a new runner
 | 
			
		||||
                    runnerFactory.newThread( runners[i] = new TaskRunner() ).start();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Attempt to stop the computer thread
 | 
			
		||||
     * Attempt to stop the computer thread. This interrupts each runner, and clears the task queue.
 | 
			
		||||
     */
 | 
			
		||||
    public static void stop()
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( s_stateLock )
 | 
			
		||||
        synchronized( threadLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( s_threads != null )
 | 
			
		||||
            running = false;
 | 
			
		||||
            if( runners != null )
 | 
			
		||||
            {
 | 
			
		||||
                s_stopped = true;
 | 
			
		||||
                for( Thread thread : s_threads )
 | 
			
		||||
                for( TaskRunner runner : runners )
 | 
			
		||||
                {
 | 
			
		||||
                    if( thread != null && thread.isAlive() )
 | 
			
		||||
                    {
 | 
			
		||||
                        thread.interrupt();
 | 
			
		||||
                    }
 | 
			
		||||
                    if( runner == null ) continue;
 | 
			
		||||
 | 
			
		||||
                    runner.running = false;
 | 
			
		||||
                    if( runner.owner != null ) runner.owner.interrupt();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        synchronized( s_taskLock )
 | 
			
		||||
        computerLock.lock();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            s_computerTaskQueues.clear();
 | 
			
		||||
            s_computerTasksActive.clear();
 | 
			
		||||
            s_computerTasksActiveSet.clear();
 | 
			
		||||
            computerQueue.clear();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            computerLock.unlock();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Queue a task to execute on the thread
 | 
			
		||||
     * Mark a computer as having work, enqueuing it on the thread
 | 
			
		||||
     *
 | 
			
		||||
     * @param task     The task to execute
 | 
			
		||||
     * @param computer The computer to execute it on, use {@code null} to execute on the default object.
 | 
			
		||||
     * You must be holding {@link ComputerExecutor}'s {@code queueLock} when calling this method - it should only
 | 
			
		||||
     * be called from {@code enqueue}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param executor The computer to execute work on.
 | 
			
		||||
     */
 | 
			
		||||
    public static void queueTask( ITask task, Computer computer )
 | 
			
		||||
    static void queue( @Nonnull ComputerExecutor executor )
 | 
			
		||||
    {
 | 
			
		||||
        Object queueObject = computer == null ? s_defaultOwner : computer;
 | 
			
		||||
 | 
			
		||||
        BlockingQueue<ITask> queue;
 | 
			
		||||
        synchronized( s_computerTaskQueues )
 | 
			
		||||
        computerLock.lock();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            queue = s_computerTaskQueues.get( queueObject );
 | 
			
		||||
            if( queue == null )
 | 
			
		||||
            if( executor.onComputerQueue ) throw new IllegalStateException( "Cannot queue already queued executor" );
 | 
			
		||||
            executor.onComputerQueue = true;
 | 
			
		||||
 | 
			
		||||
            updateRuntimes( null );
 | 
			
		||||
 | 
			
		||||
            // We're not currently on the queue, so update its current execution time to
 | 
			
		||||
            // ensure its at least as high as the minimum.
 | 
			
		||||
            long newRuntime = minimumVirtualRuntime;
 | 
			
		||||
 | 
			
		||||
            if( executor.virtualRuntime == 0 )
 | 
			
		||||
            {
 | 
			
		||||
                s_computerTaskQueues.put( queueObject, queue = new LinkedBlockingQueue<>( QUEUE_LIMIT ) );
 | 
			
		||||
                // Slow down new computers a little bit.
 | 
			
		||||
                newRuntime += scaledPeriod();
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                // Give a small boost to computers which have slept a little.
 | 
			
		||||
                newRuntime -= latency / 2;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            executor.virtualRuntime = Math.max( newRuntime, executor.virtualRuntime );
 | 
			
		||||
 | 
			
		||||
            // Add to the queue, and signal the workers.
 | 
			
		||||
            computerQueue.add( executor );
 | 
			
		||||
            hasWork.signal();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            computerLock.unlock();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Update the {@link ComputerExecutor#virtualRuntime}s of all running tasks, and then update the
 | 
			
		||||
     * {@link #minimumVirtualRuntime} based on the current tasks.
 | 
			
		||||
     *
 | 
			
		||||
     * This is called before queueing tasks, to ensure that {@link #minimumVirtualRuntime} is up-to-date.
 | 
			
		||||
     */
 | 
			
		||||
    private static void updateRuntimes( @Nullable ComputerExecutor current )
 | 
			
		||||
    {
 | 
			
		||||
        long minRuntime = Long.MAX_VALUE;
 | 
			
		||||
 | 
			
		||||
        // If we've a task on the queue, use that as our base time.
 | 
			
		||||
        if( !computerQueue.isEmpty() ) minRuntime = computerQueue.first().virtualRuntime;
 | 
			
		||||
 | 
			
		||||
        // Update all the currently executing tasks
 | 
			
		||||
        long now = System.nanoTime();
 | 
			
		||||
        int tasks = 1 + computerQueue.size();
 | 
			
		||||
        TaskRunner[] currentRunners = runners;
 | 
			
		||||
        if( currentRunners != null )
 | 
			
		||||
        {
 | 
			
		||||
            for( TaskRunner runner : currentRunners )
 | 
			
		||||
            {
 | 
			
		||||
                if( runner == null ) continue;
 | 
			
		||||
                ComputerExecutor executor = runner.currentExecutor.get();
 | 
			
		||||
                if( executor == null ) continue;
 | 
			
		||||
 | 
			
		||||
                // We do two things here: first we update the task's virtual runtime based on when we
 | 
			
		||||
                // last checked, and then we check the minimum.
 | 
			
		||||
                minRuntime = Math.min( minRuntime, executor.virtualRuntime += (now - executor.vRuntimeStart) / tasks );
 | 
			
		||||
                executor.vRuntimeStart = now;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        synchronized( s_taskLock )
 | 
			
		||||
        // And update the most recently executed one (if set).
 | 
			
		||||
        if( current != null )
 | 
			
		||||
        {
 | 
			
		||||
            if( queue.offer( task ) && !s_computerTasksActiveSet.contains( queue ) )
 | 
			
		||||
            {
 | 
			
		||||
                s_computerTasksActive.add( queue );
 | 
			
		||||
                s_computerTasksActiveSet.add( queue );
 | 
			
		||||
            }
 | 
			
		||||
            minRuntime = Math.min( minRuntime, current.virtualRuntime += (now - current.vRuntimeStart) / tasks );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if( minRuntime > minimumVirtualRuntime && minRuntime < Long.MAX_VALUE )
 | 
			
		||||
        {
 | 
			
		||||
            minimumVirtualRuntime = minRuntime;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Responsible for pulling and managing computer tasks. This pulls a task from {@link #s_computerTasksActive},
 | 
			
		||||
     * creates a new thread using {@link TaskRunner} or reuses a previous one and uses that to execute the task.
 | 
			
		||||
     * Ensure the "currently working" state of the executor is reset, the timings are updated, and then requeue the
 | 
			
		||||
     * executor if needed.
 | 
			
		||||
     *
 | 
			
		||||
     * If the task times out, then it will attempt to interrupt the {@link TaskRunner} instance.
 | 
			
		||||
     * @param runner   The runner this task was on.
 | 
			
		||||
     * @param executor The executor to requeue
 | 
			
		||||
     */
 | 
			
		||||
    private static final class TaskExecutor implements Runnable
 | 
			
		||||
    private static void afterWork( TaskRunner runner, ComputerExecutor executor )
 | 
			
		||||
    {
 | 
			
		||||
        private TaskRunner runner;
 | 
			
		||||
        private Thread thread;
 | 
			
		||||
        // Clear the executor's thread.
 | 
			
		||||
        Thread currentThread = executor.executingThread.getAndSet( null );
 | 
			
		||||
        if( currentThread != runner.owner )
 | 
			
		||||
        {
 | 
			
		||||
            ComputerCraft.log.error(
 | 
			
		||||
                "Expected computer #{} to be running on {}, but already running on {}. This is a SERIOUS bug, please report with your debug.log.",
 | 
			
		||||
                executor.getComputer().getID(), runner.owner.getName(), currentThread == null ? "nothing" : currentThread.getName()
 | 
			
		||||
            );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        computerLock.lock();
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            updateRuntimes( executor );
 | 
			
		||||
 | 
			
		||||
            // If we've no more tasks, just return.
 | 
			
		||||
            if( !executor.afterWork() ) return;
 | 
			
		||||
 | 
			
		||||
            // Otherwise, add to the queue, and signal any waiting workers.
 | 
			
		||||
            computerQueue.add( executor );
 | 
			
		||||
            hasWork.signal();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            computerLock.unlock();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The scaled period for a single task
 | 
			
		||||
     *
 | 
			
		||||
     * @return The scaled period for the task
 | 
			
		||||
     * @see #DEFAULT_LATENCY
 | 
			
		||||
     * @see #DEFAULT_MIN_PERIOD
 | 
			
		||||
     * @see #LATENCY_MAX_TASKS
 | 
			
		||||
     */
 | 
			
		||||
    static long scaledPeriod()
 | 
			
		||||
    {
 | 
			
		||||
        // +1 to include the current task
 | 
			
		||||
        int count = 1 + computerQueue.size();
 | 
			
		||||
        return count < LATENCY_MAX_TASKS ? latency / count : minPeriod;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Determine if the thread has computers queued up
 | 
			
		||||
     *
 | 
			
		||||
     * @return If we have work queued up.
 | 
			
		||||
     */
 | 
			
		||||
    static boolean hasPendingWork()
 | 
			
		||||
    {
 | 
			
		||||
        return !computerQueue.isEmpty();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Observes all currently active {@link TaskRunner}s and terminates their tasks once they have exceeded the hard
 | 
			
		||||
     * abort limit.
 | 
			
		||||
     *
 | 
			
		||||
     * @see TimeoutState
 | 
			
		||||
     */
 | 
			
		||||
    private static final class Monitor implements Runnable
 | 
			
		||||
    {
 | 
			
		||||
        @Override
 | 
			
		||||
        public void run()
 | 
			
		||||
        {
 | 
			
		||||
@@ -160,186 +359,171 @@ public class ComputerThread
 | 
			
		||||
            {
 | 
			
		||||
                while( true )
 | 
			
		||||
                {
 | 
			
		||||
                    // Wait for an active queue to execute
 | 
			
		||||
                    BlockingQueue<ITask> queue = s_computerTasksActive.take();
 | 
			
		||||
                    Thread.sleep( MONITOR_WAKEUP );
 | 
			
		||||
 | 
			
		||||
                    // If threads should be stopped then return
 | 
			
		||||
                    synchronized( s_stateLock )
 | 
			
		||||
                    TaskRunner[] currentRunners = ComputerThread.runners;
 | 
			
		||||
                    if( currentRunners != null )
 | 
			
		||||
                    {
 | 
			
		||||
                        if( s_stopped ) return;
 | 
			
		||||
                    }
 | 
			
		||||
                        for( int i = 0; i < currentRunners.length; i++ )
 | 
			
		||||
                        {
 | 
			
		||||
                            TaskRunner runner = currentRunners[i];
 | 
			
		||||
                            // If we've no runner, skip.
 | 
			
		||||
                            if( runner == null ) continue;
 | 
			
		||||
 | 
			
		||||
                    execute( queue );
 | 
			
		||||
                            // If the runner has no work, skip
 | 
			
		||||
                            ComputerExecutor executor = runner.currentExecutor.get();
 | 
			
		||||
                            if( executor == null ) continue;
 | 
			
		||||
 | 
			
		||||
                            // If we're still within normal execution times (TIMEOUT) or soft abort (ABORT_TIMEOUT),
 | 
			
		||||
                            // then we can let the Lua machine do its work.
 | 
			
		||||
                            long afterStart = executor.timeout.nanoCumulative();
 | 
			
		||||
                            long afterHardAbort = afterStart - TIMEOUT - ABORT_TIMEOUT;
 | 
			
		||||
                            if( afterHardAbort < 0 ) continue;
 | 
			
		||||
 | 
			
		||||
                            // Set the hard abort flag.
 | 
			
		||||
                            executor.timeout.hardAbort();
 | 
			
		||||
                            executor.abort();
 | 
			
		||||
 | 
			
		||||
                            if( afterHardAbort >= ABORT_TIMEOUT )
 | 
			
		||||
                            {
 | 
			
		||||
                                // If we've hard aborted but we're still not dead, dump the stack trace and interrupt
 | 
			
		||||
                                // the task.
 | 
			
		||||
                                timeoutTask( executor, runner.owner, afterStart );
 | 
			
		||||
                                runner.owner.interrupt();
 | 
			
		||||
                            }
 | 
			
		||||
                            else if( afterHardAbort >= ABORT_TIMEOUT * 2 )
 | 
			
		||||
                            {
 | 
			
		||||
                                // If we've hard aborted and interrupted, and we're still not dead, then mark the runner
 | 
			
		||||
                                // as dead, finish off the task, and spawn a new runner.
 | 
			
		||||
                                timeoutTask( executor, runner.owner, afterStart );
 | 
			
		||||
                                runner.running = false;
 | 
			
		||||
                                runner.owner.interrupt();
 | 
			
		||||
 | 
			
		||||
                                ComputerExecutor thisExecutor = runner.currentExecutor.getAndSet( null );
 | 
			
		||||
                                if( thisExecutor != null ) afterWork( runner, executor );
 | 
			
		||||
 | 
			
		||||
                                synchronized( threadLock )
 | 
			
		||||
                                {
 | 
			
		||||
                                    if( running && runners.length > i && runners[i] == runner )
 | 
			
		||||
                                    {
 | 
			
		||||
                                        runnerFactory.newThread( currentRunners[i] = new TaskRunner() ).start();
 | 
			
		||||
                                    }
 | 
			
		||||
                                }
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch( InterruptedException ignored )
 | 
			
		||||
            {
 | 
			
		||||
                Thread.currentThread().interrupt();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void execute( BlockingQueue<ITask> queue ) throws InterruptedException
 | 
			
		||||
        {
 | 
			
		||||
            ITask task = queue.remove();
 | 
			
		||||
 | 
			
		||||
            if( thread == null || !thread.isAlive() )
 | 
			
		||||
            {
 | 
			
		||||
                runner = new TaskRunner();
 | 
			
		||||
                (thread = s_RunnerFactory.newThread( runner )).start();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            long start = System.nanoTime();
 | 
			
		||||
 | 
			
		||||
            // Execute the task
 | 
			
		||||
            runner.submit( task );
 | 
			
		||||
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                // If we timed out rather than exiting:
 | 
			
		||||
                boolean done = runner.await( 7000 );
 | 
			
		||||
                if( !done )
 | 
			
		||||
                {
 | 
			
		||||
                    // Attempt to soft then hard abort
 | 
			
		||||
                    Computer computer = task.getOwner();
 | 
			
		||||
                    if( computer != null )
 | 
			
		||||
                    {
 | 
			
		||||
                        computer.abort( false );
 | 
			
		||||
 | 
			
		||||
                        done = runner.await( 1500 );
 | 
			
		||||
                        if( !done )
 | 
			
		||||
                        {
 | 
			
		||||
                            computer.abort( true );
 | 
			
		||||
                            done = runner.await( 1500 );
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    // Interrupt the thread
 | 
			
		||||
                    if( !done )
 | 
			
		||||
                    {
 | 
			
		||||
                        StringBuilder builder = new StringBuilder( "Terminating " );
 | 
			
		||||
                        if( computer != null )
 | 
			
		||||
                        {
 | 
			
		||||
                            builder.append( "computer " ).append( computer.getID() );
 | 
			
		||||
                        }
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            builder.append( "unknown computer" );
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                        builder.append( ". Thread is currently running" );
 | 
			
		||||
                        for( StackTraceElement element : thread.getStackTrace() )
 | 
			
		||||
                        {
 | 
			
		||||
                            builder.append( "\n  at " ).append( element );
 | 
			
		||||
                        }
 | 
			
		||||
                        ComputerCraft.log.error( builder.toString() );
 | 
			
		||||
 | 
			
		||||
                        thread.interrupt();
 | 
			
		||||
                        thread = null;
 | 
			
		||||
                        runner = null;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            finally
 | 
			
		||||
            {
 | 
			
		||||
                long stop = System.nanoTime();
 | 
			
		||||
                Computer computer = task.getOwner();
 | 
			
		||||
                if( computer != null ) Tracking.addTaskTiming( computer, stop - start );
 | 
			
		||||
 | 
			
		||||
                // Re-add it back onto the queue or remove it
 | 
			
		||||
                synchronized( s_taskLock )
 | 
			
		||||
                {
 | 
			
		||||
                    if( queue.isEmpty() )
 | 
			
		||||
                    {
 | 
			
		||||
                        s_computerTasksActiveSet.remove( queue );
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        s_computerTasksActive.add( queue );
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Responsible for the actual running of tasks. It waitin for the {@link TaskRunner#input} semaphore to be
 | 
			
		||||
     * triggered, consumes a task and then triggers {@link TaskRunner#finished}.
 | 
			
		||||
     * Pulls tasks from the {@link #computerQueue} queue and runs them.
 | 
			
		||||
     *
 | 
			
		||||
     * This is responsible for running the {@link ComputerExecutor#work()}, {@link ComputerExecutor#beforeWork()} and
 | 
			
		||||
     * {@link ComputerExecutor#afterWork()} functions. Everything else is either handled by the executor, timeout
 | 
			
		||||
     * state or monitor.
 | 
			
		||||
     */
 | 
			
		||||
    private static final class TaskRunner implements Runnable
 | 
			
		||||
    {
 | 
			
		||||
        private final Semaphore input = new Semaphore();
 | 
			
		||||
        private final Semaphore finished = new Semaphore();
 | 
			
		||||
        private ITask task;
 | 
			
		||||
        Thread owner;
 | 
			
		||||
        volatile boolean running = true;
 | 
			
		||||
 | 
			
		||||
        final AtomicReference<ComputerExecutor> currentExecutor = new AtomicReference<>();
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void run()
 | 
			
		||||
        {
 | 
			
		||||
            try
 | 
			
		||||
            owner = Thread.currentThread();
 | 
			
		||||
 | 
			
		||||
            tasks:
 | 
			
		||||
            while( running && ComputerThread.running )
 | 
			
		||||
            {
 | 
			
		||||
                while( true )
 | 
			
		||||
                // Wait for an active queue to execute
 | 
			
		||||
                ComputerExecutor executor;
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    input.await();
 | 
			
		||||
                    computerLock.lockInterruptibly();
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        task.execute();
 | 
			
		||||
                        while( computerQueue.isEmpty() ) hasWork.await();
 | 
			
		||||
                        executor = computerQueue.pollFirst();
 | 
			
		||||
                        assert executor != null : "hasWork should ensure we never receive null work";
 | 
			
		||||
                    }
 | 
			
		||||
                    catch( RuntimeException e )
 | 
			
		||||
                    finally
 | 
			
		||||
                    {
 | 
			
		||||
                        ComputerCraft.log.error( "Error running task.", e );
 | 
			
		||||
                        computerLock.unlock();
 | 
			
		||||
                    }
 | 
			
		||||
                    task = null;
 | 
			
		||||
                    finished.signal();
 | 
			
		||||
                }
 | 
			
		||||
                catch( InterruptedException ignored )
 | 
			
		||||
                {
 | 
			
		||||
                    // If we've been interrupted, our running flag has probably been reset, so we'll
 | 
			
		||||
                    // just jump into the next iteration.
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // If we're trying to executing some task on this computer while someone else is doing work, something
 | 
			
		||||
                // is seriously wrong.
 | 
			
		||||
                while( !executor.executingThread.compareAndSet( null, owner ) )
 | 
			
		||||
                {
 | 
			
		||||
                    Thread existing = executor.executingThread.get();
 | 
			
		||||
                    if( existing != null )
 | 
			
		||||
                    {
 | 
			
		||||
                        ComputerCraft.log.error(
 | 
			
		||||
                            "Trying to run computer #{} on thread {}, but already running on {}. This is a SERIOUS bug, please report with your debug.log.",
 | 
			
		||||
                            executor.getComputer().getID(), owner.getName(), existing.getName()
 | 
			
		||||
                        );
 | 
			
		||||
                        continue tasks;
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                // Reset the timers
 | 
			
		||||
                executor.beforeWork();
 | 
			
		||||
 | 
			
		||||
                // And then set the current executor. It's important to do it afterwards, as otherwise we introduce
 | 
			
		||||
                // race conditions with the monitor.
 | 
			
		||||
                currentExecutor.set( executor );
 | 
			
		||||
 | 
			
		||||
                // Execute the task
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    executor.work();
 | 
			
		||||
                }
 | 
			
		||||
                catch( Exception e )
 | 
			
		||||
                {
 | 
			
		||||
                    ComputerCraft.log.error( "Error running task on computer #" + executor.getComputer().getID(), e );
 | 
			
		||||
                }
 | 
			
		||||
                finally
 | 
			
		||||
                {
 | 
			
		||||
                    ComputerExecutor thisExecutor = currentExecutor.getAndSet( null );
 | 
			
		||||
                    if( thisExecutor != null ) afterWork( this, executor );
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch( InterruptedException e )
 | 
			
		||||
            {
 | 
			
		||||
                ComputerCraft.log.error( "Error running task.", e );
 | 
			
		||||
                Thread.currentThread().interrupt();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        void submit( ITask task )
 | 
			
		||||
        {
 | 
			
		||||
            this.task = task;
 | 
			
		||||
            input.signal();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        boolean await( long timeout ) throws InterruptedException
 | 
			
		||||
        {
 | 
			
		||||
            return finished.await( timeout );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A simple method to allow awaiting/providing a signal.
 | 
			
		||||
     *
 | 
			
		||||
     * Java does provide similar classes, but I only needed something simple.
 | 
			
		||||
     */
 | 
			
		||||
    private static final class Semaphore
 | 
			
		||||
    private static void timeoutTask( ComputerExecutor executor, Thread thread, long time )
 | 
			
		||||
    {
 | 
			
		||||
        private volatile boolean state = false;
 | 
			
		||||
        if( !ComputerCraft.logPeripheralErrors ) return;
 | 
			
		||||
 | 
			
		||||
        synchronized void signal()
 | 
			
		||||
        StringBuilder builder = new StringBuilder()
 | 
			
		||||
            .append( "Terminating computer #" ).append( executor.getComputer().getID() )
 | 
			
		||||
            .append( " due to timeout (running for " ).append( time * 1e-9 )
 | 
			
		||||
            .append( " seconds). This is NOT a bug, but may mean a computer is misbehaving. " )
 | 
			
		||||
            .append( thread.getName() )
 | 
			
		||||
            .append( " is currently " )
 | 
			
		||||
            .append( thread.getState() );
 | 
			
		||||
        Object blocking = LockSupport.getBlocker( thread );
 | 
			
		||||
        if( blocking != null ) builder.append( "\n  on " ).append( blocking );
 | 
			
		||||
 | 
			
		||||
        for( StackTraceElement element : thread.getStackTrace() )
 | 
			
		||||
        {
 | 
			
		||||
            state = true;
 | 
			
		||||
            notify();
 | 
			
		||||
            builder.append( "\n  at " ).append( element );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        synchronized void await() throws InterruptedException
 | 
			
		||||
        {
 | 
			
		||||
            while( !state ) wait();
 | 
			
		||||
            state = false;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        synchronized boolean await( long timeout ) throws InterruptedException
 | 
			
		||||
        {
 | 
			
		||||
            if( !state )
 | 
			
		||||
            {
 | 
			
		||||
                wait( timeout );
 | 
			
		||||
                if( !state ) return false;
 | 
			
		||||
            }
 | 
			
		||||
            state = false;
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
        ComputerCraft.log.warn( builder.toString() );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,307 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.peripheral.IPeripheral;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IWorkMonitor;
 | 
			
		||||
import dan200.computercraft.core.apis.IAPIEnvironment;
 | 
			
		||||
import dan200.computercraft.core.filesystem.FileSystem;
 | 
			
		||||
import dan200.computercraft.core.terminal.Terminal;
 | 
			
		||||
import dan200.computercraft.core.tracking.Tracking;
 | 
			
		||||
import dan200.computercraft.core.tracking.TrackingField;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents the "environment" that a {@link Computer} exists in.
 | 
			
		||||
 *
 | 
			
		||||
 * This handles storing and updating of peripherals and redstone.
 | 
			
		||||
 *
 | 
			
		||||
 * <h1>Redstone</h1>
 | 
			
		||||
 * We holds three kinds of arrays for redstone, in normal and bundled versions:
 | 
			
		||||
 * <ul>
 | 
			
		||||
 * <li>{@link #internalOutput} is the redstone output which the computer has currently set. This is read on both
 | 
			
		||||
 * threads, and written on the computer thread.</li>
 | 
			
		||||
 * <li>{@link #externalOutput} is the redstone output currently propagated to the world. This is only read and written
 | 
			
		||||
 * on the main thread.</li>
 | 
			
		||||
 * <li>{@link #input} is the redstone input from external sources. This is read on both threads, and written on the main
 | 
			
		||||
 * thread.</li>
 | 
			
		||||
 * </ul>
 | 
			
		||||
 *
 | 
			
		||||
 * <h1>Peripheral</h1>
 | 
			
		||||
 * We also keep track of peripherals. These are read on both threads, and only written on the main thread.
 | 
			
		||||
 */
 | 
			
		||||
public final class Environment implements IAPIEnvironment
 | 
			
		||||
{
 | 
			
		||||
    private final Computer computer;
 | 
			
		||||
 | 
			
		||||
    private boolean internalOutputChanged = false;
 | 
			
		||||
    private final int[] internalOutput = new int[SIDE_COUNT];
 | 
			
		||||
    private final int[] internalBundledOutput = new int[SIDE_COUNT];
 | 
			
		||||
 | 
			
		||||
    private final int[] externalOutput = new int[SIDE_COUNT];
 | 
			
		||||
    private final int[] externalBundledOutput = new int[SIDE_COUNT];
 | 
			
		||||
 | 
			
		||||
    private boolean inputChanged = false;
 | 
			
		||||
    private final int[] input = new int[SIDE_COUNT];
 | 
			
		||||
    private final int[] bundledInput = new int[SIDE_COUNT];
 | 
			
		||||
 | 
			
		||||
    private final IPeripheral[] peripherals = new IPeripheral[SIDE_COUNT];
 | 
			
		||||
    private IPeripheralChangeListener peripheralListener = null;
 | 
			
		||||
 | 
			
		||||
    Environment( Computer computer )
 | 
			
		||||
    {
 | 
			
		||||
        this.computer = computer;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getComputerID()
 | 
			
		||||
    {
 | 
			
		||||
        return computer.assignID();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public IComputerEnvironment getComputerEnvironment()
 | 
			
		||||
    {
 | 
			
		||||
        return computer.getComputerEnvironment();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public IWorkMonitor getMainThreadMonitor()
 | 
			
		||||
    {
 | 
			
		||||
        return computer.getMainThreadMonitor();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public Terminal getTerminal()
 | 
			
		||||
    {
 | 
			
		||||
        return computer.getTerminal();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public FileSystem getFileSystem()
 | 
			
		||||
    {
 | 
			
		||||
        return computer.getFileSystem();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void shutdown()
 | 
			
		||||
    {
 | 
			
		||||
        computer.shutdown();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void reboot()
 | 
			
		||||
    {
 | 
			
		||||
        computer.reboot();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void queueEvent( String event, Object[] args )
 | 
			
		||||
    {
 | 
			
		||||
        computer.queueEvent( event, args );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getInput( int side )
 | 
			
		||||
    {
 | 
			
		||||
        return input[side];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getBundledInput( int side )
 | 
			
		||||
    {
 | 
			
		||||
        return bundledInput[side];
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setOutput( int side, int output )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( internalOutput )
 | 
			
		||||
        {
 | 
			
		||||
            if( internalOutput[side] != output )
 | 
			
		||||
            {
 | 
			
		||||
                internalOutput[side] = output;
 | 
			
		||||
                internalOutputChanged = true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getOutput( int side )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( internalOutput )
 | 
			
		||||
        {
 | 
			
		||||
            return computer.isOn() ? internalOutput[side] : 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setBundledOutput( int side, int output )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( internalOutput )
 | 
			
		||||
        {
 | 
			
		||||
            if( internalBundledOutput[side] != output )
 | 
			
		||||
            {
 | 
			
		||||
                internalBundledOutput[side] = output;
 | 
			
		||||
                internalOutputChanged = true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int getBundledOutput( int side )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( internalOutput )
 | 
			
		||||
        {
 | 
			
		||||
            return computer.isOn() ? internalBundledOutput[side] : 0;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getExternalRedstoneOutput( int side )
 | 
			
		||||
    {
 | 
			
		||||
        return computer.isOn() ? externalOutput[side] : 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public int getExternalBundledRedstoneOutput( int side )
 | 
			
		||||
    {
 | 
			
		||||
        return computer.isOn() ? externalBundledOutput[side] : 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setRedstoneInput( int side, int level )
 | 
			
		||||
    {
 | 
			
		||||
        if( input[side] != level )
 | 
			
		||||
        {
 | 
			
		||||
            input[side] = level;
 | 
			
		||||
            inputChanged = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setBundledRedstoneInput( int side, int combination )
 | 
			
		||||
    {
 | 
			
		||||
        if( bundledInput[side] != combination )
 | 
			
		||||
        {
 | 
			
		||||
            bundledInput[side] = combination;
 | 
			
		||||
            inputChanged = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Called on the main thread to update the internal state of the computer.
 | 
			
		||||
     *
 | 
			
		||||
     * This just queues a {@code redstone} event if the input has changed.
 | 
			
		||||
     */
 | 
			
		||||
    void update()
 | 
			
		||||
    {
 | 
			
		||||
        if( inputChanged )
 | 
			
		||||
        {
 | 
			
		||||
            inputChanged = false;
 | 
			
		||||
            queueEvent( "redstone", null );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Called on the main thread to propagate the internal outputs to the external ones.
 | 
			
		||||
     *
 | 
			
		||||
     * @return If the outputs have changed.
 | 
			
		||||
     */
 | 
			
		||||
    boolean updateOutput()
 | 
			
		||||
    {
 | 
			
		||||
        // Mark output as changed if the internal redstone has changed
 | 
			
		||||
        synchronized( internalOutput )
 | 
			
		||||
        {
 | 
			
		||||
            if( !internalOutputChanged ) return false;
 | 
			
		||||
 | 
			
		||||
            boolean changed = false;
 | 
			
		||||
 | 
			
		||||
            for( int i = 0; i < SIDE_COUNT; i++ )
 | 
			
		||||
            {
 | 
			
		||||
                if( externalOutput[i] != internalOutput[i] )
 | 
			
		||||
                {
 | 
			
		||||
                    externalOutput[i] = internalOutput[i];
 | 
			
		||||
                    changed = true;
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                if( externalBundledOutput[i] != internalBundledOutput[i] )
 | 
			
		||||
                {
 | 
			
		||||
                    externalBundledOutput[i] = internalBundledOutput[i];
 | 
			
		||||
                    changed = true;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            internalOutputChanged = false;
 | 
			
		||||
 | 
			
		||||
            return changed;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void resetOutput()
 | 
			
		||||
    {
 | 
			
		||||
        // Reset redstone output
 | 
			
		||||
        synchronized( internalOutput )
 | 
			
		||||
        {
 | 
			
		||||
            Arrays.fill( internalOutput, 0 );
 | 
			
		||||
            Arrays.fill( internalBundledOutput, 0 );
 | 
			
		||||
            internalOutputChanged = true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public IPeripheral getPeripheral( int side )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( peripherals )
 | 
			
		||||
        {
 | 
			
		||||
            return peripherals[side];
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void setPeripheral( int side, IPeripheral peripheral )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( peripherals )
 | 
			
		||||
        {
 | 
			
		||||
            IPeripheral existing = peripherals[side];
 | 
			
		||||
            if( (existing == null && peripheral != null) ||
 | 
			
		||||
                (existing != null && peripheral == null) ||
 | 
			
		||||
                (existing != null && !existing.equals( peripheral )) )
 | 
			
		||||
            {
 | 
			
		||||
                peripherals[side] = peripheral;
 | 
			
		||||
                if( peripheralListener != null ) peripheralListener.onPeripheralChanged( side, peripheral );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setPeripheralChangeListener( IPeripheralChangeListener listener )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( peripherals )
 | 
			
		||||
        {
 | 
			
		||||
            peripheralListener = listener;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public String getLabel()
 | 
			
		||||
    {
 | 
			
		||||
        return computer.getLabel();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void setLabel( String label )
 | 
			
		||||
    {
 | 
			
		||||
        computer.setLabel( label );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void addTrackingChange( @Nonnull TrackingField field, long change )
 | 
			
		||||
    {
 | 
			
		||||
        Tracking.addValue( computer, field, change );
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,15 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
 | 
			
		||||
public interface IComputerOwned
 | 
			
		||||
{
 | 
			
		||||
    @Nullable
 | 
			
		||||
    Computer getComputer();
 | 
			
		||||
}
 | 
			
		||||
@@ -1,14 +0,0 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
public interface ITask
 | 
			
		||||
{
 | 
			
		||||
    Computer getOwner();
 | 
			
		||||
 | 
			
		||||
    void execute();
 | 
			
		||||
}
 | 
			
		||||
@@ -6,66 +6,190 @@
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.core.tracking.Tracking;
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaTask;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayDeque;
 | 
			
		||||
import java.util.Queue;
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.TreeSet;
 | 
			
		||||
import java.util.concurrent.atomic.AtomicLong;
 | 
			
		||||
 | 
			
		||||
public class MainThread
 | 
			
		||||
/**
 | 
			
		||||
 * Runs tasks on the main (server) thread, ticks {@link MainThreadExecutor}s, and limits how much time is used this
 | 
			
		||||
 * tick.
 | 
			
		||||
 *
 | 
			
		||||
 * Similar to {@link MainThreadExecutor}, the {@link MainThread} can be in one of three states: cool, hot and cooling.
 | 
			
		||||
 * However, the implementation here is a little different:
 | 
			
		||||
 *
 | 
			
		||||
 * {@link MainThread} starts cool, and runs as many tasks as it can in the current {@link #budget}ns. Any external tasks
 | 
			
		||||
 * (those run by tile entities, etc...) will also consume the budget
 | 
			
		||||
 *
 | 
			
		||||
 * Next tick, we put {@link ComputerCraft#maxMainGlobalTime} into our budget (and clamp it to that value to). If we're
 | 
			
		||||
 * still over budget, then we should not execute <em>any</em> work (either as part of {@link MainThread} or externally).
 | 
			
		||||
 */
 | 
			
		||||
public final class MainThread
 | 
			
		||||
{
 | 
			
		||||
    private static final int MAX_TASKS_PER_TICK = 1000;
 | 
			
		||||
    private static final int MAX_TASKS_TOTAL = 50000;
 | 
			
		||||
    /**
 | 
			
		||||
     * An internal counter for {@link ILuaTask} ids.
 | 
			
		||||
     *
 | 
			
		||||
     * @see dan200.computercraft.api.lua.ILuaContext#issueMainThreadTask(ILuaTask)
 | 
			
		||||
     * @see #getUniqueTaskID()
 | 
			
		||||
     */
 | 
			
		||||
    private static final AtomicLong lastTaskId = new AtomicLong();
 | 
			
		||||
 | 
			
		||||
    private static final Queue<ITask> m_outstandingTasks = new ArrayDeque<>();
 | 
			
		||||
    private static final Object m_nextUnusedTaskIDLock = new Object();
 | 
			
		||||
    private static long m_nextUnusedTaskID = 0;
 | 
			
		||||
    /**
 | 
			
		||||
     * The queue of {@link MainThreadExecutor}s with tasks to perform.
 | 
			
		||||
     */
 | 
			
		||||
    private static final TreeSet<MainThreadExecutor> executors = new TreeSet<>( ( a, b ) -> {
 | 
			
		||||
        if( a == b ) return 0; // Should never happen, but let's be consistent here
 | 
			
		||||
 | 
			
		||||
        long at = a.virtualTime, bt = b.virtualTime;
 | 
			
		||||
        if( at == bt ) return Integer.compare( a.hashCode(), b.hashCode() );
 | 
			
		||||
        return at < bt ? -1 : 1;
 | 
			
		||||
    } );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The set of executors which went over budget in a previous tick, and are waiting for their time to run down.
 | 
			
		||||
     *
 | 
			
		||||
     * @see MainThreadExecutor#tickCooling()
 | 
			
		||||
     * @see #cooling(MainThreadExecutor)
 | 
			
		||||
     */
 | 
			
		||||
    private static final HashSet<MainThreadExecutor> cooling = new HashSet<>();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The current tick number. This is used by {@link MainThreadExecutor} to determine when to reset its own time
 | 
			
		||||
     * counter.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #currentTick()
 | 
			
		||||
     */
 | 
			
		||||
    private static int currentTick;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The remaining budgeted time for this tick. This may be negative, in the case that we've gone over budget.
 | 
			
		||||
     */
 | 
			
		||||
    private static long budget;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether we should be executing any work this tick.
 | 
			
		||||
     *
 | 
			
		||||
     * This is true iff {@code MAX_TICK_TIME - currentTime} was true <em>at the beginning of the tick</em>.
 | 
			
		||||
     */
 | 
			
		||||
    private static boolean canExecute = true;
 | 
			
		||||
 | 
			
		||||
    private static long minimumTime = 0;
 | 
			
		||||
 | 
			
		||||
    private MainThread() {}
 | 
			
		||||
 | 
			
		||||
    public static long getUniqueTaskID()
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( m_nextUnusedTaskIDLock )
 | 
			
		||||
        return lastTaskId.incrementAndGet();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static void queue( @Nonnull MainThreadExecutor executor, boolean sleeper )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( executors )
 | 
			
		||||
        {
 | 
			
		||||
            return ++m_nextUnusedTaskID;
 | 
			
		||||
            if( executor.onQueue ) throw new IllegalStateException( "Cannot queue already queued executor" );
 | 
			
		||||
            executor.onQueue = true;
 | 
			
		||||
            executor.updateTime();
 | 
			
		||||
 | 
			
		||||
            // We're not currently on the queue, so update its current execution time to
 | 
			
		||||
            // ensure its at least as high as the minimum.
 | 
			
		||||
            long newRuntime = minimumTime;
 | 
			
		||||
 | 
			
		||||
            // Slow down new computers a little bit.
 | 
			
		||||
            if( executor.virtualTime == 0 ) newRuntime += ComputerCraft.maxMainComputerTime;
 | 
			
		||||
 | 
			
		||||
            executor.virtualTime = Math.max( newRuntime, executor.virtualTime );
 | 
			
		||||
 | 
			
		||||
            executors.add( executor );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static boolean queueTask( ITask task )
 | 
			
		||||
    static void cooling( @Nonnull MainThreadExecutor executor )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( m_outstandingTasks )
 | 
			
		||||
        {
 | 
			
		||||
            if( m_outstandingTasks.size() < MAX_TASKS_TOTAL )
 | 
			
		||||
            {
 | 
			
		||||
                m_outstandingTasks.offer( task );
 | 
			
		||||
                return true;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return false;
 | 
			
		||||
        cooling.add( executor );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static void consumeTime( long time )
 | 
			
		||||
    {
 | 
			
		||||
        budget -= time;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static boolean canExecute()
 | 
			
		||||
    {
 | 
			
		||||
        return canExecute;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    static int currentTick()
 | 
			
		||||
    {
 | 
			
		||||
        return currentTick;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void executePendingTasks()
 | 
			
		||||
    {
 | 
			
		||||
        int tasksThisTick = 0;
 | 
			
		||||
        while( tasksThisTick < MAX_TASKS_PER_TICK )
 | 
			
		||||
        // Move onto the next tick and cool down the global executor. We're allowed to execute if we have _any_ time
 | 
			
		||||
        // allocated for this tick. This means we'll stick much closer to doing MAX_TICK_TIME work every tick.
 | 
			
		||||
        //
 | 
			
		||||
        // Of course, we'll go over the MAX_TICK_TIME most of the time, but eventually that overrun will accumulate
 | 
			
		||||
        // and we'll skip a whole tick - bringing the average back down again.
 | 
			
		||||
        currentTick++;
 | 
			
		||||
        budget += Math.min( budget + ComputerCraft.maxMainGlobalTime, ComputerCraft.maxMainGlobalTime );
 | 
			
		||||
        canExecute = budget > 0;
 | 
			
		||||
 | 
			
		||||
        // Cool down any warm computers.
 | 
			
		||||
        cooling.removeIf( MainThreadExecutor::tickCooling );
 | 
			
		||||
 | 
			
		||||
        if( !canExecute ) return;
 | 
			
		||||
 | 
			
		||||
        // Run until we meet the deadline.
 | 
			
		||||
        long start = System.nanoTime();
 | 
			
		||||
        long deadline = start + budget;
 | 
			
		||||
        while( true )
 | 
			
		||||
        {
 | 
			
		||||
            ITask task = null;
 | 
			
		||||
            synchronized( m_outstandingTasks )
 | 
			
		||||
            MainThreadExecutor executor;
 | 
			
		||||
            synchronized( executors )
 | 
			
		||||
            {
 | 
			
		||||
                task = m_outstandingTasks.poll();
 | 
			
		||||
                executor = executors.pollFirst();
 | 
			
		||||
            }
 | 
			
		||||
            if( task != null )
 | 
			
		||||
            {
 | 
			
		||||
                long start = System.nanoTime();
 | 
			
		||||
                task.execute();
 | 
			
		||||
            if( executor == null ) break;
 | 
			
		||||
 | 
			
		||||
                long stop = System.nanoTime();
 | 
			
		||||
                Computer computer = task.getOwner();
 | 
			
		||||
                if( computer != null ) Tracking.addServerTiming( computer, stop - start );
 | 
			
		||||
            long taskStart = System.nanoTime();
 | 
			
		||||
            executor.execute();
 | 
			
		||||
 | 
			
		||||
                ++tasksThisTick;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            long taskStop = System.nanoTime();
 | 
			
		||||
            synchronized( executors )
 | 
			
		||||
            {
 | 
			
		||||
                break;
 | 
			
		||||
                if( executor.afterExecute( taskStop - taskStart ) ) executors.add( executor );
 | 
			
		||||
 | 
			
		||||
                // Compute the new minimum time (including the next task on the queue too). Note that this may also include
 | 
			
		||||
                // time spent in external tasks.
 | 
			
		||||
                long newMinimum = executor.virtualTime;
 | 
			
		||||
                if( !executors.isEmpty() )
 | 
			
		||||
                {
 | 
			
		||||
                    MainThreadExecutor next = executors.first();
 | 
			
		||||
                    if( next.virtualTime < newMinimum ) newMinimum = next.virtualTime;
 | 
			
		||||
                }
 | 
			
		||||
                minimumTime = Math.max( minimumTime, newMinimum );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if( taskStop >= deadline ) break;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        consumeTime( System.nanoTime() - start );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static void reset()
 | 
			
		||||
    {
 | 
			
		||||
        currentTick = 0;
 | 
			
		||||
        budget = 0;
 | 
			
		||||
        canExecute = true;
 | 
			
		||||
        minimumTime = 0;
 | 
			
		||||
        lastTaskId.set( 0 );
 | 
			
		||||
        cooling.clear();
 | 
			
		||||
        synchronized( executors )
 | 
			
		||||
        {
 | 
			
		||||
            executors.clear();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,250 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.api.peripheral.IWorkMonitor;
 | 
			
		||||
import dan200.computercraft.core.tracking.Tracking;
 | 
			
		||||
import dan200.computercraft.shared.turtle.core.TurtleBrain;
 | 
			
		||||
import net.minecraft.tileentity.TileEntity;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.util.ArrayDeque;
 | 
			
		||||
import java.util.Queue;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Keeps track of tasks that a {@link Computer} should run on the main thread and how long that has been spent executing
 | 
			
		||||
 * them.
 | 
			
		||||
 *
 | 
			
		||||
 * This provides rate-limiting mechanism for tasks enqueued with {@link Computer#queueMainThread(Runnable)}, but also
 | 
			
		||||
 * those run elsewhere (such as during the turtle's tick - see {@link TurtleBrain#update()}). In order to handle this,
 | 
			
		||||
 * the executor goes through three stages:
 | 
			
		||||
 *
 | 
			
		||||
 * When {@link State#COOL}, the computer is allocated {@link ComputerCraft#maxMainComputerTime}ns to execute any work
 | 
			
		||||
 * this tick. At the beginning of the tick, we execute as many {@link MainThread} tasks as possible, until our
 | 
			
		||||
 * time-frame or the global time frame has expired.
 | 
			
		||||
 *
 | 
			
		||||
 * Then, when other objects (such as {@link TileEntity}) are ticked, we update how much time we've used using
 | 
			
		||||
 * {@link IWorkMonitor#trackWork(long, TimeUnit)}.
 | 
			
		||||
 *
 | 
			
		||||
 * Now, if anywhere during this period, we use more than our allocated time slice, the executor is marked as
 | 
			
		||||
 * {@link State#HOT}. This means it will no longer be able to execute {@link MainThread} tasks (though will still
 | 
			
		||||
 * execute tile entity tasks, in order to prevent the main thread from exhausting work every tick).
 | 
			
		||||
 *
 | 
			
		||||
 * At the beginning of the next tick, we increment the budget e by {@link ComputerCraft#maxMainComputerTime} and any
 | 
			
		||||
 * {@link State#HOT} executors are marked as {@link State#COOLING}. They will remain cooling until their budget is
 | 
			
		||||
 * fully replenished (is equal to {@link ComputerCraft#maxMainComputerTime}). Note, this is different to
 | 
			
		||||
 * {@link MainThread}, which allows running when it has any budget left. When cooling, <em>no</em> tasks are executed -
 | 
			
		||||
 * be they on the tile entity or main thread.
 | 
			
		||||
 *
 | 
			
		||||
 * This mechanism means that, on average, computers will use at most {@link ComputerCraft#maxMainComputerTime}ns per
 | 
			
		||||
 * second, but one task source will not prevent others from executing.
 | 
			
		||||
 *
 | 
			
		||||
 * @see MainThread
 | 
			
		||||
 * @see IWorkMonitor
 | 
			
		||||
 * @see Computer#getMainThreadMonitor()
 | 
			
		||||
 * @see Computer#queueMainThread(Runnable)
 | 
			
		||||
 */
 | 
			
		||||
final class MainThreadExecutor implements IWorkMonitor
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * The maximum number of {@link MainThread} tasks allowed on the queue.
 | 
			
		||||
     */
 | 
			
		||||
    private static final int MAX_TASKS = 5000;
 | 
			
		||||
 | 
			
		||||
    private final Computer computer;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A lock used for any changes to {@link #tasks}, or {@link #onQueue}. This will be
 | 
			
		||||
     * used on the main thread, so locks should be kept as brief as possible.
 | 
			
		||||
     */
 | 
			
		||||
    private final Object queueLock = new Object();
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The queue of tasks which should be executed.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #queueLock
 | 
			
		||||
     */
 | 
			
		||||
    private final Queue<Runnable> tasks = new ArrayDeque<>( 4 );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Determines if this executor is currently present on the queue.
 | 
			
		||||
     *
 | 
			
		||||
     * This should be true iff {@link #tasks} is non-empty.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #queueLock
 | 
			
		||||
     * @see #enqueue(Runnable)
 | 
			
		||||
     * @see #afterExecute(long)
 | 
			
		||||
     */
 | 
			
		||||
    volatile boolean onQueue;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The remaining budgeted time for this tick. This may be negative, in the case that we've gone over budget.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #tickCooling()
 | 
			
		||||
     * @see #consumeTime(long)
 | 
			
		||||
     */
 | 
			
		||||
    private long budget = 0;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The last tick that {@link #budget} was updated.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #tickCooling()
 | 
			
		||||
     * @see #consumeTime(long)
 | 
			
		||||
     */
 | 
			
		||||
    private int currentTick = -1;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The current state of this executor.
 | 
			
		||||
     *
 | 
			
		||||
     * @see #canWork()
 | 
			
		||||
     */
 | 
			
		||||
    private State state = State.COOL;
 | 
			
		||||
 | 
			
		||||
    private long pendingTime;
 | 
			
		||||
 | 
			
		||||
    long virtualTime;
 | 
			
		||||
 | 
			
		||||
    MainThreadExecutor( Computer computer )
 | 
			
		||||
    {
 | 
			
		||||
        this.computer = computer;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Push a task onto this executor's queue, pushing it onto the {@link MainThread} if needed.
 | 
			
		||||
     *
 | 
			
		||||
     * @param runnable The task to run on the main thread.
 | 
			
		||||
     * @return Whether this task was enqueued (namely, was there space).
 | 
			
		||||
     */
 | 
			
		||||
    boolean enqueue( Runnable runnable )
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( tasks.size() >= MAX_TASKS || !tasks.offer( runnable ) ) return false;
 | 
			
		||||
            if( !onQueue && state == State.COOL ) MainThread.queue( this, true );
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void execute()
 | 
			
		||||
    {
 | 
			
		||||
        if( state != State.COOL ) return;
 | 
			
		||||
 | 
			
		||||
        Runnable task;
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            task = tasks.poll();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if( task != null ) task.run();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Update the time taken to run an {@link #enqueue(Runnable)} task.
 | 
			
		||||
     *
 | 
			
		||||
     * @param time The time some task took to run.
 | 
			
		||||
     * @return Whether this should be added back to the queue.
 | 
			
		||||
     */
 | 
			
		||||
    boolean afterExecute( long time )
 | 
			
		||||
    {
 | 
			
		||||
        consumeTime( time );
 | 
			
		||||
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            virtualTime += time;
 | 
			
		||||
            updateTime();
 | 
			
		||||
            if( state != State.COOL || tasks.isEmpty() ) return onQueue = false;
 | 
			
		||||
            return true;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether we should execute "external" tasks (ones not part of {@link #tasks}).
 | 
			
		||||
     *
 | 
			
		||||
     * @return Whether we can execute external tasks.
 | 
			
		||||
     */
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean canWork()
 | 
			
		||||
    {
 | 
			
		||||
        return state != State.COOLING && MainThread.canExecute();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean shouldWork()
 | 
			
		||||
    {
 | 
			
		||||
        return state == State.COOL && MainThread.canExecute();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void trackWork( long time, @Nonnull TimeUnit unit )
 | 
			
		||||
    {
 | 
			
		||||
        long nanoTime = unit.toNanos( time );
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            pendingTime += nanoTime;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        consumeTime( nanoTime );
 | 
			
		||||
        MainThread.consumeTime( nanoTime );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void consumeTime( long time )
 | 
			
		||||
    {
 | 
			
		||||
        Tracking.addServerTiming( computer, time );
 | 
			
		||||
 | 
			
		||||
        // Reset the budget if moving onto a new tick. We know this is safe, as this will only have happened if
 | 
			
		||||
        // #tickCooling() isn't called, and so we didn't overrun the previous tick.
 | 
			
		||||
        if( currentTick != MainThread.currentTick() )
 | 
			
		||||
        {
 | 
			
		||||
            currentTick = MainThread.currentTick();
 | 
			
		||||
            budget = ComputerCraft.maxMainComputerTime;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        budget -= time;
 | 
			
		||||
 | 
			
		||||
        // If we've gone over our limit, mark us as having to cool down.
 | 
			
		||||
        if( budget < 0 && state == State.COOL )
 | 
			
		||||
        {
 | 
			
		||||
            state = State.HOT;
 | 
			
		||||
            MainThread.cooling( this );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Move this executor forward one tick, replenishing the budget by {@link ComputerCraft#maxMainComputerTime}.
 | 
			
		||||
     *
 | 
			
		||||
     * @return Whether this executor has cooled down, and so is safe to run again.
 | 
			
		||||
     */
 | 
			
		||||
    boolean tickCooling()
 | 
			
		||||
    {
 | 
			
		||||
        state = State.COOLING;
 | 
			
		||||
        currentTick = MainThread.currentTick();
 | 
			
		||||
        budget += Math.min( budget + ComputerCraft.maxMainComputerTime, ComputerCraft.maxMainComputerTime );
 | 
			
		||||
        if( budget < ComputerCraft.maxMainComputerTime ) return false;
 | 
			
		||||
 | 
			
		||||
        state = State.COOL;
 | 
			
		||||
        synchronized( queueLock )
 | 
			
		||||
        {
 | 
			
		||||
            if( !tasks.isEmpty() && !onQueue ) MainThread.queue( this, false );
 | 
			
		||||
        }
 | 
			
		||||
        return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    void updateTime()
 | 
			
		||||
    {
 | 
			
		||||
        virtualTime += pendingTime;
 | 
			
		||||
        pendingTime = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private enum State
 | 
			
		||||
    {
 | 
			
		||||
        COOL,
 | 
			
		||||
        HOT,
 | 
			
		||||
        COOLING,
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,168 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.computer;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.core.lua.ILuaMachine;
 | 
			
		||||
import dan200.computercraft.core.lua.MachineResult;
 | 
			
		||||
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Used to measure how long a computer has executed for, and thus the relevant timeout states.
 | 
			
		||||
 *
 | 
			
		||||
 * Timeouts are mostly used for execution of Lua code: we should ideally never have a state where constructing the APIs
 | 
			
		||||
 * or machines themselves takes more than a fraction of a second.
 | 
			
		||||
 *
 | 
			
		||||
 * When a computer runs, it is allowed to run for 7 seconds ({@link #TIMEOUT}). After that point, the "soft abort" flag
 | 
			
		||||
 * is set ({@link #isSoftAborted()}). Here, the Lua machine will attempt to abort the program in some safe manner
 | 
			
		||||
 * (namely, throwing a "Too long without yielding" error).
 | 
			
		||||
 *
 | 
			
		||||
 * Now, if a computer still does not stop after that period, they're behaving really badly. 1.5 seconds after a soft
 | 
			
		||||
 * abort ({@link #ABORT_TIMEOUT}), we trigger a hard abort (note, this is done from the computer thread manager). This
 | 
			
		||||
 * will destroy the entire Lua runtime and shut the computer down.
 | 
			
		||||
 *
 | 
			
		||||
 * The Lua runtime is also allowed to pause execution if there are other computers contesting for work. All computers
 | 
			
		||||
 * are allowed to run for {@link ComputerThread#scaledPeriod()} nanoseconds (see {@link #currentDeadline}). After that
 | 
			
		||||
 * period, if any computers are waiting to be executed then we'll set the paused flag to true ({@link #isPaused()}.
 | 
			
		||||
 *
 | 
			
		||||
 * @see ComputerThread
 | 
			
		||||
 * @see ILuaMachine
 | 
			
		||||
 * @see MachineResult#isPause()
 | 
			
		||||
 */
 | 
			
		||||
public final class TimeoutState
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * The total time a task is allowed to run before aborting in nanoseconds
 | 
			
		||||
     */
 | 
			
		||||
    static final long TIMEOUT = TimeUnit.MILLISECONDS.toNanos( 7000 );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The time the task is allowed to run after each abort in nanoseconds
 | 
			
		||||
     */
 | 
			
		||||
    static final long ABORT_TIMEOUT = TimeUnit.MILLISECONDS.toNanos( 1500 );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * The error message to display when we trigger an abort.
 | 
			
		||||
     */
 | 
			
		||||
    public static final String ABORT_MESSAGE = "Too long without yielding";
 | 
			
		||||
 | 
			
		||||
    private boolean paused;
 | 
			
		||||
    private boolean softAbort;
 | 
			
		||||
    private volatile boolean hardAbort;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * When the cumulative time would have started had the whole event been processed in one go.
 | 
			
		||||
     */
 | 
			
		||||
    private long cumulativeStart;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * How much cumulative time has elapsed. This is effectively {@code cumulativeStart - currentStart}.
 | 
			
		||||
     */
 | 
			
		||||
    private long cumulativeElapsed;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * When this execution round started.
 | 
			
		||||
     */
 | 
			
		||||
    private long currentStart;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * When this execution round should look potentially be paused.
 | 
			
		||||
     */
 | 
			
		||||
    private long currentDeadline;
 | 
			
		||||
 | 
			
		||||
    long nanoCumulative()
 | 
			
		||||
    {
 | 
			
		||||
        return System.nanoTime() - cumulativeStart;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    long nanoCurrent()
 | 
			
		||||
    {
 | 
			
		||||
        return System.nanoTime() - currentStart;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Recompute the {@link #isSoftAborted()} and {@link #isPaused()} flags.
 | 
			
		||||
     */
 | 
			
		||||
    public void refresh()
 | 
			
		||||
    {
 | 
			
		||||
        // Important: The weird arithmetic here is important, as nanoTime may return negative values, and so we
 | 
			
		||||
        // need to handle overflow.
 | 
			
		||||
        long now = System.nanoTime();
 | 
			
		||||
        if( !paused ) paused = currentDeadline - now <= 0 && ComputerThread.hasPendingWork(); // now >= currentDeadline
 | 
			
		||||
        if( !softAbort ) softAbort = now - cumulativeStart - TIMEOUT >= 0; // now - cumulativeStart >= TIMEOUT
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Whether we should pause execution of this machine.
 | 
			
		||||
     *
 | 
			
		||||
     * This is determined by whether we've consumed our time slice, and if there are other computers waiting to perform
 | 
			
		||||
     * work.
 | 
			
		||||
     *
 | 
			
		||||
     * @return Whether we should pause execution.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isPaused()
 | 
			
		||||
    {
 | 
			
		||||
        return paused;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * If the machine should be passively aborted.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isSoftAborted()
 | 
			
		||||
    {
 | 
			
		||||
        return softAbort;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * If the machine should be forcibly aborted.
 | 
			
		||||
     */
 | 
			
		||||
    public boolean isHardAborted()
 | 
			
		||||
    {
 | 
			
		||||
        return hardAbort;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * If the machine should be forcibly aborted.
 | 
			
		||||
     */
 | 
			
		||||
    void hardAbort()
 | 
			
		||||
    {
 | 
			
		||||
        softAbort = hardAbort = true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Start the current and cumulative timers again.
 | 
			
		||||
     */
 | 
			
		||||
    void startTimer()
 | 
			
		||||
    {
 | 
			
		||||
        long now = System.nanoTime();
 | 
			
		||||
        currentStart = now;
 | 
			
		||||
        currentDeadline = now + ComputerThread.scaledPeriod();
 | 
			
		||||
        // Compute the "nominal start time".
 | 
			
		||||
        cumulativeStart = now - cumulativeElapsed;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Pauses the cumulative time, to be resumed by {@link #startTimer()}
 | 
			
		||||
     *
 | 
			
		||||
     * @see #nanoCumulative()
 | 
			
		||||
     */
 | 
			
		||||
    void pauseTimer()
 | 
			
		||||
    {
 | 
			
		||||
        // We set the cumulative time to difference between current time and "nominal start time".
 | 
			
		||||
        cumulativeElapsed = System.nanoTime() - cumulativeStart;
 | 
			
		||||
        paused = false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Resets the cumulative time and resets the abort flags.
 | 
			
		||||
     */
 | 
			
		||||
    void stopTimer()
 | 
			
		||||
    {
 | 
			
		||||
        cumulativeElapsed = 0;
 | 
			
		||||
        paused = softAbort = hardAbort = false;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,50 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.filesystem;
 | 
			
		||||
 | 
			
		||||
import java.io.Closeable;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.nio.channels.Channel;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Wraps some closeable object such as a buffered writer, and the underlying stream.
 | 
			
		||||
 *
 | 
			
		||||
 * When flushing a buffer before closing, some implementations will not close the buffer if an exception is thrown
 | 
			
		||||
 * this causes us to release the channel, but not actually close it. This wrapper will attempt to close the wrapper (and
 | 
			
		||||
 * so hopefully flush the channel), and then close the underlying channel.
 | 
			
		||||
 *
 | 
			
		||||
 * @param <T> The type of the closeable object to write.
 | 
			
		||||
 */
 | 
			
		||||
class ChannelWrapper<T extends Closeable> implements Closeable
 | 
			
		||||
{
 | 
			
		||||
    private final T wrapper;
 | 
			
		||||
    private final Channel channel;
 | 
			
		||||
 | 
			
		||||
    ChannelWrapper( T wrapper, Channel channel )
 | 
			
		||||
    {
 | 
			
		||||
        this.wrapper = wrapper;
 | 
			
		||||
        this.channel = channel;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void close() throws IOException
 | 
			
		||||
    {
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            wrapper.close();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            channel.close();
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public T get()
 | 
			
		||||
    {
 | 
			
		||||
        return wrapper;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -55,14 +55,8 @@ public class FileMount implements IWritableMount
 | 
			
		||||
                m_ignoredBytesLeft = 0;
 | 
			
		||||
 | 
			
		||||
                long bytesLeft = m_capacity - m_usedSpace;
 | 
			
		||||
                if( newBytes > bytesLeft )
 | 
			
		||||
                {
 | 
			
		||||
                    throw new IOException( "Out of space" );
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    m_usedSpace += newBytes;
 | 
			
		||||
                }
 | 
			
		||||
                if( newBytes > bytesLeft ) throw new IOException( "Out of space" );
 | 
			
		||||
                m_usedSpace += newBytes;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@@ -86,14 +80,17 @@ public class FileMount implements IWritableMount
 | 
			
		||||
        SeekableCountingChannel( SeekableByteChannel inner, long bytesToIgnore )
 | 
			
		||||
        {
 | 
			
		||||
            super( inner, bytesToIgnore );
 | 
			
		||||
            this.m_inner = inner;
 | 
			
		||||
            m_inner = inner;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public SeekableByteChannel position( long newPosition ) throws IOException
 | 
			
		||||
        {
 | 
			
		||||
            if( !isOpen() ) throw new ClosedChannelException();
 | 
			
		||||
            if( newPosition < 0 ) throw new IllegalArgumentException();
 | 
			
		||||
            if( newPosition < 0 )
 | 
			
		||||
            {
 | 
			
		||||
                throw new IllegalArgumentException( "Cannot seek before the beginning of the stream" );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            long delta = newPosition - m_inner.position();
 | 
			
		||||
            if( delta < 0 )
 | 
			
		||||
@@ -115,7 +112,7 @@ public class FileMount implements IWritableMount
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public int read( ByteBuffer dst ) throws IOException
 | 
			
		||||
        public int read( ByteBuffer dst ) throws ClosedChannelException
 | 
			
		||||
        {
 | 
			
		||||
            if( !m_inner.isOpen() ) throw new ClosedChannelException();
 | 
			
		||||
            throw new NonReadableChannelException();
 | 
			
		||||
@@ -150,29 +147,19 @@ public class FileMount implements IWritableMount
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean exists( @Nonnull String path )
 | 
			
		||||
    {
 | 
			
		||||
        if( !created() )
 | 
			
		||||
        {
 | 
			
		||||
            return path.length() == 0;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            return file.exists();
 | 
			
		||||
        }
 | 
			
		||||
        if( !created() ) return path.isEmpty();
 | 
			
		||||
 | 
			
		||||
        File file = getRealPath( path );
 | 
			
		||||
        return file.exists();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isDirectory( @Nonnull String path )
 | 
			
		||||
    {
 | 
			
		||||
        if( !created() )
 | 
			
		||||
        {
 | 
			
		||||
            return path.length() == 0;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            return file.exists() && file.isDirectory();
 | 
			
		||||
        }
 | 
			
		||||
        if( !created() ) return path.isEmpty();
 | 
			
		||||
 | 
			
		||||
        File file = getRealPath( path );
 | 
			
		||||
        return file.exists() && file.isDirectory();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -180,29 +167,17 @@ public class FileMount implements IWritableMount
 | 
			
		||||
    {
 | 
			
		||||
        if( !created() )
 | 
			
		||||
        {
 | 
			
		||||
            if( path.length() != 0 )
 | 
			
		||||
            {
 | 
			
		||||
                throw new IOException( "/" + path + ": Not a directory" );
 | 
			
		||||
            }
 | 
			
		||||
            if( !path.isEmpty() ) throw new IOException( "/" + path + ": Not a directory" );
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
 | 
			
		||||
        File file = getRealPath( path );
 | 
			
		||||
        if( !file.exists() || !file.isDirectory() ) throw new IOException( "/" + path + ": Not a directory" );
 | 
			
		||||
 | 
			
		||||
        String[] paths = file.list();
 | 
			
		||||
        for( String subPath : paths )
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            if( file.exists() && file.isDirectory() )
 | 
			
		||||
            {
 | 
			
		||||
                String[] paths = file.list();
 | 
			
		||||
                for( String subPath : paths )
 | 
			
		||||
                {
 | 
			
		||||
                    if( new File( file, subPath ).exists() )
 | 
			
		||||
                    {
 | 
			
		||||
                        contents.add( subPath );
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                throw new IOException( "/" + path + ": Not a directory" );
 | 
			
		||||
            }
 | 
			
		||||
            if( new File( file, subPath ).exists() ) contents.add( subPath );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -211,26 +186,14 @@ public class FileMount implements IWritableMount
 | 
			
		||||
    {
 | 
			
		||||
        if( !created() )
 | 
			
		||||
        {
 | 
			
		||||
            if( path.length() == 0 )
 | 
			
		||||
            {
 | 
			
		||||
                return 0;
 | 
			
		||||
            }
 | 
			
		||||
            if( path.isEmpty() ) return 0;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            if( file.exists() )
 | 
			
		||||
            {
 | 
			
		||||
                if( file.isDirectory() )
 | 
			
		||||
                {
 | 
			
		||||
                    return 0;
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    return file.length();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            if( file.exists() ) return file.isDirectory() ? 0 : file.length();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new IOException( "/" + path + ": No such file" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -242,11 +205,9 @@ public class FileMount implements IWritableMount
 | 
			
		||||
        if( created() )
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            if( file.exists() && !file.isDirectory() )
 | 
			
		||||
            {
 | 
			
		||||
                return new FileInputStream( file );
 | 
			
		||||
            }
 | 
			
		||||
            if( file.exists() && !file.isDirectory() ) return new FileInputStream( file );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new IOException( "/" + path + ": No such file" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -257,11 +218,9 @@ public class FileMount implements IWritableMount
 | 
			
		||||
        if( created() )
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            if( file.exists() && !file.isDirectory() )
 | 
			
		||||
            {
 | 
			
		||||
                return FileChannel.open( file.toPath(), READ_OPTIONS );
 | 
			
		||||
            }
 | 
			
		||||
            if( file.exists() && !file.isDirectory() ) return FileChannel.open( file.toPath(), READ_OPTIONS );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        throw new IOException( "/" + path + ": No such file" );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -274,53 +233,42 @@ public class FileMount implements IWritableMount
 | 
			
		||||
        File file = getRealPath( path );
 | 
			
		||||
        if( file.exists() )
 | 
			
		||||
        {
 | 
			
		||||
            if( !file.isDirectory() )
 | 
			
		||||
            {
 | 
			
		||||
                throw new IOException( "/" + path + ": File exists" );
 | 
			
		||||
            }
 | 
			
		||||
            if( !file.isDirectory() ) throw new IOException( "/" + path + ": File exists" );
 | 
			
		||||
            return;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        int dirsToCreate = 1;
 | 
			
		||||
        File parent = file.getParentFile();
 | 
			
		||||
        while( !parent.exists() )
 | 
			
		||||
        {
 | 
			
		||||
            ++dirsToCreate;
 | 
			
		||||
            parent = parent.getParentFile();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if( getRemainingSpace() < dirsToCreate * MINIMUM_FILE_SIZE )
 | 
			
		||||
        {
 | 
			
		||||
            throw new IOException( "/" + path + ": Out of space" );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if( file.mkdirs() )
 | 
			
		||||
        {
 | 
			
		||||
            m_usedSpace += dirsToCreate * MINIMUM_FILE_SIZE;
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        {
 | 
			
		||||
            int dirsToCreate = 1;
 | 
			
		||||
            File parent = file.getParentFile();
 | 
			
		||||
            while( !parent.exists() )
 | 
			
		||||
            {
 | 
			
		||||
                ++dirsToCreate;
 | 
			
		||||
                parent = parent.getParentFile();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if( getRemainingSpace() < dirsToCreate * MINIMUM_FILE_SIZE )
 | 
			
		||||
            {
 | 
			
		||||
                throw new IOException( "/" + path + ": Out of space" );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            boolean success = file.mkdirs();
 | 
			
		||||
            if( success )
 | 
			
		||||
            {
 | 
			
		||||
                m_usedSpace += dirsToCreate * MINIMUM_FILE_SIZE;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                throw new IOException( "/" + path + ": Access denied" );
 | 
			
		||||
            }
 | 
			
		||||
            throw new IOException( "/" + path + ": Access denied" );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void delete( @Nonnull String path ) throws IOException
 | 
			
		||||
    {
 | 
			
		||||
        if( path.length() == 0 )
 | 
			
		||||
        {
 | 
			
		||||
            throw new IOException( "/" + path + ": Access denied" );
 | 
			
		||||
        }
 | 
			
		||||
        if( path.isEmpty() ) throw new IOException( "/" + path + ": Access denied" );
 | 
			
		||||
 | 
			
		||||
        if( created() )
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            if( file.exists() )
 | 
			
		||||
            {
 | 
			
		||||
                deleteRecursively( file );
 | 
			
		||||
            }
 | 
			
		||||
            if( file.exists() ) deleteRecursively( file );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -371,60 +319,39 @@ public class FileMount implements IWritableMount
 | 
			
		||||
    {
 | 
			
		||||
        create();
 | 
			
		||||
        File file = getRealPath( path );
 | 
			
		||||
        if( file.exists() && file.isDirectory() )
 | 
			
		||||
        if( file.exists() && file.isDirectory() ) throw new IOException( "/" + path + ": Cannot write to directory" );
 | 
			
		||||
 | 
			
		||||
        if( file.exists() )
 | 
			
		||||
        {
 | 
			
		||||
            throw new IOException( "/" + path + ": Cannot write to directory" );
 | 
			
		||||
            m_usedSpace -= Math.max( file.length(), MINIMUM_FILE_SIZE );
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        else if( getRemainingSpace() < MINIMUM_FILE_SIZE )
 | 
			
		||||
        {
 | 
			
		||||
            if( !file.exists() )
 | 
			
		||||
            {
 | 
			
		||||
                if( getRemainingSpace() < MINIMUM_FILE_SIZE )
 | 
			
		||||
                {
 | 
			
		||||
                    throw new IOException( "/" + path + ": Out of space" );
 | 
			
		||||
                }
 | 
			
		||||
                else
 | 
			
		||||
                {
 | 
			
		||||
                    m_usedSpace += MINIMUM_FILE_SIZE;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                m_usedSpace -= Math.max( file.length(), MINIMUM_FILE_SIZE );
 | 
			
		||||
                m_usedSpace += MINIMUM_FILE_SIZE;
 | 
			
		||||
            }
 | 
			
		||||
            return new SeekableCountingChannel( Files.newByteChannel( file.toPath(), WRITE_OPTIONS ), MINIMUM_FILE_SIZE );
 | 
			
		||||
            throw new IOException( "/" + path + ": Out of space" );
 | 
			
		||||
        }
 | 
			
		||||
        m_usedSpace += MINIMUM_FILE_SIZE;
 | 
			
		||||
 | 
			
		||||
        return new SeekableCountingChannel( Files.newByteChannel( file.toPath(), WRITE_OPTIONS ), MINIMUM_FILE_SIZE );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    @Override
 | 
			
		||||
    public WritableByteChannel openChannelForAppend( @Nonnull String path ) throws IOException
 | 
			
		||||
    {
 | 
			
		||||
        if( created() )
 | 
			
		||||
        {
 | 
			
		||||
            File file = getRealPath( path );
 | 
			
		||||
            if( !file.exists() )
 | 
			
		||||
            {
 | 
			
		||||
                throw new IOException( "/" + path + ": No such file" );
 | 
			
		||||
            }
 | 
			
		||||
            else if( file.isDirectory() )
 | 
			
		||||
            {
 | 
			
		||||
                throw new IOException( "/" + path + ": Cannot write to directory" );
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                // Allowing seeking when appending is not recommended, so we use a separate channel.
 | 
			
		||||
                return new WritableCountingChannel(
 | 
			
		||||
                    Files.newByteChannel( file.toPath(), APPEND_OPTIONS ),
 | 
			
		||||
                    Math.max( MINIMUM_FILE_SIZE - file.length(), 0 )
 | 
			
		||||
                );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        else
 | 
			
		||||
        if( !created() )
 | 
			
		||||
        {
 | 
			
		||||
            throw new IOException( "/" + path + ": No such file" );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        File file = getRealPath( path );
 | 
			
		||||
        if( !file.exists() ) throw new IOException( "/" + path + ": No such file" );
 | 
			
		||||
        if( file.isDirectory() ) throw new IOException( "/" + path + ": Cannot write to directory" );
 | 
			
		||||
 | 
			
		||||
        // Allowing seeking when appending is not recommended, so we use a separate channel.
 | 
			
		||||
        return new WritableCountingChannel(
 | 
			
		||||
            Files.newByteChannel( file.toPath(), APPEND_OPTIONS ),
 | 
			
		||||
            Math.max( MINIMUM_FILE_SIZE - file.length(), 0 )
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -433,7 +360,7 @@ public class FileMount implements IWritableMount
 | 
			
		||||
        return Math.max( m_capacity - m_usedSpace, 0 );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public File getRealPath( String path )
 | 
			
		||||
    private File getRealPath( String path )
 | 
			
		||||
    {
 | 
			
		||||
        return new File( m_rootPath, path );
 | 
			
		||||
    }
 | 
			
		||||
@@ -455,12 +382,10 @@ public class FileMount implements IWritableMount
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private long measureUsedSpace( File file )
 | 
			
		||||
    private static long measureUsedSpace( File file )
 | 
			
		||||
    {
 | 
			
		||||
        if( !file.exists() )
 | 
			
		||||
        {
 | 
			
		||||
            return 0;
 | 
			
		||||
        }
 | 
			
		||||
        if( !file.exists() ) return 0;
 | 
			
		||||
 | 
			
		||||
        if( file.isDirectory() )
 | 
			
		||||
        {
 | 
			
		||||
            long size = MINIMUM_FILE_SIZE;
 | 
			
		||||
 
 | 
			
		||||
@@ -19,6 +19,7 @@ import java.io.IOException;
 | 
			
		||||
import java.lang.ref.Reference;
 | 
			
		||||
import java.lang.ref.ReferenceQueue;
 | 
			
		||||
import java.lang.ref.WeakReference;
 | 
			
		||||
import java.nio.channels.Channel;
 | 
			
		||||
import java.nio.channels.ReadableByteChannel;
 | 
			
		||||
import java.nio.channels.WritableByteChannel;
 | 
			
		||||
import java.nio.file.AccessDeniedException;
 | 
			
		||||
@@ -28,7 +29,7 @@ import java.util.regex.Pattern;
 | 
			
		||||
 | 
			
		||||
public class FileSystem
 | 
			
		||||
{
 | 
			
		||||
    private class MountWrapper
 | 
			
		||||
    private static class MountWrapper
 | 
			
		||||
    {
 | 
			
		||||
        private String m_label;
 | 
			
		||||
        private String m_location;
 | 
			
		||||
@@ -36,7 +37,7 @@ public class FileSystem
 | 
			
		||||
        private IMount m_mount;
 | 
			
		||||
        private IWritableMount m_writableMount;
 | 
			
		||||
 | 
			
		||||
        public MountWrapper( String label, String location, IMount mount )
 | 
			
		||||
        MountWrapper( String label, String location, IMount mount )
 | 
			
		||||
        {
 | 
			
		||||
            m_label = label;
 | 
			
		||||
            m_location = location;
 | 
			
		||||
@@ -79,7 +80,7 @@ public class FileSystem
 | 
			
		||||
 | 
			
		||||
        public boolean isReadOnly( String path )
 | 
			
		||||
        {
 | 
			
		||||
            return (m_writableMount == null);
 | 
			
		||||
            return m_writableMount == null;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // IMount forwarders:
 | 
			
		||||
@@ -317,7 +318,7 @@ public class FileSystem
 | 
			
		||||
    private final FileSystemWrapperMount m_wrapper = new FileSystemWrapperMount( this );
 | 
			
		||||
    private final Map<String, MountWrapper> m_mounts = new HashMap<>();
 | 
			
		||||
 | 
			
		||||
    private final HashMap<WeakReference<FileSystemWrapper<?>>, Closeable> m_openFiles = new HashMap<>();
 | 
			
		||||
    private final HashMap<WeakReference<FileSystemWrapper<?>>, ChannelWrapper<?>> m_openFiles = new HashMap<>();
 | 
			
		||||
    private final ReferenceQueue<FileSystemWrapper<?>> m_openFileQueue = new ReferenceQueue<>();
 | 
			
		||||
 | 
			
		||||
    public FileSystem( String rootLabel, IMount rootMount ) throws FileSystemException
 | 
			
		||||
@@ -330,7 +331,7 @@ public class FileSystem
 | 
			
		||||
        mountWritable( rootLabel, "", rootMount );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public void unload()
 | 
			
		||||
    public void close()
 | 
			
		||||
    {
 | 
			
		||||
        // Close all dangling open files
 | 
			
		||||
        synchronized( m_openFiles )
 | 
			
		||||
@@ -473,7 +474,7 @@ public class FileSystem
 | 
			
		||||
        String[] list = list( dir );
 | 
			
		||||
        for( String entry : list )
 | 
			
		||||
        {
 | 
			
		||||
            String entryPath = dir.isEmpty() ? entry : (dir + "/" + entry);
 | 
			
		||||
            String entryPath = dir.isEmpty() ? entry : dir + "/" + entry;
 | 
			
		||||
            if( wildPattern.matcher( entryPath ).matches() )
 | 
			
		||||
            {
 | 
			
		||||
                matches.add( entryPath );
 | 
			
		||||
@@ -661,7 +662,7 @@ public class FileSystem
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private synchronized <T extends Closeable> FileSystemWrapper<T> openFile( @Nonnull T file ) throws FileSystemException
 | 
			
		||||
    private synchronized <T extends Closeable> FileSystemWrapper<T> openFile( @Nonnull Channel channel, @Nonnull T file ) throws FileSystemException
 | 
			
		||||
    {
 | 
			
		||||
        synchronized( m_openFiles )
 | 
			
		||||
        {
 | 
			
		||||
@@ -669,12 +670,14 @@ public class FileSystem
 | 
			
		||||
                m_openFiles.size() >= ComputerCraft.maximumFilesOpen )
 | 
			
		||||
            {
 | 
			
		||||
                IoUtil.closeQuietly( file );
 | 
			
		||||
                IoUtil.closeQuietly( channel );
 | 
			
		||||
                throw new FileSystemException( "Too many files already open" );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            FileSystemWrapper<T> wrapper = new FileSystemWrapper<>( this, file, m_openFileQueue );
 | 
			
		||||
            m_openFiles.put( wrapper.self, file );
 | 
			
		||||
            return wrapper;
 | 
			
		||||
            ChannelWrapper<T> channelWrapper = new ChannelWrapper<>( file, channel );
 | 
			
		||||
            FileSystemWrapper<T> fsWrapper = new FileSystemWrapper<>( this, channelWrapper, m_openFileQueue );
 | 
			
		||||
            m_openFiles.put( fsWrapper.self, channelWrapper );
 | 
			
		||||
            return fsWrapper;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -695,7 +698,7 @@ public class FileSystem
 | 
			
		||||
        ReadableByteChannel channel = mount.openForRead( path );
 | 
			
		||||
        if( channel != null )
 | 
			
		||||
        {
 | 
			
		||||
            return openFile( open.apply( channel ) );
 | 
			
		||||
            return openFile( channel, open.apply( channel ) );
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
@@ -709,7 +712,7 @@ public class FileSystem
 | 
			
		||||
        WritableByteChannel channel = append ? mount.openForAppend( path ) : mount.openForWrite( path );
 | 
			
		||||
        if( channel != null )
 | 
			
		||||
        {
 | 
			
		||||
            return openFile( open.apply( channel ) );
 | 
			
		||||
            return openFile( channel, open.apply( channel ) );
 | 
			
		||||
        }
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
@@ -765,7 +768,7 @@ public class FileSystem
 | 
			
		||||
        path = path.replace( '\\', '/' );
 | 
			
		||||
 | 
			
		||||
        // Clean the path or illegal characters.
 | 
			
		||||
        final char[] specialChars = {
 | 
			
		||||
        final char[] specialChars = new char[] {
 | 
			
		||||
            '"', ':', '<', '>', '?', '|' // Sorted by ascii value (important)
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
@@ -785,13 +788,14 @@ public class FileSystem
 | 
			
		||||
        Stack<String> outputParts = new Stack<>();
 | 
			
		||||
        for( String part : parts )
 | 
			
		||||
        {
 | 
			
		||||
            if( part.length() == 0 || part.equals( "." ) || threeDotsPattern.matcher( part ).matches() )
 | 
			
		||||
            if( part.isEmpty() || part.equals( "." ) || threeDotsPattern.matcher( part ).matches() )
 | 
			
		||||
            {
 | 
			
		||||
                // . is redundant
 | 
			
		||||
                // ... and more are treated as .
 | 
			
		||||
                continue;
 | 
			
		||||
            }
 | 
			
		||||
            else if( part.equals( ".." ) )
 | 
			
		||||
 | 
			
		||||
            if( part.equals( ".." ) )
 | 
			
		||||
            {
 | 
			
		||||
                // .. can cancel out the last folder entered
 | 
			
		||||
                if( !outputParts.empty() )
 | 
			
		||||
@@ -824,7 +828,7 @@ public class FileSystem
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Recombine the output parts into a new string
 | 
			
		||||
        StringBuilder result = new StringBuilder( "" );
 | 
			
		||||
        StringBuilder result = new StringBuilder();
 | 
			
		||||
        Iterator<String> it = outputParts.iterator();
 | 
			
		||||
        while( it.hasNext() )
 | 
			
		||||
        {
 | 
			
		||||
@@ -871,7 +875,7 @@ public class FileSystem
 | 
			
		||||
        path = sanitizePath( path );
 | 
			
		||||
        location = sanitizePath( location );
 | 
			
		||||
 | 
			
		||||
        assert (contains( location, path ));
 | 
			
		||||
        assert contains( location, path );
 | 
			
		||||
        String local = path.substring( location.length() );
 | 
			
		||||
        if( local.startsWith( "/" ) )
 | 
			
		||||
        {
 | 
			
		||||
 
 | 
			
		||||
@@ -15,22 +15,27 @@ import java.lang.ref.WeakReference;
 | 
			
		||||
/**
 | 
			
		||||
 * An alternative closeable implementation that will free up resources in the filesystem.
 | 
			
		||||
 *
 | 
			
		||||
 * The {@link FileSystem} maps weak references of this to its underlying object. If the wrapper has been disposed of
 | 
			
		||||
 * (say, the Lua object referencing it has gone), then the wrapped object will be closed by the filesystem.
 | 
			
		||||
 *
 | 
			
		||||
 * Closing this will stop the filesystem tracking it, reducing the current descriptor count.
 | 
			
		||||
 *
 | 
			
		||||
 * In an ideal world, we'd just wrap the closeable. However, as we do some {@code instanceof} checks
 | 
			
		||||
 * on the stream, it's not really possible as it'd require numerous instances.
 | 
			
		||||
 *
 | 
			
		||||
 * @param <T> The stream to wrap.
 | 
			
		||||
 * @param <T> The type of writer or channel to wrap.
 | 
			
		||||
 */
 | 
			
		||||
public class FileSystemWrapper<T extends Closeable> implements Closeable
 | 
			
		||||
{
 | 
			
		||||
    private final FileSystem fileSystem;
 | 
			
		||||
    private final T closeable;
 | 
			
		||||
    private final ChannelWrapper<T> closeable;
 | 
			
		||||
    final WeakReference<FileSystemWrapper<?>> self;
 | 
			
		||||
 | 
			
		||||
    FileSystemWrapper( FileSystem fileSystem, T closeable, ReferenceQueue<FileSystemWrapper<?>> queue )
 | 
			
		||||
    FileSystemWrapper( FileSystem fileSystem, ChannelWrapper<T> closeable, ReferenceQueue<FileSystemWrapper<?>> queue )
 | 
			
		||||
    {
 | 
			
		||||
        this.fileSystem = fileSystem;
 | 
			
		||||
        this.closeable = closeable;
 | 
			
		||||
        this.self = new WeakReference<>( this, queue );
 | 
			
		||||
        self = new WeakReference<>( this, queue );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@@ -43,6 +48,6 @@ public class FileSystemWrapper<T extends Closeable> implements Closeable
 | 
			
		||||
    @Nonnull
 | 
			
		||||
    public T get()
 | 
			
		||||
    {
 | 
			
		||||
        return closeable;
 | 
			
		||||
        return closeable.get();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,9 +23,9 @@ public class FileSystemWrapperMount implements IFileSystem
 | 
			
		||||
{
 | 
			
		||||
    private final FileSystem m_filesystem;
 | 
			
		||||
 | 
			
		||||
    public FileSystemWrapperMount( FileSystem m_filesystem )
 | 
			
		||||
    public FileSystemWrapperMount( FileSystem filesystem )
 | 
			
		||||
    {
 | 
			
		||||
        this.m_filesystem = m_filesystem;
 | 
			
		||||
        this.m_filesystem = filesystem;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
 
 | 
			
		||||
@@ -76,9 +76,9 @@ public class JarMount implements IMount
 | 
			
		||||
        {
 | 
			
		||||
            zip = new ZipFile( jarFile );
 | 
			
		||||
        }
 | 
			
		||||
        catch( Exception e )
 | 
			
		||||
        catch( IOException e )
 | 
			
		||||
        {
 | 
			
		||||
            throw new IOException( "Error loading zip file" );
 | 
			
		||||
            throw new IOException( "Error loading zip file", e );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Ensure the root entry exists.
 | 
			
		||||
@@ -212,7 +212,7 @@ public class JarMount implements IMount
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            catch( Exception e )
 | 
			
		||||
            catch( IOException e )
 | 
			
		||||
            {
 | 
			
		||||
                // Treat errors as non-existence of file
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
@@ -68,7 +68,7 @@ public class SubMount implements IMount
 | 
			
		||||
 | 
			
		||||
    private String getFullPath( String path )
 | 
			
		||||
    {
 | 
			
		||||
        if( path.length() == 0 )
 | 
			
		||||
        if( path.isEmpty() )
 | 
			
		||||
        {
 | 
			
		||||
            return m_subPath;
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -9,8 +9,8 @@ package dan200.computercraft.core.lua;
 | 
			
		||||
import dan200.computercraft.ComputerCraft;
 | 
			
		||||
import dan200.computercraft.api.lua.*;
 | 
			
		||||
import dan200.computercraft.core.computer.Computer;
 | 
			
		||||
import dan200.computercraft.core.computer.ITask;
 | 
			
		||||
import dan200.computercraft.core.computer.MainThread;
 | 
			
		||||
import dan200.computercraft.core.computer.TimeoutState;
 | 
			
		||||
import dan200.computercraft.core.tracking.Tracking;
 | 
			
		||||
import dan200.computercraft.core.tracking.TrackingField;
 | 
			
		||||
import dan200.computercraft.shared.util.ThreadUtils;
 | 
			
		||||
@@ -20,16 +20,13 @@ import org.squiddev.cobalt.compiler.LoadState;
 | 
			
		||||
import org.squiddev.cobalt.debug.DebugFrame;
 | 
			
		||||
import org.squiddev.cobalt.debug.DebugHandler;
 | 
			
		||||
import org.squiddev.cobalt.debug.DebugState;
 | 
			
		||||
import org.squiddev.cobalt.function.LibFunction;
 | 
			
		||||
import org.squiddev.cobalt.function.LuaFunction;
 | 
			
		||||
import org.squiddev.cobalt.function.VarArgFunction;
 | 
			
		||||
import org.squiddev.cobalt.lib.*;
 | 
			
		||||
import org.squiddev.cobalt.lib.platform.VoidResourceManipulator;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.io.OutputStream;
 | 
			
		||||
import java.util.Arrays;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.IdentityHashMap;
 | 
			
		||||
@@ -38,88 +35,44 @@ import java.util.concurrent.SynchronousQueue;
 | 
			
		||||
import java.util.concurrent.ThreadPoolExecutor;
 | 
			
		||||
import java.util.concurrent.TimeUnit;
 | 
			
		||||
 | 
			
		||||
import static org.squiddev.cobalt.Constants.NONE;
 | 
			
		||||
import static org.squiddev.cobalt.ValueFactory.valueOf;
 | 
			
		||||
import static org.squiddev.cobalt.ValueFactory.varargsOf;
 | 
			
		||||
import static org.squiddev.cobalt.debug.DebugFrame.FLAG_HOOKED;
 | 
			
		||||
import static org.squiddev.cobalt.debug.DebugFrame.FLAG_HOOKYIELD;
 | 
			
		||||
 | 
			
		||||
public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
{
 | 
			
		||||
    private static final ThreadPoolExecutor coroutines = new ThreadPoolExecutor(
 | 
			
		||||
    private static final ThreadPoolExecutor COROUTINES = new ThreadPoolExecutor(
 | 
			
		||||
        0, Integer.MAX_VALUE,
 | 
			
		||||
        60L, TimeUnit.SECONDS,
 | 
			
		||||
        5L, TimeUnit.MINUTES,
 | 
			
		||||
        new SynchronousQueue<>(),
 | 
			
		||||
        ThreadUtils.factory( "Coroutine" )
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
    private final Computer m_computer;
 | 
			
		||||
    private final TimeoutState timeout;
 | 
			
		||||
    private final TimeoutDebugHandler debug;
 | 
			
		||||
    private final ILuaContext context = new CobaltLuaContext();
 | 
			
		||||
 | 
			
		||||
    private LuaState m_state;
 | 
			
		||||
    private LuaTable m_globals;
 | 
			
		||||
    private LuaThread m_mainRoutine;
 | 
			
		||||
 | 
			
		||||
    private String m_eventFilter;
 | 
			
		||||
    private String m_softAbortMessage;
 | 
			
		||||
    private String m_hardAbortMessage;
 | 
			
		||||
    private LuaThread m_mainRoutine = null;
 | 
			
		||||
    private String m_eventFilter = null;
 | 
			
		||||
 | 
			
		||||
    public CobaltLuaMachine( Computer computer )
 | 
			
		||||
    public CobaltLuaMachine( Computer computer, TimeoutState timeout )
 | 
			
		||||
    {
 | 
			
		||||
        m_computer = computer;
 | 
			
		||||
        this.timeout = timeout;
 | 
			
		||||
        debug = new TimeoutDebugHandler();
 | 
			
		||||
 | 
			
		||||
        // Create an environment to run in
 | 
			
		||||
        LuaState state = this.m_state = LuaState.builder()
 | 
			
		||||
        LuaState state = m_state = LuaState.builder()
 | 
			
		||||
            .resourceManipulator( new VoidResourceManipulator() )
 | 
			
		||||
            .debug( new DebugHandler()
 | 
			
		||||
            {
 | 
			
		||||
                private int count = 0;
 | 
			
		||||
                private boolean hasSoftAbort;
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public void onInstruction( DebugState ds, DebugFrame di, int pc, Varargs extras, int top ) throws LuaError
 | 
			
		||||
                {
 | 
			
		||||
                    int count = ++this.count;
 | 
			
		||||
                    if( count > 100000 )
 | 
			
		||||
                    {
 | 
			
		||||
                        if( m_hardAbortMessage != null ) LuaThread.yield( m_state, NONE );
 | 
			
		||||
                        this.count = 0;
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        handleSoftAbort();
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    super.onInstruction( ds, di, pc, extras, top );
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                @Override
 | 
			
		||||
                public void poll() throws LuaError
 | 
			
		||||
                {
 | 
			
		||||
                    if( m_hardAbortMessage != null ) LuaThread.yield( m_state, NONE );
 | 
			
		||||
                    handleSoftAbort();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                private void handleSoftAbort() throws LuaError
 | 
			
		||||
                {
 | 
			
		||||
                    // If the soft abort has been cleared then we can reset our flags and continue.
 | 
			
		||||
                    String message = m_softAbortMessage;
 | 
			
		||||
                    if( message == null )
 | 
			
		||||
                    {
 | 
			
		||||
                        hasSoftAbort = false;
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    if( hasSoftAbort && m_hardAbortMessage == null )
 | 
			
		||||
                    {
 | 
			
		||||
                        // If we have fired our soft abort, but we haven't been hard aborted then everything is OK.
 | 
			
		||||
                        return;
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                    hasSoftAbort = true;
 | 
			
		||||
                    throw new LuaError( message );
 | 
			
		||||
                }
 | 
			
		||||
            } )
 | 
			
		||||
            .coroutineFactory( command -> {
 | 
			
		||||
            .debug( debug )
 | 
			
		||||
            .coroutineExecutor( command -> {
 | 
			
		||||
                Tracking.addValue( m_computer, TrackingField.COROUTINES_CREATED, 1 );
 | 
			
		||||
                coroutines.execute( () -> {
 | 
			
		||||
                COROUTINES.execute( () -> {
 | 
			
		||||
                    try
 | 
			
		||||
                    {
 | 
			
		||||
                        command.run();
 | 
			
		||||
@@ -144,11 +97,8 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
        m_globals.load( state, new Bit32Lib() );
 | 
			
		||||
        if( ComputerCraft.debug_enable ) m_globals.load( state, new DebugLib() );
 | 
			
		||||
 | 
			
		||||
        // Register custom load/loadstring provider which automatically adds prefixes.
 | 
			
		||||
        LibFunction.bind( state, m_globals, PrefixLoader.class, new String[] { "load", "loadstring" } );
 | 
			
		||||
 | 
			
		||||
        // Remove globals we don't want to expose
 | 
			
		||||
        // m_globals.rawset( "collectgarbage", Constants.NIL );
 | 
			
		||||
        m_globals.rawset( "collectgarbage", Constants.NIL );
 | 
			
		||||
        m_globals.rawset( "dofile", Constants.NIL );
 | 
			
		||||
        m_globals.rawset( "loadfile", Constants.NIL );
 | 
			
		||||
        m_globals.rawset( "print", Constants.NIL );
 | 
			
		||||
@@ -161,17 +111,10 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
        {
 | 
			
		||||
            m_globals.rawset( "_CC_DISABLE_LUA51_FEATURES", Constants.TRUE );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // Our main function will go here
 | 
			
		||||
        m_mainRoutine = null;
 | 
			
		||||
        m_eventFilter = null;
 | 
			
		||||
 | 
			
		||||
        m_softAbortMessage = null;
 | 
			
		||||
        m_hardAbortMessage = null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void addAPI( ILuaAPI api )
 | 
			
		||||
    public void addAPI( @Nonnull ILuaAPI api )
 | 
			
		||||
    {
 | 
			
		||||
        // Add the methods of an API to the global table
 | 
			
		||||
        LuaTable table = wrapLuaObject( api );
 | 
			
		||||
@@ -183,37 +126,44 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void loadBios( InputStream bios )
 | 
			
		||||
    public MachineResult loadBios( @Nonnull InputStream bios )
 | 
			
		||||
    {
 | 
			
		||||
        // Begin executing a file (ie, the bios)
 | 
			
		||||
        if( m_mainRoutine != null ) return;
 | 
			
		||||
        if( m_mainRoutine != null ) return MachineResult.OK;
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            LuaFunction value = LoadState.load( m_state, bios, "@bios.lua", m_globals );
 | 
			
		||||
            m_mainRoutine = new LuaThread( m_state, value, m_globals );
 | 
			
		||||
            return MachineResult.OK;
 | 
			
		||||
        }
 | 
			
		||||
        catch( CompileException e )
 | 
			
		||||
        {
 | 
			
		||||
            unload();
 | 
			
		||||
            close();
 | 
			
		||||
            return MachineResult.error( e );
 | 
			
		||||
        }
 | 
			
		||||
        catch( IOException e )
 | 
			
		||||
        catch( Exception e )
 | 
			
		||||
        {
 | 
			
		||||
            ComputerCraft.log.warn( "Could not load bios.lua ", e );
 | 
			
		||||
            unload();
 | 
			
		||||
            ComputerCraft.log.warn( "Could not load bios.lua", e );
 | 
			
		||||
            close();
 | 
			
		||||
            return MachineResult.GENERIC_ERROR;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void handleEvent( String eventName, Object[] arguments )
 | 
			
		||||
    public MachineResult handleEvent( String eventName, Object[] arguments )
 | 
			
		||||
    {
 | 
			
		||||
        if( m_mainRoutine == null ) return;
 | 
			
		||||
        if( m_mainRoutine == null ) return MachineResult.OK;
 | 
			
		||||
 | 
			
		||||
        if( m_eventFilter != null && eventName != null && !eventName.equals( m_eventFilter ) && !eventName.equals( "terminate" ) )
 | 
			
		||||
        {
 | 
			
		||||
            return;
 | 
			
		||||
            return MachineResult.OK;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // If the soft abort has been cleared then we can reset our flag.
 | 
			
		||||
        timeout.refresh();
 | 
			
		||||
        if( !timeout.isSoftAborted() ) debug.thrownSoftAbort = false;
 | 
			
		||||
 | 
			
		||||
        try
 | 
			
		||||
        {
 | 
			
		||||
            Varargs resumeArgs = Constants.NONE;
 | 
			
		||||
@@ -222,71 +172,47 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
                resumeArgs = varargsOf( valueOf( eventName ), toValues( arguments ) );
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            Varargs results = m_mainRoutine.resume( resumeArgs );
 | 
			
		||||
            if( m_hardAbortMessage != null )
 | 
			
		||||
            // Resume the current thread, or the main one when first starting off.
 | 
			
		||||
            LuaThread thread = m_state.getCurrentThread();
 | 
			
		||||
            if( thread == null || thread == m_state.getMainThread() ) thread = m_mainRoutine;
 | 
			
		||||
 | 
			
		||||
            Varargs results = LuaThread.run( thread, resumeArgs );
 | 
			
		||||
            if( timeout.isHardAborted() ) throw HardAbortError.INSTANCE;
 | 
			
		||||
            if( results == null ) return MachineResult.PAUSE;
 | 
			
		||||
 | 
			
		||||
            LuaValue filter = results.first();
 | 
			
		||||
            m_eventFilter = filter.isString() ? filter.toString() : null;
 | 
			
		||||
 | 
			
		||||
            if( m_mainRoutine.getStatus().equals( "dead" ) )
 | 
			
		||||
            {
 | 
			
		||||
                throw new LuaError( m_hardAbortMessage );
 | 
			
		||||
            }
 | 
			
		||||
            else if( !results.first().checkBoolean() )
 | 
			
		||||
            {
 | 
			
		||||
                throw new LuaError( results.arg( 2 ).checkString() );
 | 
			
		||||
                close();
 | 
			
		||||
                return MachineResult.GENERIC_ERROR;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                LuaValue filter = results.arg( 2 );
 | 
			
		||||
                m_eventFilter = filter.isString() ? filter.toString() : null;
 | 
			
		||||
                return MachineResult.OK;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if( m_mainRoutine.getStatus().equals( "dead" ) ) unload();
 | 
			
		||||
        }
 | 
			
		||||
        catch( HardAbortError | InterruptedException e )
 | 
			
		||||
        {
 | 
			
		||||
            close();
 | 
			
		||||
            return MachineResult.TIMEOUT;
 | 
			
		||||
        }
 | 
			
		||||
        catch( LuaError e )
 | 
			
		||||
        {
 | 
			
		||||
            unload();
 | 
			
		||||
        }
 | 
			
		||||
        finally
 | 
			
		||||
        {
 | 
			
		||||
            m_softAbortMessage = null;
 | 
			
		||||
            m_hardAbortMessage = null;
 | 
			
		||||
            close();
 | 
			
		||||
            ComputerCraft.log.warn( "Top level coroutine errored", e );
 | 
			
		||||
            return MachineResult.error( e );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void softAbort( String abortMessage )
 | 
			
		||||
    public void close()
 | 
			
		||||
    {
 | 
			
		||||
        m_softAbortMessage = abortMessage;
 | 
			
		||||
    }
 | 
			
		||||
        LuaState state = m_state;
 | 
			
		||||
        if( state == null ) return;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void hardAbort( String abortMessage )
 | 
			
		||||
    {
 | 
			
		||||
        m_softAbortMessage = abortMessage;
 | 
			
		||||
        m_hardAbortMessage = abortMessage;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean saveState( OutputStream output )
 | 
			
		||||
    {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean restoreState( InputStream input )
 | 
			
		||||
    {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean isFinished()
 | 
			
		||||
    {
 | 
			
		||||
        return m_mainRoutine == null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void unload()
 | 
			
		||||
    {
 | 
			
		||||
        if( m_state == null ) return;
 | 
			
		||||
 | 
			
		||||
        m_state.abandon();
 | 
			
		||||
        state.abandon();
 | 
			
		||||
        m_mainRoutine = null;
 | 
			
		||||
        m_state = null;
 | 
			
		||||
        m_globals = null;
 | 
			
		||||
@@ -306,151 +232,13 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
                table.rawset( methodName, new VarArgFunction()
 | 
			
		||||
                {
 | 
			
		||||
                    @Override
 | 
			
		||||
                    public Varargs invoke( final LuaState state, Varargs _args ) throws LuaError
 | 
			
		||||
                    public Varargs invoke( final LuaState state, Varargs args ) throws LuaError
 | 
			
		||||
                    {
 | 
			
		||||
                        Object[] arguments = toObjects( _args, 1 );
 | 
			
		||||
                        Object[] arguments = toObjects( args, 1 );
 | 
			
		||||
                        Object[] results;
 | 
			
		||||
                        try
 | 
			
		||||
                        {
 | 
			
		||||
                            results = apiObject.callMethod( new ILuaContext()
 | 
			
		||||
                            {
 | 
			
		||||
                                @Nonnull
 | 
			
		||||
                                @Override
 | 
			
		||||
                                public Object[] pullEvent( String filter ) throws LuaException, InterruptedException
 | 
			
		||||
                                {
 | 
			
		||||
                                    Object[] results = pullEventRaw( filter );
 | 
			
		||||
                                    if( results.length >= 1 && results[0].equals( "terminate" ) )
 | 
			
		||||
                                    {
 | 
			
		||||
                                        throw new LuaException( "Terminated", 0 );
 | 
			
		||||
                                    }
 | 
			
		||||
                                    return results;
 | 
			
		||||
                                }
 | 
			
		||||
 | 
			
		||||
                                @Nonnull
 | 
			
		||||
                                @Override
 | 
			
		||||
                                public Object[] pullEventRaw( String filter ) throws InterruptedException
 | 
			
		||||
                                {
 | 
			
		||||
                                    return yield( new Object[] { filter } );
 | 
			
		||||
                                }
 | 
			
		||||
 | 
			
		||||
                                @Nonnull
 | 
			
		||||
                                @Override
 | 
			
		||||
                                public Object[] yield( Object[] yieldArgs ) throws InterruptedException
 | 
			
		||||
                                {
 | 
			
		||||
                                    try
 | 
			
		||||
                                    {
 | 
			
		||||
                                        Varargs results = LuaThread.yield( state, toValues( yieldArgs ) );
 | 
			
		||||
                                        return toObjects( results, 1 );
 | 
			
		||||
                                    }
 | 
			
		||||
                                    catch( OrphanedThread e )
 | 
			
		||||
                                    {
 | 
			
		||||
                                        throw new InterruptedException();
 | 
			
		||||
                                    }
 | 
			
		||||
                                    catch( Throwable e )
 | 
			
		||||
                                    {
 | 
			
		||||
                                        throw new RuntimeException( e );
 | 
			
		||||
                                    }
 | 
			
		||||
                                }
 | 
			
		||||
 | 
			
		||||
                                @Override
 | 
			
		||||
                                public long issueMainThreadTask( @Nonnull final ILuaTask task ) throws LuaException
 | 
			
		||||
                                {
 | 
			
		||||
                                    // Issue command
 | 
			
		||||
                                    final long taskID = MainThread.getUniqueTaskID();
 | 
			
		||||
                                    final ITask iTask = new ITask()
 | 
			
		||||
                                    {
 | 
			
		||||
                                        @Override
 | 
			
		||||
                                        public Computer getOwner()
 | 
			
		||||
                                        {
 | 
			
		||||
                                            return m_computer;
 | 
			
		||||
                                        }
 | 
			
		||||
 | 
			
		||||
                                        @Override
 | 
			
		||||
                                        public void execute()
 | 
			
		||||
                                        {
 | 
			
		||||
                                            try
 | 
			
		||||
                                            {
 | 
			
		||||
                                                Object[] results = task.execute();
 | 
			
		||||
                                                if( results != null )
 | 
			
		||||
                                                {
 | 
			
		||||
                                                    Object[] eventArguments = new Object[results.length + 2];
 | 
			
		||||
                                                    eventArguments[0] = taskID;
 | 
			
		||||
                                                    eventArguments[1] = true;
 | 
			
		||||
                                                    System.arraycopy( results, 0, eventArguments, 2, results.length );
 | 
			
		||||
                                                    m_computer.queueEvent( "task_complete", eventArguments );
 | 
			
		||||
                                                }
 | 
			
		||||
                                                else
 | 
			
		||||
                                                {
 | 
			
		||||
                                                    m_computer.queueEvent( "task_complete", new Object[] { taskID, true } );
 | 
			
		||||
                                                }
 | 
			
		||||
                                            }
 | 
			
		||||
                                            catch( LuaException e )
 | 
			
		||||
                                            {
 | 
			
		||||
                                                m_computer.queueEvent( "task_complete", new Object[] {
 | 
			
		||||
                                                    taskID, false, e.getMessage()
 | 
			
		||||
                                                } );
 | 
			
		||||
                                            }
 | 
			
		||||
                                            catch( Throwable t )
 | 
			
		||||
                                            {
 | 
			
		||||
                                                if( ComputerCraft.logPeripheralErrors )
 | 
			
		||||
                                                {
 | 
			
		||||
                                                    ComputerCraft.log.error( "Error running task", t );
 | 
			
		||||
                                                }
 | 
			
		||||
                                                m_computer.queueEvent( "task_complete", new Object[] {
 | 
			
		||||
                                                    taskID, false, "Java Exception Thrown: " + t.toString()
 | 
			
		||||
                                                } );
 | 
			
		||||
                                            }
 | 
			
		||||
                                        }
 | 
			
		||||
                                    };
 | 
			
		||||
                                    if( MainThread.queueTask( iTask ) )
 | 
			
		||||
                                    {
 | 
			
		||||
                                        return taskID;
 | 
			
		||||
                                    }
 | 
			
		||||
                                    else
 | 
			
		||||
                                    {
 | 
			
		||||
                                        throw new LuaException( "Task limit exceeded" );
 | 
			
		||||
                                    }
 | 
			
		||||
                                }
 | 
			
		||||
 | 
			
		||||
                                @Override
 | 
			
		||||
                                public Object[] executeMainThreadTask( @Nonnull final ILuaTask task ) throws LuaException, InterruptedException
 | 
			
		||||
                                {
 | 
			
		||||
                                    // Issue task
 | 
			
		||||
                                    final long taskID = issueMainThreadTask( task );
 | 
			
		||||
 | 
			
		||||
                                    // Wait for response
 | 
			
		||||
                                    while( true )
 | 
			
		||||
                                    {
 | 
			
		||||
                                        Object[] response = pullEvent( "task_complete" );
 | 
			
		||||
                                        if( response.length >= 3 && response[1] instanceof Number && response[2] instanceof Boolean )
 | 
			
		||||
                                        {
 | 
			
		||||
                                            if( ((Number) response[1]).intValue() == taskID )
 | 
			
		||||
                                            {
 | 
			
		||||
                                                Object[] returnValues = new Object[response.length - 3];
 | 
			
		||||
                                                if( (Boolean) response[2] )
 | 
			
		||||
                                                {
 | 
			
		||||
                                                    // Extract the return values from the event and return them
 | 
			
		||||
                                                    System.arraycopy( response, 3, returnValues, 0, returnValues.length );
 | 
			
		||||
                                                    return returnValues;
 | 
			
		||||
                                                }
 | 
			
		||||
                                                else
 | 
			
		||||
                                                {
 | 
			
		||||
                                                    // Extract the error message from the event and raise it
 | 
			
		||||
                                                    if( response.length >= 4 && response[3] instanceof String )
 | 
			
		||||
                                                    {
 | 
			
		||||
                                                        throw new LuaException( (String) response[3] );
 | 
			
		||||
                                                    }
 | 
			
		||||
                                                    else
 | 
			
		||||
                                                    {
 | 
			
		||||
                                                        throw new LuaException();
 | 
			
		||||
                                                    }
 | 
			
		||||
                                                }
 | 
			
		||||
                                            }
 | 
			
		||||
                                        }
 | 
			
		||||
                                    }
 | 
			
		||||
 | 
			
		||||
                                }
 | 
			
		||||
                            }, method, arguments );
 | 
			
		||||
                            results = apiObject.callMethod( context, method, arguments );
 | 
			
		||||
                        }
 | 
			
		||||
                        catch( InterruptedException e )
 | 
			
		||||
                        {
 | 
			
		||||
@@ -466,7 +254,7 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
                            {
 | 
			
		||||
                                ComputerCraft.log.error( "Error calling " + methodName + " on " + apiObject, t );
 | 
			
		||||
                            }
 | 
			
		||||
                            throw new LuaError( "Java Exception Thrown: " + t.toString(), 0 );
 | 
			
		||||
                            throw new LuaError( "Java Exception Thrown: " + t, 0 );
 | 
			
		||||
                        }
 | 
			
		||||
                        return toValues( results );
 | 
			
		||||
                    }
 | 
			
		||||
@@ -560,22 +348,14 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
        {
 | 
			
		||||
            case Constants.TNIL:
 | 
			
		||||
            case Constants.TNONE:
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
            case Constants.TINT:
 | 
			
		||||
            case Constants.TNUMBER:
 | 
			
		||||
            {
 | 
			
		||||
                return value.toDouble();
 | 
			
		||||
            }
 | 
			
		||||
            case Constants.TBOOLEAN:
 | 
			
		||||
            {
 | 
			
		||||
                return value.toBoolean();
 | 
			
		||||
            }
 | 
			
		||||
            case Constants.TSTRING:
 | 
			
		||||
            {
 | 
			
		||||
                return value.toString();
 | 
			
		||||
            }
 | 
			
		||||
            case Constants.TTABLE:
 | 
			
		||||
            {
 | 
			
		||||
                // Table:
 | 
			
		||||
@@ -623,9 +403,7 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
                return table;
 | 
			
		||||
            }
 | 
			
		||||
            default:
 | 
			
		||||
            {
 | 
			
		||||
                return null;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -642,93 +420,199 @@ public class CobaltLuaMachine implements ILuaMachine
 | 
			
		||||
        return objects;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static class PrefixLoader extends VarArgFunction
 | 
			
		||||
    /**
 | 
			
		||||
     * A {@link DebugHandler} which observes the {@link TimeoutState} and responds accordingly.
 | 
			
		||||
     */
 | 
			
		||||
    private class TimeoutDebugHandler extends DebugHandler
 | 
			
		||||
    {
 | 
			
		||||
        private static final LuaString FUNCTION_STR = valueOf( "function" );
 | 
			
		||||
        private static final LuaString EQ_STR = valueOf( "=" );
 | 
			
		||||
        private final TimeoutState timeout;
 | 
			
		||||
        private int count = 0;
 | 
			
		||||
        boolean thrownSoftAbort;
 | 
			
		||||
 | 
			
		||||
        private boolean isPaused;
 | 
			
		||||
        private int oldFlags;
 | 
			
		||||
        private boolean oldInHook;
 | 
			
		||||
 | 
			
		||||
        TimeoutDebugHandler()
 | 
			
		||||
        {
 | 
			
		||||
            timeout = CobaltLuaMachine.this.timeout;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public Varargs invoke( LuaState state, Varargs args ) throws LuaError
 | 
			
		||||
        public void onInstruction( DebugState ds, DebugFrame di, int pc ) throws LuaError, UnwindThrowable
 | 
			
		||||
        {
 | 
			
		||||
            switch( opcode )
 | 
			
		||||
            di.pc = pc;
 | 
			
		||||
 | 
			
		||||
            if( isPaused ) resetPaused( ds, di );
 | 
			
		||||
 | 
			
		||||
            // We check our current pause/abort state every 128 instructions.
 | 
			
		||||
            if( (count = (count + 1) & 127) == 0 )
 | 
			
		||||
            {
 | 
			
		||||
                case 0: // "load", // ( func [,chunkname] ) -> chunk | nil, msg
 | 
			
		||||
                // If we've been hard aborted or closed then abort.
 | 
			
		||||
                if( timeout.isHardAborted() || m_state == null ) throw HardAbortError.INSTANCE;
 | 
			
		||||
 | 
			
		||||
                timeout.refresh();
 | 
			
		||||
                if( timeout.isPaused() )
 | 
			
		||||
                {
 | 
			
		||||
                    LuaValue func = args.arg( 1 ).checkFunction();
 | 
			
		||||
                    LuaString chunkname = args.arg( 2 ).optLuaString( FUNCTION_STR );
 | 
			
		||||
                    if( !chunkname.startsWith( '@' ) && !chunkname.startsWith( '=' ) )
 | 
			
		||||
                    {
 | 
			
		||||
                        chunkname = OperationHelper.concat( EQ_STR, chunkname );
 | 
			
		||||
                    }
 | 
			
		||||
                    return BaseLib.loadStream( state, new StringInputStream( state, func ), chunkname );
 | 
			
		||||
                }
 | 
			
		||||
                case 1: // "loadstring", // ( string [,chunkname] ) -> chunk | nil, msg
 | 
			
		||||
                {
 | 
			
		||||
                    LuaString script = args.arg( 1 ).checkLuaString();
 | 
			
		||||
                    LuaString chunkname = args.arg( 2 ).optLuaString( script );
 | 
			
		||||
                    if( !chunkname.startsWith( '@' ) && !chunkname.startsWith( '=' ) )
 | 
			
		||||
                    {
 | 
			
		||||
                        chunkname = OperationHelper.concat( EQ_STR, chunkname );
 | 
			
		||||
                    }
 | 
			
		||||
                    return BaseLib.loadStream( state, script.toInputStream(), chunkname );
 | 
			
		||||
                    // Preserve the current state
 | 
			
		||||
                    isPaused = true;
 | 
			
		||||
                    oldInHook = ds.inhook;
 | 
			
		||||
                    oldFlags = di.flags;
 | 
			
		||||
 | 
			
		||||
                    // Suspend the state. This will probably throw, but we need to handle the case where it won't.
 | 
			
		||||
                    di.flags |= FLAG_HOOKYIELD | FLAG_HOOKED;
 | 
			
		||||
                    LuaThread.suspend( ds.getLuaState() );
 | 
			
		||||
                    resetPaused( ds, di );
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                handleSoftAbort();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return NONE;
 | 
			
		||||
            super.onInstruction( ds, di, pc );
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public void poll() throws LuaError
 | 
			
		||||
        {
 | 
			
		||||
            // If we've been hard aborted or closed then abort.
 | 
			
		||||
            LuaState state = m_state;
 | 
			
		||||
            if( timeout.isHardAborted() || state == null ) throw HardAbortError.INSTANCE;
 | 
			
		||||
 | 
			
		||||
            timeout.refresh();
 | 
			
		||||
            if( timeout.isPaused() ) LuaThread.suspendBlocking( state );
 | 
			
		||||
            handleSoftAbort();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void resetPaused( DebugState ds, DebugFrame di )
 | 
			
		||||
        {
 | 
			
		||||
            // Restore the previous paused state
 | 
			
		||||
            isPaused = false;
 | 
			
		||||
            ds.inhook = oldInHook;
 | 
			
		||||
            di.flags = oldFlags;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void handleSoftAbort() throws LuaError
 | 
			
		||||
        {
 | 
			
		||||
            // If we already thrown our soft abort error then don't do it again.
 | 
			
		||||
            if( !timeout.isSoftAborted() || thrownSoftAbort ) return;
 | 
			
		||||
 | 
			
		||||
            thrownSoftAbort = true;
 | 
			
		||||
            throw new LuaError( TimeoutState.ABORT_MESSAGE );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static class StringInputStream extends InputStream
 | 
			
		||||
    private class CobaltLuaContext implements ILuaContext
 | 
			
		||||
    {
 | 
			
		||||
        private final LuaState state;
 | 
			
		||||
        private final LuaValue func;
 | 
			
		||||
        private byte[] bytes;
 | 
			
		||||
        private int offset, remaining = 0;
 | 
			
		||||
 | 
			
		||||
        StringInputStream( LuaState state, LuaValue func )
 | 
			
		||||
        @Nonnull
 | 
			
		||||
        @Override
 | 
			
		||||
        public Object[] yield( Object[] yieldArgs ) throws InterruptedException
 | 
			
		||||
        {
 | 
			
		||||
            this.state = state;
 | 
			
		||||
            this.func = func;
 | 
			
		||||
            try
 | 
			
		||||
            {
 | 
			
		||||
                LuaState state = m_state;
 | 
			
		||||
                if( state == null ) throw new InterruptedException();
 | 
			
		||||
                Varargs results = LuaThread.yieldBlocking( state, toValues( yieldArgs ) );
 | 
			
		||||
                return toObjects( results, 1 );
 | 
			
		||||
            }
 | 
			
		||||
            catch( LuaError e )
 | 
			
		||||
            {
 | 
			
		||||
                throw new IllegalStateException( e.getMessage() );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        @Override
 | 
			
		||||
        public int read() throws IOException
 | 
			
		||||
        public long issueMainThreadTask( @Nonnull final ILuaTask task ) throws LuaException
 | 
			
		||||
        {
 | 
			
		||||
            if( remaining <= 0 )
 | 
			
		||||
            // Issue command
 | 
			
		||||
            final long taskID = MainThread.getUniqueTaskID();
 | 
			
		||||
            final Runnable iTask = () -> {
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    Object[] results = task.execute();
 | 
			
		||||
                    if( results != null )
 | 
			
		||||
                    {
 | 
			
		||||
                        Object[] eventArguments = new Object[results.length + 2];
 | 
			
		||||
                        eventArguments[0] = taskID;
 | 
			
		||||
                        eventArguments[1] = true;
 | 
			
		||||
                        System.arraycopy( results, 0, eventArguments, 2, results.length );
 | 
			
		||||
                        m_computer.queueEvent( "task_complete", eventArguments );
 | 
			
		||||
                    }
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        m_computer.queueEvent( "task_complete", new Object[] { taskID, true } );
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
                catch( LuaException e )
 | 
			
		||||
                {
 | 
			
		||||
                    m_computer.queueEvent( "task_complete", new Object[] { taskID, false, e.getMessage() } );
 | 
			
		||||
                }
 | 
			
		||||
                catch( Throwable t )
 | 
			
		||||
                {
 | 
			
		||||
                    if( ComputerCraft.logPeripheralErrors ) ComputerCraft.log.error( "Error running task", t );
 | 
			
		||||
                    m_computer.queueEvent( "task_complete", new Object[] {
 | 
			
		||||
                        taskID, false, "Java Exception Thrown: " + t
 | 
			
		||||
                    } );
 | 
			
		||||
                }
 | 
			
		||||
            };
 | 
			
		||||
            if( m_computer.queueMainThread( iTask ) )
 | 
			
		||||
            {
 | 
			
		||||
                LuaValue s;
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    s = OperationHelper.call( state, func );
 | 
			
		||||
                }
 | 
			
		||||
                catch( LuaError e )
 | 
			
		||||
                {
 | 
			
		||||
                    throw new IOException( e );
 | 
			
		||||
                }
 | 
			
		||||
                return taskID;
 | 
			
		||||
            }
 | 
			
		||||
            else
 | 
			
		||||
            {
 | 
			
		||||
                throw new LuaException( "Task limit exceeded" );
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
                if( s.isNil() )
 | 
			
		||||
        @Override
 | 
			
		||||
        public Object[] executeMainThreadTask( @Nonnull final ILuaTask task ) throws LuaException, InterruptedException
 | 
			
		||||
        {
 | 
			
		||||
            // Issue task
 | 
			
		||||
            final long taskID = issueMainThreadTask( task );
 | 
			
		||||
 | 
			
		||||
            // Wait for response
 | 
			
		||||
            while( true )
 | 
			
		||||
            {
 | 
			
		||||
                Object[] response = pullEvent( "task_complete" );
 | 
			
		||||
                if( response.length >= 3 && response[1] instanceof Number && response[2] instanceof Boolean )
 | 
			
		||||
                {
 | 
			
		||||
                    return -1;
 | 
			
		||||
                }
 | 
			
		||||
                LuaString ls;
 | 
			
		||||
                try
 | 
			
		||||
                {
 | 
			
		||||
                    ls = s.strvalue();
 | 
			
		||||
                }
 | 
			
		||||
                catch( LuaError e )
 | 
			
		||||
                {
 | 
			
		||||
                    throw new IOException( e );
 | 
			
		||||
                }
 | 
			
		||||
                bytes = ls.bytes;
 | 
			
		||||
                offset = ls.offset;
 | 
			
		||||
                remaining = ls.length;
 | 
			
		||||
                if( remaining <= 0 )
 | 
			
		||||
                {
 | 
			
		||||
                    return -1;
 | 
			
		||||
                    if( ((Number) response[1]).intValue() == taskID )
 | 
			
		||||
                    {
 | 
			
		||||
                        Object[] returnValues = new Object[response.length - 3];
 | 
			
		||||
                        if( (Boolean) response[2] )
 | 
			
		||||
                        {
 | 
			
		||||
                            // Extract the return values from the event and return them
 | 
			
		||||
                            System.arraycopy( response, 3, returnValues, 0, returnValues.length );
 | 
			
		||||
                            return returnValues;
 | 
			
		||||
                        }
 | 
			
		||||
                        else
 | 
			
		||||
                        {
 | 
			
		||||
                            // Extract the error message from the event and raise it
 | 
			
		||||
                            if( response.length >= 4 && response[3] instanceof String )
 | 
			
		||||
                            {
 | 
			
		||||
                                throw new LuaException( (String) response[3] );
 | 
			
		||||
                            }
 | 
			
		||||
                            else
 | 
			
		||||
                            {
 | 
			
		||||
                                throw new LuaException();
 | 
			
		||||
                            }
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            --remaining;
 | 
			
		||||
            return bytes[offset++];
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static final class HardAbortError extends Error
 | 
			
		||||
    {
 | 
			
		||||
        private static final long serialVersionUID = 7954092008586367501L;
 | 
			
		||||
 | 
			
		||||
        static final HardAbortError INSTANCE = new HardAbortError();
 | 
			
		||||
 | 
			
		||||
        private HardAbortError()
 | 
			
		||||
        {
 | 
			
		||||
            super( "Hard Abort", null, true, false );
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -7,27 +7,61 @@
 | 
			
		||||
package dan200.computercraft.core.lua;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaAPI;
 | 
			
		||||
import dan200.computercraft.api.lua.ILuaObject;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
import java.io.OutputStream;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Represents a machine which will execute Lua code. Technically this API is flexible enough to support many languages,
 | 
			
		||||
 * but you'd need a way to provide alternative ROMs, BIOSes, etc...
 | 
			
		||||
 *
 | 
			
		||||
 * There should only be one concrete implementation at any one time, which is currently {@link CobaltLuaMachine}. If
 | 
			
		||||
 * external mod authors are interested in registering their own machines, we can look into how we can provide some
 | 
			
		||||
 * mechanism for registering these.
 | 
			
		||||
 *
 | 
			
		||||
 * This should provide implementations of {@link dan200.computercraft.api.lua.ILuaContext}, and the ability to convert
 | 
			
		||||
 * {@link ILuaObject}s into something the VM understands, as well as handling method calls.
 | 
			
		||||
 */
 | 
			
		||||
public interface ILuaMachine
 | 
			
		||||
{
 | 
			
		||||
    void addAPI( ILuaAPI api );
 | 
			
		||||
    /**
 | 
			
		||||
     * Inject an API into the global environment of this machine. This should construct an object, as it would for any
 | 
			
		||||
     * {@link ILuaObject} and set it to all names in {@link ILuaAPI#getNames()}.
 | 
			
		||||
     *
 | 
			
		||||
     * Called before {@link #loadBios(InputStream)}.
 | 
			
		||||
     *
 | 
			
		||||
     * @param api The API to register.
 | 
			
		||||
     */
 | 
			
		||||
    void addAPI( @Nonnull ILuaAPI api );
 | 
			
		||||
 | 
			
		||||
    void loadBios( InputStream bios );
 | 
			
		||||
    /**
 | 
			
		||||
     * Create a function from the provided program, and set it up to run when {@link #handleEvent(String, Object[])} is
 | 
			
		||||
     * called.
 | 
			
		||||
     *
 | 
			
		||||
     * This should destroy the machine if it failed to load the bios.
 | 
			
		||||
     *
 | 
			
		||||
     * @param bios The stream containing the boot program.
 | 
			
		||||
     * @return The result of loading this machine. Will either be OK, or the error message when loading the bios.
 | 
			
		||||
     */
 | 
			
		||||
    MachineResult loadBios( @Nonnull InputStream bios );
 | 
			
		||||
 | 
			
		||||
    void handleEvent( String eventName, Object[] arguments );
 | 
			
		||||
    /**
 | 
			
		||||
     * Resume the machine, either starting or resuming the coroutine.
 | 
			
		||||
     *
 | 
			
		||||
     * This should destroy the machine if it failed to execute successfully.
 | 
			
		||||
     *
 | 
			
		||||
     * @param eventName The name of the event. This is {@code null} when first starting the machine. Note, this may
 | 
			
		||||
     *                  do nothing if it does not match the event filter.
 | 
			
		||||
     * @param arguments The arguments for this event.
 | 
			
		||||
     * @return The result of loading this machine. Will either be OK, or the error message that occurred when
 | 
			
		||||
     * executing.
 | 
			
		||||
     */
 | 
			
		||||
    MachineResult handleEvent( @Nullable String eventName, @Nullable Object[] arguments );
 | 
			
		||||
 | 
			
		||||
    void softAbort( String abortMessage );
 | 
			
		||||
 | 
			
		||||
    void hardAbort( String abortMessage );
 | 
			
		||||
 | 
			
		||||
    boolean saveState( OutputStream output );
 | 
			
		||||
 | 
			
		||||
    boolean restoreState( InputStream input );
 | 
			
		||||
 | 
			
		||||
    boolean isFinished();
 | 
			
		||||
 | 
			
		||||
    void unload();
 | 
			
		||||
    /**
 | 
			
		||||
     * Close the Lua machine, aborting any running functions and deleting the internal state.
 | 
			
		||||
     */
 | 
			
		||||
    void close();
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,81 @@
 | 
			
		||||
/*
 | 
			
		||||
 * This file is part of ComputerCraft - http://www.computercraft.info
 | 
			
		||||
 * Copyright Daniel Ratcliffe, 2011-2019. Do not distribute without permission.
 | 
			
		||||
 * Send enquiries to dratcliffe@gmail.com
 | 
			
		||||
 */
 | 
			
		||||
 | 
			
		||||
package dan200.computercraft.core.lua;
 | 
			
		||||
 | 
			
		||||
import dan200.computercraft.core.computer.TimeoutState;
 | 
			
		||||
 | 
			
		||||
import javax.annotation.Nonnull;
 | 
			
		||||
import javax.annotation.Nullable;
 | 
			
		||||
import java.io.InputStream;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * The result of executing an action on a machine.
 | 
			
		||||
 *
 | 
			
		||||
 * Errors should halt the machine and display the error to the user.
 | 
			
		||||
 *
 | 
			
		||||
 * @see ILuaMachine#loadBios(InputStream)
 | 
			
		||||
 * @see ILuaMachine#handleEvent(String, Object[])
 | 
			
		||||
 */
 | 
			
		||||
public final class MachineResult
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * A successful complete execution.
 | 
			
		||||
     */
 | 
			
		||||
    public static final MachineResult OK = new MachineResult( false, false, null );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * A successful paused execution.
 | 
			
		||||
     */
 | 
			
		||||
    public static final MachineResult PAUSE = new MachineResult( false, true, null );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * An execution which timed out.
 | 
			
		||||
     */
 | 
			
		||||
    public static final MachineResult TIMEOUT = new MachineResult( true, false, TimeoutState.ABORT_MESSAGE );
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * An error with no user-friendly error message
 | 
			
		||||
     */
 | 
			
		||||
    public static final MachineResult GENERIC_ERROR = new MachineResult( true, false, null );
 | 
			
		||||
 | 
			
		||||
    private final boolean error;
 | 
			
		||||
    private final boolean pause;
 | 
			
		||||
    private final String message;
 | 
			
		||||
 | 
			
		||||
    private MachineResult( boolean error, boolean pause, String message )
 | 
			
		||||
    {
 | 
			
		||||
        this.pause = pause;
 | 
			
		||||
        this.message = message;
 | 
			
		||||
        this.error = error;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static MachineResult error( @Nonnull String error )
 | 
			
		||||
    {
 | 
			
		||||
        return new MachineResult( true, false, error );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public static MachineResult error( @Nonnull Exception error )
 | 
			
		||||
    {
 | 
			
		||||
        return new MachineResult( true, false, error.getMessage() );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isError()
 | 
			
		||||
    {
 | 
			
		||||
        return error;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public boolean isPause()
 | 
			
		||||
    {
 | 
			
		||||
        return pause;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
    public String getMessage()
 | 
			
		||||
    {
 | 
			
		||||
        return message;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -22,9 +22,9 @@ public class Terminal
 | 
			
		||||
    private int m_width;
 | 
			
		||||
    private int m_height;
 | 
			
		||||
 | 
			
		||||
    private TextBuffer m_text[];
 | 
			
		||||
    private TextBuffer m_textColour[];
 | 
			
		||||
    private TextBuffer m_backgroundColour[];
 | 
			
		||||
    private TextBuffer[] m_text;
 | 
			
		||||
    private TextBuffer[] m_textColour;
 | 
			
		||||
    private TextBuffer[] m_backgroundColour;
 | 
			
		||||
 | 
			
		||||
    private final Palette m_palette;
 | 
			
		||||
 | 
			
		||||
@@ -40,7 +40,7 @@ public class Terminal
 | 
			
		||||
    {
 | 
			
		||||
        m_width = width;
 | 
			
		||||
        m_height = height;
 | 
			
		||||
        this.onChanged = changedCallback;
 | 
			
		||||
        onChanged = changedCallback;
 | 
			
		||||
 | 
			
		||||
        m_cursorColour = 0;
 | 
			
		||||
        m_cursorBackgroundColour = 15;
 | 
			
		||||
 
 | 
			
		||||
@@ -29,23 +29,23 @@ public class ComputerTracker
 | 
			
		||||
    public ComputerTracker( Computer computer )
 | 
			
		||||
    {
 | 
			
		||||
        this.computer = new WeakReference<>( computer );
 | 
			
		||||
        this.computerId = computer.getID();
 | 
			
		||||
        this.fields = new Object2LongOpenHashMap<>();
 | 
			
		||||
        computerId = computer.getID();
 | 
			
		||||
        fields = new Object2LongOpenHashMap<>();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    ComputerTracker( ComputerTracker timings )
 | 
			
		||||
    {
 | 
			
		||||
        this.computer = timings.computer;
 | 
			
		||||
        this.computerId = timings.computerId;
 | 
			
		||||
        computer = timings.computer;
 | 
			
		||||
        computerId = timings.computerId;
 | 
			
		||||
 | 
			
		||||
        this.tasks = timings.tasks;
 | 
			
		||||
        this.totalTime = timings.totalTime;
 | 
			
		||||
        this.maxTime = timings.maxTime;
 | 
			
		||||
        tasks = timings.tasks;
 | 
			
		||||
        totalTime = timings.totalTime;
 | 
			
		||||
        maxTime = timings.maxTime;
 | 
			
		||||
 | 
			
		||||
        this.serverCount = timings.serverCount;
 | 
			
		||||
        this.serverTime = timings.serverTime;
 | 
			
		||||
        serverCount = timings.serverCount;
 | 
			
		||||
        serverTime = timings.serverTime;
 | 
			
		||||
 | 
			
		||||
        this.fields = new Object2LongOpenHashMap<>( timings.fields );
 | 
			
		||||
        fields = new Object2LongOpenHashMap<>( timings.fields );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Nullable
 | 
			
		||||
 
 | 
			
		||||
Some files were not shown because too many files have changed in this diff Show More
		Reference in New Issue
	
	Block a user