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
83de4b4c
Commit
83de4b4c
authored
Aug 20, 2019
by
liujiahua123123
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
YAML supported
parent
a23c2e0d
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
228 additions
and
5 deletions
+228
-5
mirai-core/pom.xml
mirai-core/pom.xml
+10
-0
mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java
mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java
+47
-5
mirai-core/src/main/java/net/mamoe/mirai/utils/MiraiLogger.kt
...i-core/src/main/java/net/mamoe/mirai/utils/MiraiLogger.kt
+1
-0
mirai-core/src/main/java/net/mamoe/mirai/utils/Utils.java
mirai-core/src/main/java/net/mamoe/mirai/utils/Utils.java
+73
-0
mirai-core/src/main/java/net/mamoe/mirai/utils/config/MiraiConfig.java
...c/main/java/net/mamoe/mirai/utils/config/MiraiConfig.java
+76
-0
mirai-core/src/main/java/net/mamoe/mirai/utils/config/MiraiConfigSection.java
...java/net/mamoe/mirai/utils/config/MiraiConfigSection.java
+21
-0
No files found.
mirai-core/pom.xml
View file @
83de4b4c
...
...
@@ -40,6 +40,16 @@
<artifactId>
log4j-core
</artifactId>
<version>
2.12.1
</version>
</dependency>
<dependency>
<groupId>
org.yaml
</groupId>
<artifactId>
snakeyaml
</artifactId>
<version>
1.18
</version>
</dependency>
<dependency>
<groupId>
org.yaml
</groupId>
<artifactId>
snakeyaml
</artifactId>
<version>
1.18
</version>
</dependency>
</dependencies>
...
...
mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java
View file @
83de4b4c
...
...
@@ -8,7 +8,10 @@ import net.mamoe.mirai.network.Robot;
import
net.mamoe.mirai.task.MiraiTaskManager
;
import
net.mamoe.mirai.utils.LoggerTextFormat
;
import
net.mamoe.mirai.utils.MiraiLogger
;
import
net.mamoe.mirai.utils.config.MiraiConfig
;
import
net.mamoe.mirai.utils.config.MiraiConfigSection
;
import
net.mamoe.mirai.utils.setting.MiraiSetting
;
import
net.mamoe.mirai.utils.setting.MiraiSettingListSection
;
import
net.mamoe.mirai.utils.setting.MiraiSettingMapSection
;
import
java.io.File
;
...
...
@@ -42,6 +45,8 @@ public class MiraiServer {
MiraiSetting
setting
;
MiraiConfig
qqs
;
protected
MiraiServer
(){
instance
=
this
;
...
...
@@ -73,13 +78,24 @@ public class MiraiServer {
getLogger
().
info
(
"Loading data under "
+
LoggerTextFormat
.
GREEN
+
this
.
parentFolder
);
File
setting
=
new
File
(
this
.
parentFolder
+
"/Mirai.ini"
);
getLogger
().
info
(
"Selecting setting from "
+
LoggerTextFormat
.
GREEN
+
setting
);
if
(!
setting
.
exists
()){
this
.
initSetting
(
setting
);
}
else
{
this
.
setting
=
new
MiraiSetting
(
setting
);
}
getLogger
().
info
(
"Success"
);
File
qqs
=
new
File
(
this
.
parentFolder
+
"/QQ.yml"
);
getLogger
().
info
(
"Reading QQ accounts from "
+
LoggerTextFormat
.
GREEN
+
qqs
);
if
(!
qqs
.
exists
()){
this
.
initQQConfig
(
qqs
);
}
else
{
this
.
qqs
=
new
MiraiConfig
(
qqs
);
}
if
(
this
.
qqs
.
isEmpty
()){
this
.
initQQConfig
(
qqs
);
}
/*
MiraiSettingMapSection qqs = this.setting.getMapSection("qq");
...
...
@@ -95,6 +111,8 @@ public class MiraiServer {
});
*/
getLogger
().
info
(
"ready to connect"
);
Robot
robot
=
new
Robot
(
1994701021
,
"xiaoqqq"
);
try
{
//System.out.println(Protocol.Companion.getSERVER_IP().toString());
...
...
@@ -136,8 +154,28 @@ public class MiraiServer {
}
this
.
setting
=
new
MiraiSetting
(
setting
);
MiraiSettingMapSection
network
=
this
.
setting
.
getMapSection
(
"network"
);
network
.
set
(
"enable_proxy"
,
"not supporting yet"
);
MiraiSettingMapSection
qqs
=
this
.
setting
.
getMapSection
(
"qq"
);
MiraiSettingListSection
proxy
=
this
.
setting
.
getListSection
(
"proxy"
);
proxy
.
add
(
"1.2.3.4:95"
);
proxy
.
add
(
"1.2.3.4:100"
);
MiraiSettingMapSection
worker
=
this
.
setting
.
getMapSection
(
"worker"
);
worker
.
set
(
"core_task_pool_worker_amount"
,
5
);
MiraiSettingMapSection
plugin
=
this
.
setting
.
getMapSection
(
"plugin"
);
plugin
.
set
(
"debug"
,
false
);
this
.
setting
.
save
();
getLogger
().
info
(
LoggerTextFormat
.
SKY_BLUE
+
"initialized; changing can be made in setting file: "
+
setting
.
toString
());
}
private
void
initQQConfig
(
File
qqConfig
){
this
.
qqs
=
new
MiraiConfig
(
qqConfig
);
MiraiConfigSection
<
Object
>
section
=
new
MiraiConfigSection
<>();
System
.
out
.
println
(
"/"
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
getLogger
().
info
(
LoggerTextFormat
.
SKY_BLUE
+
"input one "
+
LoggerTextFormat
.
RED
+
" QQ number "
+
LoggerTextFormat
.
SKY_BLUE
+
"for default robot"
);
getLogger
().
info
(
LoggerTextFormat
.
SKY_BLUE
+
"输入用于默认机器人的QQ号"
);
...
...
@@ -145,9 +183,13 @@ public class MiraiServer {
getLogger
().
info
(
LoggerTextFormat
.
SKY_BLUE
+
"input the password for that QQ account"
);
getLogger
().
info
(
LoggerTextFormat
.
SKY_BLUE
+
"输入该QQ号对应密码"
);
String
qqPassword
=
scanner
.
next
();
getLogger
().
info
(
LoggerTextFormat
.
SKY_BLUE
+
"initialized; changing can be made in config file: "
+
setting
.
toString
());
qqs
.
put
(
String
.
valueOf
(
qqNumber
),
qqPassword
);
this
.
setting
.
save
();
section
.
put
(
"password"
,
qqPassword
);
section
.
put
(
"owner"
,
"default"
);
this
.
qqs
.
put
(
String
.
valueOf
(
qqNumber
),
section
);
this
.
qqs
.
save
();
getLogger
().
info
(
LoggerTextFormat
.
SKY_BLUE
+
"QQ account initialized; changing can be made in Config file: "
+
qqConfig
.
toString
());
}
private
void
onEnable
(){
...
...
mirai-core/src/main/java/net/mamoe/mirai/utils/MiraiLogger.kt
View file @
83de4b4c
...
...
@@ -34,4 +34,5 @@ object MiraiLogger {
}
fun
log
(
any
:
Any
?)
=
MiraiLogger
.
info
(
any
)
\ No newline at end of file
mirai-core/src/main/java/net/mamoe/mirai/utils/Utils.java
0 → 100644
View file @
83de4b4c
package
net.mamoe.mirai.utils
;
import
java.io.*
;
import
java.nio.charset.StandardCharsets
;
public
class
Utils
{
/**
* File supporting from Nukkit
* */
public
static
void
writeFile
(
String
fileName
,
String
content
)
throws
IOException
{
writeFile
(
fileName
,
new
ByteArrayInputStream
(
content
.
getBytes
(
StandardCharsets
.
UTF_8
)));
}
public
static
void
writeFile
(
String
fileName
,
InputStream
content
)
throws
IOException
{
writeFile
(
new
File
(
fileName
),
content
);
}
public
static
void
writeFile
(
File
file
,
String
content
)
throws
IOException
{
writeFile
(
file
,
new
ByteArrayInputStream
(
content
.
getBytes
(
StandardCharsets
.
UTF_8
)));
}
public
static
void
writeFile
(
File
file
,
InputStream
content
)
throws
IOException
{
if
(
content
==
null
)
{
throw
new
IllegalArgumentException
(
"content must not be null"
);
}
if
(!
file
.
exists
())
{
file
.
createNewFile
();
}
try
(
FileOutputStream
stream
=
new
FileOutputStream
(
file
))
{
byte
[]
buffer
=
new
byte
[
1024
];
int
length
;
while
((
length
=
content
.
read
(
buffer
))
!=
-
1
)
{
stream
.
write
(
buffer
,
0
,
length
);
}
}
content
.
close
();
}
public
static
String
readFile
(
File
file
)
throws
IOException
{
if
(!
file
.
exists
()
||
file
.
isDirectory
())
{
throw
new
FileNotFoundException
();
}
return
readFile
(
new
FileInputStream
(
file
));
}
public
static
String
readFile
(
String
filename
)
throws
IOException
{
File
file
=
new
File
(
filename
);
if
(!
file
.
exists
()
||
file
.
isDirectory
())
{
throw
new
FileNotFoundException
();
}
return
readFile
(
new
FileInputStream
(
file
));
}
public
static
String
readFile
(
InputStream
inputStream
)
throws
IOException
{
return
readFile
(
new
InputStreamReader
(
inputStream
,
StandardCharsets
.
UTF_8
));
}
private
static
String
readFile
(
Reader
reader
)
throws
IOException
{
try
(
BufferedReader
br
=
new
BufferedReader
(
reader
))
{
String
temp
;
StringBuilder
stringBuilder
=
new
StringBuilder
();
temp
=
br
.
readLine
();
while
(
temp
!=
null
)
{
if
(
stringBuilder
.
length
()
!=
0
)
{
stringBuilder
.
append
(
"\n"
);
}
stringBuilder
.
append
(
temp
);
temp
=
br
.
readLine
();
}
return
stringBuilder
.
toString
();
}
}
}
mirai-core/src/main/java/net/mamoe/mirai/utils/config/MiraiConfig.java
0 → 100644
View file @
83de4b4c
package
net.mamoe.mirai.utils.config
;
import
net.mamoe.mirai.MiraiServer
;
import
net.mamoe.mirai.utils.Utils
;
import
org.yaml.snakeyaml.DumperOptions
;
import
org.yaml.snakeyaml.Yaml
;
import
java.io.File
;
import
java.io.IOException
;
import
java.util.LinkedHashMap
;
import
java.util.Map
;
/**
* YAML-TYPE CONFIG
* Thread SAFE
* @author NaturalHG
*/
public
class
MiraiConfig
extends
MiraiConfigSection
<
Object
>{
private
volatile
File
root
;
public
MiraiConfig
(
File
file
){
super
();
if
(!
file
.
toURI
().
getPath
().
contains
(
MiraiServer
.
getInstance
().
getParentFolder
().
getPath
())){
file
=
new
File
((
MiraiServer
.
getInstance
().
getParentFolder
().
getPath
()
+
"/"
+
file
).
replace
(
"//"
,
"/"
));
}
this
.
root
=
file
;
if
(!
file
.
exists
()){
try
{
if
(!
file
.
createNewFile
()){
return
;
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
this
.
parse
();
}
private
MiraiConfig
(){
}
public
synchronized
void
save
(){
DumperOptions
dumperOptions
=
new
DumperOptions
();
dumperOptions
.
setDefaultFlowStyle
(
DumperOptions
.
FlowStyle
.
BLOCK
);
Yaml
yaml
=
new
Yaml
(
dumperOptions
);
String
content
=
yaml
.
dump
(
this
);
try
{
Utils
.
writeFile
(
this
.
root
,
content
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
@SuppressWarnings
(
"unchecked"
)
private
void
parse
(){
DumperOptions
dumperOptions
=
new
DumperOptions
();
dumperOptions
.
setDefaultFlowStyle
(
DumperOptions
.
FlowStyle
.
BLOCK
);
Yaml
yaml
=
new
Yaml
(
dumperOptions
);
this
.
clear
();
try
{
Map
<
String
,
Object
>
content
=
yaml
.
loadAs
(
Utils
.
readFile
(
this
.
root
),
LinkedHashMap
.
class
);
if
(
content
!=
null
)
{
this
.
putAll
(
content
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
mirai-core/src/main/java/net/mamoe/mirai/utils/config/MiraiConfigSection.java
0 → 100644
View file @
83de4b4c
package
net.mamoe.mirai.utils.config
;
import
org.jetbrains.annotations.NotNull
;
import
java.util.*
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentSkipListMap
;
import
java.util.concurrent.locks.Lock
;
import
java.util.concurrent.locks.ReentrantLock
;
public
class
MiraiConfigSection
<
T
>
extends
ConcurrentSkipListMap
<
String
,
T
>
{
public
MiraiConfigSection
(){
/*
* Ensure the key will be in order
* */
super
((
a
,
b
)
->
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