Skip to content

Commit

Permalink
Send settings using protobufs over the wire
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Mar 11, 2024
1 parent 7cd4f49 commit 512daa6
Show file tree
Hide file tree
Showing 36 changed files with 216 additions and 145 deletions.
Expand Up @@ -27,7 +27,6 @@
import com.soulfiremc.brigadier.PlatformCommandManager;
import com.soulfiremc.client.grpc.RPCClient;
import com.soulfiremc.client.settings.ClientSettingsManager;
import com.soulfiremc.grpc.generated.AttackStartRequest;
import com.soulfiremc.grpc.generated.AttackStartResponse;
import com.soulfiremc.grpc.generated.CommandCompletionRequest;
import com.soulfiremc.grpc.generated.CommandHistoryRequest;
Expand Down Expand Up @@ -61,9 +60,7 @@ public void postConstruct() {
rpcClient
.attackStub()
.startAttack(
AttackStartRequest.newBuilder()
.setSettings(clientSettingsManager.exportSettings())
.build(),
clientSettingsManager.exportSettingsProto(),
new StreamObserver<>() {
@Override
public void onNext(AttackStartResponse value) {
Expand Down
Expand Up @@ -17,10 +17,10 @@
*/
package com.soulfiremc.client.cli;

import com.soulfiremc.account.AuthType;
import com.soulfiremc.brigadier.GenericTerminalConsole;
import com.soulfiremc.builddata.BuildData;
import com.soulfiremc.proxy.ProxyType;
import com.soulfiremc.settings.account.AuthType;
import com.soulfiremc.settings.proxy.ProxyType;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down
Expand Up @@ -17,11 +17,11 @@
*/
package com.soulfiremc.client.gui.navigation;

import com.soulfiremc.account.AuthType;
import com.soulfiremc.account.MinecraftAccount;
import com.soulfiremc.client.gui.GUIFrame;
import com.soulfiremc.client.gui.GUIManager;
import com.soulfiremc.client.gui.popups.ImportTextDialog;
import com.soulfiremc.settings.account.AuthType;
import com.soulfiremc.settings.account.MinecraftAccount;
import com.soulfiremc.util.BuiltinSettingsConstants;
import com.soulfiremc.util.EnabledWrapper;
import com.soulfiremc.util.SFPathConstants;
Expand Down
Expand Up @@ -18,7 +18,6 @@
package com.soulfiremc.client.gui.navigation;

import com.soulfiremc.client.gui.GUIManager;
import com.soulfiremc.grpc.generated.AttackStartRequest;
import com.soulfiremc.grpc.generated.AttackStartResponse;
import com.soulfiremc.grpc.generated.AttackStateToggleRequest;
import com.soulfiremc.grpc.generated.AttackStateToggleResponse;
Expand Down Expand Up @@ -65,9 +64,7 @@ public ControlPanel(GUIManager guiManager) {
.rpcClient()
.attackStub()
.startAttack(
AttackStartRequest.newBuilder()
.setSettings(guiManager.clientSettingsManager().exportSettings())
.build(),
guiManager.clientSettingsManager().exportSettingsProto(),
new StreamObserver<>() {
@Override
public void onNext(AttackStartResponse value) {
Expand Down
Expand Up @@ -21,8 +21,8 @@
import com.soulfiremc.client.gui.GUIManager;
import com.soulfiremc.client.gui.libs.JEnumComboBox;
import com.soulfiremc.client.gui.popups.ImportTextDialog;
import com.soulfiremc.proxy.ProxyType;
import com.soulfiremc.proxy.SFProxy;
import com.soulfiremc.settings.proxy.ProxyType;
import com.soulfiremc.settings.proxy.SFProxy;
import com.soulfiremc.util.BuiltinSettingsConstants;
import com.soulfiremc.util.EnabledWrapper;
import com.soulfiremc.util.SFPathConstants;
Expand Down
Expand Up @@ -17,12 +17,12 @@
*/
package com.soulfiremc.client.settings;

import com.soulfiremc.account.AuthType;
import com.soulfiremc.account.MinecraftAccount;
import com.soulfiremc.client.grpc.RPCClient;
import com.soulfiremc.grpc.generated.AuthRequest;
import com.soulfiremc.grpc.generated.MinecraftAccountProto;
import com.soulfiremc.proxy.SFProxy;
import com.soulfiremc.settings.account.AuthType;
import com.soulfiremc.settings.account.MinecraftAccount;
import com.soulfiremc.settings.proxy.SFProxy;
import com.soulfiremc.util.EnabledWrapper;
import java.util.ArrayList;
import java.util.Collections;
Expand Down
Expand Up @@ -20,8 +20,14 @@
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.gson.JsonElement;
import com.soulfiremc.grpc.generated.AttackStartRequest;
import com.soulfiremc.grpc.generated.SettingsEntry;
import com.soulfiremc.grpc.generated.SettingsNamespace;
import com.soulfiremc.settings.ProfileDataStructure;
import com.soulfiremc.settings.PropertyKey;
import com.soulfiremc.settings.account.MinecraftAccount;
import com.soulfiremc.settings.proxy.SFProxy;
import com.soulfiremc.util.EnabledWrapper;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -40,12 +46,14 @@
public class ClientSettingsManager {
private final Multimap<PropertyKey, Consumer<JsonElement>> listeners =
Multimaps.newListMultimap(new LinkedHashMap<>(), ArrayList::new);
private final Map<PropertyKey, Provider<JsonElement>> providers = new LinkedHashMap<>();
private final Map<String, Map<String, Provider<JsonElement>>> providers = new LinkedHashMap<>();
@Getter private final AccountRegistry accountRegistry;
@Getter private final ProxyRegistry proxyRegistry = new ProxyRegistry();

public void registerProvider(PropertyKey property, Provider<JsonElement> provider) {
providers.put(property, provider);
providers
.computeIfAbsent(property.namespace(), k -> new LinkedHashMap<>())
.put(property.key(), provider);
}

public void registerListener(PropertyKey property, Consumer<JsonElement> listener) {
Expand Down Expand Up @@ -76,19 +84,90 @@ public void saveProfile(Path path) throws IOException {

public String exportSettings() {
var settingsData = new LinkedHashMap<String, Map<String, JsonElement>>();
for (var providerEntry : providers.entrySet()) {
var property = providerEntry.getKey();
var provider = providerEntry.getValue();
for (var namespaceEntry : providers.entrySet()) {
for (var entry : namespaceEntry.getValue().entrySet()) {
var namespace = namespaceEntry.getKey();
var key = entry.getKey();
var value = entry.getValue().get();

var namespace = property.namespace();
var settingId = property.key();
var value = provider.get();

settingsData.computeIfAbsent(namespace, k -> new LinkedHashMap<>()).put(settingId, value);
settingsData.computeIfAbsent(namespace, k -> new LinkedHashMap<>()).put(key, value);
}
}

return new ProfileDataStructure(
settingsData, accountRegistry.getAccounts(), proxyRegistry.getProxies())
.serialize();
}

public AttackStartRequest exportSettingsProto() {
var namespaces = new ArrayList<SettingsNamespace>();

for (var namespaceEntry : providers.entrySet()) {
var namespace = namespaceEntry.getKey();
var namespaceSettings = new ArrayList<SettingsEntry>();

for (var entry : namespaceEntry.getValue().entrySet()) {
var key = entry.getKey();
var value = entry.getValue().get();

if (!value.isJsonPrimitive()) {
log.warn("Skipping non-primitive setting: {}#{}", namespace, key);
continue;
}

var primitive = value.getAsJsonPrimitive();
if (primitive.isBoolean()) {
namespaceSettings.add(
SettingsEntry.newBuilder()
.setKey(key)
.setBoolValue(primitive.getAsBoolean())
.build());
} else if (primitive.isNumber()) {
var number = primitive.getAsNumber();
if (number instanceof Integer) {
namespaceSettings.add(
SettingsEntry.newBuilder().setKey(key).setIntValue(number.intValue()).build());
} else if (number instanceof Double) {
namespaceSettings.add(
SettingsEntry.newBuilder()
.setKey(key)
.setDoubleValue(number.doubleValue())
.build());
} else {
log.warn("Skipping unsupported number setting: {}#{}", namespace, key);
}
} else if (primitive.isString()) {
namespaceSettings.add(
SettingsEntry.newBuilder()
.setKey(key)
.setStringValue(primitive.getAsString())
.build());
} else {
log.warn("Skipping unsupported primitive setting: {}#{}", namespace, key);
}
}

namespaces.add(
SettingsNamespace.newBuilder()
.setNamespace(namespace)
.addAllEntries(namespaceSettings)
.build());
}

return AttackStartRequest.newBuilder()
.addAllSettings(namespaces)
.addAllAccounts(
accountRegistry.getAccounts().stream()
.filter(EnabledWrapper::enabled)
.map(EnabledWrapper::value)
.map(MinecraftAccount::toProto)
.toList())
.addAllProxies(
proxyRegistry.getProxies().stream()
.filter(EnabledWrapper::enabled)
.map(EnabledWrapper::value)
.map(SFProxy::toProto)
.toList())
.build();
}
}
Expand Up @@ -17,8 +17,8 @@
*/
package com.soulfiremc.client.settings;

import com.soulfiremc.proxy.ProxyType;
import com.soulfiremc.proxy.SFProxy;
import com.soulfiremc.settings.proxy.ProxyType;
import com.soulfiremc.settings.proxy.SFProxy;
import com.soulfiremc.util.EnabledWrapper;
import java.util.ArrayList;
import java.util.Collections;
Expand Down
Expand Up @@ -26,10 +26,10 @@
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.soulfiremc.account.AuthType;
import com.soulfiremc.account.MinecraftAccount;
import com.soulfiremc.account.service.AccountData;
import com.soulfiremc.proxy.SFProxy;
import com.soulfiremc.settings.account.AuthType;
import com.soulfiremc.settings.account.MinecraftAccount;
import com.soulfiremc.settings.account.service.AccountData;
import com.soulfiremc.settings.proxy.SFProxy;
import com.soulfiremc.util.EnabledWrapper;
import java.lang.reflect.Type;
import java.security.GeneralSecurityException;
Expand Down
Expand Up @@ -15,12 +15,12 @@
* 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.account;
package com.soulfiremc.settings.account;

import com.soulfiremc.account.service.AccountData;
import com.soulfiremc.account.service.BedrockData;
import com.soulfiremc.account.service.OfflineJavaData;
import com.soulfiremc.account.service.OnlineJavaData;
import com.soulfiremc.settings.account.service.AccountData;
import com.soulfiremc.settings.account.service.BedrockData;
import com.soulfiremc.settings.account.service.OfflineJavaData;
import com.soulfiremc.settings.account.service.OnlineJavaData;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand Down
Expand Up @@ -15,13 +15,13 @@
* 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.account;
package com.soulfiremc.settings.account;

import com.soulfiremc.account.service.AccountData;
import com.soulfiremc.account.service.BedrockData;
import com.soulfiremc.account.service.OfflineJavaData;
import com.soulfiremc.account.service.OnlineJavaData;
import com.soulfiremc.grpc.generated.MinecraftAccountProto;
import com.soulfiremc.settings.account.service.AccountData;
import com.soulfiremc.settings.account.service.BedrockData;
import com.soulfiremc.settings.account.service.OfflineJavaData;
import com.soulfiremc.settings.account.service.OnlineJavaData;
import java.util.UUID;
import lombok.NonNull;

Expand Down
Expand Up @@ -15,6 +15,6 @@
* 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.account.service;
package com.soulfiremc.settings.account.service;

public sealed interface AccountData permits BedrockData, OfflineJavaData, OnlineJavaData {}
Expand Up @@ -15,7 +15,7 @@
* 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.account.service;
package com.soulfiremc.settings.account.service;

import com.soulfiremc.grpc.generated.MinecraftAccountProto;
import com.soulfiremc.util.KeyHelper;
Expand Down
Expand Up @@ -15,7 +15,7 @@
* 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.account.service;
package com.soulfiremc.settings.account.service;

import com.soulfiremc.grpc.generated.MinecraftAccountProto;
import java.nio.charset.StandardCharsets;
Expand Down
Expand Up @@ -15,7 +15,7 @@
* 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.account.service;
package com.soulfiremc.settings.account.service;

import com.soulfiremc.grpc.generated.MinecraftAccountProto;

Expand Down
Expand Up @@ -15,7 +15,7 @@
* 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.proxy;
package com.soulfiremc.settings.proxy;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
Expand Down
Expand Up @@ -15,7 +15,7 @@
* 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.proxy;
package com.soulfiremc.settings.proxy;

import com.soulfiremc.grpc.generated.ProxyProto;
import java.net.InetSocketAddress;
Expand Down
Expand Up @@ -18,7 +18,7 @@
package com.soulfiremc.util;

import com.soulfiremc.builddata.BuildData;
import com.soulfiremc.proxy.SFProxy;
import com.soulfiremc.settings.proxy.SFProxy;
import java.time.Duration;
import reactor.netty.transport.ProxyProvider;

Expand Down
Expand Up @@ -94,7 +94,8 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
// Ignore
}

// In the next step, we pretend we own the classes of either the parent or the child class loaders
// In the next step, we pretend we own the classes of either the parent or the child class
// loaders
var parentClassData = getClassBytes(this.getParent(), name);
if (parentClassData == null) {
if (lookingThroughPlugins) {
Expand Down
21 changes: 20 additions & 1 deletion proto/src/main/proto/com/soulfiremc/grpc/generated/attack.proto
@@ -1,11 +1,30 @@
syntax = "proto3";

import "com/soulfiremc/grpc/generated/common.proto";

option java_multiple_files = true;

package com.soulfiremc.grpc.generated;

message SettingsEntry {
string key = 1;
oneof value {
string stringValue = 2;
int32 intValue = 3;
bool boolValue = 4;
double doubleValue = 5;
}
}

message SettingsNamespace {
string namespace = 1;
repeated SettingsEntry entries = 2;
}

message AttackStartRequest {
string settings = 1;
repeated SettingsNamespace settings = 1;
repeated MinecraftAccountProto accounts = 2;
repeated ProxyProto proxies = 3;
}

message AttackStartResponse {
Expand Down

0 comments on commit 512daa6

Please sign in to comment.