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
c50c4365
Commit
c50c4365
authored
Feb 07, 2020
by
Him188
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Override equals, hashcode and toString
parent
4ae0eecd
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
110 deletions
+64
-110
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/MessageQQA.kt
...Main/kotlin/net/mamoe/mirai/qqandroid/utils/MessageQQA.kt
+17
-1
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt
...c/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt
+46
-108
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/Main.kt
...rai-demo-gentleman/src/main/kotlin/demo/gentleman/Main.kt
+1
-1
No files found.
mirai-core-qqandroid/src/commonMain/kotlin/net/mamoe/mirai/qqandroid/utils/MessageQQA.kt
View file @
c50c4365
...
...
@@ -216,7 +216,15 @@ internal class CustomFaceFromServer(
override
val
size
:
Int
get
()
=
delegate
.
size
override
val
original
:
Int
get
()
=
delegate
.
origin
override
val
pbReserve
:
ByteArray
get
()
=
delegate
.
pbReserve
override
val
miraiImageId
:
String
get
()
=
delegate
.
filePath
override
val
imageId
:
String
get
()
=
delegate
.
filePath
override
fun
equals
(
other
:
Any
?):
Boolean
{
return
other
is
CustomFaceFromServer
&&
other
.
filepath
==
this
.
filepath
&&
other
.
md5
.
contentEquals
(
this
.
md5
)
}
override
fun
hashCode
():
Int
{
return
filepath
.
hashCode
()
+
31
*
md5
.
hashCode
()
}
}
internal
class
NotOnlineImageFromServer
(
...
...
@@ -233,6 +241,14 @@ internal class NotOnlineImageFromServer(
override
val
downloadPath
:
String
get
()
=
delegate
.
downloadPath
override
val
fileId
:
Int
get
()
=
delegate
.
fileId
override
val
original
:
Int
get
()
=
delegate
.
original
override
fun
equals
(
other
:
Any
?):
Boolean
{
return
other
is
NotOnlineImageFromServer
&&
other
.
resourceId
==
this
.
resourceId
&&
other
.
md5
.
contentEquals
(
this
.
md5
)
}
override
fun
hashCode
():
Int
{
return
resourceId
.
hashCode
()
+
31
*
md5
.
hashCode
()
}
}
@UseExperimental
(
ExperimentalUnsignedTypes
::
class
,
MiraiInternalAPI
::
class
)
...
...
mirai-core/src/commonMain/kotlin/net.mamoe.mirai/message/data/Image.kt
View file @
c50c4365
...
...
@@ -3,6 +3,7 @@
package
net.mamoe.mirai.message.data
import
kotlinx.serialization.Serializable
import
kotlinx.serialization.Transient
import
net.mamoe.mirai.utils.io.chunkedHexToBytes
import
kotlin.jvm.JvmName
...
...
@@ -12,17 +13,30 @@ import kotlin.jvm.JvmName
sealed
class
Image
:
Message
{
companion
object
Key
:
Message
.
Key
<
Image
>
{
@JvmName
(
"fromId"
)
operator
fun
invoke
(
miraiImageId
:
String
):
Image
=
when
(
miraiI
mageId
.
length
)
{
37
->
NotOnlineImageFromFile
(
miraiI
mageId
)
// /f8f1ab55-bf8e-4236-b55e-955848d7069f
42
->
CustomFaceFromFile
(
miraiI
mageId
)
// {01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.png
else
->
throw
IllegalArgumentException
(
"Bad
miraiImageId, expecting length=37 or 42, got ${miraiI
mageId.length}"
)
operator
fun
invoke
(
imageId
:
String
):
Image
=
when
(
i
mageId
.
length
)
{
37
->
NotOnlineImageFromFile
(
i
mageId
)
// /f8f1ab55-bf8e-4236-b55e-955848d7069f
42
->
CustomFaceFromFile
(
i
mageId
)
// {01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.png
else
->
throw
IllegalArgumentException
(
"Bad
imageId, expecting length=37 or 42, got ${i
mageId.length}"
)
}
}
abstract
val
miraiImageId
:
String
abstract
override
fun
toString
():
String
/**
* 图片的 id. 只需要有这个 id 即可发送图片.
*
* 示例:
* 好友图片的 id: `/f8f1ab55-bf8e-4236-b55e-955848d7069f`
* 群图片的 id: `{01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.png`
*/
abstract
val
imageId
:
String
final
override
fun
toString
():
String
{
return
"[image::$imageId]"
}
abstract
override
fun
eq
(
other
:
Message
):
Boolean
final
override
fun
eq
(
other
:
Message
):
Boolean
{
return
if
(
other
is
Image
)
return
other
.
imageId
==
this
.
imageId
else
this
.
toString
()
==
other
.
toString
()
}
}
abstract
class
CustomFace
:
Image
()
{
...
...
@@ -42,27 +56,19 @@ abstract class CustomFace : Image() {
abstract
val
size
:
Int
abstract
val
pbReserve
:
ByteArray
abstract
val
original
:
Int
override
fun
toString
():
String
{
return
"[CustomFace]"
}
override
fun
eq
(
other
:
Message
):
Boolean
{
return
this
.
toString
()
==
other
.
toString
()
}
}
private
val
EMPTY_BYTE_ARRAY
=
ByteArray
(
0
)
private
fun
calculateImageMd5By
MiraiImageId
(
miraiI
mageId
:
String
):
ByteArray
{
return
if
(
miraiI
mageId
.
startsWith
(
'/'
))
{
miraiI
mageId
private
fun
calculateImageMd5By
ImageId
(
i
mageId
:
String
):
ByteArray
{
return
if
(
i
mageId
.
startsWith
(
'/'
))
{
i
mageId
.
drop
(
1
)
.
replace
(
'-'
,
' '
)
.
take
(
16
*
2
)
.
chunkedHexToBytes
()
}
else
{
miraiI
mageId
i
mageId
.
substringAfter
(
'{'
)
.
substringBefore
(
'}'
)
.
replace
(
'-'
,
' '
)
...
...
@@ -75,7 +81,7 @@ data class CustomFaceFromFile(
override
val
filepath
:
String
,
// {01E9451B-70ED-EAE3-B37C-101F1EEBF5B5}.png
override
val
md5
:
ByteArray
)
:
CustomFace
()
{
constructor
(
miraiImageId
:
String
)
:
this
(
filepath
=
miraiImageId
,
md5
=
calculateImageMd5ByMiraiImageId
(
miraiI
mageId
))
constructor
(
imageId
:
String
)
:
this
(
filepath
=
imageId
,
md5
=
calculateImageMd5ByImageId
(
i
mageId
))
override
val
fileId
:
Int
get
()
=
0
override
val
serverIp
:
Int
get
()
=
0
...
...
@@ -91,51 +97,14 @@ data class CustomFaceFromFile(
override
val
size
:
Int
get
()
=
0
override
val
original
:
Int
get
()
=
1
override
val
pbReserve
:
ByteArray
get
()
=
EMPTY_BYTE_ARRAY
override
val
miraiI
mageId
:
String
get
()
=
filepath
override
val
i
mageId
:
String
get
()
=
filepath
override
fun
equals
(
other
:
Any
?):
Boolean
{
if
(
this
===
other
)
return
true
if
(
other
==
null
||
this
::
class
!= other::class) return false
other as
CustomFaceFromFile
if
(
filepath
!=
other
.
filepath
)
return
false
if
(
fileId
!=
other
.
fileId
)
return
false
if
(
serverIp
!=
other
.
serverIp
)
return
false
if
(
serverPort
!=
other
.
serverPort
)
return
false
if
(
fileType
!=
other
.
fileType
)
return
false
if
(!
signature
.
contentEquals
(
other
.
signature
))
return
false
if
(
useful
!=
other
.
useful
)
return
false
if
(!
md5
.
contentEquals
(
other
.
md5
))
return
false
if
(
bizType
!=
other
.
bizType
)
return
false
if
(
imageType
!=
other
.
imageType
)
return
false
if
(
width
!=
other
.
width
)
return
false
if
(
height
!=
other
.
height
)
return
false
if
(
source
!=
other
.
source
)
return
false
if
(
size
!=
other
.
size
)
return
false
if
(
original
!=
this
.
original
)
return
false
if
(!
pbReserve
.
contentEquals
(
other
.
pbReserve
))
return
false
return
true
override
fun
hashCode
():
Int
{
return
filepath
.
hashCode
()
+
31
*
md5
.
hashCode
()
}
override
fun
hashCode
():
Int
{
var
result
=
filepath
.
hashCode
()
result
=
31
*
result
+
fileId
result
=
31
*
result
+
serverIp
result
=
31
*
result
+
serverPort
result
=
31
*
result
+
fileType
result
=
31
*
result
+
signature
.
contentHashCode
()
result
=
31
*
result
+
useful
result
=
31
*
result
+
md5
.
contentHashCode
()
result
=
31
*
result
+
bizType
result
=
31
*
result
+
imageType
result
=
31
*
result
+
width
result
=
31
*
result
+
height
result
=
31
*
result
+
source
result
=
31
*
result
+
size
result
=
31
*
result
+
pbReserve
.
contentHashCode
()
return
result
override
fun
equals
(
other
:
Any
?):
Boolean
{
return
other
is
CustomFaceFromFile
&&
other
.
md5
.
contentEquals
(
this
.
md5
)
&&
other
.
filepath
==
this
.
filepath
}
}
...
...
@@ -155,60 +124,29 @@ abstract class NotOnlineImage : Image() {
open
val
downloadPath
:
String
get
()
=
resourceId
open
val
original
:
Int
get
()
=
1
override
val
miraiImageId
:
String
get
()
=
resourceId
override
fun
toString
():
String
{
return
"[NotOnlineImage $resourceId]"
}
override
fun
eq
(
other
:
Message
):
Boolean
{
return
other
.
toString
()
==
this
.
toString
()
}
override
val
imageId
:
String
get
()
=
resourceId
}
@Serializable
data class
NotOnlineImageFromFile
(
override
val
resourceId
:
String
,
override
val
md5
:
ByteArray
,
override
val
filepath
:
String
=
resourceId
,
override
val
fileLength
:
Int
=
0
,
override
val
height
:
Int
=
0
,
override
val
width
:
Int
=
0
,
override
val
bizType
:
Int
=
0
,
override
val
imageType
:
Int
=
1000
,
override
val
downloadPath
:
String
=
resourceId
,
override
val
fileId
:
Int
=
0
@Transient
override
val
filepath
:
String
=
resourceId
,
@Transient
override
val
fileLength
:
Int
=
0
,
@Transient
override
val
height
:
Int
=
0
,
@Transient
override
val
width
:
Int
=
0
,
@Transient
override
val
bizType
:
Int
=
0
,
@Transient
override
val
imageType
:
Int
=
1000
,
@Transient
override
val
downloadPath
:
String
=
resourceId
,
@Transient
override
val
fileId
:
Int
=
0
)
:
NotOnlineImage
()
{
constructor
(
miraiImageId
:
String
)
:
this
(
resourceId
=
miraiImageId
,
md5
=
calculateImageMd5ByMiraiImageId
(
miraiI
mageId
))
constructor
(
imageId
:
String
)
:
this
(
resourceId
=
imageId
,
md5
=
calculateImageMd5ByImageId
(
i
mageId
))
override
fun
equals
(
other
:
Any
?):
Boolean
{
if
(
this
===
other
)
return
true
if
(
other
==
null
||
this
::
class
!= other::class) return false
other as
NotOnlineImageFromFile
if
(
resourceId
!=
other
.
resourceId
)
return
false
if
(!
md5
.
contentEquals
(
other
.
md5
))
return
false
if
(
filepath
!=
other
.
filepath
)
return
false
if
(
fileLength
!=
other
.
fileLength
)
return
false
if
(
height
!=
other
.
height
)
return
false
if
(
width
!=
other
.
width
)
return
false
if
(
bizType
!=
other
.
bizType
)
return
false
if
(
imageType
!=
other
.
imageType
)
return
false
if
(
downloadPath
!=
other
.
downloadPath
)
return
false
return
true
override
fun
hashCode
():
Int
{
return
resourceId
.
hashCode
()
+
31
*
md5
.
hashCode
()
}
override
fun
hashCode
():
Int
{
var
result
=
resourceId
.
hashCode
()
result
=
31
*
result
+
md5
.
contentHashCode
()
result
=
31
*
result
+
filepath
.
hashCode
()
result
=
31
*
result
+
fileLength
result
=
31
*
result
+
height
result
=
31
*
result
+
width
result
=
31
*
result
+
bizType
result
=
31
*
result
+
imageType
result
=
31
*
result
+
downloadPath
.
hashCode
()
return
result
override
fun
equals
(
other
:
Any
?):
Boolean
{
return
other
is
NotOnlineImageFromFile
&&
other
.
md5
.
contentEquals
(
this
.
md5
)
&&
other
.
resourceId
==
this
.
resourceId
}
}
\ No newline at end of file
mirai-demos/mirai-demo-gentleman/src/main/kotlin/demo/gentleman/Main.kt
View file @
c50c4365
...
...
@@ -131,7 +131,7 @@ suspend fun main() {
try
{
image
.
downloadTo
(
newTestTempFile
(
suffix
=
".png"
).
also
{
reply
(
"Temp file: ${it.absolutePath}"
)
})
reply
(
image
.
miraiI
mageId
+
" downloaded"
)
reply
(
image
.
i
mageId
+
" downloaded"
)
}
catch
(
e
:
Exception
)
{
e
.
printStackTrace
()
reply
(
e
.
message
?:
e
::
class
.
java
.
simpleName
)
...
...
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