mirror of
https://github.com/sciwhiz12/Janitor.git
synced 2024-11-09 22:51:26 +00:00
Rework config system to use a config spec
This commit is contained in:
parent
412c499cfc
commit
cbf57fb585
460
LICENSE-lgpl2.1.txt
Normal file
460
LICENSE-lgpl2.1.txt
Normal file
|
@ -0,0 +1,460 @@
|
||||||
|
|
||||||
|
|
||||||
|
GNU LESSER GENERAL PUBLIC LICENSE
|
||||||
|
Version 2.1, February 1999
|
||||||
|
|
||||||
|
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
|
||||||
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
Everyone is permitted to copy and distribute verbatim copies
|
||||||
|
of this license document, but changing it is not allowed.
|
||||||
|
|
||||||
|
[This is the first released version of the Lesser GPL. It also counts
|
||||||
|
as the successor of the GNU Library Public License, version 2, hence
|
||||||
|
the version number 2.1.]
|
||||||
|
|
||||||
|
Preamble
|
||||||
|
|
||||||
|
The licenses for most software are designed to take away your
|
||||||
|
freedom to share and change it. By contrast, the GNU General Public
|
||||||
|
Licenses are intended to guarantee your freedom to share and change
|
||||||
|
free software--to make sure the software is free for all its users.
|
||||||
|
|
||||||
|
This license, the Lesser General Public License, applies to some
|
||||||
|
specially designated software packages--typically libraries--of the
|
||||||
|
Free Software Foundation and other authors who decide to use it. You
|
||||||
|
can use it too, but we suggest you first think carefully about whether
|
||||||
|
this license or the ordinary General Public License is the better
|
||||||
|
strategy to use in any particular case, based on the explanations below.
|
||||||
|
|
||||||
|
When we speak of free software, we are referring to freedom of use,
|
||||||
|
not price. Our General Public Licenses are designed to make sure that
|
||||||
|
you have the freedom to distribute copies of free software (and charge
|
||||||
|
for this service if you wish); that you receive source code or can get
|
||||||
|
it if you want it; that you can change the software and use pieces of
|
||||||
|
it in new free programs; and that you are informed that you can do
|
||||||
|
these things.
|
||||||
|
|
||||||
|
To protect your rights, we need to make restrictions that forbid
|
||||||
|
distributors to deny you these rights or to ask you to surrender these
|
||||||
|
rights. These restrictions translate to certain responsibilities for
|
||||||
|
you if you distribute copies of the library or if you modify it.
|
||||||
|
|
||||||
|
For example, if you distribute copies of the library, whether gratis
|
||||||
|
or for a fee, you must give the recipients all the rights that we gave
|
||||||
|
you. You must make sure that they, too, receive or can get the source
|
||||||
|
code. If you link other code with the library, you must provide
|
||||||
|
complete object files to the recipients, so that they can relink them
|
||||||
|
with the library after making changes to the library and recompiling
|
||||||
|
it. And you must show them these terms so they know their rights.
|
||||||
|
|
||||||
|
We protect your rights with a two-step method: (1) we copyright the
|
||||||
|
library, and (2) we offer you this license, which gives you legal
|
||||||
|
permission to copy, distribute and/or modify the library.
|
||||||
|
|
||||||
|
To protect each distributor, we want to make it very clear that
|
||||||
|
there is no warranty for the free library. Also, if the library is
|
||||||
|
modified by someone else and passed on, the recipients should know
|
||||||
|
that what they have is not the original version, so that the original
|
||||||
|
author's reputation will not be affected by problems that might be
|
||||||
|
introduced by others.
|
||||||
|
|
||||||
|
Finally, software patents pose a constant threat to the existence of
|
||||||
|
any free program. We wish to make sure that a company cannot
|
||||||
|
effectively restrict the users of a free program by obtaining a
|
||||||
|
restrictive license from a patent holder. Therefore, we insist that
|
||||||
|
any patent license obtained for a version of the library must be
|
||||||
|
consistent with the full freedom of use specified in this license.
|
||||||
|
|
||||||
|
Most GNU software, including some libraries, is covered by the
|
||||||
|
ordinary GNU General Public License. This license, the GNU Lesser
|
||||||
|
General Public License, applies to certain designated libraries, and
|
||||||
|
is quite different from the ordinary General Public License. We use
|
||||||
|
this license for certain libraries in order to permit linking those
|
||||||
|
libraries into non-free programs.
|
||||||
|
|
||||||
|
When a program is linked with a library, whether statically or using
|
||||||
|
a shared library, the combination of the two is legally speaking a
|
||||||
|
combined work, a derivative of the original library. The ordinary
|
||||||
|
General Public License therefore permits such linking only if the
|
||||||
|
entire combination fits its criteria of freedom. The Lesser General
|
||||||
|
Public License permits more lax criteria for linking other code with
|
||||||
|
the library.
|
||||||
|
|
||||||
|
We call this license the "Lesser" General Public License because it
|
||||||
|
does Less to protect the user's freedom than the ordinary General
|
||||||
|
Public License. It also provides other free software developers Less
|
||||||
|
of an advantage over competing non-free programs. These disadvantages
|
||||||
|
are the reason we use the ordinary General Public License for many
|
||||||
|
libraries. However, the Lesser license provides advantages in certain
|
||||||
|
special circumstances.
|
||||||
|
|
||||||
|
For example, on rare occasions, there may be a special need to
|
||||||
|
encourage the widest possible use of a certain library, so that it becomes
|
||||||
|
a de-facto standard. To achieve this, non-free programs must be
|
||||||
|
allowed to use the library. A more frequent case is that a free
|
||||||
|
library does the same job as widely used non-free libraries. In this
|
||||||
|
case, there is little to gain by limiting the free library to free
|
||||||
|
software only, so we use the Lesser General Public License.
|
||||||
|
|
||||||
|
In other cases, permission to use a particular library in non-free
|
||||||
|
programs enables a greater number of people to use a large body of
|
||||||
|
free software. For example, permission to use the GNU C Library in
|
||||||
|
non-free programs enables many more people to use the whole GNU
|
||||||
|
operating system, as well as its variant, the GNU/Linux operating
|
||||||
|
system.
|
||||||
|
|
||||||
|
Although the Lesser General Public License is Less protective of the
|
||||||
|
users' freedom, it does ensure that the user of a program that is
|
||||||
|
linked with the Library has the freedom and the wherewithal to run
|
||||||
|
that program using a modified version of the Library.
|
||||||
|
|
||||||
|
The precise terms and conditions for copying, distribution and
|
||||||
|
modification follow. Pay close attention to the difference between a
|
||||||
|
"work based on the library" and a "work that uses the library". The
|
||||||
|
former contains code derived from the library, whereas the latter must
|
||||||
|
be combined with the library in order to run.
|
||||||
|
|
||||||
|
GNU LESSER GENERAL PUBLIC LICENSE
|
||||||
|
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
||||||
|
|
||||||
|
0. This License Agreement applies to any software library or other
|
||||||
|
program which contains a notice placed by the copyright holder or
|
||||||
|
other authorized party saying it may be distributed under the terms of
|
||||||
|
this Lesser General Public License (also called "this License").
|
||||||
|
Each licensee is addressed as "you".
|
||||||
|
|
||||||
|
A "library" means a collection of software functions and/or data
|
||||||
|
prepared so as to be conveniently linked with application programs
|
||||||
|
(which use some of those functions and data) to form executables.
|
||||||
|
|
||||||
|
The "Library", below, refers to any such software library or work
|
||||||
|
which has been distributed under these terms. A "work based on the
|
||||||
|
Library" means either the Library or any derivative work under
|
||||||
|
copyright law: that is to say, a work containing the Library or a
|
||||||
|
portion of it, either verbatim or with modifications and/or translated
|
||||||
|
straightforwardly into another language. (Hereinafter, translation is
|
||||||
|
included without limitation in the term "modification".)
|
||||||
|
|
||||||
|
"Source code" for a work means the preferred form of the work for
|
||||||
|
making modifications to it. For a library, complete source code means
|
||||||
|
all the source code for all modules it contains, plus any associated
|
||||||
|
interface definition files, plus the scripts used to control compilation
|
||||||
|
and installation of the library.
|
||||||
|
|
||||||
|
Activities other than copying, distribution and modification are not
|
||||||
|
covered by this License; they are outside its scope. The act of
|
||||||
|
running a program using the Library is not restricted, and output from
|
||||||
|
such a program is covered only if its contents constitute a work based
|
||||||
|
on the Library (independent of the use of the Library in a tool for
|
||||||
|
writing it). Whether that is true depends on what the Library does
|
||||||
|
and what the program that uses the Library does.
|
||||||
|
|
||||||
|
1. You may copy and distribute verbatim copies of the Library's
|
||||||
|
complete source code as you receive it, in any medium, provided that
|
||||||
|
you conspicuously and appropriately publish on each copy an
|
||||||
|
appropriate copyright notice and disclaimer of warranty; keep intact
|
||||||
|
all the notices that refer to this License and to the absence of any
|
||||||
|
warranty; and distribute a copy of this License along with the
|
||||||
|
Library.
|
||||||
|
|
||||||
|
You may charge a fee for the physical act of transferring a copy,
|
||||||
|
and you may at your option offer warranty protection in exchange for a
|
||||||
|
fee.
|
||||||
|
|
||||||
|
2. You may modify your copy or copies of the Library or any portion
|
||||||
|
of it, thus forming a work based on the Library, and copy and
|
||||||
|
distribute such modifications or work under the terms of Section 1
|
||||||
|
above, provided that you also meet all of these conditions:
|
||||||
|
|
||||||
|
a) The modified work must itself be a software library.
|
||||||
|
|
||||||
|
b) You must cause the files modified to carry prominent notices
|
||||||
|
stating that you changed the files and the date of any change.
|
||||||
|
|
||||||
|
c) You must cause the whole of the work to be licensed at no
|
||||||
|
charge to all third parties under the terms of this License.
|
||||||
|
|
||||||
|
d) If a facility in the modified Library refers to a function or a
|
||||||
|
table of data to be supplied by an application program that uses
|
||||||
|
the facility, other than as an argument passed when the facility
|
||||||
|
is invoked, then you must make a good faith effort to ensure that,
|
||||||
|
in the event an application does not supply such function or
|
||||||
|
table, the facility still operates, and performs whatever part of
|
||||||
|
its purpose remains meaningful.
|
||||||
|
|
||||||
|
(For example, a function in a library to compute square roots has
|
||||||
|
a purpose that is entirely well-defined independent of the
|
||||||
|
application. Therefore, Subsection 2d requires that any
|
||||||
|
application-supplied function or table used by this function must
|
||||||
|
be optional: if the application does not supply it, the square
|
||||||
|
root function must still compute square roots.)
|
||||||
|
|
||||||
|
These requirements apply to the modified work as a whole. If
|
||||||
|
identifiable sections of that work are not derived from the Library,
|
||||||
|
and can be reasonably considered independent and separate works in
|
||||||
|
themselves, then this License, and its terms, do not apply to those
|
||||||
|
sections when you distribute them as separate works. But when you
|
||||||
|
distribute the same sections as part of a whole which is a work based
|
||||||
|
on the Library, the distribution of the whole must be on the terms of
|
||||||
|
this License, whose permissions for other licensees extend to the
|
||||||
|
entire whole, and thus to each and every part regardless of who wrote
|
||||||
|
it.
|
||||||
|
|
||||||
|
Thus, it is not the intent of this section to claim rights or contest
|
||||||
|
your rights to work written entirely by you; rather, the intent is to
|
||||||
|
exercise the right to control the distribution of derivative or
|
||||||
|
collective works based on the Library.
|
||||||
|
|
||||||
|
In addition, mere aggregation of another work not based on the Library
|
||||||
|
with the Library (or with a work based on the Library) on a volume of
|
||||||
|
a storage or distribution medium does not bring the other work under
|
||||||
|
the scope of this License.
|
||||||
|
|
||||||
|
3. You may opt to apply the terms of the ordinary GNU General Public
|
||||||
|
License instead of this License to a given copy of the Library. To do
|
||||||
|
this, you must alter all the notices that refer to this License, so
|
||||||
|
that they refer to the ordinary GNU General Public License, version 2,
|
||||||
|
instead of to this License. (If a newer version than version 2 of the
|
||||||
|
ordinary GNU General Public License has appeared, then you can specify
|
||||||
|
that version instead if you wish.) Do not make any other change in
|
||||||
|
these notices.
|
||||||
|
|
||||||
|
Once this change is made in a given copy, it is irreversible for
|
||||||
|
that copy, so the ordinary GNU General Public License applies to all
|
||||||
|
subsequent copies and derivative works made from that copy.
|
||||||
|
|
||||||
|
This option is useful when you wish to copy part of the code of
|
||||||
|
the Library into a program that is not a library.
|
||||||
|
|
||||||
|
4. You may copy and distribute the Library (or a portion or
|
||||||
|
derivative of it, under Section 2) in object code or executable form
|
||||||
|
under the terms of Sections 1 and 2 above provided that you accompany
|
||||||
|
it with the complete corresponding machine-readable source code, which
|
||||||
|
must be distributed under the terms of Sections 1 and 2 above on a
|
||||||
|
medium customarily used for software interchange.
|
||||||
|
|
||||||
|
If distribution of object code is made by offering access to copy
|
||||||
|
from a designated place, then offering equivalent access to copy the
|
||||||
|
source code from the same place satisfies the requirement to
|
||||||
|
distribute the source code, even though third parties are not
|
||||||
|
compelled to copy the source along with the object code.
|
||||||
|
|
||||||
|
5. A program that contains no derivative of any portion of the
|
||||||
|
Library, but is designed to work with the Library by being compiled or
|
||||||
|
linked with it, is called a "work that uses the Library". Such a
|
||||||
|
work, in isolation, is not a derivative work of the Library, and
|
||||||
|
therefore falls outside the scope of this License.
|
||||||
|
|
||||||
|
However, linking a "work that uses the Library" with the Library
|
||||||
|
creates an executable that is a derivative of the Library (because it
|
||||||
|
contains portions of the Library), rather than a "work that uses the
|
||||||
|
library". The executable is therefore covered by this License.
|
||||||
|
Section 6 states terms for distribution of such executables.
|
||||||
|
|
||||||
|
When a "work that uses the Library" uses material from a header file
|
||||||
|
that is part of the Library, the object code for the work may be a
|
||||||
|
derivative work of the Library even though the source code is not.
|
||||||
|
Whether this is true is especially significant if the work can be
|
||||||
|
linked without the Library, or if the work is itself a library. The
|
||||||
|
threshold for this to be true is not precisely defined by law.
|
||||||
|
|
||||||
|
If such an object file uses only numerical parameters, data
|
||||||
|
structure layouts and accessors, and small macros and small inline
|
||||||
|
functions (ten lines or less in length), then the use of the object
|
||||||
|
file is unrestricted, regardless of whether it is legally a derivative
|
||||||
|
work. (Executables containing this object code plus portions of the
|
||||||
|
Library will still fall under Section 6.)
|
||||||
|
|
||||||
|
Otherwise, if the work is a derivative of the Library, you may
|
||||||
|
distribute the object code for the work under the terms of Section 6.
|
||||||
|
Any executables containing that work also fall under Section 6,
|
||||||
|
whether or not they are linked directly with the Library itself.
|
||||||
|
|
||||||
|
6. As an exception to the Sections above, you may also combine or
|
||||||
|
link a "work that uses the Library" with the Library to produce a
|
||||||
|
work containing portions of the Library, and distribute that work
|
||||||
|
under terms of your choice, provided that the terms permit
|
||||||
|
modification of the work for the customer's own use and reverse
|
||||||
|
engineering for debugging such modifications.
|
||||||
|
|
||||||
|
You must give prominent notice with each copy of the work that the
|
||||||
|
Library is used in it and that the Library and its use are covered by
|
||||||
|
this License. You must supply a copy of this License. If the work
|
||||||
|
during execution displays copyright notices, you must include the
|
||||||
|
copyright notice for the Library among them, as well as a reference
|
||||||
|
directing the user to the copy of this License. Also, you must do one
|
||||||
|
of these things:
|
||||||
|
|
||||||
|
a) Accompany the work with the complete corresponding
|
||||||
|
machine-readable source code for the Library including whatever
|
||||||
|
changes were used in the work (which must be distributed under
|
||||||
|
Sections 1 and 2 above); and, if the work is an executable linked
|
||||||
|
with the Library, with the complete machine-readable "work that
|
||||||
|
uses the Library", as object code and/or source code, so that the
|
||||||
|
user can modify the Library and then relink to produce a modified
|
||||||
|
executable containing the modified Library. (It is understood
|
||||||
|
that the user who changes the contents of definitions files in the
|
||||||
|
Library will not necessarily be able to recompile the application
|
||||||
|
to use the modified definitions.)
|
||||||
|
|
||||||
|
b) Use a suitable shared library mechanism for linking with the
|
||||||
|
Library. A suitable mechanism is one that (1) uses at run time a
|
||||||
|
copy of the library already present on the user's computer system,
|
||||||
|
rather than copying library functions into the executable, and (2)
|
||||||
|
will operate properly with a modified version of the library, if
|
||||||
|
the user installs one, as long as the modified version is
|
||||||
|
interface-compatible with the version that the work was made with.
|
||||||
|
|
||||||
|
c) Accompany the work with a written offer, valid for at
|
||||||
|
least three years, to give the same user the materials
|
||||||
|
specified in Subsection 6a, above, for a charge no more
|
||||||
|
than the cost of performing this distribution.
|
||||||
|
|
||||||
|
d) If distribution of the work is made by offering access to copy
|
||||||
|
from a designated place, offer equivalent access to copy the above
|
||||||
|
specified materials from the same place.
|
||||||
|
|
||||||
|
e) Verify that the user has already received a copy of these
|
||||||
|
materials or that you have already sent this user a copy.
|
||||||
|
|
||||||
|
For an executable, the required form of the "work that uses the
|
||||||
|
Library" must include any data and utility programs needed for
|
||||||
|
reproducing the executable from it. However, as a special exception,
|
||||||
|
the materials to be distributed need not include anything that is
|
||||||
|
normally distributed (in either source or binary form) with the major
|
||||||
|
components (compiler, kernel, and so on) of the operating system on
|
||||||
|
which the executable runs, unless that component itself accompanies
|
||||||
|
the executable.
|
||||||
|
|
||||||
|
It may happen that this requirement contradicts the license
|
||||||
|
restrictions of other proprietary libraries that do not normally
|
||||||
|
accompany the operating system. Such a contradiction means you cannot
|
||||||
|
use both them and the Library together in an executable that you
|
||||||
|
distribute.
|
||||||
|
|
||||||
|
7. You may place library facilities that are a work based on the
|
||||||
|
Library side-by-side in a single library together with other library
|
||||||
|
facilities not covered by this License, and distribute such a combined
|
||||||
|
library, provided that the separate distribution of the work based on
|
||||||
|
the Library and of the other library facilities is otherwise
|
||||||
|
permitted, and provided that you do these two things:
|
||||||
|
|
||||||
|
a) Accompany the combined library with a copy of the same work
|
||||||
|
based on the Library, uncombined with any other library
|
||||||
|
facilities. This must be distributed under the terms of the
|
||||||
|
Sections above.
|
||||||
|
|
||||||
|
b) Give prominent notice with the combined library of the fact
|
||||||
|
that part of it is a work based on the Library, and explaining
|
||||||
|
where to find the accompanying uncombined form of the same work.
|
||||||
|
|
||||||
|
8. You may not copy, modify, sublicense, link with, or distribute
|
||||||
|
the Library except as expressly provided under this License. Any
|
||||||
|
attempt otherwise to copy, modify, sublicense, link with, or
|
||||||
|
distribute the Library is void, and will automatically terminate your
|
||||||
|
rights under this License. However, parties who have received copies,
|
||||||
|
or rights, from you under this License will not have their licenses
|
||||||
|
terminated so long as such parties remain in full compliance.
|
||||||
|
|
||||||
|
9. You are not required to accept this License, since you have not
|
||||||
|
signed it. However, nothing else grants you permission to modify or
|
||||||
|
distribute the Library or its derivative works. These actions are
|
||||||
|
prohibited by law if you do not accept this License. Therefore, by
|
||||||
|
modifying or distributing the Library (or any work based on the
|
||||||
|
Library), you indicate your acceptance of this License to do so, and
|
||||||
|
all its terms and conditions for copying, distributing or modifying
|
||||||
|
the Library or works based on it.
|
||||||
|
|
||||||
|
10. Each time you redistribute the Library (or any work based on the
|
||||||
|
Library), the recipient automatically receives a license from the
|
||||||
|
original licensor to copy, distribute, link with or modify the Library
|
||||||
|
subject to these terms and conditions. You may not impose any further
|
||||||
|
restrictions on the recipients' exercise of the rights granted herein.
|
||||||
|
You are not responsible for enforcing compliance by third parties with
|
||||||
|
this License.
|
||||||
|
|
||||||
|
11. If, as a consequence of a court judgment or allegation of patent
|
||||||
|
infringement or for any other reason (not limited to patent issues),
|
||||||
|
conditions are imposed on you (whether by court order, agreement or
|
||||||
|
otherwise) that contradict the conditions of this License, they do not
|
||||||
|
excuse you from the conditions of this License. If you cannot
|
||||||
|
distribute so as to satisfy simultaneously your obligations under this
|
||||||
|
License and any other pertinent obligations, then as a consequence you
|
||||||
|
may not distribute the Library at all. For example, if a patent
|
||||||
|
license would not permit royalty-free redistribution of the Library by
|
||||||
|
all those who receive copies directly or indirectly through you, then
|
||||||
|
the only way you could satisfy both it and this License would be to
|
||||||
|
refrain entirely from distribution of the Library.
|
||||||
|
|
||||||
|
If any portion of this section is held invalid or unenforceable under any
|
||||||
|
particular circumstance, the balance of the section is intended to apply,
|
||||||
|
and the section as a whole is intended to apply in other circumstances.
|
||||||
|
|
||||||
|
It is not the purpose of this section to induce you to infringe any
|
||||||
|
patents or other property right claims or to contest validity of any
|
||||||
|
such claims; this section has the sole purpose of protecting the
|
||||||
|
integrity of the free software distribution system which is
|
||||||
|
implemented by public license practices. Many people have made
|
||||||
|
generous contributions to the wide range of software distributed
|
||||||
|
through that system in reliance on consistent application of that
|
||||||
|
system; it is up to the author/donor to decide if he or she is willing
|
||||||
|
to distribute software through any other system and a licensee cannot
|
||||||
|
impose that choice.
|
||||||
|
|
||||||
|
This section is intended to make thoroughly clear what is believed to
|
||||||
|
be a consequence of the rest of this License.
|
||||||
|
|
||||||
|
12. If the distribution and/or use of the Library is restricted in
|
||||||
|
certain countries either by patents or by copyrighted interfaces, the
|
||||||
|
original copyright holder who places the Library under this License may add
|
||||||
|
an explicit geographical distribution limitation excluding those countries,
|
||||||
|
so that distribution is permitted only in or among countries not thus
|
||||||
|
excluded. In such case, this License incorporates the limitation as if
|
||||||
|
written in the body of this License.
|
||||||
|
|
||||||
|
13. The Free Software Foundation may publish revised and/or new
|
||||||
|
versions of the Lesser General Public License from time to time.
|
||||||
|
Such new versions will be similar in spirit to the present version,
|
||||||
|
but may differ in detail to address new problems or concerns.
|
||||||
|
|
||||||
|
Each version is given a distinguishing version number. If the Library
|
||||||
|
specifies a version number of this License which applies to it and
|
||||||
|
"any later version", you have the option of following the terms and
|
||||||
|
conditions either of that version or of any later version published by
|
||||||
|
the Free Software Foundation. If the Library does not specify a
|
||||||
|
license version number, you may choose any version ever published by
|
||||||
|
the Free Software Foundation.
|
||||||
|
|
||||||
|
14. If you wish to incorporate parts of the Library into other free
|
||||||
|
programs whose distribution conditions are incompatible with these,
|
||||||
|
write to the author to ask for permission. For software which is
|
||||||
|
copyrighted by the Free Software Foundation, write to the Free
|
||||||
|
Software Foundation; we sometimes make exceptions for this. Our
|
||||||
|
decision will be guided by the two goals of preserving the free status
|
||||||
|
of all derivatives of our free software and of promoting the sharing
|
||||||
|
and reuse of software generally.
|
||||||
|
|
||||||
|
NO WARRANTY
|
||||||
|
|
||||||
|
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
|
||||||
|
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
|
||||||
|
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
|
||||||
|
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
|
||||||
|
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||||
|
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
|
||||||
|
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
|
||||||
|
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||||
|
|
||||||
|
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
|
||||||
|
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
|
||||||
|
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
|
||||||
|
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
|
||||||
|
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
|
||||||
|
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
|
||||||
|
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
|
||||||
|
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
|
||||||
|
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
|
||||||
|
DAMAGES.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
18
src/README.md
Normal file
18
src/README.md
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
Janitor
|
||||||
|
=======
|
||||||
|
|
||||||
|
**Janitor** is my personal testing and housekeeping bot. It is under heavy development; the codebase may and will change
|
||||||
|
massively between any two versions.
|
||||||
|
|
||||||
|
Licensing
|
||||||
|
---------
|
||||||
|
|
||||||
|
This project is licensed under the MIT License (see the `LICENSE.txt` file for the full license text).
|
||||||
|
|
||||||
|
This project includes code copied and modified from [the Minecraft Forge project][mcforge], which is licensed under the
|
||||||
|
LGPL v2.1 (see the `LICENSE-lgpl2.1.txt` file for the full license text); specifically, the file
|
||||||
|
`sciwhiz12.janitor.config.CommentedConfigSpec` is a modified and renamed version of
|
||||||
|
`net.minecraftforge.common.ForgeConfigSpec` (as of the writing of this document). This file is relicensed under the
|
||||||
|
LGPL v2.1, to comply with the Minecraft Forge project's license.
|
||||||
|
|
||||||
|
[mcforge]: https://github.com/MinecraftForge/MinecraftForge
|
|
@ -21,13 +21,12 @@ public class BotStartup {
|
||||||
|
|
||||||
BotOptions options = new BotOptions(args);
|
BotOptions options = new BotOptions(args);
|
||||||
BotConfig config = new BotConfig(options);
|
BotConfig config = new BotConfig(options);
|
||||||
checkArgument(config.getToken().isPresent(),
|
checkArgument(!config.getToken().isEmpty(), "Supply a client token through config or command line");
|
||||||
"Token is not supplied through config or command line");
|
|
||||||
|
|
||||||
JANITOR.info("Building bot instance and connecting to Discord...");
|
JANITOR.info("Building bot instance and connecting to Discord...");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
JDABuilder.create(config.getToken().get(), EnumSet.allOf(GatewayIntent.class))
|
JDABuilder.create(config.getToken(), EnumSet.allOf(GatewayIntent.class))
|
||||||
.setStatus(OnlineStatus.DO_NOT_DISTURB)
|
.setStatus(OnlineStatus.DO_NOT_DISTURB)
|
||||||
.setActivity(Activity.listening("for the ready call..."))
|
.setActivity(Activity.listening("for the ready call..."))
|
||||||
.setAutoReconnect(true)
|
.setAutoReconnect(true)
|
||||||
|
@ -38,7 +37,8 @@ public class BotStartup {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.build();
|
.build();
|
||||||
} catch (Exception ex) {
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
JANITOR.error("Error while building Discord connection", ex);
|
JANITOR.error("Error while building Discord connection", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,7 @@ import sciwhiz12.janitor.msg.Messages;
|
||||||
import sciwhiz12.janitor.msg.Translations;
|
import sciwhiz12.janitor.msg.Translations;
|
||||||
import sciwhiz12.janitor.utils.Util;
|
import sciwhiz12.janitor.utils.Util;
|
||||||
|
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
import static sciwhiz12.janitor.Logging.JANITOR;
|
import static sciwhiz12.janitor.Logging.JANITOR;
|
||||||
|
@ -30,7 +31,7 @@ public class JanitorBot {
|
||||||
public JanitorBot(JDA discord, BotConfig config) {
|
public JanitorBot(JDA discord, BotConfig config) {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
this.console = new BotConsole(this, System.in);
|
this.console = new BotConsole(this, System.in);
|
||||||
this.storage = new GuildStorage(this, config.getStoragePath());
|
this.storage = new GuildStorage(this, Path.of(config.STORAGE_PATH.get()));
|
||||||
this.cmdRegistry = new CommandRegistry(this, config.getCommandPrefix());
|
this.cmdRegistry = new CommandRegistry(this, config.getCommandPrefix());
|
||||||
this.discord = discord;
|
this.discord = discord;
|
||||||
this.translations = new Translations(this, config.getTranslationsFile());
|
this.translations = new Translations(this, config.getTranslationsFile());
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package sciwhiz12.janitor.config;
|
package sciwhiz12.janitor.config;
|
||||||
|
|
||||||
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
import com.electronwill.nightconfig.core.file.CommentedFileConfig;
|
||||||
|
import com.electronwill.nightconfig.core.file.FileNotFoundAction;
|
||||||
import com.electronwill.nightconfig.core.file.FileWatcher;
|
import com.electronwill.nightconfig.core.file.FileWatcher;
|
||||||
import com.electronwill.nightconfig.toml.TomlFormat;
|
import com.electronwill.nightconfig.toml.TomlFormat;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
@ -14,27 +15,52 @@ import static sciwhiz12.janitor.Logging.JANITOR;
|
||||||
|
|
||||||
public class BotConfig {
|
public class BotConfig {
|
||||||
public static final Path DEFAULT_CONFIG_PATH = Path.of("config.toml");
|
public static final Path DEFAULT_CONFIG_PATH = Path.of("config.toml");
|
||||||
public static final Path DEFAULT_STORAGE_PATH = Path.of("guild_storage");
|
|
||||||
public static final String CLIENT_TOKEN = "discord.client_token";
|
private final CommentedConfigSpec.ConfigValue<String> CLIENT_TOKEN;
|
||||||
public static final String OWNER_ID = "discord.owner_id";
|
private final CommentedConfigSpec.LongValue OWNER_ID;
|
||||||
public static final String STORAGE_PATH = "storage.main_path";
|
public final CommentedConfigSpec.ConfigValue<String> STORAGE_PATH;
|
||||||
public static final String TRANSLATION_FILE_PATH = "messages.translation_file";
|
public final CommentedConfigSpec.ConfigValue<String> CUSTOM_TRANSLATION_FILE;
|
||||||
public static final String COMMAND_PREFIX = "commands.prefix";
|
public final CommentedConfigSpec.ConfigValue<String> COMMAND_PREFIX;
|
||||||
|
|
||||||
private final BotOptions options;
|
private final BotOptions options;
|
||||||
private final Path configPath;
|
private final Path configPath;
|
||||||
|
private final CommentedConfigSpec spec;
|
||||||
private final CommentedFileConfig config;
|
private final CommentedFileConfig config;
|
||||||
|
|
||||||
public BotConfig(BotOptions options) {
|
public BotConfig(BotOptions options) {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
|
||||||
|
final CommentedConfigSpec.Builder builder = new CommentedConfigSpec.Builder();
|
||||||
|
|
||||||
|
CLIENT_TOKEN = builder
|
||||||
|
.comment("The client secret/token for the bot user", "This must be set, or the application will not start up.")
|
||||||
|
.define("discord.client_token", "");
|
||||||
|
OWNER_ID = builder
|
||||||
|
.comment("The id of the bot owner; used for sending status messages and for bot administration commands.",
|
||||||
|
"If 0, then the bot has no owner set.")
|
||||||
|
.defineInRange("discord.owner_id", 0L, Long.MIN_VALUE, Long.MAX_VALUE);
|
||||||
|
STORAGE_PATH = builder
|
||||||
|
.comment("The folder where per-guild storage is kept.")
|
||||||
|
.define("storage.main_path", "guild_storage");
|
||||||
|
CUSTOM_TRANSLATION_FILE = builder
|
||||||
|
.comment("A file which contains custom translation keys to load for messages.",
|
||||||
|
"If blank, no file shall be loaded.")
|
||||||
|
.define("messages.custom_translations", "");
|
||||||
|
COMMAND_PREFIX = builder
|
||||||
|
.comment("The prefix for commands.")
|
||||||
|
.define("commands.prefix", "!");
|
||||||
|
|
||||||
|
spec = builder.build();
|
||||||
|
|
||||||
this.configPath = options.getConfigPath().orElse(DEFAULT_CONFIG_PATH);
|
this.configPath = options.getConfigPath().orElse(DEFAULT_CONFIG_PATH);
|
||||||
this.config = CommentedFileConfig.builder(configPath, TomlFormat.instance())
|
this.config = CommentedFileConfig.builder(configPath, TomlFormat.instance())
|
||||||
.defaultResource("default-config.toml")
|
.onFileNotFound(FileNotFoundAction.CREATE_EMPTY)
|
||||||
.preserveInsertionOrder()
|
.preserveInsertionOrder()
|
||||||
.build();
|
.build();
|
||||||
try {
|
try {
|
||||||
JANITOR.info("Building config from {}", configPath);
|
JANITOR.info("Building config from {}", configPath);
|
||||||
config.load();
|
config.load();
|
||||||
|
spec.setConfig(config);
|
||||||
// TODO: config spec
|
// TODO: config spec
|
||||||
FileWatcher.defaultInstance().addWatch(configPath, this::onFileChange);
|
FileWatcher.defaultInstance().addWatch(configPath, this::onFileChange);
|
||||||
}
|
}
|
||||||
|
@ -50,24 +76,24 @@ public class BotConfig {
|
||||||
@Nullable
|
@Nullable
|
||||||
public Path getTranslationsFile() {
|
public Path getTranslationsFile() {
|
||||||
return options.getTranslationsFile().
|
return options.getTranslationsFile().
|
||||||
or(() -> Optional.ofNullable(config.<String>get(TRANSLATION_FILE_PATH)).map(Path::of))
|
or(() -> CUSTOM_TRANSLATION_FILE.get().isBlank() ?
|
||||||
|
Optional.empty() :
|
||||||
|
Optional.of(Path.of(CUSTOM_TRANSLATION_FILE.get())))
|
||||||
.orElse(null);
|
.orElse(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Path getStoragePath() {
|
public String getToken() {
|
||||||
return config.<String>getOptional(STORAGE_PATH).map(Path::of).orElse(DEFAULT_STORAGE_PATH);
|
return options.getToken().orElse(CLIENT_TOKEN.get());
|
||||||
}
|
|
||||||
|
|
||||||
public Optional<String> getToken() {
|
|
||||||
return options.getToken().or(() -> config.getOptional(CLIENT_TOKEN));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCommandPrefix() {
|
public String getCommandPrefix() {
|
||||||
return options.getCommandPrefix().orElseGet(() -> config.get(COMMAND_PREFIX));
|
return options.getCommandPrefix().orElseGet(COMMAND_PREFIX::get);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<Long> getOwnerID() {
|
public Optional<Long> getOwnerID() {
|
||||||
return options.getOwnerID().or(() -> config.getOptional(OWNER_ID));
|
final Long ret = options.getOwnerID().orElse(OWNER_ID.get());
|
||||||
|
if (ret == 0) return Optional.empty();
|
||||||
|
return Optional.of(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() {
|
public void close() {
|
||||||
|
@ -78,6 +104,7 @@ public class BotConfig {
|
||||||
try {
|
try {
|
||||||
CONFIG.info("Reloading config due to file change {}", configPath);
|
CONFIG.info("Reloading config due to file change {}", configPath);
|
||||||
config.load();
|
config.load();
|
||||||
|
spec.setConfig(config);
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
CONFIG.error("Error while reloading config from {}", configPath, ex);
|
CONFIG.error("Error while reloading config from {}", configPath, ex);
|
||||||
|
|
850
src/main/java/sciwhiz12/janitor/config/CommentedConfigSpec.java
Normal file
850
src/main/java/sciwhiz12/janitor/config/CommentedConfigSpec.java
Normal file
|
@ -0,0 +1,850 @@
|
||||||
|
/*
|
||||||
|
* Minecraft Forge
|
||||||
|
* Copyright (c) 2016-2020.
|
||||||
|
*
|
||||||
|
* This library is free software; you can redistribute it and/or
|
||||||
|
* modify it under the terms of the GNU Lesser General Public
|
||||||
|
* License as published by the Free Software Foundation version 2.1
|
||||||
|
* of the License.
|
||||||
|
*
|
||||||
|
* This library is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
* Lesser General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Lesser General Public
|
||||||
|
* License along with this library; if not, write to the Free Software
|
||||||
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
package sciwhiz12.janitor.config;
|
||||||
|
|
||||||
|
import com.electronwill.nightconfig.core.CommentedConfig;
|
||||||
|
import com.electronwill.nightconfig.core.Config;
|
||||||
|
import com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction;
|
||||||
|
import com.electronwill.nightconfig.core.ConfigSpec.CorrectionListener;
|
||||||
|
import com.electronwill.nightconfig.core.EnumGetMethod;
|
||||||
|
import com.electronwill.nightconfig.core.InMemoryFormat;
|
||||||
|
import com.electronwill.nightconfig.core.UnmodifiableConfig;
|
||||||
|
import com.electronwill.nightconfig.core.file.FileConfig;
|
||||||
|
import com.electronwill.nightconfig.core.utils.UnmodifiableConfigWrapper;
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
|
import com.google.common.base.Splitter;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.google.common.collect.ObjectArrays;
|
||||||
|
import sciwhiz12.janitor.Logging;
|
||||||
|
import sciwhiz12.janitor.utils.Pair;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import javax.annotation.Nonnull;
|
||||||
|
|
||||||
|
import static com.electronwill.nightconfig.core.ConfigSpec.CorrectionAction.*;
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
import static com.google.common.base.Preconditions.checkState;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Like {@link com.electronwill.nightconfig.core.ConfigSpec} except in builder format, and extended to accept comments.
|
||||||
|
*
|
||||||
|
* @author @MinecraftForge
|
||||||
|
* @author SciWhiz12 (modified to remove unneede parts)
|
||||||
|
*/
|
||||||
|
//TODO: Remove extends and pipe everything through getSpec/getValues?
|
||||||
|
public class CommentedConfigSpec extends UnmodifiableConfigWrapper<UnmodifiableConfig> {
|
||||||
|
private final Map<List<String>, String> levelComments;
|
||||||
|
|
||||||
|
private final UnmodifiableConfig values;
|
||||||
|
private Config childConfig;
|
||||||
|
|
||||||
|
private boolean isCorrecting = false;
|
||||||
|
|
||||||
|
private CommentedConfigSpec(UnmodifiableConfig storage, UnmodifiableConfig values,
|
||||||
|
Map<List<String>, String> levelComments) {
|
||||||
|
super(storage);
|
||||||
|
this.values = values;
|
||||||
|
this.levelComments = levelComments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setConfig(CommentedConfig config) {
|
||||||
|
this.childConfig = config;
|
||||||
|
if (config != null && !isCorrect(config)) {
|
||||||
|
String configName = config instanceof FileConfig ?
|
||||||
|
((FileConfig) config).getNioPath().toString() :
|
||||||
|
config.toString();
|
||||||
|
Logging.CONFIG.warn("Configuration file {} is not correct. Correcting", configName);
|
||||||
|
correct(config, (action, path, incorrectValue, correctedValue) ->
|
||||||
|
Logging.CONFIG.warn("Incorrect key {} was corrected from {} to {}", DOT_JOINER.join(path), incorrectValue,
|
||||||
|
correctedValue));
|
||||||
|
if (config instanceof FileConfig) {
|
||||||
|
((FileConfig) config).save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isCorrecting() {
|
||||||
|
return isCorrecting;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isLoaded() {
|
||||||
|
return childConfig != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnmodifiableConfig getSpec() {
|
||||||
|
return this.config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UnmodifiableConfig getValues() {
|
||||||
|
return this.values;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save() {
|
||||||
|
checkNotNull(childConfig, "Cannot save config value without assigned Config object present");
|
||||||
|
if (childConfig instanceof FileConfig) {
|
||||||
|
((FileConfig) childConfig).save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized boolean isCorrect(CommentedConfig config) {
|
||||||
|
LinkedList<String> parentPath = new LinkedList<>();
|
||||||
|
return correct(this.config, config, parentPath, Collections.unmodifiableList(parentPath), (a, b, c, d) -> {},
|
||||||
|
true) == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int correct(CommentedConfig config) {
|
||||||
|
return correct(config, (action, path, incorrectValue, correctedValue) -> {});
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized int correct(CommentedConfig config, CorrectionListener listener) {
|
||||||
|
LinkedList<String> parentPath = new LinkedList<>(); // Linked list for fast add/removes
|
||||||
|
// noinspection UnusedAssignment
|
||||||
|
int ret = -1;
|
||||||
|
try {
|
||||||
|
isCorrecting = true;
|
||||||
|
ret = correct(this.config, config, parentPath, Collections.unmodifiableList(parentPath), listener, false);
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
isCorrecting = false;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int correct(UnmodifiableConfig spec, CommentedConfig config, LinkedList<String> parentPath,
|
||||||
|
List<String> parentPathUnmodifiable, CorrectionListener listener, boolean dryRun) {
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
Map<String, Object> specMap = spec.valueMap();
|
||||||
|
Map<String, Object> configMap = config.valueMap();
|
||||||
|
|
||||||
|
for (Map.Entry<String, Object> specEntry : specMap.entrySet()) {
|
||||||
|
final String key = specEntry.getKey();
|
||||||
|
final Object specValue = specEntry.getValue();
|
||||||
|
final Object configValue = configMap.get(key);
|
||||||
|
final CorrectionAction action = configValue == null ? ADD : REPLACE;
|
||||||
|
|
||||||
|
parentPath.addLast(key);
|
||||||
|
|
||||||
|
if (specValue instanceof Config) {
|
||||||
|
if (configValue instanceof CommentedConfig) {
|
||||||
|
count += correct((Config) specValue, (CommentedConfig) configValue, parentPath, parentPathUnmodifiable,
|
||||||
|
listener, dryRun);
|
||||||
|
if (count > 0 && dryRun)
|
||||||
|
return count;
|
||||||
|
} else if (dryRun) {
|
||||||
|
return 1;
|
||||||
|
} else {
|
||||||
|
CommentedConfig newValue = config.createSubConfig();
|
||||||
|
configMap.put(key, newValue);
|
||||||
|
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
|
||||||
|
count++;
|
||||||
|
count += correct((Config) specValue, newValue, parentPath, parentPathUnmodifiable, listener, dryRun);
|
||||||
|
}
|
||||||
|
|
||||||
|
String newComment = levelComments.get(parentPath);
|
||||||
|
String oldComment = config.getComment(key);
|
||||||
|
if (!Objects.equals(oldComment, newComment)) {
|
||||||
|
if (dryRun)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
//TODO: Comment correction listener?
|
||||||
|
config.setComment(key, newComment);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
ValueSpec valueSpec = (ValueSpec) specValue;
|
||||||
|
if (!valueSpec.test(configValue)) {
|
||||||
|
if (dryRun)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
Object newValue = valueSpec.correct(configValue);
|
||||||
|
configMap.put(key, newValue);
|
||||||
|
listener.onCorrect(action, parentPathUnmodifiable, configValue, newValue);
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
String oldComment = config.getComment(key);
|
||||||
|
if (!Objects.equals(oldComment, valueSpec.getComment())) {
|
||||||
|
if (dryRun)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
//TODO: Comment correction listener?
|
||||||
|
config.setComment(key, valueSpec.getComment());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parentPath.removeLast();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second step: removes the unspecified values
|
||||||
|
for (Iterator<Map.Entry<String, Object>> ittr = configMap.entrySet().iterator(); ittr.hasNext(); ) {
|
||||||
|
Map.Entry<String, Object> entry = ittr.next();
|
||||||
|
if (!specMap.containsKey(entry.getKey())) {
|
||||||
|
if (dryRun)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
ittr.remove();
|
||||||
|
parentPath.addLast(entry.getKey());
|
||||||
|
listener.onCorrect(REMOVE, parentPathUnmodifiable, entry.getValue(), null);
|
||||||
|
parentPath.removeLast();
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("SuspiciousMethodCalls")
|
||||||
|
public static class Builder {
|
||||||
|
private final Config storage = Config
|
||||||
|
.of(LinkedHashMap::new, InMemoryFormat.withUniversalSupport()); // Use LinkedHashMap for consistent ordering
|
||||||
|
private BuilderContext context = new BuilderContext();
|
||||||
|
private final Map<List<String>, String> levelComments = new HashMap<>();
|
||||||
|
private final List<String> currentPath = new ArrayList<>();
|
||||||
|
private final List<ConfigValue<?>> values = new ArrayList<>();
|
||||||
|
|
||||||
|
// Arbitrary Objects
|
||||||
|
public <T> ConfigValue<T> define(String path, T defaultValue) {
|
||||||
|
return define(split(path), defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> define(List<String> path, T defaultValue) {
|
||||||
|
return define(path, defaultValue, o -> o != null && defaultValue.getClass().isAssignableFrom(o.getClass()));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> define(String path, T defaultValue, Predicate<Object> validator) {
|
||||||
|
return define(split(path), defaultValue, validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> define(List<String> path, T defaultValue, Predicate<Object> validator) {
|
||||||
|
Objects.requireNonNull(defaultValue, "Default value can not be null");
|
||||||
|
return define(path, () -> defaultValue, validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> define(String path, Supplier<T> defaultSupplier, Predicate<Object> validator) {
|
||||||
|
return define(split(path), defaultSupplier, validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> define(List<String> path, Supplier<T> defaultSupplier, Predicate<Object> validator) {
|
||||||
|
return define(path, defaultSupplier, validator, Object.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> define(List<String> path, Supplier<T> defaultSupplier, Predicate<Object> validator,
|
||||||
|
Class<?> clazz) {
|
||||||
|
context.setClazz(clazz);
|
||||||
|
return define(path, new ValueSpec(defaultSupplier, validator, context), defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> define(List<String> path, ValueSpec value, Supplier<T> defaultSupplier) {
|
||||||
|
// This is the root where everything at the end of the day ends up.
|
||||||
|
if (!currentPath.isEmpty()) {
|
||||||
|
List<String> tmp = new ArrayList<>(currentPath.size() + path.size());
|
||||||
|
tmp.addAll(currentPath);
|
||||||
|
tmp.addAll(path);
|
||||||
|
path = tmp;
|
||||||
|
}
|
||||||
|
storage.set(path, value);
|
||||||
|
context = new BuilderContext();
|
||||||
|
return new ConfigValue<>(this, path, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Comparable<? super V>> ConfigValue<V> defineInRange(String path, V defaultValue, V min, V max,
|
||||||
|
Class<V> clazz) {
|
||||||
|
return defineInRange(split(path), defaultValue, min, max, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Comparable<? super V>> ConfigValue<V> defineInRange(List<String> path, V defaultValue, V min, V max,
|
||||||
|
Class<V> clazz) {
|
||||||
|
return defineInRange(path, (Supplier<V>) () -> defaultValue, min, max, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Comparable<? super V>> ConfigValue<V> defineInRange(String path, Supplier<V> defaultSupplier, V min,
|
||||||
|
V max, Class<V> clazz) {
|
||||||
|
return defineInRange(split(path), defaultSupplier, min, max, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Comparable<? super V>> ConfigValue<V> defineInRange(List<String> path, Supplier<V> defaultSupplier,
|
||||||
|
V min, V max, Class<V> clazz) {
|
||||||
|
Range<V> range = new Range<>(clazz, min, max);
|
||||||
|
context.setRange(range);
|
||||||
|
context.setComment(ObjectArrays.concat(context.getComment(), "Range: " + range.toString()));
|
||||||
|
if (min.compareTo(max) > 0)
|
||||||
|
throw new IllegalArgumentException("Range min most be less then max.");
|
||||||
|
return define(path, defaultSupplier, range);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> defineInList(String path, T defaultValue, Collection<? extends T> acceptableValues) {
|
||||||
|
return defineInList(split(path), defaultValue, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> defineInList(String path, Supplier<T> defaultSupplier,
|
||||||
|
Collection<? extends T> acceptableValues) {
|
||||||
|
return defineInList(split(path), defaultSupplier, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> defineInList(List<String> path, T defaultValue, Collection<? extends T> acceptableValues) {
|
||||||
|
return defineInList(path, () -> defaultValue, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<T> defineInList(List<String> path, Supplier<T> defaultSupplier,
|
||||||
|
Collection<? extends T> acceptableValues) {
|
||||||
|
return define(path, defaultSupplier, acceptableValues::contains);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<List<? extends T>> defineList(String path, List<? extends T> defaultValue,
|
||||||
|
Predicate<Object> elementValidator) {
|
||||||
|
return defineList(split(path), defaultValue, elementValidator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<List<? extends T>> defineList(String path, Supplier<List<? extends T>> defaultSupplier,
|
||||||
|
Predicate<Object> elementValidator) {
|
||||||
|
return defineList(split(path), defaultSupplier, elementValidator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<List<? extends T>> defineList(List<String> path, List<? extends T> defaultValue,
|
||||||
|
Predicate<Object> elementValidator) {
|
||||||
|
return defineList(path, () -> defaultValue, elementValidator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> ConfigValue<List<? extends T>> defineList(List<String> path, Supplier<List<? extends T>> defaultSupplier,
|
||||||
|
Predicate<Object> elementValidator) {
|
||||||
|
context.setClazz(List.class);
|
||||||
|
return define(path,
|
||||||
|
new ValueSpec(defaultSupplier, x -> x instanceof List && ((List<?>) x).stream().allMatch(elementValidator),
|
||||||
|
context) {
|
||||||
|
@Override
|
||||||
|
public Object correct(Object value) {
|
||||||
|
if (!(value instanceof List) || ((List<?>) value).isEmpty()) {
|
||||||
|
return getDefault();
|
||||||
|
}
|
||||||
|
List<?> list = Lists.newArrayList((List<?>) value);
|
||||||
|
list.removeIf(elementValidator.negate());
|
||||||
|
if (list.isEmpty()) {
|
||||||
|
return getDefault();
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
}, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enums
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue) {
|
||||||
|
return defineEnum(split(path), defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter) {
|
||||||
|
return defineEnum(split(path), defaultValue, converter);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue) {
|
||||||
|
return defineEnum(path, defaultValue, defaultValue.getDeclaringClass().getEnumConstants());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter) {
|
||||||
|
return defineEnum(path, defaultValue, converter, defaultValue.getDeclaringClass().getEnumConstants());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public final <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, V... acceptableValues) {
|
||||||
|
return defineEnum(split(path), defaultValue, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public final <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter,
|
||||||
|
V... acceptableValues) {
|
||||||
|
return defineEnum(split(path), defaultValue, converter, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public final <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, V... acceptableValues) {
|
||||||
|
return defineEnum(path, defaultValue, Arrays.asList(acceptableValues));
|
||||||
|
}
|
||||||
|
|
||||||
|
@SafeVarargs
|
||||||
|
public final <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter,
|
||||||
|
V... acceptableValues) {
|
||||||
|
return defineEnum(path, defaultValue, converter, Arrays.asList(acceptableValues));
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, Collection<V> acceptableValues) {
|
||||||
|
return defineEnum(split(path), defaultValue, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter,
|
||||||
|
Collection<V> acceptableValues) {
|
||||||
|
return defineEnum(split(path), defaultValue, converter, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, Collection<V> acceptableValues) {
|
||||||
|
return defineEnum(path, defaultValue, EnumGetMethod.NAME_IGNORECASE, acceptableValues);
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter,
|
||||||
|
Collection<V> acceptableValues) {
|
||||||
|
return defineEnum(path, defaultValue, converter, obj -> {
|
||||||
|
if (obj instanceof Enum) {
|
||||||
|
return acceptableValues.contains(obj);
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
//noinspection SuspiciousMethodCalls
|
||||||
|
return acceptableValues.contains(converter.get(obj, defaultValue.getClass()));
|
||||||
|
}
|
||||||
|
catch (IllegalArgumentException | ClassCastException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, Predicate<Object> validator) {
|
||||||
|
return defineEnum(split(path), defaultValue, validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, V defaultValue, EnumGetMethod converter,
|
||||||
|
Predicate<Object> validator) {
|
||||||
|
return defineEnum(split(path), defaultValue, converter, validator);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, Predicate<Object> validator) {
|
||||||
|
return defineEnum(path, () -> defaultValue, validator, defaultValue.getDeclaringClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, V defaultValue, EnumGetMethod converter,
|
||||||
|
Predicate<Object> validator) {
|
||||||
|
return defineEnum(path, () -> defaultValue, converter, validator, defaultValue.getDeclaringClass());
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, Supplier<V> defaultSupplier,
|
||||||
|
Predicate<Object> validator, Class<V> clazz) {
|
||||||
|
return defineEnum(split(path), defaultSupplier, validator, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(String path, Supplier<V> defaultSupplier, EnumGetMethod converter,
|
||||||
|
Predicate<Object> validator, Class<V> clazz) {
|
||||||
|
return defineEnum(split(path), defaultSupplier, converter, validator, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, Supplier<V> defaultSupplier,
|
||||||
|
Predicate<Object> validator, Class<V> clazz) {
|
||||||
|
return defineEnum(path, defaultSupplier, EnumGetMethod.NAME_IGNORECASE, validator, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <V extends Enum<V>> EnumValue<V> defineEnum(List<String> path, Supplier<V> defaultSupplier,
|
||||||
|
EnumGetMethod converter, Predicate<Object> validator, Class<V> clazz) {
|
||||||
|
context.setClazz(clazz);
|
||||||
|
V[] allowedValues = clazz.getEnumConstants();
|
||||||
|
context.setComment(ObjectArrays.concat(context.getComment(),
|
||||||
|
"Allowed Values: " + Arrays.stream(allowedValues).filter(validator).map(Enum::name)
|
||||||
|
.collect(Collectors.joining(", "))));
|
||||||
|
return new EnumValue<>(this,
|
||||||
|
define(path, new ValueSpec(defaultSupplier, validator, context), defaultSupplier).getPath(), defaultSupplier,
|
||||||
|
converter, clazz);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Booleans
|
||||||
|
public BooleanValue define(String path, boolean defaultValue) {
|
||||||
|
return define(split(path), defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanValue define(List<String> path, boolean defaultValue) {
|
||||||
|
return define(path, () -> defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanValue define(String path, Supplier<Boolean> defaultSupplier) {
|
||||||
|
return define(split(path), defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public BooleanValue define(List<String> path, Supplier<Boolean> defaultSupplier) {
|
||||||
|
return new BooleanValue(this, define(path, defaultSupplier, o -> {
|
||||||
|
if (o instanceof String) return ((String) o).equalsIgnoreCase("true") || ((String) o).equalsIgnoreCase("false");
|
||||||
|
return o instanceof Boolean;
|
||||||
|
}, Boolean.class).getPath(), defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Double
|
||||||
|
public DoubleValue defineInRange(String path, double defaultValue, double min, double max) {
|
||||||
|
return defineInRange(split(path), defaultValue, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoubleValue defineInRange(List<String> path, double defaultValue, double min, double max) {
|
||||||
|
return defineInRange(path, () -> defaultValue, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoubleValue defineInRange(String path, Supplier<Double> defaultSupplier, double min, double max) {
|
||||||
|
return defineInRange(split(path), defaultSupplier, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public DoubleValue defineInRange(List<String> path, Supplier<Double> defaultSupplier, double min, double max) {
|
||||||
|
return new DoubleValue(this, defineInRange(path, defaultSupplier, min, max, Double.class).getPath(),
|
||||||
|
defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Integers
|
||||||
|
public IntValue defineInRange(String path, int defaultValue, int min, int max) {
|
||||||
|
return defineInRange(split(path), defaultValue, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntValue defineInRange(List<String> path, int defaultValue, int min, int max) {
|
||||||
|
return defineInRange(path, () -> defaultValue, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntValue defineInRange(String path, Supplier<Integer> defaultSupplier, int min, int max) {
|
||||||
|
return defineInRange(split(path), defaultSupplier, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IntValue defineInRange(List<String> path, Supplier<Integer> defaultSupplier, int min, int max) {
|
||||||
|
return new IntValue(this, defineInRange(path, defaultSupplier, min, max, Integer.class).getPath(), defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Longs
|
||||||
|
public LongValue defineInRange(String path, long defaultValue, long min, long max) {
|
||||||
|
return defineInRange(split(path), defaultValue, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LongValue defineInRange(List<String> path, long defaultValue, long min, long max) {
|
||||||
|
return defineInRange(path, () -> defaultValue, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LongValue defineInRange(String path, Supplier<Long> defaultSupplier, long min, long max) {
|
||||||
|
return defineInRange(split(path), defaultSupplier, min, max);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LongValue defineInRange(List<String> path, Supplier<Long> defaultSupplier, long min, long max) {
|
||||||
|
return new LongValue(this, defineInRange(path, defaultSupplier, min, max, Long.class).getPath(), defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder comment(String comment) {
|
||||||
|
context.setComment(comment);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder comment(String... comment) {
|
||||||
|
context.setComment(comment);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder push(String path) {
|
||||||
|
return push(split(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder push(List<String> path) {
|
||||||
|
currentPath.addAll(path);
|
||||||
|
if (context.hasComment()) {
|
||||||
|
|
||||||
|
levelComments.put(new ArrayList<>(currentPath), context.buildComment());
|
||||||
|
context.setComment(); // Set to empty
|
||||||
|
}
|
||||||
|
context.ensureEmpty();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder pop() {
|
||||||
|
return pop(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder pop(int count) {
|
||||||
|
if (count > currentPath.size())
|
||||||
|
throw new IllegalArgumentException("Attempted to pop " + count + " elements when we only had: " + currentPath);
|
||||||
|
for (int x = 0; x < count; x++)
|
||||||
|
currentPath.remove(currentPath.size() - 1);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> Pair<T, CommentedConfigSpec> configure(Function<Builder, T> consumer) {
|
||||||
|
T o = consumer.apply(this);
|
||||||
|
return Pair.of(o, this.build());
|
||||||
|
}
|
||||||
|
|
||||||
|
public CommentedConfigSpec build() {
|
||||||
|
context.ensureEmpty();
|
||||||
|
Config valueCfg = Config
|
||||||
|
.of(Config.getDefaultMapCreator(true, true), InMemoryFormat.withSupport(ConfigValue.class::isAssignableFrom));
|
||||||
|
values.forEach(v -> valueCfg.set(v.getPath(), v));
|
||||||
|
|
||||||
|
CommentedConfigSpec ret = new CommentedConfigSpec(storage, valueCfg, levelComments);
|
||||||
|
values.forEach(v -> v.spec = ret);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
public interface BuilderConsumer {
|
||||||
|
void accept(Builder builder);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class BuilderContext {
|
||||||
|
private @Nonnull String[] comment = new String[0];
|
||||||
|
private Range<?> range;
|
||||||
|
private Class<?> clazz;
|
||||||
|
|
||||||
|
public void setComment(String... value) { this.comment = value; }
|
||||||
|
|
||||||
|
public boolean hasComment() { return this.comment.length > 0; }
|
||||||
|
|
||||||
|
public String[] getComment() { return this.comment; }
|
||||||
|
|
||||||
|
public String buildComment() { return LINE_JOINER.join(comment); }
|
||||||
|
|
||||||
|
public <V extends Comparable<? super V>> void setRange(Range<V> value) {
|
||||||
|
this.range = value;
|
||||||
|
this.setClazz(value.getClazz());
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <V extends Comparable<? super V>> Range<V> getRange() { return (Range<V>) this.range; }
|
||||||
|
|
||||||
|
public void setClazz(Class<?> clazz) { this.clazz = clazz; }
|
||||||
|
|
||||||
|
public Class<?> getClazz() { return this.clazz; }
|
||||||
|
|
||||||
|
public void ensureEmpty() {
|
||||||
|
checkState(!hasComment(), "Non-empty comment when empty expected");
|
||||||
|
checkState(range == null, "Non-null range when null expected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
private static class Range<V extends Comparable<? super V>> implements Predicate<Object> {
|
||||||
|
private final Class<? extends V> clazz;
|
||||||
|
private final V min;
|
||||||
|
private final V max;
|
||||||
|
|
||||||
|
private Range(Class<V> clazz, V min, V max) {
|
||||||
|
this.clazz = clazz;
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Class<? extends V> getClazz() { return clazz; }
|
||||||
|
|
||||||
|
public V getMin() { return min; }
|
||||||
|
|
||||||
|
public V getMax() { return max; }
|
||||||
|
|
||||||
|
private boolean isNumber(Object other) {
|
||||||
|
return Number.class.isAssignableFrom(clazz) && other instanceof Number;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean test(Object t) {
|
||||||
|
if (isNumber(t)) {
|
||||||
|
Number n = (Number) t;
|
||||||
|
return ((Number) min).doubleValue() <= n.doubleValue() && n.doubleValue() <= ((Number) max).doubleValue();
|
||||||
|
}
|
||||||
|
if (!clazz.isInstance(t)) return false;
|
||||||
|
V c = clazz.cast(t);
|
||||||
|
return c.compareTo(min) >= 0 && c.compareTo(max) <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object correct(Object value, Object def) {
|
||||||
|
if (isNumber(value)) {
|
||||||
|
Number n = (Number) value;
|
||||||
|
return n.doubleValue() < ((Number) min).doubleValue() ?
|
||||||
|
min :
|
||||||
|
n.doubleValue() > ((Number) max).doubleValue() ? max : value;
|
||||||
|
}
|
||||||
|
if (!clazz.isInstance(value)) return def;
|
||||||
|
V c = clazz.cast(value);
|
||||||
|
return c.compareTo(min) < 0 ? min : c.compareTo(max) > 0 ? max : value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
if (clazz == Integer.class) {
|
||||||
|
if (max.equals(Integer.MAX_VALUE) && min.equals(Integer.MIN_VALUE)) {
|
||||||
|
return "unbounded";
|
||||||
|
} else if (max.equals(Integer.MAX_VALUE)) {
|
||||||
|
return "> " + min;
|
||||||
|
} else if (min.equals(Integer.MIN_VALUE)) {
|
||||||
|
return "< " + max;
|
||||||
|
}
|
||||||
|
} else if (clazz == Long.class) {
|
||||||
|
if (max.equals(Long.MAX_VALUE) && min.equals(Long.MIN_VALUE)) {
|
||||||
|
return "unbounded";
|
||||||
|
} else if (max.equals(Long.MAX_VALUE)) {
|
||||||
|
return "> " + min;
|
||||||
|
} else if (min.equals(Long.MIN_VALUE)) {
|
||||||
|
return "< " + max;
|
||||||
|
}
|
||||||
|
}// TODO add more special cases?
|
||||||
|
return min + " ~ " + max;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ValueSpec {
|
||||||
|
private final String comment;
|
||||||
|
private final Range<?> range;
|
||||||
|
private final Class<?> clazz;
|
||||||
|
private final Supplier<?> supplier;
|
||||||
|
private final Predicate<Object> validator;
|
||||||
|
private Object _default = null;
|
||||||
|
|
||||||
|
private ValueSpec(Supplier<?> supplier, Predicate<Object> validator, BuilderContext context) {
|
||||||
|
Objects.requireNonNull(supplier, "Default supplier can not be null");
|
||||||
|
Objects.requireNonNull(validator, "Validator can not be null");
|
||||||
|
|
||||||
|
this.comment = context.hasComment() ? context.buildComment() : null;
|
||||||
|
this.range = context.getRange();
|
||||||
|
this.clazz = context.getClazz();
|
||||||
|
this.supplier = supplier;
|
||||||
|
this.validator = validator;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getComment() { return comment; }
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <V extends Comparable<? super V>> Range<V> getRange() { return (Range<V>) this.range; }
|
||||||
|
|
||||||
|
public Class<?> getClazz() { return this.clazz; }
|
||||||
|
|
||||||
|
public boolean test(Object value) { return validator.test(value); }
|
||||||
|
|
||||||
|
public Object correct(Object value) { return range == null ? getDefault() : range.correct(value, getDefault()); }
|
||||||
|
|
||||||
|
public Object getDefault() {
|
||||||
|
if (_default == null)
|
||||||
|
_default = supplier.get();
|
||||||
|
return _default;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class ConfigValue<T> {
|
||||||
|
private final Builder parent;
|
||||||
|
private final List<String> path;
|
||||||
|
private final Supplier<T> defaultSupplier;
|
||||||
|
|
||||||
|
private CommentedConfigSpec spec;
|
||||||
|
|
||||||
|
ConfigValue(Builder parent, List<String> path, Supplier<T> defaultSupplier) {
|
||||||
|
this.parent = parent;
|
||||||
|
this.path = path;
|
||||||
|
this.defaultSupplier = defaultSupplier;
|
||||||
|
this.parent.values.add(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getPath() {
|
||||||
|
return Lists.newArrayList(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public T get() {
|
||||||
|
checkNotNull(spec, "Cannot get config value before spec is built");
|
||||||
|
if (spec.childConfig == null)
|
||||||
|
return defaultSupplier.get();
|
||||||
|
return getRaw(spec.childConfig, path, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected T getRaw(Config config, List<String> path, Supplier<T> defaultSupplier) {
|
||||||
|
return config.getOrElse(path, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Builder next() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void save() {
|
||||||
|
checkNotNull(spec, "Cannot save config value before spec is built");
|
||||||
|
checkNotNull(spec.childConfig, "Cannot save config value without assigned Config object present");
|
||||||
|
spec.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(T value) {
|
||||||
|
checkNotNull(spec, "Cannot set config value before spec is built");
|
||||||
|
checkNotNull(spec.childConfig, "Cannot set config value without assigned Config object present");
|
||||||
|
spec.childConfig.set(path, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class BooleanValue extends ConfigValue<Boolean> {
|
||||||
|
BooleanValue(Builder parent, List<String> path, Supplier<Boolean> defaultSupplier) {
|
||||||
|
super(parent, path, defaultSupplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class IntValue extends ConfigValue<Integer> {
|
||||||
|
IntValue(Builder parent, List<String> path, Supplier<Integer> defaultSupplier) {
|
||||||
|
super(parent, path, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Integer getRaw(Config config, List<String> path, Supplier<Integer> defaultSupplier) {
|
||||||
|
return config.getIntOrElse(path, defaultSupplier::get);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class LongValue extends ConfigValue<Long> {
|
||||||
|
LongValue(Builder parent, List<String> path, Supplier<Long> defaultSupplier) {
|
||||||
|
super(parent, path, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Long getRaw(Config config, List<String> path, Supplier<Long> defaultSupplier) {
|
||||||
|
return config.getLongOrElse(path, defaultSupplier::get);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class DoubleValue extends ConfigValue<Double> {
|
||||||
|
DoubleValue(Builder parent, List<String> path, Supplier<Double> defaultSupplier) {
|
||||||
|
super(parent, path, defaultSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Double getRaw(Config config, List<String> path, Supplier<Double> defaultSupplier) {
|
||||||
|
Number n = config.get(path);
|
||||||
|
return n == null ? defaultSupplier.get() : n.doubleValue();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class EnumValue<T extends Enum<T>> extends ConfigValue<T> {
|
||||||
|
private final EnumGetMethod converter;
|
||||||
|
private final Class<T> clazz;
|
||||||
|
|
||||||
|
EnumValue(Builder parent, List<String> path, Supplier<T> defaultSupplier, EnumGetMethod converter, Class<T> clazz) {
|
||||||
|
super(parent, path, defaultSupplier);
|
||||||
|
this.converter = converter;
|
||||||
|
this.clazz = clazz;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected T getRaw(Config config, List<String> path, Supplier<T> defaultSupplier) {
|
||||||
|
return config.getEnumOrElse(path, clazz, converter, defaultSupplier);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final Joiner LINE_JOINER = Joiner.on("\n");
|
||||||
|
private static final Joiner DOT_JOINER = Joiner.on(".");
|
||||||
|
private static final Splitter DOT_SPLITTER = Splitter.on(".");
|
||||||
|
|
||||||
|
private static List<String> split(String path) {
|
||||||
|
return Lists.newArrayList(DOT_SPLITTER.split(path));
|
||||||
|
}
|
||||||
|
}
|
44
src/main/java/sciwhiz12/janitor/utils/Pair.java
Normal file
44
src/main/java/sciwhiz12/janitor/utils/Pair.java
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
package sciwhiz12.janitor.utils;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class Pair<L, R> implements Map.Entry<L, R> {
|
||||||
|
private final L left;
|
||||||
|
private final R right;
|
||||||
|
|
||||||
|
public static <L, R> Pair<L, R> of(L left, R right) {
|
||||||
|
Preconditions.checkNotNull(left, "Left value should not be null");
|
||||||
|
Preconditions.checkNotNull(right, "Right value should not be null");
|
||||||
|
return new Pair<>(left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Pair(L left, R right) {
|
||||||
|
this.left = left;
|
||||||
|
this.right = right;
|
||||||
|
}
|
||||||
|
|
||||||
|
public L getLeft() {
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
public R getRight() {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public L getKey() {
|
||||||
|
return left;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R getValue() {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R setValue(R value) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +0,0 @@
|
||||||
[discord]
|
|
||||||
# The client secret/token for the bot user
|
|
||||||
client_token = "NONE"
|
|
||||||
|
|
||||||
# The id of the bot owner; used for sending status messages and allowing bot admin commands
|
|
||||||
owner_id = 0
|
|
||||||
|
|
||||||
[messages]
|
|
||||||
# The file containing the translation keys
|
|
||||||
# translation_file = "english.json"
|
|
||||||
|
|
||||||
[commands]
|
|
||||||
# The prefix for commands
|
|
||||||
prefix = "!"
|
|
Loading…
Reference in New Issue
Block a user