-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AutoConfig #3723
AutoConfig #3723
Conversation
Hooray Jenkins reported success with all tests good! |
2 similar comments
Hooray Jenkins reported success with all tests good! |
Hooray Jenkins reported success with all tests good! |
Hooray Jenkins reported success with all tests good! |
1 similar comment
Hooray Jenkins reported success with all tests good! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm convinced by this changeset, some minor things that could be done/added/changed, but it could also be done in another iteration.
Thanks a lot @eviltak
engine/src/main/java/org/terasology/config/flexible/AutoConfig.java
Outdated
Show resolved
Hide resolved
engine/src/main/java/org/terasology/config/flexible/AutoConfigManager.java
Outdated
Show resolved
Hide resolved
engine/src/main/java/org/terasology/config/flexible/AutoConfigManager.java
Outdated
Show resolved
Hide resolved
engine/src/main/java/org/terasology/config/flexible/SettingArgument.java
Show resolved
Hide resolved
engine/src/main/java/org/terasology/config/flexible/internal/SettingBuilder.java
Show resolved
Hide resolved
engine/src/main/java/org/terasology/engine/subsystem/common/ConfigurationSubsystem.java
Show resolved
Hide resolved
engine/src/main/java/org/terasology/config/flexible/internal/SettingImplBuilder.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested, played around with the values to explore different available options, and with the exception of the "description" key (which is intentionally pending for UI to deal with) everything functioned as expected.
Rebased, please re-fetch the branch. |
Uh oh, something went wrong with the build. Need to check on that |
Hooray Jenkins reported success with all tests good! |
Motivation
The goal of FCs (as listed in #2668) was to allow the "engine and modules [to be] able to publish their configuration settings, as needed." This is currently achieved by creating a map of settings which can be added to and accessed at runtime (see #2757). However, after looking at #3258 and existing config classes, it looks like that settings will only ever be accessed dynamically at runtime -- there shouldn't ever be a need for settings to be published at runtime, since all the settings to be published should be known at the time of writing.
This is evident from the fact that all configs are currently stored as hardcoded classes, with concrete fields as the "settings". FlexibleConfigs, thus, might be a bit too flexible for our uses! This PR thus proposes a hybrid approach, one which addresses all the shortcomings of both the FC and the concrete class approaches while also in my opinion being the most suitable to our use case.
The new approach
The hybrid approach involves using concrete config classes (which are marked as such by inheriting the
AutoConfig
class) with public final fields that are of typeSetting
(the FC setting). Here is theAudioConfig
as an actual, usableAutoConfig
in action (no seriously, that is all that is required to publish an AutoConfig 😄):Terasology/engine/src/main/java/org/terasology/config/AudioConfig.java
Lines 26 to 44 in dfec0c3
Notes
AudioConfig
to an AutoConfig as a proof-of-concept -- other engine configs will be covered in future PRsFlexibleConfig
and all related types with the intention of replacing them withAutoConfig
, but it is possible for both to coexist if needed/homeDir/configs
under a subdirectory named after the module declaring themThe DSL
Rather than use the newly introduced
FlexibleConfig.SettingEntry
API, settings are constructed using a DSL of sorts, enumerated by theAutoConfig.setting
static method and theSettingArgument
class (and its static factory methods). Couple of examples are above inAudioConfig
.Another example using all setting parameters:
Terasology/engine-tests/src/test/java/org/terasology/config/flexible/AutoConfigTest.java
Lines 54 to 60 in 6d2ec48
All the methods used are static and stateless. They can also be statically imported to make setting declaration easy to read.
The DSL is also fully type-checked and compiler enforced; it is also possible to make some parameters compulsory (as is done for the
type
anddefaultValue
parameters) and the others optional. It is easy to employ this DSL in FCs too, if needed.Advantages
Versus FCs
Versus concrete classes (currently used)
AutoConfig
base type are treated as a config class and stored in aAutoConfigManager
.AutoConfigManager
then looks through the fields of each loadedConfig
and recognizes those fields with the typeSetting
as a setting in the config and handles the UI/serialization automagically.AutoConfig
and publish settings in its fields.This will work very much like
ComponentSystem
andComponentSystemManager
do -- to create aComponentSystem
, all a module does is create a class that inherits theComponentSystem
interface. AllComponentSystem
s in theModuleEnvironment
are loaded by theComponentSystemManager
, and are initialized/handled/called as needed.Config
class needs to do to publish a setting is to declare a public final field of typeSetting
and initialize it with the constraints and the defaults; no other methods needed.Setting
interface encapsulates all of the responsibilities that the getters and setters currently fulfill via thegetValue
andsetValue
methods.This includes checking if the value is valid (via the
SettingConstraint
) and notifying subscribers of any changes.AutoConfig
base type could also contain helpful utility methods likesubscribeAll
andunsubscribeAll
, which subscribes and unsubscribes a listener to and from all settings in a config.Future work