Compare commits

...
Author SHA1 Message Date
Cooldude2606 5b56aa3fea v2.0.0-alpha.21 2025-05-26 00:29:28 +01:00
Daniel 56479246ed Merge pull request #767 from Cooldude2606/tests/factorio-2.0
Bump tests: Remove Node 18 & Add Factorio 2.0
2025-05-25 23:31:27 +02:00
Danielv123 773c98b31a Prepare factorio 2.0 in devcontainer 2025-05-25 22:46:25 +02:00
Cooldude2606 4f6abca9d7 Bump ts module as per node 20 recommended 2025-05-21 17:13:54 +01:00
Cooldude2606 0bbaef4751 Remove node 18 add factorio 2.0 2025-05-21 17:13:54 +01:00
Cooldude2606 3ff80119e6 Fix flaking tests 2025-05-21 17:13:43 +01:00
Cooldude2606 63e30dd0ee Add missing migrations for system info and tests (#766)
* Add missing migration for SystemInfo

* Add missing migration tests
2025-05-21 17:07:24 +01:00
Daniel 6e01ba18c5 Merge pull request #765 from Cooldude2606/fix/exchange-strings-v2
Impliment decode for map exchange strings v2
2025-05-19 21:17:03 +02:00
Daniel a39911d667 Merge pull request #764 from Cooldude2606/refactor/config-file-path
Refactor config file paths into the config class
2025-05-19 20:52:14 +02:00
Cooldude2606 4c5ab21c64 Fix version comparison 2025-05-18 21:37:46 +01:00
Daniel cca1d4efc0 Merge pull request #763 from Cooldude2606/feature/whitelist-watching
Add bidirectional whitelist sync
2025-05-18 15:50:08 +02:00
Daniel 78d4217a2a Merge pull request #762 from Cooldude2606/feature/remote-update
Add core package updates + plugin listing, updating, and remote install
2025-05-17 23:26:19 +02:00
Cooldude2606 25a5a2257c Fix test that depends on map strings 2025-05-16 19:40:17 +01:00
Cooldude2606 0f8075ad41 Implement v2 map strings 2025-05-16 19:39:43 +01:00
Cooldude2606 e089e2df4c Improve exchange string tests 2025-05-15 16:46:21 +01:00
Cooldude2606 cd20464952 Fix grammar error 2025-05-13 23:14:20 +01:00
Cooldude2606 cb23428507 Respond to comments and add docs 2025-05-12 23:19:14 +01:00
Cooldude2606 f285d7d803 Add tests for save and load 2025-05-12 22:00:40 +01:00
Cooldude2606 117713eda3 Fix silent test for alt host 2025-05-12 21:32:45 +01:00
Cooldude2606 515093abd2 Improve error message logged for config load error 2025-05-12 21:32:44 +01:00
Cooldude2606 726a3538c2 Move save and load into config class 2025-05-12 21:32:44 +01:00
Cooldude2606 17db53403a Move filepath into config class 2025-05-12 21:30:48 +01:00
Cooldude2606 c650f42276 Enable source maps for test setup 2025-05-12 21:30:47 +01:00
Cooldude2606 6989fbdd44 Move defaultAccess to static property 2025-05-12 21:30:47 +01:00
Cooldude2606andCopilot 9f0633abbb Update packages/lib/src/permissions.ts
Copilot Suggestion - Fxies spelling error

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-11 17:33:54 +01:00
Cooldude2606andCopilot 4e6f7c5402 Update packages/lib/src/rce_ops.ts
Copilot Suggestion - A-Z was the intend range.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-11 17:33:26 +01:00
Daniel 0adb45bba4 Merge pull request #758 from Cooldude2606/feature/pending-restart
Add indicators for host version mismatch and required restart
2025-05-11 14:14:39 +02:00
Cooldude2606 be071be8f2 Add recovery startup mode (#757)
* Add recovery startup mode

* Change error code to RecoveryMode
2025-05-10 21:33:45 +01:00
Cooldude2606 7cf13a8b3b Add user search and pagination 2025-05-10 15:52:16 +02:00
Cooldude2606 90de1ecbf0 Fix bidirectional sync test 2025-05-06 18:05:05 +01:00
Cooldude2606 405af0e173 Add bidirectional whitelist sync 2025-05-06 17:14:27 +01:00
Cooldude2606 5e9beb0cf6 Add tests 2025-05-04 03:35:50 +01:00
Cooldude2606 0eb56b1711 Disable options if a system doesn't support it 2025-05-03 01:53:56 +01:00
Cooldude2606 42ad7f8704 Add plugin list to update modal 2025-04-30 00:05:24 +01:00
Cooldude2606 ff35b5c378 Basic functionality of remote npm 2025-04-29 01:40:22 +01:00
Cooldude2606 c120443657 Add checks for updated version on disk 2025-04-27 23:57:58 +01:00
Cooldude2606 96c5f33dbe Add tests for config changes 2025-04-27 23:29:14 +01:00
Cooldude2606 cf9f087cba Add icons for mis-matched version and restart required 2025-04-27 22:35:00 +01:00
Cooldude2606 47c5e0fabd Fix incorrect ip detection in create 2025-04-22 20:52:30 +01:00
Cooldude2606 a9def876e8 Remove phin without introducing new dependencies (#755) 2025-04-15 19:09:25 +01:00
Cooldude2606 eafbc32d3d Fix open file stream error on some systems 2025-04-15 00:02:54 +01:00
Daniel bbf65dea57 Support quality and ghosts for power armor equipment (#753) 2025-04-13 21:32:23 +01:00
Daniel fcd3dbb65b Merge pull request #748 from Cooldude2606/feature/console-log
Add config option for console logging
2025-04-13 21:56:40 +02:00
Daniel f21a3f45bf Merge pull request #712 from O2theC/auto-mod-update - basic mod portal integration
Add mod portal integration and mod downloads to the web interface
2025-04-13 21:52:07 +02:00
Danielv123 1711271ca8 Fix broken test 2025-04-13 21:44:25 +02:00
Danielv123 25e1398229 Revert "fixed mod export command"
This reverts commit 7f4159b6c6.
2025-04-13 18:13:27 +02:00
Danielv123 2d62ff6689 Reduce type duplication 2025-04-13 18:10:17 +02:00
Danielv123 25099c03ba Use config instead of constants, other review commetns 2025-04-13 18:00:52 +02:00
Danielv123 c6f10e2195 Refactor to avoid duplicated code 2025-04-13 14:22:57 +02:00
Danielv123 1f5f28c820 Same styling of mod portal link and mod download button 2025-04-12 00:06:33 +02:00
Danielv123 a79b129b0c Fix and refactor download buttons 2025-04-12 00:03:37 +02:00
Danielv123 e841aa5685 Add tests 2025-04-11 23:54:42 +02:00
Danielv123 35fef5e021 Cleanup redundant code, remove unused code, add checks 2025-04-11 23:22:37 +02:00
Danielv123 de8a7ef884 Support mod portal downloads 2025-04-11 22:19:11 +02:00
Cooldude2606 f3a62e585f Add log filters to web ui (#746)
* Add max level select to logs

Closes: #745

* Add log filter for server actions

Closes: #744

* Respond to comments

Change name of component, clean up onChange anon function
2025-04-10 16:04:05 +01:00
Danielv123 2936973a75 Link to mod portal 2025-04-08 22:54:16 +02:00
Danielv123 d50230becc Cache mods on controller 2025-04-08 22:42:03 +02:00
Danielv123 600a5d5ce1 Search mods clientside 2025-04-08 22:34:32 +02:00
Daniel 81fb223d5e Fix webpack export not found warnings (#750) 2025-04-08 13:13:50 -07:00
Cooldude2606andDanielv123 0e6d33aa82 Add button to download ctl config (#743)
* Move saveJson to util file

* Add button to download ctl config

* Add icon and tooltip to describe what the button does

---------

Co-authored-by: Danielv123 <danielv@danielv.no>
2025-04-08 20:34:50 +01:00
Danielv123 3bf0d48641 Fix linter warnings 2025-04-08 20:53:06 +02:00
Danielv123 3fae97ac8b Fix mod description rendering 2025-04-08 20:52:56 +02:00
Daniel 42cd099eea Merge pull request #749 from Cooldude2606/fix/736
Fix incorrect use of Array.splice
2025-04-06 13:46:47 -07:00
Cooldude2606 916c48844a Fix incorrect use of Array.splice
This should resolve the sync loops for bans and admins
2025-04-06 18:06:05 +01:00
Cooldude2606 ce85149b4c Add config cloning on instance creation (#747)
* Add instance config cloning on creation

* Add test for instance cloning

* Add tooltip to copy config button
2025-04-06 02:00:08 +01:00
Cooldude2606 0055f28f45 Add bulk user actions (Import/Export/Restore) (#741)
* Add "Bulk Actions" modal to users page

* Add control logic without validation

* Add restore and better permission check

* Add controller logic and automatic backup

* Add CTL commands for bulk actions

* Add tests for bulk actions

* Improve description on permissions
2025-04-06 01:42:51 +01:00
Cooldude2606 1d5040dc0c Improve error page with social links 2025-04-05 02:33:24 -07:00
Cooldude2606 60ee4b2c86 Add config option for console logging 2025-03-29 01:41:19 +00:00
Cooldude2606 7cb191d332 v2.0.0-alpha.20.b
Republish controller to use correct web_ui version.
2025-03-23 17:24:17 +00:00
Cooldude2606 c50298d408 Bump version of web_ui to alpha 20
There has been no functional changes or bug fixes made to web ui between this commit and the previous alpha 20 release.
2025-03-23 15:58:22 +00:00
Hornwitser aeac7eedec zip_ops: Deduce root folder from first zip entry (#739)
Some mods, for example cubium, includes bogus folders like __MACOSX in
addition to the root folder where everything is located.  This only
works by chance as Factorio will check for the folder of the first
entry in the zip file and use that.

Match Factorio's logic in findRoot so that these mods work with
Clusterio.
2025-03-21 23:00:30 +00:00
Danielv123 2ae3f9a600 Add mod portal search 2025-03-15 23:13:33 +01:00
O2theC 05790966db removed unused imports 2025-03-15 15:30:00 +01:00
O2theC a60a3bcbd7 fixed type, split download modpack function into downloadmods and updatemods , returns a map of mods and their latest versions 2025-03-15 15:29:59 +01:00
O2theC 61a241b02d added function to update modpacks 2025-03-15 15:29:59 +01:00
O2theC e1c29bcde1 added functions to download mods from a Map of names and versions 2025-03-15 15:29:59 +01:00
O2theC 7f4159b6c6 fixed mod export command 2025-03-15 15:29:59 +01:00
O2theC 4080c1025d added download file function 2025-03-15 15:29:59 +01:00
O2theC 64e429c5f6 got rid of pesky type definitions for function returns and better map merge 2025-03-15 15:29:59 +01:00
O2theC 24c07c0ed1 it use maps now, much safer 2025-03-15 15:29:59 +01:00
O2theC 61249e5e7e no more undefines in that return dict 2025-03-15 15:29:59 +01:00
O2theC a2a3bcd4f8 adjusted function input for getting latest mod version and adding chunking to not hit request limits 2025-03-15 15:29:59 +01:00
O2theC 71c1cffcac moved interfaces to ModStore and made fetchLatestVersionMod functions 2025-03-15 15:29:59 +01:00
O2theC a769c3179a made modRelease and modDetails interface and made a rough function to get the latest version of a mod from it's name 2025-03-15 15:29:59 +01:00
Cooldude2606 6180a0b27b Fix lint error in UsersPage.tsx 2025-03-14 23:43:42 +00:00
Cooldude2606 4e9df0bc21 v2.0.0-alpha.20 2025-02-28 23:55:11 +00:00
Cooldude2606 2c44890100 Move package release to workflow docs 2025-02-28 23:34:24 +00:00
Daniel febdbd4fd6 Document release workflow (#738) 2025-02-28 23:11:59 +00:00
Danielv123 d622ab5127 Fix missing key on users table roles column
Results in inconsistent visual behaviour when creating and deleting roles
2025-02-27 16:13:33 +01:00
Danielv123 a893109a06 Fix NaN in subspace storage database 2025-02-27 00:05:48 +01:00
Cooldude2606 9f888867fe Dep version bumps
Temp Fix #737
2025-02-25 21:09:11 +00:00
Danielv123 12f833fcc3 Add clusterio_lib release changelog 2025-02-25 19:14:32 +01:00
Daniel a16dcd315f Merge pull request #733 from clusterio/feature/subspace-storage-space-age
Feature/subspace storage space age
2025-02-25 18:53:36 +01:00
Danielv123 9cd68fcb36 Bump version number 2025-02-25 18:39:05 +01:00
Danielv123 ebafda5407 Update with comments from review 2025-02-25 16:33:52 +01:00
Danielv123 a9e1bdcc65 Test itemDatabase with quality and migrations from pre quality format 2025-02-16 14:44:49 +01:00
Danielv123 f907455620 Validate quality parameter 2025-02-16 14:44:15 +01:00
Danielv123 19336cdd3b Remove test dependency on old version of subspace mod 2025-02-16 14:30:47 +01:00
Danielv123 5d37712db4 Fix online time export in 2.0 w backwards compat 2025-02-16 13:34:35 +01:00
Cooldude2606 8996671bea Add default mod packs (#735) 2025-02-14 14:47:26 +00:00
Daniel 7b8945ff15 Don't write input stats to both input and output (#734) 2025-02-14 14:47:13 +00:00
Danielv123 fbdf206e31 Test: Don't depend on a specific version of clusterio_lib 2025-01-25 17:55:43 +01:00
Danielv123andCooldude2606 edfe32c59e Simplify checks based on review comments
Co-authored-by: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com>
2025-01-25 17:39:47 +01:00
Danielv123 f78966860e Build clusterio lib with CI 2025-01-24 19:23:15 +01:00
Danielv123 2162351710 Bump clusterio_lib versions along with subspace 2025-01-24 17:49:04 +01:00
Danielv123 a33aa3b7e5 Show quality in web UI 2025-01-23 18:54:06 +01:00
Danielv123 e8570029b0 Send and return requests 2025-01-23 00:04:00 +01:00
Danielv123 4fd123abc1 Add quality support to subspace storage 2025-01-22 19:46:23 +01:00
Cooldude2606 26269df7d4 Add 2.0.29 freeplay hash
Hot patch to add the updated 2.0.29 control.lua freeplay hash to knoww scenarios.
2025-01-21 20:43:00 +00:00
phidias 52d8822048 Fix human readable time format (#732) 2025-01-20 11:12:34 +00:00
Daniel 97e25eb885 Handle missing logistic requester point (#731) 2025-01-20 11:11:47 +00:00
Cooldude2606 fa1338ddd4 Add decisions log (#714)
* Add decisions log

* Response to Daniel's comments

* Add PR Links
2025-01-13 21:30:06 +00:00
Cooldude2606 4d66dc5de3 v2.0.0-alpha19.b
Add missing prepare step for web_ui package.
2024-12-24 20:26:31 +00:00
Cooldude2606 0baf47c8e7 v2.0.0-alpha.19 2024-12-23 22:08:23 +00:00
Cooldude2606 65d1fe53d6 Update Change Log 2024-12-23 22:02:59 +00:00
Cooldude2606 16bc651afc Add bidirectional sync to admin and ban list (#709)
* Add bidirectional sync to admin and ban list

* Add tests

* Response to comments

* Skip broken tests from change of default value
2024-12-23 18:58:44 +00:00
Cooldude2606 91c1d2e6c6 Fix version variables in compat.lua (#713) 2024-12-23 15:31:04 +00:00
Cooldude2606 ff9ba0271f Expand compat to 0.17 (#711) 2024-12-13 13:48:52 +00:00
Daniel 87746edbf5 Merge pull request #708 from clusterio/fix/cpu-usage-without-wmic
Use Get-Process instead of wmic on windows
2024-12-09 15:25:13 -08:00
Daniel 6f5547b524 Merge pull request #706 from clusterio/feature/stat-exporter-surfaces
Feature/stat exporter surfaces
2024-12-09 15:24:19 -08:00
Danielv123 c0d33d5852 In 2.0, icon_size is optional with a default of 64 - fixes #699 2024-12-09 15:23:45 -08:00
Daniel 485504d885 Recreate instance's required subfolders on startup (#705)
Fixes #698
2024-12-09 15:21:59 -08:00
Daniel b093cd136f Merge pull request #703 from clusterio/feature/connect-to-server
Add connect to server button to web UI using steam launch api
2024-12-09 15:17:12 -08:00
Danielv123 7b34d238ea Use Get-Process instead of wmic on windows 2024-12-03 17:37:40 +00:00
Danielv123 8e22de9d2d Add platform speed and weight metrics
clusterio_statistics_exporter_platform_speed
clusterio_statistics_exporter_platform_weight
2024-12-02 21:35:25 +00:00
Danielv123 9464541d61 Update tests for multi surface support 2024-12-02 21:14:50 +00:00
Danielv123 fd741f9b49 Add clusterio_statistics_exporter_platform_mapping
This allows queries to map between the platform surfaces tracked in the statistics and the name assigned by the player
2024-12-02 21:05:24 +00:00
Danielv123 7934e471a7 Provide production statistics for all surfaces 2024-12-02 21:04:00 +00:00
Daniel 1090197acc Merge pull request #697 from clusterio/fix/json-function-backwards-compatibility
Add backwards compatible json serialization capability
2024-12-01 10:42:05 +00:00
Danielv123 c89b773f26 Reference in readme 2024-12-01 10:38:06 +00:00
Danielv123 c5c73c8c96 Add connect to server button to web UI using steam launch api
Does not work if factorio is already open. Resolves #608
2024-12-01 10:32:25 +00:00
Danielv123 54411bc327 Use builtin version when available 2024-12-01 09:14:04 +00:00
Daniel 98a15d3366 Merge pull request #692 from clusterio/plugin-management
Add plugins automatically
2024-11-29 20:18:42 +00:00
Danielv123 17133dc1f5 Test for compatability with missing dependencies field in package.json 2024-11-29 20:12:53 +00:00
Danielv123 d7792ca369 Log error when a specified plugin does not exist, but allow loading without it
Preventing loading just because a plugin is missing would cause the host to become inaccessible if something went wrong when managing plugins through the remote interface
2024-11-29 20:01:45 +00:00
Danielv123 747c13a715 Only write changed plugin list once 2024-11-29 20:00:42 +00:00
Cooldude2606 583d484573 Merge pull request #701 from Cooldude2606/feature/enum-config-input 2024-11-28 23:22:54 +00:00
Danielv123 9b0a06815b Correctly handle installs with no plugins or dependencies 2024-11-28 20:27:31 +00:00
Danielv123 45ae20051f Fix specifying default quality for 1.1 migration 2024-11-28 20:18:22 +00:00
Danielv123 d1baab5e7f Synchronize space age logistics slots
Requests of the v1 format is automatically upgraded and placed in a new section. Filters in named sections are not synchronized to avoid messing with non-player entities on servers you join. Synchronizing logistics groups falls under the scope of another plugin.
2024-11-28 20:18:22 +00:00
Daniel 6c3c094e94 Prefer linking to issues over pull requests 2024-11-27 17:32:45 +00:00
Daniel 1ed52d8fe6 Create pull_request_template.md 2024-11-27 17:32:45 +00:00
Danielv123 2fb33c9648 Support synchronizing crafting queue in 2.0 with new storage api 2024-11-27 17:28:59 +00:00
Cooldude2606andDanielv123 4248e57dcb Add config migrations (#693)
Co-authored-by: Danielv123 <danielv@danielv.no>
2024-11-26 23:14:56 +00:00
Danielv123 85cdbb1185 Add backwards compatible json serialization capability
resolves #687 using https://github.com/rxi/json.lua
2024-11-26 19:11:53 +00:00
Danielv123 2e729b9e47 Add tests for monorepo plugins and non-plugin packages 2024-11-26 10:33:26 +00:00
Danielv123 427acfe82e Refactor, fix linting issues 2024-11-26 10:18:54 +00:00
Danielv123 79c4bf1441 Ignore missing plugins instead of throwing 2024-11-26 10:07:14 +00:00
Danielv123 fea07e4f55 Add tests 2024-11-26 10:03:35 +00:00
Danielv123 5dbfff9bfd Support for monorepo autodiscovery 2024-11-26 09:23:09 +00:00
Danielv123 37fed2064e Warn when multiple plugins of the same name are detected 2024-11-26 08:49:30 +00:00
Danielv123 f95e1d2998 Only log not-a-plugin for non-core packages 2024-11-26 08:27:29 +00:00
Danielv123 5586b1640b Include keywords for clusterio and factorio in all first party packages 2024-11-26 08:17:45 +00:00
Danielv123 91cbd7a464 Handle files not existing 2024-11-25 18:26:35 +00:00
Danielv123 472177bf39 Add jsdoc, reduce logging 2024-11-25 18:07:51 +00:00
Danielv123 397dc6a5a6 Autodiscover plugins in dependencies with keyword "clusterio-plugin" 2024-11-25 17:23:03 +00:00
Danielv123 636777d935 Add locally provided plugins by default 2024-11-25 12:22:16 +00:00
Cooldude2606 05ca4eb25e Merge pull request #686 from Cooldude2606/feature/2.0-compat
Add 2.0 support for clusterio api and 1st party plugins
2024-11-23 22:39:47 +00:00
Cooldude2606 ebf8504507 Patches after manual testing 2024-11-23 14:27:06 +00:00
Cooldude2606 ba3d74388d Update changelog 2024-11-23 14:27:03 +00:00
Cooldude2606 8b551deda8 Add 2.0 support to module template 2024-11-23 14:26:12 +00:00
Cooldude2606 2ba33d58ed Add 2.0 support to statistics_exporter 2024-11-23 14:26:11 +00:00
Cooldude2606 33e05c244c Add 2.0 support to research_sync 2024-11-23 14:26:11 +00:00
Cooldude2606 4ca5d17cb0 Add 2.0 support to player_auth 2024-11-23 14:26:11 +00:00
Cooldude2606 4ee508d1b2 Add 2.0 support to inventory_sync 2024-11-23 14:26:10 +00:00
Cooldude2606 ad836d3776 Add 2.0 support to core lua 2024-11-23 14:26:10 +00:00
Cooldude2606 201a201f05 Add compat lib 2024-11-23 14:25:57 +00:00
Cooldude2606 4c64ef9d15 Merge pull request #684 from Cooldude2606/feature/2.0-scenarios
Add support for 2.0 builtin mods and scenarios
2024-11-22 13:42:40 +00:00
Cooldude2606 e4f0ab1e8f Merge branch 'master' into feature/2.0-scenarios 2024-11-22 13:36:33 +00:00
Cooldude2606 efa2b76ab0 Take the earlier first join time 2024-11-22 04:06:34 -08:00
Cooldude2606 d80385c947 Add test for user merging 2024-11-22 04:06:34 -08:00
Cooldude2606 bdf02a5ea2 Made user ids case insensitive 2024-11-22 04:06:34 -08:00
Daniel 41d76ff757 Prevent bypassing command permission with leading whitespace 2024-11-21 08:46:22 -08:00
Cooldude2606 c5841b17a0 Add tests for new config value 2024-11-21 08:46:22 -08:00
Cooldude2606 762212747e Added factorio.enable_script_commands 2024-11-21 08:46:22 -08:00
Cooldude2606 feb09a6fe0 Update change log 2024-11-21 08:25:23 -08:00
Cooldude2606 06d9d74b67 Add default autoComplete for credential fields 2024-11-21 08:25:23 -08:00
Cooldude2606 be4c31c10f Add new node LTS to CI 2024-11-21 08:25:23 -08:00
Cooldude2606 1556c0afdd Enable source maps in tests 2024-11-21 08:25:23 -08:00
Cooldude2606 069f568941 Add silent test as npm command 2024-11-21 08:25:23 -08:00
Cooldude2606 4ef930c08c Add file excluded by gitignore 2024-11-13 22:29:21 +00:00
Cooldude2606 3850558d49 Add and fix tests 2024-11-13 22:17:27 +00:00
Cooldude2606 81444cc447 Fix ModPackView and add 2.0 scenario support 2024-11-13 19:11:59 +00:00
Cooldude2606 c714d86c5c Added Builtin Mods 2024-11-13 17:32:14 +00:00
Cooldude2606 c6354ded70 Merge pull request #683 from Cooldude2606/fix/winston-log-format
Bump winston, winston-transport, and resolve type error
2024-11-12 18:11:17 +00:00
Cooldude2606 cb29db0e45 Bump winston-transport@4.9 2024-11-12 17:15:15 +00:00
Cooldude2606 7298ef2ea7 Bump winston@3.17 and resolve type error 2024-11-12 16:46:01 +00:00
Cooldude2606 ace8356f68 Merge pull request #679 from Cooldude2606/fix/whitelist-remove
Fix whitelist remove command with tests. Tests check that set-banned and set-whitelist will send rcon commands to running instances and that it is case insensitive.
2024-11-01 01:29:10 +00:00
Cooldude2606 19b5c4d857 Fix whitelist command 2024-10-31 16:07:58 +00:00
Cooldude2606 f6a40baa55 Added tests for banlist/whitelist rcon commands.
Tests check that set-banned and set-whitelist will send rcon commands to running instances and that it is case insensitive.
2024-10-31 16:02:12 +00:00
Cooldude2606 0afbb69308 Use sync file writes and catch errors to reset state. (#677)
By using sync file writes it prevents interrupts between the state change and the server process starting. Also adding error catching to reset the state following an error writing to the files.
Fixes: #676
2024-10-29 16:50:05 +00:00
Cooldude2606 9e03c743e9 Merge pull request #678 from Cooldude2606/fix/host-user-sync
Fix host being unable to sync user state to instances after controller connection
2024-10-29 16:28:37 +00:00
Cooldude2606 dba2dd246e Update change log 2024-10-29 01:15:58 +00:00
Cooldude2606 198e58c762 Allow host as src for user sync events 2024-10-29 01:03:08 +00:00
Cooldude2606 ef6861a566 Fixed host tests not checking the message was valid 2024-10-29 00:59:17 +00:00
Hornwitser 60e5ed008d Fix unhandled promise rejection in Link tests 2024-10-28 20:15:33 +01:00
Hornwitser 3838bc6a70 Remove "instance" from ConfigLocation
When the config system was ported to TypeScript the "instance" location
was accidentally added to the type of available locations for access
control.  This has never been a valid location as it was deemed too
complicated to work with.  The correct location for instances is
"controller" and "host" as these are the locations the configs are
handled.
2024-10-28 20:15:09 +01:00
Hornwitser a6033f4171 Merge pull request #675 from clusterio/fix-instance-update
Fix instance update
2024-10-28 20:14:18 +01:00
Hornwitser 6f85ec9dd6 Log files written to aid debugging
Log to the verbose level when a datastore file read or written to in
order to make it easier to diagnose whether the files are written or
not.
2024-10-28 20:07:34 +01:00
Cooldude2606 e0b72e568d Mark RCON password as a credential (#673)
* Mark RCON password as a credential

* Update location name and description

* Add controller to array to avoid UB
2024-10-28 18:46:26 +00:00
Hornwitser 34da073800 Fix setting instance config fields not being saved
If only the config of an instance is updated the dirty flag of the
instance is not set, which results in changes being discarded when the
controller shuts down afterwards.

Fix by always triggering the update logic when an instance field is
changed.
2024-10-28 19:30:03 +01:00
Cooldude2606 300db5cbfd Change display title for controller.external_address (#674)
* Rename controller.external_address

* Revert and correct title to avoid breaking change

* Update change log
2024-10-28 17:53:37 +00:00
Cooldude2606 c49bc07e15 Add hidden and readonly config properties (#665) 2024-10-27 23:02:45 +00:00
Cooldude2606 c31671d1e6 Added 2.0 zip file name to version matcher (#671)
* Added 2.0 zip file name to version matcher

Fixes: Install problem ? #668

* Add changelog entry for 671.
2024-10-27 22:53:34 +00:00
Cooldude2606 9d18d18603 Update CI to support testing of multiple factorio versions (#672)
We have plans to support the latest version and one minor previous. 2.0 will be added to CI once enough changes have been made to support it.
2024-10-27 22:24:15 +00:00
Cooldude2606 8a47fd8414 Update tests 2024-09-21 18:09:29 +01:00
Cooldude2606 49d584394d Add security logic to readonly and object support 2024-09-21 17:41:46 +01:00
Cooldude2606 e3eb266704 Add hidden and readonly config property 2024-09-21 01:52:01 +01:00
Cooldude2606 3e6806ff0e Merge pull request #664 from Cooldude2606/feature/role-drop-down
Add InputRole as a drop down selector
2024-09-21 01:22:52 +01:00
Cooldude2606 76349fa780 Merge branch 'master' into feature/role-drop-down 2024-09-21 01:06:07 +01:00
Cooldude2606 d4b0e658c3 Change log updates 2024-09-21 01:03:51 +01:00
Cooldude2606 549ec3d1e7 Add InputRole as a drop down selector 2024-09-21 01:01:36 +01:00
Hornwitser d5f3ac0f4c Merge pull request #663 from Hornwitser/config-credentials
Implement shared Factorio credentials
2024-09-19 00:42:52 +02:00
Hornwitser a09a564358 Log where Factorio credentials taken from
Provide ease of diagnosing credential issues by accurately logging if
the credentials were sourced from the instance config, the host config,
the controller config, or if no credentials were found.
2024-09-18 23:41:45 +02:00
Hornwitser f873f4b369 Add autocomplete attribute to credential fields
Prevent browsers from automatically autofilling the password entry box
for the Factorio credentials by setting the autocomplete property to
new-password on the password input.
2024-09-18 23:10:31 +02:00
Hornwitser 44e5847a1d Implement shared Factorio credentials
Add config entries to controller and host for the Factorio username and
token to use when talking to the Factorio APIs.  If set an username and
token is not set in factorio.settings then these settings are used,
making it easy to configure one set of credentials for a cluster/host.

For running more public provide your own hardware clusters, there's an
option to disable controller sharing the credential with the hosts so
that each host have to set their own credentials.
2024-09-16 23:23:21 +02:00
Hornwitser eedf5b8f67 Credential option for config entries
Implement a credential option to config entries which is a list of
locations the config entry can be read from.  When specified and the
option is a string field this gets rendered as a password field in the
config UI.  If the credential is not readable from control the value of
the fields is cleared when the UI is refreshed.
2024-09-16 23:23:21 +02:00
Cooldude2606 77fea7e295 Fix unrelibable datastore tests (#662)
* Use incrementDateMs

* Change wait time to 15ms

* Fix loop condition
2024-09-14 17:20:52 +01:00
Hornwitser a1c7173dd2 Merge pull request #661 from Hornwitser/windows-rework
Rework Factorio server stop logic
2024-09-14 18:15:19 +02:00
Cooldude2606 dfc406d025 Host shutdown on duplicate connection (#647)
* Update no-shadow lint rule for enums

https://typescript-eslint.io/rules/no-shadow/

* Add websocket close codes enum

* Pass close code and reason to event handlers

* Shutdown host on duplicate connection

* Add connection refusal

* Fix lib/link tests

* Response to comments

* Additional Tests

* Removed missed codes in close event
2024-09-14 16:39:59 +01:00
Hornwitser 10a857c4ad Gracefully handle RCON connection dropping
Handle the edge cases of RCON authentication failing and RCON connection
failing during startup so that server stop still works in these cases.
2024-09-14 16:09:17 +02:00
Hornwitser e4aeafaf14 Set a map limit to speed up integration test 2024-09-14 15:05:13 +02:00
Hornwitser a1e7b24298 Fix lingering killed server flag
Fix killing the Factorio server causing the next run of it to report a
different error when it's killed by an external factor than what's
expected.
2024-09-14 15:05:13 +02:00
Hornwitser 7ff06f2b96 Rewrite Factorio server shutdown logic
Rewrite shutdown logic for Factorio servers to send a /quit command via
RCON for both Linux and Windows when stopping a server to avoid having
two confusingly intermixed code paths for Windows and Unix systems.
This makes shutdown on Windows tell clients the server shut down instead
of abruptly stoping to respond.
2024-09-14 15:05:13 +02:00
Hornwitser 36fc2ac1db Support nested factorio dirs in server test
Source the factorio directory from the host config as with other tests
and handle it pointing to a folder of Factorio installations.
2024-09-14 15:05:13 +02:00
Hornwitser 64cc9c2b64 Use factorioDir for alt-host config
Source the factorio installation from the host config for the alt host
test like with other tests.
2024-09-14 14:58:55 +02:00
Hornwitser c0e631e251 Fix tables sorting being broken
Newer versions of Ant Design requires either dataIndex or key to be
defined on columns for sorting of the tables to work.  Add missing key
entries on columns.
2024-09-12 22:32:42 +02:00
Hornwitser 8e21290b22 Enable skipped tests on Windows
Use the stop host command to stop alt-host so that the tests that depend
on it can also be run on Windows.
2024-09-12 15:26:12 +02:00
Hornwitser a05aef260f Allow iterating through datastores like a Map
Support the for (const [k, v] of datastore) syntax which is commonly
used with maps by adding a Symbol.iterator function returning the
appropriate iterator.
2024-09-04 13:43:56 +02:00
Hornwitser 7547a48fd6 Fix controller not accepting old hosts.json format
Migrate the content of the hosts.json file before attempting to loading
it into the new datastore format.
2024-09-04 13:40:15 +02:00
Arvids Godjuks 33c9542524 Merge pull request #660 from clusterio/bugfix/special-case-base-mod
Fix built-in mods showing up as missing in web UI
2024-09-04 14:36:29 +03:00
Hornwitser 95396ff1c3 Fix this is not a constructor error on startup
When passing methods around they lose their binding to this, which means
the fromJSON methods no longer has this be their class.  Fix by binding
the methods to the class they belong to before passing them on to the
datastore.
2024-09-04 13:32:38 +02:00
Arvids Godjuks 59091d74f5 Fix built-in mods showing up as missing in web ui
Added "builtInMods" array that contains a list of built-in mod names that we check against to not show an error and remove ability to delete those mods from mod packs

Fixes #581
2024-09-04 14:14:55 +03:00
Hornwitser 6226a45d0b Fix safeOutputFile writing to wrong directory (#659)
When renaming a file over another file the operation is only atomic if
both the source file and the target file is in the same directory.  Fix
safeOutputFile writing the temporary file to a different directory than
the target file due to the path separator not being included when
creating the temporary file path.
2024-09-04 00:14:47 +02:00
Hornwitser 84958e3f9d Merge pull request #629 from Cooldude2606/feature/datastore
Impliment datastores to reduce boilerplate
2024-09-04 00:14:16 +02:00
Hornwitser a2ea2f2604 Merge branch 'master' into feature/datastore 2024-09-04 00:08:52 +02:00
Hornwitser 5d97e8e816 Source factorio dir from config-host.json in tests
If a factorio dir is configured in config-host.json then use that in the
tests.  This removes the requirement of having a factorio installation
in ./factorio or linking to one there when running the tests.
2024-09-03 21:58:45 +02:00
Hornwitser f3150a9261 Add tests for Address.addressedTo
Make sure the confusing logic in addressedTo is working correctly by
adding tests for it.
2024-09-03 21:58:45 +02:00
Hornwitser 4068129124 Remove useless div on drop zone
This would render on top of modals and doesn't appear to be necessary.
2024-09-03 21:58:45 +02:00
Hornwitser a883e692e4 Add changelog for #619 2024-09-03 21:58:45 +02:00
Hornwitser 5a73a73c36 Fix installer setting bad controller_url
The host.controller_url config is required to end with a / for the url
joining logic to work.  Fix code added #619 setting this to a value not
ending with a /.
2024-09-03 21:58:45 +02:00
Hornwitser a1856e6edf Merge pull request #573 from CCpersonguy/feature/instance-version-2
Display Instance's Factorio version in the web UI
2024-09-03 21:57:42 +02:00
Arvids Godjuks b2f0000a64 Update CHANGELOG.md
Fixed extra new line in changelog
2024-09-03 14:58:38 +03:00
Cooldude2606 8054bd66d2 Fix instance subscriptions (#655)
Remove some assumptions that prevent subscription listeners from working on an
instance.  This means allowing the host to forward the event from the instance
to the controller; correctly storing subscription listeners by address rather
than by connection; and a bunch of network and event stuff to correctly clean up
a listener.

Remove the assumption that all subscriptions events contains the updates
property.  This meant fixing inconsistent types; always checking if the property
existed; fixing lots of the tests that made incorrect assumptions.

Minior fixes:
- Fix double firing of listener callbacks on first connection.
- Fix synced being set to true too early on first connection.
- Fix edge case interaction between last response and last snapshot for values
  with updated set as 0.

Also change the the output made during the start of tests to indicate something
is happening when start up is slow and allow for silent testing to reduce the
amount of text output to the console.
2024-09-03 12:31:32 +02:00
Arvids Godjuks 0c63852a2c Merge branch 'master' into feature/instance-version-2 2024-09-03 02:09:28 +03:00
Hornwitser 47896247fe Add option to set path to server executable (#657)
Allow overriding the logic selecting the path to the Factorio
executable.  This is mainly useful when working with development
versions of the game.
2024-09-03 01:04:50 +02:00
Arvids Godjuks 1687f2f896 Merge pull request #654 from Cooldude2606/feature/plugin-mono-repos
Include mono repo plugins in build
2024-09-03 02:01:04 +03:00
Arvids Godjuks 7e5608e2d8 Merge pull request #656 from clusterio/bugfix/numeric-user-name-as-string
Fix bootstrap command missing type declaration for admin name
2024-09-03 01:31:57 +03:00
Arvids Godjuks 55fcd90ed2 Fix bootstrap command missing type declaration for admin name
Lack of typing was resulting in admin name being serialized to storage as a number and breaking websocket communications protocol due to type mismatch: expected string, got number.

Fixes #536
2024-09-03 01:24:24 +03:00
Cooldude2606 7db5821967 Update change log and publish steps (#646) 2024-09-02 23:06:40 +02:00
Cooldude2606 5215ee8eef Include mono repo plugins in build 2024-09-02 22:01:59 +01:00
Hornwitser 0b50a6a29e Add changelog entry for #652 2024-09-02 22:43:53 +02:00
Arvids Godjuks b69db08414 Merge pull request #652 from Hornwitser/fix-installer-breaking-on-space
Escape arguments when installer invokes commands
2024-09-02 23:18:02 +03:00
Hornwitser 0cd24d120c Install optional dependencies on CI
The @swc/core module depends on optional dependencies being installed in
order to get the correct native binding's package installed.  Remove
--no-optional flag passed to pnpm to install these.
2024-08-30 01:03:54 +02:00
Hornwitser 70cf680edd Escape arguments when installer invokes commands
The usage of shell: true means that no escaping is done on the arguments
passed to the commands invoked by the installer. This leads to the
installation failing if the user for example puts a space in the
username for the admin account.

Fix by escaping the arguments passed on command lines. This is
unreasonably complicated on Windows 😡.
2024-08-30 00:32:58 +02:00
Cooldude2606 660af756e6 Minor Corrections 2024-08-16 07:00:08 +02:00
Cooldude2606 08c522429b Implemented configDir for outDir 2024-08-16 07:00:08 +02:00
Cooldude2606 02df956f6c Fix breaking change introduced in v5.5
https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/#simplified-reference-directive-declaration-emit
2024-08-16 07:00:08 +02:00
Cooldude2606 263bb16619 Fix missing codecov upload token 2024-08-16 06:58:14 +02:00
Cooldude2606 1f47f6dfc9 Update all pages to use PageHeader 2024-06-14 19:20:07 +02:00
Danielv123 51657bea44 Extend PageHeader for plugins
My plugins used the antd pageheader more than base did. I did not notice that these features dissappeared when we migrated from antd to our own implementation - this brings them back
2024-06-14 19:19:42 +02:00
Cooldude2606 498638c0c9 Improve config, message, and module templates (#641)
* Improve config, message, and module templates

* Update global handler in template module

* Add comments to empty instance template
2024-06-08 13:53:23 +01:00
Cooldude2606 51c450727d Add clean script to remove dist and other artifacts (#640)
* Add clean script

* Switch to using yargs
2024-06-08 13:53:00 +01:00
Cooldude2606 dbc79779ea Fix sending log messages across invalid link (#639) 2024-06-08 13:52:29 +01:00
Cooldude2606 2c2521a688 Add error tests for sendEvent and sendRequest (#638) 2024-06-07 15:46:49 +01:00
Cooldude2606 70e95a56b9 Fix test starting alt host under windows 2024-06-07 07:28:04 -06:00
Cooldude2606 fa0b0a6c21 Bump ci action versions (#637) 2024-05-26 19:28:27 +01:00
Cooldude2606 8b823a571d Coverage 100% 2024-05-25 18:17:50 +01:00
Cooldude2606 e90451ead8 Fix broken test 2024-05-25 01:20:58 +01:00
Cooldude2606 222a8abdb8 Update change log 2024-05-25 00:32:14 +01:00
Cooldude2606 eb658f86c0 Add unit tests 2024-05-24 22:10:04 +01:00
Cooldude2606 d696b8b52c Change abstract class name 2024-05-24 18:29:08 +01:00
Danielv123 2dbf264d46 Add bun compatibility 2024-05-24 17:25:29 +01:00
Danielv123 ffc1a0fccd Add devcontainer configuration file 2024-05-23 11:54:04 +01:00
Lars Corbijn fddd65ddb8 Update documentation links for typescript change (#633) 2024-05-22 17:52:27 +01:00
Cooldude2606 94a07b5af4 Give restore abilty to memory provider 2024-05-22 17:42:25 +01:00
Cooldude2606 af50cfd47a Consistency changes 2024-05-22 17:19:05 +01:00
Cooldude2606 609db31027 Add hosts, instances, systems 2024-05-22 16:39:22 +01:00
Danielv123 e3ab71a2e9 Fix broken types
Some time between antd 5.15.0 and 5.17.3 they moved around some types, which caused our builds to break. This fixes the issue.
2024-05-21 19:08:15 +01:00
Cooldude2606 509fab358a Add saves datastore 2024-05-19 02:28:36 +01:00
Cooldude2606 6b11fb810a Change mod packs to datastore 2024-05-19 01:41:28 +01:00
Cooldude2606 80b4a96e85 Add datastore to lib 2024-05-19 01:24:30 +01:00
Cooldude2606 f4679ef6d1 Merge pull request #625 from Cooldude2606/fix/loopback-routing
Fix loopback routing message and add tests
2024-05-18 23:04:18 +01:00
Danielv123 eb78cdc322 Allow declaring custom config input components for objects
Input components allow us to customize the config field editing experience in plugins, but due to an oversight this did not include object definitions.
2024-05-18 20:57:46 +01:00
Danielv123 8705377232 Add typebox dependency 2024-05-18 20:57:16 +01:00
Danielv123 692075df1d Add useInstanceConfig hook
Currently the web interface implements configs by pulling the state directly in the components where they are used. I need to access the instance config for a plugin, which would require me to duplicate parts of the instance config retrival code.
Instead, I implement it here under lib. This allows 3rd party consumers to get immediate access if we decide to implement a subscription model for configs in the future.
2024-05-18 20:57:16 +01:00
Cooldude2606 ebc0298115 Update changelog 2024-05-18 18:48:08 +01:00
Cooldude2606 d1fa5e67d4 Improve error throwing for sending events 2024-05-18 18:42:53 +01:00
Daniel 7489a76d4a Merge pull request #602 from Danielv123/react-to-drag-and-drop
React to drag and drop
2024-05-18 17:57:31 +02:00
Cooldude2606 201209efb3 Fix undefined stack on some IPC errors (#624) 2024-05-18 14:56:37 +01:00
Danielv123 c6abe29e89 Correctly disable dropzone when host is offline 2024-05-18 10:27:40 +01:00
Danielv123 c8e54cd563 Fix crash loading mutiplayer map in singleplayer
Caused by on_player_created not being called when
loading a multiplayer map in singleplayer. For
further discussion see $622 and
https://forums.factorio.com/viewtopic.php?f=7&t=113497
2024-05-18 10:59:19 +02:00
Cooldude2606 0c9e91b953 Fix error message on instance loopback attempt 2024-05-18 03:40:18 +01:00
Cooldude2606 fb4a5e40f3 Add tests for loopbacks 2024-05-18 03:33:55 +01:00
Danielv123 05b697fd24 Prevent dropping files outside of dropzones 2024-05-15 19:43:40 +01:00
Daniel d7c2e7eed7 Update mod management section of readme (#618)
* Update mod management section of readme

Resolves #617

* Update section after review
2024-05-15 10:41:24 -06:00
Danielv123 f1fd8247fb Add factorio version indicator feature to changelog 2024-05-15 10:40:36 +01:00
Danielv123 92912fa9e5 Show configured instance version while not running 2024-05-15 10:33:37 +01:00
Danielv123 e3753f292b Show current factorio version on instance page 2024-05-15 09:50:39 +01:00
CCpersonguy b57c419769 Version should disappear first on narrow windows 2024-05-15 09:50:39 +01:00
CCpersonguy 594158deac Remove verion-caching logic in Controller 2024-05-15 09:50:39 +01:00
CCpersonguy 3d3276b02c fix lint errors 2024-05-15 09:50:39 +01:00
CCpersonguy dc2f86dcf3 allow FactorioServer.version to return undefined 2024-05-15 09:50:39 +01:00
CCpersonguy 8a57d9b5f6 display Factorio version in Instances web UI 2024-05-15 09:50:39 +01:00
Cooldude2606 b9e9a754b8 Change all workspace versions to workspace:* (#621) 2024-05-08 20:38:05 +01:00
Cooldude2606 54f56d0ecd Merge pull request #619 from Danielv123/prompt-for-http-port
Installer promt for http port
2024-05-05 20:39:25 +01:00
Danielv123 131e45e10a Skip create integration tests
Tests are skipped because they are really slow and fail in CI
2024-05-05 02:24:57 +02:00
Danielv123 b27256421d Fixed tests 2024-05-05 02:00:16 +02:00
Danielv123 e405f09f33 Add integration tests for create 2024-05-05 01:46:48 +02:00
Danielv123 78a9f73de8 Installer promt for http port
Resolves #564
2024-05-05 00:18:36 +02:00
Danielv123 b6113a1007 Merge branch 'master' into react-to-drag-and-drop 2024-05-04 20:49:29 +02:00
Danielv123 c34a13b3e4 Use global drag and drop hint overlay 2024-05-04 20:31:36 +02:00
Danielv123 920b5c1655 Merge branch 'master' into react-to-drag-and-drop 2024-05-04 15:55:33 +02:00
Hornwitser e341adfb29 Remove redundant if condition
Noticed-By: @Cooldude2606
2024-04-28 20:39:27 +02:00
Hornwitser 464d588b5e v2.0.0-alpha.18 2024-04-28 19:14:27 +02:00
Hornwitser 0ddc1899fa Merge pull request #616 from Hornwitser/fix-mod-settings-types
Handle bad mod setting types
2024-04-28 19:12:22 +02:00
Hornwitser 4e1116ae2d Gracefully handle bad mod setting types in Web UI
Make sure all cobinations of missmatched types and missing mod settings
are gracefully handled by the Web UI.

Fixes #609.
2024-04-28 16:59:03 +02:00
Hornwitser 3cf9f96233 Coerce mod settings types during data export
Use the settings prototypes to coerce any incorrectly typed mod settings
to the correct type either by casting or by replacing the value with the
default value from the prototype.

This will fix cases where an update to a mod changes the type of ono of
its defined settings.
2024-04-28 16:52:33 +02:00
Cooldude2606 c37fe0f438 Merge pull request #613 from Danielv123/Fix-patch-with-missing-file
Correctly handle module files missing from a previously patched save
2024-04-27 22:12:35 +01:00
Danielv123 8831b33c01 Filter out missing files on load 2024-04-27 22:09:16 +02:00
Hornwitser 56124574fb Add changelog for #614 2024-04-27 19:58:00 +02:00
Hornwitser 9b416583f0 Add changelog for #570 2024-04-27 19:57:28 +02:00
Hornwitser b885e37b5b Add changelog for #606 2024-04-27 19:39:57 +02:00
Hornwitser 7e7e763517 Add changelog for #600 2024-04-27 19:38:21 +02:00
Daniel 811c4f7127 Use shell:true when running non-user input .cmd files on windows (#614)
Required after CVE fix: https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2/
Resolves #612
2024-04-23 22:19:30 +01:00
Danielv123 f0fb471ea1 Correctly handle module files missing from a previously patched save
Resolves #611
2024-04-23 21:57:55 +02:00
Hornwitser 79c1a2dc99 Fix plugin hooks called before host removal (#607)
Due a bad order of operations when a host connection was closed the
event handler listening on "close" to remove the host from the
hostConnections mapping was added after the HostConnection's event
handler listening on "close" and invoking plugin hooks in response to
the host going offline.  This in turn causes plugins that send
broadcasts in repsonse to thos hooks to throw an error due to the
broadcast sending code assuming connections that exist are valid target
to send messages to.

Fix by ordering the listener for the removal of the host connection
before the listener invoking plugin events.
2024-03-10 00:43:38 +01:00
Cooldude2606 a8ce8e7cc0 Merge pull request #570 from Cooldude2606/feature/plugin-template
Add command to create a new plugin from a template
2024-03-08 23:05:40 +00:00
Cooldude2606 50687515fa Merge branch 'feature/plugin-template' of https://github.com/Cooldude2606/clusterio into feature/plugin-template 2024-03-08 22:00:55 +00:00
Cooldude2606 6f81216e7c Addressed remaining review comments 2024-03-08 22:00:38 +00:00
Cooldude2606andHornwitser 3423e0a30c Apply suggestions from code review
- Copy paste errors.
- Incorrect capitalisation.
- Incorrect exports.
- Missed find and replace.

Co-authored-by: Hornwitser <github@hornwitser.no>
2024-03-08 20:08:16 +00:00
Cooldude2606 8fbdb5dfab Merge branch 'master' into feature/plugin-template 2024-03-06 20:12:08 +00:00
Hornwitser 7625fd82e3 Cache ModInfo to reduce startup time (#606)
Store and use a cache of the loaded ModInfo data to mod-info-cache.json
in the mods directory during startup.  For simplicity this does not
cache mods added to the controller or host after startup, these get
added to the cache on the next startup.
2024-03-06 17:30:45 +00:00
Cooldude2606 5dcc66034b Fix possible undefined title error (#605) 2024-03-06 18:11:45 +01:00
Cooldude2606 a66b1af454 Extra touch ups 2024-03-05 00:56:01 +00:00
Cooldude2606 3adfcf0a29 Updated web content permission 2024-02-28 16:36:35 +00:00
Cooldude2606andHornwitser 9a4241e4cd Update flags for output file
Co-authored-by: Hornwitser <github@hornwitser.no>
2024-02-28 16:26:53 +00:00
Cooldude2606 8dcb2197d6 Use try catch for output file existing 2024-02-28 15:25:08 +00:00
Cooldude2606 a168f7db5e Add call to action for config desc 2024-02-27 23:57:13 +00:00
Cooldude2606 283aba70df Update descriptions 2024-02-27 23:53:20 +00:00
Cooldude2606 62abfa903b Change to new preprocessor style 2024-02-27 23:07:38 +00:00
Danielv123 b7fec4c700 Set codecov to informational 2024-02-27 12:09:15 -06:00
Cooldude2606 ec19d5456e Merge branch 'master' into feature/plugin-template 2024-02-27 13:07:08 +00:00
Danielv123 e5d82dcfde Don't fail CI due to project level codecov regressions 2024-02-26 22:34:41 -06:00
Danielv123 b70feb37f2 Show table overlay when dragging and dropping saves/mods 2024-02-26 22:04:37 -06:00
Danielv123 ebf6536374 Change style of mod upload button on drag and drop 2024-02-26 22:03:47 -06:00
Daniel a68bb9c0a8 Increase libLink event emitter leak warning limit (#600) 2024-02-25 23:50:14 +01:00
Hornwitser 2075814ed1 Add changelog for #598 2024-02-23 18:09:09 +01:00
Hornwitser f07ee22969 Add changelog for #556 2024-02-23 18:06:02 +01:00
Cooldude2606 a70490648f Fix total db size calculation for inv sync (#598) 2024-02-23 16:53:15 +00:00
Hornwitser e3abf8ba33 Merge pull request #556 from Cooldude2606/feature/http-logging
Add logging for express requests and errors
2024-02-23 17:35:06 +01:00
Cooldude2606 46309ce5b8 Lint Fixes 2024-02-18 17:58:16 +00:00
Cooldude2606 f392b258f7 Add subscriptions and luacheck 2024-02-17 03:11:54 +00:00
Cooldude2606 1afee2ede5 Merge branch 'master' into feature/http-logging 2024-02-14 22:46:19 +00:00
Cooldude2606 0b2fcdcec4 Merged redundant middleware 2024-02-14 22:37:48 +00:00
DanielandHornwitser 735faf4b2c Apply suggestions from review
Co-authored-by: Hornwitser <github@hornwitser.no>
2024-02-14 09:26:13 +01:00
Danielv123 d2775e4db9 Update message section of writing-plugins document 2024-02-14 09:26:13 +01:00
Hornwitser b3fc278b2c Add template to CHANGELOG.md
Document the format and expected sections of entries as a template in
the changelog so that it's easier to add new log entries.
2024-02-12 18:21:46 +01:00
Cooldude2606 58231cc02c Add IPC to templates and handle helper function server 2024-01-24 00:40:22 +00:00
Cooldude2606 2916dcc58c Fix hang from rl.close on windows 2024-01-23 02:47:10 +00:00
Cooldude2606 b9a2d6b7bd Add current folder as default for plugin name 2024-01-23 01:00:04 +00:00
Cooldude2606 224a451bbd Fix lint for jsx template 2024-01-23 00:34:46 +00:00
Cooldude2606 b222f6dcb1 Update plugin creation docs 2024-01-23 00:24:54 +00:00
Cooldude2606 9b0293501f Add js templates and config templates 2024-01-23 00:14:08 +00:00
Cooldude2606 de00a043c3 Update lint rules for templates 2024-01-22 19:09:48 +00:00
Cooldude2606 396e2c59c7 Revert accidental edit to vscode settings 2024-01-22 18:55:34 +00:00
Cooldude2606 c3b2c846b7 Add package.json template 2024-01-22 18:42:37 +00:00
Cooldude2606 be8bba8ff8 Complete template files 2024-01-22 05:01:09 +00:00
Cooldude2606 c81d2b61a6 Add template copying 2024-01-22 02:47:34 +00:00
Cooldude2606 534dc14221 Add create command mode/args for plugin templates 2024-01-21 23:36:27 +00:00
Cooldude2606 fcf174376d Clean up the request correctly 2024-01-18 18:50:31 +00:00
Cooldude2606 0e68596fbd Remove insecure and bloated module 2024-01-18 18:50:30 +00:00
Cooldude2606 9bcbc1e7f3 Add http logging for express 2024-01-18 18:50:29 +00:00
265 changed files with 17551 additions and 2596 deletions

No files matched your search

+4
View File
@@ -0,0 +1,4 @@
{
"image":"mcr.microsoft.com/devcontainers/universal:2",
"postCreateCommand": "mkdir factorio && cd factorio && wget https://factorio.com/get-download/1.1.107/headless/linux64 && tar -xf linux64 && mv factorio factorio_1.1.107 && rm linux64 && wget https://factorio.com/get-download/2.0.47/headless/linux64 && tar -xf linux64 && mv factorio factorio_2.0.47 && rm linux64 && pnpm install",
}
+17 -1
View File
@@ -23,7 +23,9 @@ module.exports = {
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/triple-slash-reference": "off",
"@typescript-eslint/no-shadow": ["error", { "allow": ["Event", "Request", "yargs"] }],
"prefer-const": "off",
"no-shadow": "off",
},
},
{
@@ -44,6 +46,7 @@ module.exports = {
"files": [
"packages/web_ui/src/**/*.ts?(x)",
"packages/lib/browser.ts",
"packages/create/templates/*/web/**/*.{t,j}s?(x)",
"{packages,plugins}/*/web/**/*.ts?(x)",
],
"env": {
@@ -94,6 +97,19 @@ module.exports = {
"node/shebang": "off",
},
},
{
"files": [
"packages/create/templates/**/*.{t,j}s?(x)",
],
"rules": {
"node/no-missing-require": "off",
"node/no-missing-import": "off",
"spaced-comment": "off",
"comma-spacing": "off",
"max-len": "off",
"indent": "off",
},
},
],
"rules": {
@@ -175,7 +191,7 @@ module.exports = {
"multiline-comment-style": ["error", "separate-lines"],
"multiline-ternary": "off",
"new-cap": ["error", {
"capIsNewExceptions": ["StringEnum"],
"capIsNewExceptions": ["StringEnum", "StringKey"],
"capIsNewExceptionPattern": "^Type\\.",
}],
"new-parens": "error",
+17
View File
@@ -0,0 +1,17 @@
Describe why and what has been changed by this pullrequest, feel free to also ask for input on decisions you are unsure about etc.
The changelog lines below will be copied directly from merged pull requests to the changelog on new releases. Changes are expected to preferrentially link to the associated issues. If there are no relevant issues, link to the pull request itself.
## Changelog
```
### Features
- Added doodad to web interface. [#999](https://github.com/clusterio/clusterio/issues/999)
### Changes
- ...
### Fixes
- ...
### Breaking changes
- ...
```
+40 -15
View File
@@ -12,41 +12,66 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18.x, 20.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
# Supported versions are active lts and maintenance see https://nodejs.org/en/about/releases/
node-version: [20.x, 22.x]
# Supported versions are latest stable and one minor version before stable (even if from a previous major)
factorio-version: [1.1.110, 2.0.47]
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2.4.0
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 8
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- run: pnpm i --no-optional --no-frozen-lockfile
- run: wget -q -O factorio.tar.gz https://www.factorio.com/get-download/latest/headless/linux64 && tar -xf factorio.tar.gz && rm factorio.tar.gz
- name: Prepare and install
run: pnpm i --no-frozen-lockfile
- name: Use Factorio ${{ matrix.factorio-version }}
run: wget -q -O factorio.tar.gz https://www.factorio.com/get-download/${{ matrix.factorio-version }}/headless/linux64 && tar -xf factorio.tar.gz && rm factorio.tar.gz
- name: Run tests
if: ${{ matrix.node-version != '18.x' }}
if: ${{ matrix.node-version != '20.x' || matrix.factorio-version != '1.1.110' }}
run: pnpm test
- name: Run coverage
if: ${{ matrix.node-version == '18.x' }}
if: ${{ matrix.node-version == '20.x' && matrix.factorio-version == '1.1.110' }}
run: pnpm run ci-cover
- name : Upload coverage to Codecov
if: ${{ matrix.node-version == '18.x' }}
uses: codecov/codecov-action@v2
if: ${{ matrix.node-version == '20.x' && matrix.factorio-version == '1.1.110' }}
uses: codecov/codecov-action@v4
with:
files: ./coverage/lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: pnpm/action-setup@v2.4.0
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 8
- uses: actions/setup-node@v3
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: pnpm i --no-optional --no-frozen-lockfile
- run: pnpm i --no-frozen-lockfile
- run: pnpm run lint
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 8
- uses: actions/setup-node@v4
with:
node-version: 18.x
- run: pnpm i --no-frozen-lockfile
- run: pnpm run build-mod
# Upload build to artifacts
- name: Upload build to artifacts
uses: actions/upload-artifact@v4
with:
name: clusterio_lib
path: ./dist
if-no-files-found: error
+3
View File
@@ -31,6 +31,9 @@ node_modules
Factorio*
/factorio
!/test/file/factorio
!/test/lib/factorio
!/packages/lib/src/factorio
/instances
/mods
/database
+220
View File
@@ -1,5 +1,225 @@
# Changelog
<!-- Template for entries. Add sections that apply as changes are made.
## Version 2.0.0-alpha.xx
### Major Features
- Major additions and/or major improvements to managing clusters.
### Features
- New features added.
### Changes
- Small changes to existing behaviour.
### Fixes
- Bugfixes
### Breaking Changes
- Changes which break backwards compatibility with older hosts or plugins code.
### Meta
- Changes to the project activities / structure worth highlighting but have no impact on the published package.
Many thanks to the following for contributing to this release:
[@username](https://github.com/username)
-->
## Version 2.0.0-alpha.21
### Major Features
- Added import, export, and restore of user lists (bans / admins / whitelist / combined). [#717](https://github.com/clusterio/clusterio/issues/717)
- Added remote update for core packages and third party plugins. [#761](https://github.com/clusterio/clusterio/issues/761)
- Added config cloning on instance creation. [#720](https://github.com/clusterio/clusterio/issues/720)
### Features
- Added social links and instructions to error page. [#742](https://github.com/clusterio/clusterio/issues/742)
- Added button to download control config. [#740](https://github.com/clusterio/clusterio/issues/740)
- Added server action filter (aka "chat filter") to instance log in web ui. [#744](https://github.com/clusterio/clusterio/issues/744)
- Added `factorio.console_logging` to enable dedicated console log file for servers. [#748](https://github.com/clusterio/clusterio/issues/748)
- Added a recovery mode to host and controller accessible as an option on the run command. [#752](https://github.com/clusterio/clusterio/issues/752)
- Added username search to the users list. [#751](https://github.com/clusterio/clusterio/issues/751)
- Added log level filtering to the web ui. [#745](https://github.com/clusterio/clusterio/issues/745)
- Added pending update detection for host and controller. [#758](https://github.com/clusterio/clusterio/issues/758)
- Added restart required indicator for host and controller. [#758](https://github.com/clusterio/clusterio/issues/758)
- Added version mismatch indicator in host list. [#758](https://github.com/clusterio/clusterio/issues/758)
- Added host plugin list command to crl where previously only controller plugin list existed. [#760](https://github.com/clusterio/clusterio/issues/760)
- Added optional feature which allows new plugins to be installed remotely.
- Added bidirectional whitelist syncing. [#716](https://github.com/clusterio/clusterio/issues/716)
- Added support for v2 map exchange strings. [#700](https://github.com/clusterio/clusterio/issues/700)
### Fixes
- Fixed handling of zip files with multiple top-level folders. [#739](https://github.com/clusterio/clusterio/issues/739)
- Fixed looping between instances of ban and admin sync events. [#736](https://github.com/clusterio/clusterio/issues/736)
- Fixed quality being stripped from power armour equipment when moving between servers [#753](https://github.com/clusterio/clusterio/pull/753)
- Resolved webpack type export warnings [#750](https://github.com/clusterio/clusterio/pull/750)
- Reduce page load time on users list by enabling pagination. [#718](https://github.com/clusterio/clusterio/issues/718)
- Corrected stack trace and enabled source mapping during bootstrap. [#764](https://github.com/clusterio/clusterio/pull/764)
- Fixed map gen settings using the incorrect name for cliff settings following exchange string decode.
### Meta
- Silent testing is now truly silent. [#764](https://github.com/clusterio/clusterio/pull/764)
- Node 18 has reached end of life and is no longer maintained, we no longer guarantee support for it.
- CI testing now runs on node 20 & 22 as well as factorio 1.1.110 & 2.0.47.
Many thanks to the following for contributing to this release:
[@Cooldude2606](https://github.com/Cooldude2606)
[@Danielv123](https://github.com/Danielv123)
[@Hornwitser](https://github.com/Hornwitser)
## Version 2.0.0-alpha.20
### Features
- Update subspace storage to 2.0 and space age. [#733](https://github.com/clusterio/clusterio/pull/733)
- A fresh install of Clusterio will include default mod packs. [#719](https://github.com/clusterio/clusterio/issues/719)
### Fixes
- Fix crash when leaving a server without a logistic requester point [#731](https://github.com/clusterio/clusterio/pull/731)
- Use floor instead of round to prevent "1 minute and 60 seconds" being shown [#723](https://github.com/clusterio/clusterio/issues/723)
- Fixed production statistics overwriting consumption statistics and vise versa [#734](https://github.com/clusterio/clusterio/pulls/734)
- Hot patch to add the updated 2.0.29 control.lua freeplay hash to know scenarios. [commit](https://github.com/clusterio/clusterio/commit/26269df7d4d8f39042c59653008bc8f034a4f3b4)
### Meta
- We have now introduced a new [decisions log](https://github.com/clusterio/clusterio/blob/master/docs/decisions.md) to aid tracking all the technical and design decisions we make and their reasons. [#714](https://github.com/clusterio/clusterio/pull/714)
Many thanks to the following for contributing to this release:
[@Cooldude2606](https://github.com/Cooldude2606)
[@Danielv123](https://github.com/Danielv123)
[@psihius](https://github.com/psihius)
## Version 2.0.0-alpha.19
### Major Features
- Added support for 2.0 builtin mods during mod pack creation. [#684](https://github.com/clusterio/clusterio/pull/684)
- Added basic 2.0 support to clusterio lua api and 1st party plugins. [#685](https://github.com/clusterio/clusterio/pull/685)
### Features
- Added Datastore and Datastore Provider classes to the library to support savable Maps. [#629](https://github.com/clusterio/clusterio/pull/629)
- Added clean script to speed up publishing and debugging of build steps. [#640](https://github.com/clusterio/clusterio/pull/640)
- Added sub-tile option on plugin page headers. [#642](https://github.com/clusterio/clusterio/pull/642)
- Greatly improved plugin and module template generation. [#641](https://github.com/clusterio/clusterio/pull/641)
- Duplicate host connections from the same address will kill the older host process. [#647](https://github.com/clusterio/clusterio/pull/647)
- Controller will refuse duplicate host connections from different addresses. [#647](https://github.com/clusterio/clusterio/pull/647)
- Monorepos are now supported for external plugins. [#654](https://github.com/clusterio/clusterio/pull/654)
- Changed default role selection to be a dropdown menu rather than id. [#664](https://github.com/clusterio/clusterio/pull/664)
- Added credential option to config entries to allow writing sensitive data that can't be read back remotely.
- Allow hidden and readonly graphical representations of config values. [#665](https://github.com/clusterio/clusterio/pull/665)
- Added locale exporting for all builtin mods, not just for the base mod. [#684](https://github.com/clusterio/clusterio/pull/684)
- Added option for silent testing which only shows the results, can be ran using `pnpm silent-test` [#680](https://github.com/clusterio/clusterio/pull/680)
- Added prompt for http port on installation of Clusterio. [#564](https://github.com/clusterio/clusterio/issues/564)
- Added drag and drop overlay to upload button on mods page and save table on instance page. [#591](https://github.com/clusterio/clusterio/issues/591)
- Automatically add plugins from ./plugins, ./external_plugins and installed npm modules. [#692](https://github.com/clusterio/clusterio/pull/692)
- `compat.table_to_json` and `compat.json_to_table` supported in all stages in pre 2.0 [#687](https://github.com/clusterio/clusterio/issues/687)
- Add "connect to server" button to web interface, launches with steam api. [#608](https://github.com/clusterio/clusterio/issues/608)
- Add space platform name mapping to space platform statistics. [#706](https://github.com/clusterio/clusterio/pull/706)
- Track space platform speed metric clusterio_statistics_exporter_platform_speed. [#706](https://github.com/clusterio/clusterio/pull/706)
- Track space platform weight metric clusterio_statistics_exporter_platform_weight. [#706](https://github.com/clusterio/clusterio/pull/706)
- Added bidirectional sync to admin and ban list. [#709](https://github.com/clusterio/clusterio/pull/709)
### Fixes
- Fixed IPC errors reporting undefined when no stack trace was present. [#624](https://github.com/clusterio/clusterio/pull/624)
- Fixed loopback routing for instances reporting the wrong error message. [#625](https://github.com/clusterio/clusterio/pull/625)
- Fixed docs linking to non-existing js files after ts migration. [#633](https://github.com/clusterio/clusterio/pull/633)
- Fixed CI workflows using deprecated version of nodejs. [#637](https://github.com/clusterio/clusterio/pull/637)
- Fixed linux only tests to run on windows. [#661](https://github.com/clusterio/clusterio/pull/661)
- Fixed log events being sent from a host over an invalid websocket. [#639](https://github.com/clusterio/clusterio/pull/639)
- Fixed spaces passed in arguments to the installer causing it to break. [#620](https://github.com/clusterio/clusterio/issues/620)
- Fixed numeric admin name breaking login. [#536](https://github.com/clusterio/clusterio/issues/536)
- Fixed issues with subscriptions which prevented them from working on instances or generic events. [#655](https://github.com/clusterio/clusterio/pull/655).
- Fixed unreliable datastore tests. [#662](https://github.com/clusterio/clusterio/pull/662).
- Fixed 2.0 version extraction from linux headless. downloaded during installation of clusterio. [#671](https://github.com/clusterio/clusterio/pull/671)
- Fixed invalid transient state during server start. [#676](https://github.com/clusterio/clusterio/issues/676)
- Fixed host user sync to instances after reconnecting to the. controller. [#678](https://github.com/clusterio/clusterio/pull/678)
- Fixed WebUI adding and displaying the wrong base mod version. [#684](https://github.com/clusterio/clusterio/pull/684)
- Fixed WebUI inconsistent pattern matching for factorio version. [#684](https://github.com/clusterio/clusterio/pull/684)
- Fixed `safeOutputFile` not being atomic because of a missing path separator. [#659](https://github.com/clusterio/clusterio/pull/659)
- Fixed built-in mods showing as missing in the WebUI. [#581](https://github.com/clusterio/clusterio/issues/581)
- Fixed dirty flag not being set when only the config of an instance is updated. [#675](https://github.com/clusterio/clusterio/pull/675)
- Fixed incorrect command being used to remove players from the whitelist. [#679](https://github.com/clusterio/clusterio/pull/679)
- Fixed CI following undocumented breaking change from downstream dependency. [#683](https://github.com/clusterio/clusterio/pull/683)
- Fixed instance crash when missing saves folder. [#698](https://github.com/clusterio/clusterio/issues/698)
- Fixed statistics exporter not working on newer versions of windows 11 due to wmic deprecation. [#707](https://github.com/clusterio/clusterio/issues/707)
### Changes
- Added prompt for the HTTP port to host the controller to the installer. [#619](https://github.com/clusterio/clusterio/pull/619)
- Added tests for sendEvent and sendRequest throwing the correct error messages. [#638](https://github.com/clusterio/clusterio/pull/638)
- Updated all web ui pages to use correct page header component. [#643](https://github.com/clusterio/clusterio/pull/643)
- Bumped Typescript version to 5.5 and implemented `${configDir}` in base configs for "outDir". [#648](https://github.com/clusterio/clusterio/pull/648).
- Added factorio.executable_path option which allows overriding the default path to the Factorio executable run.
- Added instance version display. [#573](https://github.com/clusterio/clusterio/pull/573)
- Added `factorio.shutdown_timeout` config to set the time the host will wait for a Factorio server to shut down befor killing it.
- Changed shutdown logic to prefer sending a /quit command via RCON instead of using diverging logic on Windows and Linux.
- Added `controller.factorio_username` and `controller.factorio_token` config to set Factorio credentials used for the whole cluster.
- Added `controller.share_factorio_credential_with_hosts` config to optionally require hosts to provide their own credentials.
- Added `host.factorio_username` and `host.factorio_token` config to set Factorio credentials used on a given host.
- Changed `instance.rcon_password` config to be a credential field that can not be read by the control interface.
- Changed `host.id` `instance.id` and `instance.assign_host` config to be hidden fields that can not be seen on the web ui.
- Updated display name and description of `controller.external_address` to avoid confusion. [#674](https://github.com/clusterio/clusterio/pull/674)
- Renamed config value `controller.external_address` to `controller.public_url`, migrations are applied automatically.
- String config values with whitelisted values (enum) now display as a selection dropdown. [#701](https://github.com/clusterio/clusterio/pull/701)
- Existing selection dropdowns now support search filtering. [#701](https://github.com/clusterio/clusterio/pull/701)
- Added surface label to clusterio_statistics_exporter_instance_force_flow_statistics. [#691](https://github.com/clusterio/clusterio/issues/691)
- Compat lib now supports versions from 0.17 to 2.0 [#711](https://github.com/clusterio/clusterio/pull/711)
- Add bidirectional sync to admin and ban list. [#709](https://github.com/clusterio/clusterio/pull/709)
### Breaking Changes
- Previously silent errors for controller.sendEvent now throw exceptions [#625](https://github.com/clusterio/clusterio/pull/625).
- @clusterio/controller export InstanceInfo has added factorioVersion parameter.
- Argument order for `Config.canAccess` changed to require an access mode passed as the second argument.
- Instances can now have `factorio.enable_script_commands` disabled which will throw an error when any script command is used over rcon. [#681](https://github.com/clusterio/clusterio/pull/681).
- User IDs are now case insensitive, duplicate users will be automatically merged with a backup created. [#682](https://github.com/clusterio/clusterio/pull/682)
- Plugins now require the keyword "clusterio-plugin" in package.json to be automatically loaded [#692](https://github.com/clusterio/clusterio/pull/692)
Many thanks to the following for contributing to this release:
[@CCpersonguy](https://github.com/CCpersonguy)
[@Cooldude2606](https://github.com/Cooldude2606)
[@Danielv123](https://github.com/Danielv123)
[@Hornwitser](https://github.com/Hornwitser)
[@Laar](https://github.com/Laar)
[@psihius](https://github.com/psihius)
## Version 2.0.0-alpha.18
### Features
- Added logging of HTTP requests and errors on the controller [#556](https://github.com/clusterio/clusterio/pull/556).
- Added caching of data parsed from mods to speed up startup time [#606](https://github.com/clusterio/clusterio/pull/606).
- Added command to installer to generate the code for making a new plugin [#570](https://github.com/clusterio/clusterio/pull/570).
### Changes
- Fixed Inventory Sync reporting a negative database size [#598](https://github.com/clusterio/clusterio/pull/598).
- Fixed plugin events being invoked while a host connection was in an invalid state.
- Suppresed bogus warning about event listener leak in the browser's console [#600](https://github.com/clusterio/clusterio/pull/600).
- Fixed installer breaking on Windows after Node.js released a security fix [#614](https://github.com/clusterio/clusterio/pull/614).
- During a data export the mod settings in mod packs will now be corrected to the type of the settings prototype if the type is incorrect.
- Fixed missing color setting causing the Mod Pack view in the Web UI to show an error [#609](https://github.com/clusterio/clusterio/issues/609).
Many thanks to the following for contributing to this release:
[@Cooldude2606](https://github.com/Cooldude2606)
[@Danielv123](https://github.com/Danielv123)
[@Hornwitser](https://github.com/Hornwitser)
## Version 2.0.0-alpha.17
### Features
+14 -9
View File
@@ -90,13 +90,13 @@ Check out the documentation on [Writing Plugins](/docs/writing-plugins.md) for w
## Installation
Clusterio runs on Node.js v18 and up, it's distributed via npm and comes with a guided install script.
Clusterio runs on all LTS version of NodeJS, it is distributed via npm and comes with a guided install script.
If you already have a recent Node.js installed, you can set it up in a new directory with:
npm init "@clusterio"
Otherwise see below for OS specific instructions.
Otherwise see below for OS specific instructions.
Check out all our of decisions for [node, factorio, and operation support](/docs/decisions.md#supportability).
### Ubuntu setup
@@ -198,7 +198,7 @@ For the standalone installation mode you can use
If you plan to make your cluster available externally set the address
that it will be accessible under with, for example
npx clusteriocontroller config set controller.external_address http://203.0.113.4:1234/
npx clusteriocontroller config set controller.public_url http://203.0.113.4:1234/
Change the url to reflect the IP, protocol, and port the controller is accessible under, dns names are also supported.
If you're planning on making the controller accessible on the internet it's recommended to set up TLS, see the [Setting Up TLS](/docs/setting-up-tls.md) document for more details.
@@ -209,12 +209,17 @@ See the [readme for @clusterio/controller](/packages/controller/README.md) for m
## Managing Factorio mods
Clusterio currently only provides a manual method for running mods on servers by copying them either to each instance's individual `mods` folder, or by copying them into a host installation's `sharedMods` folder which will link/copy them to the `mods` folder of each instance on that host if the file doesn't already exist in the `mods` folder for that instance.
Mods are copied from `sharedMods` on Windows and symlinked on Linux, this means that when mods are removed from `sharedMods` the mods will also be removed from the instance's `mods` folder on Linux if it got there by being linked by Clusterio, but not on Windows.
Mods in clusterio are managed as parts of modpacks. There is currently no mod portal integration, so you first need to navigate to the mods section of the web UI and upload your mods. You can then create a modpack from the uploaded mods by navigating to the mods page, hitting `create`. Once you have a modpack, you can assign it to an instance under Instance config -> Factorio -> Mod pack. You can also set a default modpack for new instances in the controller settings.
For managing the mod settings, the `mod-settings.dat` and `mod-list.json` files will also be linked/copied over if present in `sharedMods`, but since Factorio creates these files automatically it might be necessary to delete them from each instance's `mods` folder for changes to them in `sharedMods` to take effect.
![Select mod pack for instance](docs/assets/config_select_mod_pack.png)
Better remote management of mods is a planned feature.
Mod settings can be managed under the modpack configuration page in the web UI. For mod settings to show, you first need to run an export. This is done by creating an instance with the modpack assigned, opening the instance page and pressing More -> Export data.
![Export mod configuration data](docs/assets/export_mod_config_data.png)
Once this is done, the mod settings will show up under the modpack configuration page.
![Mod settings configuration in web interface](docs/assets/mod_settings_configuration.png)
## Setting up shared storage
@@ -231,7 +236,7 @@ After completing the installation start up the controller and at least one host
The installer provides the `run-controller` and `run-host` scripts to make this simple.
Once the controller process is running you can log into the web interface which is hosted by default on http://localhost:8080/ (adjust the port number if you changed it), use the admin authentication token provided from the installation to log in.
The basics of setting up a Factorio server from the web interface is to create an instance, assign it to a host and then click start.
The basics of setting up a Factorio server from the web interface is to create an instance, assign it to a host and then click start. You can also use the "Connect via Steam" button to launch Factorio directly and connect to the server using Steam's protocol handler.
### Running via Systemd
+116
View File
@@ -0,0 +1,116 @@
/**
* Clean the repository of build and execution artifacts.
*
* clean: A quick clean that removes all build artifacts making the output safe to publish
* clean-tests: Removes all build and test artifacts should not be required if tests are setup correctly.
* clean-all: Removes all build, test, and execution artifacts. Including cluster config and logs!
*/
"use strict";
const path = require("path");
const fs = require("fs-extra");
const yargs = require("yargs");
const npmPackage = require("./package.json");
const workspaces = npmPackage.workspaces
.map(p => p.slice(0, -2))
.filter(p => !p.includes("external_plugins"));
let DRY = false;
/**
* Attempt to remove a file / directory if it exists.
*
* @param {string} filePath The path to remove.
*/
async function tryRemove(filePath) {
if (await fs.exists(filePath)) {
// eslint-disable-next-line no-console
console.log(filePath);
}
if (!DRY) {
await fs.remove(filePath);
}
}
/**
* Removes all build artifacts
*/
async function removeBuildArtifacts() {
const tasks = [];
for (const workspace of workspaces) {
const basePath = path.resolve(workspace);
const packages = await fs.readdir(basePath);
for (const packageName of packages) {
tasks.push(
tryRemove(path.join(basePath, packageName, "dist"))
);
}
}
await Promise.all(tasks);
}
/**
* Removes all test artifacts
*/
async function removeTestArtifacts() {
const artifacts = [
".nyc_output", "coverage", "temp", "static",
];
await Promise.all(artifacts.map(artifact => tryRemove(path.resolve(artifact))));
}
/**
* Removes all execution artifacts
*/
async function removeExecutionArtifacts() {
const artifacts = [
"database", "instances", "logs", "mods",
"config-control.json", "config-controller.json", "config-host.json",
"FactorioAdminToken.txt", "plugin-list.json",
];
await Promise.all(artifacts.map(artifact => tryRemove(path.resolve(artifact))));
}
/**
* Main function for this script
*/
async function main() {
await yargs
.scriptName("clean")
.usage("$0 <command> [options]")
.option("dry", {
nargs: 0,
describe: "Will not delete any files",
default: false,
type: "boolean",
})
.command("all", "Remove all artifacts", () => {}, argv => {
DRY = argv.dry;
removeBuildArtifacts();
removeTestArtifacts();
removeExecutionArtifacts();
})
.command("tests", "Remove all build and test artifacts", () => {}, argv => {
DRY = argv.dry;
removeBuildArtifacts();
removeTestArtifacts();
})
.command("fast", "Remove all build artifacts", () => {}, argv => {
DRY = argv.dry;
removeBuildArtifacts();
})
.demandCommand(1, "You need to specify a command to run")
.strict()
.parse();
}
// Run main if started from command line
if (module === require.main) {
main();
}
+6
View File
@@ -0,0 +1,6 @@
coverage:
status:
project:
default:
if_ci_failed: success
informational: true
Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 128 KiB

+1 -1
View File
@@ -30,7 +30,7 @@ There's also the virtual connector which lets two links in the same program be c
## Message
There's two types of messages currently implemented apart from the Link builtin close message and that's Request and Event.
The builtin messages are defined in [packages/lib/link/messages.js](/packages/lib/link/messages.js) and are attached to links via the attachAllMessages function, which is usally called in the Link subclass constructor.
The builtin messages are defined in [packages/lib/src/link/messages.ts](/packages/lib/src/link/messages.ts) and are attached to links via the attachAllMessages function, which is usally called in the Link subclass constructor.
The messages define which links they are valid on and only attaches handlers for those links.
There's also a forwarding mechanism for when a request or event is to be forwarded another hop.
+1 -1
View File
@@ -6,7 +6,7 @@ Clusterio uses a field based configuration system to manage settings for the con
Plugins can add their own config fields for the controller and instances configs.
The fields are defined early in the startup, and fields for disabled plugins will still be created.
The built-in config fields are defined in [packages/lib/config/definitions.js](/packages/lib/config/definitions.js) while plugins can add their own config field definitions to the `controllerConfigFields` and `instanceConfigFields` properties of the `plugin` export.
The built-in config fields are defined in [packages/lib/src/config/definitions.ts](/packages/lib/src/config/definitions.ts) while plugins can add their own config field definitions to the `controllerConfigFields` and `instanceConfigFields` properties of the `plugin` export.
A `<plugin_name>.load_plugin` field is automatically created for each plugin and it's used to enable/disable the loading plugins on startup.
+2 -2
View File
@@ -51,9 +51,9 @@ interfaces.
Defaults to null.
### controller.external_address
### controller.public_url
External address the controller is accessible on.
Public address the controller is accessible on.
Currently only used for `clusteriocontroller bootstrap create-ctl-config` in order to give the right url to connect to.
This should be a full URL ending with a /.
+11 -41
View File
@@ -3,6 +3,8 @@
You are probably reading this because you want to contribute code to Clusterio.
Great! We need people like you.
Though we have some conventions and workflows that we kindly ask you to follow.
We have included all the pratical considerations in this document.
Although, we recomend checking out out our [past decisions](/docs/decisions.md) if you have the time.
## Contents
@@ -15,6 +17,7 @@ Though we have some conventions and workflows that we kindly ask you to follow.
- [Editing your Feature Branch](#editing-your-feature-branch)
- [Resolving Merge Conflicts](#resolving-merge-conflicts)
- [Updating Your GitHub Fork](#updating-your-github-fork)
- [Publishing releases to npm](#publishing-releases-to-npm)
- [Changelog](#changelog)
- [Supported Node.js and Factorio Version](#supported-node.js-and-factorio-version)
- [Code Style](#code-style)
@@ -23,7 +26,6 @@ Though we have some conventions and workflows that we kindly ask you to follow.
- [Indenting](#indenting)
- [Line Length](#line-length)
- [Markdown](#markdown)
- [Pushing releases to npm](#pushing-releases-to-npm)
## Workflow
@@ -178,11 +180,15 @@ Fetch the main repository directly as described above and base your feature bran
GitHub has no interface or functionality for keeping the branches in your fork up to date with the branches in the main repository and there is no point in doing this for our feature branch based workflow.
### Publishing releases to npm
For publishing new releases see [package release workflow](./release-workflow.md#packages).
## Changelog
There's a [Changelog](../CHANGELOG.md) in the root of the project.
If you make changes that are visible to users of this project you should add an entry to the changelog describing what has changed.
The format of this log should be self explanatory, please follow it carefully.
The format of this log should be self explanatory, please follow it carefully. Please make sure to document any [breaking changes](/docs/decisions.md#breaking-changes).
## Supported Node.js and Factorio Versions
@@ -194,12 +200,14 @@ For Factorio Clusterio 2.0 aims to support version 0.17.69 and up, including the
It's recommended that you use the lua API reference for 0.17.69, as there's no information on what version Factorio API's were introduced in.
The GitHub Actions tests are run against the latest experimental release.
Check out all our of decisions for [node, factorio, and operation support](/docs/decisions.md#supportability).
## Code Style
The style of the code is a bit of a mixed bag at the moment.
But there are at least a few things that have been agreed upon.
See our [decisions document](/docs/decisions.md) for justifcations.
### Naming style
@@ -284,41 +292,3 @@ And there's two spaces before the next heading.
Headings use the `# heading` format.
```
## Pushing releases to npm
The releases are done by updating packages versions and then publishing them with pnpm.
You will need commit access to the repository as well publish access to the @clusterio org on npm to do this.
1. To do a release switch to the master branch and make sure it's up to date with the repository before proceeding.
```sh
git switch master
git pull --ff-only upstream master
```
Check and make sure there are no untracked files with `git status`.
2. Update the version in package files.
Make sure to update the command for the right version jump.
```sh
sed -i 's/2\.0\.0-alpha\.12/2\.0\.0-alpha\.13/' {packages,plugins}/*/package.json
```
3. Inspect, commit and tag the changes.
Again, make sure to be tagging the right version.
```sh
git add -p
git commit -m v2.0.0-alpha.13
git tag v2.0.0-alpha.13
```
4. Publish the packages.
```sh
rm -r ./{plugins,packages}/*/dist
pnpm -r --filter='./{plugins,packages}/*' publish
```
+479
View File
@@ -0,0 +1,479 @@
# Decisions
This document outlines key decisions taken by the project team that should be followed by all maintainers and contributors.
If there is a particular issue or concern you would like the team to comment on then please create a github issue for it.
<!-- Try to use categories that already exist if possible -->
<!-- Sublists should be alphabetical, avoiding starting with "the" -->
<!-- For code style, add (Lang) to section names if present in both -->
1. Documentation
- [Decision Log](#decision-log)
- [TSDoc](#tsdoc)
2. Code Style (General)
- [Indenting](#indenting)
- [Line Length](#line-length)
- [Naming Booleans](#naming-booleans)
- [Naming Files](#naming-files)
- [Naming Times and Durations](#naming-times-and-durations)
- [Strings](#strings)
3. Code Style (TS / JS / Web)
- [ESLint](#eslint)
- [Imports Within `lib` Package](#imports-within-lib-package)
- [Name Style](#name-style-ts)
4. Code Style (Lua)
- [FMTK](#fmtk)
- [Name Style](#name-style-lua)
5. Code Style (Other)
- [Markdown](#markdown)
6. Repository Structure
- [External Plugins](#external-plugins)
- [First Party Plugins](#first-party-plugins)
- [Core Packages](#core-packages)
- [TypeScript Configs](#typescript-configs)
- [Workspace](#workspace)
7. Project Architecture
- [Deprecation of Phin in favour of `fetch`](#deprecation-of-phin-in-favour-of-fetch)
- [Minimal Dependencies](#minimal-dependencies)
- [React](#react)
- [Separation of Host and Controller](#separation-of-host-and-controller)
- [Typescript](#typescript)
- [Webpack](#webpack)
8. Supportability
- [Breaking Changes](#breaking-changes)
- [Distribution](#distribution)
- [Factorio](#factorio)
- [General Support](#general-support)
- [NodeJS](#nodejs)
- [Operating System](#operating-systems)
9. Testing
- [Continuous Integration](#continuous-integration)
- [Continuous Deployment](#continuous-deployment)
- [Mocha](#mocha)
- [Version Matrix](#version-matrix)
> [!note]
> The use of `Time Immemorial` indicates an obsverion of a previously undocumented decision with no known start date, which has remained unchallenged.
<!--
### Template
🧾 0th Month 2000 | Source
> A short sentence describing the action to take in the context of our project.
Write a breif description which includes: the reason a decision was needed, the outcome of the discussion, and the justification of the outcome.
Write using the active voice in third person "we".
Remember to have one sentence per line.
-->
## Documentation
### Decision Log
🧾 22nd December 2024 | Discord Voice Call
> Maintain a document that includes a brief description of the outcomes and the reasoning behind our decisions, along with links to the relevant sources for further reading.
We discovered that we were beginning to ask similar questions to other maintainers that we had previously raised, which led to a waste of time for all involved.
Consequently, we decided that a log of important decisions should be kept for the benefit of ourselves and future maintainers.
### TSDoc
🧾 12th August 2023 | [Discord Discussion](https://discord.com/channels/450361298220351489/450361298220351491/1320573126731370506) | [Github PR #493](https://github.com/clusterio/clusterio/pull/493)
> When a function or it's arguments do not have a clear purpose, a TSDoc comment should be used to describe it.
While TypeScript provides useful type infomation in function hover text, it does not allow for comments about the purpose of functions and their arguments.
Therefore, we decided to use TSDoc comments for this purpose as it is well intergrated in many text editors and IDEs (such as VSCode).
Our specific implementation is TypeDoc because it is lightweight it and can output TSDoc comments as html.
API Extractor was also considered but not selected.
## Code Style (General)
### Indenting
🧾 Time Immemorial
> Use tabs for indention. Except for Markdown where spaces carry meaning.
We needed to maintain a consistent whitespace style across all our files.
Therefore, we selected tabs as our standard.
### Line Length
🧾 Time Immemorial
> Lines are limited to 120 characters where tabs count as 4 characters.
We needed to maintain a consistent max line legnth across all our files.
Therefore, we selected 120 characters as our standard.
### Naming Booleans
🧾 25th November 2024 | [Discord Discussion](https://discord.com/channels/450361298220351489/450361298220351491/1310611960508977223)
> Variable and member properties which contain booleans should be named starting with a verb unless it ends with "ed", e.g. `canRestart` `expectEmpty` `hasFallback` `connected` `enabled` `loaded` `banned`
We had inconsistencies in our naming of booleans and aimed to standardise them.
There was little preference for any particular approach, so we chose the one that would require the fewest changes to existing code.
As a result, all boolean values should begin with a verb, unless they end with "ed." For clarity, both "is" and "has" are considered verbs.
### Naming Files
🧾 Time Immemorial
> Files use lowercase_underscore if multiple values are exported. If a single class is exported then it uses the class name in PascalCase.
We needed to maintain a consistent name style across all our files.
Therefore, we selected lowercase undercore for most files, with class files using PascalCase.
### Naming Times and Durations
🧾 18th January 2024 | [Discord Discussion](https://discord.com/channels/450361298220351489/450361298220351491/1197566542548828170)
> Variable and member properties which contain times or durations should end with their SI unit of messure, e.g. `updatedAtMs`
We found verifying the units of measurement used in different locations to be a simple yet repetitive task that could lead to errors.
Our main concern was the mixing of variables containing seconds and milliseconds.
Therefore, we decided that times and durations should explicitly include their units, whereas other value types do not require any units.
### Strings
🧾 Time Immemorial
> Strings should use double quotes `"` unless the string contains them, in which case single quotes `'` can be used. For JS string templates use backticks ``` ` ``` as normal.
We needed to maintain a consistent string style across all our files.
Therefore, we selected double quotes as our standard.
## Code Style (TS / JS / Web)
### ESLint
🧾 Time Immemorial
> Use ESLint for style checking.
We needed to maintain a consistent style across all our files.
Therefore, we utilise the industry-standard ESLint, as it is familiar and highly configurable.
### Imports Within `lib` Package
🧾 Time Immemorial
> When importing files within the lib package, all names should be prefixed with "lib".
The exports of the various library files are often very generic and may conflict with variable names.
Therefore, when importing these files the "lib" prefix should be used to avoid confussing.
This only applies to files within `lib` importing other files in `lib` because all other packages import the whole of `lib` as a single package.
### Name Style (TS)
🧾 Time Immemorial
> Variables and members of classes use camelCase. Classes use PascalCase. Config values use lowercase_underscore. Exceptions allowed for over the write objects.
We needed to maintain a consistent name style across all our files.
Therefore, we follow common practive for JavaScript of using camelCase and PascalCase.
Although, we have also allowed the use of lowercase_underscore for config values to distinguish them from other variables.
## Code Style (Lua)
### FMTK
🧾 14th June 2024 | [External PR](https://github.com/justarandomgeek/vscode-factoriomod-debug/pull/126)
> Use of FMTK is not required but is advised.
> To enable Clusterio support, use the CLI flag `--clusterio-modules` under `Lua.runtime.pluginArgs`.
We needed to maintain a consistent style across all our files.
There are multiple competing standards for Lua code, and given the limited amount of Lua code in this project, we chose not to adopt any automatic linting or shared luarc file.
Nevertheless, we recomend the use of [FMTK](https://github.com/justarandomgeek/vscode-factoriomod-debug) and have taken steps towords supporting it in Clusterio.
### Name Style (Lua)
🧾 Time Immemorial
> Everything uses lowercase_underscore
We needed to maintain a consistent name style across all our files.
Therefore, we follow common practive for Lua of using lowercase_underscore.
## Code Style (Other)
### Markdown
🧾 Time Immemorial
> Use one sentence per line.
> Place two lines before each heading, except for the first one or when two headings are adjacent.
We needed to maintain a consistent code style across all our markdown files.
Therefore, we selected an approach which makes diff reports more useful and which makes headings use additional space to make them stand out.
## Repository Structure
### External Plugins
🧾 Time Immemorial
> No external plugins should be included in the main repository.
> A folder will exist which external plugin repositories can be cloned into and will be given the same treatment as first party plugins.
We require a designated space for developers to create their plugins without the concern of conflicts with first-party plugins.
To address this, we have introduced a dedicated folder for external plugins.
This solution is straightforward and has since proven to be highly effective.
### First Party Plugins
🧾 Time Immemorial
> First party plugins are included in the main repository.
> Those selected to be first party are held to the same stanards which apply to the core packages.
> Each will recieve a new release in step with the core packages.
We require a designated space for first-party plugins, separate from the core packages, to clearly represent the dependency between them.
Creating a folder provided a simple solution to this.
### Core Packages
🧾 Time Immemorial
> All core packages will be contained in the packages folder of the repository.
> They are allowed to be interdependent on each other.
We require a set of core packages to represent the different applications used in a cluster.
See [Separation of Host and Controller](#separation-of-host-and-controller).
To avoid putting more files into the repository root we created a folder for the core packages.
### TypeScript Configs
🧾 26th August 2023 | [Github PR #504](https://github.com/clusterio/clusterio/pull/504)
> Each package / plugin may contain any of: `tsconfig.json` `tsconfig.node.json` and `tsconfig.web.json`.
> The main respository root will contain all three files in addition to `tsconfig.base.json` which node and web extend from.
> The configs inside packages / plugins should extend from those in the respository root.
We found that maintaining independent tsconfig files became burdensome and required a way to consolidate them.
Initially, we introduced a base configuration file; however, this proved insufficient, so we extended it to include separate configurations for Node.js and the Web.
This approach led to each package having up to three tsconfig files.
Nevertheless, we believe this to be justified, given the need to differentiate between Node.js and Web builds while maintaining a single, shared configuration.
### Workspace
🧾 Time Immemorial
> The workspace root should contain only: configuration files, scripts used to build / clean the workspace, the readme, and the changelog.
We need to maintain a clean workspace root by only including files which effect all pacakges and plugins such as configuration files.
We have allowed for build and clean scripts to exist here as long as they are not published as part of any package or plugin.
## Project Architecture
### Deprecation of Phin in favour of `fetch`
🧾 12th December 2024 | [Discord Discussion Dec 24](https://discord.com/channels/450361298220351489/450361298220351491/1316781609206419567) | [Discord Discussion May 25](https://discord.com/channels/450361298220351489/450361298220351491/1240246997496102942)
> The builtin `fetch` method should be used as a replacement of the deprecated Phin module.
We required a replacement for the deprecated Phin HTTP client, which was used for making server-side requests and as part of testing.
With `fetch` being marked as stable in Node.js v21.0.0 and having first been made available in v16.15.0, it serves as a suitable replacement without the need to add new dependencies.
It had previously not been used because it was not marked as stable in any LTS version of NodeJS.
### Minimal Dependencies
🧾 Time Immemorial
> Any new dependencies need to be well justified.
We aim to minimise our exposure to upstream changes by using a minimal number of dependencies.
This also serves to reduce the bloat of a cluster installation and improve installation times.
Consequently, the addition of any new dependencies must be thoroughly justified, with evidence of research into alternative options and comparisons between them.
### React
🧾 Time Immemorial
> Our web stack uses ReactJS
We require a reactive website for our control panel, and ReactJS was selected due to its widespread use and familiarity.
Switching away from React for any reason would represent a significant undertaking.
### Separation of Host and Controller
🧾 Time Immemorial
> The primary application locations are Control (web and cli), Controller, and Host.
> Therefore the core packages are: `ctl` `controller` `host`. There are two libaray packages `lib` and `web_ui`.
> Additionally `create` exists to provide easy creation of a cluster.
A single application to perform all the duties of a cluster would complicate the reasoning and maintenance of the project, so we decided to split it into several packages.
The primary components of a cluster include servers connected to an instance, which runs on a host that connects to the controller, whose configuration is managed via a control connection from a CLI or web portal.
Based on this structure, we found it logical to divide the packages according to their installation location, resulting in `ctl`, `controller`, and `host`, with shared code separated into `lib` and `web_ui` depending on where it is utilised.
The additional `create` package is used by `npm create` to provide a user-friendly installation process.
### TypeScript
🧾 6th August 2023 | [Github PR #491](https://github.com/clusterio/clusterio/issues/491)
> We use TypeScript as our language of choice.
We previously used JavaScript and switched to TypeScript to provide a robust type system.
Transitioning to any other langauge would be too significant an undertaking.
Initially, we supported multiple languages for plugins, including Rust and Go; however, this approach was quickly abandoned due to its complexity.
### Webpack
🧾 Time Immemorial
> Our web stack uses Webpack and associated plugins.
We required a bundler for our web build, and at the time, Webpack was the standard.
While we have considered alternatives since, transitioning to another solution would be a significant undertaking.
## Supportability
### Breaking Changes
🧾 Time Immemorial
> We follow symver versioning for breaking changes.
> In our alpha stage, we allow every alpha to contain breaking changes as long as their are documented and provide migration actions.
> Once we enter beta, breaking changes will be more restricted.
We aim to be highly transparent with breaking changes and maintain compatibility with as many existing installations as possible.
However, as we are currently in the alpha stage, things are unstable, and breaking changes occur frequently.
As such, we have decided to document all breaking changes and provide migration steps where the changes are non-trivial.
Once we enter the beta stage, we will implement stricter restrictions on breaking changes.
### Distribution
🧾 Time Immemorial
> We use `git` and `pnpm` for development distribution.
> We use `npm` and the factorio mod portal for production distribution.
We need to distribute both production builds and development source code for the project.
By necessity, we use `npm` and the Factorio Mod Portal to distribute our production builds.
For development, we rely on `git` and GitHub, which are industry standards, while using `pnpm` as a more efficient variant of `npm` for development installations.
Although bun was considered upon its release, we were not convinced it was the right choice.
### Factorio
🧾 26th October 2024 | [Discord Discussion](https://discord.com/channels/450361298220351489/450361298220351491/1299637042350653461)
> We actively provide full support for the last two major versions of Factorio.
> This approach does not follow semantic versioning; instead, it follows the "middle" versions: 2.1 > 2.0 > 1.1 > 1.0 > 0.18 > 0.17.
> We offer passive support for all versions from 0.17 onwards, where implementation is trivial and does not impose restrictions on our actively supported versions.
Following the 2.0 update of Factorio, which introduced a large number of breaking changes, we needed to reconsider which versions would be actively supported, as it was no longer trivial to support all versions.
Anticipating that future breaking changes would also be significant, and assuming the majority of players would update to the latest stable version upon release, we decided to support the latest stable version and the previous major version that introduced breaking changes.
Support for other versions will not be removed if they have already been implemented and do not impose restrictions on our ability to support newer versions.
### General Support
🧾 Time Immemorial
> We provide full support via GitHub and our Discord server for the latest published version of Clusterio, as well as assistance with migrating from the previous published version.
> However, we do not offer any guarantees with our support; we assist when time permits and may not be able to resolve every issue.
> Additionally, while we offer support in setting up development workspaces and guidance for plugin development, we do not provide support for those running development builds as production instances.
We aim to support as many users of Clusterio as possible in setting up and using the platform for a variety of purposes, including setting up production instances, development workspaces, and providing guidance on plugin development.
However, our time to work on Clusterio is limited, and even more so our ability to offer support.
As such, we cannot assist with every issue and must set reasonable restrictions to ensure expectations are aligned.
Consequently, we do not offer any guarantees for our support, but we strive to provide as much assistance as possible for those setting up or running the latest published version.
Additionally, we support users in setting up development workspaces, as the more people willing to contribute, the better.
### NodeJS
🧾 Time Immemorial
> We support all LTS versions of NodeJS in their active and maintenance phases.
Our options were: supporting all versions, supporting all active versions, supporting all LTS versions, or supporting only the latest version.
As we aim to provide a stable, production-ready tool while still limiting the number of versions we need to manage, we chose to support all LTS versions because NodeJS can be installed by the majority of linux distro package managers which will always offer at least one LTS version.
### Operating Systems
🧾 Time Immemorial
> We support all major versions of Windows, Linux, and MacOS.
This is largely inpart to our use of NodeJS and is not a guarantee from us.
Our choice of Node.js has enabled us to support all major operating systems with minimal effort on our part.
Nevertheless, there have been instances where code paths have diverged between systems, and we have attempted to maintain support for as many systems as possible.
However, this is not a priority, and such support has often been reactive or provided by external contributors.
## Testing
### Continuous Integration
🧾 Time Immemorial
> We run unit and integration tests through GitHub CI actions as well as test coverage through CodeCov.
Continuous integration actions in GitHub are a valuable tool that we use to assist with the review of pull requests from both maintainers and contributors.
While we aim to increase the number of useful checks performed by CI in the future, this is not a priority over the development of new features.
### Continuous Deployment
🧾 Time Immemorial
> We do not use any CD actions, and have no plans of implementing them.
Unlike CI, we do not find continuous deployment useful at our current stage of the project.
This is due to the frequent breaking changes merged into the main branch, which we prefer to bundle together as a single version.
In the future, we may automate the deployment process, but it will always require manual activation.
### Mocha
🧾 Time Immemorial
> We use Mocha for testing and CodeCov for coverage.
We chose Mocha because it was a good fit when we selected it.
However, we are always looking for a better framework which is worth the effort of switching.
### Version Matrix
🧾 Time Immemorial
> We perform all tests through GitHub actions using Ubuntu latest, using all supported versions of node and all supported versions of factorio.
We perform tests on all versions of NodeJS and Factorio that we support.
However, we do not conduct tests on all supported operating systems, as the added demand on our testing resources would be significant, and the value it would provide for a cross-platform engine like NodeJS would be limited.
+3 -3
View File
@@ -1,7 +1,7 @@
# Protocol
Clusterio communicates using a simple homebrew message passing protocol based on JSON over WebSocket with limited support for reliable transport of messages.
The protocol is implemented in [packages/lib/link](/packages/lib/link) and uses the `ws` library to handle the WebSocket connections on the Node.js side and the native WebSocket API on the browser side.
The protocol is implemented in [packages/lib/src/link](/packages/lib/src/link) and uses the `ws` library to handle the WebSocket connections on the Node.js side and the native WebSocket API on the browser side.
Two general kinds of messages are supported: events and requests.
Events are messages sent by one party and received by the other and offer no notification for the sender on whether it was actually received.
@@ -166,7 +166,7 @@ Events are messages send in one direction over the link, that the receiver is ex
By convention message types ending in `*_event` are events.
The data payload of an event consists solely of event specific properties.
See [packages/lib/link/messages.js](/packages/lib/link/messages.js) for the recognized core events and their contents.
See [packages/lib/src/link/messages.ts](/packages/lib/src/link/messages.ts) for the recognized core events and their contents.
Plugins may defined their own events as well.
#### `*_event`
@@ -181,7 +181,7 @@ They function a lot like HTTP requests except that both parties of a connection
By convention message types ending in `*_request` are requests, and is expected to be replied to with a corresponding `*_response` message.
If an error occured while processing the request an error response containing an error property in the data payload is sent instead.
See [packages/lib/link/messages.js](/packages/lib/link/messages.js) for the recognized core requests and their contents.
See [packages/lib/src/link/messages.ts](/packages/lib/src/link/messages.ts) for the recognized core requests and their contents.
Plugins may define their own requests as well.
#### `*_request`
+62
View File
@@ -28,6 +28,21 @@ Generate a token for a host. `--id` is the host id.
Create a host config. `--id` id the host id. `--name` is the host name. `--generate-token` decides whether to also generate a token, defaulting to `0`. `--output` is the filename for the output of the config; Default is "config-host.json", "-" for stdout.
### Update Clusterio
ctl> host update [--id <host id>] [--name <host name>] [--restart]
If remote updates are enabled on the host, this will attempt to update `@clusterio/host` on the machine. Changes will not be applied until the host is restarted. If the `--restart` flag is provided, the host will automatically restart once the update is complete.
Remote updates are enabled by default but can be disabled on a per-machine basis at the owner's discretion. For security reasons, this configuration setting cannot be changed remotely. To modify it, run the following command locally while the host is offline:
host> config set host.allow_remote_updates true/false
The controller also supports remote updates, following the same logic. The corresponding commands are:
ctl> controller update [--restart]
controller> config set controller.allow_remote_updates true/false
## Instances
### List Instances
@@ -199,3 +214,50 @@ Note: If the player joins the cluster again a new account will be made for them
## Roles
To be written.
## Plugins (Local)
To be written.
## Plugins (Remote)
By default, remote updates are enabled unless you selected "no" during installation.
Remote updates allow ctl and the web UI to update plugins on machines within the cluster.
However, remote installations are disabled by default to prevent the risk of arbitrary code execution through the installation of malicious npm packages onto the machine. See below if you wish to enable this feature.
### List
ctl> controller plugin list
ctl> host plugin list <host>
This command lists all plugins currently known to the remote. Newly installed plugins will not be displayed until the remote is restarted. Plugins that are distributed as an npm package can be updated remotely.
### Update
ctl> controller plugin update <name> [--restart]
ctl> host plugin update <host> <name> [--restart]
If remote plugin updates are enabled on the remote, this will execute `npm update --save <name>`. Changes will not be applied until the remote is restarted. If the `--restart` flag is provided, the remote will automatically restart once the update is complete.
### Install
ctl> controller plugin install <name> [--restart]
ctl> host plugin install <host> <name> [--restart]
If remote plugin installs are enabled on the remote, this will execute `npm install --save <name>`. Changes will not be applied until the remote is restarted. If the `--restart` flag is provided, the remote will automatically restart once the update is complete.
### Enable/Disable Updates
host> config set host.allow_plugin_updates true/false
controller> config set controller.allow_plugin_updates true/false
Remote updates are enabled by default but can be disabled on a per-machine basis at the owner's discretion. For security reasons, this configuration setting cannot be changed remotely. To modify it, run the above commands locally while the remote is offline.
### Enable/Disable Installs
host> config set host.allow_plugin_install true/false
controller> config set controller.allow_plugin_install true/false
Remote installs are disabled by default but can be enabled on a per-machine basis at the owner's discretion. For security reasons, this configuration setting cannot be changed remotely. To modify it, run the above commands locally while the remote is offline.
+4
View File
@@ -10,6 +10,10 @@ You can do this by running the follow RCON command on an instance that is curren
/sc local m={name="pack",description="",factorio_version=game.active_mods.base,mods={},settings={startup={},["runtime-global"]={},["runtime-per-user"]={}}} for n,v in pairs(game.active_mods) do table.insert(m.mods,{name=n,enabled=true,version=v}) end local function set(s,c) for n,v in pairs(s) do m.settings[c][n]=v end end set(settings.startup,"startup") set(settings.global,"runtime-global") if game.player then set(game.player.mod_settings,"runtime-per-user") else set(settings.player,"runtime-per-user") end local e=game.encode_string(game.table_to_json(m)) log(e) rcon.print(e)
If you for some reason want to run this function in 2.0+, use the following command:
/sc local a={name="pack",description="",factorio_version=script.active_mods.base,mods={},settings={startup={},["runtime-global"]={},["runtime-per-user"]={}}}for b,c in pairs(script.active_mods)do table.insert(a.mods,{name=b,enabled=true,version=c})end;local function d(e,f)for b,c in pairs(e)do a.settings[f][b]=c end end;d(settings.startup,"startup")d(settings.global,"runtime-global")if game.player then d(game.player.mod_settings,"runtime-per-user")else d(settings.player,"runtime-per-user")end;local g=helpers.encode_string(helpers.table_to_json(a))log(g)rcon.print(g)
This should produce a [Mod Pack String](https://forums.factorio.com/viewtopic.php?f=96&t=103578) which you need to copy and paste somewhere to keep it for later.
Mod Pack strings looks something like this:
+67
View File
@@ -0,0 +1,67 @@
# Release workflow
## Update changelog
- CHANGELOG.md in the repository root
- The changelog.txt file in mod directories
Pull requests will usually have suggestions for changelog entries.
## Packages
> [!NOTE]
> You will need commit access to the repository as well publish access to the @clusterio org on npm to do this.
1. Ensure you are on the latest version of master branch with no untracked files
```sh
git switch master
git pull --ff-only upstream master
git status
```
2. Bump the version in the `package.json` files
```sh
sed -i 's/2\.0\.0-alpha\.XX/2\.0\.0-alpha\.YY/' {packages,plugins}/*/package.json
````
3. Tag and push the release to github
```sh
git add -p
git commit -m v2.0.0-alpha.XX
git tag v2.0.0-alpha.XX
```
4. Perform a clean install and publish the packages.
```sh
pnpm clean
pnpm -r --filter='./{plugins,packages}/*' publish
```
## Plugins
1. Bump the version in the `package.json` file
2. Run `pnpm install` in the repository root
3. Run `pnpm adduser` to authenticate
4. Run `pnpm publish` in the plugin/package directory
Before confirming the publish, ensure the packed size is approximately the same size as the last version (or within the expected size)
After publishing, test creating and installing the plugin on a fresh install.
## Lua mods
To publish lua mods like clusterio_lib, start by building from source:
```sh
pnpm run build-mod
```
or download the latest artifacts from the CI. When uploading the mods to the mod portal, make sure the latest version is the last version uploaded, as the game/mod portal/ingame updater isn't consistent in how it figures out the "latest version"
## Release notes
Release notes are expected to be published alongside new releases. We use the github releases system as well as posting in the #announcements channel on the discord. Major releases may warrant a ping.
+114 -139
View File
@@ -12,8 +12,6 @@ The plugin classes have pre-defined hooks that are called during various stages
- [Plugin Configuration](#plugin-configuration)
- [Communicating with Factorio](#communicating-with-factorio)
- [Defining Link Messages](#defining-link-messages)
- [Defining Events](#defining-events)
- [Defining Requests](#defining-requests)
- [Sending Link Messages](#sending-link-messages)
- [Handling connection events](#handling-connection-events)
- [Collecting Statistics](#collecting-statistics)
@@ -41,6 +39,7 @@ The usual guides for creating such packages apply.
At minimum the `package.json` file must contain a version entry.
A possible workflow for developing plugins is to place the plugin in a sub-directory of where clusterio has been installed, and rely on Node.js searching up the folder heirarchy for it to find `@clusterio/lib`.
After creating a directory for your plugin, the boilerplate can be generated automaticlly from a set of tempaltes using `npm exec @clusterio/create -y -- --plugin-template` after which you will be asked which areas your plugin will interact with and your plugin's name.
To add it to `plugin-list.json` so that it gets loaded use the `plugin add <path>` sub-command to either clusteriocontroller, clusteriohost or clusterioctl.
Note that it's important that the path starts with ./ or ../ (use .\ or ..\ on Windows).
@@ -302,6 +301,20 @@ local clusterio_api = require("modules/clusterio/api")
clusterio_api.send_json("my_plugin_foo", { data = 123 })
```
For convience a helper function is exposed on server specifically for handling IPC events.
```js
async init() {
this.instance.server.handle("my_plugin_foo", this.handleFoo.bind(this));
}
async handleFoo(content) {
// Do stuff with content
}
```
**Note:** This only handles IPC events, all other events must use `on`. You should not include the `ipc-` prefix when using this method. Error logging is handled for you, if you want custom error handling use `on`.
It's recommended to either use the plugin name as the channel name or to prefix the channel name with the name of the plugin if you need multiple channels.
It's also important to catch any errors that might occur as they will otherwise be propogated to the instance code and kill the server.
@@ -325,175 +338,137 @@ Events are simple one-way notifications that invoke a handler on the target it's
Requests are pairs of request and response messages where the request is sent to the target and the response is the reply back from the target.
The requests are similar to HTTP requests, except both parties of a link may initiate one.
### Defining Events
Events are defined as properties of the messages object exported on `plugin` that map to instances of the `Event` class from `lib/link`.
The name of the property correspond to the handler invoked on the plugin class.
The Event constructor takes an object of properties that define the event, for example the following could be defined in `plugin`:
```js
messages: {
startFrobnication: new lib.Event({
type: "foo_frobber:start_frobnication",
links: ["controller-host", "host-instance"],
forwardTo: "instance",
eventRequired: ["frobnication_type"],
eventProperties: {
"frobnication_type": { type: "string" },
"urgent": { type: "boolean" },
},
}),
},
```
This specifies an event that can be sent from the controller to a host, and from a host to an instance.
It also specifies that the event must contain the property `frobnication_type`, with a string value in the data payload and that it may optionally contain a boolean `urgent` property.
It will also be forwarded by hosts to a specific instance.
The following properties are recognized by the Event constructor:
#### type
The message type sent over the wire.
This must start with the name of the plugin followed by colon and and be unique for the plugin.
The type of the message sent over the socket will have the suffix `_event` appended to it.
#### links
An array of strings describing which links this event can be sent over.
Direction matters, `"controller-host"` means the event can be sent from the controller to the host, but can't be sent back the other way, unless `"host-controller"` is also present in the links array.
The available endpoints are `controller`, `host`, `instance`, and `control`.
Controller talks with host and control, and host talks to instance.
The full chain must be specified as the individual links in order for a message to travers multiple hops, (i.e., for a message to go from controller to instance it must have both `"controller-host"` and `"host-instance"` in the links array).
See `forwardTo` and `broadcastTo` for ways to forward an event to the next link in a chain.
#### forwardTo
Target to forward an event to.
Can either be `"controller"`, to indicate a host should forward it to the controller, or `"instance"`, to indicate it should be forwarded to the instances specified by the `instance_id` event property.
This works by using a default handler for the event at the links that forward it.
#### broadcastTo
Target to broadcast this message towards.
A value of "instance" means the event will be broadcast to all instances downstream of the target it's sent to, but not back from where it came from.
Currently, only "instance" is supported.
This means that sending the event to a host from an instance will cause it to be broadcast to all instances of that host except for the instance it came from.
#### eventRequired
By default all properties defined in `eventProperties` are required to be present in the payload for the event.
This can be overriden by specifying an array of properties which are required here.
This is equivalent to using the `required` keyword in JSON schema.
#### eventProperties
Object with properties mapping to a JSON schema of that property that specifies what's valid to send in the event.
This is equivalent to using the `properties` keyword in JSON schema, except that the properties specified are by default required and additional properties are not allowed.
See [this guide][guide] for an introduction to writing JSON schemas.
The forwardTo and broadcastTo can be combined such that specifying `"controller"` as the forwardTo value and `"instance"` as the broadcastTo value will cause the event to be broadcast to all instances in the cluster.
For this to work, you will need to specify `instance-host`, `host-controller`, `controller-host`, and `host-instance` as the links.
Keep in mind when forwarding events that if the target an event is being forwarded to is not online, the event will be dropped.
Use a request if you need a confirmation that the message was received.
### Definining Requests
Requests are defined as properties of the messages object exported by `plugin` that map to instances of the `Request` class from `lib/link`.
The name of the property corresponds to the handler invoked on the plugin class.
The Request constructor takes an object of properties that define the event.
Messages are defined as items of the `plugin.messages` array exported by `index.js`.
For example, the following could be defined in `plugin`:
```js
messages: {
reportFrobnication: new lib.Request({
type: "foo_frobber:report_frobnication",
links: ["controller-host", "host-instance"],
forwardTo: "instance",
requestRequired: ["verbosity"],
requestProperties: {
"verbosity": { type: "integer" },
"special": { type: "boolean" },
},
responseProperties: {
"report": {
type: "array",
items: { type: "string" },
messages: [
class Frobnicate {
static plugin = "foo_frobber"; // Plugin name
static type = "request";
static src = "controller";
static dst = "instance";
static jsonSchema = {
type: "object",
properties: {
"verbosity": { type: "integer" },
"special": { type: "boolean" },
},
},
}),
},
required: ["verbosity"],
additionalProperties: false,
};
constructor(json) {
this.verbosity = json.verbosity;
this.special = json.special;
}
static fromJSON(json) {
return new Frobnicate(json);
}
toJSON() {
return {
verbosity: this.verbosity,
special: this.special,
};
}
static Response = {
jsonSchema: {
type: "object",
properties: {
"report": {
type: "array",
items: { type: "string" },
},
},
},
fromJSON(json) {
return json
}
}
}
]
```
This specifies a request that can be sent from the controller to a host, and from a host to an instance.
The request data must contain the property `verbosity` with an integer number as the value, as well as the `instance_id` property (implied by `forwardTo: "instance"`) and it may also contain a boolean `special` property.
It also defines that the response sent must contain a `report` property mapping to an array of strings.
When received by a host, it will also be forwarded to the instance specified by `instance_id`.
This specifies a request that can be sent from the controller to an instance, being routed through the correct host behind the scenes.
The request data must contain the property `verbosity` with an integer number as the value, and it may also contain a boolean `special` property.
It also defines that the response sent may contain a `report` property mapping to an array of strings.
The following properties are recognized by the Request constructor:
The following properties are recognized as part of a message class:
#### type
#### static plugin
The message type sent over the wire.
This must start with the name of the plugin followed by colon and and be unique for the plugin.
The type of the message sent over the socket will have the suffix `_request` appended to it for the request and `_response` appended to it for the response.
Must contain the name of the plugin this message is defined by.
This property is not used in core messages.
#### links
#### static type
An array of strings describing which links this request can be sent over.
Direction matters; `"controller-host"` means the request can be sent from the controller to the host and the host can reply to the controller, but the host can't send a request to the controller unless `"host-controller"` is also present in the links array.
A string describing the message type sent over the wire.
This can be either `"request"` or `"event"`.
The primary difference is whether they allow for a response.
The available endpoints are `controller`, `host`, `instance`, and `control`.
Controller talks with host and control, and host talks to instance.
The full chain must be specified as the individual links in order for a message to travers multiple hops, (i.e., for a message to go from controller to instance it must have both `"controller-host"` and `"host-instance"` in the links array).
See `forwardTo` for ways to forward a request to the next link in a chain.
#### static src
#### forwardTo
A string or an array of strings describing where this message is allowed to originate from.
The available endpoints are `"controller"`, `"host"`, `"instance"`, or `"control"`.
Messages sent from a different source will be rejected.
Target to forward the request to.
Can either be `"controller"` to indicate a host should forward it to the controller when receiving it from an instance, or `"instance"` to indicate it should be forwarded to the instances specified by the `instance_id` request property.
This works by using a default handler for the request by the links that forward it.
#### static dst
#### requestRequired
A string or array of strings describing where this message may be addressed to.
The available endpoints are `"controller"`, `"host"`, `"instance"`, or `"control"`.
If the target is multiple hops away it the message will be forwarded towards its destination by the intermediaries.
For example a message sent from a control connection an instance will be forwarded by the controller to the relevant host and from the host to the correct instance.
Messages sent to a different destination will be rejected.
By default all properties defined in `requestProperties` are required to be present in the payload for the request.
This can be overriden by specifying an array of properties which are required here.
This is equivalent to using the `required` keyword in JSON schema.
#### static jsonSchema
#### requestProperties
A JSON schema that specifies what's valid to send in the message.
Object with properties mapping to a JSON schema of that property that specifies what's valid to send in the request.
This is equivalent to using the `properties` keyword in JSON schema, except that the properties specified are by default required and additional properties are not allowed.
See [this guide][guide] for an introduction to writing JSON schemas
#### toJSON
#### responseRequired
Optional method to serialize an instance of the class to a JSON object, see the [documentation for `JSON.stringify()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#description) for the semantics of this method.
This should return an object that's valid according to `jsonSchema` and accepted by `fromJSON`, and may be omitted if the default serialisation by `JSON.stringify()` already does this.
Same as the requestRequired only for the response sent back by the target.
#### static fromJSON
#### responseProperties
Required function to restore the class from a JSON object.
This function is called when the message reaches its destination and is expected to return an identical instance of the class.
I.e. the result of `fromJSON(JSON.parse(JSON.stringify(instance)))` should be a complete copy of the message.
Same as the requestProperties only for the response sent back by the target.
#### static Response
Optional [JSON sterilisable class](/docs/devs/json-serialisable-classes.md) that specifies the response to the request.
Required properties are `jsonSchema` and `fromJSON`.
## Sending Link Messages
Link messages are sent by calling the `.send()` method on the Event/Request instance with the link you want to send it over and the data to send.
For `InstancePlugin` code the link to the host is the `instance` itself, which is accessible through the `.instance` property of the `InstancePlugin`.
The `.info` property of the plugin class exposes the data exported from the plugin's `plugin` export.
In other words:
Link messages are sent by calling the `.sendTo()` method on a connection object with the destination and the data you want to send, where the data is an instance of one of the defined message classes in plugin.messages.
For example:
```js
// In an InstancePlugin class
async frobnicate() {
this.info.messages.exampleEvent.send(this.instance, { foo: "bar" });
const response = await this.instance.sendTo("controller", new messages.Frobnicate({
verbosity: 2,
special: false,
}));
console.log(response); // { report: [...] }
}
```
For the Request class the send method is async and returns the response data received from the target it was sent to, or throws an error if the request failed.
For classes with `static type = "request"` the send method is async and returns a promise that resolves to the response data received from the target it was sent to, or rejects with an error if the request failed.
The destination specification can either be the ID of a particular control, host or instance, or one of the keywords used to send to multiple targets at once.
Example destinations:
```js
sendTo("controller", message);
sendTo("allHosts", message);
sendTo("allControls", message);
sendTo("allInstances", message);
sendTo({ hostId: 123 }, message);
sendTo({ instanceId: 123 }, message);
sendTo({ controlId: 123 }, message);
```
### Handling connection events
@@ -508,7 +483,7 @@ For example the sending of an event from an `InstancePlugin` class can be stoppe
```js
if (this.host.connected) {
this.info.messages.frobnicate.send(this.instance, { foo: "bar" });
this.instance.sendTo("controller", new messages.Frobnicate({ foo: "bar" }));
}
```
@@ -617,4 +592,4 @@ class CtlPlugin extends BaseCtlPlugin {
module.exports = {
CtlPlugin,
}
```
```
+23 -9
View File
@@ -1,16 +1,22 @@
{
"name": "root",
"private": true,
"files": [],
"scripts": {
"build": "tsc --build",
"watch": "tsc --build --watch",
"test": "pnpm run build && mocha --check-leaks -R spec --recursive test \"plugins/*/test/**\"",
"build-mod": "node ./packages/lib/build_mod.js --source-dir ./packages/host/lua/clusterio_lib",
"test": "pnpm run build && mocha --enable-source-maps --check-leaks -R spec --recursive test \"plugins/*/test/**\" --exclude \"test/manual/**\"",
"silent-test": "SILENT_TEST=y pnpm test",
"fast-test": "FAST_TEST=y pnpm test",
"cover": "nyc pnpm test",
"fast-cover": "FAST_TEST=y nyc pnpm test",
"ci-cover": "nyc --reporter=lcovonly pnpm run-script test",
"lint": "eslint packages plugins test",
"lint-fix": "eslint --fix packages plugins test",
"clean": "node ./clean.js fast",
"clean-tests": "node ./clean.js tests",
"clean-all": "node ./clean.js all",
"docs": "typedoc",
"bundle-dependencies": "bundle-dependencies"
},
@@ -23,25 +29,33 @@
"engines": {
"node": ">=18"
},
"workspaces": [
"packages/*",
"plugins/*",
"external_plugins/*",
"external_plugins/*/*"
],
"devDependencies": {
"@clusterio/controller": "workspace:*",
"@clusterio/ctl": "workspace:*",
"@clusterio/host": "workspace:*",
"@clusterio/lib": "workspace:*",
"@swc/core": "^1.4.0",
"@clusterio/web_ui": "workspace:*",
"@swc/core": "^1.11.1",
"@typescript-eslint/eslint-plugin": "^6.4.1",
"@typescript-eslint/parser": "^6.4.1",
"eslint": "^8.45.0",
"eslint": "^8.57.1",
"eslint-plugin-node": "^11.1.0",
"express": "^4.18.2",
"form-data": "^4.0.0",
"fs-extra": "^11.1.1",
"express": "^4.21.2",
"form-data": "^4.0.2",
"fs-extra": "^11.3.0",
"jsonwebtoken": "^9.0.1",
"jszip": "^3.10.0",
"mocha": "^10.2.0",
"mocha": "^10.8.2",
"nyc": "^15.1.0",
"phin": "^3.7.0",
"typedoc": "^0.24.8",
"typescript": "^5.1.6"
"typedoc": "^0.26.11",
"typescript": "^5.7.3",
"yargs": "^17.7.2"
}
}
+33 -36
View File
@@ -66,9 +66,9 @@ void new lib.Gauge(
if (!controller || !controller.hosts) {
return;
}
for (let [id, host] of controller.hosts) {
for (const host of controller.hosts.values()) {
gauge.labels({
host_id: String(id),
host_id: String(host.id),
host_name: host.name,
}).set(1);
};
@@ -86,9 +86,9 @@ void new lib.Gauge(
if (!controller || !controller.instances) {
return;
}
for (let [id, instance] of controller.instances) {
for (const instance of controller.instances.values()) {
gauge.labels({
instance_id: String(id),
instance_id: String(instance.id),
instance_name: String(instance.config.get("instance.name")),
host_id: String(instance.config.get("instance.assigned_host")),
}).set(1);
@@ -139,7 +139,7 @@ async function handleBootstrapCommand(
return;
}
let admin = userManager.users.get(args.name);
let admin = userManager.getByName(args.name);
if (!admin) {
admin = userManager.createUser(args.name);
}
@@ -150,14 +150,14 @@ async function handleBootstrapCommand(
await userManager.save(path.join(controllerConfig.get("controller.database_directory"), "users.json"));
} else if (subCommand === "generate-user-token") {
let user = userManager.users.get(args.name);
let user = userManager.getByName(args.name);
if (!user) {
logger.error(`No user named '${args.name}'`);
process.exitCode = 1;
return;
}
// eslint-disable-next-line no-console
console.log(userManager.signUserToken(user.name));
console.log(userManager.signUserToken(user));
} else if (subCommand === "generate-host-token") {
// eslint-disable-next-line no-console
@@ -167,7 +167,7 @@ async function handleBootstrapCommand(
));
} else if (subCommand === "create-ctl-config") {
let admin = userManager.users.get(args.name);
let admin = userManager.getByName(args.name);
if (!admin) {
logger.error(`No user named '${args.name}'`);
process.exitCode = 1;
@@ -178,7 +178,7 @@ async function handleBootstrapCommand(
controlConfig.set("control.controller_url", Controller.calculateControllerUrl(controllerConfig));
controlConfig.set(
"control.controller_token",
userManager.signUserToken(admin.name),
userManager.signUserToken(admin),
);
let content = JSON.stringify(controlConfig, null, "\t");
@@ -210,7 +210,6 @@ interface InitializeParameters {
shouldRun: boolean;
clusterLogger: winston.Logger;
pluginInfos: lib.PluginNodeEnvInfo[] | null;
controllerConfigPath: string | null;
controllerConfig: lib.ControllerConfig | null;
}
@@ -248,8 +247,12 @@ async function initialize(): Promise<InitializeParameters> {
.command("config", "Manage Controller config", lib.configCommand)
.command("bootstrap", "Bootstrap access to cluster", yargs => {
yargs
.command("create-admin <name>", "Create a cluster admin")
.command("generate-user-token <name>", "Generate authentication token for the given user")
.command("create-admin <name>", "Create a cluster admin", yargs => {
yargs.positional("name", { describe: "Name of the admin user", type: "string" });
})
.command("generate-user-token <name>", "Generate authentication token for the given user", yargs => {
yargs.positional("name", { describe: "Name of the user", type: "string" });
})
.command("generate-host-token <id>", "Generate authentication token for the given host", yargs => {
yargs.positional("id", { describe: "ID of the host", type: "number" });
})
@@ -266,6 +269,10 @@ async function initialize(): Promise<InitializeParameters> {
type: "boolean", nargs: 0, default: false,
describe: "Indicate that a process monitor will restart the controller on failure",
});
yargs.option("recovery", {
type: "boolean", nargs: 0, default: false,
describe: "Start the controller in recovery mode with all plugins disabled and hosts disconnected",
});
yargs.option("dev", { hidden: true, type: "boolean", nargs: 0 });
yargs.option("dev-plugin", { hidden: true, type: "array" });
})
@@ -309,18 +316,14 @@ async function initialize(): Promise<InitializeParameters> {
let shouldRun = false;
if (command === "run") {
logger.info(`Starting Clusterio controller ${version}`);
if (args.recovery) {
logger.warn("Controller recovery mode enabled. Some features will be disabled.");
}
shouldRun = true;
}
logger.info(`Loading available plugins from ${args.pluginList}`);
let pluginList = new Map();
try {
pluginList = new Map(JSON.parse(await fs.readFile(args.pluginList, { encoding: "utf8" })));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
let pluginList = await lib.loadPluginList(args.pluginList);
// If the command is plugin management we don't try to load plugins
if (command === "plugin") {
@@ -330,7 +333,6 @@ async function initialize(): Promise<InitializeParameters> {
shouldRun,
clusterLogger,
pluginInfos: null,
controllerConfigPath: null,
controllerConfig: null,
};
}
@@ -340,37 +342,34 @@ async function initialize(): Promise<InitializeParameters> {
lib.registerPluginMessages(pluginInfos);
lib.addPluginConfigFields(pluginInfos);
let controllerConfig;
const controllerConfigPath = args.config;
logger.info(`Loading config from ${controllerConfigPath}`);
let controllerConfig = new lib.ControllerConfig("controller");
try {
let jsonConfig = JSON.parse(await fs.readFile(controllerConfigPath, { encoding: "utf8" }));
controllerConfig = lib.ControllerConfig.fromJSON(jsonConfig, "controller");
controllerConfig = await lib.ControllerConfig.fromFile("controller", controllerConfigPath);
} catch (err: any) {
if (err.code === "ENOENT") {
logger.info("Config not found, initializing new config");
controllerConfig = new lib.ControllerConfig("controller");
controllerConfig = new lib.ControllerConfig("controller", undefined, controllerConfigPath);
} else {
throw new lib.StartupError(`Failed to load ${controllerConfigPath}: ${err.message}`);
throw new lib.StartupError(`Failed to load ${controllerConfigPath}: ${err.stack ?? err.message ?? err}`);
}
}
controllerConfig.set("controller.version", version); // Allows tracking last loaded version
if (!controllerConfig.get("controller.auth_secret")) {
logger.info("Generating new controller authentication secret");
let asyncRandomBytes = util.promisify(crypto.randomBytes);
let bytes = await asyncRandomBytes(256);
controllerConfig.set("controller.auth_secret", bytes.toString("base64"));
await lib.safeOutputFile(
controllerConfigPath, JSON.stringify(controllerConfig, null, "\t")
);
await controllerConfig.save();
}
if (command === "config") {
await lib.handleConfigCommand(
args, controllerConfig, controllerConfigPath
);
await lib.handleConfigCommand(args, controllerConfig);
} else if (command === "bootstrap") {
await handleBootstrapCommand(args, controllerConfig);
@@ -380,7 +379,6 @@ async function initialize(): Promise<InitializeParameters> {
args,
clusterLogger,
pluginInfos,
controllerConfigPath,
controllerConfig,
shouldRun,
};
@@ -396,19 +394,18 @@ async function startup() {
shouldRun,
clusterLogger,
pluginInfos,
controllerConfigPath,
controllerConfig,
} = await initialize();
if (!shouldRun || !pluginInfos || !controllerConfigPath || !controllerConfig) {
if (!shouldRun || !pluginInfos || !controllerConfig) {
return;
}
controller = new Controller(
clusterLogger,
pluginInfos,
controllerConfigPath,
controllerConfig,
Boolean(args.canRestart),
Boolean(args.recovery),
...await Controller.bootstrap(controllerConfig)
);
+1
View File
@@ -5,6 +5,7 @@ export { default as ControllerRouter } from "./src/ControllerRouter";
export { default as ControlConnection } from "./src/ControlConnection";
export { default as HostConnection } from "./src/HostConnection";
export { default as InstanceInfo } from "./src/InstanceInfo";
export { default as HostInfo } from "./src/HostInfo";
export { default as BaseControllerPlugin } from "./src/BaseControllerPlugin";
export { default as UserManager } from "./src/UserManager";
export { default as WsServer } from "./src/WsServer";
+20 -17
View File
@@ -3,7 +3,7 @@
"description": "Implementation of Clusterio controller",
"author": "Hornwitser",
"license": "MIT",
"version": "2.0.0-alpha.17",
"version": "2.0.0-alpha.21",
"main": "dist/node/index.js",
"scripts": {
"prepare": "tsc --build && webpack-cli --env production",
@@ -13,44 +13,47 @@
"clusteriocontroller": "./dist/node/controller.js"
},
"keywords": [
"clusterio",
"factorio"
],
"engines": {
"node": ">=18"
},
"dependencies": {
"@clusterio/lib": "^2.0.0-alpha.17",
"@clusterio/lib": "workspace:*",
"@sinclair/typebox": "^0.30.4",
"busboy": "^1.6.0",
"compression": "^1.7.4",
"express": "^4.18.2",
"fs-extra": "^11.1.1",
"compression": "^1.8.0",
"express": "^4.21.2",
"finalhandler": "^1.3.1",
"fs-extra": "^11.3.0",
"jsonwebtoken": "^9.0.1",
"jszip": "^3.10.1",
"set-blocking": "^2.0.0",
"webpack-cli": "^5.1.4",
"winston": "^3.10.0",
"winston": "^3.17.0",
"winston-daily-rotate-file": "^4.7.1",
"ws": "^8.13.0",
"ws": "^8.18.1",
"yargs": "^17.7.2"
},
"devDependencies": {
"@clusterio/web_ui": "^2.0.0-alpha.17",
"@swc/core": "^1.4.0",
"@clusterio/web_ui": "workspace:*",
"@swc/core": "^1.11.1",
"@types/busboy": "^1.5.0",
"@types/compression": "^1.7.2",
"@types/express": "^4.17.17",
"@types/finalhandler": "^1.2.0",
"@types/fs-extra": "^11.0.1",
"@types/jsonwebtoken": "^9.0.2",
"@types/node": "^20.4.5",
"@types/jsonwebtoken": "^9.0.9",
"@types/node": "^20.17.19",
"@types/set-blocking": "^2.0.0",
"@types/ws": "^8.5.5",
"@types/yargs": "^17.0.24",
"@types/ws": "^8.5.14",
"@types/yargs": "^17.0.33",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.4.14",
"typescript": "^5.1.6",
"webpack": "^5.88.2",
"react-router-dom": "^6.29.0",
"typescript": "^5.7.3",
"webpack": "^5.98.0",
"webpack-cli": "^5.1.4",
"webpack-dev-middleware": "^6.1.1",
"webpack-merge": "^5.9.0"
},
+2 -2
View File
@@ -35,13 +35,13 @@ export default class BaseConnection extends lib.Link {
this.handle(lib.SubscriptionRequest, this.handleSubscriptionRequest.bind(this));
this.connector.on("close", () => {
this._controller.subscriptions.unsubscribe(this);
this._controller.subscriptions.unsubscribeLink(this);
});
}
async disconnect(code: number, reason: string) {
try {
await this.connector.disconnect();
await this.connector.disconnect(code, reason);
} catch (err: any) {
if (!(err instanceof lib.SessionLost)) {
logger.error(`"Unexpected error preparing disconnect:\n${err.stack}`);
+348 -20
View File
@@ -70,6 +70,7 @@ export default class ControlConnection extends BaseConnection {
this.handle(lib.ControllerStopRequest, this.handleControllerStopRequest.bind(this));
this.handle(lib.ControllerRestartRequest, this.handleControllerRestartRequest.bind(this));
this.handle(lib.ControllerUpdateRequest, this.handleControllerUpdateRequest.bind(this));
this.handle(lib.ControllerConfigGetRequest, this.handleControllerConfigGetRequest.bind(this));
this.handle(lib.ControllerConfigSetFieldRequest, this.handleControllerConfigSetFieldRequest.bind(this));
this.handle(lib.ControllerConfigSetPropRequest, this.handleControllerConfigSetPropRequest.bind(this));
@@ -98,6 +99,7 @@ export default class ControlConnection extends BaseConnection {
this.handle(lib.ModGetRequest, this.handleModGetRequest.bind(this));
this.handle(lib.ModListRequest, this.handleModListRequest.bind(this));
this.handle(lib.ModSearchRequest, this.handleModSearchRequest.bind(this));
this.handle(lib.ModPortalGetAllRequest, this.handleModPortalGetAllRequest.bind(this));
this.handle(lib.ModDeleteRequest, this.handleModDeleteRequest.bind(this));
this.handle(lib.LogSetSubscriptionsRequest, this.handleLogSetSubscriptionsRequest.bind(this));
this.handle(lib.LogQueryRequest, this.handleLogQueryRequest.bind(this));
@@ -116,7 +118,13 @@ export default class ControlConnection extends BaseConnection {
this.handle(lib.UserSetBannedRequest, this.handleUserSetBannedRequest.bind(this));
this.handle(lib.UserSetWhitelistedRequest, this.handleUserSetWhitelistedRequest.bind(this));
this.handle(lib.UserDeleteRequest, this.handleUserDeleteRequest.bind(this));
this.handle(lib.UserBulkImportRequest, this.handleUserBulkImportRequest.bind(this));
this.handle(lib.UserBulkExportRequest, this.handleUserBulkExportRequest.bind(this));
this.handle(lib.PluginListRequest, this.handlePluginListRequest.bind(this));
this.handle(lib.PluginUpdateRequest, this.handlePluginUpdateRequest.bind(this));
this.handle(lib.PluginInstallRequest, this.handlePluginInstallRequest.bind(this));
this.handle(lib.DebugDumpWsRequest, this.handleDebugDumpWsRequest.bind(this));
this.handle(lib.ModPortalDownloadRequest, this.handleModPortalDownloadRequest.bind(this));
}
validateIngress(message: lib.MessageRequest | lib.MessageEvent) {
@@ -134,7 +142,7 @@ export default class ControlConnection extends BaseConnection {
this.checkPermission(message, entry);
} catch (err: any) {
this.connector.sendResponseError(new lib.ResponseError(err.message, err.code), message.src);
logger.audit(`Permission denied for ${message.name} by ${this.user.name} from ${this.connector.dst}`);
logger.audit(`Permission denied for ${message.name} by ${this.user.id} from ${this.connector.dst}`);
throw err;
}
}
@@ -195,7 +203,7 @@ export default class ControlConnection extends BaseConnection {
}
async handleHostRevokeTokensRequest(request: lib.HostRevokeTokensRequest) {
const host = this._controller.hosts.get(request.hostId);
const host = this._controller.hosts.getMutable(request.hostId);
if (!host) {
throw new Error(`Unknown host id (${request.hostId})`);
}
@@ -207,7 +215,7 @@ export default class ControlConnection extends BaseConnection {
hostConnection.connector.terminate();
}
this._controller.hostsUpdated([host]);
this._controller.hosts.set(host);
}
async handleHostListRequest(): Promise<lib.HostDetails[]> {
@@ -249,6 +257,14 @@ export default class ControlConnection extends BaseConnection {
async handleInstanceCreateRequest(request: lib.InstanceCreateRequest) {
const instanceConfig = new lib.InstanceConfig("controller");
if (request.cloneFromId) {
const baseInstance = this._controller.instances.get(request.cloneFromId);
if (!baseInstance) {
throw new lib.RequestError(`Instance with ID ${request.cloneFromId} does not exist`);
}
instanceConfig.update(baseInstance.config.toJSON(), false);
instanceConfig.set("instance.assigned_host", null); // New instances are unassigned
}
instanceConfig.update(request.config, false, "control");
await this._controller.instanceCreate(instanceConfig);
}
@@ -397,8 +413,7 @@ export default class ControlConnection extends BaseConnection {
if (this._controller.modPacks.has(modPack.id)) {
throw new lib.RequestError(`Mod pack with ID ${modPack.id} already exist`);
}
this._controller.modPacks.set(modPack.id, modPack);
this._controller.modPacksUpdated([modPack]);
this._controller.modPacks.set(modPack);
}
async handleModPackUpdateRequest(request: lib.ModPackUpdateRequest) {
@@ -406,19 +421,16 @@ export default class ControlConnection extends BaseConnection {
if (modPack.id === undefined || !this._controller.modPacks.has(modPack.id)) {
throw new lib.RequestError(`Mod pack with ID ${modPack.id} does not exist`);
}
this._controller.modPacks.set(modPack.id, modPack);
this._controller.modPacksUpdated([modPack]);
this._controller.modPacks.set(modPack);
}
async handleModPackDeleteRequest(request: lib.ModPackDeleteRequest) {
let { id } = request;
let modPack = this._controller.modPacks.get(id);
let modPack = this._controller.modPacks.getMutable(id);
if (!modPack) {
throw new lib.RequestError(`Mod pack with ID ${id} does not exist`);
}
modPack.isDeleted = true;
this._controller.modPacks.delete(id);
this._controller.modPacksUpdated([modPack]);
this._controller.modPacks.delete(modPack);
}
async handleModGetRequest(request: lib.ModGetRequest) {
@@ -518,6 +530,88 @@ export default class ControlConnection extends BaseConnection {
};
}
/**
* Handle request to fetch all mods from the Factorio Mod Portal for a given version.
* @param request - The request object containing factorioVersion.
*/
async handleModPortalGetAllRequest(request: lib.ModPortalGetAllRequest) {
const cacheKey = `${request.factorioVersion}-${request.hide_deprecated}`;
const cachedData = this._controller.modPortalCache.get(cacheKey);
const cacheDuration = this._controller.config.get("controller.mod_portal_cache_duration_minutes") * 60 * 1000;
if (cachedData && Date.now() - cachedData.timestamp < cacheDuration) {
return new lib.ModPortalGetAllRequest.Response(cachedData.data);
}
try {
logger.info(`Fetching mod portal data for ${cacheKey}`);
const mods = await lib.ModStore.fetchAllModsFromPortal(
request.factorioVersion,
this._controller.config.get("controller.mod_portal_page_size"),
request.hide_deprecated
);
this._controller.modPortalCache.set(cacheKey, { timestamp: Date.now(), data: mods });
// The Response class is defined inline within the Request class
return new lib.ModPortalGetAllRequest.Response(mods);
} catch (error: any) {
logger.error(`Error fetching all mods from portal (${request.factorioVersion}): ${error}`);
// Propagate a user-friendly error back to the client
throw new lib.RequestError(`Portal mod fetch failed: ${error}`);
}
}
/**
* Handle request to download a mod from the Factorio Mod Portal to the controller.
* @param request - Request object with mod name, version, and factorioVersion.
*/
async handleModPortalDownloadRequest(request: lib.ModPortalDownloadRequest): Promise<lib.ModInfo> {
const { name, version, factorioVersion } = request;
const filename = lib.ModInfo.filename(name, version);
// Check if mod already exists
if (this._controller.modStore.files.has(filename)) {
logger.verbose(`Mod ${filename} already exists, skipping download.`);
return this._controller.modStore.files.get(filename)!;
}
// Get Factorio credentials from config
const username = this._controller.config.get("controller.factorio_username");
const token = this._controller.config.get("controller.factorio_token");
if (!username || !token) {
throw new lib.RequestError("Factorio credentials (username, token) not configured on the controller.");
}
try {
logger.info(`Downloading mod ${filename} for Factorio ${factorioVersion} from portal.`);
const modMap = new Map([[name, version]]);
await this._controller.modStore.downloadMods(modMap, username || "", token || "", factorioVersion);
// Verify download and return ModInfo
const downloadedMod = this._controller.modStore.files.get(filename);
if (!downloadedMod) {
throw new lib.RequestError(`Failed to find mod ${filename} in store after download attempt.`);
}
logger.info(`Successfully downloaded mod ${filename}.`);
return downloadedMod;
} catch (error: any) {
logger.error(`Error downloading mod ${filename} from portal: ${error.message}`);
// Improve error message clarity
let errorMessage = `Mod portal download failed for ${name} v${version}`;
if (error instanceof lib.RequestError) {
errorMessage += `: ${error.message}`;
} else if (error.message.includes("401") || error.message.includes("Unauthorized")) {
errorMessage += ": Authentication failed. Check Factorio username/token in controller config.";
} else if (error.message.includes("404") || error.message.includes("Not Found")) {
errorMessage += ": Mod version not found on the portal.";
} else {
errorMessage += `: ${error.message}`;
}
throw new lib.RequestError(errorMessage);
}
}
async handleModDeleteRequest(request: lib.ModDeleteRequest) {
await this._controller.modStore.deleteMod(request.name, request.version);
}
@@ -630,7 +724,7 @@ export default class ControlConnection extends BaseConnection {
async handleUserGetRequest(request: lib.UserGetRequest): Promise<lib.User> {
let name = request.name;
let user = this._controller.userManager.users.get(name);
let user = this._controller.userManager.getByName(name);
if (!user) {
throw new lib.RequestError(`User ${name} does not exist`);
}
@@ -648,17 +742,17 @@ export default class ControlConnection extends BaseConnection {
}
async handleUserRevokeTokenRequest(request: lib.UserRevokeTokenRequest) {
let user = this._controller.userManager.users.get(request.name);
let user = this._controller.userManager.getByName(request.name);
if (!user) {
throw new lib.RequestError(`User '${request.name}' does not exist`);
}
if (user.name !== this.user.name) {
if (user.id !== this.user.id) {
this.user.checkPermission("core.user.revoke_other_token");
}
user.invalidateToken();
for (let controlConnection of this._controller.wsServer.controlConnections.values()) {
if (controlConnection.user.name === user.name) {
if (controlConnection.user.id === user.id) {
controlConnection.connector.terminate();
}
}
@@ -666,7 +760,7 @@ export default class ControlConnection extends BaseConnection {
}
async handleUserUpdateRolesRequest(request: lib.UserUpdateRolesRequest) {
let user = this._controller.userManager.users.get(request.name);
let user = this._controller.userManager.getByName(request.name);
if (!user) {
throw new lib.RequestError(`User '${request.name}' does not exist`);
}
@@ -684,7 +778,7 @@ export default class ControlConnection extends BaseConnection {
async handleUserSetAdminRequest(request: lib.UserSetAdminRequest) {
let { name, create, admin } = request;
let user = this._controller.userManager.users.get(name);
let user = this._controller.userManager.getByName(name);
if (!user) {
if (create) {
this.user.checkPermission("core.user.create");
@@ -701,7 +795,7 @@ export default class ControlConnection extends BaseConnection {
async handleUserSetBannedRequest(request: lib.UserSetBannedRequest) {
let { name, create, banned, reason } = request;
let user = this._controller.userManager.users.get(name);
let user = this._controller.userManager.getByName(name);
if (!user) {
if (create) {
this.user.checkPermission("core.user.create");
@@ -719,7 +813,7 @@ export default class ControlConnection extends BaseConnection {
async handleUserSetWhitelistedRequest(request: lib.UserSetWhitelistedRequest) {
let { name, create, whitelisted } = request;
let user = this._controller.userManager.users.get(name);
let user = this._controller.userManager.getByName(name);
if (!user) {
if (create) {
this.user.checkPermission("core.user.create");
@@ -736,7 +830,7 @@ export default class ControlConnection extends BaseConnection {
async handleUserDeleteRequest(request: lib.UserDeleteRequest) {
let name = request.name;
let user = this._controller.userManager.users.get(name);
let user = this._controller.userManager.getByName(name);
if (!user) {
throw new lib.RequestError(`User '${name}' does not exist`);
}
@@ -756,6 +850,240 @@ export default class ControlConnection extends BaseConnection {
}
}
async handleUserBulkRestoreRequest(request: lib.UserBulkImportRequest, updated: Map<string, ControllerUser>) {
if (request.importType === "users") {
// Update all fields for all users
const users = request.users as Extract<typeof request.users, { username: string }[]>;
const admin = new Set(users.filter(u => u.is_admin).map(u => u.username.toLowerCase()));
const banned = new Set(users.filter(u => u.is_banned).map(u => u.username.toLowerCase()));
const whitelist = new Set(users.filter(u => u.is_whitelisted).map(u => u.username.toLowerCase()));
for (const user of this._controller.userManager.users.values()) {
if (user.isAdmin && !admin.has(user.name)) {
user.isAdmin = false;
updated.set(user.id, user);
}
if (user.isBanned && !banned.has(user.name)) {
user.isBanned = false;
user.banReason = "";
updated.set(user.id, user);
}
if (user.isWhitelisted && !whitelist.has(user.name)) {
user.isWhitelisted = false;
updated.set(user.id, user);
}
}
} else if (request.importType === "bans") {
// Bans have extra logic to handle ban reason
const users = request.users as (string | { username: string, reason: string })[];
const expected = new Set(
users.map(u => (typeof u === "string" ? u.toLowerCase() : u.username.toLowerCase()))
);
for (const user of this._controller.userManager.users.values()) {
if (user.isBanned && !expected.has(user.name)) {
user.isBanned = false;
user.banReason = "";
updated.set(user.id, user);
}
}
} else {
// Whitelist and admin have the same logic
const users = request.users as string[];
const expected = new Set(users.map(u => u.toLowerCase()));
const prop = request.importType === "admins" ? "isAdmin" : "isWhitelisted";
for (const user of this._controller.userManager.users.values()) {
if (user[prop] && !expected.has(user.name)) {
user[prop] = false;
updated.set(user.id, user);
}
}
}
}
// A large number of small if statements are unavoidable when reducing the number of updates sent
// eslint-disable-next-line complexity
async handleUserBulkImportRequest(request: lib.UserBulkImportRequest) {
let backup: undefined | Awaited<ReturnType<ControlConnection["handleUserBulkExportRequest"]>>;
const updated = new Map<ControllerUser["id"], ControllerUser>();
if (request.restore) {
// Unban / Demote / Unwhitelist players not on the list
backup = await this.handleUserBulkExportRequest(new lib.UserBulkExportRequest(request.importType));
await this.handleUserBulkRestoreRequest(request, updated);
}
// Will get a user or attempt to create one
const getUserOrCreate = (username: string) => {
const user = this._controller.userManager.getByName(username);
if (user) { return user; }
this.user.checkPermission("core.user.create");
return this._controller.userManager.createUser(username);
};
// Merge the imported data with existing data, this is strictly additive
if (request.importType === "users") {
// Update all fields
const users = request.users as Extract<typeof request.users, { username: string }[]>;
for (const user of users) {
const cUser = getUserOrCreate(user.username);
if (user.is_admin && !cUser.isAdmin) {
cUser.isAdmin = true;
updated.set(cUser.id, cUser);
}
if (user.is_banned && !cUser.isBanned) {
cUser.isBanned = true;
cUser.banReason = user.ban_reason ?? "";
updated.set(cUser.id, cUser);
}
if (user.is_whitelisted && !cUser.isWhitelisted) {
cUser.isWhitelisted = true;
updated.set(cUser.id, cUser);
}
}
} else if (request.importType === "bans") {
// Bans have extra logic to handle ban reason
const users = request.users as (string | { username: string, reason: string })[];
for (const user of users) {
if (typeof user === "string") {
const cUser = getUserOrCreate(user);
if (!cUser.isBanned) {
cUser.isBanned = true;
cUser.banReason = "";
updated.set(cUser.id, cUser);
}
} else {
const cUser = getUserOrCreate(user.username);
if (!cUser.isBanned) {
cUser.isBanned = true;
cUser.banReason = user.reason;
updated.set(cUser.id, cUser);
}
}
}
} else {
// Whitelist and admin have the same logic
const users = request.users as string[];
const prop = request.importType === "admins" ? "isAdmin" : "isWhitelisted";
for (const user of users) {
const cUser = getUserOrCreate(user);
if (!cUser[prop]) {
cUser[prop] = true;
updated.set(cUser.id, cUser);
}
}
}
// Send the necessary update events
const updatedUsers = [...updated.values()];
this._controller.usersUpdated(updatedUsers);
// Resync the host user lists
const adminlist: Set<string> = new Set();
const banlist: Map<string, string> = new Map();
const whitelist: Set<string> = new Set();
for (let user of this._controller.userManager.users.values()) {
if (user.isAdmin) {
adminlist.add(user.id);
}
if (user.isBanned) {
banlist.set(user.id, user.banReason);
}
if (user.isWhitelisted) {
whitelist.add(user.id);
}
}
this._controller.sendEvent(
new lib.SyncUserListsEvent(adminlist, banlist, whitelist),
lib.Address.fromShorthand("allHosts")
);
return backup ?? [];
}
async handleUserBulkExportRequest(request: lib.UserBulkExportRequest) {
if (request.exportType === "users") {
// Send a full user export
const usersToSend = [] as lib.ClusterioUserExport["users"];
for (const user of this._controller.userManager.users.values()) {
let send = false;
const userToSend = { username: user.name } as typeof usersToSend[0];
if (user.isBanned) {
send = true;
userToSend.is_banned = true;
if (user.banReason !== "") {
userToSend.ban_reason = user.banReason;
}
}
if (user.isAdmin) {
send = true;
userToSend.is_admin = true;
}
if (user.isWhitelisted) {
send = true;
userToSend.is_whitelisted = true;
}
if (send) {
usersToSend.push(userToSend);
}
}
return new lib.ClusterioUserExport(usersToSend);
} else if (request.exportType === "bans") {
// Bans have extra logic to handle ban reason
const usersToSend = [] as Array<string | { username: string, reason: string }>;
for (const user of this._controller.userManager.users.values()) {
if (user.isBanned) {
if (user.banReason && user.banReason !== "") {
usersToSend.push({ username: user.name, reason: user.banReason });
} else {
usersToSend.push(user.name);
}
}
}
return usersToSend;
}
// This is a whitelist or admins export
const usersToSend = [] as string[];
const prop = request.exportType === "admins" ? "isAdmin" : "isWhitelisted";
for (const user of this._controller.userManager.users.values()) {
if (user[prop]) {
usersToSend.push(user.name);
}
}
return usersToSend;
}
async handleControllerUpdateRequest(request: lib.ControllerUpdateRequest) {
if (!this._controller.config.get("controller.allow_remote_updates")) {
throw new lib.RequestError("Remote updates are disabled on this machine");
}
return lib.updatePackage("@clusterio/controller");
}
async handlePluginUpdateRequest(request: lib.PluginUpdateRequest) {
if (!this._controller.config.get("controller.allow_plugin_updates")) {
throw new lib.RequestError("Plugin updates are disabled on this machine");
}
return await lib.handlePluginUpdate(request.pluginPackage, this._controller.pluginInfos);
}
async handlePluginInstallRequest(request: lib.PluginInstallRequest) {
if (!this._controller.config.get("controller.allow_plugin_install")) {
throw new lib.RequestError("Plugin installs are disabled on this machine");
}
return await lib.handlePluginInstall(request.pluginPackage);
}
async handlePluginListRequest(request: lib.PluginListRequest) {
return this._controller.pluginInfos.map(pluginInfo => lib.PluginDetails.fromNodeEnvInfo(
pluginInfo,
this._controller.plugins.has(pluginInfo.name),
this._controller.config.get(
`${pluginInfo.name}.load_plugin` as keyof lib.ControllerConfigFields
) as boolean,
));
}
async handleDebugDumpWsRequest(request: lib.DebugDumpWsRequest) {
this.ws_dumper = data => {
if (this.connector.connected) {
+199 -229
View File
@@ -3,6 +3,7 @@ import { BlockList, type AddressInfo, isIPv4, isIPv6 } from "net";
import type { ControllerArgs } from "../controller";
import express, { type Request, type Response, type NextFunction, type Application } from "express";
import type { Static } from "@sinclair/typebox";
import finalhandler from "finalhandler";
import compression from "compression";
import events, { EventEmitter } from "events";
@@ -49,22 +50,10 @@ export default class Controller {
clusterLogger: winston.Logger;
/** Array of plugin info objects for known plugins */
pluginInfos: lib.PluginNodeEnvInfo[];
configPath: string;
/** Controller config. */
config: lib.ControllerConfig;
app: Application;
/** True if (@link Controller.systems} has changed since last time it was saved */
systemsDirty = false;
/** True if {@link Controller.hosts} has changed since last time it was saved */
hostsDirty = false;
/** True if {@link Controller.instances} has changed since last time it was saved */
instancesDirty = false;
/** True if {@link Controller.saves} has changed since last time it was saved */
savesDirty = false;
/** True if {@link Controller.modPacks} has changed since last time it was saved */
modPacksDirty = false;
httpServer: http.Server | null = null;
httpServerCloser: HttpCloser | null = null;
httpsServer: https.Server | null = null;
@@ -94,7 +83,7 @@ export default class Controller {
public shouldRestart: boolean = false;
_fallbackedRequests: Map<lib.RequestClass<unknown, unknown>, lib.RequestHandler<unknown, unknown>> = new Map();
_registeredRequests: Map<lib.RequestClass<unknown, unknown>, lib.RequestHandler<unknown, unknown>> = new Map();
_registeredEvents = new Map();
_registeredEvents = new Map<lib.EventClass<unknown>, lib.EventHandler<unknown>>();
_snoopedEvents = new Map();
devMiddleware: any | null = null;
@@ -106,15 +95,45 @@ export default class Controller {
clusterLogIndex: lib.LogIndex | null = null;
clusterLogBuildInterval: ReturnType<typeof setInterval> | null = null;
// Cache for mod portal requests
modPortalCache = new Map<string, { timestamp: number, data: any[] }>();
static async bootstrap(config: lib.ControllerConfig) {
let databaseDirectory = config.get("controller.database_directory");
await fs.ensureDir(databaseDirectory);
const systems = await Controller.loadSystems(path.join(databaseDirectory, "systems.json"));
const hosts = await Controller.loadHosts(path.join(databaseDirectory, "hosts.json"));
const instances = await Controller.loadInstances(path.join(databaseDirectory, "instances.json"));
const saves = await Controller.loadSaves(path.join(databaseDirectory, "saves.json"));
const modPacks = await Controller.loadModPacks(path.join(databaseDirectory, "mod-packs.json"));
const systems = new lib.SubscribableDatastore(...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "systems.json"),
lib.SystemInfo.fromJSON.bind(lib.SystemInfo),
this.migrateSystems,
).bootstrap());
const hosts = new lib.SubscribableDatastore(...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "hosts.json"),
HostInfo.fromJSON.bind(HostInfo),
this.migrateHosts, this.finaliseHosts,
).bootstrap());
const instances = new lib.SubscribableDatastore(...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "instances.json"),
json => InstanceInfo.fromJSON(json, "controller"),
this.migrateInstances, this.finaliseInstances,
).bootstrap());
const saves = new lib.SubscribableDatastore(...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "saves.json"),
lib.SaveDetails.fromJSON.bind(lib.SaveDetails)
).bootstrap());
const modPacks = new lib.SubscribableDatastore(...await new lib.JsonIdDatastoreProvider(
path.join(databaseDirectory, "mod-packs.json"),
lib.ModPack.fromJSON.bind(lib.ModPack)
).bootstrap());
if (modPacks.size === 0) {
modPacks.setMany(lib.ModPack.defaultModPacks);
}
const userManager = new UserManager(config);
await userManager.load(path.join(databaseDirectory, "users.json"));
@@ -136,7 +155,6 @@ export default class Controller {
constructor(
clusterLogger: winston.Logger,
pluginInfos: lib.PluginNodeEnvInfo[],
configPath: string,
config: lib.ControllerConfig,
/**
@@ -144,15 +162,20 @@ export default class Controller {
* will restart the controller on non-zero exit codes.
*/
public canRestart: boolean = false,
public systems = new Map<lib.SystemInfo["id"], lib.SystemInfo>(),
/**
* If true indicates the controller is in recovery mode and should
* disable certain actions such as loading plugins or connecting to hosts
*/
public recoveryMode: boolean = false,
public systems = new lib.SubscribableDatastore<lib.SystemInfo>(),
/** Mapping of host id to host info */
public hosts = new Map<number, HostInfo>(),
public hosts = new lib.SubscribableDatastore<HostInfo>(),
/** Mapping of instance id to instance info */
public instances = new Map<number, InstanceInfo>(),
public instances = new lib.SubscribableDatastore<InstanceInfo>(),
/** Mapping of save id to save details */
public saves = new Map<string, lib.SaveDetails>(),
public saves = new lib.SubscribableDatastore<lib.SaveDetails>(),
/** Mapping of mod pack id to mod pack */
public modPacks = new Map<number, lib.ModPack>(),
public modPacks = new lib.SubscribableDatastore<lib.ModPack>(),
/** Mods stored on the controller */
public modStore = new lib.ModStore(config.get("controller.mods_directory"), new Map()),
/** User and roles manager for the cluster */
@@ -160,7 +183,6 @@ export default class Controller {
) {
this.clusterLogger = clusterLogger;
this.pluginInfos = pluginInfos;
this.configPath = configPath;
this.config = config;
this.app = express();
@@ -184,6 +206,13 @@ export default class Controller {
this.subscriptions.handle(lib.ModPackUpdatesEvent, this.handleModPackSubscription.bind(this));
this.subscriptions.handle(lib.ModUpdatesEvent, this.handleModSubscription.bind(this));
this.subscriptions.handle(lib.UserUpdatesEvent, this.handleUserSubscription.bind(this));
// Handle updates for datastores
this.systems.on("update", this.systemsUpdated.bind(this));
this.hosts.on("update", this.hostsUpdated.bind(this));
this.instances.on("update", this.instanceDetailsUpdated.bind(this));
this.saves.on("update", this.savesUpdated.bind(this));
this.modPacks.on("update", this.modPacksUpdated.bind(this));
}
async start(args: ControllerArgs) {
@@ -265,7 +294,7 @@ export default class Controller {
let tls_cert = this.config.get("controller.tls_certificate");
let tls_key = this.config.get("controller.tls_private_key");
if (httpsPort && (!tls_cert || !tls_key)) {
if (httpsPort && (!tls_cert || !tls_key)) {
throw new lib.StartupError(
"tls_certificate and tls_private_key must be configure in order to use https_port"
);
@@ -286,6 +315,26 @@ export default class Controller {
// Load plugins
await this.loadPlugins();
// Log all express errors from middleware or routes (app.use is missing the type overload)
this.app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction): void => {
logger.log({
level: "error",
message: "Error while handling HTTP" +
`${req.httpVersion} ${req.method} ${req.originalUrl}:\n${err.stack}`,
meta: {
method: req.method,
url: req.originalUrl,
httpVersion: req.httpVersion,
headers: req.headers,
query: req.query,
},
});
// Create and call the final handler, this is the same method used by express
// This is used instead of next(err) to stop express from logging the error to the console
finalhandler(req, res, { env: this.app.get("env") })(err);
});
this.wsServer = new WsServer(this);
// Only start listening for connections after all plugins have loaded
@@ -477,13 +526,23 @@ export default class Controller {
}
requests.push(hostConnection.send(new lib.SystemInfoRequest()));
}
requests.push(lib.gatherSystemInfo("controller", this.canRestart));
if (!this.config.restartRequired) {
// If a restart isn't already required, then test if a new version is installed
try {
const runningVersion = this.config.get("controller.version");
const packageJson = await fs.readJSON(path.join(__dirname, "..", "package.json"));
if (runningVersion !== packageJson.version) {
this.config.restartRequired = true;
}
} catch (err: any) {
logger.warn(`Failed to read package json:\n${err.stack ?? err.message}`);
}
}
requests.push(lib.gatherSystemInfo("controller", this.canRestart, this.config.restartRequired));
const newMetrics = await Promise.all(requests);
for (const metric of newMetrics) {
this.systems.set(metric.id, metric);
this.systems.set(metric);
}
this.systemsDirty = true;
this.subscriptions.broadcast(new lib.SystemInfoUpdateEvent(newMetrics));
} catch (err: any) {
logger.error(`Unexpected error updating system infos:\n${err.stack ?? err.message}`);
}
@@ -494,6 +553,10 @@ export default class Controller {
clearInterval(this.autosaveInterval);
this.autosaveInterval = undefined;
}
if (this.recoveryMode) {
logger.warn("Recovery | autosaving disabled");
return;
}
const autosaveIntervalSeconds = this.config.get("controller.autosave_interval");
if (autosaveIntervalSeconds > 0) {
this.autosaveInterval = setInterval(this.saveData.bind(this), autosaveIntervalSeconds * 1000);
@@ -519,165 +582,71 @@ export default class Controller {
});
private async _saveDataInternal() {
if (this.config.dirty) {
this.config.dirty = false;
await lib.safeOutputFile(this.configPath, JSON.stringify(this.config, null, "\t"));
}
let databaseDirectory = this.config.get("controller.database_directory");
if (this.systemsDirty) {
this.systemsDirty = false;
await Controller.saveSystems(path.join(databaseDirectory, "systems.json"), this.systems);
}
if (this.hostsDirty) {
this.hostsDirty = false;
await Controller.saveHosts(path.join(databaseDirectory, "hosts.json"), this.hosts);
}
if (this.instancesDirty) {
this.instancesDirty = false;
await Controller.saveInstances(path.join(databaseDirectory, "instances.json"), this.instances);
}
if (this.savesDirty) {
this.savesDirty = false;
await Controller.saveSaves(path.join(databaseDirectory, "saves.json"), this.saves);
}
if (this.modPacksDirty) {
this.modPacksDirty = false;
await Controller.saveModPacks(path.join(databaseDirectory, "mod-packs.json"), this.modPacks);
}
await Promise.all([
this.config.save(),
this.systems.save(),
this.hosts.save(),
this.instances.save(),
this.saves.save(),
this.modPacks.save(),
]);
if (this.userManager.dirty) {
await this.userManager.save(path.join(databaseDirectory, "users.json"));
await this.userManager.save(path.join(this.config.get("controller.database_directory"), "users.json"));
}
await lib.invokeHook(this.plugins, "onSaveData");
}
static async loadSystems(filePath: string): Promise<Map<lib.SystemInfo["id"], lib.SystemInfo>> {
let json: Static<typeof lib.SystemInfo.jsonSchema>[];
try {
json = JSON.parse(await fs.readFile(filePath, { encoding: "utf8" }));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
static migrateSystems(rawJson: unknown[]): Static<typeof lib.SystemInfo.jsonSchema>[] {
const serialized = rawJson as Static<typeof lib.SystemInfo.jsonSchema>[];
return serialized.map(json => {
if (!json.canRestart) { // Added in 2.0.0.alpha.17
json.canRestart = false;
}
return new Map();
if (!json.restartRequired) { // Added in 2.0.0.alpha.21
json.restartRequired = false;
}
return json;
});
}
static migrateHosts(rawJson: unknown[]): Static<typeof HostInfo.jsonSchema>[] {
let serialized = rawJson as any;
// New format 2.0.0.alpha.19
if (serialized.length && serialized[0] instanceof Array) {
serialized = serialized.map((e: any) => e[1]);
}
return new Map(json.map(m => lib.SystemInfo.fromJSON(m)).map(m => [m.id, m]));
return serialized;
}
static async saveSystems(filePath: string, systems: Map<lib.SystemInfo["id"], lib.SystemInfo>) {
await lib.safeOutputFile(filePath, JSON.stringify([...systems.values()], null, "\t"));
}
static async loadHosts(filePath: string): Promise<Map<number, HostInfo>> {
let serialized: [number, Static<typeof HostInfo.jsonSchema>][];
try {
serialized = JSON.parse(await fs.readFile(filePath, { encoding: "utf8" }));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
return new Map();
static finaliseHosts(host: HostInfo): HostInfo {
if (host.connected) {
host.connected = false;
host.updatedAtMs = Date.now();
}
return host;
}
// TODO: migrate remove after release.
if (serialized.length && !(serialized[0] instanceof Array)) {
return new Map(); // Discard old format.
}
const hosts = new Map();
for (let [id, json] of serialized) {
const host = HostInfo.fromJSON(json);
if (host.connected) {
host.connected = false;
host.updatedAtMs = Date.now();
static migrateInstances(rawJson: unknown[]): Static<typeof InstanceInfo.jsonSchema>[] {
const serialized = rawJson as Static<typeof InstanceInfo.jsonSchema>[];
return serialized.map(json => {
if (!json.config) { // New format 2.0.0.alpha.14
return { config: json as any, status: "running" }; // Use running to force updatedAtMs
}
hosts.set(id, host);
return json;
});
}
static finaliseInstances(instance: InstanceInfo): InstanceInfo {
const status = instance.config.get("instance.assigned_host") === null ? "unassigned" : "unknown";
if (instance.status !== status) {
instance.status = status;
instance.updatedAtMs = Date.now();
}
return hosts;
}
static async saveHosts(filePath: string, hosts: Map<number, HostInfo>) {
await lib.safeOutputFile(filePath, JSON.stringify([...hosts.entries()], null, "\t"));
}
static async loadInstances(filePath: string): Promise<Map<number, InstanceInfo>> {
logger.info(`Loading ${filePath}`);
let instances = new Map();
try {
let serialized = JSON.parse(
await fs.readFile(filePath, { encoding: "utf8" })
) as Static<typeof InstanceInfo.jsonSchema>[];
for (let json of serialized) {
if (!json.config) { // migrate: from pre Alpha 14 format.
json = { config: json as any, status: "running" }; // Use running to force updatedAtMs
}
const instance = InstanceInfo.fromJSON(json, "controller");
const status = instance.config.get("instance.assigned_host") === null ? "unassigned" : "unknown";
if (instance.status !== status) {
instance.status = status;
instance.updatedAtMs = Date.now();
}
instances.set(instance.id, instance);
}
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
return new Map();
}
return instances;
}
static async saveInstances(filePath: string, instances: Map<number, InstanceInfo>) {
let serialized = [...instances.values()];
await lib.safeOutputFile(filePath, JSON.stringify(serialized, null, "\t"));
}
static async loadSaves(filePath: string): Promise<Map<string, lib.SaveDetails>> {
let json: Static<typeof lib.SaveDetails.jsonSchema>[];
try {
json = JSON.parse(await fs.readFile(filePath, { encoding: "utf8" }));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
return new Map();
}
return new Map(json.map(s => lib.SaveDetails.fromJSON(s)).map(s => [s.id, s]));
}
static async saveSaves(filePath: string, saves: Map<string, lib.SaveDetails>) {
await lib.safeOutputFile(filePath, JSON.stringify([...saves.values()], null, "\t"));
}
static async loadModPacks(filePath: string): Promise<Map<number, lib.ModPack>> {
let json;
try {
json = JSON.parse(await fs.readFile(filePath, { encoding: "utf8" }));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
return new Map();
}
return new Map(json.map((e: any) => [e.id, lib.ModPack.fromJSON(e)]));
}
static async saveModPacks(filePath: string, modPacks: Map<number, lib.ModPack>) {
await lib.safeOutputFile(filePath, JSON.stringify([...modPacks.values()], null, "\t"));
return instance;
}
static async loadJsonObject(filePath: string, throwOnMissing: boolean = false): Promise<any> {
@@ -721,14 +690,29 @@ export default class Controller {
static addAppRoutes(app: Application, pluginInfos: any[]) {
app.use((req: Request, res: Response, next) => {
let startNs = process.hrtime.bigint();
const startNs = process.hrtime.bigint();
stream.finished(res, () => {
let routePath = "static";
if (req.route && req.route.path) {
routePath = req.route.path;
}
let endNs = process.hrtime.bigint();
endpointDurationSummary.labels(routePath).observe(Number(endNs - startNs) / 1e9);
const endNs = process.hrtime.bigint();
const durationMs = (Number(endNs - startNs) / 1e6);
endpointDurationSummary.labels(routePath).observe(durationMs / 1e3);
logger.log({
level: "http",
message: `HTTP${req.httpVersion} ${req.method} ${req.originalUrl}` +
` (Status ${res.statusCode} in ${durationMs}ms)`,
meta: {
method: req.method,
url: req.originalUrl,
statusCode: res.statusCode,
responseTime: durationMs,
httpVersion: req.httpVersion,
headers: req.headers,
query: req.query,
},
});
});
next();
});
@@ -759,6 +743,10 @@ export default class Controller {
}
}
systemsUpdated(systems: lib.SystemInfo[]) {
this.subscriptions.broadcast(new lib.SystemInfoUpdateEvent(systems));
}
async handleSystemInfoSubscription(request: lib.SubscriptionRequest) {
const systems = [...this.systems.values()].filter(
metric => metric.updatedAtMs > request.lastRequestTimeMs,
@@ -780,11 +768,6 @@ export default class Controller {
}
hostsUpdated(hosts: HostInfo[]) {
const now = Date.now();
for (const host of hosts) {
host.updatedAtMs = now;
}
this.hostsDirty = true;
let updates = hosts.map(host => host.toHostDetails());
this.subscriptions.broadcast(new lib.HostUpdatesEvent(updates));
}
@@ -840,8 +823,6 @@ export default class Controller {
"tags": ["clusterio"],
"max_players": 0,
"visibility": { "public": true, "lan": true },
"username": "",
"token": "",
"game_password": "",
"require_user_verification": true,
"max_upload_in_kilobytes_per_second": 0,
@@ -860,12 +841,10 @@ export default class Controller {
};
instanceConfig.set("factorio.settings", settings);
let instance = new InstanceInfo(instanceConfig, "unassigned", undefined, Date.now());
this.instances.set(instanceId, instance);
this.instancesDirty = true;
let instance = new InstanceInfo(instanceConfig, "unassigned", undefined, undefined, Date.now());
this.instances.set(instance);
await lib.invokeHook(this.plugins, "onInstanceStatusChanged", instance);
this.addInstanceHooks(instance);
this.instanceDetailsUpdated([instance]);
return instance;
}
@@ -875,19 +854,7 @@ export default class Controller {
* @internal
*/
clearSavesOfInstance(instanceId: number) {
const updates = [];
const now = Date.now();
for (const [id, save] of this.saves) {
if (save.instanceId === instanceId) {
save.isDeleted = true;
save.updatedAtMs = now;
updates.push(save);
this.saves.delete(id);
}
}
if (updates.length) {
this.savesUpdated(updates);
}
this.saves.deleteMany([...this.saves.values()].filter(save => save.instanceId === instanceId));
}
/**
@@ -934,7 +901,6 @@ export default class Controller {
// Assign to target
instance.config.set("instance.assigned_host", hostId ?? null);
// "fieldChanged" event handler will set this.instancesDirty
if (hostId !== undefined && newHostConnection) {
await newHostConnection.send(
new lib.InstanceAssignInternalRequest(instanceId, instance.config.toRemote("host"))
@@ -942,8 +908,7 @@ export default class Controller {
} else {
instance.status = "unassigned";
}
instance.updatedAtMs = Date.now();
this.instanceDetailsUpdated([instance]);
this.instances.set(instance);
}
/**
@@ -960,16 +925,13 @@ export default class Controller {
if (hostId !== null) {
await this.sendTo({ hostId }, new lib.InstanceDeleteInternalRequest(instanceId));
}
this.instances.delete(instanceId);
this.instancesDirty = true;
let prev = instance.status;
this.instances.delete(instance);
this.clearSavesOfInstance(instanceId);
this.userManager.clearStatsOfInstance(instanceId);
let prev = instance.status;
instance.status = "deleted";
instance.updatedAtMs = Date.now();
this.instanceDetailsUpdated([instance]);
await lib.invokeHook(this.plugins, "onInstanceStatusChanged", instance, prev);
}
@@ -997,16 +959,18 @@ export default class Controller {
addInstanceHooks(instance: InstanceInfo) {
instance.config.on("fieldChanged", (field: string, curr: any, prev: any) => {
instance.updatedAtMs = Date.now();
if (field === "instance.name") {
this.instanceDetailsUpdated([instance]);
}
this.instances.set(instance); // Trigger update logic
this.instancesDirty = true;
lib.invokeHook(this.plugins, "onInstanceConfigFieldChanged", instance, field, curr, prev);
});
}
instanceDetailsUpdated(instances: InstanceInfo[]) {
for (const instance of instances) {
if (instance.status === "stopped") {
this.subscriptions.unsubscribeAddress(lib.Address.fromShorthand({instanceId: instance.id}));
}
}
const updates = instances.map(instance => instance.toInstanceDetails());
this.subscriptions.broadcast(new lib.InstanceDetailsUpdatesEvent(updates));
}
@@ -1019,7 +983,6 @@ export default class Controller {
}
savesUpdated(saves: lib.SaveDetails[]) {
this.savesDirty = true;
this.subscriptions.broadcast(new lib.InstanceSaveDetailsUpdatesEvent(saves));
}
@@ -1030,11 +993,6 @@ export default class Controller {
modPacksUpdated(modPacks: lib.ModPack[]) {
const now = Date.now();
for (const modPack of modPacks) {
modPack.updatedAtMs = now;
}
this.modPacksDirty = true;
this.subscriptions.broadcast(new lib.ModPackUpdatesEvent(modPacks));
lib.invokeHook(this.plugins, "onModPacksUpdated", modPacks);
}
@@ -1128,6 +1086,11 @@ export default class Controller {
continue;
}
if (this.recoveryMode) {
logger.warn(`Recovery | force disabled plugin ${pluginInfo.name}`);
continue;
}
let ControllerPluginClass = BaseControllerPlugin;
try {
if (pluginInfo.controllerEntrypoint) {
@@ -1192,7 +1155,7 @@ export default class Controller {
}
static calculateControllerUrl(config: lib.ControllerConfig) {
let url = config.get("controller.external_address");
let url = config.get("controller.public_url");
if (!url) {
if (config.get("controller.https_port")) {
url = `https://localhost:${config.get("controller.https_port")}/`;
@@ -1315,10 +1278,10 @@ export default class Controller {
throw Error(`Unknown type ${requestOrEvent.constructor.type}.`);
}
async sendRequest(request: any, dst: lib.Address) {
sendRequest(request: any, dst: lib.Address) {
let connection;
if (dst.type === lib.Address.controller) {
throw new Error("controller as dst is not supported.");
throw new Error(`Message would return back to sender ${dst}.`);
} else if (dst.type === lib.Address.control) {
connection = this.wsServer.controlConnections.get(dst.id);
@@ -1353,24 +1316,33 @@ export default class Controller {
sendEvent<T>(event: lib.Event<T>, dst: lib.Address) {
let connection;
if (dst.type === lib.Address.controller) {
throw new Error("controller as dst is not supported.");
throw new Error(`Message would return back to sender ${dst}.`);
} else if (dst.type === lib.Address.control) {
connection = this.wsServer.controlConnections.get(dst.id);
if (!connection) {
throw new Error("Target control connection does not exist.");
}
} else if (dst.type === lib.Address.instance) {
let instance = this.instances.get(dst.id);
if (!instance) {
return;
throw new Error(`Instance with ID ${dst.id} does not exist`);
}
let hostId = instance.config.get("instance.assigned_host");
if (hostId === null) {
return;
throw new Error("Instance is not assigned to a host");
}
connection = this.wsServer.hostConnections.get(hostId);
if (!connection) {
throw new Error("Host containing instance is not connected");
}
} else if (dst.type === lib.Address.host) {
connection = this.wsServer.hostConnections.get(dst.id);
if (!connection) {
throw new Error("Host is not connected");
}
} else if (dst.type === lib.Address.broadcast) {
if (dst.id === lib.Address.control) {
@@ -1388,7 +1360,7 @@ export default class Controller {
}
} else {
throw new Error(`Unexpected broacdast target ${dst.id}`);
throw new Error(`Unexpected broadcast target ${dst.id}`);
}
return;
@@ -1396,9 +1368,7 @@ export default class Controller {
throw new Error(`Unknown address type ${dst.type}`);
}
if (connection) {
connection.sendEvent(event, dst);
}
connection.sendEvent(event, dst);
}
async sendToHostByInstanceId<Req extends InstanceId, Res>(request: Req & lib.Request<Req, Res>): Promise<Res>;
+110 -37
View File
@@ -42,8 +42,7 @@ export default class HostConnection extends BaseConnection {
0,
false,
);
this._controller.hosts.set(this.id, this.info);
this._controller.hostsUpdated([this.info]);
this._controller.hosts.set(this.info);
this._checkPluginVersions();
@@ -57,10 +56,9 @@ export default class HostConnection extends BaseConnection {
}
this.connector.on("close", () => {
const now = Date.now();
// Update status to unknown for instances on this host.
const instances: InstanceInfo[] = [];
for (let instance of this._controller.instances.values()) {
for (let instance of this._controller.instances.valuesMutable()) {
if (instance.config.get("instance.assigned_host") !== this.id) {
continue;
}
@@ -68,21 +66,24 @@ export default class HostConnection extends BaseConnection {
instances.push(instance);
let prev = instance.status;
instance.status = "unknown";
instance.updatedAtMs = now;
lib.invokeHook(this._controller.plugins, "onInstanceStatusChanged", instance, prev);
}
this._controller.instanceDetailsUpdated(instances);
this._controller.instances.setMany(instances);
this.info.connected = false;
this._controller.hostsUpdated([this.info]);
this._controller.hosts.set(this.info);
});
this.handle(lib.GetFactorioCredentialsRequest, this.handleGetFactorioCredentialsRequest.bind(this));
this.handle(lib.HostInfoUpdateEvent, this.handleHostInfoUpdateEvent.bind(this));
this.handle(lib.InstanceStatusChangedEvent, this.handleInstanceStatusChangedEvent.bind(this));
this.handle(lib.InstancesUpdateRequest, this.handleInstancesUpdateRequest.bind(this));
this.handle(lib.InstanceSaveDetailsUpdatesEvent, this.handleInstanceSaveDetailsUpdatesEvent.bind(this));
this.handle(lib.LogMessageEvent, this.handleLogMessageEvent.bind(this));
this.handle(lib.InstancePlayerUpdateEvent, this.handleInstancePlayerUpdateEvent.bind(this));
this.snoopEvent(lib.InstanceAdminlistUpdateEvent, this.snoopInstanceAdminlistUpdateEvent.bind(this));
this.snoopEvent(lib.InstanceBanlistUpdateEvent, this.snoopInstanceBanlistUpdateEvent.bind(this));
this.snoopEvent(lib.InstanceWhitelistUpdateEvent, this.snoopInstanceWhitelistUpdateEvent.bind(this));
}
validateIngress(message: lib.MessageRoutable) {
@@ -155,14 +156,33 @@ export default class HostConnection extends BaseConnection {
return this.info.id;
}
/**
* Remote address of the host this connection is connected to
*
* @returns {string} host remote address.
*/
get remoteAddress() {
return this.info.remoteAddress;
}
async handleGetFactorioCredentialsRequest() {
if (this._controller.config.get("controller.share_factorio_credentials_with_hosts")) {
return {
username: this._controller.config.get("controller.factorio_username") ?? undefined,
token: this._controller.config.get("controller.factorio_token") ?? undefined,
};
}
return {};
}
async handleHostInfoUpdateEvent(event: lib.HostInfoUpdateEvent) {
this.info.name = event.update.name;
this.info.publicAddress = event.update.publicAddress;
this._controller.hostsUpdated([this.info]);
this._controller.hosts.set(this.info);
}
async handleInstanceStatusChangedEvent(request: lib.InstanceStatusChangedEvent) {
let instance = this._controller.instances.get(request.instanceId);
let instance = this._controller.instances.getMutable(request.instanceId);
// It's possible to get updates from an instance that does not exist
// or is not assigned to the host it originated from if it was
@@ -188,9 +208,9 @@ export default class HostConnection extends BaseConnection {
let prev = instance.status;
instance.status = request.status;
instance.gamePort = request.gamePort;
instance.updatedAtMs = Date.now();
instance.factorioVersion = request.factorioVersion;
this._controller.instances.set(instance);
logger.verbose(`Instance ${instance.config.get("instance.name")} State: ${instance.status}`);
this._controller.instanceDetailsUpdated([instance]);
await lib.invokeHook(this._controller.plugins, "onInstanceStatusChanged", instance, prev);
}
@@ -210,7 +230,7 @@ export default class HostConnection extends BaseConnection {
const instanceConfig = new lib.InstanceConfig("controller");
instanceConfig.update(instanceData.config, false, "host");
let controllerInstance = this._controller.instances.get(instanceConfig.get("instance.id"));
let controllerInstance = this._controller.instances.getMutable(instanceConfig.get("instance.id"));
if (controllerInstance) {
// Check if this instance is assigned somewhere else.
if (controllerInstance.config.get("instance.assigned_host") !== this.id) {
@@ -225,9 +245,9 @@ export default class HostConnection extends BaseConnection {
let prev = controllerInstance.status;
controllerInstance.status = instanceData.status;
controllerInstance.gamePort = instanceData.gamePort;
controllerInstance.updatedAtMs = Date.now();
logger.verbose(`Instance ${instanceConfig.get("instance.name")} State: ${instanceData.status}`);
controllerInstance.factorioVersion = instanceData.factorioVersion;
instanceUpdates.push(controllerInstance);
logger.verbose(`Instance ${instanceConfig.get("instance.name")} State: ${instanceData.status}`);
await lib.invokeHook(
this._controller.plugins, "onInstanceStatusChanged", controllerInstance, prev
);
@@ -240,23 +260,20 @@ export default class HostConnection extends BaseConnection {
instanceConfig,
instanceData.status,
instanceData.gamePort,
instanceData.factorioVersion,
Date.now(),
);
this._controller.instances.set(instanceConfig.get("instance.id"), newInstance);
this._controller.instancesDirty = true;
instanceUpdates.push(newInstance);
this._controller.addInstanceHooks(newInstance);
await this.send(
new lib.InstanceAssignInternalRequest(
instanceConfig.get("instance.id"), instanceConfig.toRemote("host")
)
);
instanceUpdates.push(newInstance);
await lib.invokeHook(this._controller.plugins, "onInstanceStatusChanged", newInstance);
}
if (instanceUpdates.length) {
this._controller.instanceDetailsUpdated(instanceUpdates);
}
this._controller.instances.setMany(instanceUpdates);
// Push lists to make sure they are in sync.
let adminlist: Set<string> = new Set();
@@ -265,13 +282,13 @@ export default class HostConnection extends BaseConnection {
for (let user of this._controller.userManager.users.values()) {
if (user.isAdmin) {
adminlist.add(user.name);
adminlist.add(user.id);
}
if (user.isBanned) {
banlist.set(user.name, user.banReason);
banlist.set(user.id, user.banReason);
}
if (user.isWhitelisted) {
whitelist.add(user.name);
whitelist.add(user.id);
}
}
@@ -279,33 +296,27 @@ export default class HostConnection extends BaseConnection {
}
async handleInstanceSaveDetailsUpdatesEvent(event: lib.InstanceSaveDetailsUpdatesEvent) {
const now = Date.now();
const updates: lib.SaveDetails[] = [];
const deletes: lib.SaveDetails[] = [];
for (const save of event.updates) {
const existingSave = this._controller.saves.get(save.id);
if (existingSave && save.equals(existingSave)) {
continue;
}
save.updatedAtMs = now;
this._controller.saves.set(save.id, save);
updates.push(save);
}
if (event.instanceId !== undefined) {
const updatedSaves = new Set(event.updates.map(s => s.id));
for (const [id, save] of this._controller.saves) {
if (save.instanceId === event.instanceId && !updatedSaves.has(id)) {
save.isDeleted = true;
save.updatedAtMs = now;
updates.push(save);
this._controller.saves.delete(id);
for (const save of this._controller.saves.values()) {
if (save.instanceId === event.instanceId && !updatedSaves.has(save.id)) {
deletes.push(save);
}
}
}
// Hosts eagerly send updates, which means we may get an update
// where nothing actualy changed.
if (updates.length) {
this._controller.savesUpdated(updates);
}
// where nothing actually changed.
this._controller.saves.setMany(updates);
this._controller.saves.deleteMany(deletes);
}
async handleLogMessageEvent(event: lib.LogMessageEvent) {
@@ -318,7 +329,7 @@ export default class HostConnection extends BaseConnection {
async handleInstancePlayerUpdateEvent(event: lib.InstancePlayerUpdateEvent, src: lib.Address) {
let instanceId = src.id;
let user = this._controller.userManager.users.get(event.name);
let user = this._controller.userManager.getByName(event.name);
if (!user) {
user = this._controller.userManager.createUser(event.name);
}
@@ -342,4 +353,66 @@ export default class HostConnection extends BaseConnection {
stats: event.stats,
});
}
async snoopInstanceAdminlistUpdateEvent(event: lib.InstanceAdminlistUpdateEvent, src: lib.Address) {
if (src.type !== lib.Address.instance) {
return;
}
let user = this._controller.userManager.getByName(event.name);
if (!user) {
user = this._controller.userManager.createUser(event.name);
}
user.isAdmin = event.admin;
this._controller.usersUpdated([user]);
const instance = this._controller.instances.get(src.id)!;
await lib.invokeHook(this._controller.plugins, "onPlayerEvent", instance, {
type: event.admin ? "promote" : "demote" as lib.PlayerEvent["type"],
name: event.name,
stats: user.instanceStats.get(src.id)!,
});
}
async snoopInstanceBanlistUpdateEvent(event: lib.InstanceBanlistUpdateEvent, src: lib.Address) {
if (src.type !== lib.Address.instance) {
return;
}
let user = this._controller.userManager.getByName(event.name);
if (!user) {
user = this._controller.userManager.createUser(event.name);
}
user.isBanned = event.banned;
user.banReason = event.reason;
this._controller.usersUpdated([user]);
const instance = this._controller.instances.get(src.id)!;
await lib.invokeHook(this._controller.plugins, "onPlayerEvent", instance, {
type: event.banned ? "ban" : "unban" as lib.PlayerEvent["type"],
name: event.name,
stats: user.instanceStats.get(src.id)!,
resaon: event.reason,
});
}
async snoopInstanceWhitelistUpdateEvent(event: lib.InstanceWhitelistUpdateEvent, src: lib.Address) {
if (src.type !== lib.Address.instance) {
return;
}
let user = this._controller.userManager.getByName(event.name);
if (!user) {
user = this._controller.userManager.createUser(event.name);
}
user.isWhitelisted = event.whitelisted;
this._controller.usersUpdated([user]);
const instance = this._controller.instances.get(src.id)!;
await lib.invokeHook(this._controller.plugins, "onPlayerEvent", instance, {
type: event.whitelisted ? "whitelisted" : "unwhitelisted" as lib.PlayerEvent["type"],
name: event.name,
stats: user.instanceStats.get(src.id)!,
});
}
}
+11
View File
@@ -11,6 +11,7 @@ export default class InstanceInfo {
public config: lib.InstanceConfig,
public status: lib.InstanceStatus,
public gamePort?: number,
public factorioVersion?: string,
public updatedAtMs = 0,
) {
this.config = config;
@@ -22,6 +23,7 @@ export default class InstanceInfo {
"status": lib.InstanceStatus,
"gamePort": Type.Optional(Type.Number()),
"updatedAtMs": Type.Optional(Type.Number()),
"factorioVersion": Type.Optional(Type.String()),
});
static fromJSON(json: Static<typeof this.jsonSchema>, location: lib.ConfigLocation) {
@@ -29,6 +31,7 @@ export default class InstanceInfo {
lib.InstanceConfig.fromJSON(json.config, location),
json.status,
json.gamePort,
json.factorioVersion,
json.updatedAtMs,
);
}
@@ -40,6 +43,7 @@ export default class InstanceInfo {
this.config.get("instance.assigned_host") ?? undefined,
this.gamePort,
this.status,
this.factorioVersion,
this.updatedAtMs,
);
}
@@ -48,6 +52,13 @@ export default class InstanceInfo {
return this.status === "deleted";
}
set isDeleted(value: boolean) {
if (value === false) {
throw new Error("Setting isDeleted to false is not supported, use status instead.");
}
this.status = "deleted";
}
/** Shorthand for `instance.config.get("instance.id")` */
get id():number {
return this.config.get("instance.id");
+35 -11
View File
@@ -10,8 +10,8 @@ import ControllerUser from "./ControllerUser";
* @alias module:controller/src/UserManager
*/
export default class UserManager {
roles: Map<number, lib.Role> = new Map();
users: Map<string, ControllerUser> = new Map();
roles: Map<lib.Role["id"], lib.Role> = new Map();
users: Map<ControllerUser["id"], ControllerUser> = new Map();
dirty = false;
/**
@@ -24,6 +24,10 @@ export default class UserManager {
) {
}
getByName(name: string) {
return this.users.get(name.toLowerCase());
}
async load(filePath: string): Promise<void> {
try {
let content = JSON.parse(await fs.readFile(filePath, { encoding: "utf8" }));
@@ -32,9 +36,29 @@ export default class UserManager {
this.roles.set(role.id, role);
}
let duplicates = 0;
for (let serializedUser of content.users) {
let user = ControllerUser.fromJSON(serializedUser, this);
this.users.set(user.name, user);
const existingUser = this.users.get(user.id);
if (existingUser) {
// Required migration to all lowercase ids in alpha 19
duplicates += 1;
// We assume the user with the lower online time is the duplicate
if (user.playerStats.onlineTimeMs <= existingUser.playerStats.onlineTimeMs) {
existingUser.merge(user);
continue; // Skip users.set
}
user.merge(existingUser);
}
this.users.set(user.id, user);
}
if (duplicates) {
const backupPath = `${filePath}.${Date.now()}.bak`;
lib.logger.warn(
`A total of ${duplicates} users were merged, a backup was written to: ${backupPath}`
);
await lib.safeOutputFile(backupPath, JSON.stringify(content, null, "\t"));
}
} catch (err: any) {
@@ -48,7 +72,7 @@ export default class UserManager {
}
}
async save(filePath:string): Promise<void> {
async save(filePath: string): Promise<void> {
let serializedRoles = [];
for (let role of this.roles.values()) {
serializedRoles.push(role.toJSON());
@@ -72,8 +96,8 @@ export default class UserManager {
* @param name - Name of the user to create.
* @returns The created user.
*/
createUser(name:string): ControllerUser {
if (this.users.has(name)) {
createUser(name: string): ControllerUser {
if (this.getByName(name)) {
throw new Error(`User '${name}' already exists`);
}
@@ -84,20 +108,20 @@ export default class UserManager {
}
let user = new ControllerUser(this, 0, name, roles);
this.users.set(name, user);
this.users.set(user.id, user);
this.dirty = true;
return user;
}
/**
* Sign access token for the given user name
* Sign access token for the given user
*
* @param name - user name to sign token for
* @param user - user to sign token for
* @returns JWT access token for the user.
*/
signUserToken(name: string): string {
signUserToken(user: ControllerUser): string {
return jwt.sign(
{ aud: "user", user: name },
{ aud: "user", user: user.id },
Buffer.from(this._config.get("controller.auth_secret"), "base64")
);
}
+39 -15
View File
@@ -78,11 +78,11 @@ export default class WsServer {
let disconnectTasks = [];
for (let controlConnection of this.controlConnections.values()) {
disconnectTasks.push(controlConnection.disconnect(1001, "Server Quit"));
disconnectTasks.push(controlConnection.disconnect(lib.ConnectionClosed.GoingAway, "Server Quit"));
}
for (let hostConnection of this.hostConnections.values()) {
disconnectTasks.push(hostConnection.disconnect(1001, "Server Quit"));
disconnectTasks.push(hostConnection.disconnect(lib.ConnectionClosed.GoingAway, "Server Quit"));
}
logger.info(`WsServer | Waiting for ${disconnectTasks.length} connectors to close`);
@@ -97,7 +97,7 @@ export default class WsServer {
}
for (let socket of this.pendingSockets) {
socket.close(1001, "Server Quit");
socket.close(lib.ConnectionClosed.GoingAway, "Server Quit");
}
}
@@ -170,7 +170,7 @@ export default class WsServer {
${err.stack}`
);
wsRejectedConnectionsCounter.inc();
socket.close(1011, "Unexpected error");
socket.close(lib.ConnectionClosed.InternalError, "Unexpected error");
});
});
}
@@ -184,7 +184,7 @@ ${err.stack}`
} catch (err: any) {
logger.verbose(`WsServer | closing ${this.remoteAddr(req)} after receiving invalid message`);
wsRejectedConnectionsCounter.inc();
socket.close(1002, `Invalid message: ${err.message}`);
socket.close(lib.ConnectionClosed.ProtocolError, `Invalid message: ${err.message}`);
return;
}
@@ -196,14 +196,14 @@ ${err.stack}`
if (message.type !== "registerHost" && message.type !== "registerControl") {
logger.verbose(`WsServer | closing ${this.remoteAddr(req)} after receiving invalid handshake`);
wsRejectedConnectionsCounter.inc();
socket.close(1002, "Bad handshake");
socket.close(lib.ConnectionClosed.ProtocolError, "Bad handshake");
return;
}
if (this.stopAcceptingNewSessions) {
logger.verbose(`WsServer | closing ${this.remoteAddr(req)}, server is shutting down`);
wsRejectedConnectionsCounter.inc();
socket.close(1001, "Shutting down");
socket.close(lib.ConnectionClosed.GoingAway, "Shutting down");
return;
}
@@ -239,6 +239,13 @@ ${err.stack}`
}
async registerHost(data: lib.RegisterHostData, socket: WebSocket, req: IncomingMessage) {
if (this.controller.recoveryMode) {
logger.warn(`Recovery | rejected host connection attempt for ${this.remoteAddr(req)}`);
wsRejectedConnectionsCounter.inc();
socket.close(lib.ConnectionClosed.RecoveryMode, "Connection Refused: Controller in recovery mode");
return;
}
try {
let tokenPayload = jwt.verify(
data.token,
@@ -263,15 +270,26 @@ ${err.stack}`
} catch (err: any) {
logger.verbose(`WsServer | authentication failed for ${this.remoteAddr(req)}`);
wsRejectedConnectionsCounter.inc();
socket.close(4003, `Authentication failed: ${err.message}`);
socket.close(lib.ConnectionClosed.Unauthorized, `Authentication failed: ${err.message}`);
return;
}
let [connector, sessionToken] = this.createSession(new lib.Address(lib.Address.host, data.id));
let connection = this.hostConnections.get(data.id);
if (connection) {
logger.verbose(`WsServer | disconnecting existing connection for host ${data.id}`);
await connection.disconnect(1008, "Registered from another connection");
if (connection.remoteAddress !== this.remoteAddr(req)) {
logger.verbose(
`WsServer | disallowed duplicate connection from ${this.remoteAddr(req)} for host ${data.id}`
);
wsRejectedConnectionsCounter.inc();
socket.close(lib.ConnectionClosed.PolicyViolation, "Host already connected from another address");
return;
}
logger.verbose(
`WsServer | disconnecting duplicate connection from host ${data.id}`
);
await connection.disconnect(
lib.ConnectionClosed.PolicyViolation, "Newer connection for host from same address"
);
}
logger.info(
@@ -284,7 +302,11 @@ ${err.stack}`
);
}
connection = new HostConnection(data, connector, this.controller, this.remoteAddr(req));
let [connector, sessionToken] = this.createSession(new lib.Address(lib.Address.host, data.id));
// Handle close before HostConnection does. This avoids a host
// connection event happening between the connection breaking and
// it being removed from the host connections list.
connector.on("close", () => {
if (this.hostConnections.get(data.id) === connection) {
this.hostConnections.delete(data.id);
@@ -292,6 +314,8 @@ ${err.stack}`
logger.warn("Unlisted HostConnection closed");
}
});
connection = new HostConnection(data, connector, this.controller, this.remoteAddr(req));
this.hostConnections.set(data.id, connection);
let src = new lib.Address(lib.Address.host, data.id);
connector.ready(socket, src, sessionToken, undefined);
@@ -310,7 +334,7 @@ ${err.stack}`
throw new Error("unexpected JsonWebToken type");
}
user = this.controller.userManager.users.get(tokenPayload.user);
user = this.controller.userManager.getByName(tokenPayload.user);
if (!user) {
throw new Error("invalid user");
}
@@ -322,7 +346,7 @@ ${err.stack}`
} catch (err: any) {
logger.verbose(`WsServer | authentication failed for ${this.remoteAddr(req)}`);
wsRejectedConnectionsCounter.inc();
socket.close(4003, `Authentication failed: ${err.message}`);
socket.close(lib.ConnectionClosed.Unauthorized, `Authentication failed: ${err.message}`);
return;
}
@@ -331,10 +355,10 @@ ${err.stack}`
let [connector, sessionToken] = this.createSession(new lib.Address(lib.Address.control, id));
logger.verbose(`WsServer | registered control ${id} from ${this.remoteAddr(req)}`);
let connection = new ControlConnection(data, connector, this.controller, user, id);
connector.on("close", () => {
this.controlConnections.delete(id);
});
let connection = new ControlConnection(data, connector, this.controller, user, id);
this.controlConnections.set(id, connection);
let account = new lib.AccountDetails(
user.name,
+4 -3
View File
@@ -185,7 +185,7 @@ function validateUserToken(req: Request, res: Response, next: any) {
if (typeof tokenPayload === "string") {
throw new Error("unexpected JsonWebToken type");
}
let user = req.app.locals.controller.userManager.users.get(tokenPayload.user);
let user = req.app.locals.controller.userManager.getByName(tokenPayload.user);
if (!user) {
throw new Error("invalid user");
}
@@ -218,7 +218,7 @@ async function uploadExport(req: Request, res: Response) {
res.sendStatus(400);
return;
}
let modPack = res.app.locals.controller.modPacks.get(modPackId);
let modPack = res.app.locals.controller.modPacks.getMutable(modPackId);
if (!modPack) {
res.sendStatus(400);
return;
@@ -261,7 +261,7 @@ async function uploadExport(req: Request, res: Response) {
modPack.exportManifest = new lib.ExportManifest(assets);
modPack.fillDefaultSettings(settingPrototypes, logger);
res.app.locals.controller.modPacksUpdated([modPack]);
res.app.locals.controller.modPacks.set(modPack);
res.sendStatus(200);
}
@@ -690,4 +690,5 @@ export const webRoutes = [
"/roles/:id/view",
"/plugins",
"/plugins/:name/view",
"/error",
];
@@ -7,6 +7,5 @@
"include": [ "web/**/*.tsx", "web/**/*.ts", "package.json" ],
"compilerOptions": {
"rootDir": "..",
"outDir": "dist/browser",
}
}
-3
View File
@@ -4,7 +4,4 @@
{ "path": "../lib/tsconfig.node.json" },
],
"include": ["index.ts", "controller.ts", "package.json", "src/**/*.ts"],
"compilerOptions": {
"outDir": "dist/node",
},
}
+135 -20
View File
@@ -6,16 +6,29 @@ const fs = require("fs-extra");
const inquirer = require("inquirer");
const os = require("os");
const path = require("path");
const phin = require("phin");
const stream = require("stream");
const util = require("util");
const yargs = require("yargs");
const { levels, logger, setLogLevel } = require("./logging");
const { copyPluginTemplates } = require("./template");
let dev = false;
const scriptExt = process.platform === "win32" ? ".cmd" : "";
const finished = util.promisify(stream.finished);
const { escapeArg } = require("./escape_arg");
// I hate this, there is a bug with rl.close on windows that was meant to have been fixed in Node14.4
// See nodejs/node#21771 and nodejs/node#30701 for the apparent fix that was applied
// One option is to not call rl.close: SBoudrias/Inquirer.js#767
// But this comes with its own issues: SBoudrias/Inquirer.js#899
// The solution below was adapted from aws-amplify/amplify-cli#12347
if (process.platform === "win32") {
const _inquirer_close = inquirer.ui.Prompt.prototype.close;
inquirer.ui.Prompt.prototype.close = function() {
this.rl.terminal = false;
_inquirer_close.call(this);
};
}
const availablePlugins = [
{ name: "Global Chat", value: "@clusterio/plugin-global_chat" },
@@ -93,15 +106,25 @@ async function safeOutputFile(file, data, options={}) {
}
async function execFile(cmd, args) {
logger.verbose(`executing ${cmd} ${args.join(" ")}`);
const escaped = args.map(escapeArg);
logger.verbose(`executing ${cmd} ${escaped.join(" ")}`);
return new Promise((resolve, reject) => {
let child = child_process.execFile(cmd, args, (err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
let child = child_process.execFile(
cmd,
escaped,
{
shell: true,
// eslint-disable-next-line node/no-process-env
env: process.platform === "win32" ? { ...process.env, pct: "%" } : undefined,
},
(err, stdout, stderr) => {
if (err) {
reject(err);
} else {
resolve({ stdout, stderr });
}
}
});
);
let stdout = new LineSplitter({ readableObjectMode: true });
stdout.on("data", line => { logger.verbose(line.toString()); });
child.stdout.pipe(stdout);
@@ -505,6 +528,7 @@ WantedBy=multi-user.target
async function inquirerMissingArgs(args) {
let answers = {};
if (args.mode) { answers.mode = args.mode; }
if (args["plugin-template"]) { answers.mode = "plugin-template"; }
answers = await inquirer.prompt([
{
type: "list",
@@ -517,6 +541,7 @@ async function inquirerMissingArgs(args) {
{ name: "Host only", value: "host" },
{ name: "Ctl only", value: "ctl" },
{ name: "Plugins only", value: "plugins" },
{ name: "Plugin template", value: "plugin-template" },
],
},
], answers);
@@ -536,6 +561,25 @@ async function inquirerMissingArgs(args) {
},
},
], answers);
if (args.httpPort) { answers.httpPort = args.httpPort; }
answers = await inquirer.prompt([
{
type: "input",
name: "httpPort",
message: "HTTP port to listen on",
default: 8080,
validate: input => {
if (!input) {
return "May not be empty";
}
if (!Number.isInteger(Number(input))) {
return "Must be a number";
}
return true;
},
},
], answers);
}
if (answers.mode === "host") {
@@ -579,8 +623,8 @@ async function inquirerMissingArgs(args) {
answers.publicAddress = args.publicAddress;
} else {
try {
let result = await phin("https://api.ipify.org/");
myIp = result.body.toString();
let result = await fetch("https://api.ipify.org/");
myIp = await result.text();
} catch (err) { /* ignore */ }
}
answers = await inquirer.prompt([
@@ -644,6 +688,50 @@ async function inquirerMissingArgs(args) {
}
}
if (["standalone", "controller", "host"].includes(answers.mode)) {
answers = inquirer.prompt([
{
type: "confirm",
name: "remoteNpm",
message: "Allow remote updates via npm?",
default: true,
},
], answers);
}
if (answers.mode === "plugin-template") {
if (args["plugin-template"] && args["plugin-template"].length > 0) {
answers.pluginTemplate = args["plugin-template"];
}
if (args["plugin-name"]) {
answers.pluginName = args["plugin-name"];
}
answers.plugins = [];
answers = await inquirer.prompt([
{
type: "checkbox",
name: "pluginTemplate",
message: "Plugin templates to use",
choices: [
{ name: "Controller", value: "controller" },
{ name: "Host", value: "host" },
{ name: "Instance", value: "instance" },
{ name: "Lua Module", value: "module" },
{ name: "Command Line", value: "ctl" },
{ name: "Web UI", value: "web" },
{ name: "No Typescript", value: "js" },
{ name: "No Config", value: "no_config" },
],
},
{
type: "input",
name: "pluginName",
message: "Name of your new plugin",
default: path.basename(path.resolve()),
},
], answers);
}
if (dev) {
answers.plugins = [];
} else if (args.plugins) {
@@ -663,12 +751,16 @@ async function inquirerMissingArgs(args) {
}
async function downloadLinuxServer() {
let res = await phin("https://factorio.com/get-download/stable/headless/linux64");
let res = await fetch("https://factorio.com/get-download/stable/headless/linux64");
const url = new URL(res.headers.location);
// get the filename of the latest factorio archive from redirected url
const filename = path.posix.basename(url.pathname);
const version = filename.match(/(?<=factorio_headless_x64_).*(?=\.tar\.xz)/)[0];
const match = filename.match(/(?<=factorio_headless_x64_|factorio-headless_linux_)\d+\.\d+\.\d+(?=\.tar\.xz)/);
if (!match || !match.length) {
throw Error(`Unable to extract version from filename: ${filename}`);
}
const version = match[0];
const tmpDir = "temp/create-temp/";
const archivePath = tmpDir + filename;
@@ -682,14 +774,11 @@ async function downloadLinuxServer() {
await fs.ensureDir(tmpDir);
// follow the redirect
res = await phin({
url: url.href,
stream: true,
});
res = await fetch(url.href);
logger.info("Downloading latest Factorio server release. This may take a while.");
const writeStream = fs.createWriteStream(tmpArchivePath);
res.pipe(writeStream);
stream.Readable.fromWeb(res.body).pipe(writeStream);
await finished(writeStream);
@@ -720,7 +809,7 @@ async function main() {
})
.option("mode", {
nargs: 1, describe: "Operating mode to install",
choices: ["standalone", "controller", "host", "ctl", "plugins"],
choices: ["standalone", "controller", "host", "ctl", "plugins", "plugin-template"],
})
.option("migrate-rename", {
nargs: 0, describe: "Migrate from before slave/master rename", default: false, type: "boolean",
@@ -728,6 +817,9 @@ async function main() {
.option("admin", {
nargs: 1, describe: "Admin account name [standalone/controller]", type: "string",
})
.option("http-port", {
nargs: 1, describe: "HTTP port to listen on [standalone/controller]", type: "number",
})
.option("host-name", {
nargs: 1, describe: "Host name [host]", type: "string",
})
@@ -743,9 +835,18 @@ async function main() {
.option("factorio-dir", {
nargs: 1, describe: "Path to Factorio installation [standalone/host]", type: "string",
})
.option("remote-npm", {
nargs: 0, description: "Allow remote updates via npm [standalone/controller/host]", type: "boolean",
})
.option("plugins", {
array: true, describe: "Plugins to install", type: "string",
})
.option("plugin-template", {
array: true, describe: "Plugin template to use [plugin-template]", type: "string",
})
.option("plugin-name", {
nargs: 1, describe: "Name of your new plugin [plugin-template]", type: "string",
})
.strict()
;
@@ -779,6 +880,13 @@ async function main() {
}
let answers = await inquirerMissingArgs(args);
logger.verbose(JSON.stringify(answers));
if (answers.pluginTemplate) {
await copyPluginTemplates(answers.pluginName, answers.pluginTemplate);
return;
}
if (answers.downloadHeadless) {
if (answers.factorioDir !== "factorio") {
throw new InstallError("--download-headless option requires --factorio-dir to be set to factorio");
@@ -786,8 +894,6 @@ async function main() {
await downloadLinuxServer();
}
logger.verbose(JSON.stringify(answers));
if (!dev) {
await installClusterio(answers.mode, answers.plugins);
}
@@ -796,6 +902,9 @@ async function main() {
if (["standalone", "controller"].includes(answers.mode)) {
logger.info("Setting up controller");
await execController(["bootstrap", "create-admin", answers.admin]);
await execController(["config", "set", "controller.http_port", answers.httpPort]);
await execController(["config", "set", "controller.allow_remote_updates", answers.remoteNpm]);
await execController(["config", "set", "controller.allow_plugin_updates", answers.remoteNpm]);
let result = await execController(["bootstrap", "generate-user-token", answers.admin]);
adminToken = result.stdout.split("\n").slice(-2)[0];
}
@@ -810,9 +919,13 @@ async function main() {
result = await execController(["bootstrap", "generate-host-token", hostId]);
let hostToken = result.stdout.split("\n").slice(-2)[0];
// Default to localhost on correct port for host in standalone mode
await execHost(["config", "set", "host.controller_url", `http://localhost:${answers.httpPort}/`]);
await execHost(["config", "set", "host.controller_token", hostToken]);
await execHost(["config", "set", "host.public_address", answers.publicAddress]);
await execHost(["config", "set", "host.factorio_directory", answers.factorioDir]);
await execHost(["config", "set", "host.allow_remote_updates", answers.remoteNpm]);
await execHost(["config", "set", "host.allow_plugin_updates", answers.remoteNpm]);
}
if (answers.mode === "host") {
@@ -824,6 +937,8 @@ async function main() {
await execHost(["config", "set", "host.controller_token", answers.controllerToken]);
await execHost(["config", "set", "host.public_address", answers.publicAddress]);
await execHost(["config", "set", "host.factorio_directory", answers.factorioDir]);
await execHost(["config", "set", "host.allow_remote_updates", answers.remoteNpm]);
await execHost(["config", "set", "host.allow_plugin_updates", answers.remoteNpm]);
}
if (!dev && ["standalone", "controller", "host"].includes(answers.mode)) {
+62
View File
@@ -0,0 +1,62 @@
"use strict";
function escapeArg(arg) {
// When shell: true is used execFile does not escaping of arguments, they are
// simply joined by space and then passed to the shell.
// For Windows the command is cmd.exe /d /s /c "command arg arg ..."
// The parsing and escaping on windows is cursed. The following references are not for mortals:
// https://daviddeley.com/autohotkey/parameters/parameters.htm#WIN
// https://stackoverflow.com/a/4095133
// https://ss64.com/nt/syntax-esc.html
if (process.platform === "win32") {
if (!/[\t "%&<>\\^|]/.test(arg)) {
return arg;
}
const escaped = [];
let pos = 0;
while (pos < arg.length) {
if (arg[pos] === "\\") {
// If a backslash is followed by a quote or the end of the string, then
// double the number of backslashes because why not!
let peek = pos + 1;
while (peek !== arg.length && arg[peek] === "\\") {
peek += 1;
}
if (peek === arg.length || arg[peek] === '"') {
const count = peek - pos;
escaped.push("\\".repeat(count * 2));
pos = peek;
continue;
} else {
escaped.push("\\");
}
} else if (arg[pos] === '"') {
escaped.push('""');
} else if (arg[pos] === "%") {
// Expect a pct environment variable to contain %, because cmd does variable substitutions
// before parsing string quotes, and there's no mechanism for escaping % symbols, and this
// is a half sane workaround. The less sane workaround is to replace % with %%cd:~,%.
escaped.push("%pct%");
} else {
escaped.push(arg[pos]);
}
pos += 1;
}
return `"${escaped.join("")}"`;
}
// For all other platforms the command is /bin/sh -c <command arg arg ...>
// The command and its arguments are passed as a single argument to the shell.
// See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02
// eslint-disable-next-line no-lonely-if
if (!/[|&;<>()$`\\"' \t*?\[#~=%]/.test(arg)) {
return arg;
}
return `"${arg.replace(/[$`"\\]/g, "\\$&")}"`;
}
module.exports = {
escapeArg,
};
+4 -5
View File
@@ -3,23 +3,22 @@
"description": "Installer for clusterio",
"author": "Hornwitser",
"license": "MIT",
"version": "2.0.0-alpha.17",
"version": "2.0.0-alpha.21",
"main": "create.js",
"bin": {
"clusteriocreate": "./create.js"
},
"keywords": [
"factorio",
"clusterio"
"clusterio",
"factorio"
],
"engines": {
"node": ">=18"
},
"dependencies": {
"chalk": "^4.1.2",
"fs-extra": "^11.1.1",
"fs-extra": "^11.3.0",
"inquirer": "^8.2.4",
"phin": "^3.7.0",
"yargs": "^17.7.2"
},
"publishConfig": {
+194
View File
@@ -0,0 +1,194 @@
"use strict";
const { logger } = require("./logging");
const fs = require("fs-extra");
const path = require("path");
async function copyTemplateFile(src, dst, properties) {
logger.verbose(`Writing ${dst} from template ${src}`);
// Closure, get the property value or throw
function getProperty(property) {
if (!properties.hasOwnProperty(property)) {
throw new Error(`Unknown property (${property}) found in template file ${src}`);
}
return properties[property];
}
// Closure, Get the boolean conjunction of a set of properties
function propertyConjunction(props) {
for (const property of props) {
if (property.startsWith("!")) {
if (Boolean(getProperty(property.slice(1)))) {
return false;
}
} else if (!Boolean(getProperty(property))) {
return false;
}
}
return true;
}
// Process the the template file
const badChars = /[\(\)-+/*<>=]/;
const templateData = await fs.readFile(src, "utf8");
const outputData = templateData
// match preprocessor comments: //%//
.replace(/^\/\/%\/\/.*\n/gm, "")
// match preprocessor insert: __property__
.replace(/__([a-zA-Z][a-zA-Z0-9_]*?[a-zA-Z0-9]?)__/gm, (_, property) => getProperty(property))
// match preprocessor conditionals: //%if <CONDITION> [// COMMENT]\n <CONTENT> \n//%endif [COMMENT]\n
.replace(/^\/\/%if ([^\n\/]*)(?:\/\/.*?)?\n((?:.|\n)*?)\/\/%endif.*\n/gm, (_, condition, content) => {
let include = false;
const conjunctions = condition.split("|");
for (const conjunct of conjunctions) {
if (badChars.test(conjunct)) {
throw new Error("Preprocessor condition can only contain logical operators" +
"such as & | or ! it can not have arithmetic, comparisons or parenthesis"
);
}
const property_names = conjunct.split("&").map(s => s.trim());
if (propertyConjunction(property_names)) {
include = true;
break;
}
}
return include ? content : "";
});
// Attempt to write the output file, warn if it already exists
try {
await fs.outputFile(dst, outputData, { flag: "wx" });
} catch (err) {
if (err.code === "EEXIST") {
logger.warn(`Could not create file ${dst} because it already exists`);
} else {
throw err;
}
}
}
async function copyPluginTemplates(pluginName, templates) {
logger.info(`Please wait, coping templates for ${templates.join(", ")}`);
const files = new Map();
const prepare = [];
// Some basic flags for selecting files to include
const javascriptOnly = templates.includes("js");
const config = !templates.includes("no_config") && templates.some(value => (
["controller", "host", "instance", "ctl"].includes(value)
));
const webpack = config || templates.includes("controller") || templates.includes("web");
// A count of the number of isolated contexts the plugin runs under
let pluginContexts = 0;
// Get the file extension and path to the templates
const ext = javascriptOnly ? "js" : "ts";
const templatePath = path.resolve(__dirname, javascriptOnly ? "./templates/plugin-js" : "./templates/plugin-ts");
const commonPath = path.resolve(__dirname, "./templates/common");
// Files included in all templates
files.set(".gitignore", path.join(commonPath, ".gitignore"));
files.set(".npmignore", path.join(commonPath, ".npmignore"));
files.set("package.json", path.join(commonPath, "package.json"));
files.set(`index.${ext}`, path.join(templatePath, `index.${ext}`));
// Files and dependencies to support typescript
if (!javascriptOnly) {
prepare.push("tsc --build");
files.set("tsconfig.json", path.join(templatePath, "tsconfig.json"));
files.set("tsconfig.node.json", path.join(templatePath, "tsconfig.node.json"));
if (webpack) {
files.set("tsconfig.browser.json", path.join(templatePath, "tsconfig.browser.json"));
}
}
// Files and dependences to support webpack
if (webpack) {
prepare.push("webpack-cli --env production");
files.set("webpack.config.js", path.join(commonPath, "webpack.config.js"));
if (templates.includes("web")) {
files.set(`web/index.${ext}x`, path.join(templatePath, `web/plugin.${ext}x`));
pluginContexts += 1;
} else {
files.set(`web/index.${ext}x`, path.join(templatePath, `web/no_plugin.${ext}x`));
}
}
// Files for the controller
if (templates.includes("controller")) {
files.set(`controller.${ext}`, path.join(templatePath, `controller.${ext}`));
pluginContexts += 1;
}
// Files for hosts
if (templates.includes("host")) {
files.set(`host.${ext}`, path.join(templatePath, `host.${ext}`));
pluginContexts += 1;
}
// Files for instances
if (templates.includes("instance")) {
files.set(`instance.${ext}`, path.join(templatePath, `instance.${ext}`));
pluginContexts += 1;
}
// Files for lua modules
if (templates.includes("module")) {
files.set(".luacheckrc", path.join(commonPath, ".luacheckrc"));
files.set("module/module.json", path.join(commonPath, "module/module.json"));
files.set("module/control.lua", path.join(commonPath, "module/control.lua"));
files.set("module/module_exports.lua", path.join(commonPath, "module/module_exports.lua"));
if (templates.includes("instance")) {
files.set("module/globals.lua", path.join(commonPath, "module/globals.lua"));
} else {
files.set(`instance.${ext}`, path.join(templatePath, `instance_empty.${ext}`));
pluginContexts += 1;
}
}
// Files for the control tool
if (templates.includes("ctl")) {
files.set(`ctl.${ext}`, path.join(templatePath, `ctl.${ext}`));
pluginContexts += 1;
}
// If there are more than one contexts then include the message file
if (pluginContexts > 1) {
files.set(`messages.${ext}`, path.join(templatePath, `messages.${ext}`));
}
// Properties that will control the replacements in the templates
const properties = {
config: config,
typescript: !javascriptOnly,
controller: templates.includes("controller"),
host: templates.includes("host"),
instance: templates.includes("instance"),
module: templates.includes("module"),
ctl: templates.includes("ctl"),
web: templates.includes("web"),
multi_context: pluginContexts > 1,
plugin_name: pluginName,
prepare: prepare.join(" && "),
ext: ext,
};
// Write all the template files
const writes = [];
for (let [dst, src] of files) {
writes.push(copyTemplateFile(src, dst, properties));
}
// Wait for writes to complete
await Promise.all(writes);
logger.info("Successfully wrote all template files");
}
module.exports = {
copyPluginTemplates,
};
@@ -0,0 +1,3 @@
dist/
node_modules/
package-lock.json
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,3 @@
webpack.config.js
dist/web/build
dist/browser
@@ -0,0 +1,79 @@
local clusterio_api = require("modules/clusterio/api")
local compat = require("modules/clusterio/compat")
--- Top level module table, contains event handlers and public methods
--- @class __plugin_name__
local MyModule = {
}
--- script_data is 'synced' between players, you should use your plugin name to avoid name conflicts
-- The following helper function is desync safe and avoids name conflicts after being called during on_server_startup
-- There are edge cases in the following events: pre player left, player left, console command, and config changed
-- If you use any of these events and access globalData then make sure to call this function first
local script_data = {}
local function setup_script_data()
if compat.script_data["__plugin_name__"] == nil then
compat.script_data["__plugin_name__"] = {
-- starting values go here
}
end
MyModule.on_load()
end
--- The on_load function is called independently for each client when they first load the map
-- It should be used to restore global aliases and metatables not registered with script.register_metatable
function MyModule.on_load()
script_data = compat.script_data["__plugin_name__"]
end
--- Public methods should be available though your top level module table
function MyModule.foo()
game.print("foo")
end
--- Private methods should be local to the file, this will prevent others from calling it
local function bar()
game.print("bar")
end
--- Clusterio provides a few custom events, on_server_startup is the most useful and should be used in place of on_init
--- @param event EventData.on_server_startup
local function on_server_startup(event)
setup_script_data()
game.print(compat.table_to_json(event))
end
--- Triggered every time a player crafts an item, see below for how handlers are registered
--- @param event EventData.on_player_crafted_item
local function on_player_crafted_item(event)
game.print(compat.table_to_json(event))
//%if instance
local player = assert(game.get_player(event.player_index))
clusterio_api.send_json("__plugin_name__-plugin_example_ipc", {
tick = game.tick, player_name = player.name
})
//%endif
end
--- Run bar every 5 seconds, see below for how handlers are registered
local function on_nth_tick_300()
game.print(game.tick)
bar()
end
--- Factorio events are accessible through defines.events, you can have one handler per event per module
local e = defines.events
local events = {
[clusterio_api.events.on_server_startup] = on_server_startup,
[e.on_player_crafted_item] = on_player_crafted_item,
}
--- Nth tick is a special case that requires its own table, the index represents the time period between calls in ticks
local on_nth_tick = {
[300] = on_nth_tick_300,
}
--- Always return the top level module table from control, this is how clusterio will access your event handlers
MyModule.events = events --- @package
MyModule.on_nth_tick = on_nth_tick --- @package
return MyModule
@@ -0,0 +1,3 @@
--- Expose any globals here, typically just your control file if you want to access it through rcon or commands
-- luacheck: globals __plugin_name__
__plugin_name__ = require("modules/__plugin_name__/control")
@@ -0,0 +1,14 @@
{
"name": "__plugin_name__",
"load": [
"control.lua"
],
"require": [
//%if instance
"globals.lua"
//%endif
],
"dependencies": {
"clusterio": "*"
}
}
@@ -0,0 +1,4 @@
--- Can contain anything you want to allow other plugins to have access to, this example exposes the control file
--- If you only export a single file then it may be better to rename that file to module_exports and save the redirection
-- Access the exports from other modules using require("modules/__plugin_name__")
return require("modules/__plugin_name__/control")
@@ -0,0 +1,54 @@
{
"name": "__plugin_name__",
"version": "0.1.0",
"description": "Example Description. Package. Change me in package.json",
//%if typescript
"main": "dist/node/index.js",
//%endif
//%if !typescript
"main": "index.js",
//%endif
"scripts": {
"prepare": "__prepare__"
},
"engines": {
"node": ">=18"
},
"peerDependencies": {
"@clusterio/lib": "workspace:*"
},
"devDependencies": {
//%// Typescript and type declarations
//%if typescript
"typescript": "^5.5.3",
"@types/node": "^20.4.5",
//%endif
//%if typescript & web
"@types/react": "^18.2.21",
//%endif
//%// Everything else
//%if web
"antd": "^5.13.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
//%endif
//%if web | controller // The controller uses webpack even when there is no plugin code
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-merge": "^5.9.0",
"@clusterio/web_ui": "workspace:*",
//%endif
"@clusterio/lib": "workspace:*"
},
"dependencies": {
"@sinclair/typebox": "^0.30.4"
},
"publishConfig": {
"access": "public"
},
"keywords": [
"clusterio",
"clusterio-plugin",
"factorio"
]
}
@@ -0,0 +1,36 @@
"use strict";
const path = require("path");
const webpack = require("webpack");
const { merge } = require("webpack-merge");
const common = require("@clusterio/web_ui/webpack.common");
module.exports = (env = {}) => merge(common(env), {
context: __dirname,
entry: "./web/index.__ext__x",
output: {
path: path.resolve(__dirname, "dist", "web"),
},
plugins: [
new webpack.container.ModuleFederationPlugin({
name: "__plugin_name__",
library: { type: "window", name: "plugin___plugin_name__" },
exposes: {
"./": "./index.__ext__",
"./package.json": "./package.json",
//%if web
"./web": "./web/index.__ext__x",
//%endif
},
shared: {
"@clusterio/lib": { import: false },
"@clusterio/web_ui": { import: false },
//%if web
"antd": { import: false },
"react": { import: false },
"react-dom": { import: false },
//%endif
},
}),
],
});
@@ -0,0 +1,83 @@
"use strict";
const lib = require("@clusterio/lib");
const { BaseControllerPlugin } = require("@clusterio/controller");
//%if multi_context // Subscribing requires multi context
const {
PluginExampleEvent, PluginExampleRequest,
//%endif
//%if controller & web // Subscribing requires web content and the controller
ExampleSubscribableUpdate, ExampleSubscribableValue,
//%endif
//%if multi_context // Subscribing requires multi context
} = require("./messages");
//%endif
class ControllerPlugin extends BaseControllerPlugin {
//%if controller & web // Subscribing requires web content and the controller
exampleDatabase;
storageDirty = false;
//%endif
//%if multi_context // Subscribing requires multi context
async init() {
this.controller.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.controller.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%endif
//%if controller & web // Subscribing requires web content and the controller
this.controller.subscriptions.handle(ExampleSubscribableUpdate, this.handleExampleSubscription.bind(this));
this.exampleDatabase = new Map(); // If needed, replace with loading from database file
//%endif
//%if multi_context // Subscribing requires multi context
}
//%endif
async onControllerConfigFieldChanged(field, curr, prev) {
this.logger.info(`controller::onControllerConfigFieldChanged ${field}`);
}
//%if instance
async onInstanceConfigFieldChanged(instance, field, curr, prev) {
this.logger.info(`controller::onInstanceConfigFieldChanged ${instance.id} ${field}`);
}
//%endif
async onSaveData() {
this.logger.info("controller::onSaveData");
}
async onShutdown() {
this.logger.info("controller::onShutdown");
}
async onPlayerEvent(instance, event) {
this.logger.info(`controller::onPlayerEvent ${instance.id} ${JSON.stringify(event)}`);
}
//%if multi_context
async handlePluginExampleEvent(event) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
//%if controller & web // Subscribing requires web content and the controller
async handleExampleSubscription(request) {
const values = [...this.exampleDatabase.values()].filter(
value => value.updatedAtMs > request.lastRequestTimeMs,
);
return values.length ? new ExampleSubscribableUpdate(values) : null;
}
//%endif
}
module.exports = {
ControllerPlugin,
};
@@ -0,0 +1,32 @@
"use strict";
const { BaseCtlPlugin } = require("@clusterio/ctl");
const { CommandTree, Command } = require("@clusterio/lib");
//%// We do not check for multi context here because it doesn't make sense to have a ctl without messages
const { PluginExampleEvent, PluginExampleRequest } = require("./messages");
/* eslint-disable no-console */
const pluginCommands = new CommandTree({
name: "__plugin_name__", description: "The default description I forgot to change",
});
pluginCommands.add(new Command({
definition: ["my-command <foo>", "My command description that I really should change", (yargs) => {
yargs.positional("foo", { describe: "foo is foo!", type: "string" });
}],
handler: async function(args, control) {
const response = await control.sendTo("controller", new PluginExampleRequest(args.foo, [1, 2, 3]));
console.log(response);
console.log(args);
},
}));
/* eslint-enable no-console */
class CtlPlugin extends BaseCtlPlugin {
async addCommands(rootCommand) {
rootCommand.add(pluginCommands);
}
}
module.exports = {
CtlPlugin,
};
@@ -0,0 +1,41 @@
"use strict";
const lib = require("@clusterio/lib");
const { BaseHostPlugin } = require("@clusterio/host");
//%if multi_context
const { PluginExampleEvent, PluginExampleRequest } = require("./messages");
//%endif
class HostPlugin extends BaseHostPlugin {
//%if multi_context
async init() {
this.host.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.host.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
}
//%endif
async onHostConfigFieldChanged(field, curr, prev) {
this.logger.info(`host::onInstanceConfigFieldChanged ${field}`);
}
async onShutdown() {
this.logger.info("host::onShutdown");
}
//%if multi_context
async handlePluginExampleEvent(event) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
}
module.exports = {
HostPlugin,
};
@@ -0,0 +1,126 @@
"use strict";
const lib = require("@clusterio/lib");
//%if multi_context
const Messages = require("./messages");
lib.definePermission({
name: "__plugin_name__.example.permission.event",
title: "Example permission event",
description: "Example Description. Event. Change me in index.js",
});
lib.definePermission({
name: "__plugin_name__.example.permission.request",
title: "Example permission request",
description: "Example Description. Request. Change me in index.js",
});
//%endif
//%if controller & web // Subscribing requires web content and the controller
lib.definePermission({
name: "__plugin_name__.example.permission.subscribe",
title: "Example permission subscribe",
description: "Example Description. Subscribe. Change me in index.js",
});
//%endif
//%if web
lib.definePermission({
name: "__plugin_name__.page.view",
title: "Example page view permission",
description: "Example Description. View. Change me in index.js",
});
//%endif
const plugin = {
name: "__plugin_name__",
title: "__plugin_name__",
description: "Example Description. Plugin. Change me in index.js",
//%if controller | host & !config | instance & !config | ctl & !config // Blank line for formatting
//%endif
//%if controller
controllerEntrypoint: "./dist/node/controller",
//%endif
//%if controller & config
controllerConfigFields: {
"__plugin_name__.myControllerField": {
title: "My Controller Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if host & config // Blank line for formatting
//%endif
//%if host
hostEntrypoint: "./dist/node/host",
//%endif
//%if host & config
hostConfigFields: {
"__plugin_name__.myHostField": {
title: "My Host Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if instance & config | module & config // Blank line for formatting
//%endif
//%if instance | module // Modules load an empty instance plugin
instanceEntrypoint: "./dist/node/instance",
//%endif
//%if instance & config
instanceConfigFields: {
"__plugin_name__.myInstanceField": {
title: "My Instance Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if ctl & config // Blank line for formatting
//%endif
//%if ctl
ctlEntrypoint: "./dist/node/ctl",
//%endif
//%if ctl & config
controlConfigFields: {
"__plugin_name__.myControlField": {
title: "My Control Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if multi_context // Subscribing requires multi context
messages: [
Messages.PluginExampleEvent,
Messages.PluginExampleRequest,
//%endif
//%if controller & web // Subscribing requires web content and the controller
Messages.ExampleSubscribableUpdate,
//%endif
//%if multi_context // Subscribing requires multi context
],
//%endif
//%if web // Web content template has an example route which is the plugin name
webEntrypoint: "./web",
routes: [
"/__plugin_name__",
],
//%endif
};
module.exports = {
plugin,
};
@@ -0,0 +1,66 @@
"use strict";
const lib = require("@clusterio/lib");
const { BaseInstancePlugin } = require("@clusterio/host");
//%if multi_context
const { PluginExampleEvent, PluginExampleRequest } = require("./messages");
//%endif
class InstancePlugin extends BaseInstancePlugin {
//%if multi_context | module
async init() {
//%if multi_context
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%endif
//%if module
this.instance.server.handle("__plugin_name__-plugin_example_ipc", this.handlePluginExampleIPC.bind(this));
//%endif
//%if multi_context | module
}
//%endif
async onInstanceConfigFieldChanged(field, curr, prev) {
this.logger.info(`instance::onInstanceConfigFieldChanged ${field}`);
}
async onStart() {
this.logger.info("instance::onStart");
}
async onStop() {
this.logger.info("instance::onStop");
}
async onPlayerEvent(event) {
this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);
//%if module
if (this.instance.status === "running") {
this.sendRcon("/sc __plugin_name__.foo()");
}
//%endif
}
//%if multi_context
async handlePluginExampleEvent(event) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
//%if module
async handlePluginExampleIPC(event) {
this.logger.info(JSON.stringify(event));
}
//%endif
}
module.exports = {
InstancePlugin,
};
@@ -0,0 +1,11 @@
"use strict";
const { BaseInstancePlugin } = require("@clusterio/host");
class InstancePlugin extends BaseInstancePlugin {
// This class is empty because an instance plugin must be defined for a module to be injected
// This requirement may change in the future to allow for standalone modules
}
module.exports = {
InstancePlugin,
};
@@ -0,0 +1,101 @@
"use strict";
const { plainJson, jsonArray, JsonBoolean, JsonNumber, JsonString, StringEnum } = require("@clusterio/lib");
const { Type } = require("@sinclair/typebox");
class PluginExampleEvent {
static type = "event";
static src = ["host", "control"];
static dst = ["controller", "host", "instance"];
static plugin = "__plugin_name__";
static permission = "__plugin_name__.example.permission.event";
constructor(myString, myNumberArray) {
this.myString = myString;
this.myNumberArray = myNumberArray;
}
static jsonSchema = Type.Object({
"myString": Type.String(),
"myNumberArray": Type.Array(Type.Number()),
});
static fromJSON(json) {
return new PluginExampleEvent(json.myString, json.myNumberArray);
}
}
class PluginExampleRequest {
static type = "request";
static src = ["host", "control"];
static dst = ["controller", "host", "instance"];
static plugin = "__plugin_name__";
static permission = "__plugin_name__.example.permission.request";
constructor(myString, myNumberArray) {
this.myString = myString;
this.myNumberArray = myNumberArray;
}
static jsonSchema = Type.Object({
"myString": Type.String(),
"myNumberArray": Type.Array(Type.Number()),
});
static fromJSON(json) {
return new PluginExampleRequest(json.myString, json.myNumberArray);
}
static Response = plainJson(Type.Object({
"myResponseString": Type.String(),
"myResponseNumbers": Type.Array(Type.Number()),
}));
}
//%if controller & web // Subscribing requires web content and the controller
class ExampleSubscribableValue {
constructor(id, updatedAtMs, isDeleted) {
this.id = id;
this.updatedAtMs = updatedAtMs;
this.isDeleted = isDeleted;
}
static jsonSchema = Type.Object({
id: Type.String(),
updatedAtMs: Type.Number(),
isDeleted: Type.Boolean(),
});
static fromJSON(json) {
return new this(json.id, json.updatedAtMs, json.isDeleted);
}
}
class ExampleSubscribableUpdate {
static type = "event";
static src = "controller";
static dst = "control";
static plugin = "__plugin_name__";
static permission = "__plugin_name__.example.permission.subscribe";
constructor(updates) {
this.updates = updates;
}
static jsonSchema = Type.Object({
"updates": Type.Array(ExampleSubscribableValue.jsonSchema),
});
static fromJSON(json) {
return new this(json.updates.map(update => ExampleSubscribableValue.fromJSON(update)));
}
}
//%endif
module.exports = {
PluginExampleEvent,
PluginExampleRequest,
//%if controller & web // Subscribing requires web content and the controller
ExampleSubscribableValue,
ExampleSubscribableUpdate,
//%endif
};
Whitespace-only changes.
@@ -0,0 +1,93 @@
import React, {
useContext, useEffect, useState,
//%if controller // Subscribing requires web content and the controller
useCallback, useSyncExternalStore,
//%endif
} from "react";
// import {
//
// } from "antd";
import {
BaseWebPlugin, PageLayout, PageHeader, Control, ControlContext, notifyErrorHandler,
} from "@clusterio/web_ui";
//%if multi_context // Subscribing requires multi context
import {
PluginExampleEvent, PluginExampleRequest,
//%endif
//%if controller // Subscribing requires web content and the controller
ExampleSubscribableUpdate, ExampleSubscribableValue,
//%endif
//%if multi_context // Subscribing requires multi context
} from "../messages";
//%endif
import * as lib from "@clusterio/lib";
function MyTemplatePage() {
let control = useContext(ControlContext);
//%if controller // Subscribing requires web content and the controller
const plugin = control.plugins.get("__plugin_name__");
const [subscribableData, synced] = plugin.useSubscribableData();
//%endif
return <PageLayout nav={[{ name: "__plugin_name__" }]}>
<PageHeader title="__plugin_name__" />
//%if controller // Subscribing requires web content and the controller
Synced: {String(synced)} Data: {JSON.stringify([...subscribableData.values()])}
//%endif
</PageLayout>;
}
export class WebPlugin extends BaseWebPlugin {
//%if controller // Subscribing requires web content and the controller
subscribableData = new lib.EventSubscriber(ExampleSubscribableUpdate, this.control);
//%endif
async init() {
this.pages = [
{
path: "/__plugin_name__",
sidebarName: "__plugin_name__",
// This permission is client side only, so it must match the permission string of a resource request to be secure
// An undefined value means that the page will always be visible
//%if controller // Subscribing requires web content and the controller
permission: "__plugin_name__.example.permission.subscribe",
//%endif
//%if !controller
permission: undefined, // "__plugin_name__.example.permission.request",
//%endif
content: <MyTemplatePage/>,
},
];
//%if multi_context
this.control.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.control.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%endif
}
//%if controller// Subscribing requires web content and the controller
useSubscribableData() {
const control = useContext(ControlContext);
const subscribe = useCallback((callback) => this.subscribableData.subscribe(callback), [control]);
return useSyncExternalStore(subscribe, () => this.subscribableData.getSnapshot());
}
//%endif
//%if multi_context
async handlePluginExampleEvent(event) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
}
@@ -0,0 +1,78 @@
import * as lib from "@clusterio/lib";
import { BaseControllerPlugin, InstanceInfo } from "@clusterio/controller";
//%if multi_context // Subscribing requires multi context
import {
PluginExampleEvent, PluginExampleRequest,
//%endif
//%if controller & web // Subscribing requires web content and the controller
ExampleSubscribableUpdate, ExampleSubscribableValue,
//%endif
//%if multi_context // Subscribing requires multi context
} from "./messages";
//%endif
export class ControllerPlugin extends BaseControllerPlugin {
//%if controller & web // Subscribing requires web content and the controller
exampleDatabase!: Map<string, ExampleSubscribableValue>;
storageDirty = false;
//%endif
//%if multi_context // Subscribing requires multi context
async init() {
this.controller.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.controller.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%endif
//%if controller & web // Subscribing requires web content and the controller
this.controller.subscriptions.handle(ExampleSubscribableUpdate, this.handleExampleSubscription.bind(this));
this.exampleDatabase = new Map(); // If needed, replace with loading from database file
//%endif
//%if multi_context // Subscribing requires multi context
}
//%endif
async onControllerConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
this.logger.info(`controller::onControllerConfigFieldChanged ${field}`);
}
//%if instance
async onInstanceConfigFieldChanged(instance: InstanceInfo, field: string, curr: unknown, prev: unknown) {
this.logger.info(`controller::onInstanceConfigFieldChanged ${instance.id} ${field}`);
}
//%endif
async onSaveData() {
this.logger.info("controller::onSaveData");
}
async onShutdown() {
this.logger.info("controller::onShutdown");
}
async onPlayerEvent(instance: InstanceInfo, event: lib.PlayerEvent) {
this.logger.info(`controller::onPlayerEvent ${instance.id} ${JSON.stringify(event)}`);
}
//%if multi_context
async handlePluginExampleEvent(event: PluginExampleEvent) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request: PluginExampleRequest) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
//%if controller & web // Subscribing requires web content and the controller
async handleExampleSubscription(request: lib.SubscriptionRequest) {
const values = [...this.exampleDatabase.values()].filter(
value => value.updatedAtMs > request.lastRequestTimeMs,
);
return values.length ? new ExampleSubscribableUpdate(values) : null;
}
//%endif
}
@@ -0,0 +1,27 @@
import { BaseCtlPlugin, type Control } from "@clusterio/ctl";
import { CommandTree, Command } from "@clusterio/lib";
//%// We do not check for multi context here because it doesn't make sense to have a ctl without messages
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
/* eslint-disable no-console */
const pluginCommands = new CommandTree({
name: "__plugin_name__", description: "The default description I forgot to change",
});
pluginCommands.add(new Command({
definition: ["my-command <foo>", "My command description that I really should change", (yargs) => {
yargs.positional("foo", { describe: "foo is foo!", type: "string" });
}],
handler: async function(args: { foo: string }, control: Control) {
const response = await control.sendTo("controller", new PluginExampleRequest(args.foo, [1, 2, 3]));
console.log(response);
console.log(args);
},
}));
/* eslint-enable no-console */
export class CtlPlugin extends BaseCtlPlugin {
async addCommands(rootCommand: CommandTree) {
rootCommand.add(pluginCommands);
}
}
@@ -0,0 +1,36 @@
import * as lib from "@clusterio/lib";
import { BaseHostPlugin } from "@clusterio/host";
//%if multi_context
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
//%endif
export class HostPlugin extends BaseHostPlugin {
//%if multi_context
async init() {
this.host.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.host.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
}
//%endif
async onHostConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
this.logger.info(`host::onInstanceConfigFieldChanged ${field}`);
}
async onShutdown() {
this.logger.info("host::onShutdown");
}
//%if multi_context
async handlePluginExampleEvent(event: PluginExampleEvent) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request: PluginExampleRequest) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
}
@@ -0,0 +1,149 @@
import * as lib from "@clusterio/lib";
//%if multi_context
import * as Messages from "./messages";
lib.definePermission({
name: "__plugin_name__.example.permission.event",
title: "Example permission event",
description: "Example Description. Event. Change me in index.ts",
});
lib.definePermission({
name: "__plugin_name__.example.permission.request",
title: "Example permission request",
description: "Example Description. Request. Change me in index.ts",
});
//%endif
//%if controller & web // Subscribing requires web content and the controller
lib.definePermission({
name: "__plugin_name__.example.permission.subscribe",
title: "Example permission subscribe",
description: "Example Description. Subscribe. Change me in index.ts",
});
//%endif
//%if web
lib.definePermission({
name: "__plugin_name__.page.view",
title: "Example page view permission",
description: "Example Description. View. Change me in index.ts",
});
//%endif
//%if config
declare module "@clusterio/lib" {
//%endif
//%if controller & config
export interface ControllerConfigFields {
"__plugin_name__.myControllerField": string;
}
//%endif
//%if host & config
export interface HostConfigFields {
"__plugin_name__.myHostField": string;
}
//%endif
//%if instance & config
export interface InstanceConfigFields {
"__plugin_name__.myInstanceField": string;
}
//%endif
//%if ctl & config
export interface ControlConfigFields {
"__plugin_name__.myControlField": string;
}
//%endif
//%if config
}
//%endif
export const plugin: lib.PluginDeclaration = {
name: "__plugin_name__",
title: "__plugin_name__",
description: "Example Description. Plugin. Change me in index.ts",
//%// There are a lot of statements here to make the formatting look good with and without 'config'
//%if controller | host & !config | instance & !config | ctl & !config // Blank line for formatting
//%endif
//%if controller
controllerEntrypoint: "./dist/node/controller",
//%endif
//%if controller & config
controllerConfigFields: {
"__plugin_name__.myControllerField": {
title: "My Controller Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if host & config // Blank line for formatting
//%endif
//%if host
hostEntrypoint: "./dist/node/host",
//%endif
//%if host & config
hostConfigFields: {
"__plugin_name__.myHostField": {
title: "My Host Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if instance & config | module & config // Blank line for formatting
//%endif
//%if instance | module // Modules load an empty instance plugin
instanceEntrypoint: "./dist/node/instance",
//%endif
//%if instance & config
instanceConfigFields: {
"__plugin_name__.myInstanceField": {
title: "My Instance Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if ctl & config // Blank line for formatting
//%endif
//%if ctl
ctlEntrypoint: "./dist/node/ctl",
//%endif
//%if ctl & config
controlConfigFields: {
"__plugin_name__.myControlField": {
title: "My Control Field",
description: "This should be removed from index.js",
type: "string",
initialValue: "Remove Me",
},
},
//%endif
//%if multi_context // Subscribing requires multi context
messages: [
Messages.PluginExampleEvent,
Messages.PluginExampleRequest,
//%endif
//%if controller & web // Subscribing requires web content and the controller
Messages.ExampleSubscribableUpdate,
//%endif
//%if multi_context // Subscribing requires multi context
],
//%endif
//%if web // Web content template has an example route which is the plugin name
webEntrypoint: "./web",
routes: [
"/__plugin_name__",
],
//%endif
};
@@ -0,0 +1,69 @@
import * as lib from "@clusterio/lib";
import { BaseInstancePlugin } from "@clusterio/host";
//%if multi_context
import { PluginExampleEvent, PluginExampleRequest } from "./messages";
//%endif
//%if module
type PuginExampleIPC = {
tick: number,
player_name: string,
};
//%endif
export class InstancePlugin extends BaseInstancePlugin {
//%if multi_context | module
async init() {
//%endif
//%if multi_context
this.instance.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.instance.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%endif
//%if module
this.instance.server.handle("__plugin_name__-plugin_example_ipc", this.handlePluginExampleIPC.bind(this));
//%endif
//%if multi_context | module
}
//%endif
async onInstanceConfigFieldChanged(field: string, curr: unknown, prev: unknown) {
this.logger.info(`instance::onInstanceConfigFieldChanged ${field}`);
}
async onStart() {
this.logger.info("instance::onStart");
}
async onStop() {
this.logger.info("instance::onStop");
}
async onPlayerEvent(event: lib.PlayerEvent) {
this.logger.info(`onPlayerEvent::onPlayerEvent ${JSON.stringify(event)}`);
//%if module
if (this.instance.status === "running") {
this.sendRcon("/sc __plugin_name__.foo()");
}
//%endif
}
//%if multi_context
async handlePluginExampleEvent(event: PluginExampleEvent) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request: PluginExampleRequest) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
//%if module
async handlePluginExampleIPC(event: PuginExampleIPC) {
this.logger.info(JSON.stringify(event));
}
//%endif
}
@@ -0,0 +1,7 @@
import * as lib from "@clusterio/lib";
import { BaseInstancePlugin } from "@clusterio/host";
export class InstancePlugin extends BaseInstancePlugin {
// This class is empty because an instance plugin must be defined for a module to be injected
// This requirement may change in the future to allow for standalone modules
}
@@ -0,0 +1,97 @@
import { plainJson, jsonArray, JsonBoolean, JsonNumber, JsonString, StringEnum } from "@clusterio/lib";
import { Type, Static } from "@sinclair/typebox";
export class PluginExampleEvent {
declare ["constructor"]: typeof PluginExampleEvent;
static type = "event" as const;
static src = ["host", "control"] as const;
static dst = ["controller", "host", "instance"] as const;
static plugin = "__plugin_name__" as const;
static permission = "__plugin_name__.example.permission.event";
constructor(
public myString: string,
public myNumberArray: number[],
) {
}
static jsonSchema = Type.Object({
"myString": Type.String(),
"myNumberArray": Type.Array(Type.Number()),
});
static fromJSON(json: Static<typeof PluginExampleEvent.jsonSchema>) {
return new PluginExampleEvent(json.myString, json.myNumberArray);
}
}
export class PluginExampleRequest {
declare ["constructor"]: typeof PluginExampleRequest;
static type = "request" as const;
static src = ["host", "control"] as const;
static dst = ["controller", "host", "instance"] as const;
static plugin = "__plugin_name__" as const;
static permission = "__plugin_name__.example.permission.request";
constructor(
public myString: string,
public myNumberArray: number[],
) {
}
static jsonSchema = Type.Object({
"myString": Type.String(),
"myNumberArray": Type.Array(Type.Number()),
});
static fromJSON(json: Static<typeof PluginExampleRequest.jsonSchema>) {
return new PluginExampleRequest(json.myString, json.myNumberArray);
}
static Response = plainJson(Type.Object({
"myResponseString": Type.String(),
"myResponseNumbers": Type.Array(Type.Number()),
}));
}
//%if controller & web // Subscribing requires web content and the controller
export class ExampleSubscribableValue {
constructor(
public id: string,
public updatedAtMs: number,
public isDeleted: boolean,
) {
}
static jsonSchema = Type.Object({
id: Type.String(),
updatedAtMs: Type.Number(),
isDeleted: Type.Boolean(),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.id, json.updatedAtMs, json.isDeleted);
}
}
export class ExampleSubscribableUpdate {
declare ["constructor"]: typeof ExampleSubscribableUpdate;
static type = "event" as const;
static src = "controller" as const;
static dst = "control" as const;
static plugin = "__plugin_name__" as const;
static permission = "__plugin_name__.example.permission.subscribe";
constructor(
public updates: ExampleSubscribableValue[],
) { }
static jsonSchema = Type.Object({
"updates": Type.Array(ExampleSubscribableValue.jsonSchema),
});
static fromJSON(json: Static<typeof this.jsonSchema>) {
return new this(json.updates.map(update => ExampleSubscribableValue.fromJSON(update)));
}
}
//%endif
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.browser.json",
"references": [
{ "path": "../../packages/lib/tsconfig.browser.json" },
{ "path": "../../packages/web_ui/tsconfig.browser.json" },
],
"include": [ "web/**/*.tsx", "web/**/*.ts", "messages.ts", "package.json" ],
}
@@ -0,0 +1,9 @@
{
"files": [],
"references": [
//%if web
{ "path": "./tsconfig.browser.json" },
//%endif
{ "path": "./tsconfig.node.json" }
]
}
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.node.json",
"references": [
{ "path": "../../packages/lib/tsconfig.node.json" },
],
"include": ["./**/*.ts"],
"exclude": ["test/*", "./dist/*"],
}
Whitespace-only changes.
@@ -0,0 +1,93 @@
import React, {
useContext, useEffect, useState,
//%if controller & web // Subscribing requires web content and the controller
useCallback, useSyncExternalStore,
//%endif
} from "react";
// import {
//
// } from "antd";
import {
BaseWebPlugin, PageLayout, PageHeader, Control, ControlContext, notifyErrorHandler,
} from "@clusterio/web_ui";
//%if multi_context // Subscribing requires multi context
import {
PluginExampleEvent, PluginExampleRequest,
//%endif
//%if controller & web // Subscribing requires web content and the controller
ExampleSubscribableUpdate, ExampleSubscribableValue,
//%endif
//%if multi_context // Subscribing requires multi context
} from "../messages";
//%endif
import * as lib from "@clusterio/lib";
function MyTemplatePage() {
const control = useContext(ControlContext);
//%if controller // Subscribing requires web content and the controller
const plugin = control.plugins.get("__plugin_name__") as WebPlugin;
const [subscribableData, synced] = plugin.useSubscribableData();
//%endif
return <PageLayout nav={[{ name: "__plugin_name__" }]}>
<PageHeader title="__plugin_name__" />
//%if controller // Subscribing requires web content and the controller
Synced: {String(synced)} Data: {JSON.stringify([...subscribableData.values()])}
//%endif
</PageLayout>;
}
export class WebPlugin extends BaseWebPlugin {
//%if controller // Subscribing requires web content and the controller
subscribableData = new lib.EventSubscriber(ExampleSubscribableUpdate, this.control);
//%endif
async init() {
this.pages = [
{
path: "/__plugin_name__",
sidebarName: "__plugin_name__",
// This permission is client side only, so it must match the permission string of a resource request to be secure
// An undefined value means that the page will always be visible
//%if controller // Subscribing requires web content and the controller
permission: "__plugin_name__.example.permission.subscribe",
//%endif
//%if !controller
permission: undefined, // "__plugin_name__.example.permission.request",
//%endif
content: <MyTemplatePage/>,
},
];
//%if multi_context
this.control.handle(PluginExampleEvent, this.handlePluginExampleEvent.bind(this));
this.control.handle(PluginExampleRequest, this.handlePluginExampleRequest.bind(this));
//%endif
}
//%if controller // Subscribing requires web content and the controller
useSubscribableData() {
const control = useContext(ControlContext);
const subscribe = useCallback((callback: () => void) => this.subscribableData.subscribe(callback), [control]);
return useSyncExternalStore(subscribe, () => this.subscribableData.getSnapshot());
}
//%endif
//%if multi_context
async handlePluginExampleEvent(event: PluginExampleEvent) {
this.logger.info(JSON.stringify(event));
}
async handlePluginExampleRequest(request: PluginExampleRequest) {
this.logger.info(JSON.stringify(request));
return {
myResponseString: request.myString,
myResponseNumbers: request.myNumberArray,
};
}
//%endif
}
+114
View File
@@ -0,0 +1,114 @@
# Templates
Create has the ability to create new plugins from templates to avoid needing to manually copy lots of boiler plate. This folder contains all those templates for both Javascript and Typescript plugins. These templates have custom preprocessor instructions that can be used to alter a template based on developer input, this syntax is described below.
- [The Basics](#the-basics)
- [Insert Instruction](#insert-instruction) (`__variable__`)
- [Conditional Instruction](#conditional-instruction) (`//%if` `//%endif`)
- [Available Variables](#available-variables)
## The Basics
All files can contain preprocessor instructions whether json, js, lua, or anything else. All instructions use the same syntax although it has been designed to work well with js and ts files.
Except for the insert instruction, all preprocessors use a `//%` prefix and must be on their own line. These lines are never included in the output. Parsing is stopped when `//` is encountered to allow for comments on preprocessors. Eg. `//%if foo // this is a comment`.
Empty instructions, aka just comments, can be inserted into templates using `//%//` and are not included in the output file.
`templates/plugin-js` contains all the files which will be used by a javascript only plugin.
`templates/plugin-ts` contains all the files which will be used by a plugin that supports typescript.
`templates/common` contains files used by both types of plugins, this is mostly lua code or root files like `package.json`
Files are not copied via wildcards and instead are explicitly listed within `template.js` so any new templates will need to be added there.
## Insert Instruction
There are a number of template variables available to be inserted into templates. All variables are raw string values, that is they are inserted directly into the output file without any modifications. To signify a place to insert a variable you need only surround its identifier with double underscores, examples below.
Example Variables:
```sh
my_number=1
my_string=foo
my_property=bar: "baz"
```
Example Template:
```ts
const __my_string__: number = __my_number__;
console.log(__my_string__);
console.log({
foo: "Hello, __my_string__!",
__my_property__,
my_property: __my_string__,
});
```
Example Output:
```ts
const foo: number = 1;
console.log(foo);
console.log({
foo: "Hello, foo!",
bar: "baz",
my_property: foo,
});
```
## Conditional Instruction
Conditionals allow for code to be included/omitted based on the value of template variables. Currently only boolean logic in [disjunctive normal form](https://en.wikipedia.org/wiki/Disjunctive_normal_form) is supported and any variables that are not booleans are evaluated with the [Boolean constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean).
Variables used in condition statements do not require double underscores. Their values can be inverted using `!`. Arithmetic and comparison operations are not supported. Parentheses are also not supported, conditions must be in DNF.
Conversion to DNF:
`A & (B | !C)` -> `A & B | A & !C` evaluated as `(A and B) or (A and not C)`
Example Variables:
```sh
my_string=foo
say_hello=true
say_done=true
verbose=false
```
Example Template:
```ts
//%if verbose
console.log("Starting...");
//%endif
//%if say_hello & my_string
console.log("Hello, __my_string__!");
//%endif
//%if say_done | verbose
console.log("Done.");
//%endif
```
Example Output:
```ts
console.log("Hello, foo!");
console.log("Done.");
```
## Available Variables
| Identifier | Example | Description |
| - | - | - |
| typescript | `true` / `false` | `true` if typescript templates are included |
| controller | `true` / `false` | `true` if controller templates are included |
| host | `true` / `false` | `true` if host templates are included |
| instance | `true` / `false` | `true` if instance templates are included |
| module | `true` / `false` | `true` if module templates are included |
| ctl | `true` / `false` | `true` if control templates are included |
| web | `true` / `false` | `true` if web templates are included |
| plugin_name | `my_plugin` | name of the plugin |
| prepare | `tsc --build` | commands to run for the prepare script |
| ext | `ts` / `js` | file extension of source files |
+7 -14
View File
@@ -188,14 +188,7 @@ async function startControl() {
lib.handleUnhandledErrors();
logger.verbose(`Loading available plugins from ${args.pluginList}`);
let pluginList = new Map();
try {
pluginList = new Map(JSON.parse(await fs.readFile(args.pluginList, "utf8")));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
let pluginList = await lib.loadPluginList(args.pluginList);
// If the command is plugin management we don't try to load plugins
if (args._[0] === "plugin") {
@@ -216,19 +209,19 @@ async function startControl() {
.parse() as CtlArguments
;
logger.verbose(`Loading config from ${args.config}`);
let controlConfig;
const controlConfigPath = args.config;
logger.verbose(`Loading config from ${controlConfigPath}`);
try {
const jsonConfig = JSON.parse(await fs.readFile(args.config, "utf8"));
controlConfig = lib.ControlConfig.fromJSON(jsonConfig, "control");
controlConfig = await lib.ControlConfig.fromFile("control", controlConfigPath);
} catch (err: any) {
if (err.code === "ENOENT") {
logger.verbose("Config not found, initializing new config");
controlConfig = new lib.ControlConfig("control");
controlConfig = new lib.ControlConfig("control", undefined, controlConfigPath);
} else {
throw new lib.StartupError(`Failed to load ${args.config}: ${err.message}`);
throw new lib.StartupError(`Failed to load ${args.config}: ${err.stack ?? err.message ?? err}`);
}
}
@@ -239,7 +232,7 @@ async function startControl() {
// Handle the control-config command before trying to connect.
if (args._[0] === "control-config") {
await lib.handleConfigCommand(args, controlConfig, args.config);
await lib.handleConfigCommand(args, controlConfig);
return;
}
+8 -7
View File
@@ -3,7 +3,7 @@
"description": "Clusterio command line management tool",
"author": "Hornwitser",
"license": "MIT",
"version": "2.0.0-alpha.17",
"version": "2.0.0-alpha.21",
"main": "dist/node/index.js",
"scripts": {
"prepare": "tsc --build",
@@ -13,18 +13,19 @@
"clusterioctl": "./dist/node/ctl.js"
},
"keywords": [
"clusterio",
"factorio"
],
"engines": {
"node": ">=18"
},
"dependencies": {
"@clusterio/lib": "^2.0.0-alpha.17",
"@clusterio/lib": "workspace:*",
"as-table": "^1.0.55",
"fs-extra": "^11.1.1",
"fs-extra": "^11.3.0",
"phin": "^3.7.0",
"set-blocking": "^2.0.0",
"winston": "^3.10.0",
"winston": "^3.17.0",
"yargs": "^17.7.2"
},
"publishConfig": {
@@ -32,9 +33,9 @@
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/node": "^20.4.5",
"@types/node": "^20.17.19",
"@types/set-blocking": "^2.0.0",
"@types/yargs": "^17.0.24",
"typescript": "^5.1.6"
"@types/yargs": "^17.0.33",
"typescript": "^5.7.3"
}
}
+233 -3
View File
@@ -95,6 +95,19 @@ controllerCommands.add(new lib.Command({
await control.send(new lib.ControllerRestartRequest());
},
}));
controllerCommands.add(new lib.Command({
definition: ["update", "Update the controller", (yargs) => {
yargs.option("restart", { alias: "r", type: "boolean", description: "Restart after update" });
}],
handler: async function(args: { restart: boolean }, control: Control) {
await control.send(new lib.ControllerUpdateRequest());
if (args.restart) {
await control.send(new lib.ControllerRestartRequest());
} else {
print("Controller updated; a restart is required to apply the changes.");
}
},
}));
const controllerConfigCommands = new lib.CommandTree({
name: "config", alias: ["c"], description: "controller config management",
@@ -234,6 +247,35 @@ controllerPluginCommands.add(new lib.Command({
print(asTable(response.body));
},
}));
controllerPluginCommands.add(new lib.Command({
definition: ["update <plugin>", "Update a plugin on the controller", (yargs) => {
yargs.positional("plugin", { describe: "Plugin to update", type: "string" });
yargs.option("restart", { alias: "r", type: "boolean", description: "Restart after update" });
}],
handler: async function(args: { plugin: string, restart: boolean }, control: Control) {
await control.sendTo("controller", new lib.PluginUpdateRequest(args.plugin));
if (args.restart) {
await control.send(new lib.ControllerRestartRequest());
} else {
print("Plugin updated; a restart is required to apply the changes.");
}
},
}));
controllerPluginCommands.add(new lib.Command({
definition: ["install <plugin>", "Install a plugin on the controller", (yargs) => {
yargs.positional("plugin", { describe: "Plugin to install", type: "string" });
yargs.option("restart", { alias: "r", type: "boolean", description: "Restart after update" });
}],
handler: async function(args: { plugin: string, restart: boolean }, control: Control) {
await control.sendTo("controller", new lib.PluginInstallRequest(args.plugin));
if (args.restart) {
await control.send(new lib.ControllerRestartRequest());
} else {
print("Plugin installed; a restart is required to apply the changes.");
}
},
}));
controllerCommands.add(controllerPluginCommands);
@@ -322,6 +364,22 @@ hostCommands.add(new lib.Command({
}
},
}));
hostCommands.add(new lib.Command({
definition: ["update <host>", "Update the host", (yargs) => {
yargs.positional("host", { describe: "Host to update", type: "string" });
yargs.option("restart", { alias: "r", type: "boolean", description: "Restart after update" });
}],
handler: async function(args: { host: string, restart: boolean }, control: Control) {
let hostId = await lib.resolveHost(control, args.host);
await control.sendTo({ hostId }, new lib.HostUpdateRequest());
if (args.restart) {
await control.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Host updated; a restart is required to apply the changes.");
}
},
}));
const hostConfigCommands = new lib.CommandTree({
name: "config", alias: ["c"], description: "Host config management",
@@ -464,9 +522,60 @@ hostConfigCommands.add(new lib.Command({
}
},
}));
hostCommands.add(hostConfigCommands);
const hostPluginCommands = new lib.CommandTree({
name: "plugin", alias: ["p"], description: "host plugin inspection",
});
hostPluginCommands.add(new lib.Command({
definition: ["list <host>", "List plugins on a host", (yargs) => {
yargs.positional("host", { describe: "Host to list for", type: "string" });
}],
handler: async function(args: { host: string }, control: Control) {
let hostId = await lib.resolveHost(control, args.host);
const plugins = await control.sendTo({ hostId }, new lib.PluginListRequest());
print(asTable(plugins.map(p => ({
title: p.title, version: p.version, loaded: p.loaded, enabled: p.enabled, npmPackage: p.npmPackage,
}))));
},
}));
hostPluginCommands.add(new lib.Command({
definition: ["update <host> <plugin>", "Update a plugin on a host", (yargs) => {
yargs.positional("host", { describe: "Host to update on", type: "string" });
yargs.positional("plugin", { describe: "Plugin to update", type: "string" });
yargs.option("restart", { alias: "r", type: "boolean", description: "Restart after update" });
}],
handler: async function(args: { host: string, plugin: string, restart: boolean }, control: Control) {
let hostId = await lib.resolveHost(control, args.host);
await control.sendTo({ hostId }, new lib.PluginUpdateRequest(args.plugin));
if (args.restart) {
await control.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Plugin updated; a restart is required to apply the changes.");
}
},
}));
hostPluginCommands.add(new lib.Command({
definition: ["install <host> <plugin>", "Install a plugin on a host", (yargs) => {
yargs.positional("host", { describe: "Host to install on", type: "string" });
yargs.positional("plugin", { describe: "Plugin to install", type: "string" });
yargs.option("restart", { alias: "r", type: "boolean", description: "Restart after update" });
}],
handler: async function(args: { host: string, plugin: string, restart: boolean }, control: Control) {
let hostId = await lib.resolveHost(control, args.host);
await control.sendTo({ hostId }, new lib.PluginInstallRequest(args.plugin));
if (args.restart) {
await control.sendTo({ hostId }, new lib.HostRestartRequest());
} else {
print("Plugin installed; a restart is required to apply the changes.");
}
},
}));
hostCommands.add(hostPluginCommands);
const instanceCommands = new lib.CommandTree({
name: "instance", alias: ["i"], description: "Instance management",
});
@@ -484,16 +593,21 @@ instanceCommands.add(new lib.Command({
yargs.positional("name", { describe: "Instance name", type: "string" });
yargs.options({
"id": { type: "number", nargs: 1, describe: "Instance id" },
"from": { type: "number", nargs: 1, describe: "Clone config from instance id" },
});
}],
handler: async function(args: { name: string, id?: number }, control: Control) {
handler: async function(args: { name: string, id?: number, from?: number, }, control: Control) {
let instanceConfig = new lib.InstanceConfig("control");
if (args.id !== undefined) {
instanceConfig.set("instance.id", args.id);
}
instanceConfig.set("instance.name", args.name);
const serializedConfig = instanceConfig.toRemote("controller");
await control.send(new lib.InstanceCreateRequest(serializedConfig));
await control.send(new lib.InstanceCreateRequest(
instanceConfig.toRemote("controller", [
"instance.id", "instance.name",
]),
args.from
));
},
}));
@@ -1836,6 +1950,122 @@ userCommands.add(new lib.Command({
},
}));
async function handleImportOrRestore(args: {
filepath: string,
users: boolean,
bans: boolean,
admins: boolean,
whitelist: boolean,
noBackup?: boolean,
}, control: Control, restore: boolean) {
let importType: ConstructorParameters<typeof lib.UserBulkImportRequest>[0];
const data = JSON.parse((await fs.readFile(args.filepath)).toString());
const optionCount = [args.users, args.bans, args.admins, args.whitelist]
.reduce((acc, bool) => (bool ? acc + 1 : acc), 0);
if (optionCount > 1) {
throw new lib.CommandError("Can not specify multiple options");
}
// Assign based on options or attempt to guess based on filename
const filename = path.basename(args.filepath);
if (args.users || (optionCount === 0 && data.export_version)) {
importType = "users";
} else if (args.bans || (optionCount === 0 && filename.includes("ban"))) {
importType = "bans";
} else if (args.admins || (optionCount === 0 && filename.includes("admin"))) {
importType = "admins";
} else if (args.whitelist || (optionCount === 0 && filename.includes("whitelist"))) {
importType = "whitelist";
} else {
throw new lib.CommandError("Unknown json file, please specify an option");
}
const backup = await control.send(
new lib.UserBulkImportRequest(importType, importType === "users" ? data.users : data, restore)
);
print(`${restore ? "Restored" : "Imported"} ${importType} from ${args.filepath}`);
if (restore && backup && !args.noBackup) {
await fs.writeJSON(`${importType}-backup.json`, backup, { spaces: 2 });
print(`Wrote backup to ${importType}-backup.json`);
}
}
userCommands.add(new lib.Command({
definition: ["import <filepath>", "Import user data from a file", (yargs) => {
yargs.positional("filepath", { describe: "Path to the file to import", type: "string" });
yargs.options({
"users": { describe: "Import users json", nargs: 0, type: "boolean", default: false },
"bans": { describe: "Import banlist json", nargs: 0, type: "boolean", default: false },
"admins": { describe: "Import adminlist json", nargs: 0, type: "boolean", default: false },
"whitelist": { describe: "Import whitelist json", nargs: 0, type: "boolean", default: false },
});
}],
handler: async function(args: any, control: any) {
await handleImportOrRestore(args, control, false);
},
}));
userCommands.add(new lib.Command({
definition: ["restore <filepath>", "Restore user data from a file", (yargs) => {
yargs.positional("filepath", { describe: "Path to the file to import", type: "string" });
yargs.options({
"users": { describe: "Restore users json", nargs: 0, type: "boolean", default: false },
"bans": { describe: "Restore banlist json", nargs: 0, type: "boolean", default: false },
"admins": { describe: "Restore adminlist json", nargs: 0, type: "boolean", default: false },
"whitelist": { describe: "Restore whitelist json", nargs: 0, type: "boolean", default: false },
"no-backup": { describe: "Don't save a backup to the cwd", nargs: 0, type: "boolean", default: false },
});
}],
handler: async function(args: any, control: any) {
await handleImportOrRestore(args, control, true);
},
}));
userCommands.add(new lib.Command({
definition: ["export <filepath>", "Export user data to a file", (yargs) => {
yargs.positional("filepath", { describe: "Path to the file to save to", type: "string" });
yargs.options({
"users": { describe: "Export users json", nargs: 0, type: "boolean", default: false },
"bans": { describe: "Export banlist json", nargs: 0, type: "boolean", default: false },
"admins": { describe: "Export adminlist json", nargs: 0, type: "boolean", default: false },
"whitelist": { describe: "Export whitelist json", nargs: 0, type: "boolean", default: false },
});
}],
handler: async function(args: {
filepath: string,
users: boolean,
bans: boolean,
admins: boolean,
whitelist: boolean,
}, control: Control) {
let exportType = "users" as ConstructorParameters<typeof lib.UserBulkExportRequest>[0];
const optionCount = [args.bans, args.admins, args.whitelist]
.reduce((acc, bool) => (bool ? acc + 1 : acc), 0);
if (optionCount > 1) {
throw new lib.CommandError("Can not specify multiple options");
}
// Assign based on options or attempt to guess based on filename
if (args.users) {
exportType = "users";
} else if (args.bans) {
exportType = "bans";
} else if (args.admins) {
exportType = "admins";
} else if (args.whitelist) {
exportType = "whitelist";
}
fs.writeJSON(args.filepath, await control.send(
new lib.UserBulkExportRequest(exportType)
), { spaces: 2 });
print(`Exported ${exportType} to ${args.filepath}`);
},
}));
const logCommands = new lib.CommandTree({ name: "log", description: "Log inspection" });
logCommands.add(new lib.Command({
definition: ["follow", "follow cluster log", (yargs) => {
-3
View File
@@ -4,7 +4,4 @@
{ "path": "../lib/tsconfig.node.json" },
],
"include": ["index.ts", "package.json", "ctl.ts", "src/**/*.ts"],
"compilerOptions": {
"outDir": "dist/node",
},
}
+19 -18
View File
@@ -120,6 +120,10 @@ async function startHost() {
type: "boolean", nargs: 0, default: false,
describe: "Indicate that a process monitor will restart this host on failure",
});
yargs.option("recovery", {
type: "boolean", nargs: 0, default: false,
describe: "Start the host in recovery mode with all plugins disabled and controller disconnected",
});
})
.demandCommand(1, "You need to specify a command to run")
.strict()
@@ -144,17 +148,13 @@ async function startHost() {
let command = args._[0];
if (command === "run") {
logger.info(`Starting Clusterio host ${version}`);
if (args.recovery) {
logger.warn("Host recovery mode enabled. Some features will be disabled.");
}
}
logger.info(`Loading available plugins from ${args.pluginList}`);
let pluginList = new Map();
try {
pluginList = new Map(JSON.parse(await fs.readFile(args.pluginList, "utf8")));
} catch (err: any) {
if (err.code !== "ENOENT") {
throw err;
}
}
let pluginList = await lib.loadPluginList(args.pluginList);
// If the command is plugin management we don't try to load plugins
if (command === "plugin") {
@@ -167,29 +167,30 @@ async function startHost() {
lib.registerPluginMessages(pluginInfos);
lib.addPluginConfigFields(pluginInfos);
logger.info(`Loading config from ${args.config}`);
let hostConfig;
const hostConfigPath = args.config;
logger.info(`Loading config from ${hostConfigPath}`);
try {
const jsonConfig = JSON.parse(await fs.readFile(args.config, "utf8"));
hostConfig = lib.HostConfig.fromJSON(jsonConfig, "host");
hostConfig = await lib.HostConfig.fromFile("host", hostConfigPath);
} catch (err: any) {
if (err.code === "ENOENT") {
logger.info("Config not found, initializing new config");
hostConfig = new lib.HostConfig("host");
hostConfig = new lib.HostConfig("host", undefined, hostConfigPath);
} else {
throw new lib.StartupError(`Failed to load ${args.config}: ${err.message}`);
throw new lib.StartupError(`Failed to load ${args.config}: ${err.stack ?? err.message ?? err}`);
}
}
hostConfig.set("host.version", version); // Allows tracking last loaded version
if (command === "config") {
await lib.handleConfigCommand(args, hostConfig, args.config);
await lib.handleConfigCommand(args, hostConfig);
return;
}
// If we get here the command was run
await fs.ensureDir(hostConfig.get("host.instances_directory"));
await fs.ensureDir(hostConfig.get("host.mods_directory"));
await fs.ensureDir("modules");
@@ -228,11 +229,11 @@ async function startHost() {
let hostConnector = new HostConnector(hostConfig, tlsCa, pluginInfos);
host = new Host(
hostConnector,
args.config,
hostConfig,
tlsCa,
pluginInfos,
Boolean(args.canRestart),
Boolean(args.recovery),
...await Host.bootstrap(hostConfig)
);
@@ -293,8 +294,8 @@ I version of clusterio. Expect things to break. I
`
);
startHost().catch(err => {
if (err instanceof lib.AuthenticationFailed) {
logger.fatal(err.message);
if (err instanceof lib.WebSocketError) {
// Already handled by connector.on("error") within Host
} else if (err instanceof lib.StartupError) {
logger.fatal(`
+3 -2
View File
@@ -1,4 +1,5 @@
-- Important: Keep this API in sync with modules/clusterio/api.lua
local compat = require("compat")
local api = {}
@@ -48,7 +49,7 @@ function api.send_json(channel, data)
return "\\x" .. string.format("%02x", match:byte())
end)
data = game.table_to_json(data)
data = compat.table_to_json(data)
-- If there's more than about 4kB of data users running with the Windows
-- console open will start to experience stuttering, otherwise we could
@@ -58,7 +59,7 @@ function api.send_json(channel, data)
else
local file_no = remote.call("clusterio_api", "get_file_no")
local file_name = "clst_" .. file_no .. ".json"
game.write_file(file_name, data, false, 0)
compat.write_file(file_name, data, false, 0)
print("\f$ipc:" .. channel .. "?f" .. file_name)
end
end
@@ -1,4 +1,10 @@
---------------------------------------------------------------------------------------------------
Version: 2.0.20
Date: 25. 02. 2025
Features:
- Added support for Factorio 2.0 and space-age
- Add compatibility library with support for factorio 0.17 - 2.0
---------------------------------------------------------------------------------------------------
Version: 0.1.2
Features:
- Added support for Factorio 1.1.
+10 -4
View File
@@ -2,16 +2,20 @@
"name": "clusterio_lib",
"variants": [
{
"version": "0.1.0",
"version": "2.0.17",
"factorio_version": "0.17"
},
{
"version": "0.1.1",
"version": "2.0.18",
"factorio_version": "0.18"
},
{
"version": "0.1.2",
"version": "2.0.11",
"factorio_version": "1.1"
},
{
"version": "2.0.20",
"factorio_version": "2.0"
}
],
"title": "Clusterio Library (Alpha)",
@@ -19,6 +23,8 @@
"homepage": "https://github.com/clusterio/clusterio",
"description": "Library for interfacing with Clusterio. Warning: work in progress alpha.",
"additional_files": {
"serialize.lua": ["..", "..", "modules", "clusterio", "serialize.lua"]
"compat.lua": ["..", "..", "modules", "clusterio", "compat.lua"],
"serialize.lua": ["..", "..", "modules", "clusterio", "serialize.lua"],
"json.lua": ["..", "..", "modules", "clusterio", "json.lua"]
}
}
+4 -3
View File
@@ -1,11 +1,12 @@
local function send_json(channel, data)
data = game.table_to_json(data)
data = helpers and helpers.table_to_json(data) or game.table_to_json(data)
print("\f$ipc:" .. channel .. "?j" .. data)
end
script.on_init(function()
send_json("mod_list", game.active_mods)
for name, mod_setting in pairs(game.mod_setting_prototypes) do
send_json("mod_list", script.active_mods)
local mod_settings = prototypes and prototypes.mod_setting or game.mod_setting_prototypes
for name, mod_setting in pairs(mod_settings) do
send_json("mod_setting_mod", { name = name, mod = mod_setting.mod })
end
end)
+9 -6
View File
@@ -1,6 +1,8 @@
-- Important: Keep this API in sync with mod/api.lua
local compat = require("modules/clusterio/compat")
local api = {}
clusterio_patch_number = clusterio_patch_number or 0 --- @type number
api.events = {
on_instance_updated = script.generate_event_name(),
@@ -8,11 +10,11 @@ api.events = {
}
function api.get_instance_name()
return global.clusterio.instance_name
return compat.script_data.clusterio.instance_name
end
function api.get_instance_id()
return global.clusterio.instance_id
return compat.script_data.clusterio.instance_id
end
function api.send_json(channel, data)
@@ -23,7 +25,7 @@ function api.send_json(channel, data)
return "\\x" .. string.format("%02x", match:byte())
end)
data = game.table_to_json(data)
data = compat.table_to_json(data)
-- If there's more than about 4kB of data users running with the Windows
-- console open will start to experience stuttering, otherwise we could
@@ -31,9 +33,10 @@ function api.send_json(channel, data)
if #data < 4000 then
print("\f$ipc:" .. channel .. "?j" .. data)
else
global.clusterio_file_no = (global.clusterio_file_no or 0) + 1
local file_name = "clst_" .. global.clusterio_file_no .. ".json"
game.write_file(file_name, data, false, 0)
local script_data = compat.script_data
script_data.clusterio_file_no = (script_data.clusterio_file_no or 0) + 1
local file_name = "clst_" .. script_data.clusterio_file_no .. ".json"
compat.write_file(file_name, data, false, 0)
print("\f$ipc:" .. channel .. "?f" .. file_name)
end
end
+221
View File
@@ -0,0 +1,221 @@
--[[
Adds various functions used to ensure compatibly between version including some polyfills.
Implementation Guidelines
- If a function is only available at runtime in one version, then all versions must be made to error.
- Where possible, directly assign the function / table to the member of compat.
- When not possible, then it should replace itself with a direct assignment asap.
- Versions are handled in ascending order covering all supported versions and ending with an error.
]]
local lib_json = require("json")
--- @diagnostic disable: deprecated
--- @class LibCompat
--- @field script_data table
--- @field prototypes LuaPrototypes
--- @field active_mods table<string, string>
--- @field write_file fun(filename: string, data: LocalisedString, append: boolean?, for_player: uint?)
--- @field table_to_json fun(data: table): string
--- @field json_to_table fun(json: string): AnyBasic?
local compat = {}
--- @class LibCompatMt
--- @package
local compat_mt = {}
--- @alias VersionTable { [1]: number, [2]: number, [3]: number }
--- Concert a version string into a version table
--- @param version string
--- @return VersionTable
local function version_to_table(version)
local index = 0
local rtn = { 0, 0, 0 }
for part in version:gmatch("%d+") do
index = index + 1
rtn[index] = tonumber(part)
end
return rtn
end
--- The current version of factorio, the earliest supported is 0.17.69
local current_version_str = "0.17.69"
local current_version = version_to_table(current_version_str)
do
local success, base_version = pcall(function()
return script.active_mods.base
end)
if success then
current_version_str = base_version
current_version = version_to_table(current_version_str)
end
end
--- Returns true if the current version is equal to the provided version
--- @param version string Version to compare against
--- @return boolean # True if versions are equal
function compat.version_eq(version)
local version_tbl = version_to_table(version)
for i = 1, 3 do
if version_tbl[i] ~= current_version[i] then
return false
end
end
return true
end
--- Returns true if the current version is greater than or equal to the the provided version
--- @param version string Version to compare against
--- @return boolean # True if the version is greater or equal to provided
function compat.version_ge(version)
local version_tbl = version_to_table(version)
for i = 1, 3 do
if version_tbl[i] > current_version[i] then
return false
elseif version_tbl[i] < current_version[i] then
return true
end
end
return true
end
--- Returns true if the current version is less than or equal to the the provided version
--- @param version string Version to compare against
--- @return boolean # True if the version is less or equal to provided
function compat.version_le(version)
local version_tbl = version_to_table(version)
for i = 1, 3 do
if version_tbl[i] < current_version[i] then
return false
elseif version_tbl[i] > current_version[i] then
return true
end
end
return true
end
--- The major versions of factorio we support
local v2 = compat.version_ge("2.0")
local v1 = compat.version_ge("1.0") and not compat.version_ge("2.0")
local v0 = compat.version_ge("0.17") and not compat.version_ge("1.0")
--- The minor versions of factorio we support
local v2_0 = compat.version_ge("2.0")
local v1_1 = compat.version_ge("1.1") and not compat.version_ge("2.0")
local v1_0 = compat.version_ge("1.0") and not compat.version_ge("1.1")
local v0_18 = compat.version_ge("0.18") and not compat.version_ge("1.0")
local v0_17 = compat.version_ge("0.17") and not compat.version_ge("0.18")
--- Raise an error because the current version is unsupported
local function unsupported_version()
error("Unsupported version: " .. current_version_str, 3)
end
--- Raise an error because the function is only available at runtime
local function runtime_only()
error("To maintain compatibly, this value can only be used during runtime.", 3)
end
--- Keys of this table will raise an unsupported error when accessed
local unsupported_properties = {}
--- Keys of this table can only be accessed at runtime
--- The value is a function which returns the real value
local runtime_properties = {}
--- Handles the indexing of the tables above
function compat_mt:__index(key)
if unsupported_properties[key] then
return unsupported_version()
elseif runtime_properties[key] then
if game == nil then
return runtime_only()
end
local value = runtime_properties[key]()
if value == nil then
return unsupported_version()
end
rawset(self, key, value)
return value
end
end
--- Prototype data contains all the definitions for the game, it is only available during runtime
function runtime_properties.prototypes()
if v0 or v1 then
--- Lazy loading of prototype data
--- Iteration is UB, we can fix this once someone actually needs it
return setmetatable({}, {
__index = function(self, key)
local value = game[key .. "_prototypes"]
rawset(self, key, value)
return value
end
})
elseif v2 then
return prototypes
end
end
--- List of all active mods
function runtime_properties.active_mods()
if v0_17 then
return game.active_mods
elseif v0 or v1 or v2 then
return script.active_mods
end
end
--- Writes a string to file
function runtime_properties.write_file()
if v0 or v1 then
return game.write_file
elseif v2 then
return helpers.write_file
end
end
--- Convert tables to and from json, we have backported this to work before runtime
if v0 or v1 then
compat.table_to_json = function(tbl)
if game then
compat.table_to_json = game.table_to_json
return game.table_to_json(tbl)
end
return lib_json.encode(tbl)
end
compat.json_to_table = function(json)
if game then
compat.json_to_table = game.json_to_table
return game.json_to_table(json)
end
return lib_json.decode(json)
end
elseif v2 then
compat.table_to_json = helpers.table_to_json
compat.json_to_table = helpers.json_to_table
else
unsupported_properties.table_to_json = true
unsupported_properties.json_to_table = true
end
--- Script data refers to lua data which is persisted between loads
--- This is called immediately, during init and during load
local function set_script_data()
if v0 or v1 then
--- @diagnostic disable-next-line
compat.script_data = global
elseif v2 then
compat.script_data = storage
else
unsupported_properties.script_data = true
end
end
set_script_data()
compat.on_init = set_script_data --- @package
compat.on_load = set_script_data --- @package
return setmetatable(compat, compat_mt)
+39 -11
View File
@@ -1,10 +1,14 @@
local api = require('modules/clusterio/api')
local compat = require("modules/clusterio/compat")
--- @class (exact) EventData.on_server_startup:EventData
local function check_patch()
if global.clusterio_patch_number ~= clusterio_patch_number then
global.clusterio_patch_number = clusterio_patch_number
script.raise_event(api.events.on_server_startup, {})
if compat.script_data.clusterio_patch_number ~= clusterio_patch_number then
compat.script_data.clusterio_patch_number = clusterio_patch_number
script.raise_event(api.events.on_server_startup, {
name = api.events.on_server_startup, tick = game.tick
})
end
end
@@ -14,8 +18,8 @@ impl.events = {}
impl.events[defines.events.on_tick] = check_patch
impl.events[api.events.on_server_startup] = function()
if not global.clusterio then
global.clusterio = {
if not compat.script_data.clusterio then
compat.script_data.clusterio = {
instance_id = nil,
instance_name = nil,
}
@@ -46,12 +50,35 @@ impl.events[defines.events.on_player_left_game] = function(event)
})
end
impl.events[defines.events.on_player_banned] = function(event)
api.send_json("player_event", {
type = "BAN",
name = event.player_name,
reason = event.reason,
})
end
impl.events[defines.events.on_player_unbanned] = function(event)
api.send_json("player_event", { type = "UNBANNED", name = event.player_name })
end
impl.events[defines.events.on_player_promoted] = function(event)
local player = game.players[event.player_index]
api.send_json("player_event", { type = "PROMOTE", name = player.name })
end
impl.events[defines.events.on_player_demoted] = function(event)
local player = game.players[event.player_index]
api.send_json("player_event", { type = "DEMOTE", name = player.name })
end
-- Internal API
clusterio_private = {}
function clusterio_private.update_instance(new_id, new_name)
check_patch()
global.clusterio.instance_id = new_id
global.clusterio.instance_name = new_name
local script_data = compat.script_data
script_data.clusterio.instance_id = new_id
script_data.clusterio.instance_name = new_name
script.raise_event(api.events.on_instance_updated, {
instance_id = new_id,
instance_name = new_name,
@@ -71,16 +98,17 @@ remote.add_interface('clusterio_api', {
end,
get_instance_id = function()
return global.clusterio.instance_id
return compat.script_data.clusterio.instance_id
end,
get_instance_name = function()
return global.clusterio.instance_name
return compat.script_data.clusterio.instance_name
end,
get_file_no = function()
global.clusterio_file_no = (global.clusterio_file_no or 0) + 1
return global.clusterio_file_no
local script_data = compat.script_data
script_data.clusterio_file_no = (script_data.clusterio_file_no or 0) + 1
return script_data.clusterio_file_no
end,
})
+388
View File
@@ -0,0 +1,388 @@
--
-- json.lua / https://github.com/rxi/json.lua
--
-- Copyright (c) 2020 rxi
--
-- 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.
--
local json = { _version = "0.1.2" }
-------------------------------------------------------------------------------
-- Encode
-------------------------------------------------------------------------------
local encode
local escape_char_map = {
[ "\\" ] = "\\",
[ "\"" ] = "\"",
[ "\b" ] = "b",
[ "\f" ] = "f",
[ "\n" ] = "n",
[ "\r" ] = "r",
[ "\t" ] = "t",
}
local escape_char_map_inv = { [ "/" ] = "/" }
for k, v in pairs(escape_char_map) do
escape_char_map_inv[v] = k
end
local function escape_char(c)
return "\\" .. (escape_char_map[c] or string.format("u%04x", c:byte()))
end
local function encode_nil(val)
return "null"
end
local function encode_table(val, stack)
local res = {}
stack = stack or {}
-- Circular reference?
if stack[val] then error("circular reference") end
stack[val] = true
if rawget(val, 1) ~= nil or next(val) == nil then
-- Treat as array -- check keys are valid and it is not sparse
local n = 0
for k in pairs(val) do
if type(k) ~= "number" then
error("invalid table: mixed or invalid key types")
end
n = n + 1
end
if n ~= #val then
error("invalid table: sparse array")
end
-- Encode
for i, v in ipairs(val) do
table.insert(res, encode(v, stack))
end
stack[val] = nil
return "[" .. table.concat(res, ",") .. "]"
else
-- Treat as an object
for k, v in pairs(val) do
if type(k) ~= "string" then
error("invalid table: mixed or invalid key types")
end
table.insert(res, encode(k, stack) .. ":" .. encode(v, stack))
end
stack[val] = nil
return "{" .. table.concat(res, ",") .. "}"
end
end
local function encode_string(val)
return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"'
end
local function encode_number(val)
-- Check for NaN, -inf and inf
if val ~= val or val <= -math.huge or val >= math.huge then
error("unexpected number value '" .. tostring(val) .. "'")
end
return string.format("%.14g", val)
end
local type_func_map = {
[ "nil" ] = encode_nil,
[ "table" ] = encode_table,
[ "string" ] = encode_string,
[ "number" ] = encode_number,
[ "boolean" ] = tostring,
}
encode = function(val, stack)
local t = type(val)
local f = type_func_map[t]
if f then
return f(val, stack)
end
error("unexpected type '" .. t .. "'")
end
function json.encode(val)
return ( encode(val) )
end
-------------------------------------------------------------------------------
-- Decode
-------------------------------------------------------------------------------
local parse
local function create_set(...)
local res = {}
for i = 1, select("#", ...) do
res[ select(i, ...) ] = true
end
return res
end
local space_chars = create_set(" ", "\t", "\r", "\n")
local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",")
local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u")
local literals = create_set("true", "false", "null")
local literal_map = {
[ "true" ] = true,
[ "false" ] = false,
[ "null" ] = nil,
}
local function next_char(str, idx, set, negate)
for i = idx, #str do
if set[str:sub(i, i)] ~= negate then
return i
end
end
return #str + 1
end
local function decode_error(str, idx, msg)
local line_count = 1
local col_count = 1
for i = 1, idx - 1 do
col_count = col_count + 1
if str:sub(i, i) == "\n" then
line_count = line_count + 1
col_count = 1
end
end
error( string.format("%s at line %d col %d", msg, line_count, col_count) )
end
local function codepoint_to_utf8(n)
-- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa
local f = math.floor
if n <= 0x7f then
return string.char(n)
elseif n <= 0x7ff then
return string.char(f(n / 64) + 192, n % 64 + 128)
elseif n <= 0xffff then
return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128)
elseif n <= 0x10ffff then
return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128,
f(n % 4096 / 64) + 128, n % 64 + 128)
end
error( string.format("invalid unicode codepoint '%x'", n) )
end
local function parse_unicode_escape(s)
local n1 = tonumber( s:sub(1, 4), 16 )
local n2 = tonumber( s:sub(7, 10), 16 )
-- Surrogate pair?
if n2 then
return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000)
else
return codepoint_to_utf8(n1)
end
end
local function parse_string(str, i)
local res = ""
local j = i + 1
local k = j
while j <= #str do
local x = str:byte(j)
if x < 32 then
decode_error(str, j, "control character in string")
elseif x == 92 then -- `\`: Escape
res = res .. str:sub(k, j - 1)
j = j + 1
local c = str:sub(j, j)
if c == "u" then
local hex = str:match("^[dD][89aAbB]%x%x\\u%x%x%x%x", j + 1)
or str:match("^%x%x%x%x", j + 1)
or decode_error(str, j - 1, "invalid unicode escape in string")
res = res .. parse_unicode_escape(hex)
j = j + #hex
else
if not escape_chars[c] then
decode_error(str, j - 1, "invalid escape char '" .. c .. "' in string")
end
res = res .. escape_char_map_inv[c]
end
k = j + 1
elseif x == 34 then -- `"`: End of string
res = res .. str:sub(k, j - 1)
return res, j + 1
end
j = j + 1
end
decode_error(str, i, "expected closing quote for string")
end
local function parse_number(str, i)
local x = next_char(str, i, delim_chars)
local s = str:sub(i, x - 1)
local n = tonumber(s)
if not n then
decode_error(str, i, "invalid number '" .. s .. "'")
end
return n, x
end
local function parse_literal(str, i)
local x = next_char(str, i, delim_chars)
local word = str:sub(i, x - 1)
if not literals[word] then
decode_error(str, i, "invalid literal '" .. word .. "'")
end
return literal_map[word], x
end
local function parse_array(str, i)
local res = {}
local n = 1
i = i + 1
while 1 do
local x
i = next_char(str, i, space_chars, true)
-- Empty / end of array?
if str:sub(i, i) == "]" then
i = i + 1
break
end
-- Read token
x, i = parse(str, i)
res[n] = x
n = n + 1
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "]" then break end
if chr ~= "," then decode_error(str, i, "expected ']' or ','") end
end
return res, i
end
local function parse_object(str, i)
local res = {}
i = i + 1
while 1 do
local key, val
i = next_char(str, i, space_chars, true)
-- Empty / end of object?
if str:sub(i, i) == "}" then
i = i + 1
break
end
-- Read key
if str:sub(i, i) ~= '"' then
decode_error(str, i, "expected string for key")
end
key, i = parse(str, i)
-- Read ':' delimiter
i = next_char(str, i, space_chars, true)
if str:sub(i, i) ~= ":" then
decode_error(str, i, "expected ':' after key")
end
i = next_char(str, i + 1, space_chars, true)
-- Read value
val, i = parse(str, i)
-- Set
res[key] = val
-- Next token
i = next_char(str, i, space_chars, true)
local chr = str:sub(i, i)
i = i + 1
if chr == "}" then break end
if chr ~= "," then decode_error(str, i, "expected '}' or ','") end
end
return res, i
end
local char_func_map = {
[ '"' ] = parse_string,
[ "0" ] = parse_number,
[ "1" ] = parse_number,
[ "2" ] = parse_number,
[ "3" ] = parse_number,
[ "4" ] = parse_number,
[ "5" ] = parse_number,
[ "6" ] = parse_number,
[ "7" ] = parse_number,
[ "8" ] = parse_number,
[ "9" ] = parse_number,
[ "-" ] = parse_number,
[ "t" ] = parse_literal,
[ "f" ] = parse_literal,
[ "n" ] = parse_literal,
[ "[" ] = parse_array,
[ "{" ] = parse_object,
}
parse = function(str, idx)
local chr = str:sub(idx, idx)
local f = char_func_map[chr]
if f then
return f(str, idx)
end
decode_error(str, idx, "unexpected character '" .. chr .. "'")
end
function json.decode(str)
if type(str) ~= "string" then
error("expected argument of type string, got " .. type(str))
end
local res, idx = parse(str, next_char(str, 1, space_chars, true))
idx = next_char(str, idx, space_chars, true)
if idx <= #str then
decode_error(str, idx, "trailing garbage")
end
return res
end
return json
+4 -1
View File
@@ -2,5 +2,8 @@
"name": "clusterio",
"version": "0.0.0",
"dependencies": {},
"load": ["impl.lua"]
"load": [
"impl.lua",
"compat.lua"
]
}
+29 -31
View File
@@ -1,48 +1,28 @@
-- Library for serializing items in Factorio
-- Based on code from playerManager and trainTeleports
local compat = require("modules/clusterio/compat")
local serialize = {}
local function version_to_table(version)
local t = {}
for p in string.gmatch(version, "%d+") do
t[#t + 1] = tonumber(p)
end
return t
end
-- 0.17 compatibility
local supports_bar, get_bar, set_bar, version
if (pcall(function() local mods = script.active_mods end)) then
local supports_bar, get_bar, set_bar
if compat.version_ge("1.0.0") then
supports_bar = "supports_bar"
get_bar = "get_bar"
set_bar = "set_bar"
version = version_to_table(script.active_mods.base)
else
supports_bar = "hasbar"
get_bar = "getbar"
set_bar = "setbar"
version = version_to_table("0.17.69")
end
-- returns true if the game version is greater than or equal to the given version
local function version_ge(comp)
comp = version_to_table(comp)
for i=1, 3 do
if comp[i] > version[i] then
return false
elseif comp[i] < version[i] then
return true
end
end
return true
end
local has_create_grid = version_ge("1.1.7")
local has_create_grid = compat.version_ge("1.1.7")
local has_quality = compat.version_ge("2.0.0")
-- Equipment Grids are serialized into an array of equipment entries
-- where ench entry is a table with the following fields:
-- n: name
-- g: ghost name (optional)
-- t: to_be_removed (optional)
-- p: position (array of 2 numbers corresponding to x and y)
-- s: shield (optional)
-- e: energy (optional)
@@ -51,6 +31,8 @@ local has_create_grid = version_ge("1.1.7")
-- r: result inventory
-- b: currently burning (optional)
-- f: remaining_burning_fuel (optional)
-- If the equipment has a quality the following is also present:
-- q: quality (optional)
function serialize.serialize_equipment_grid(grid)
local serialized = {}
local processed = {}
@@ -66,6 +48,11 @@ function serialize.serialize_equipment_grid(grid)
n = equipment.name,
p = {pos.x, pos.y},
}
if equipment.type == "equipment-ghost" then
if equipment.ghost_name then
entry.g = equipment.ghost_name
end
end
if equipment.shield > 0 then entry.s = equipment.shield end
if equipment.energy > 0 then entry.e = equipment.energy end
if equipment.burner then
@@ -77,6 +64,13 @@ function serialize.serialize_equipment_grid(grid)
entry.f = burner.remaining_burning_fuel
end
end
if equipment.quality then
entry.q = equipment.quality.name
end
if equipment.to_be_removed then
entry.t = equipment.to_be_removed
end
log(entry)
table.insert(serialized, entry)
end
end
@@ -89,8 +83,10 @@ function serialize.deserialize_equipment_grid(grid, serialized)
grid.clear()
for _, entry in ipairs(serialized) do
local equipment = grid.put({
name = entry.n,
name = entry.g or entry.n,
quality = has_quality and entry.q or nil,
position = entry.p,
ghost = entry.g and true or nil,
})
if equipment then
if entry.s then equipment.shield = entry.s end
@@ -109,6 +105,7 @@ end
-- Item stacks are serialized into a table with the following fields:
-- n: name
-- c: count
-- q: quality (optional)
-- h: health (optional)
-- d: durability (optional)
-- a: ammo count (optional)
@@ -141,8 +138,9 @@ function serialize.serialize_item_stack(slot, entry)
entry.n = slot.name
entry.c = slot.count
if has_quality and slot.quality.level ~= 0 then entry.q = slot.quality.name end
if slot.health < 1 then entry.h = slot.health end
if slot.durability then entry.d = slot.durability end
if slot.type == "tool" and slot.durability then entry.d = slot.durability end
if slot.type == "ammo" then entry.a = slot.ammo end
if slot.is_item_with_label then
local label = {}
@@ -178,6 +176,7 @@ function serialize.deserialize_item_stack(slot, entry)
name = entry.n,
count = entry.c,
}
if entry.q then item_stack.quality = entry.q end
if entry.h then item_stack.health = entry.h end
if entry.d then item_stack.durability = entry.d end
if entry.a then item_stack.ammo = entry.a end
@@ -244,7 +243,7 @@ function serialize.serialize_inventory(inventory)
end
if item.n or item.f or item.e then
local item_serialized = game.table_to_json(item)
local item_serialized = compat.table_to_json(item)
if item_serialized == previous_serialized then
local previous_item = serialized.i[#serialized.i]
previous_item.r = (previous_item.r or 0) + 1
@@ -305,5 +304,4 @@ function serialize.deserialize_inventory(inventory, serialized)
end
end
return serialize
+10 -9
View File
@@ -3,7 +3,7 @@
"description": "Implementation of Clusterio host server",
"author": "Hornwitser",
"license": "MIT",
"version": "2.0.0-alpha.17",
"version": "2.0.0-alpha.21",
"main": "dist/node/index.js",
"scripts": {
"prepare": "tsc --build",
@@ -13,23 +13,24 @@
"clusteriohost": "./dist/node/host.js"
},
"keywords": [
"clusterio",
"factorio"
],
"engines": {
"node": ">=18"
},
"dependencies": {
"@clusterio/lib": "^2.0.0-alpha.17",
"@clusterio/lib": "workspace:*",
"@sinclair/typebox": "^0.30.4",
"fs-extra": "^11.1.1",
"fs-extra": "^11.3.0",
"jimp": "^0.22.8",
"jszip": "^3.10.1",
"phin": "^3.7.0",
"pidusage": "^3.0.2",
"rcon-client": "^4.2.3",
"semver": "^7.5.4",
"rcon-client": "^4.2.5",
"semver": "^7.7.1",
"set-blocking": "^2.0.0",
"winston": "^3.10.0",
"winston": "^3.17.0",
"winston-daily-rotate-file": "^4.7.1",
"yargs": "^17.7.2"
},
@@ -38,11 +39,11 @@
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/node": "^20.4.5",
"@types/node": "^20.17.19",
"@types/pidusage": "^2.0.2",
"@types/semver": "^7.5.0",
"@types/set-blocking": "^2.0.0",
"@types/yargs": "^17.0.24",
"typescript": "^5.1.6"
"@types/yargs": "^17.0.33",
"typescript": "^5.7.3"
}
}
+99 -37
View File
@@ -43,19 +43,20 @@ async function discoverInstances(instancesDir: string) {
let instanceInfos = new Map<number, { path: string, config: lib.InstanceConfig }>();
for (let entry of await fs.readdir(instancesDir, { withFileTypes: true })) {
if (entry.isDirectory()) {
let instanceConfig = new lib.InstanceConfig("host");
let configPath = path.join(instancesDir, entry.name, "instance.json");
let instanceConfig = new lib.InstanceConfig("host", Host.instanceConfigWarning);
const instancePath = path.join(instancesDir, entry.name);
const configPath = path.join(instancePath, "instance.json");
try {
const jsonConfig = JSON.parse(await fs.readFile(configPath, "utf8"));
instanceConfig = lib.InstanceConfig.fromJSON(jsonConfig, "host");
instanceConfig = await lib.InstanceConfig.fromFile("host", configPath);
instanceConfig.update(Host.instanceConfigWarning, false); // File might be missing the warning
} catch (err: any) {
if (err.code === "ENOENT") {
continue; // Ignore folders without config.json
}
logger.error(`Error occured while parsing ${configPath}: ${err.message}`);
logger.error(`Error occurred while parsing ${configPath}: ${err.message}`);
continue;
}
@@ -64,7 +65,6 @@ async function discoverInstances(instancesDir: string) {
continue;
}
let instancePath = path.join(instancesDir, entry.name);
logger.verbose(`found instance ${instanceConfig.get("instance.name")} in ${instancePath}`);
instanceInfos.set(instanceConfig.get("instance.id"), {
path: instancePath,
@@ -140,7 +140,7 @@ export class HostRouter {
return false;
}
origin.connector.sendResponseError(
new lib.ResponseError("Instance is not running."), message.src
new lib.ResponseError(msg || "Instance is not running."), message.src
);
}
return true;
@@ -258,7 +258,6 @@ export default class Host extends lib.Link {
*/
tlsCa?: string;
pluginInfos: lib.PluginNodeEnvInfo[];
configPath: string;
config: lib.HostConfig;
/** Mapping of plugin name to loaded plugin */
@@ -279,6 +278,10 @@ export default class Host extends lib.Link {
_disconnecting = false;
_shuttingDown = false;
static instanceConfigWarning = {
"_warning": "Changes to this file will be overwritten by the controller's copy.",
};
static async bootstrap(hostConfig: lib.HostConfig) {
const modsDirectory = hostConfig.get("host.mods_directory");
await fs.ensureDir(modsDirectory);
@@ -289,7 +292,6 @@ export default class Host extends lib.Link {
constructor(
connector: HostConnector,
hostConfigPath: string,
hostConfig: lib.HostConfig,
tlsCa: string | undefined,
pluginInfos: lib.PluginNodeEnvInfo[],
@@ -298,13 +300,17 @@ export default class Host extends lib.Link {
* will restart this host on a non-zero exit codes.
*/
public canRestart = false,
/**
* If true indicates the host is in recovery mode and should
* disable certain actions such as loading plugins or instance autostart
*/
public recoveryMode = false,
public modStore = new lib.ModStore(hostConfig.get("host.mods_directory"), new Map()),
) {
super(connector);
this.tlsCa = tlsCa;
this.pluginInfos = pluginInfos;
this.configPath = hostConfigPath;
this.config = hostConfig;
this.config.on("fieldChanged", (name, curr, prev) => {
@@ -346,13 +352,17 @@ export default class Host extends lib.Link {
logger.fatal(`Unexpected error reconnecting to controller:\n${err.stack}`);
return this.shutdown();
});
} else {
logger.fatal("Controller connection was unexpectedly closed");
this.shutdown();
}
});
this.connector.on("error", err => {
logger.fatal(err.message);
this.shutdown();
});
for (let event of ["connect", "drop", "resume", "close"] as const) {
this.connector.on(event, () => {
let message = new lib.ControllerConnectionEvent(event);
@@ -367,17 +377,23 @@ export default class Host extends lib.Link {
this.handle(lib.HostStopRequest, this.handleHostStopRequest.bind(this));
this.handle(lib.HostRestartRequest, this.handleHostRestartRequest.bind(this));
this.handle(lib.HostUpdateRequest, this.handleHostUpdateRequest.bind(this));
this.handle(lib.HostConfigGetRequest, this.handleHostConfigGetRequest.bind(this));
this.handle(lib.HostConfigSetFieldRequest, this.handleHostConfigSetFieldRequest.bind(this));
this.handle(lib.HostConfigSetPropRequest, this.handleHostConfigSetPropRequest.bind(this));
this.handle(lib.HostMetricsRequest, this.handleHostMetricsRequest.bind(this));
this.handle(lib.SyncUserListsEvent, this.handleSyncUserListsEvent.bind(this));
this.handle(lib.SystemInfoRequest, this.handleSystemInfoRequest.bind(this));
this.handle(lib.PluginListRequest, this.handlePluginListRequest.bind(this));
this.handle(lib.PluginUpdateRequest, this.handlePluginUpdateRequest.bind(this));
this.handle(lib.PluginInstallRequest, this.handlePluginInstallRequest.bind(this));
this.snoopEvent(lib.InstanceAdminlistUpdateEvent, this.handleAdminlistUpdateEvent.bind(this));
this.snoopEvent(lib.InstanceBanlistUpdateEvent, this.handleBanlistUpdateEvent.bind(this));
this.snoopEvent(lib.InstanceWhitelistUpdateEvent, this.handleWhitelistUpdateEvent.bind(this));
this.handle(lib.InstanceAssignInternalRequest, this.handleInstanceAssignInternalRequest.bind(this));
this.handle(lib.InstanceUnassignInternalRequest, this.handleInstanceUnassignInternalRequest.bind(this));
this.handle(lib.SystemInfoRequest, this.handleSystemInfoRequest.bind(this));
this.handle(lib.HostMetricsRequest, this.handleHostMetricsRequest.bind(this));
this.fallbackRequest(
lib.InstanceSaveDetailsListRequest, this.fallbackInstanceSaveDetailsListRequest.bind(this),
);
@@ -399,6 +415,11 @@ export default class Host extends lib.Link {
continue;
}
if (this.recoveryMode) {
logger.warn(`Recovery | force disabled plugin ${pluginInfo.name}`);
continue;
}
let HostPluginClass = BaseHostPlugin;
try {
if (pluginInfo.hostEntrypoint) {
@@ -614,11 +635,12 @@ export default class Host extends lib.Link {
}
async fetchMods(mods: Iterable<lib.ModRecord>) {
// This is better than the previous hard coded names
// But it really shouldn't be a hard coded version either
const builtinModNames = lib.ModPack.getBuiltinModNames("2.0");
const modInfos: Promise<lib.ModInfo>[] = [];
for (const mod of mods) {
// XXX These mods ship with Factorio, but the list of mods may
// change and should not be hardcoded here.
if (["core", "base"].includes(mod.name)) {
if (builtinModNames.includes(mod.name)) {
continue;
}
modInfos.push(this.fetchMod(mod));
@@ -636,16 +658,18 @@ export default class Host extends lib.Link {
} else {
instanceInfo = this.discoveredInstanceInfos.get(instanceId);
if (instanceInfo) {
await Instance.populate_folders(instanceInfo.path);
instanceInfo.config.update(config, true, "controller");
} else {
let instanceConfig = new lib.InstanceConfig("host");
let instanceConfig = new lib.InstanceConfig("host", Host.instanceConfigWarning);
instanceConfig.update(config, false, "controller");
let instanceDir = await this._createNewInstanceDir(instanceConfig.get("instance.name"));
instanceConfig.filepath = path.join(instanceDir, "instance.json");
logger.info(`Creating ${instanceDir}`);
await Instance.create(instanceDir, this.config.get("host.factorio_directory"));
await Instance.populate_folders(instanceDir);
instanceInfo = {
path: instanceDir,
config: instanceConfig,
@@ -667,6 +691,9 @@ export default class Host extends lib.Link {
instanceId,
instanceConnection ? instanceConnection.instance.status : "stopped",
instanceConnection ? instanceConnection.instance.server.gamePort : this.gamePort(instanceId),
instanceConnection
? instanceConnection.instance.server.version
: instanceInfo.config.get("factorio.version"),
)
);
@@ -674,18 +701,7 @@ export default class Host extends lib.Link {
await this.sendSaveListUpdate(instanceId, path.join(instanceInfo.path, "saves"));
// save a copy of the instance config
await this.saveInstanceConfig(instanceInfo);
}
async saveInstanceConfig(instanceInfo: { path: string, config: lib.InstanceConfig }) {
let warnedOutput = {
_warning: "Changes to this file will be overwritten by the controller's copy.",
...instanceInfo.config.toJSON(),
};
await lib.safeOutputFile(
path.join(instanceInfo.path, "instance.json"),
JSON.stringify(warnedOutput, null, "\t")
);
await instanceInfo.config.save();
}
async handleInstanceUnassignInternalRequest(request: lib.InstanceUnassignInternalRequest) {
@@ -736,7 +752,7 @@ export default class Host extends lib.Link {
}
assignGamePort(instanceId: number) {
const availablePorts = lib.parseRanges(this.config.get("host.factorio_port_range"), 1, 2**16 - 1);
const availablePorts = lib.parseRanges(this.config.get("host.factorio_port_range"), 1, 2 ** 16 - 1);
for (const [id, instance] of this.instanceInfos) {
if (id === instanceId) {
continue;
@@ -789,7 +805,19 @@ export default class Host extends lib.Link {
}
async handleSystemInfoRequest() {
return lib.gatherSystemInfo(this.config.get("host.id"), this.canRestart);
if (!this.config.restartRequired) {
// If a restart isn't already required, then test if a new version is installed
try {
const runningVersion = this.config.get("host.version");
const packageJson = await fs.readJSON(path.join(__dirname, "..", "package.json"));
if (runningVersion !== packageJson.version) {
this.config.restartRequired = true;
}
} catch (err: any) {
logger.warn(`Failed to read package json:\n${err.stack ?? err.message}`);
}
}
return lib.gatherSystemInfo(this.config.get("host.id"), this.canRestart, this.config.restartRequired);
}
async handleHostMetricsRequest() {
@@ -1034,6 +1062,35 @@ export default class Host extends lib.Link {
await fs.remove(instanceInfo.path);
}
async handleHostUpdateRequest(request: lib.HostUpdateRequest) {
if (!this.config.get("host.allow_remote_updates")) {
throw new lib.RequestError("Remote updates are disabled on this machine");
}
return lib.updatePackage("@clusterio/host");
}
async handlePluginUpdateRequest(request: lib.PluginUpdateRequest) {
if (!this.config.get("host.allow_plugin_updates")) {
throw new lib.RequestError("Plugin updates are disabled on this machine");
}
return await lib.handlePluginUpdate(request.pluginPackage, this.pluginInfos);
}
async handlePluginInstallRequest(request: lib.PluginInstallRequest) {
if (!this.config.get("host.allow_plugin_install")) {
throw new lib.RequestError("Plugin installs are disabled on this machine");
}
return await lib.handlePluginInstall(request.pluginPackage);
}
async handlePluginListRequest(request: lib.PluginListRequest) {
return this.pluginInfos.map(pluginInfo => lib.PluginDetails.fromNodeEnvInfo(
pluginInfo,
this.plugins.has(pluginInfo.name),
this.config.get(`${pluginInfo.name}.load_plugin` as keyof lib.HostConfigFields) as boolean,
));
}
sendHostUpdate() {
this.send(
new lib.HostInfoUpdateEvent(
@@ -1060,6 +1117,7 @@ export default class Host extends lib.Link {
instanceInfo.config.toRemote("controller"),
instanceConnection ? instanceConnection.instance.status : "stopped",
instanceConnection ? instanceConnection.instance.server.gamePort : this.gamePort(instanceId),
instanceConnection ? instanceConnection.instance.server.version : undefined,
));
}
await this.send(new lib.InstancesUpdateRequest(list));
@@ -1070,6 +1128,10 @@ export default class Host extends lib.Link {
for (let [instanceId, instanceInfo] of this.instanceInfos) {
if (instanceInfo.config.get("instance.auto_start")) {
if (this.recoveryMode) {
logger.warn(`Recovery | skipping auto startup for ${instanceInfo.config.get("instance.name")}`);
continue;
}
try {
let instanceConnection = await this._connectInstance(instanceId);
await instanceConnection.instance.handleInstanceStartRequest(new lib.InstanceStartRequest());
@@ -1121,11 +1183,11 @@ export default class Host extends lib.Link {
}
logger.info("Saving config");
await lib.safeOutputFile(this.configPath, JSON.stringify(this.config, null, "\t"));
for (const instanceInfo of this.instanceInfos.values()) {
// Save in case host_assigned_game_port changed in the config.
await this.saveInstanceConfig(instanceInfo);
}
await this.config.save();
// Save host side in case host_assigned_game_port changed in the config.
await Promise.all(
[...this.instanceInfos.values()].map(instanceInfo => instanceInfo.config.save())
);
try {
// Clear silly interval in pidfile library.
+253 -62
View File
@@ -4,6 +4,8 @@ import pidusage from "pidusage";
import phin from "phin";
import util from "util";
import type { Static } from "@sinclair/typebox";
import { exec } from "child_process";
const execAsync = util.promisify(exec);
// internal libraries
import * as lib from "@clusterio/lib";
@@ -14,6 +16,12 @@ import { exportData } from "./export";
import type Host from "./Host";
import BaseInstancePlugin from "./BaseInstancePlugin";
const scriptCommands = [
"/cheat", "/editor",
"/command", "/c",
"/measured-command", "/mc",
"/silent-command", "/sc",
];
const instanceRconCommandDuration = new lib.Histogram(
"clusterio_instance_rcon_command_duration_seconds",
@@ -91,6 +99,38 @@ const serverSettingsActions = {
},
};
/**
* Get process stats for a Windows process
* Due to wmic being removed in later versions of Windows, pidusage does not work
* until this PR is merged: https://github.com/soyuka/pidusage/pull/143
* @param pid - Process ID
* @param logger - Logger instance
* @returns Process stats
*/
async function getWindowsProcessStats(pid: number, logger: lib.Logger) {
try {
const { stdout } = await execAsync(
`powershell "Get-Process -Id ${pid} | Select-Object TotalProcessorTime,WorkingSet | ConvertTo-Json"`
);
const stats = JSON.parse(stdout);
// Convert ticks (100-nanosecond intervals) to milliseconds
const totalTimeMs = stats.TotalProcessorTime.Ticks / 10000;
return {
ctime: totalTimeMs,
memory: stats.WorkingSet,
};
} catch (err) {
logger.warn("Failed to get process stats:", err);
return { ctime: 0, memory: 0 };
}
}
async function getProcessStats(pid: number, logger: lib.Logger) {
if (process.platform === "win32") {
return getWindowsProcessStats(pid, logger);
}
return pidusage(pid);
}
/**
* Keeps track of the runtime parameters of an instance
@@ -127,7 +167,7 @@ export default class Instance extends lib.Link {
_playerCheckInterval: ReturnType<typeof setInterval> | undefined;
_hadPlayersOnline = false;
_playerAutosaveSlot = 1;
_expectedUserUpdates: { action: string, name: string, reason: string }[] = [];
constructor(
host: Host,
@@ -153,7 +193,9 @@ export default class Instance extends lib.Link {
this._configFieldChanged = (field: string, curr: unknown, prev: unknown) => {
let hook = () => lib.invokeHook(this.plugins, "onInstanceConfigFieldChanged", field, curr, prev);
if (field === "factorio.settings") {
if (field === "factorio.shutdown_timeout") {
this.server.shutdownTimeoutMs = curr as number * 1000;
} else if (field === "factorio.settings") {
this.updateFactorioSettings(curr as any, prev as any).finally(hook);
} else if (field === "factorio.enable_whitelist") {
this.updateFactorioWhitelist(curr as any).finally(hook);
@@ -169,14 +211,17 @@ export default class Instance extends lib.Link {
let serverOptions = {
logger: this.logger,
version: this.config.get("factorio.version"),
executablePath: this.config.get("factorio.executable_path") ?? undefined,
gamePort: this.config.get("factorio.game_port") ?? host.assignGamePort(this.id),
rconPort: this.config.get("factorio.rcon_port") ?? undefined,
rconPassword: this.config.get("factorio.rcon_password") ?? undefined,
enableWhitelist: this.config.get("factorio.enable_whitelist"),
enableAuthserverBans: this.config.get("factorio.enable_authserver_bans"),
verboseLogging: this.config.get("factorio.verbose_logging"),
consoleLogging: this.config.get("factorio.console_logging"),
stripPaths: this.config.get("factorio.strip_paths"),
maxConcurrentCommands: this.config.get("factorio.max_concurrent_commands"),
shutdownTimeoutMs: this.config.get("factorio.shutdown_timeout") * 1000,
};
this.server = new FactorioServer(
@@ -214,6 +259,19 @@ export default class Instance extends lib.Link {
this._recordPlayerJoin(event.name);
} else if (event.type === "leave") {
this._recordPlayerLeave(event.name, event.reason);
} else if (["BAN", "UNBANNED", "PROMOTE", "DEMOTE"].includes(event.type)) {
this._recordUserUpdate(event.type, event.name, event.reason);
} else {
this.logger.warn(`Unknown type from player event ipc: ${event.type}`);
}
});
this.server.on("whitelist-change", (added: string[], removed: string[]) => {
for (const player of added) {
this._recordUserUpdate("WHITELISTED", player);
}
for (const player of removed) {
this._recordUserUpdate("UNWHITELISTED", player);
}
});
@@ -236,13 +294,15 @@ export default class Instance extends lib.Link {
this.handle(lib.InstanceSendRconRequest, this.handleInstanceSendRconRequest.bind(this));
}
_watchPlayerJoinsByChat() {
_watchServerLogActions() {
this.server.on("output", (parsed: lib.ParsedFactorioOutput, line: string) => {
if (parsed.type !== "action") {
return;
}
let name = /^([^ ]+)/.exec(parsed.message)![1];
// Detect player leave and join
if (parsed.action === "JOIN") {
this._recordPlayerJoin(name);
} else if (["LEAVE", "KICK", "BAN"].includes(parsed.action)) {
@@ -253,6 +313,14 @@ export default class Instance extends lib.Link {
}[parsed.action]!;
this._recordPlayerLeave(name, reason);
}
// Detect banned and admin state change, whitelist changes are not logged
if (parsed.action === "BAN") {
const reason = /\. Reason: (.+)\.$/.exec(parsed.message)![1];
this._recordUserUpdate(parsed.action, name, reason !== "unspecified" ? reason : "");
} else if (["UNBANNED", "PROMOTE", "DEMOTE"].includes(parsed.action)) {
this._recordUserUpdate(parsed.action as "UNBANNED" | "PROMOTE" | "DEMOTE", name);
}
});
// Leave log entries are unreliable and sometimes don't show up.
@@ -263,6 +331,22 @@ export default class Instance extends lib.Link {
}, 60e3);
}
// Needed to cover promote/demote when player is not on map, not required when _watchServerLogActions is used
_watchPlayerPromote() {
this.server.on("output", (parsed: lib.ParsedFactorioOutput, line: string) => {
if (
parsed.type !== "action"
|| !["PROMOTE", "DEMOTE"].includes(parsed.action)
|| !parsed.message.includes("be promoted upon joining the game.")
) {
return;
}
const name = /^([^ ]+)/.exec(parsed.message)![1];
this._recordUserUpdate(parsed.action as "PROMOTE" | "DEMOTE", name);
});
}
async _checkOnlinePlayers() {
if (this.playersOnline.size) {
let actualPlayers = (await this.sendRcon("/players online"))
@@ -342,7 +426,11 @@ local players = {}
for _, p in pairs(game.players) do
players[p.name] = p.online_time
end
rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
if helpers ~= nil then
rcon.print(helpers.table_to_json(players))
else
rcon.print(game.table_to_json(players))
end`.replace(/\r?\n/g, " ");
let playerTimes: Record<string, number> = JSON.parse(await this.sendRcon(exportPlayerTimes));
let count = 0;
@@ -367,6 +455,17 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
}
async sendRcon(message: string, expectEmpty = false, plugin = "") {
const trimmedMessage = message.trim();
if (
!this.config.get("factorio.enable_script_commands")
&& scriptCommands.find(cmd => trimmedMessage.startsWith(cmd))
) {
throw new Error(
"Attempted to use script command while disabled. See config factorio.enable_script_commands.\n" +
`Command: ${message}`
);
}
let instanceId = String(this.id);
let observeDuration = instanceRconCommandDuration.labels(instanceId).startTimer();
try {
@@ -453,6 +552,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
this.id,
status,
this.server.gamePort,
status === "running"? this.server.version : this.config.get("factorio.version"),
),
);
}
@@ -469,6 +569,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
notifyExit() {
this._loadedSave = null;
this.notifyStatus("stopped");
this.connector.emit("close");
this.config.off("fieldChanged", this._configFieldChanged);
clearTimeout(this._playerCheckInterval);
@@ -554,6 +655,11 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
continue;
}
if (this._host.recoveryMode) {
this.logger.warn(`Recovery | force disabled plugin ${pluginInfo.name}`);
continue;
}
try {
await this._loadPlugin(pluginInfo, this._host);
} catch (err) {
@@ -577,11 +683,32 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
* entries from the given settings object.
*
* @param overrides - Server settings to override.
* @param includeCredentials - Include Factorio username and token from host/controller config.
* @returns
* server example settings with the given settings applied over it.
*/
async resolveServerSettings(overrides: Record<string, unknown>) {
async resolveServerSettings(overrides: Record<string, unknown>, includeCredentials: boolean) {
let serverSettings = await this.server.exampleSettings();
if (includeCredentials && overrides.username === undefined && overrides.token === undefined) {
let credentials = {
username: this._host.config.get("host.factorio_username") ?? undefined,
token: this._host.config.get("host.factorio_token") ?? undefined,
};
if (!credentials.username && !credentials.token) {
Object.assign(credentials, await this.sendTo("controller", new lib.GetFactorioCredentialsRequest()));
if (credentials.username || credentials.token) {
this.logger.info("Using Factorio credentials from controller config");
} else {
this.logger.warn("No Factorio credentials found");
}
} else {
this.logger.info("Using Factorio credentials from host config");
}
if (credentials.username) { serverSettings.username = credentials.username; }
if (credentials.token) { serverSettings.token = credentials.token; }
} else if (includeCredentials) {
this.logger.info("Using Factorio credentials from instance config");
}
for (let [key, value] of Object.entries(overrides)) {
if (!Object.hasOwnProperty.call(serverSettings, key)) {
@@ -599,12 +726,14 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
* Generate the server-settings.json file from the example file in the
* data directory and override any settings configured in the instance's
* factorio_settings config entry.
*
* @param includeCredentials - Include Factorio username and token from host/controller config.
*/
async writeServerSettings() {
async writeServerSettings(includeCredentials: boolean) {
const warning = "Changes to this file will be overwitten by the factorio.settings config on the instance.";
const serverSettings = {
"_comment_warning": warning,
...await this.resolveServerSettings(this.config.get("factorio.settings")),
...await this.resolveServerSettings(this.config.get("factorio.settings"), includeCredentials),
};
await lib.safeOutputFile(
this.server.writePath("server-settings.json"),
@@ -615,14 +744,12 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
/**
* Creates a new empty instance directory
*
* Creates the neccessary files for starting up a new instance into the
* provided instance directory.
* Ensures the neccessary files for starting up a new instance into the
* provided instance directory are present.
*
* @param instanceDir -
* Directory to create the new instance into.
* @param factorioDir - Path to factorio installation.
*/
static async create(instanceDir: string, factorioDir: string) {
static async populate_folders(instanceDir: string) {
await fs.ensureDir(path.join(instanceDir, "script-output"));
await fs.ensureDir(path.join(instanceDir, "saves"));
}
@@ -708,9 +835,10 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
*/
async prepare() {
this.logger.verbose("Writing server-settings.json");
await this.writeServerSettings();
await this.writeServerSettings(true);
this._expectedUserUpdates = [];
if (this.config.get("factorio.sync_adminlist")) {
if (this.config.get("factorio.sync_adminlist") !== "disabled") {
this.logger.verbose("Writing server-adminlist.json");
lib.safeOutputFile(
this.server.writePath("server-adminlist.json"),
@@ -718,7 +846,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
);
}
if (this.config.get("factorio.sync_banlist")) {
if (this.config.get("factorio.sync_banlist") !== "disabled") {
this.logger.verbose("Writing server-banlist.json");
lib.safeOutputFile(
this.server.writePath("server-banlist.json"),
@@ -728,7 +856,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
);
}
if (this.config.get("factorio.sync_whitelist")) {
if (this.config.get("factorio.sync_whitelist") !== "disabled") {
this.logger.verbose("Writing server-whitelist.json");
lib.safeOutputFile(
this.server.writePath("server-whitelist.json"),
@@ -831,11 +959,12 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
this._loadedSave = saveName;
await this.server.start(saveName);
if (this.config.get("factorio.enable_save_patching")) {
if (this.config.get("factorio.enable_save_patching") && this.config.get("factorio.enable_script_commands")) {
await this.server.disableAchievements();
await this.updateInstanceData();
this._watchPlayerPromote();
} else {
this._watchPlayerJoinsByChat();
this._watchServerLogActions();
}
await this.sendSaveListUpdate();
@@ -862,7 +991,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
this.server.on("exit", () => this.notifyExit());
await this.server.startScenario(scenario, seed, mapGenSettings, mapSettings);
this._watchPlayerJoinsByChat();
this._watchServerLogActions();
await lib.invokeHook(this.plugins, "onStart");
@@ -878,8 +1007,8 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
}
async updateFactorioSettings(current: Record<string, unknown>, previous: Record<string, unknown>) {
current = await this.resolveServerSettings(current);
previous = await this.resolveServerSettings(previous);
current = await this.resolveServerSettings(current, false);
previous = await this.resolveServerSettings(previous, false);
for (let [key, action] of Object.entries(serverSettingsActions)) {
if (current[key] !== undefined && !util.isDeepStrictEqual(current[key], previous[key])) {
@@ -899,7 +1028,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
await this.sendRcon("/whitelist disable");
}
if (this.config.get("factorio.sync_whitelist")) {
if (this.config.get("factorio.sync_whitelist") !== "disabled") {
await this.sendRcon("/whitelist clear");
for (let player of this._host.whitelist) {
await this.sendRcon(`/whitelist ${player}`);
@@ -911,33 +1040,81 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
}
}
_recordUserUpdate(
action: "BAN" | "UNBANNED" | "PROMOTE" | "DEMOTE" | "WHITELISTED" | "UNWHITELISTED",
name: string,
reason: string = ""
) {
const addr = lib.Address.fromShorthand("allInstances");
const expectedIndex = this._expectedUserUpdates.findIndex(
expected => expected.name === name && expected.action === action && expected.reason === reason
);
if (expectedIndex >= 0) {
this._expectedUserUpdates.splice(expectedIndex, 1);
} else if (action === "BAN") {
if (this.config.get("factorio.sync_banlist") !== "bidirectional") { return; }
this.sendTo(addr, new lib.InstanceBanlistUpdateEvent(name, true, reason ?? ""));
} else if (action === "UNBANNED") {
if (this.config.get("factorio.sync_banlist") !== "bidirectional") { return; }
this.sendTo(addr, new lib.InstanceBanlistUpdateEvent(name, false, ""));
} else if (action === "PROMOTE") {
if (this.config.get("factorio.sync_adminlist") !== "bidirectional") { return; }
this.sendTo(addr, new lib.InstanceAdminlistUpdateEvent(name, true));
} else if (action === "DEMOTE") {
if (this.config.get("factorio.sync_adminlist") !== "bidirectional") { return; }
this.sendTo(addr, new lib.InstanceAdminlistUpdateEvent(name, false));
} else if (action === "WHITELISTED") {
if (this.config.get("factorio.sync_whitelist") !== "bidirectional") { return; }
this.sendTo(addr, new lib.InstanceWhitelistUpdateEvent(name, true));
} else if (action === "UNWHITELISTED") {
if (this.config.get("factorio.sync_whitelist") !== "bidirectional") { return; }
this.sendTo(addr, new lib.InstanceWhitelistUpdateEvent(name, false));
} else {
throw new Error(`Unexpected Action: ${action} for ${name}`);
}
}
async handleInstanceAdminlistUpdateEvent(request: lib.InstanceAdminlistUpdateEvent) {
if (!this.config.get("factorio.sync_adminlist")) {
const { name, admin } = request;
const sync = this.config.get("factorio.sync_adminlist");
if (sync === "disabled") {
return;
} else if (sync === "bidirectional") {
this._expectedUserUpdates.push({action: admin ? "PROMOTE" : "DEMOTE", name: name, reason: "" });
}
let { name, admin } = request;
let command = admin ? `/promote ${name}` : `/demote ${name}`;
await this.sendRcon(command);
}
async handleInstanceBanlistUpdateEvent(request: lib.InstanceBanlistUpdateEvent) {
if (!this.config.get("factorio.sync_banlist")) {
const { name, banned, reason } = request;
const sync = this.config.get("factorio.sync_banlist");
if (sync === "disabled") {
return;
} else if (sync === "bidirectional") {
this._expectedUserUpdates.push({action: banned ? "BAN" : "UNBANNED", name: name, reason: reason});
}
let { name, banned, reason } = request;
let command = banned ? `/ban ${name} ${reason}` : `/unban ${name}`;
await this.sendRcon(command);
}
async handleInstanceWhitelistUpdateEvent(request: lib.InstanceWhitelistUpdateEvent) {
if (!this.config.get("factorio.sync_whitelist")) {
const { name, whitelisted } = request;
const sync = this.config.get("factorio.sync_whitelist");
if (this.config.get("factorio.sync_whitelist") === "disabled") {
return;
} else if (sync === "bidirectional") {
// Factorio does not contain log events for whitelist so these values currently have no special meaning
this._expectedUserUpdates.push({
action: whitelisted ? "WHITELISTED" : "UNWHITELISTED",
name: name,
reason: "",
});
}
let { name, whitelisted } = request;
let command = whitelisted ? `/whitelist add ${name}` : `/whiteliste remove ${name}`;
let command = whitelisted ? `/whitelist add ${name}` : `/whitelist remove ${name}`;
await this.sendRcon(command);
}
@@ -991,9 +1168,13 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
let pid = this.server.pid;
if (pid) {
let stats = await pidusage(pid);
instanceFactorioCpuTime.labels(String(this.id)).set(stats.ctime / 1000);
instanceFactorioMemoryUsage.labels(String(this.id)).set(stats.memory);
try {
let stats = await getProcessStats(pid, this.logger);
instanceFactorioCpuTime.labels(String(this.id)).set(stats.ctime / 1000);
instanceFactorioMemoryUsage.labels(String(this.id)).set(stats.memory);
} catch (err) {
this.logger.warn("Failed to get process stats:", err);
}
}
return new lib.InstanceMetricsRequest.Response(results);
@@ -1002,21 +1183,26 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
async handleInstanceStartRequest(request: lib.InstanceStartRequest) {
let saveName = request.save;
try {
await this.prepare();
saveName = await this.prepareSave(saveName);
} catch (err: any) {
this.logger.error(`Error preparing instance: ${err.message}`);
this.notifyExit();
await this.sendSaveListUpdate();
throw err;
}
try {
await this.prepare();
saveName = await this.prepareSave(saveName);
} catch (err: any) {
this.logger.error(`Error preparing instance: ${err.message}`);
this.notifyExit();
await this.sendSaveListUpdate();
throw err;
}
try {
await this.start(saveName);
} catch (err: any) {
this.logger.error(`Error starting ${saveName}: ${err.message}`);
await this.stop();
throw err;
try {
await this.start(saveName);
} catch (err: any) {
this.logger.error(`Error starting ${saveName}: ${err.message}`);
await this.stop();
throw err;
}
} finally {
this.logger.verbose("Wiping credentials from server-settings.json");
await this.writeServerSettings(false);
}
}
@@ -1027,21 +1213,26 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
}
try {
await this.prepare();
} catch (err: any) {
this.logger.error(`Error preparing instance: ${err.message}`);
this.notifyExit();
await this.sendSaveListUpdate();
throw err;
}
try {
await this.prepare();
} catch (err: any) {
this.logger.error(`Error preparing instance: ${err.message}`);
this.notifyExit();
await this.sendSaveListUpdate();
throw err;
}
let { scenario, seed, mapGenSettings, mapSettings } = request;
try {
await this.startScenario(scenario, seed, mapGenSettings, mapSettings);
} catch (err: any) {
this.logger.error(`Error starting scenario ${scenario}: ${err.message}`);
await this.stop();
throw err;
let { scenario, seed, mapGenSettings, mapSettings } = request;
try {
await this.startScenario(scenario, seed, mapGenSettings, mapSettings);
} catch (err: any) {
this.logger.error(`Error starting scenario ${scenario}: ${err.message}`);
await this.stop();
throw err;
}
} finally {
this.logger.verbose("Wiping credentials from server-settings.json");
await this.writeServerSettings(false);
}
}
@@ -1053,7 +1244,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
this.notifyStatus("creating_save");
try {
this.logger.verbose("Writing server-settings.json");
await this.writeServerSettings();
await this.writeServerSettings(false);
await this.syncMods();
} catch (err: any) {
@@ -1081,7 +1272,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " ");
this.notifyStatus("exporting_data");
try {
this.logger.verbose("Writing server-settings.json");
await this.writeServerSettings();
await this.writeServerSettings(false);
this.logger.info("Exporting data .....");
await this.syncMods();
+20 -12
View File
@@ -15,12 +15,12 @@ interface Prototype {
}
interface SimpleIconSpecification {
icon: string;
icon_size: number;
icon_size?: number;
icon_mipmaps?: number;
}
interface IconLayer {
icon: string;
icon_size: number;
icon_size?: number;
tint?: lib.FactorioColor,
shift?: [number, number],
scale?: number,
@@ -28,7 +28,7 @@ interface IconLayer {
}
interface LayeredIconSpecification {
icons: IconLayer[];
icon_size: number;
icon_size?: number;
icon_mipmaps?: number;
}
type IconSpecification = LayeredIconSpecification | SimpleIconSpecification;
@@ -64,7 +64,7 @@ async function generateExportMod(server: FactorioServer) {
sourceDir: path.join(__dirname, "..", "..", "..", "lua", "export"),
outputDir: server.writePath("mods"),
bumpPatch: false,
factorioVersion: server.version.replace(/\.\d+$/, ""),
factorioVersion: server.version?.replace(/\.\d+$/, ""),
dependencies,
});
}
@@ -103,7 +103,8 @@ async function loadFile(server: FactorioServer, modVersions: Map<string, string>
let [, mod, filePath] = match;
if (["core", "base"].includes(mod)) {
const builtinModNames = ["core", ...lib.ModPack.getBuiltinModNames(server.version!)];
if (builtinModNames.includes(mod)) {
try {
return await fs.readFile(server.dataPath(mod, filePath));
} catch (err: any) {
@@ -164,9 +165,9 @@ async function loadSimpleIcon(
size: number,
iconCache: IconCache,
) {
let icon = await loadIcon(server, modVersions, item.icon, item.icon_size, 0, iconCache);
let icon = await loadIcon(server, modVersions, item.icon, item.icon_size ?? 64, 0, iconCache);
if (icon) {
let iconScale = size / item.icon_size;
let iconScale = size / (item.icon_size ?? 64);
if (iconScale !== 1) {
icon = icon.clone();
icon.scale(iconScale);
@@ -182,7 +183,7 @@ async function loadLayeredIcon(
size: number,
iconCache: IconCache,
) {
let baseLayerSize = item.icons[0].icon_size || item.icon_size;
let baseLayerSize = (item.icons[0].icon_size || item.icon_size) ?? 64;
let icon = await Jimp.create(size, size);
// The scaling factor of the base layer
@@ -192,7 +193,7 @@ async function loadLayeredIcon(
let baseUnit = size / (baseLayerSize * baseLayerScale);
for (let layer of item.icons) {
let layerSize = layer.icon_size || item.icon_size;
let layerSize = (layer.icon_size || item.icon_size) ?? 64;
let iconLayer = await loadIcon(server, modVersions, layer.icon, layerSize, layer.icon_mipmaps || 0, iconCache);
if (!iconLayer) {
@@ -396,11 +397,18 @@ async function exportLocale(
}
}
let baseLocaleFilePath = server.dataPath("base", "locale", languageCode, "base.cfg");
mergeLocale(lib.parse(await fs.readFile(baseLocaleFilePath, "utf8")));
const builtinModNames = [
// Filter if on builtin mods so "core" does not need to be specified in modOrder
"core", ...lib.ModPack.getBuiltinModNames(server.version!).filter(mod => modOrder.includes(mod)),
];
for (const builtinModName of builtinModNames) {
const localeFilePath = server.dataPath(builtinModName, "locale", languageCode, `${builtinModName}.cfg`);
mergeLocale(lib.parse(await fs.readFile(localeFilePath, "utf8")));
}
const builtinModNamesExport = ["export", ...builtinModNames];
for (let mod of modOrder) {
if (["core", "base", "export"].includes(mod)) {
if (builtinModNamesExport.includes(mod)) {
continue;
}
+25 -11
View File
@@ -77,9 +77,16 @@ export class SaveModule {
static async fromSave(json: Static<typeof SaveModule.jsonSchema>, root: JSZip) {
const module = new this(lib.ModuleInfo.fromJSON(json));
module.files = new Map(
await Promise.all(json.files.map(async f => [f, await root.file(f)!.async("nodebuffer")] as const))
);
module.files = new Map(await Promise.all(json.files
.map(filename => ({filename, file: root.file(filename)}))
.filter(({filename, file}) => {
if (file === null) {
lib.logger.warn(`Missing file ${filename} in save`);
}
return file !== null;
})
.map(async ({filename, file}) => [filename, await file!.async("nodebuffer")] as const)
));
return module;
}
@@ -195,12 +202,6 @@ export class PatchInfo {
}
}
const knownScenarios: Record<string, lib.ModuleInfo> = {
// First seen in 0.17.63
"4e866186ebe297f1038fd325b09df1a1f5e2fdd1": new lib.ModuleInfo("freeplay", "0.17.63", ["freeplay", "silo-script"]),
};
/**
* Generates control.lua code for loading the Clusterio modules
*
@@ -342,6 +343,15 @@ function reorderDependencies(modules: SaveModule[]) {
}
}
const knownScenarios: Record<string, lib.ModuleInfo> = {
// First seen in 0.17.63
"4e866186ebe297f1038fd325b09df1a1f5e2fdd1": new lib.ModuleInfo("freeplay", "0.17.63", [], ["scenario"]),
// First seen in 2.0
"bcbdde18ce4ec16ebfd93bd694cd9b12ef969c9a": new lib.ModuleInfo("freeplay", "2.0.0", [], ["scenario"]),
// First seen in 2.0.29
"3af547d2f4db3728b75ac5d1b3a4f47830dc48e7": new lib.ModuleInfo("freeplay", "2.0.29", [], ["scenario"]),
};
/**
* Patch a save with the given modules
*
@@ -380,6 +390,7 @@ export async function patch(savePath: string, modules: SaveModule[]) {
if (controlHash in knownScenarios) {
patchInfo = new PatchInfo(0, knownScenarios[controlHash], []);
root.file("scenario.lua", controlFile.nodeStream("nodebuffer"));
} else {
throw new Error(`Unable to patch save, unknown scenario (${controlHash})`);
}
@@ -395,8 +406,11 @@ export async function patch(savePath: string, modules: SaveModule[]) {
}
} else {
for (let module of patchInfo.modules) {
for (let file of module.files.keys()) {
zip.remove(root.file(file)!.name);
for (let filepath of module.files.keys()) {
const file = root.file(filepath);
if (file !== null) {
zip.remove(file.name);
}
}
}
patchInfo.modules = [];
+343 -167
View File
@@ -351,20 +351,37 @@ const outputHeuristics: Heuristic[] = [
},
},
// Message indicating the server is shutting down
// Last messages before the server hangs during shutdown waiting for console input on Windows.
{
filter: {
type: "generic",
message: /^Quitting: /,
message: "Quitting: multiplayer error.",
},
action: function(parsed) {
this.emit("_quitting");
// On windows the Factorio server will wait for input on the console input buffer
// when an error occurs. Since we can't send input there we terminate the server
// before shutting down. Since we can't send input there we terminate the server
// process when this happens.
if (process.platform === "win32" && parsed.message === "Quitting: multiplayer error.") {
if (process.platform === "win32") {
this._server!.kill();
// Notify that the server is being killed after the last message
// is logged so that it doesn't look like it was killed unnecessarily.
process.nextTick(() => {
this._logger.info("Killing Factorio process to avoid it hanging forever.");
});
}
},
},
{
filter: {
type: "log",
message: /^updateTick\(\d+\) changing state from\(Disconnected\) to\(Closed\)$/,
},
action: function() {
if (process.platform === "win32") {
this._server!.kill();
process.nextTick(() => {
this._logger.info("Killing Factorio process to avoid it hanging forever.");
});
}
},
},
@@ -407,6 +424,8 @@ export interface FactorioServerOptions {
* the latest version found in `factorioDir`.
*/
version?: string,
/** Path to executable to invoke when starting the server */
executablePath?: string;
/** UDP port to host game on. */
gamePort?: number,
/** TCP port to use for RCON. */
@@ -419,6 +438,8 @@ export interface FactorioServerOptions {
enableAuthserverBans?: boolean,
/** Enable verbose logging. */
verboseLogging?: boolean,
/** Enable console logging. */
consoleLogging?: boolean,
/** Strip paths in the console. */
stripPaths?: boolean,
/**
@@ -426,8 +447,11 @@ export interface FactorioServerOptions {
* connection.
*/
maxConcurrentCommands?: number
/** Timeout in ms to wait for a response to a shutdown command. */
hangTimeoutMs?: number,
/**
* Timeout in ms to wait after a shutdown is requested before killing the
* process. Defaults to 0 meaning no timeout
*/
shutdownTimeoutMs?: number,
}
/**
@@ -450,6 +474,8 @@ export interface FactorioServerOptions {
* @extends events.EventEmitter
*/
export class FactorioServer extends events.EventEmitter {
/** Path to executable to invoke when starting the server */
executablePath?: string;
/** UDP port used for hosting the Factorio game server on */
gamePort: number;
/** TCP port used for RCON on the Factorio game server */
@@ -462,8 +488,10 @@ export class FactorioServer extends events.EventEmitter {
enableAuthserverBans: boolean;
/** Enable verbose logging */
verboseLogging: boolean;
/** Timeout in ms to wait for a response to a shutdown command */
hangTimeoutMs = 5000;
/** Enable console logging */
consoleLogging: boolean;
_shutdownTimeoutMs = 0;
_stopTimeoutId?: ReturnType<typeof setTimeout>;
_factorioDir: string;
_writeDir: string;
@@ -472,6 +500,10 @@ export class FactorioServer extends events.EventEmitter {
_version: string | null = null;
_dataDir: string | null = null;
// Due to inconsistencies in the factorio api, we must manually watch the whitelist
// https://forums.factorio.com/viewtopic.php?t=123673
_whitelistWatcher: fs.FSWatcher | null = null;
_whitelist = new Set<string>();
_logger: lib.Logger;
_targetVersion: string;
@@ -504,6 +536,7 @@ export class FactorioServer extends events.EventEmitter {
this._logger = options.logger || lib.logger;
this._targetVersion = options.version || "latest";
this.executablePath = options.executablePath;
/** UDP port used for hosting the Factorio game server on */
this.gamePort = options.gamePort || randomDynamicPort();
/** TCP port used for RCON on the Factorio game server */
@@ -516,10 +549,13 @@ export class FactorioServer extends events.EventEmitter {
this.enableAuthserverBans = options.enableAuthserverBans || false;
/** Enable verbose logging */
this.verboseLogging = options.verboseLogging || false;
/** Enable console logging */
this.consoleLogging = options.consoleLogging || false;
/** Maximum number of RCON commands transmitted in parallel on the RCON connection */
this.maxConcurrentCommands = options.maxConcurrentCommands || 5;
/** Timeout in ms to wait for a response to a shutdown command */
this.hangTimeoutMs = options.hangTimeoutMs ?? 5000;
if (options.shutdownTimeoutMs !== undefined) {
this.shutdownTimeoutMs = options.shutdownTimeoutMs;
}
if (options.stripPaths) {
let charSet = new Set(path.resolve(this.writePath("temp")));
@@ -562,6 +598,13 @@ export class FactorioServer extends events.EventEmitter {
}
}
// TODO at the moment this is just a helper function, it has the potential for schema checking
handle<Event>(eventName: string, handler: (event: Event) => Promise<void>) {
this.on(`ipc-${eventName}`, (event) => handler(event).catch((err: Error) => {
this._logger.error(`Error handling ipc event:\n${err.stack ?? err.message}`);
}));
}
async _handleIpc(line: Buffer) {
let channelEnd = line.indexOf("?");
if (channelEnd === -1) {
@@ -654,10 +697,6 @@ export class FactorioServer extends events.EventEmitter {
}
async _startRcon() {
if (this._state === "stopping") {
return;
}
if (this._rconClient !== null) {
throw Error("RCON client is already started");
}
@@ -671,16 +710,95 @@ export class FactorioServer extends events.EventEmitter {
};
this._rconClient = new Rcon(config);
this._rconClient.on("error", () => { /* Ignore */ });
this._rconClient.on("authenticated", () => { this._rconReady = true; this.emit("rcon-ready"); });
this._rconClient.on("error", () => {
// Errors before being authenticated might not emit end
this._rconClient = null;
if (!this._rconReady) {
this._rconReady = true;
this.emit("rcon-ready");
}
});
this._rconClient.on("authenticated", () => {
this._rconReady = true;
this.emit("rcon-ready");
});
this._rconClient.on("end", () => {
this._rconClient = null;
if (!this._rconReady) {
this._rconReady = true;
this.emit("rcon-ready");
}
});
// XXX: Workaround to suppress bogus event listener warning
if (this._rconClient.emitter instanceof events.EventEmitter) {
this._rconClient.emitter.setMaxListeners(this.maxConcurrentCommands + 5);
}
await this._rconClient.connect();
try {
await this._rconClient.connect();
} catch (err: any) {
this._logger.error(`Failed to start RCON connection:\n${err?.stack ?? err?.message ?? err}`);
this._rconClient = null;
if (!this._rconReady) {
this._rconReady = true;
this.emit("rcon-ready");
}
}
}
_watchWhitelist() {
if (this._whitelistWatcher !== null) {
throw new Error("Whitelist watcher already started");
}
if (!this.enableWhitelist) {
return; // The file is not updated if the whitelist is disabled
}
const filePath = this.writePath("server-whitelist.json");
try {
this._whitelistWatcher = fs.watch(filePath);
} catch (err: any) {
this._logger.error(`Unable to watch whitelist, bidirectional sync will not be available:\n${err}`);
return;
}
this._whitelistWatcher.unref();
this._whitelistWatcher.on("error", (err) => {
this._whitelistWatcher = null;
this._logger.error(`Whitelist Watcher has errored:\n${err?.stack ?? err?.message ?? err}`);
});
this._whitelistWatcher.on("close", () => {
this._whitelistWatcher = null;
});
this._whitelistWatcher.on("change", async (eventType) => {
if (eventType !== "change") {
this._logger.warn(`Unexpected file watcher event: ${eventType}`);
return;
}
let newWhitelistJson;
try {
newWhitelistJson = await fs.readJSON(filePath);
} catch (err: any) {
this._logger.error(`Unable to read whitelist, bidirectional sync will not be available:\n${err}`);
return;
}
if (!(newWhitelistJson instanceof Array) || !newWhitelistJson.every(e => typeof e === "string")) {
// The whitelist must be an array of strings
this._logger.error("Unable to read whitelist, bidirectional sync will not be available:" +
"Unexpected whitelist format");
return;
}
const newWhitelist = new Set(newWhitelistJson);
// Set.difference added in node v22 and so we can not use this due to v20 support
const added = newWhitelistJson.filter(e => !this._whitelist.has(e));
const removed = [...this._whitelist.values()].filter(e => !newWhitelist.has(e));
this._whitelist = newWhitelist;
this.emit("whitelist-change", added, removed);
});
}
/** Maximum number of RCON commands transmitted in parallel on the RCON connection */
@@ -718,12 +836,12 @@ export class FactorioServer extends events.EventEmitter {
}
/**
* The version of Factorio in use. This will be the actual version if
* "latest" (the default) was specified as `factorioVersion` to the
* constructor.
* The version of Factorio in use. This will be the actual version if
* "latest" (the default) was specified as `factorioVersion` to the constructor.
* This will be undefined before the server is initialized, or if init failed.
*/
get version() {
return this._version!;
get version(): string | undefined {
return this._version ?? undefined;
}
/**
@@ -752,6 +870,7 @@ export class FactorioServer extends events.EventEmitter {
this._rconReady = false;
this._gameReady = false;
this._unexpected = [];
this._killed = false;
this._runningAutosave = null;
}
@@ -786,12 +905,19 @@ export class FactorioServer extends events.EventEmitter {
`\n- ${this._unexpected.join("\n- ")}`
));
}
} else { // if state === "stopping"
this._clearStopTimeout();
}
if (this._rconClient) {
this._rconClient.end().catch(() => {});
}
if (this._whitelistWatcher) {
this._whitelistWatcher.close();
}
this._resetState();
this.emit("exit");
});
@@ -806,6 +932,10 @@ export class FactorioServer extends events.EventEmitter {
this._rconClient.end().catch(() => {});
}
if (this._whitelistWatcher) {
this._whitelistWatcher.close();
}
this._resetState();
this.emit("exit");
});
@@ -840,28 +970,36 @@ export class FactorioServer extends events.EventEmitter {
this._check(["init"]);
this._state = "create";
await this._writeConfigIni();
await this._writeMapSettings(mapGenSettings, mapSettings);
this._server = child_process.spawn(
this.binaryPath(),
[
"--config", this.writePath("config.ini"),
"--create", this.writePath("saves", name),
...(seed !== undefined ? ["--map-gen-seed", String(seed)] : []),
...(mapGenSettings !== undefined
? ["--map-gen-settings", this.writePath("map-gen-settings.json")] : []
),
...(mapSettings !== undefined ? ["--map-settings", this.writePath("map-settings.json")] : []),
...(this.verboseLogging ? ["--verbose"] : []),
],
{
detached: true,
stdio: "pipe",
}
);
this._attachStdio();
this._server.once("exit", () => this.emit("exit"));
try {
// Everything between _state and once exit must be sync and call _resetState on error
// If this this thread yields then the server will be left in an invalid transient state
/* eslint-disable node/no-sync */
this._writeConfigIniSync();
this._writeMapSettingsSync(mapGenSettings, mapSettings);
this._server = child_process.spawn(
this.binaryPath(),
[
"--config", this.writePath("config.ini"),
"--create", this.writePath("saves", name),
...(seed !== undefined ? ["--map-gen-seed", String(seed)] : []),
...(mapGenSettings !== undefined
? ["--map-gen-settings", this.writePath("map-gen-settings.json")] : []
),
...(mapSettings !== undefined ? ["--map-settings", this.writePath("map-settings.json")] : []),
...(this.verboseLogging ? ["--verbose"] : []),
],
{
detached: true,
stdio: "pipe",
}
);
this._attachStdio();
this._server.once("exit", () => this.emit("exit"));
/* eslint-enable node/no-sync */
} catch (err: any) {
this._resetState();
throw new lib.EnvironmentError(`Unexpected error:\n${err.stack}`);
}
try {
let [code, signal] = await events.once(this._server, "exit");
@@ -893,27 +1031,38 @@ export class FactorioServer extends events.EventEmitter {
this._check(["init"]);
this._state = "running";
await this._writeConfigIni();
this._server = child_process.spawn(
this.binaryPath(),
[
"--config", this.writePath("config.ini"),
"--start-server", this.writePath("saves", save),
"--port", String(this.gamePort),
"--rcon-port", String(this.rconPort),
"--rcon-password", this.rconPassword,
...(this.enableWhitelist ? ["--use-server-whitelist"] : []),
...(this.enableAuthserverBans ? ["--use-authserver-bans"] : []),
...(this.verboseLogging ? ["--verbose"] : []),
],
{
detached: true,
stdio: "pipe",
}
);
try {
// Everything between _state and _watchExist must be sync and call _resetState on error
// If this this thread yields then the server will be left in an invalid transient state
/* eslint-disable node/no-sync */
this._writeConfigIniSync();
this._server = child_process.spawn(
this.binaryPath(),
[
"--config", this.writePath("config.ini"),
"--start-server", this.writePath("saves", save),
"--port", String(this.gamePort),
"--rcon-port", String(this.rconPort),
"--rcon-password", this.rconPassword,
...(this.enableWhitelist ? ["--use-server-whitelist"] : []),
...(this.enableAuthserverBans ? ["--use-authserver-bans"] : []),
...(this.verboseLogging ? ["--verbose"] : []),
...(this.consoleLogging ? ["--console-log", this.writePath("console.log")] : []),
],
{
detached: true,
stdio: "pipe",
}
);
this._watchWhitelist();
this._attachStdio();
this._watchExit();
/* eslint-enable node/no-sync */
} catch (err: any) {
this.emit("error", new lib.EnvironmentError(`Unexpected error:\n${err.stack}`));
this._resetState();
}
this._attachStdio();
this._watchExit();
await this._waitForReady();
}
@@ -934,33 +1083,44 @@ export class FactorioServer extends events.EventEmitter {
this._check(["init"]);
this._state = "running";
await this._writeConfigIni();
await this._writeMapSettings(mapGenSettings, mapSettings);
this._server = child_process.spawn(
this.binaryPath(),
[
"--config", this.writePath("config.ini"),
"--start-server-load-scenario", scenario,
...(seed !== undefined ? ["--map-gen-seed", String(seed)] : []),
...(mapGenSettings !== undefined
? ["--map-gen-settings", this.writePath("map-gen-settings.json")] : []
),
...(mapSettings !== undefined ? ["--map-settings", this.writePath("map-settings.json")] : []),
"--port", String(this.gamePort),
"--rcon-port", String(this.rconPort),
"--rcon-password", this.rconPassword,
...(this.enableWhitelist ? ["--use-server-whitelist"] : []),
...(this.enableAuthserverBans ? ["--use-authserver-bans"] : []),
...(this.verboseLogging ? ["--verbose"] : []),
],
{
detached: true,
stdio: "pipe",
}
);
try {
// Everything between _state and _watchExist must be sync and call _resetState on error
// If this this thread yields then the server will be left in an invalid transient state
/* eslint-disable node/no-sync */
this._writeConfigIniSync();
this._writeMapSettingsSync(mapGenSettings, mapSettings);
this._server = child_process.spawn(
this.binaryPath(),
[
"--config", this.writePath("config.ini"),
"--start-server-load-scenario", scenario,
...(seed !== undefined ? ["--map-gen-seed", String(seed)] : []),
...(mapGenSettings !== undefined
? ["--map-gen-settings", this.writePath("map-gen-settings.json")] : []
),
...(mapSettings !== undefined ? ["--map-settings", this.writePath("map-settings.json")] : []),
"--port", String(this.gamePort),
"--rcon-port", String(this.rconPort),
"--rcon-password", this.rconPassword,
...(this.enableWhitelist ? ["--use-server-whitelist"] : []),
...(this.enableAuthserverBans ? ["--use-authserver-bans"] : []),
...(this.verboseLogging ? ["--verbose"] : []),
...(this.consoleLogging ? ["--console-log", this.writePath("console.log")] : []),
],
{
detached: true,
stdio: "pipe",
}
);
this._watchWhitelist();
this._attachStdio();
this._watchExit();
/* eslint-enable node/no-sync */
} catch (err: any) {
this.emit("error", new lib.EnvironmentError(`Unexpected error:\n${err.stack}`));
this._resetState();
}
this._attachStdio();
this._watchExit();
await this._waitForReady();
}
@@ -993,6 +1153,47 @@ export class FactorioServer extends events.EventEmitter {
return response;
}
/**
* Timeout in ms to wait after a shutdown is requested before killing the
* process. Defaults to 0 meaning no timeout
*/
get shutdownTimeoutMs() {
return this._shutdownTimeoutMs;
}
set shutdownTimeoutMs(newTimeoutMs: number) {
this._shutdownTimeoutMs = newTimeoutMs;
if (this._state === "stopping") {
this._clearStopTimeout();
this._setStopTimeout();
}
}
_setStopTimeout() {
if (this._shutdownTimeoutMs !== 0) {
this._stopTimeoutId = setTimeout(
this.onStopTimeout.bind(this),
this.shutdownTimeoutMs
);
}
}
_clearStopTimeout() {
clearTimeout(this._stopTimeoutId);
this._stopTimeoutId = undefined;
}
onStopTimeout() {
this._logger.error("Factorio appears to have hanged, killing process");
if (this._rconClient) {
this._rconClient.end().catch(() => {});
}
if (this._whitelistWatcher) {
this._whitelistWatcher.close();
}
this._server!.kill("SIGKILL");
}
/**
* Stop the server
*
@@ -1001,88 +1202,54 @@ export class FactorioServer extends events.EventEmitter {
*/
async stop() {
this._check(["running"]);
let hanged = true;
let stopped = false;
function setAlive() { hanged = false; }
let timeoutId = setTimeout(() => {
if (hanged) {
this._logger.error("Factorio appears to have hanged, sending SIGKILL");
if (this._rconClient) {
this._rconClient.end().catch(() => {});
}
this._server!.kill("SIGKILL");
}
}, this.hangTimeoutMs);
// It's possible the server decides to exit on its own, in which
// case the stop operation has to be cancelled.
this._server!.once("exit", () => {
stopped = true;
clearTimeout(timeoutId);
});
this._state = "stopping";
// On Windows we need to save the map as there's no graceful shutdown.
if (this._rconClient && process.platform === "win32") {
// Not using events.once here as that will reject on error.
let saved = new Promise(resolve => this.once("_saved", resolve));
this.on("_saving", setAlive);
try {
await this.sendRcon("/server-save");
await saved;
// It's possible the server doesn't stop if it's stuck in an infinite
// loop or overloaded, kill it if the shutdown takes too long.
this._setStopTimeout();
} catch (err) {
// Ignore
}
clearTimeout(timeoutId);
this.off("_saving", setAlive);
}
if (this._rconClient) {
// If RCON is not yet fully connected that operation needs to
// complete before the RCON connection can be closed cleanly.
if (!this._rconReady) {
await events.once(this, "rcon-ready");
}
await this._rconClient.end();
// If RCON is not yet fully connected that operation needs to
// complete before the RCON connection can be used
if (!this._rconReady) {
// Not using events.once here to avoid throwing on error events.
await new Promise(resolve => this.once("rcon-ready", resolve));
}
// The Factorio server may have decided to get ahead of us and
// stop by itself before we had the chance to signal it.
if (stopped) {
if (this._state !== "stopping") {
return;
}
// On linux this sends SIGTERM to the process. On windows this
// will terminate the process with no cleanup. You can send your
// complaint for the lack of graceful shutdown to the Factorio
// developers. Rcon does not recognize /quit, stdin is not
// recognized, and there's no "send CTRL+C to process" on Windows.
this._server!.kill();
const waitForExit = events.once(this._server!, "exit");
waitForExit.catch(() => {}); // Prevent unhandled promise rejection
// There appears to be an race condition where sending SIGTERM
// immediatly before the RCON interface comes online causes the
// Factorio server to hang. It's also possible the server doesn't
// stop if it's stuck with an infinite lua code loop. In either
// case there's no recovering from it.
if (process.platform !== "win32") {
this.on("_quitting", setAlive);
await events.once(this._server!, "exit");
clearTimeout(timeoutId);
this.off("_quitting", setAlive);
// On windows the process is terminated immediately, but to keep
// ordering wait until after the exit event here.
// Stop the server
if (this._rconClient) {
// Use RCON by default to stop the the server as it's available on
// all platforms and simplifies the code.
try {
await this.sendRcon("/quit");
} catch (err: any) {
// Ignore RCON connection being closed while sending the quit command.
if (err.message !== "Connection closed") {
throw err;
}
}
} else {
await events.once(this._server!, "exit");
// No rcon connection is available, fallback to signal. On linux
// this sends SIGTERM to the process. On windows this
// will terminate the process without saving. You can send your
// complaint for the lack of graceful shutdown options to the
// Factorio developers. I haven't found any way to make stdin work
// and there's no "send CTRL+C to process" API on Windows.
if (process.platform === "win32") {
this._logger.warn("No RCON connection, falling back to killing the server.");
}
this._server!.kill();
}
await waitForExit;
}
/**
@@ -1148,11 +1315,15 @@ export class FactorioServer extends events.EventEmitter {
/**
* Get Factorio binary path
*
* Get the path to the factorio binary depending on the platform (MacOS support)
* Get the path to the factorio binary depending on the configuration
* and or the platform (MacOS support)
*
* @returns Path to factorio binary
*/
binaryPath() {
if (this.executablePath) {
return this.dataPath("..", this.executablePath);
}
if (process.platform === "darwin") {
return this.dataPath("..", "MacOS", "factorio");
}
@@ -1183,24 +1354,29 @@ export class FactorioServer extends events.EventEmitter {
return JSON.parse(await fs.readFile(this.dataPath("server-settings.example.json"), "utf-8"));
}
async _writeConfigIni() {
_writeConfigIniSync() {
let content = lib.stringify({
path: {
"read-data": this.dataPath(),
"write-data": this.writePath(),
},
});
await lib.safeOutputFile(this.writePath("config.ini"), content);
// Must be sync to allow process spawn without awaiting
// eslint-disable-next-line node/no-sync
fs.writeFileSync(this.writePath("config.ini"), content);
}
async _writeMapSettings(mapGenSettings?: object, mapSettings?: object) {
_writeMapSettingsSync(mapGenSettings?: object, mapSettings?: object) {
// Must be sync to allow process spawn without awaiting
if (mapGenSettings) {
await lib.safeOutputFile(
// eslint-disable-next-line node/no-sync
fs.writeFileSync(
this.writePath("map-gen-settings.json"), JSON.stringify(mapGenSettings, null, "\t")
);
}
if (mapSettings) {
await lib.safeOutputFile(
// eslint-disable-next-line node/no-sync
fs.writeFileSync(
this.writePath("map-settings.json"), JSON.stringify(mapSettings, null, "\t")
);
}
-3
View File
@@ -4,7 +4,4 @@
{ "path": "../lib/tsconfig.node.json" },
],
"include": ["index.ts", "host.ts", "package.json", "src/**/*.ts", "src/**/*.js"],
"compilerOptions": {
"outDir": "dist/node",
},
}
+3
View File
@@ -28,6 +28,9 @@ export * from "./src/stream";
export * from "./src/system_collectors";
export * from "./src/zip_ops";
export * from "./src/subscriptions";
export * from "./src/datastore";
export * from "./src/load_plugin_list";
export * from "./src/rce_ops";
export { default as ExponentialBackoff } from "./src/ExponentialBackoff";
export { default as ModStore } from "./src/ModStore";
+11 -10
View File
@@ -1,7 +1,7 @@
{
"name": "@clusterio/lib",
"description": "Shared library for Clusterio",
"version": "2.0.0-alpha.17",
"version": "2.0.0-alpha.21",
"repository": "https://github.com/clusterio/clusterio",
"license": "MIT",
"scripts": {
@@ -11,6 +11,7 @@
"main": "dist/node/index.js",
"browser": "browser.ts",
"keywords": [
"clusterio",
"factorio"
],
"engines": {
@@ -18,17 +19,17 @@
},
"dependencies": {
"@sinclair/typebox": "^0.30.4",
"ajv": "^8.12.0",
"ajv": "^8.17.1",
"chalk": "^4.1.2",
"fast-deep-equal": "^3.1.3",
"fs-extra": "^11.1.1",
"fs-extra": "^11.3.0",
"jszip": "^3.10.1",
"klaw": "^4.1.0",
"set-blocking": "^2.0.0",
"triple-beam": "^1.4.1",
"winston": "^3.10.0",
"winston-transport": "^4.5.0",
"ws": "^8.13.0",
"winston": "^3.17.0",
"winston-transport": "^4.9.0",
"ws": "^8.18.1",
"yargs": "^17.7.2"
},
"publishConfig": {
@@ -36,11 +37,11 @@
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/node": "^20.4.5",
"@types/node": "^20.17.19",
"@types/set-blocking": "^2.0.0",
"@types/triple-beam": "^1.3.2",
"@types/ws": "^8.5.5",
"@types/yargs": "^17.0.24",
"typescript": "^5.1.6"
"@types/ws": "^8.5.14",
"@types/yargs": "^17.0.33",
"typescript": "^5.7.3"
}
}
+242 -8
View File
@@ -1,14 +1,42 @@
import fs from "fs-extra";
import path from "path";
import { Static } from "@sinclair/typebox";
import { logger } from "./logging";
import { ModInfo } from "./data";
import { integerModVersion, ModInfo, ModPack, ModRecord } from "./data";
import TypedEventEmitter from "./TypedEventEmitter";
import { safeOutputFile } from "./file_ops";
import { Writable } from "stream";
import { ModPortalReleaseSchema, ModPortalDetailsSchema } from "./data/messages_mod";
export interface ModStoreEvents {
/** A stored mod was created, updated or deleted */
change: (mod: ModInfo) => void;
}
interface ModsInfoResponse {
pagination: any,
results: Array<Static<typeof ModPortalDetailsSchema>>,
}
/**
* Interface describing the structure of the pagination info from the portal API
*/
interface ModPortalPagination {
count: number;
links: { next?: string; prev?: string; };
page: number;
page_count: number;
page_size: number;
}
/**
* Interface describing the structure of the portal API response for a single page
*/
interface ModPortalResponse {
pagination: ModPortalPagination;
results: Static<typeof ModPortalDetailsSchema>[];
}
export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, ModStoreEvents> {
constructor(
public modsDirectory: string,
@@ -25,11 +53,6 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
return mod;
}
hasMod(name: string, version: string, sha1?: string): boolean {
const mod = this.files.get(ModInfo.filename(name, version));
return Boolean(mod && (!sha1 || sha1 !== mod.sha1));
}
mods() { return this.files.values(); }
static async loadFile(modsDirectory: string, file: string) {
@@ -42,8 +65,7 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
async loadFile(file: string) {
let modInfo = await ModStore.loadFile(this.modsDirectory, file);
this.files.set(modInfo.filename, modInfo);
this.emit("change", modInfo);
this.addMod(modInfo);
return modInfo;
}
@@ -74,6 +96,21 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
}
static async fromDirectory(modsDirectory: string) {
type ModInfoCache = Static<typeof ModInfo.jsonSchema>[];
const cacheFilename = path.join(modsDirectory, "mod-info-cache.json");
let cache = new Map<string, ModInfo>();
try {
const content = await fs.readFile(cacheFilename, "utf8");
cache = new Map((JSON.parse(content) as ModInfoCache).map(item => {
const mod = ModInfo.fromJSON(item);
return [mod.filename, mod];
}));
} catch (err: any) {
if (err.code !== "ENOENT") {
logger.warn(`Error loading ${cacheFilename}:\n${err.stack}`);
}
}
const files = new Map<string, ModInfo>();
for (let entry of await fs.readdir(modsDirectory, { withFileTypes: true })) {
if (entry.isDirectory()) {
@@ -81,10 +118,23 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
continue;
}
if (!entry.name.toLowerCase().endsWith(".zip")) {
continue;
}
if (entry.name.toLowerCase().endsWith(".tmp.zip")) {
continue;
}
const cachedModInfo = cache.get(entry.name);
if (cachedModInfo) {
const stat = await fs.stat(path.join(modsDirectory, entry.name));
if (cachedModInfo.mtimeMs === stat.mtimeMs) {
files.set(cachedModInfo.filename, cachedModInfo);
continue;
}
}
logger.info(`Loading info for Mod ${entry.name}`);
let modInfo: ModInfo;
try {
@@ -96,6 +146,190 @@ export default class ModStore extends TypedEventEmitter<keyof ModStoreEvents, Mo
files.set(modInfo.filename, modInfo);
}
if (files.size) {
logger.info(`Loaded info for ${files.size} mods`);
const content: ModInfoCache = [...files.values()].map(mod => mod.toJSON());
await safeOutputFile(cacheFilename, JSON.stringify(content));
}
return new this(modsDirectory, files);
}
private async downloadModFromReleases(releases: Array<Static<typeof ModPortalReleaseSchema>>, version: string,
username: string, token: string
) {
const selectedRelease = releases.find(release => release.version === version);
if (selectedRelease === undefined) {
logger.warn(`Mod release matching version ${version} not found.`);
return;
}
try {
const url = new URL(`https://mods.factorio.com/${selectedRelease.download_url}`);
url.searchParams.set("username", username);
url.searchParams.set("token", token);
const tempFileName = `${this.modsDirectory}/${selectedRelease.file_name}.tmp`;
const finalFileName = `${this.modsDirectory}/${selectedRelease.file_name}`;
await ModStore.downloadFile(url, tempFileName);
await fs.rename(tempFileName, finalFileName);
await this.loadFile(selectedRelease.file_name);
} catch (err: any) {
logger.error(`Failed to download or load mod ${selectedRelease.file_name} (${version}): ${err.message}`);
// Attempt to clean up temp file if it exists
try {
const tempFileName = `${this.modsDirectory}/${selectedRelease.file_name}.tmp`;
await fs.unlink(tempFileName);
} catch (unlinkErr: any) {
if (unlinkErr.code !== "ENOENT") { // Ignore if file doesn't exist
logger.warn(`Failed to clean up temp file ${selectedRelease.file_name}.tmp: ${unlinkErr.message}`);
}
}
}
}
private async downloadModsChunk(
modNames: string[], modVersions: string[], username: string, token: string, factorioVersion: string
) {
const url = new URL("https://mods.factorio.com/api/mods");
url.searchParams.set("page_size", "max");
url.searchParams.set("version", factorioVersion);
const response = await fetch(url, {
method: "POST",
body: new URLSearchParams({ namelist: modNames.join(",") }),
});
if (response.status !== 200) {
throw Error(`Fetch: ${url} returned ${response.status} ${response.statusText}`);
}
const mods = (await response.json() as ModsInfoResponse).results;
await Promise.all(mods.map((mod) => {
if (mod.releases) {
return this.downloadModFromReleases(mod.releases,
modVersions[modNames.indexOf(mod.name)], username, token);
} else if (mod.latest_release) {
return this.downloadModFromReleases([mod.latest_release],
modVersions[modNames.indexOf(mod.name)], username, token);
}
throw new Error(`Mod ${mod.name} has no releases or latest_release`);
}));
}
/**
*
* downloads mods to the mod dir, will not download mods for which are already in the mod directory
*
* @param modMap a map of what mods to download and what version to download
* @param username factorio username used for auth to download mods
* @param token factorio token used for auth to download mods
* @param factorioVersion factorio version to filter for (mods are not guaranteed to work between Middle versions)
*
*/
async downloadMods(modMap: Map<string, string>, username: string, token: string, factorioVersion: string = "1.1") {
const chunkSize = 500;
let futures: Promise<void>[] = [];
modMap = new Map(modMap);
// Truncate factorioVersion to major.minor (e.g., "1.1.100" -> "1.1") for API/builtin checks
const versionParts = factorioVersion.split(".");
if (versionParts.length > 2) {
factorioVersion = versionParts.slice(0, 2).join(".");
}
const builtInMods = new Set(ModPack.getBuiltinModNames(factorioVersion));
// get rid of mods already downloaded, built-in, or core
modMap.forEach((modVersion, modName, map) => {
const filename = `${modName}_${modVersion}.zip`;
// Filter existing, built-in from getBuiltinModNames, and explicitly filter "core"
if (this.files.has(filename) || builtInMods.has(modName) || modName === "core") {
map.delete(modName);
}
});
const modNames = Array.from(modMap.keys());
const modVersions = Array.from(modMap.values());
for (let i = 0; i < modMap.size; i += chunkSize) {
futures.push(
this.downloadModsChunk(modNames.slice(i, i + chunkSize), modVersions.slice(i, i + chunkSize),
username, token, factorioVersion)
);
}
await Promise.all(futures);
}
static async downloadFile(url: string | URL, filePath: string) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
}
if (!response.body) {
throw new Error("Response body is missing.");
}
const fileStream = fs.createWriteStream(filePath);
const writer = Writable.toWeb(fileStream);
await response.body.pipeTo(writer);
} catch (error) {
logger.error("Error downloading file:", error);
throw error;
}
}
/**
* Fetch all mods from the Factorio Mod Portal for a specific Factorio version.
*
* Handles pagination automatically to retrieve the complete list.
*
* @param factorioVersion - The Factorio version to fetch mods for (e.g., "1.1").
* @param pageSize - The page size for fetching mods.
* @param hide_deprecated - Whether to exclude deprecated mods.
* @returns An array of mod details.
*/
static async fetchAllModsFromPortal(
factorioVersion: string,
pageSize: number,
hide_deprecated: boolean = false
): Promise<Static<typeof ModPortalDetailsSchema>[]> {
logger.info(`Fetching all mods for Factorio version ${factorioVersion}...`);
let currentPage = 1;
let allMods: Static<typeof ModPortalDetailsSchema>[] = [];
let hasMorePages = true;
while (hasMorePages) {
const url = new URL("https://mods.factorio.com/api/mods");
url.searchParams.set("page_size", String(pageSize));
url.searchParams.set("version", factorioVersion);
url.searchParams.set("page", String(currentPage));
url.searchParams.set("hide_deprecated", String(hide_deprecated));
logger.verbose(`Fetching mod portal page ${currentPage}: ${url.toString()}`);
const response = await fetch(url.toString());
if (!response.ok) {
let errorMessage = `Mod portal fetch page ${currentPage} failed: `;
errorMessage += `${response.status} ${response.statusText}`;
try {
errorMessage += ` - ${await response.text()}`;
} catch (bodyError) {
logger.warn("Failed to read response body for failed portal request");
}
throw new Error(errorMessage);
}
const data = await response.json() as ModPortalResponse;
allMods = allMods.concat(data.results);
hasMorePages = currentPage < data.pagination.page_count;
currentPage += 1;
}
logger.info(`Successfully fetched ${allMods.length} mods for Factorio version ${factorioVersion}.`);
return allMods;
}
}
+179 -57
View File
@@ -1,13 +1,15 @@
// Configuration classes
import { Type, Static } from "@sinclair/typebox";
import TypedEventEmitter from "../TypedEventEmitter";
import * as fs from "fs-extra";
import TypedEventEmitter from "../TypedEventEmitter";
import isDeepStrictEqual from "../is_deep_strict_equal";
import { basicType } from "../helpers";
import * as libSchema from "../schema";
import { StringEnum } from "../data/composites";
import { safeOutputFile } from "../file_ops";
const ConfigLocation = StringEnum(["controller", "host", "instance", "control"]);
const ConfigLocation = StringEnum(["controller", "host", "control"]);
export type ConfigLocation = Static<typeof ConfigLocation>;
/**
@@ -77,6 +79,12 @@ export interface FieldDefinition {
* Text used to describe the config field in user interfaces.
*/
description?: string;
/**
* Value to use for the autocomplete attribute in the Web UI's input element.
* Useful for making browsers behave properly with credential inputs.
* Only used for string and number fields.
*/
autoComplete?: string;
/**
* Web UI component to use for rending this input. Can be extended by
* @{link "@clusterio/web_ui".BaseWebPlugin.inputComponents}
@@ -111,6 +119,22 @@ export interface FieldDefinition {
* modified from.
*/
access?: ConfigLocation[];
/**
* If present treat the config value as a credential.
* Only the locations specified will be able to read the current value.
* Access can be used to specify which locations are able to overwrite the value.
*/
credential?: ConfigLocation[];
/**
* If present treat the config value as readonly.
* Only the locations specified will be able to update the config value.
* Access can be used to specify which locations are able to read the value.
*/
readonly?: ConfigLocation[];
/**
* True if graphical interfaces should hide the config value
*/
hidden?: boolean;
}
export type ConfigDefs<Fields> = {
@@ -131,6 +155,12 @@ export type ConfigEvents = {
fieldChanged: (name: string, curr: FieldValue, prev: FieldValue) => void;
}
export enum ConfigAccess {
read = 0x1,
write = 0x2,
readWrite = 0x3,
}
/**
* Collection of config entries
*
@@ -159,25 +189,43 @@ export class Config<
declare static fieldDefinitions: ConfigDefs<any>;
declare ["constructor"]: typeof Config;
/**
* Handle migration between clusterio versions
*
* @param config - Input config read and validated from file
* @returns Output config to be loaded into the class
* @protected
*/
static migrations(config: Static<typeof this.jsonSchema>) {
return config;
}
fields: Fields;
_unknownFields: Record<string, FieldValue> = {};
/** Defines the default access locations for the config class */
static defaultAccess: ConfigLocation[] = ["controller", "host", "control"];
/** Contains the fields and their values */
private fields: Fields;
private _unknownFields: Record<string, FieldValue> = {};
/** Set to true when a field in the config is changed. */
dirty = false;
private dirty = false;
/** Set to true when a 'restart required' field in the config is changed. */
restartRequired = false;
/**
* Create a new instance of the given config
*
* @param location -
* Location to evaluate access for this instance from
* Location to evaluate access for this instance from.
* @param fields -
* Serialized representation of fields to load.
* @param filepath -
* Filepath for the config to save to.
*/
constructor(
public location: ConfigLocation,
fields?: Static<typeof Config.jsonSchema>,
public defaultAccess: ConfigLocation[] = ["controller", "host", "control"],
public filepath?: string,
) {
if (typeof location !== "string") {
throw new Error("location must be a string");
@@ -188,14 +236,14 @@ export class Config<
Object.entries(
this.constructor.fieldDefinitions
).filter(
([name, def]) => this._checkAccess(name, def, location, false)
([name, def]) => this._checkAccess(name, def, location, ConfigAccess.read, false)
).map(
([name, def]) => [name, defaultValue(def)]
)
) as Fields;
if (fields) {
this.update(fields, false, location);
this.update(this.constructor.migrations(fields), false, location);
}
}
@@ -204,28 +252,82 @@ export class Config<
*
* @param name - Name of the field to check.
* @param def - definition for for the field to check.
* @param remote - location of remote access to check for.
* @param location - location to check access for.
* @param mode - type of access to check for.
* @param error - throw on failure to pass check.
* @throws {InvalidAccess}
* if access is not granted and error is true.
* @returns true if access is granted
* @private
*/
_checkAccess(name: string, def: FieldDefinition, remote: ConfigLocation, error: boolean) {
for (let loc of [remote, this.location]) {
if (!(def.access ?? this.defaultAccess).includes(loc)) {
if (error) {
throw new InvalidAccess(`Field '${name}' is not accessible from ${loc}`);
} else {
return false;
}
_checkAccess(name: string, def: FieldDefinition, location: ConfigLocation, mode: ConfigAccess, error: boolean) {
// If credential array is present it acts as read permission and the access array as write permission,
// If readonly array is present it acts as write permission and the access array as the read permission.
// If neither is present access array acts as a combined read/write permission check.
if (
def.credential && mode & ConfigAccess.read && !(def.credential).includes(location)
|| def.readonly && mode & ConfigAccess.write && !(def.readonly).includes(location)
|| (
(!def.credential || mode & ConfigAccess.write)
&& (!def.readonly || mode & ConfigAccess.read)
&& !(def.access ?? this.constructor.defaultAccess).includes(location)
)
) {
if (error) {
throw new InvalidAccess(`Field '${name}' is not accessible from ${location}`);
} else {
return false;
}
}
return true;
}
static jsonSchema = ConfigSchema;
/**
* Set the value of a field, while also settings the required flags and events.
* This method does not perform any validation on the value being set
*
* @param name - Name of the field to set
* @param value - The new value of the field
* @param notify - False to skip setting dirty flags and emitting events
*/
_set(name: keyof Fields & string, value: FieldValue, notify: boolean = true) {
const prev = this.fields[name];
this.fields[name] = value as any;
if (notify && !isDeepStrictEqual(value, prev)) {
const def = this.constructor.fieldDefinitions[name];
if (def.restartRequired) {
this.restartRequired = true;
}
this.dirty = true;
this.emit("fieldChanged", name, value, prev);
}
}
/**
* Create config from file
*
* @param location - Location used for access control.
* @param filepath - Filepath for the config to save to.
* @returns Instance of this config
*/
static async fromFile(location: ConfigLocation, filepath: string) {
const fields = await fs.readJson(filepath, { encoding: "utf8" });
return this.fromJSON(fields, location, filepath);
}
/** Saves this config to file if there are unsaved changes */
async save() {
if (!this.dirty) {
return;
}
if (!this.filepath) {
throw new Error("Cannot save config which has no filepath");
}
await safeOutputFile(this.filepath, JSON.stringify(this, null, "\t"));
this.dirty = false;
}
static jsonSchema = ConfigSchema;
static validate = libSchema.compile(this.jsonSchema as any);
/**
@@ -233,11 +335,13 @@ export class Config<
*
* @param json - Serialized config to load.
* @param location - Location used for access control.
* @param filepath - Filepath for the config to save to.
* @returns Instance of this config
*/
static fromJSON(
json: Static<typeof this.jsonSchema>,
location: ConfigLocation,
filepath?: string,
) {
// migrate: Pre alpha 14 config format
if (typeof json === "object" && json !== null && json.groups instanceof Array) {
@@ -253,7 +357,7 @@ export class Config<
if (!valid) {
throw new Error("Invalid config");
}
return new this(location, json);
return new this(location, json, filepath);
}
toJSON() {
@@ -263,14 +367,22 @@ export class Config<
/**
* Serialize the config to a plain JavaScript object
*
* @param location - Location used for access control.
* @returns JSON serializable representation of the group.
* @param remote - Location this serialised representation is for
* @param filter - When provided, only the given fields are serialized
* @returns JSON serializable representation of the config.
*/
toRemote(location: ConfigLocation): Static<typeof Config.jsonSchema> {
toRemote(remote: ConfigLocation, filter?: (keyof Fields)[]): Static<typeof Config.jsonSchema> {
let fields: Record<string, FieldValue> = {};
for (let [name, value] of Object.entries(this.fields)) {
if (filter && !filter.includes(name as keyof Fields)) {
continue;
}
let def = this.constructor.fieldDefinitions[name];
if (!this._checkAccess(name, def, location, false)) {
if (
this.location !== "control" && !this._checkAccess(name, def, this.location, ConfigAccess.write, false)
|| this.location !== remote && !this._checkAccess(name, def, remote, ConfigAccess.read, false)
) {
continue;
}
@@ -293,12 +405,12 @@ export class Config<
* @param json -
* Result from a previous call to .toRemote().
* @param notify - Invoke fieldChanged events if true.
* @param location - Location used for access control.
* @param remote - Location this serialised representation was received from.
*/
update(
json: Static<typeof Config.jsonSchema>,
notify: boolean,
location = this.location
remote = this.location
) {
const valid = Config.validate(json);
if (!valid) {
@@ -316,7 +428,9 @@ export class Config<
continue;
}
if (!this._checkAccess(name, def, location, false)) {
if (
this.location !== "control" && !this._checkAccess(name, def, remote, ConfigAccess.write, false)
) {
continue;
}
@@ -333,12 +447,7 @@ export class Config<
continue;
}
let prev = this.fields[name];
this.fields[name] = value as any;
if (notify && !isDeepStrictEqual(value, prev)) {
this.dirty = true;
this.emit("fieldChanged", name, value, prev);
}
this._set(name, value, notify);
}
}
@@ -346,30 +455,45 @@ export class Config<
* Check if field can be accessed from the given location
*
* @param name - Name of field to check.
* @param mode - Mode to access the field as.
* @param location - Location used for access control.
* @returns true if field is accessible
*/
canAccess(name: string, location = this.location) {
canAccess(name: string, mode: ConfigAccess, location = this.location) {
if (typeof mode !== "number") {
throw new TypeError("mode argument is required to canAccess");
}
let def = this.constructor.fieldDefinitions[name];
if (!def) {
throw new InvalidField(`No field named '${name}'`);
}
return this._checkAccess(name, def, location, false);
return this._checkAccess(name, def, location, mode, false);
}
/**
* Get value for field
*
* @param name - Name of field to get.
* @param location - Location used for access control.
* @param remote - Location this value is being read from.
* @returns Value stored for the field.
*/
get<Field extends keyof Fields & string>(name: Field, location = this.location) {
get<Field extends keyof Fields & string>(name: Field, remote = this.location) {
let def = this.constructor.fieldDefinitions[name];
if (!def) {
throw new InvalidField(`No field named '${name}'`);
}
this._checkAccess(name, def, location, true);
// If remote access is requested require it to be able to read the field
if (remote !== this.location) {
this._checkAccess(name, def, remote, ConfigAccess.read, true);
}
// Require that the field is either read or writable locally so that you can get the value
// back of write only fields recently written to.
if (
!this._checkAccess(name, def, this.location, ConfigAccess.write, false)
&& !this._checkAccess(name, def, this.location, ConfigAccess.read, false)
) {
throw new InvalidAccess(`Field '${name}' is not accessible from ${this.location}`);
}
return this.fields[name];
}
@@ -377,22 +501,25 @@ export class Config<
/**
* Set value of field
*
* The field must be defined for the config group and the value is
* The field must be defined for the config and the value is
* type checked against the field definition.
*
* @param name - Name of field to set.
* @param newValue - Value to set for field.
* @param location - Location used for access control.
* @param remote - Location this field is being set from.
* @throws {InvalidField} if field is not defined.
* @throws {InvalidValue} if value is not allowed for the field.
*/
set<Field extends keyof Fields & string>(name: Field, newValue: Fields[Field], location = this.location) {
set<Field extends keyof Fields & string>(name: Field, newValue: Fields[Field], remote = this.location) {
let value: FieldValue = newValue;
let def = this.constructor.fieldDefinitions[name];
if (!def) {
throw new InvalidField(`No field named '${name}'`);
}
this._checkAccess(name, def, location, true);
this._checkAccess(name, def, remote, ConfigAccess.write, true);
if (remote !== this.location) {
this._checkAccess(name, def, this.location, ConfigAccess.write, true);
}
// Empty strings are treates as null
if (value === "") {
@@ -435,24 +562,19 @@ export class Config<
throw new InvalidValue(`Expected type of ${name} to be ${def.type}, not ${basicType(value)}`);
}
let prev = this.fields[name];
this.fields[name] = value as Fields[Field];
if (!isDeepStrictEqual(value, prev)) {
this.dirty = true;
this.emit("fieldChanged", name, value, prev);
}
this._set(name, value);
}
/**
* Set property of object field
*
* Update value of stored object field by setting the specified property
* on it. The field must be defined as an object for the config group.
* on it. The field must be defined as an object for the config.
*
* @param name - Name of field to set property on.
* @param prop - Name of property to set on field.
* @param value - the value to set the property to.
* @param location - Location used for access control.
* @param remote - Location this property is being set from
* @throws {InvalidField} if field is not defined.
* @throws {InvalidValue} if field is not an object.
*/
@@ -460,13 +582,16 @@ export class Config<
name: Field,
prop: string,
value?: unknown,
location = this.location,
remote = this.location,
) {
let def = this.constructor.fieldDefinitions[name];
if (!def) {
throw new InvalidField(`No field named '${name}'`);
}
this._checkAccess(name, def, location, true);
this._checkAccess(name, def, remote, ConfigAccess.write, true);
if (remote !== this.location) {
this._checkAccess(name, def, this.location, ConfigAccess.write, true);
}
if (def.type !== "object") {
throw new InvalidValue(`Cannot set property on non-object field '${name}'`);
@@ -480,10 +605,7 @@ export class Config<
} else {
delete updated[prop];
}
this.fields[name] = updated as Fields[Field];
if (!isDeepStrictEqual(updated, prev)) {
this.dirty = true;
this.emit("fieldChanged", name, updated, prev);
}
this._set(name, updated);
}
}
+219 -23
View File
@@ -1,9 +1,13 @@
// Core definitions for the configuration system
import * as classes from "./classes";
import type { PluginNodeEnvInfo, PluginWebpackEnvInfo } from "../plugin";
import { Static } from "@sinclair/typebox";
type configFromJSON<T> = (...args: Parameters<typeof classes.Config.fromJSON>) => T;
type configFromFile<T> = (...args: Parameters<typeof classes.Config.fromFile>) => Promise<T>;
export interface ControllerConfigFields {
"controller.version": string;
"controller.name": string;
"controller.mods_directory": string;
"controller.database_directory": string;
@@ -11,7 +15,7 @@ export interface ControllerConfigFields {
"controller.https_port": number | null;
"controller.bind_address": string | null;
"controller.trusted_proxies": string | null;
"controller.external_address": string | null;
"controller.public_url": string | null;
"controller.tls_certificate": string | null;
"controller.tls_private_key": string | null;
"controller.auth_secret": string;
@@ -20,9 +24,17 @@ export interface ControllerConfigFields {
"controller.metrics_timeout": number;
"controller.system_metrics_interval": number;
"controller.proxy_stream_timeout": number;
"controller.factorio_username": string | null;
"controller.factorio_token": string | null;
"controller.share_factorio_credentials_with_hosts": boolean;
"controller.default_mod_pack_id": number | null;
"controller.default_role_id": number | null;
"controller.autosave_interval": number;
"controller.mod_portal_cache_duration_minutes": number;
"controller.mod_portal_page_size": number;
"controller.allow_remote_updates": boolean;
"controller.allow_plugin_updates": boolean;
"controller.allow_plugin_install": boolean;
}
/**
@@ -30,8 +42,24 @@ export interface ControllerConfigFields {
* @extends classes.Config
*/
export class ControllerConfig extends classes.Config<ControllerConfigFields> {
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => ControllerConfig;
declare static fromJSON: configFromJSON<ControllerConfig>;
declare static fromFile: configFromFile<ControllerConfig>;
static migrations(config: Static<typeof this.jsonSchema>) {
if (config.hasOwnProperty("controller.external_address")) {
config["controller.public_url"] = config["controller.external_address"];
delete config["controller.external_address"];
}
return config;
}
static fieldDefinitions: classes.ConfigDefs<ControllerConfigFields> = {
"controller.version": {
type: "string",
initialValue: "", // Set on start
readonly: ["controller"],
hidden: true,
},
"controller.name": {
title: "Name",
description: "Name of the cluster.",
@@ -86,9 +114,9 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
type: "string",
optional: true,
},
"controller.external_address": {
title: "External Address",
description: "Public facing address the controller is hosted on.",
"controller.public_url": {
title: "Public URL",
description: "Public facing URL the controller is hosted on, including the protocol.",
type: "string",
optional: true,
},
@@ -147,6 +175,29 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
type: "number",
initialValue: 15,
},
"controller.factorio_username": {
title: "Factorio Username",
description: "Username to authenticate with Factorio API with.",
autoComplete: "section-factorio username",
type: "string",
optional: true,
},
"controller.factorio_token": {
title: "Factorio Token",
description: "Token to authenticate with Factorio API with.",
autoComplete: "section-factorio new-password",
type: "string",
credential: ["controller"],
optional: true,
},
"controller.share_factorio_credentials_with_hosts": {
title: "Share Factorio credentials with Hosts",
description:
"If enabled, the Factorio Username and Token will be shared with hosts and " +
"used in the server settings of instances in this cluster.",
type: "boolean",
initialValue: true,
},
"controller.default_mod_pack_id": {
title: "Default Mod Pack",
description: "Mod pack used by default for instances.",
@@ -157,14 +208,49 @@ export class ControllerConfig extends classes.Config<ControllerConfigFields> {
"controller.default_role_id": {
title: "Default role",
description: "ID of role assigned by default to new users.",
inputComponent: "role",
type: "number",
optional: true,
initialValue: 1,
},
"controller.mod_portal_cache_duration_minutes": {
title: "Mod Portal Cache Duration",
description: "Duration in minutes to cache mod portal API responses.",
type: "number",
initialValue: 30,
},
"controller.mod_portal_page_size": {
title: "Mod Portal Page Size",
description: "Maximum number of results per page when querying the Factorio mod portal API.",
type: "number",
initialValue: 1000,
},
"controller.allow_remote_updates": {
description: "When true, allows a remote event to trigger a clusterio update via npm",
type: "boolean",
initialValue: true,
readonly: ["controller"],
hidden: true,
},
"controller.allow_plugin_updates": {
description: "When true, allows a remote event to trigger a plugin update via npm",
type: "boolean",
initialValue: true,
readonly: ["controller"],
hidden: true,
},
"controller.allow_plugin_install": {
description: "When true, allows a remote event to trigger a plugin install via npm",
type: "boolean",
initialValue: false,
readonly: ["controller"],
hidden: true,
},
};
}
export interface HostConfigFields {
"host.version": string;
"host.name": string;
"host.id": number;
"host.factorio_directory": string;
@@ -175,7 +261,12 @@ export interface HostConfigFields {
"host.tls_ca": string | null;
"host.public_address": string;
"host.factorio_port_range": string;
"host.factorio_username": string | null,
"host.factorio_token": string | null,
"host.max_reconnect_delay": number;
"host.allow_remote_updates": boolean;
"host.allow_plugin_updates": boolean;
"host.allow_plugin_install": boolean;
}
/**
@@ -183,8 +274,15 @@ export interface HostConfigFields {
* @extends classes.Config
*/
export class HostConfig extends classes.Config<HostConfigFields> {
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => HostConfig;
declare static fromJSON: configFromJSON<HostConfig>;
declare static fromFile: configFromFile<HostConfig>;
static fieldDefinitions: classes.ConfigDefs<HostConfigFields> = {
"host.version": {
type: "string",
initialValue: "", // Set on start
readonly: ["host"],
hidden: true,
},
"host.name": {
description: "Name of the host",
type: "string",
@@ -194,6 +292,7 @@ export class HostConfig extends classes.Config<HostConfigFields> {
description: "ID of the host",
type: "number",
initialValue: () => Math.random() * 2**31 | 0,
hidden: true,
},
"host.factorio_directory": {
description: "Path to directory to look for factorio installs",
@@ -245,12 +344,52 @@ export class HostConfig extends classes.Config<HostConfigFields> {
type: "string",
initialValue: "34100-34199",
},
"host.factorio_username": {
title: "Factorio Username",
description:
"Username to authenticate with Factorio API with. If set this will be used in the server settings " +
"of all instances on this host.",
autoComplete: "section-factorio username",
type: "string",
optional: true,
},
"host.factorio_token": {
title: "Factorio Token",
description:
"Token to authenticate with Factorio API with. If set this will be used in the server settings " +
"of all instances on this host.",
autoComplete: "section-factorio new-password",
type: "string",
credential: ["host"],
optional: true,
},
"host.max_reconnect_delay": {
title: "Max Reconnect Delay",
description: "Maximum delay to wait before attempting to reconnect WebSocket",
type: "number",
initialValue: 60,
},
"host.allow_remote_updates": {
description: "When true, allows a remote event to trigger a clusterio update via npm",
type: "boolean",
initialValue: true,
readonly: ["host"],
hidden: true,
},
"host.allow_plugin_updates": {
description: "When true, allows a remote event to trigger a plugin update via npm",
type: "boolean",
initialValue: true,
readonly: ["host"],
hidden: true,
},
"host.allow_plugin_install": {
description: "When true, allows a remote event to trigger a plugin install via npm",
type: "boolean",
initialValue: false,
readonly: ["host"],
hidden: true,
},
};
}
@@ -261,6 +400,8 @@ export interface InstanceConfigFields {
"instance.auto_start": boolean;
"factorio.version": string;
"factorio.executable_path": string | null;
"factorio.shutdown_timeout": number;
"factorio.game_port": number | null;
"factorio.host_assigned_game_port": number | null;
"factorio.rcon_port": number | null;
@@ -268,14 +409,16 @@ export interface InstanceConfigFields {
"factorio.player_online_autosave_slots": number;
"factorio.mod_pack_id": number | null;
"factorio.enable_save_patching": boolean;
"factorio.enable_script_commands": boolean;
"factorio.enable_whitelist": boolean;
"factorio.enable_authserver_bans": boolean;
"factorio.settings": Record<string, unknown>;
"factorio.verbose_logging": boolean;
"factorio.console_logging": boolean;
"factorio.strip_paths": boolean;
"factorio.sync_adminlist": boolean;
"factorio.sync_whitelist": boolean;
"factorio.sync_banlist": boolean;
"factorio.sync_adminlist": "enabled" | "disabled" | "bidirectional";
"factorio.sync_whitelist": "enabled" | "disabled" | "bidirectional";
"factorio.sync_banlist": "enabled" | "disabled" | "bidirectional";
"factorio.max_concurrent_commands": number;
}
@@ -285,7 +428,22 @@ export interface InstanceConfigFields {
* @extends classes.Config
*/
export class InstanceConfig extends classes.Config<InstanceConfigFields> {
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => InstanceConfig;
declare static fromJSON: configFromJSON<InstanceConfig>;
declare static fromFile: configFromFile<InstanceConfig>;
static migrations(config: Static<typeof this.jsonSchema>) {
function boolToEnableDisable(name: string) {
if (config.hasOwnProperty(name) && typeof config[name] === "boolean") {
config[name] = config[name] ? "enabled" : "disabled";
}
}
boolToEnableDisable("factorio.sync_adminlist");
boolToEnableDisable("factorio.sync_whitelist");
boolToEnableDisable("factorio.sync_banlist");
return config;
}
static fieldDefinitions: classes.ConfigDefs<InstanceConfigFields> = {
"instance.name": {
type: "string",
@@ -295,10 +453,12 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
description: "ID of the instance",
type: "number",
initialValue: () => Math.random() * 2**31 | 0,
hidden: true,
},
"instance.assigned_host": {
type: "number",
optional: true,
hidden: true,
},
"instance.auto_start": {
description: "Automatically start this instance when the host hosting it is started up",
@@ -311,6 +471,22 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
type: "string",
initialValue: "latest",
},
"factorio.executable_path": {
description:
"Relative path from the Factorio installation directory to the executable to run. " +
"Defaults to auto detect the path, only needed in special setups.",
restartRequired: true,
type: "string",
optional: true,
},
"factorio.shutdown_timeout": {
description:
"Timeout in seconds to wait after requesting the server to stop before killing " +
"the process. Set to 0 to disable.",
type: "number",
initialValue: 300,
optional: true,
},
"factorio.game_port": {
description: "UDP port to run game on, uses a port in host.factorio_port_range if null",
restartRequired: true,
@@ -329,7 +505,8 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
optional: true,
},
"factorio.rcon_password": {
description: "Password for RCON, randomly generated if null",
credential: ["host", "controller"],
description: "Password for RCON, randomly generated if null.",
restartRequired: true,
type: "string",
optional: true,
@@ -357,14 +534,23 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
type: "boolean",
initialValue: true,
},
"factorio.enable_script_commands": {
description:
"Allows achievement breaking commands to be executed over rcon. " +
"Required for Clusterio integrations and most plugins. " +
"This does not prevent players using script commands.",
type: "boolean",
initialValue: true,
},
"factorio.enable_whitelist": {
description: "Turn on whitelist for joining the server.",
restartRequired: true, // Because of the cli option "--use-server-whitelist"
type: "boolean",
initialValue: false,
},
"factorio.enable_authserver_bans": {
description: "Turn on Factorio.com based multiplayer bans.",
restartRequired: true,
restartRequired: true, // Because of the cli option "--use-authserver-bans"
type: "boolean",
initialValue: false,
},
@@ -382,7 +568,13 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
},
"factorio.verbose_logging": {
description: "Enable verbose logging on the Factorio server",
restartRequired: true,
restartRequired: true, // Because of the cli option "--verbose"
type: "boolean",
initialValue: false,
},
"factorio.console_logging": {
description: "Enable console logging to a separate file, useful for 3rd party integrations",
restartRequired: true, // Because of the cli option "--console-log"
type: "boolean",
initialValue: false,
},
@@ -393,19 +585,22 @@ export class InstanceConfig extends classes.Config<InstanceConfigFields> {
initialValue: true,
},
"factorio.sync_adminlist": {
description: "Synchronize adminlist with controller",
type: "boolean",
initialValue: true,
description: "Synchronize adminlist with the controller",
type: "string",
enum: ["disabled", "enabled", "bidirectional"],
initialValue: "bidirectional",
},
"factorio.sync_whitelist": {
description: "Synchronize whitelist with controller",
type: "boolean",
initialValue: true,
description: "Synchronize whitelist with the controller (whitelist must be enabled)",
type: "string",
enum: ["disabled", "enabled", "bidirectional"],
initialValue: "bidirectional",
},
"factorio.sync_banlist": {
description: "Synchronize banlist with controller",
type: "boolean",
initialValue: true,
description: "Synchronize banlist with the controller",
type: "string",
enum: ["disabled", "enabled", "bidirectional"],
initialValue: "bidirectional",
},
"factorio.max_concurrent_commands": {
description: "Maximum number of RCON commands trasmitted in parallel",
@@ -428,7 +623,8 @@ export interface ControlConfigFields {
* @extends classes.Config
*/
export class ControlConfig extends classes.Config<ControlConfigFields> {
declare static fromJSON: (json: classes.ConfigSchema, location: classes.ConfigLocation) => ControlConfig;
declare static fromJSON: configFromJSON<ControlConfig>;
declare static fromFile: configFromFile<ControlConfig>;
static fieldDefinitions: classes.ConfigDefs<ControlConfigFields> = {
"control.controller_url": {
description: "URL to connect to the controller at",
+9
View File
@@ -116,6 +116,11 @@ export default class ModInfo {
*/
size = 0;
/**
* Last modification time of the file.
*/
mtimeMs = 0;
/**
* SHA1 hash of this mod
*/
@@ -147,6 +152,7 @@ export default class ModInfo {
static jsonSchema = Type.Object({
...this.infoJsonSchema.properties,
"size": Type.Optional(Type.Integer()),
"mtime_ms": Type.Optional(Type.Number()),
"sha1": Type.Optional(Type.String()),
"updated_at_ms": Type.Optional(Type.Number()),
"is_deleted": Type.Optional(Type.Boolean()),
@@ -170,6 +176,7 @@ export default class ModInfo {
// Additional data
if (json.size) { modInfo.size = json.size; }
if (json.mtime_ms) { modInfo.mtimeMs = json.mtime_ms; }
if (json.sha1) { modInfo.sha1 = json.sha1; }
if (json.updated_at_ms) { modInfo.updatedAtMs = json.updated_at_ms; }
if (json.is_deleted) { modInfo.isDeleted = json.is_deleted; }
@@ -192,6 +199,7 @@ export default class ModInfo {
json.dependencies = this.dependencies;
}
if (this.size) { json.size = this.size; }
if (this.mtimeMs) { json.mtime_ms = this.mtimeMs; }
if (this.sha1) { json.sha1 = this.sha1; }
if (this.updatedAtMs) { json.updated_at_ms = this.updatedAtMs; }
if (this.isDeleted) { json.is_deleted = this.isDeleted; }
@@ -229,6 +237,7 @@ export default class ModInfo {
size: stat.size,
sha1: await libHash.hashFile(modPath),
mtime_ms: stat.mtimeMs,
updated_at_ms: stat.mtimeMs,
is_deleted: false,
});
+71 -11
View File
@@ -173,8 +173,11 @@ export default class ModPack {
if (json.updated_at_ms) { modPack.updatedAtMs = json.updated_at_ms; }
if (json.is_deleted) { modPack.isDeleted = json.is_deleted; }
if (!modPack.mods.has("base")) {
modPack.mods.set("base", { name: "base", enabled: true, version: modPack.factorioVersion });
const builtinMods = ModPack.getBuiltinMods(modPack.factorioVersion);
for (const builtinMod of builtinMods) {
if (!modPack.mods.has(builtinMod.name)) {
modPack.mods.set(builtinMod.name, builtinMod);
}
}
return modPack;
@@ -334,10 +337,11 @@ export default class ModPack {
}
/**
* Fill in missing settings with their default values
* Fill in missing and incorrect settings with their default values
*
* Uses the provided setting prototypes to add any missing mod settings
* in the mod pack with the default value from the prototype.
* Uses the provided setting prototypes to correct any missing and or
* incorrect mod settings in the mod pack with the default value from
* the prototype.
*
* @param settingPrototypes -
* Setting prototypes exported from the game.
@@ -360,15 +364,71 @@ export default class ModPack {
logger.warn(`Ignoring ${prototype.name} with missing default_value`);
continue;
}
if (this.settings[settingType].get(prototype.name)) {
continue;
let value = this.settings[settingType].get(prototype.name)?.value ?? prototype.default_value;
const type = prototype.type;
if (
type === "bool-setting" && typeof value !== "boolean"
|| type === "color-setting" && typeof value !== "object"
|| (type === "int-setting" || type === "double-setting") && typeof value !== "number"
) {
value = prototype.default_value;
}
let value = prototype.default_value;
if (prototype.type === "color-setting") {
value = libLuaTools.normalizeColor(value);
if (type === "string-setting" && typeof value !== "string") {
value = typeof value === "object" ? prototype.default_value : String(value ?? "");
}
if (type === "color-setting") {
value = libLuaTools.normalizeColor(value as object);
}
this.settings[settingType].set(prototype.name, { value });
}
}
/** Array of default mod packs which should exist on a newly installed cluster */
static defaultModPacks = [
...["0.17", "0.18", "1.0", "1.1", "2.0"]
.map(version => this.fromJSON({
name: `Base Game ${version}`,
description: `Factorio ${version} with no extra mods.`,
factorio_version: version,
} as any)),
...["2.0"]
.map(version => this.fromJSON({
name: `Space Age ${version}`,
description: `Factorio ${version} with Space Age expansion.`,
factorio_version: version,
mods: [
{ name: "elevated-rails", enabled: true, version },
{ name: "quality", enabled: true, version },
{ name: "space-age", enabled: true, version },
],
} as any)),
];
/**
* Returns an array of ModInfo containing all mods included in factorio
*
* @param factorioVersion - Factorio version to get the default mods for
* @return Built in mods for the given version
*/
static getBuiltinMods(factorioVersion: string) {
let defaultMods: ModRecord[] = [
// "core" not included because core cannot be disabled
{ name: "base", enabled: true, version: factorioVersion },
];
const integerVersion = integerFactorioVersion(factorioVersion);
if (integerVersion >= integerFactorioVersion("1.2")) {
defaultMods = defaultMods.concat([
{ name: "elevated-rails", enabled: false, version: factorioVersion },
{ name: "quality", enabled: false, version: factorioVersion },
{ name: "space-age", enabled: false, version: factorioVersion },
]);
}
return defaultMods;
}
static getBuiltinModNames(factorioVersion: string) {
return this.getBuiltinMods(factorioVersion).map(builtinMod => builtinMod.name);
}
}
Loaded 100 of 265 files, more files were not shown because too many files have changed in this diff. Show more