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
ba4b2253
Commit
ba4b2253
authored
Feb 23, 2020
by
jiahua.liu
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
21990bdf
8cc25641
Changes
19
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
19 changed files
with
348 additions
and
195 deletions
+348
-195
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt
...kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt
+1
-1
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
...tlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
+1
-1
mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt
...onsole/graphical/controller/MiraiGraphicalUIController.kt
+3
-3
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt
...ommonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt
+3
-1
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt
...Main/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt
+5
-5
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/Codec.kt
...kotlin/net/mamoe/mirai/qqandroid/network/highway/Codec.kt
+15
-8
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt
...ndroid/network/protocol/packet/chat/receive/MessageSvc.kt
+0
-3
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt
.../src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt
+6
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt
...mmonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt
+1
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/At.kt
.../src/commonMain/kotlin/net.mamoe.mirai/message/data/At.kt
+2
-2
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/AtAll.kt
...c/commonMain/kotlin/net.mamoe.mirai/message/data/AtAll.kt
+1
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/CombinedMessage.kt
...in/kotlin/net.mamoe.mirai/message/data/CombinedMessage.kt
+41
-0
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt
...commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt
+5
-6
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt
...nMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt
+28
-159
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt
...mmonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt
+9
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/builder.kt
...commonMain/kotlin/net.mamoe.mirai/message/data/builder.kt
+70
-0
mirai-core/src/commonTest/kotlin/net.mamoe.mirai.message.data/CombinedMessageTest.kt
...otlin/net.mamoe.mirai.message.data/CombinedMessageTest.kt
+155
-0
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt
.../jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt
+1
-3
mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/utils/LockFreeLinkedListTest.kt
...st/kotlin/net/mamoe/mirai/utils/LockFreeLinkedListTest.kt
+1
-0
No files found.
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/data/common/MessageDTO.kt
View file @
ba4b2253
...
@@ -97,7 +97,7 @@ fun MessageChain.toDTOChain() = mutableListOf(this[MessageSource].toDTO()).apply
...
@@ -97,7 +97,7 @@ fun MessageChain.toDTOChain() = mutableListOf(this[MessageSource].toDTO()).apply
}
}
fun
MessageChainDTO
.
toMessageChain
(
contact
:
Contact
)
=
fun
MessageChainDTO
.
toMessageChain
(
contact
:
Contact
)
=
MessageChain
().
apply
{
this
@
toMessageChain
.
forEach
{
add
(
it
.
toMessage
(
contact
))
}
}
buildMessageChain
{
this
@
toMessageChain
.
forEach
{
add
(
it
.
toMessage
(
contact
))
}
}
@UseExperimental
(
ExperimentalUnsignedTypes
::
class
)
@UseExperimental
(
ExperimentalUnsignedTypes
::
class
)
fun
Message
.
toDTO
()
=
when
(
this
)
{
fun
Message
.
toDTO
()
=
when
(
this
)
{
...
...
mirai-api-http/src/main/kotlin/net/mamoe/mirai/api/http/route/MessageRouteModule.kt
View file @
ba4b2253
...
@@ -54,7 +54,7 @@ fun Application.messageModule() {
...
@@ -54,7 +54,7 @@ fun Application.messageModule() {
}
}
}
}
miraiVerify
<
SendDTO
>(
"/
q
uoteMessage"
)
{
miraiVerify
<
SendDTO
>(
"/
sendQ
uoteMessage"
)
{
it
.
session
.
messageQueue
.
quoteCache
[
it
.
target
]
?.
apply
{
it
.
session
.
messageQueue
.
quoteCache
[
it
.
target
]
?.
apply
{
quoteReply
(
it
.
messageChain
.
toMessageChain
(
group
))
quoteReply
(
it
.
messageChain
.
toMessageChain
(
group
))
}
?:
throw
NoSuchElementException
()
}
?:
throw
NoSuchElementException
()
...
...
mirai-console-graphical/src/main/kotlin/net/mamoe/mirai/console/graphical/controller/MiraiGraphicalUIController.kt
View file @
ba4b2253
...
@@ -32,11 +32,11 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI {
...
@@ -32,11 +32,11 @@ class MiraiGraphicalUIController : Controller(), MiraiConsoleUI {
val
consoleInfo
=
ConsoleInfo
()
val
consoleInfo
=
ConsoleInfo
()
suspend
fun
login
(
qq
:
String
,
psd
:
String
)
{
fun
login
(
qq
:
String
,
psd
:
String
)
{
// MiraiConsole
MiraiConsole
.
CommandProcessor
.
runConsoleCommandBlocking
(
"/login $qq $psd"
)
}
}
suspend
fun
sendCommand
(
command
:
String
)
=
Unit
fun
sendCommand
(
command
:
String
)
=
MiraiConsole
.
CommandProcessor
.
runConsoleCommandBlocking
(
command
)
override
fun
pushLog
(
identity
:
Long
,
message
:
String
)
=
Platform
.
runLater
{
override
fun
pushLog
(
identity
:
Long
,
message
:
String
)
=
Platform
.
runLater
{
when
(
identity
)
{
when
(
identity
)
{
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/ContactImpl.kt
View file @
ba4b2253
...
@@ -147,6 +147,7 @@ internal class QQImpl(
...
@@ -147,6 +147,7 @@ internal class QQImpl(
}
}
}
finally
{
}
finally
{
(
image
.
input
as
?
Closeable
)
?.
close
()
(
image
.
input
as
?
Closeable
)
?.
close
()
(
image
.
input
as
?
io
.
ktor
.
utils
.
io
.
core
.
Closeable
)
?.
close
()
}
}
@MiraiExperimentalAPI
@MiraiExperimentalAPI
...
@@ -644,7 +645,8 @@ internal class GroupImpl(
...
@@ -644,7 +645,8 @@ internal class GroupImpl(
}
}
}
}
}
finally
{
}
finally
{
(
image
.
input
as
Closeable
)
?.
close
()
(
image
.
input
as
?
Closeable
)
?.
close
()
(
image
.
input
as
?
io
.
ktor
.
utils
.
io
.
core
.
Closeable
)
?.
close
()
}
}
override
fun
toString
():
String
{
override
fun
toString
():
String
{
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/message/messages.kt
View file @
ba4b2253
...
@@ -326,10 +326,10 @@ internal class NotOnlineImageFromServer(
...
@@ -326,10 +326,10 @@ internal class NotOnlineImageFromServer(
internal
fun
MsgComm
.
Msg
.
toMessageChain
():
MessageChain
{
internal
fun
MsgComm
.
Msg
.
toMessageChain
():
MessageChain
{
val
elements
=
this
.
msgBody
.
richText
.
elems
val
elements
=
this
.
msgBody
.
richText
.
elems
val
message
=
MessageChain
(
initialCapacity
=
elements
.
size
+
1
)
val
message
=
ArrayList
<
Message
>(
elements
.
size
+
1
)
message
.
add
(
MessageSourceFromMsg
(
delegate
=
this
))
message
.
add
(
MessageSourceFromMsg
(
delegate
=
this
))
elements
.
joinToMessageChain
(
message
)
elements
.
joinToMessageChain
(
message
)
return
message
return
message
.
asMessageChain
()
}
}
// These two functions are not the same.
// These two functions are not the same.
...
@@ -338,15 +338,15 @@ internal fun MsgComm.Msg.toMessageChain(): MessageChain {
...
@@ -338,15 +338,15 @@ internal fun MsgComm.Msg.toMessageChain(): MessageChain {
internal
fun
ImMsgBody
.
SourceMsg
.
toMessageChain
():
MessageChain
{
internal
fun
ImMsgBody
.
SourceMsg
.
toMessageChain
():
MessageChain
{
val
elements
=
this
.
elems
!!
val
elements
=
this
.
elems
!!
val
message
=
MessageChain
(
initialCapacity
=
elements
.
size
+
1
)
val
message
=
ArrayList
<
Message
>(
elements
.
size
+
1
)
message
.
add
(
MessageSourceFromServer
(
delegate
=
this
))
message
.
add
(
MessageSourceFromServer
(
delegate
=
this
))
elements
.
joinToMessageChain
(
message
)
elements
.
joinToMessageChain
(
message
)
return
message
return
message
.
asMessageChain
()
}
}
@UseExperimental
(
MiraiInternalAPI
::
class
,
ExperimentalUnsignedTypes
::
class
,
MiraiDebugAPI
::
class
)
@UseExperimental
(
MiraiInternalAPI
::
class
,
ExperimentalUnsignedTypes
::
class
,
MiraiDebugAPI
::
class
)
internal
fun
List
<
ImMsgBody
.
Elem
>.
joinToMessageChain
(
message
:
M
essageChain
)
{
internal
fun
List
<
ImMsgBody
.
Elem
>.
joinToMessageChain
(
message
:
M
utableList
<
Message
>
)
{
this
.
forEach
{
this
.
forEach
{
when
{
when
{
it
.
srcMsg
!=
null
->
message
.
add
(
QuoteReply
(
MessageSourceFromServer
(
it
.
srcMsg
)))
it
.
srcMsg
!=
null
->
message
.
add
(
QuoteReply
(
MessageSourceFromServer
(
it
.
srcMsg
)))
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/highway/Codec.kt
View file @
ba4b2253
...
@@ -86,25 +86,32 @@ object Highway {
...
@@ -86,25 +86,32 @@ object Highway {
writeFully
(
head
)
writeFully
(
head
)
when
(
body
)
{
when
(
body
)
{
is
ByteReadPacket
->
writePacket
(
body
)
is
ByteReadPacket
->
writePacket
(
body
)
is
Input
->
ByteArrayPool
.
useInstance
{
buffer
->
is
Input
->
body
.
use
{
ByteArrayPool
.
useInstance
{
buffer
->
var
size
:
Int
var
size
:
Int
while
(
body
.
readAvailable
(
buffer
).
also
{
size
=
it
}
!=
0
)
{
while
(
body
.
readAvailable
(
buffer
).
also
{
size
=
it
}
!=
0
)
{
this
@
buildPacket
.
writeFully
(
buffer
,
0
,
size
)
this
@
buildPacket
.
writeFully
(
buffer
,
0
,
size
)
}
}
}
}
}
is
ByteReadChannel
->
ByteArrayPool
.
useInstance
{
buffer
->
is
ByteReadChannel
->
ByteArrayPool
.
useInstance
{
buffer
->
var
size
:
Int
var
size
:
Int
while
(
body
.
readAvailable
(
buffer
,
0
,
buffer
.
size
).
also
{
size
=
it
}
!=
0
)
{
while
(
body
.
readAvailable
(
buffer
,
0
,
buffer
.
size
).
also
{
size
=
it
}
!=
0
)
{
this
@
buildPacket
.
writeFully
(
buffer
,
0
,
size
)
this
@
buildPacket
.
writeFully
(
buffer
,
0
,
size
)
}
}
}
}
is
InputStream
->
ByteArrayPool
.
useInstance
{
buffer
->
is
InputStream
->
try
{
ByteArrayPool
.
useInstance
{
buffer
->
var
size
:
Int
var
size
:
Int
while
(
body
.
read
(
buffer
).
also
{
size
=
it
}
!=
0
)
{
while
(
body
.
read
(
buffer
).
also
{
size
=
it
}
!=
0
)
{
this
@
buildPacket
.
writeFully
(
buffer
,
0
,
size
)
this
@
buildPacket
.
writeFully
(
buffer
,
0
,
size
)
}
}
}
}
}
finally
{
body
.
close
()
}
}
}
writeByte
(
41
)
writeByte
(
41
)
}
}
}
}
...
...
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/network/protocol/packet/chat/receive/MessageSvc.kt
View file @
ba4b2253
...
@@ -26,7 +26,6 @@ import net.mamoe.mirai.event.subscribingGetAsync
...
@@ -26,7 +26,6 @@ import net.mamoe.mirai.event.subscribingGetAsync
import
net.mamoe.mirai.message.FriendMessage
import
net.mamoe.mirai.message.FriendMessage
import
net.mamoe.mirai.message.data.MessageChain
import
net.mamoe.mirai.message.data.MessageChain
import
net.mamoe.mirai.message.data.MessageSource
import
net.mamoe.mirai.message.data.MessageSource
import
net.mamoe.mirai.message.data.addOrRemove
import
net.mamoe.mirai.qqandroid.GroupImpl
import
net.mamoe.mirai.qqandroid.GroupImpl
import
net.mamoe.mirai.qqandroid.QQAndroidBot
import
net.mamoe.mirai.qqandroid.QQAndroidBot
import
net.mamoe.mirai.qqandroid.io.serialization.decodeUniPacket
import
net.mamoe.mirai.qqandroid.io.serialization.decodeUniPacket
...
@@ -398,8 +397,6 @@ internal class MessageSvc {
...
@@ -398,8 +397,6 @@ internal class MessageSvc {
msgVia
=
1
msgVia
=
1
)
)
)
)
message
.
addOrRemove
(
source
)
}
}
override
suspend
fun
ByteReadPacket
.
decode
(
bot
:
QQAndroidBot
):
Response
{
override
suspend
fun
ByteReadPacket
.
decode
(
bot
:
QQAndroidBot
):
Response
{
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/contact/Contact.kt
View file @
ba4b2253
...
@@ -139,3 +139,8 @@ suspend inline fun <C : Contact> C.sendMessage(message: Message): MessageReceipt
...
@@ -139,3 +139,8 @@ suspend inline fun <C : Contact> C.sendMessage(message: Message): MessageReceipt
* @see Contact.sendMessage
* @see Contact.sendMessage
*/
*/
suspend
inline
fun
<
C
:
Contact
>
C
.
sendMessage
(
plain
:
String
):
MessageReceipt
<
C
>
=
sendMessage
(
plain
.
toMessage
())
suspend
inline
fun
<
C
:
Contact
>
C
.
sendMessage
(
plain
:
String
):
MessageReceipt
<
C
>
=
sendMessage
(
plain
.
toMessage
())
/**
* @see Contact.sendMessage
*/
suspend
inline
fun
<
C
:
Contact
>
C
.
sendMessage
(
plain
:
CombinedMessage
):
MessageReceipt
<
C
>
=
sendMessage
(
MessageChain
(
plain
as
Message
))
as
MessageReceipt
<
C
>
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/MessageReceipt.kt
View file @
ba4b2253
...
@@ -15,6 +15,7 @@ import net.mamoe.mirai.Bot
...
@@ -15,6 +15,7 @@ import net.mamoe.mirai.Bot
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.contact.Contact
import
net.mamoe.mirai.contact.Group
import
net.mamoe.mirai.contact.Group
import
net.mamoe.mirai.contact.QQ
import
net.mamoe.mirai.contact.QQ
import
net.mamoe.mirai.contact.sendMessage
import
net.mamoe.mirai.message.data.*
import
net.mamoe.mirai.message.data.*
import
net.mamoe.mirai.recallIn
import
net.mamoe.mirai.recallIn
import
net.mamoe.mirai.utils.MiraiExperimentalAPI
import
net.mamoe.mirai.utils.MiraiExperimentalAPI
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/At.kt
View file @
ba4b2253
...
@@ -36,8 +36,8 @@ class At @MiraiInternalAPI constructor(val target: Long, val display: String) :
...
@@ -36,8 +36,8 @@ class At @MiraiInternalAPI constructor(val target: Long, val display: String) :
// 自动为消息补充 " "
// 自动为消息补充 " "
override
fun
followedBy
(
tail
:
Message
):
MessageChain
{
override
fun
followedBy
(
tail
:
Message
):
CombinedMessage
{
if
(
tail
is
PlainText
&&
tail
.
stringValue
.
startsWith
(
' '
))
{
if
(
tail
is
PlainText
&&
tail
.
stringValue
.
startsWith
(
' '
))
{
return
super
.
followedBy
(
tail
)
return
super
.
followedBy
(
tail
)
}
}
return
super
.
followedBy
(
PlainText
(
" "
))
+
tail
return
super
.
followedBy
(
PlainText
(
" "
))
+
tail
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/AtAll.kt
View file @
ba4b2253
...
@@ -27,7 +27,7 @@ object AtAll : Message, Message.Key<AtAll> {
...
@@ -27,7 +27,7 @@ object AtAll : Message, Message.Key<AtAll> {
// 自动为消息补充 " "
// 自动为消息补充 " "
override
fun
followedBy
(
tail
:
Message
):
MessageChain
{
override
fun
followedBy
(
tail
:
Message
):
CombinedMessage
{
if
(
tail
is
PlainText
&&
tail
.
stringValue
.
startsWith
(
' '
))
{
if
(
tail
is
PlainText
&&
tail
.
stringValue
.
startsWith
(
' '
))
{
return
super
.
followedBy
(
tail
)
return
super
.
followedBy
(
tail
)
}
}
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/CombinedMessage.kt
0 → 100644
View file @
ba4b2253
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.message.data
/**
* Left-biased list
*/
class
CombinedMessage
(
val
left
:
Message
,
val
element
:
Message
)
:
Iterable
<
Message
>,
Message
{
private
suspend
fun
SequenceScope
<
Message
>.
yieldCombinedOrElements
(
message
:
Message
)
{
when
(
message
)
{
is
CombinedMessage
->
{
yieldCombinedOrElements
(
message
.
element
)
yieldCombinedOrElements
(
message
.
left
)
}
is
MessageChain
->
message
.
forEach
{
yieldCombinedOrElements
(
it
)
}
else
->
yield
(
message
)
}
}
fun
asSequence
():
Sequence
<
Message
>
=
sequence
{
yieldCombinedOrElements
(
this
@CombinedMessage
)
}
override
fun
iterator
():
Iterator
<
Message
>
{
return
asSequence
().
iterator
()
}
override
fun
toString
():
String
{
return
left
.
toString
()
+
element
.
toString
()
}
}
\ No newline at end of file
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Message.kt
View file @
ba4b2253
...
@@ -85,20 +85,19 @@ interface Message {
...
@@ -85,20 +85,19 @@ interface Message {
* ```
* ```
*/
*/
@JvmSynthetic
// in java they should use `plus` instead
@JvmSynthetic
// in java they should use `plus` instead
fun
followedBy
(
tail
:
Message
):
MessageChain
{
fun
followedBy
(
tail
:
Message
):
CombinedMessage
{
require
(
tail
!
is
SingleOnly
)
{
"SingleOnly Message cannot follow another message"
}
require
(
tail
!
is
SingleOnly
)
{
"SingleOnly Message cannot follow another message"
}
require
(
this
!
is
SingleOnly
)
{
"SingleOnly Message cannot be followed"
}
require
(
this
!
is
SingleOnly
)
{
"SingleOnly Message cannot be followed"
}
return
if
(
tail
is
MessageChain
)
tail
.
followedBy
(
this
)
/*MessageChainImpl(this).also { tail.forEach { child -> it.concat(child) } }*/
return
CombinedMessage
(
tail
,
this
)
else
MessageChainImpl
(
this
,
tail
)
}
}
override
fun
toString
():
String
override
fun
toString
():
String
operator
fun
plus
(
another
:
Message
):
MessageChain
=
this
.
followedBy
(
another
)
operator
fun
plus
(
another
:
Message
):
CombinedMessage
=
this
.
followedBy
(
another
)
operator
fun
plus
(
another
:
String
):
MessageChain
=
this
.
followedBy
(
another
.
toMessage
())
operator
fun
plus
(
another
:
String
):
CombinedMessage
=
this
.
followedBy
(
another
.
toMessage
())
// `+ ""` will be resolved to `plus(String)` instead of `plus(CharSeq)`
// `+ ""` will be resolved to `plus(String)` instead of `plus(CharSeq)`
operator
fun
plus
(
another
:
CharSequence
):
MessageChain
=
this
.
followedBy
(
another
.
toString
().
toMessage
())
operator
fun
plus
(
another
:
CharSequence
):
CombinedMessage
=
this
.
followedBy
(
another
.
toString
().
toMessage
())
}
}
/**
/**
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/MessageChain.kt
View file @
ba4b2253
This diff is collapsed.
Click to expand it.
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/PlainText.kt
View file @
ba4b2253
...
@@ -14,6 +14,7 @@ package net.mamoe.mirai.message.data
...
@@ -14,6 +14,7 @@ package net.mamoe.mirai.message.data
import
kotlin.jvm.JvmMultifileClass
import
kotlin.jvm.JvmMultifileClass
import
kotlin.jvm.JvmName
import
kotlin.jvm.JvmName
import
kotlin.jvm.JvmStatic
/**
/**
* 纯文本. 可含 emoji 表情.
* 纯文本. 可含 emoji 表情.
...
@@ -21,10 +22,17 @@ import kotlin.jvm.JvmName
...
@@ -21,10 +22,17 @@ import kotlin.jvm.JvmName
* 一般不需要主动构造 [PlainText], [Message] 可直接与 [String] 相加. Java 用户请使用 [MessageChain.plus]
* 一般不需要主动构造 [PlainText], [Message] 可直接与 [String] 相加. Java 用户请使用 [MessageChain.plus]
*/
*/
inline
class
PlainText
(
val
stringValue
:
String
)
:
Message
{
inline
class
PlainText
(
val
stringValue
:
String
)
:
Message
{
constructor
(
charSequence
:
CharSequence
)
:
this
(
charSequence
.
toString
())
override
operator
fun
contains
(
sub
:
String
):
Boolean
=
sub
in
stringValue
override
operator
fun
contains
(
sub
:
String
):
Boolean
=
sub
in
stringValue
override
fun
toString
():
String
=
stringValue
override
fun
toString
():
String
=
stringValue
companion
object
Key
:
Message
.
Key
<
PlainText
>
companion
object
Key
:
Message
.
Key
<
PlainText
>
{
@JvmStatic
val
Empty
=
PlainText
(
""
)
@JvmStatic
val
Null
=
PlainText
(
"null"
)
}
}
}
/**
/**
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/builder.kt
0 → 100644
View file @
ba4b2253
/*
* Copyright 2020 Mamoe Technologies and contributors.
*
* 此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
* Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
*
* https://github.com/mamoe/mirai/blob/master/LICENSE
*/
package
net.mamoe.mirai.message.data
import
kotlin.jvm.JvmOverloads
/**
* 构造一个 [MessageChain]
*
* @see MessageChainBuilder
*/
inline
fun
buildMessageChain
(
block
:
MessageChainBuilder
.()
->
Unit
):
MessageChain
{
return
MessageChainBuilder
().
apply
(
block
).
asMessageChain
()
}
class
MessageChainBuilder
@JvmOverloads
constructor
(
private
val
container
:
MutableList
<
Message
>
=
mutableListOf
()
)
:
MutableList
<
Message
>
by
container
,
Appendable
{
operator
fun
Message
.
unaryPlus
()
{
add
(
this
)
}
operator
fun
String
.
unaryPlus
()
{
add
(
this
.
toMessage
())
}
operator
fun
plusAssign
(
plain
:
String
)
{
this
.
add
(
plain
.
toMessage
())
}
operator
fun
plusAssign
(
message
:
Message
)
{
this
.
add
(
message
)
}
fun
add
(
plain
:
String
)
{
this
.
add
(
plain
.
toMessage
())
}
operator
fun
plusAssign
(
charSequence
:
CharSequence
)
{
this
.
add
(
PlainText
(
charSequence
))
}
override
fun
append
(
c
:
Char
):
Appendable
=
apply
{
this
.
add
(
PlainText
(
c
.
toString
()))
}
override
fun
append
(
csq
:
CharSequence
?):
Appendable
=
apply
{
when
{
csq
==
null
->
this
.
add
(
PlainText
.
Null
)
csq
.
isEmpty
()
->
this
.
add
(
PlainText
.
Empty
)
else
->
this
.
add
(
PlainText
(
csq
))
}
}
override
fun
append
(
csq
:
CharSequence
?,
start
:
Int
,
end
:
Int
):
Appendable
=
apply
{
when
{
csq
==
null
->
this
.
add
(
PlainText
.
Null
)
csq
.
isEmpty
()
->
this
.
add
(
PlainText
.
Empty
)
else
->
this
.
add
(
PlainText
(
csq
.
substring
(
start
,
end
)))
}
}
}
\ No newline at end of file
mirai-core/src/commonTest/kotlin/net.mamoe.mirai.message.data/CombinedMessageTest.kt
0 → 100644
View file @
ba4b2253
package
net.mamoe.mirai.message.data
import
kotlin.test.Test
import
kotlin.test.assertEquals
import
kotlin.time.ExperimentalTime
import
kotlin.time.measureTime
internal
class
CombinedMessageTest
{
@Test
fun
testAsSequence
()
{
var
message
:
Message
=
"Hello "
.
toMessage
()
message
+=
"World"
assertEquals
(
"Hello World"
,
(
message
as
CombinedMessage
).
asSequence
().
joinToString
(
separator
=
""
)
)
}
@Test
fun
testAsSequence2
()
{
var
message
:
Message
=
"Hello "
.
toMessage
()
message
+=
MessageChain
(
PlainText
(
"W"
),
PlainText
(
"o"
),
PlainText
(
"r"
)
+
PlainText
(
"ld"
)
)
assertEquals
(
"Hello World"
,
(
message
as
CombinedMessage
).
asSequence
().
joinToString
(
separator
=
""
)
)
}
private
val
toAdd
=
"1"
.
toMessage
()
@UseExperimental
(
ExperimentalTime
::
class
)
@Test
fun
speedTest
()
=
repeat
(
100
)
{
var
count
=
1L
repeat
(
Int
.
MAX_VALUE
)
{
count
++
}
var
combineMessage
:
Message
=
toAdd
println
(
"init combine ok "
+
measureTime
{
repeat
(
1000
)
{
combineMessage
+=
toAdd
}
}.
inMilliseconds
)
val
list
=
mutableListOf
<
Message
>()
println
(
"init messageChain ok "
+
measureTime
{
repeat
(
1000
)
{
list
+=
toAdd
}
}.
inMilliseconds
)
measureTime
{
list
.
joinToString
(
separator
=
""
)
}.
let
{
time
->
println
(
"list foreach: ${time.inMilliseconds} ms"
)
}
measureTime
{
(
combineMessage
as
CombinedMessage
).
iterator
().
joinToString
(
separator
=
""
)
}.
let
{
time
->
println
(
"combined iterate: ${time.inMilliseconds} ms"
)
}
measureTime
{
(
combineMessage
as
CombinedMessage
).
asSequence
().
joinToString
(
separator
=
""
)
}.
let
{
time
->
println
(
"combined sequence: ${time.inMilliseconds} ms"
)
}
repeat
(
5
)
{
println
()
}
}
@UseExperimental
(
ExperimentalTime
::
class
)
@Test
fun
testFastIteration
()
{
println
(
"start!"
)
println
(
"start!"
)
println
(
"start!"
)
println
(
"start!"
)
var
combineMessage
:
Message
=
toAdd
println
(
"init combine ok "
+
measureTime
{
repeat
(
1000
)
{
combineMessage
+=
toAdd
}
}.
inMilliseconds
)
measureTime
{
(
combineMessage
as
CombinedMessage
).
iterator
().
joinToString
(
separator
=
""
)
}.
let
{
time
->
println
(
"combine: ${time.inMilliseconds} ms"
)
}
}
}
public
fun
<
T
>
Iterator
<
T
>.
joinToString
(
separator
:
CharSequence
=
", "
,
prefix
:
CharSequence
=
""
,
postfix
:
CharSequence
=
""
,
limit
:
Int
=
-
1
,
truncated
:
CharSequence
=
"..."
,
transform
:
((
T
)
->
CharSequence
)?
=
null
):
String
{
return
joinTo
(
StringBuilder
(),
separator
,
prefix
,
postfix
,
limit
,
truncated
,
transform
).
toString
()
}
public
fun
<
T
,
A
:
Appendable
>
Iterator
<
T
>.
joinTo
(
buffer
:
A
,
separator
:
CharSequence
=
", "
,
prefix
:
CharSequence
=
""
,
postfix
:
CharSequence
=
""
,
limit
:
Int
=
-
1
,
truncated
:
CharSequence
=
"..."
,
transform
:
((
T
)
->
CharSequence
)?
=
null
):
A
{
buffer
.
append
(
prefix
)
var
count
=
0
for
(
element
in
this
)
{
if
(++
count
>
1
)
buffer
.
append
(
separator
)
if
(
limit
<
0
||
count
<=
limit
)
{
buffer
.
appendElement
(
element
,
transform
)
}
else
break
}
if
(
limit
>=
0
&&
count
>
limit
)
buffer
.
append
(
truncated
)
buffer
.
append
(
postfix
)
return
buffer
}
internal
fun
<
T
>
Appendable
.
appendElement
(
element
:
T
,
transform
:
((
T
)
->
CharSequence
)?)
{
when
{
transform
!=
null
->
append
(
transform
(
element
))
element
is
CharSequence
?
->
append
(
element
)
element
is
Char
->
append
(
element
)
else
->
append
(
element
.
toString
())
}
}
\ No newline at end of file
mirai-core/src/jvmMain/kotlin/net/mamoe/mirai/utils/ExternalImageJvm.kt
View file @
ba4b2253
...
@@ -68,14 +68,12 @@ fun File.toExternalImage(): ExternalImage {
...
@@ -68,14 +68,12 @@ fun File.toExternalImage(): ExternalImage {
?:
error
(
"Unable to read file(path=${this.path}), no ImageReader found"
)
?:
error
(
"Unable to read file(path=${this.path}), no ImageReader found"
)
image
.
input
=
input
image
.
input
=
input
val
inputStream
=
this
.
inputStream
()
return
ExternalImage
(
return
ExternalImage
(
width
=
image
.
getWidth
(
0
),
width
=
image
.
getWidth
(
0
),
height
=
image
.
getHeight
(
0
),
height
=
image
.
getHeight
(
0
),
md5
=
this
.
inputStream
().
md5
(),
// dont change
md5
=
this
.
inputStream
().
md5
(),
// dont change
imageFormat
=
image
.
formatName
,
imageFormat
=
image
.
formatName
,
input
=
inputStream
.
asInput
(),
input
=
this
.
inputStream
(),
inputSize
=
inputStream
.
available
().
toLong
(),
filename
=
this
.
name
filename
=
this
.
name
)
)
}
}
...
...
mirai-core/src/jvmTest/kotlin/net/mamoe/mirai/utils/LockFreeLinkedListTest.kt
View file @
ba4b2253
...
@@ -58,6 +58,7 @@ internal class LockFreeLinkedListTest {
...
@@ -58,6 +58,7 @@ internal class LockFreeLinkedListTest {
@Test
@Test
fun
`so
many
concurrent
add
remove
and
foreach`
()
=
runBlocking
{
fun
`so
many
concurrent
add
remove
and
foreach`
()
=
runBlocking
{
return
@
runBlocking
// 测试通过了, 加快速度. 因为 kotlin 一些其他 bug
val
list
=
LockFreeLinkedList
<
Int
>()
val
list
=
LockFreeLinkedList
<
Int
>()
val
addJob
=
async
{
list
.
concurrentDo
(
2
,
30000
)
{
addLast
(
1
)
}
}
val
addJob
=
async
{
list
.
concurrentDo
(
2
,
30000
)
{
addLast
(
1
)
}
}
...
...
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