Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
ziqian zhang
Grasscutter
Commits
d215035f
Commit
d215035f
authored
May 11, 2022
by
KingRainbow44
Browse files
Refactor config database settings
parent
3612c6af
Changes
4
Hide whitespace changes
Inline
Side-by-side
src/main/java/emu/grasscutter/database/DatabaseHelper.java
View file @
d215035f
...
...
@@ -77,25 +77,25 @@ public final class DatabaseHelper {
}
public
static
Account
getAccountByName
(
String
username
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"username"
,
username
)).
first
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"username"
,
username
)).
first
();
}
public
static
Account
getAccountByToken
(
String
token
)
{
if
(
token
==
null
)
return
null
;
return
DatabaseManager
.
getDatastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"token"
,
token
)).
first
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"token"
,
token
)).
first
();
}
public
static
Account
getAccountBySessionKey
(
String
sessionKey
)
{
if
(
sessionKey
==
null
)
return
null
;
return
DatabaseManager
.
getDatastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"sessionKey"
,
sessionKey
)).
first
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"sessionKey"
,
sessionKey
)).
first
();
}
public
static
Account
getAccountById
(
String
uid
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
uid
)).
first
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
uid
)).
first
();
}
public
static
Account
getAccountByPlayerId
(
int
playerId
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"playerId"
,
playerId
)).
first
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"playerId"
,
playerId
)).
first
();
}
public
static
void
deleteAccount
(
Account
target
)
{
...
...
@@ -104,37 +104,37 @@ public final class DatabaseHelper {
// database in an inconsistent state, but unfortunately Mongo only supports that when we have a replica set ...
// Delete Mail.class data
DatabaseManager
.
getDatabase
().
getCollection
(
"mail"
).
deleteMany
(
eq
(
"ownerUid"
,
target
.
getPlayerUid
()));
DatabaseManager
.
get
Game
Database
().
getCollection
(
"mail"
).
deleteMany
(
eq
(
"ownerUid"
,
target
.
getPlayerUid
()));
// Delete Avatar.class data
DatabaseManager
.
getDatabase
().
getCollection
(
"avatars"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
DatabaseManager
.
get
Game
Database
().
getCollection
(
"avatars"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
// Delete GachaRecord.class data
DatabaseManager
.
getDatabase
().
getCollection
(
"gachas"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
DatabaseManager
.
get
Game
Database
().
getCollection
(
"gachas"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
// Delete GameItem.class data
DatabaseManager
.
getDatabase
().
getCollection
(
"items"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
DatabaseManager
.
get
Game
Database
().
getCollection
(
"items"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
// Delete friendships.
// Here, we need to make sure to not only delete the deleted account's friendships,
// but also all friendship entries for that account's friends.
DatabaseManager
.
getDatabase
().
getCollection
(
"friendships"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
DatabaseManager
.
getDatabase
().
getCollection
(
"friendships"
).
deleteMany
(
eq
(
"friendId"
,
target
.
getPlayerUid
()));
DatabaseManager
.
get
Game
Database
().
getCollection
(
"friendships"
).
deleteMany
(
eq
(
"ownerId"
,
target
.
getPlayerUid
()));
DatabaseManager
.
get
Game
Database
().
getCollection
(
"friendships"
).
deleteMany
(
eq
(
"friendId"
,
target
.
getPlayerUid
()));
// Delete the player.
DatabaseManager
.
getDatastore
().
find
(
Player
.
class
).
filter
(
Filters
.
eq
(
"id"
,
target
.
getPlayerUid
())).
delete
();
DatabaseManager
.
get
Game
Datastore
().
find
(
Player
.
class
).
filter
(
Filters
.
eq
(
"id"
,
target
.
getPlayerUid
())).
delete
();
// Finally, delete the account itself.
DatabaseManager
.
getDatastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"id"
,
target
.
getId
())).
delete
();
DatabaseManager
.
get
Game
Datastore
().
find
(
Account
.
class
).
filter
(
Filters
.
eq
(
"id"
,
target
.
getId
())).
delete
();
}
public
static
List
<
Player
>
getAllPlayers
()
{
return
DatabaseManager
.
getDatastore
().
find
(
Player
.
class
).
stream
().
toList
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Player
.
class
).
stream
().
toList
();
}
public
static
Player
getPlayerById
(
int
id
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Player
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
id
)).
first
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Player
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
id
)).
first
();
}
public
static
boolean
checkPlayerExists
(
int
id
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Player
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
id
)).
first
()
!=
null
;
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Player
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
id
)).
first
()
!=
null
;
}
public
static
synchronized
Player
createPlayer
(
Player
character
,
int
reservedId
)
{
...
...
@@ -151,7 +151,7 @@ public final class DatabaseHelper {
character
.
setUid
(
id
);
}
// Save to database
DatabaseManager
.
getDatastore
().
save
(
character
);
DatabaseManager
.
get
Game
Datastore
().
save
(
character
);
return
character
;
}
...
...
@@ -170,48 +170,48 @@ public final class DatabaseHelper {
}
public
static
void
savePlayer
(
Player
character
)
{
DatabaseManager
.
getDatastore
().
save
(
character
);
DatabaseManager
.
get
Game
Datastore
().
save
(
character
);
}
public
static
void
saveAvatar
(
Avatar
avatar
)
{
DatabaseManager
.
getDatastore
().
save
(
avatar
);
DatabaseManager
.
get
Game
Datastore
().
save
(
avatar
);
}
public
static
List
<
Avatar
>
getAvatars
(
Player
player
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Avatar
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
player
.
getUid
())).
stream
().
toList
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Avatar
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
player
.
getUid
())).
stream
().
toList
();
}
public
static
void
saveItem
(
GameItem
item
)
{
DatabaseManager
.
getDatastore
().
save
(
item
);
DatabaseManager
.
get
Game
Datastore
().
save
(
item
);
}
public
static
boolean
deleteItem
(
GameItem
item
)
{
DeleteResult
result
=
DatabaseManager
.
getDatastore
().
delete
(
item
);
DeleteResult
result
=
DatabaseManager
.
get
Game
Datastore
().
delete
(
item
);
return
result
.
wasAcknowledged
();
}
public
static
List
<
GameItem
>
getInventoryItems
(
Player
player
)
{
return
DatabaseManager
.
getDatastore
().
find
(
GameItem
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
player
.
getUid
())).
stream
().
toList
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
GameItem
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
player
.
getUid
())).
stream
().
toList
();
}
public
static
List
<
Friendship
>
getFriends
(
Player
player
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Friendship
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
player
.
getUid
())).
stream
().
toList
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Friendship
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
player
.
getUid
())).
stream
().
toList
();
}
public
static
List
<
Friendship
>
getReverseFriends
(
Player
player
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Friendship
.
class
).
filter
(
Filters
.
eq
(
"friendId"
,
player
.
getUid
())).
stream
().
toList
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Friendship
.
class
).
filter
(
Filters
.
eq
(
"friendId"
,
player
.
getUid
())).
stream
().
toList
();
}
public
static
void
saveFriendship
(
Friendship
friendship
)
{
DatabaseManager
.
getDatastore
().
save
(
friendship
);
DatabaseManager
.
get
Game
Datastore
().
save
(
friendship
);
}
public
static
void
deleteFriendship
(
Friendship
friendship
)
{
DatabaseManager
.
getDatastore
().
delete
(
friendship
);
DatabaseManager
.
get
Game
Datastore
().
delete
(
friendship
);
}
public
static
Friendship
getReverseFriendship
(
Friendship
friendship
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Friendship
.
class
).
filter
(
Filters
.
and
(
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Friendship
.
class
).
filter
(
Filters
.
and
(
Filters
.
eq
(
"ownerId"
,
friendship
.
getFriendId
()),
Filters
.
eq
(
"friendId"
,
friendship
.
getOwnerId
())
)).
first
();
...
...
@@ -222,7 +222,7 @@ public final class DatabaseHelper {
}
public
static
List
<
GachaRecord
>
getGachaRecords
(
int
ownerId
,
int
page
,
int
gachaType
,
int
pageSize
){
return
DatabaseManager
.
getDatastore
().
find
(
GachaRecord
.
class
).
filter
(
return
DatabaseManager
.
get
Game
Datastore
().
find
(
GachaRecord
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
ownerId
),
Filters
.
eq
(
"gachaType"
,
gachaType
)
).
iterator
(
new
FindOptions
()
...
...
@@ -237,7 +237,7 @@ public final class DatabaseHelper {
}
public
static
long
getGachaRecordsMaxPage
(
int
ownerId
,
int
page
,
int
gachaType
,
int
pageSize
){
long
count
=
DatabaseManager
.
getDatastore
().
find
(
GachaRecord
.
class
).
filter
(
long
count
=
DatabaseManager
.
get
Game
Datastore
().
find
(
GachaRecord
.
class
).
filter
(
Filters
.
eq
(
"ownerId"
,
ownerId
),
Filters
.
eq
(
"gachaType"
,
gachaType
)
).
count
();
...
...
@@ -245,19 +245,19 @@ public final class DatabaseHelper {
}
public
static
void
saveGachaRecord
(
GachaRecord
gachaRecord
){
DatabaseManager
.
getDatastore
().
save
(
gachaRecord
);
DatabaseManager
.
get
Game
Datastore
().
save
(
gachaRecord
);
}
public
static
List
<
Mail
>
getAllMail
(
Player
player
)
{
return
DatabaseManager
.
getDatastore
().
find
(
Mail
.
class
).
filter
(
Filters
.
eq
(
"ownerUid"
,
player
.
getUid
())).
stream
().
toList
();
return
DatabaseManager
.
get
Game
Datastore
().
find
(
Mail
.
class
).
filter
(
Filters
.
eq
(
"ownerUid"
,
player
.
getUid
())).
stream
().
toList
();
}
public
static
void
saveMail
(
Mail
mail
)
{
DatabaseManager
.
getDatastore
().
save
(
mail
);
DatabaseManager
.
get
Game
Datastore
().
save
(
mail
);
}
public
static
boolean
deleteMail
(
Mail
mail
)
{
DeleteResult
result
=
DatabaseManager
.
getDatastore
().
delete
(
mail
);
DeleteResult
result
=
DatabaseManager
.
get
Game
Datastore
().
delete
(
mail
);
return
result
.
wasAcknowledged
();
}
}
src/main/java/emu/grasscutter/database/DatabaseManager.java
View file @
d215035f
...
...
@@ -23,19 +23,19 @@ import emu.grasscutter.game.player.Player;
import
static
emu
.
grasscutter
.
Configuration
.*;
public
final
class
DatabaseManager
{
private
static
Datastore
d
atastore
;
private
static
Datastore
gameD
atastore
;
private
static
Datastore
dispatchDatastore
;
private
static
final
Class
<?>[]
mappedClasses
=
new
Class
<?>[]
{
DatabaseCounter
.
class
,
Account
.
class
,
Player
.
class
,
Avatar
.
class
,
GameItem
.
class
,
Friendship
.
class
,
GachaRecord
.
class
,
Mail
.
class
};
public
static
Datastore
getDatastore
()
{
return
d
atastore
;
public
static
Datastore
get
Game
Datastore
()
{
return
gameD
atastore
;
}
public
static
MongoDatabase
getDatabase
()
{
return
getDatastore
().
getDatabase
();
public
static
MongoDatabase
get
Game
Database
()
{
return
get
Game
Datastore
().
getDatabase
();
}
// Yes. I very dislike this method. However, this will be good for now.
...
...
@@ -44,42 +44,42 @@ public final class DatabaseManager {
if
(
SERVER
.
runMode
==
ServerRunMode
.
GAME_ONLY
)
{
return
dispatchDatastore
;
}
else
{
return
d
atastore
;
return
gameD
atastore
;
}
}
public
static
void
initialize
()
{
// Initialize
MongoClient
m
ongoClient
=
MongoClients
.
create
(
DATABASE
.
connectionUri
);
MongoClient
gameM
ongoClient
=
MongoClients
.
create
(
DATABASE
.
game
.
connectionUri
);
// Set mapper options.
MapperOptions
mapperOptions
=
MapperOptions
.
builder
()
.
storeEmpties
(
true
).
storeNulls
(
false
).
build
();
// Create data store.
d
atastore
=
Morphia
.
createDatastore
(
m
ongoClient
,
DATABASE
.
collection
,
mapperOptions
);
gameD
atastore
=
Morphia
.
createDatastore
(
gameM
ongoClient
,
DATABASE
.
game
.
collection
,
mapperOptions
);
// Map classes.
d
atastore
.
getMapper
().
map
(
mappedClasses
);
gameD
atastore
.
getMapper
().
map
(
mappedClasses
);
// Ensure indexes
try
{
d
atastore
.
ensureIndexes
();
gameD
atastore
.
ensureIndexes
();
}
catch
(
MongoCommandException
exception
)
{
Grasscutter
.
getLogger
().
info
(
"Mongo index error: "
,
exception
);
// Duplicate index error
if
(
exception
.
getCode
()
==
85
)
{
// Drop all indexes and re add them
MongoIterable
<
String
>
collections
=
d
atastore
.
getDatabase
().
listCollectionNames
();
MongoIterable
<
String
>
collections
=
gameD
atastore
.
getDatabase
().
listCollectionNames
();
for
(
String
name
:
collections
)
{
d
atastore
.
getDatabase
().
getCollection
(
name
).
dropIndexes
();
gameD
atastore
.
getDatabase
().
getCollection
(
name
).
dropIndexes
();
}
// Add back indexes
d
atastore
.
ensureIndexes
();
gameD
atastore
.
ensureIndexes
();
}
}
if
(
SERVER
.
runMode
==
ServerRunMode
.
GAME_ONLY
)
{
MongoClient
dispatchMongoClient
=
MongoClients
.
create
(
GAME_OPTIONS
.
databaseInfo
.
connectionUri
);
dispatchDatastore
=
Morphia
.
createDatastore
(
dispatchMongoClient
,
GAME_OPTIONS
.
databaseInfo
.
collection
);
MongoClient
dispatchMongoClient
=
MongoClients
.
create
(
DATABASE
.
server
.
connectionUri
);
dispatchDatastore
=
Morphia
.
createDatastore
(
dispatchMongoClient
,
DATABASE
.
server
.
collection
);
// Ensure indexes for dispatch server
try
{
...
...
@@ -101,14 +101,14 @@ public final class DatabaseManager {
}
public
static
synchronized
int
getNextId
(
Class
<?>
c
)
{
DatabaseCounter
counter
=
getDatastore
().
find
(
DatabaseCounter
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
c
.
getSimpleName
())).
first
();
DatabaseCounter
counter
=
get
Game
Datastore
().
find
(
DatabaseCounter
.
class
).
filter
(
Filters
.
eq
(
"_id"
,
c
.
getSimpleName
())).
first
();
if
(
counter
==
null
)
{
counter
=
new
DatabaseCounter
(
c
.
getSimpleName
());
}
try
{
return
counter
.
getNextId
();
}
finally
{
getDatastore
().
save
(
counter
);
get
Game
Datastore
().
save
(
counter
);
}
}
...
...
src/main/java/emu/grasscutter/plugin/PluginManager.java
View file @
d215035f
...
...
@@ -7,6 +7,7 @@ import emu.grasscutter.server.event.HandlerPriority;
import
emu.grasscutter.utils.Utils
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.InputStreamReader
;
import
java.lang.reflect.Method
;
import
java.net.MalformedURLException
;
...
...
@@ -90,6 +91,8 @@ public final class PluginManager {
fileReader
.
close
();
// Close the file reader.
}
catch
(
ClassNotFoundException
ignored
)
{
Grasscutter
.
getLogger
().
warn
(
"Plugin "
+
plugin
.
getName
()
+
" has an invalid main class."
);
}
catch
(
FileNotFoundException
ignored
)
{
Grasscutter
.
getLogger
().
warn
(
"Plugin "
+
plugin
.
getName
()
+
" lacks a valid config file."
);
}
}
catch
(
Exception
exception
)
{
Grasscutter
.
getLogger
().
error
(
"Failed to load plugin: "
+
plugin
.
getName
(),
exception
);
...
...
src/main/java/emu/grasscutter/utils/ConfigContainer.java
View file @
d215035f
...
...
@@ -2,6 +2,8 @@ package emu.grasscutter.utils;
import
com.google.gson.JsonObject
;
import
emu.grasscutter.Grasscutter
;
import
emu.grasscutter.Grasscutter.ServerDebugMode
;
import
emu.grasscutter.Grasscutter.ServerRunMode
;
import
java.io.FileReader
;
import
java.lang.reflect.Field
;
...
...
@@ -15,7 +17,7 @@ import static emu.grasscutter.Grasscutter.config;
*/
public
class
ConfigContainer
{
private
static
int
version
()
{
return
1
;
return
2
;
}
/**
...
...
@@ -69,8 +71,13 @@ public class ConfigContainer {
/* Option containers. */
public
static
class
Database
{
public
String
connectionUri
=
"mongodb://localhost:27017"
;
public
String
collection
=
"grasscutter"
;
public
DataStore
server
=
new
DataStore
();
public
DataStore
game
=
new
DataStore
();
public
static
class
DataStore
{
public
String
connectionUri
=
"mongodb://localhost:27017"
;
public
String
collection
=
"grasscutter"
;
}
}
public
static
class
Structure
{
...
...
@@ -86,8 +93,8 @@ public class ConfigContainer {
}
public
static
class
Server
{
public
Grasscutter
.
ServerDebugMode
debugLevel
=
Grasscutter
.
ServerDebugMode
.
NONE
;
public
Grasscutter
.
ServerRunMode
runMode
=
Grasscutter
.
ServerRunMode
.
HYBRID
;
public
ServerDebugMode
debugLevel
=
ServerDebugMode
.
NONE
;
public
ServerRunMode
runMode
=
ServerRunMode
.
HYBRID
;
public
Dispatch
dispatch
=
new
Dispatch
();
public
Game
game
=
new
Game
();
...
...
@@ -112,7 +119,7 @@ public class ConfigContainer {
public
int
bindPort
=
443
;
/* This is the port used in URLs. */
public
int
accessPort
=
443
;
public
int
accessPort
=
0
;
public
Encryption
encryption
=
new
Encryption
();
public
Policies
policies
=
new
Policies
();
...
...
@@ -128,7 +135,7 @@ public class ConfigContainer {
public
int
bindPort
=
22102
;
/* This is the port used in the default region. */
public
int
accessPort
=
22102
;
public
int
accessPort
=
0
;
public
GameOptions
gameOptions
=
new
GameOptions
();
public
JoinOptions
joinOptions
=
new
JoinOptions
();
...
...
@@ -155,16 +162,14 @@ public class ConfigContainer {
}
public
static
class
GameOptions
{
public
GameOptions
.
InventoryLimits
inventoryLimits
=
new
GameOptions
.
InventoryLimits
();
public
GameOptions
.
AvatarLimits
avatarLimits
=
new
GameOptions
.
AvatarLimits
();
public
InventoryLimits
inventoryLimits
=
new
InventoryLimits
();
public
AvatarLimits
avatarLimits
=
new
AvatarLimits
();
public
int
worldEntityLimit
=
1000
;
// Unenforced. TODO: Implement.
public
boolean
watchGachaConfig
=
false
;
public
boolean
enableShopItems
=
true
;
public
boolean
staminaUsage
=
true
;
public
GameOptions
.
Rates
rates
=
new
GameOptions
.
Rates
();
public
Database
databaseInfo
=
new
Database
();
public
Rates
rates
=
new
Rates
();
public
static
class
InventoryLimits
{
public
int
weapons
=
2000
;
...
...
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