Skip to content

Commit b68848e

Browse files
committed
Localized docs basics/*, getting-started/* and blocks/*
1 parent 8af7442 commit b68848e

13 files changed

Lines changed: 2501 additions & 78 deletions

docs/basics/building-script.md

Lines changed: 123 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,126 @@
1-
---
2-
hide_title: true
3-
---
1+
# Script Building
42

5-
```mdx-code-block
6-
import NotLocalized from "@site/src/components/NotLocalized"
3+
In addition to the ability to directly specify an executable script, there is often a need to split the source code into files. As soon as the project starts to contain the first classes, utility functions, and other things that should be separated, working in one file becomes simply impossible. This is where build files come to our aid, as well as advanced toolchain settings.
74

8-
<NotLocalized />
5+
## Compiling Files
6+
7+
The `compile` property is used to run files; adding any script to it will lead to its execution. We have already considered the description of its element:
8+
9+
```json
10+
{
11+
"path": "relative path from the folder where build.config is located",
12+
"sourceType": "launcher, main, custom, preloader or library"
13+
}
14+
```
15+
16+
If you do not know which `sourceType` to choose, consider [Mod Lifecycle](TODO). As already mentioned, the `compile` property defines scripts that will be compiled and loaded. For example, *build.config* might look like this:
17+
18+
```json title="build.config"
19+
{
20+
...
21+
"compile": [
22+
{
23+
"path": "main.js",
24+
"sourceType": "main"
25+
}
26+
]
27+
}
28+
```
29+
30+
Which means that the script will be loaded from the current folder from the file *main.js* and will start after calling `Launch();` from the project launcher. But in this case, the file is not built, but simply loaded from an existing one. And what if we want to assemble several files into one?
31+
32+
## There Can Be Any Number of Scripts
33+
34+
And to assemble them all, you need to combine the scripts into one. Inner Core or the Toolchain's TypeScript Compiler will do this for you, depending on which platform you are using.
35+
36+
Let's start with defining the `buildDirs` property of your *build.config* for assembly:
37+
38+
```json title="build.config"
39+
{
40+
...
41+
"buildDirs": [
42+
{
43+
"targetSource": "main.js",
44+
"dir": "dev/"
45+
}
46+
]
47+
}
48+
```
49+
50+
Now files located in the *dev/* folder will be assembled into the *main.js* file, which we defined earlier. But to determine which scripts will be assembled, in addition to this, we need to define an *.includes* file in the created *dev/* directory.
51+
52+
## .includes
53+
54+
The file describes the list of files that will be included in the final script. Let's consider its implementation with an example:
55+
56+
```gitignore title=".includes"
57+
# comments can describe why a particular file is included
58+
header.js
59+
60+
api/tests.js
61+
api/globals.js
62+
63+
# all files in the folder will be included, in the case of toolchain
64+
# subfolders are also included additionally
65+
module/.
66+
67+
// comments can also be used in this form
68+
```
69+
70+
Empty lines and comments are ignored when building files. In this case, at least three files will be included in the script: *header.js*, as well as *tests.js* and *globals.js* in the *api/* folder. The *module/* folder may not exist at all. Non-existent files are ignored, sending a warning about this to the log.
71+
72+
These same rules apply to the toolchain, however comments in it can contain properties for configuring *tsconfig.json*.
73+
74+
### The Power of Toolchain Comments
75+
76+
Build files in the toolchain, in addition to describing the list of files, can contain a description for compiler options, as well as exceptions for files already included in the build:
77+
78+
```gitignore title=".includes"
79+
# removeComments
80+
// checkJs: true
81+
82+
# lib: es6, es2016.array.include
83+
84+
header.js
85+
86+
# very important files
87+
module/.
88+
89+
!module/tests.ts
90+
91+
# target: es3
992
```
93+
94+
The *header.js* file and all files from the *module/* folder and subfolders will be included in the build, excluding *module/tests.ts*. Files are included as well as excluded only if they exist. Since the toolchain builds *tsconfig.json*, paths can also contain glob selections for files:
95+
96+
```gitignore title=".includes"
97+
module/tile_*.js
98+
```
99+
100+
Thus, the files *module/tile_chest.js* and *module/tile_advanced_workbench.js* will be included, but *module/bounce.js* and *module/tile_bedrock.ts* will not be included.
101+
102+
:::info Analysis of compiler option description
103+
104+
Comments will be excluded from the assembled script (and from declarations too), JavaScript will be type-checked just like TypeScript, instead of the latest libraries only ES6 (ES2015) and `Array.include` from ES2016 will be included, and the output script will be presented in ES3 instead of ES5. If you do not need any of these options or any other, just do not use them.
105+
106+
:::
107+
108+
## Building with Toolchain
109+
110+
Apart from the build file description, the structure above does not apply to the toolchain. The toolchain automatically determines the passed values, defining what needs to be assembled, a file or a folder. In addition, it supports building using TypeScript. To assemble or compile the desired file, use:
111+
112+
```json title="make.json"
113+
{
114+
...
115+
"sources": [
116+
{
117+
"type": "main",
118+
"source": "dev",
119+
"language": "typescript",
120+
"target": "main.js"
121+
}
122+
]
123+
}
124+
```
125+
126+
This will do the same as the `compile` and `buildDirs` properties described earlier; but also, files that have the *.ts* extension will be assembled. Study the *make.json* properties for more details.

docs/basics/evaluate-context.md

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,102 @@
1-
---
2-
hide_title: true
3-
---
1+
# Execution Context
42

5-
```mdx-code-block
6-
import NotLocalized from "@site/src/components/NotLocalized"
3+
Defines the space where all your code resides. Variables and constant values, functions, and converted classes are stored here, and future code interactions with the space are performed here.
74

8-
<NotLocalized />
5+
Each function and object can create its own, so-called "local" space. It is not available from the outside, but values from it can be obtained through the hierarchy below. I think if most of what is written remains unclear, it is time to fix this problem.
6+
7+
## API — Global Context
8+
9+
If in browsers, and other browser-like engines, the main object is the page whose content we modify, then in the case of Inner Core, we interact directly with the game. And for this we need spaces, methods, and classes, which are in the global context.
10+
11+
Inner Core defines several global contexts, each with its own specific goals. For many years, the main one has been Core Engine, since the entire functionality of the launcher is located around it. This context extends the capabilities of another, Adapted Script. It provides purely native methods provided by the game itself, or auxiliary ones for the operation of Core Engine itself. There are also Preloader, used exclusively by the preloader, and Preferences Window API, which was used for the functioning of the workbench, and once for in-game mod settings. Their use is quite specific, so we will not consider them in detail.
12+
13+
```md
14+
- AdaptedScript
15+
- CoreEngine
16+
- PrefsWinAPI
17+
- Preloader
18+
```
19+
20+
These contexts are used in every script and are described in the `api` properties of your *build.config*.
21+
22+
In the case of Core Engine, it is possible to change the global context, as done, for example, in Ender IO:
23+
24+
```js title="dev/Base/Items/powder.js"
25+
// highlight-malformed-start
26+
Item.createDyeItem = function(id, name, type) {
27+
IDRegistry.genItemID(id);
28+
Item.createItem(id, name, {
29+
name: "item_material_organic_" + type + "_dye"
30+
}, { stack: 64 });
31+
};
32+
// highlight-malformed-end
33+
34+
Item.createDyeItem("greenDye", "Organic Green Dye", "green");
35+
Item.createDyeItem("blackDye", "Organic Black Dye", "black");
36+
Item.createDyeItem("brownDye", "Organic Brown Dye", "brown");
37+
```
38+
39+
However, think about the consequences, this will change the context in all mods. That is, the `Item.createDyeItem` method can be called from any mod if Ender IO is installed. But in this case, there is also a danger of replacing existing methods or those added with updates. If you need to export something, consider [ModAPI.registerAPI](TODO), it was created specifically for these purposes.
40+
41+
### Global Values
42+
43+
Each context defines its own namespace, access to which can be used to obtain values or perform actions. However, in addition to values in the context, the engine provides built-in values exclusive to each mod individually. These values are the same regardless of which script this space touches. This includes technical values, mod information, and in-game constants.
44+
45+
```js
46+
const MINECRAFT_VERSION = getMCPEVersion();
47+
if (MINECRAFT_VERSION.main == 16) {
48+
alert("Minecraft " + MINECRAFT_VERSION.str + " is currently relevant!");
49+
// outputs: Minecraft 1.16.201 is currently relevant!
50+
} else if (MINECRAFT_VERSION.main == 11) {
51+
alert("Legacy version " + MINECRAFT_VERSION.array.join("-") + " is not relevant for " + __name__);
52+
// outputs: Legacy version 1-11-4 is not relevant for <mod name>
53+
}
54+
```
55+
56+
The `getMCPEVersion` and `alert` methods here are part of the global API context, while `__name__` is a global value available from any part of the mod. You do not need to think about what properties are available here, they are listed in the API summary and toolchain declarations.
57+
58+
## Script Body
59+
60+
The last and most important step in the formation of your space is the code itself. JavaScript uses Rhino 1.7.7 (partially supporting ES6, however, most of the functionality remained in the ES5 implementation) as the main engine that executes all scripts. The language is interpreted, which means it is processed in real time, or with minimal processing before launch. All code is executed sequentially, and by script body we mean all the code that is contained in the files included in the build.
61+
62+
The script is usually executed once, and any spaces it defines (this can be variables or constants, as well as objects or functions) are activated as a result of events. The content, the logic of your mod is defined in the body, other content is integrated, and everything else that the engine is capable of in principle.
63+
64+
## Restartable Contexts
65+
66+
Considering the life cycle earlier, we superficially introduced __custom__ scripts. Unlike other scripts, this is the only type that can be run any number of times, and in addition, accepts and returns values. Probably, you could have associations with functions if acquaintance with the language once went successfully. But unlike functions, these are still spaces; they work in a new context and store values.
67+
68+
Consider this with an example from Solar Flux Reborn:
69+
70+
```js title="dev/tests.js"
71+
alert(
72+
runCustomSource("custom.js", {
73+
THUNDER_MULTIPLIER: 0.4,
74+
RAIN_MULTIPLIER: 0.6,
75+
WEATHER: World.getWeather()
76+
})
77+
);
978
```
79+
80+
Values passed to the space will be overwritten or created if they do not exist yet. Any constants created in the body of restartable scripts must be set only once. Their repeated modification will cause an error, in which case a simple option would be to place them between `try-catch`.
81+
82+
```js title="custom.js"
83+
let raining = WEATHER.rain / 10;
84+
raining = raining > 0.2 ? (raining - 0.2) / 0.8 : 0;
85+
raining = Math.sin(raining * Math.PI / 2);
86+
raining = 1 - raining * (1 - RAIN_MULTIPLIER);
87+
88+
let thundering = WEATHER.thunder / 10;
89+
thundering = thundering > 0.75 ? (thundering - 0.75) / 0.25 : 0;
90+
thundering = Math.sin(thundering * Math.PI / 2);
91+
thundering = 1 - thundering * (1 - THUNDER_MULTIPLIER);
92+
93+
// Values must still be returned by a function, since
94+
// the script body is not capable of returning anything
95+
(function() {
96+
return raining * thundering;
97+
})();
98+
```
99+
100+
Depending on weather conditions, the efficiency of solar panels changes, values from the current space are involved. Thus, `runCustomSource("custom.js")` will not change the values and will return the previous result. The context does not change, so the values remain the same.
101+
102+
Modding tools use this type to execute code during the game. Since their space is easily configured and saved between different executions independently of the rest of the mod context, subdividing the main code here was a good idea. A more concrete way to use these scripts simply does not exist. Suggest your ideas where such functionality could be useful.

0 commit comments

Comments
 (0)