-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35458a7
commit 33d589a
Showing
11 changed files
with
200 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
server/src/main/java/com/soulfiremc/server/database/ServerConfigEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.server.database; | ||
|
||
import com.soulfiremc.server.settings.lib.ServerSettingsImpl; | ||
import jakarta.persistence.*; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
@Entity | ||
@Table(name = "server_config") | ||
public class ServerConfigEntity { | ||
@Id | ||
private Long id = 1L; | ||
|
||
@Convert(converter = ServerSettingsConverter.class) | ||
@Column(nullable = false) | ||
private ServerSettingsImpl settings = ServerSettingsImpl.EMPTY; | ||
|
||
@Version | ||
private long version; | ||
} |
37 changes: 37 additions & 0 deletions
37
server/src/main/java/com/soulfiremc/server/database/ServerSettingsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.server.database; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.soulfiremc.server.settings.lib.ServerSettingsImpl; | ||
import com.soulfiremc.server.util.structs.GsonInstance; | ||
import jakarta.persistence.AttributeConverter; | ||
import jakarta.persistence.Converter; | ||
|
||
@Converter(autoApply = true) | ||
public class ServerSettingsConverter implements AttributeConverter<ServerSettingsImpl, String> { | ||
@Override | ||
public String convertToDatabaseColumn(ServerSettingsImpl attribute) { | ||
return GsonInstance.GSON.toJson(attribute.serializeToTree()); | ||
} | ||
|
||
@Override | ||
public ServerSettingsImpl convertToEntityAttribute(String dbData) { | ||
return ServerSettingsImpl.deserialize(GsonInstance.GSON.fromJson(dbData, JsonElement.class)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
server/src/main/java/com/soulfiremc/server/grpc/ServerServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* SoulFire | ||
* Copyright (C) 2024 AlexProgrammerDE | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
package com.soulfiremc.server.grpc; | ||
|
||
import com.soulfiremc.grpc.generated.*; | ||
import com.soulfiremc.server.SoulFireServer; | ||
import com.soulfiremc.server.database.ServerConfigEntity; | ||
import com.soulfiremc.server.settings.lib.ServerSettingsImpl; | ||
import com.soulfiremc.server.user.PermissionContext; | ||
import io.grpc.Status; | ||
import io.grpc.StatusRuntimeException; | ||
import io.grpc.stub.StreamObserver; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.hibernate.SessionFactory; | ||
|
||
import javax.inject.Inject; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor(onConstructor_ = @Inject) | ||
public class ServerServiceImpl extends ServerServiceGrpc.ServerServiceImplBase { | ||
private final SoulFireServer soulFireServer; | ||
private final SessionFactory sessionFactory; | ||
|
||
@Override | ||
public void getServerInfo(ServerInfoRequest request, StreamObserver<ServerInfoResponse> responseObserver) { | ||
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.global(GlobalPermission.READ_SERVER_CONFIG)); | ||
|
||
try { | ||
var configEntity = sessionFactory.fromTransaction(session -> session.find(ServerConfigEntity.class, 1)); | ||
ServerSettingsImpl config; | ||
if (configEntity == null) { | ||
config = ServerSettingsImpl.EMPTY; | ||
} else { | ||
config = configEntity.settings(); | ||
} | ||
|
||
responseObserver.onNext(ServerInfoResponse.newBuilder() | ||
.setConfig(config.toProto()) | ||
.build()); | ||
responseObserver.onCompleted(); | ||
} catch (Throwable t) { | ||
log.error("Error getting server info", t); | ||
throw new StatusRuntimeException(Status.INTERNAL.withDescription(t.getMessage()).withCause(t)); | ||
} | ||
} | ||
|
||
@Override | ||
public void updateServerConfig(ServerUpdateConfigRequest request, StreamObserver<ServerUpdateConfigResponse> responseObserver) { | ||
ServerRPCConstants.USER_CONTEXT_KEY.get().hasPermissionOrThrow(PermissionContext.global(GlobalPermission.UPDATE_SERVER_CONFIG)); | ||
|
||
try { | ||
sessionFactory.inTransaction(session -> { | ||
var currentConfigEntity = session.find(ServerConfigEntity.class, 1); | ||
if (currentConfigEntity == null) { | ||
var newConfigEntity = new ServerConfigEntity(); | ||
newConfigEntity.settings(ServerSettingsImpl.fromProto(request.getConfig())); | ||
session.persist(newConfigEntity); | ||
} else { | ||
currentConfigEntity.settings(ServerSettingsImpl.fromProto(request.getConfig())); | ||
session.merge(currentConfigEntity); | ||
} | ||
}); | ||
|
||
soulFireServer.configUpdateHook(); | ||
responseObserver.onNext(ServerUpdateConfigResponse.newBuilder().build()); | ||
responseObserver.onCompleted(); | ||
} catch (Throwable t) { | ||
log.error("Error getting server info", t); | ||
throw new StatusRuntimeException(Status.INTERNAL.withDescription(t.getMessage()).withCause(t)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters