Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
M
Mirai
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
Mirai
Commits
def0e371
Commit
def0e371
authored
Feb 13, 2020
by
jasonczc
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
f6155cc6
26fc61df
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
110 additions
and
66 deletions
+110
-66
mirai-console/src/main/kotlin/MiraiConsole.kt
mirai-console/src/main/kotlin/MiraiConsole.kt
+12
-19
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/ConfigSection.kt
...e/src/main/kotlin/net/mamoe/mirai/plugin/ConfigSection.kt
+98
-47
No files found.
mirai-console/src/main/kotlin/MiraiConsole.kt
View file @
def0e371
...
...
@@ -27,7 +27,7 @@ object MiraiConsole {
var
path
:
String
=
System
.
getProperty
(
"user.dir"
)
val
version
=
"
0.13"
val
version
=
"0.13"
val
build
=
"Beta"
fun
start
()
{
...
...
@@ -42,14 +42,13 @@ object MiraiConsole {
logger
(
"Mirai为开源项目,请自觉遵守开源项目协议"
)
logger
(
"Powered by Mamoe Technology"
)
logger
()
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qq号 qq密码 \" 来登陆一个BOT"
)
MiraiProperties
()
CommandManager
.
register
(
DefaultCommands
.
DefaultLoginCommand
())
pluginManager
.
loadPlugins
()
CommandListener
.
start
()
println
(
MiraiProperties
.
HTTP_API_ENABLE
)
logger
(
"\"/login qqnumber qqpassword \" to login a bot"
)
logger
(
"\"/login qq号 qq密码 \" 来登陆一个BOT"
)
}
fun
stop
()
{
...
...
@@ -117,22 +116,16 @@ object MiraiConsole {
}
object
MiraiProperties
{
val
init
=
!
File
(
"$path/mirai.json"
).
exists
()
var
config
=
Config
.
load
(
"$path/mirai.json"
)
var
HTTP_API_ENABLE
:
Boolean
by
config
.
withDefault
{
true
}
var
HTTP_API_PORT
:
Int
by
config
.
withDefault
{
8080
}
var
HTTP_API_AUTH_KEY
:
String
by
config
operator
fun
invoke
()
{
if
(
init
)
{
HTTP_API_AUTH_KEY
=
"INITKEY"
+
generateSessionKey
()
logger
(
"Mirai HTTPAPI authkey 已随机生成, 请注意修改"
)
}
var
config
=
File
(
"$path/mirai.json"
).
loadAsConfig
()
var
HTTP_API_ENABLE
:
Boolean
by
config
.
withDefaultWrite
{
true
}
var
HTTP_API_PORT
:
Int
by
config
.
withDefaultWrite
{
8080
}
var
HTTP_API_AUTH_KEY
:
String
by
config
.
withDefaultWriteSave
{
"InitKey"
.
also
{
logger
(
"Mirai HTTPAPI auth key 已随机生成 请注意修改"
)
}
+
generateSessionKey
()
}
}
}
...
...
mirai-console/src/main/kotlin/net/mamoe/mirai/plugin/ConfigSection.kt
View file @
def0e371
...
...
@@ -14,12 +14,11 @@ import com.alibaba.fastjson.JSONObject
import
com.alibaba.fastjson.TypeReference
import
com.alibaba.fastjson.parser.Feature
import
kotlinx.serialization.*
import
kotlinx.serialization.json.Json
import
net.mamoe.mirai.utils.cryptor.contentToString
import
java.io.File
import
java.util.concurrent.ConcurrentHashMap
import
java.util.
function.BiConsumer
import
java.util.
concurrent.ConcurrentSkipListMap
import
kotlin.properties.ReadWriteProperty
import
kotlin.reflect.KClass
import
kotlin.reflect.KProperty
import
kotlin.reflect.full.isSubclassOf
...
...
@@ -47,6 +46,7 @@ interface Config {
operator
fun
set
(
key
:
String
,
value
:
Any
)
operator
fun
get
(
key
:
String
):
Any
?
fun
exist
(
key
:
String
):
Boolean
fun
setIfAbsent
(
key
:
String
,
value
:
Any
)
fun
asMap
():
Map
<
String
,
Any
>
fun
save
()
...
...
@@ -67,47 +67,109 @@ interface Config {
}
}
fun
File
.
loadAsConfig
():
Config
{
return
Config
.
load
(
this
)
}
/* 最简单的代理 */
inline
operator
fun
<
reified
T
:
Any
>
Config
.
getValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>):
T
{
return
smartCast
(
property
)
}
inline
operator
fun
<
reified
T
:
Any
>
Config
.
setValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>,
value
:
T
)
{
this
[
property
.
name
]
=
value
}
/* 带有默认值的代理 */
inline
fun
<
reified
T
:
Any
>
Config
.
withDefault
(
autoSave
:
Boolean
=
true
,
crossinline
defaultValue
:
()
->
T
noinline
defaultValue
:
()
->
T
):
ReadWriteProperty
<
Any
,
T
>
{
val
default
by
lazy
{
defaultValue
.
invoke
()
}
return
object
:
ReadWriteProperty
<
Any
,
T
>
{
override
fun
getValue
(
thisRef
:
Any
,
property
:
KProperty
<
*
>):
T
{
if
(
!
this
@
withDefault
.
exist
(
property
.
name
))
{
return
defaultValue
.
invoke
(
)
if
(
this
@
withDefault
.
exist
(
property
.
name
))
{
//unsafe
return
this
@
withDefault
.
smartCast
(
property
)
}
return
smartCast
(
property
)
return
default
}
override
fun
setValue
(
thisRef
:
Any
,
property
:
KProperty
<
*
>,
value
:
T
)
{
this
@
withDefault
[
property
.
name
]
=
value
if
(
autoSave
)
save
()
}
}
}
@Suppress
(
"IMPLICIT_CAST_TO_ANY"
)
inline
fun
<
reified
T
>
Config
.
smartCast
(
property
:
KProperty
<
*
>):
T
{
return
when
(
T
::
class
)
{
String
::
class
-> this.get
String
(
property
.
name
)
Int
::
class
-> this.get
Int
(
property
.
name
)
Float
::
class
-> this.get
Float
(
property
.
name
)
Double
::
class
-> this.get
Double
(
property
.
name
)
Long
::
class
-> this.get
Long
(
property
.
name
)
Boolean
::
class
-> this.get
Boolean
(
property
.
name
)
/* 带有默认值且如果为空会写入的代理 */
inline
fun
<
reified
T
:
Any
>
Config
.
withDefaultWrite
(
noinline
defaultValue
:
()
->
T
):
WithDefaultWriteLoader
<
T
>
{
return
WithDefaultWriteLoader
(
T
::
class
,
this
,
defaultValue
,
false
)
}
/* 带有默认值且如果为空会写入保存的代理 */
inline
fun
<
reified
T
:
Any
>
Config
.
withDefaultWriteSave
(
noinline
defaultValue
:
()
->
T
):
WithDefaultWriteLoader
<
T
>
{
return
WithDefaultWriteLoader
(
T
::
class
,
this
,
defaultValue
,
true
)
}
class
WithDefaultWriteLoader
<
T
:
Any
>(
private
val
_class
:
KClass
<
T
>,
private
val
config
:
Config
,
private
val
defaultValue
:
()
->
T
,
private
val
save
:
Boolean
)
{
operator
fun
provideDelegate
(
thisRef
:
Any
,
prop
:
KProperty
<
*
>
):
ReadWriteProperty
<
Any
,
T
>
{
val
defaultValue
by
lazy
{
defaultValue
.
invoke
()
}
config
.
setIfAbsent
(
prop
.
name
,
defaultValue
)
if
(
save
)
{
config
.
save
()
}
return
object
:
ReadWriteProperty
<
Any
,
T
>
{
override
fun
getValue
(
thisRef
:
Any
,
property
:
KProperty
<
*
>):
T
{
if
(
config
.
exist
(
property
.
name
))
{
//unsafe
return
config
.
_smartCast
(
property
.
name
,
_class
)
}
return
defaultValue
}
override
fun
setValue
(
thisRef
:
Any
,
property
:
KProperty
<
*
>,
value
:
T
)
{
config
[
property
.
name
]
=
value
}
}
}
}
inline
fun
<
reified
T
:
Any
>
Config
.
smartCast
(
property
:
KProperty
<
*
>):
T
{
return
_smartCast
(
property
.
name
,
T
::
class
)
}
@Suppress
(
"IMPLICIT_CAST_TO_ANY"
,
"UNCHECKED_CAST"
)
fun
<
T
:
Any
>
Config
.
_smartCast
(
propertyName
:
String
,
_class
:
KClass
<
T
>):
T
{
return
when
(
_class
)
{
String
::
class
-> this.get
String
(
propertyName
)
Int
::
class
-> this.get
Int
(
propertyName
)
Float
::
class
-> this.get
Float
(
propertyName
)
Double
::
class
-> this.get
Double
(
propertyName
)
Long
::
class
-> this.get
Long
(
propertyName
)
Boolean
::
class
-> this.get
Boolean
(
propertyName
)
else
->
when
{
T
::
class
.
isSubclassOf
(
ConfigSection
::
class
)
->
this
.
getConfigSection
(
property
.
n
ame
)
T
::
class
==
List
::
class
||
T
::
class
==
MutableList
::
class
-> {
val list = this.get
List
(
property
.
n
ame
)
_class
.
isSubclassOf
(
ConfigSection
::
class
)
->
this
.
getConfigSection
(
propertyN
ame
)
_class
==
List
::
class
|| _
class ==
MutableList
::
class
-> {
val list = this.get
List
(
property
N
ame
)
return
if
(
list
.
isEmpty
())
{
list
}
else
{
when
(
list
[
0
]
!!::
class
)
{
String
::
class
-> get
StringList
(
property
.
n
ame
)
Int
::
class
-> get
IntList
(
property
.
n
ame
)
Float
::
class
-> get
FloatList
(
property
.
n
ame
)
Double
::
class
-> get
DoubleList
(
property
.
n
ame
)
Long
::
class
-> get
LongList
(
property
.
n
ame
)
String
::
class
-> get
StringList
(
property
N
ame
)
Int
::
class
-> get
IntList
(
property
N
ame
)
Float
::
class
-> get
FloatList
(
property
N
ame
)
Double
::
class
-> get
DoubleList
(
property
N
ame
)
Long
::
class
-> get
LongList
(
property
N
ame
)
else
->
{
error
(
"unsupported type"
)
}
...
...
@@ -121,14 +183,6 @@ inline fun <reified T> Config.smartCast(property: KProperty<*>): T {
}
as
T
}
inline
operator
fun
<
reified
T
>
Config
.
getValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>):
T
{
return
smartCast
(
property
)
}
inline
operator
fun
<
reified
T
>
Config
.
setValue
(
thisRef
:
Any
?,
property
:
KProperty
<
*
>,
value
:
T
)
{
this
[
property
.
name
]
=
value
!!
this
.
save
()
}
interface
ConfigSection
:
Config
{
...
...
@@ -187,12 +241,16 @@ interface ConfigSection : Config {
override
fun
exist
(
key
:
String
):
Boolean
{
return
get
(
key
)
!=
null
}
override
fun
setIfAbsent
(
key
:
String
,
value
:
Any
)
{
if
(!
exist
(
key
))
set
(
key
,
value
)
}
}
@Serializable
open
class
ConfigSectionImpl
()
:
ConcurrentHashMap
<
String
,
Any
>(),
ConfigSection
{
override
fun
set
(
key
:
String
,
value
:
Any
)
{
this
.
put
(
key
,
value
)
super
.
put
(
key
,
value
)
}
override
operator
fun
get
(
key
:
String
):
Any
?
{
...
...
@@ -210,6 +268,10 @@ open class ConfigSectionImpl() : ConcurrentHashMap<String, Any>(), ConfigSection
override
fun
save
()
{
}
override
fun
setIfAbsent
(
key
:
String
,
value
:
Any
)
{
this
.
putIfAbsent
(
key
,
value
)
//atomic
}
}
...
...
@@ -255,26 +317,15 @@ class JsonConfig internal constructor(file: File) : FileConfigImpl(file) {
if
(
content
.
isEmpty
()
||
content
.
isBlank
()
||
content
==
"{}"
)
{
return
ConfigSectionImpl
()
}
val
section
=
JSON
.
parseObject
(
return
JSON
.
parseObject
<
ConfigSectionImpl
>
(
content
,
object
:
TypeReference
<
ConfigSectionImpl
>()
{},
Feature
.
OrderedField
)
return
section
}
@UnstableDefault
override
fun
serialize
(
config
:
ConfigSection
):
String
{
return
JSONObject
.
toJSONString
(
config
)
}
internal
class
AnySerializer
(
override
val
descriptor
:
SerialDescriptor
)
:
KSerializer
<
Any
>
{
override
fun
deserialize
(
decoder
:
Decoder
):
Any
{
TODO
(
"not implemented"
)
//To change body of created functions use File | Settings | File Templates.
}
override
fun
serialize
(
encoder
:
Encoder
,
obj
:
Any
)
{
TODO
(
"not implemented"
)
//To change body of created functions use File | Settings | File Templates.
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment