Skip to content

Commit db1ed01

Browse files
committed
Cache fields for reflection
1 parent f887ef9 commit db1ed01

File tree

1 file changed

+28
-14
lines changed
  • Aliucord/src/main/java/com/aliucord/coreplugins/slashcommandsfix

1 file changed

+28
-14
lines changed

Aliucord/src/main/java/com/aliucord/coreplugins/slashcommandsfix/Patches.java

+28-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.aliucord.patcher.PreHook;
1616
import com.aliucord.Utils;
1717
import com.aliucord.utils.GsonUtils;
18-
import com.aliucord.utils.ReflectUtils;
1918
import com.discord.api.channel.Channel;
2019
import com.discord.models.commands.Application;
2120
import com.discord.models.commands.ApplicationCommand;
@@ -51,6 +50,13 @@ final class Patches {
5150
private Method handleDiscoverCommandsUpdateMethod;
5251
private Method handleQueryCommandsUpdateMethod;
5352
private Field applicationCommandCountField;
53+
private Field storeApplicationCommandsQueryField;
54+
private Field errorResponseErrorField;
55+
private Field skemaErrorSubErrorsField;
56+
private Field skemaErrorErrorsField;
57+
private Field skemaErrorItemCodeField;
58+
private Field skemaErrorItemMessageField;
59+
private Field storeApplicationCommandsBuiltInCommandsProviderField;
5460

5561
Patches(Logger logger) throws Throwable {
5662
this.logger = logger;
@@ -67,6 +73,20 @@ public void loadPatches(Context context) throws Throwable {
6773
this.handleQueryCommandsUpdateMethod.setAccessible(true);
6874
this.applicationCommandCountField = Application.class.getDeclaredField("commandCount");
6975
this.applicationCommandCountField.setAccessible(true);
76+
this.storeApplicationCommandsQueryField = StoreApplicationCommands.class.getDeclaredField("query");
77+
this.storeApplicationCommandsQueryField.setAccessible(true);
78+
this.errorResponseErrorField = Error.Response.class.getDeclaredField("skemaError");
79+
this.errorResponseErrorField.setAccessible(true);
80+
this.skemaErrorSubErrorsField = Error.SkemaError.class.getDeclaredField("subErrors");
81+
this.skemaErrorSubErrorsField.setAccessible(true);
82+
this.skemaErrorErrorsField = Error.SkemaError.class.getDeclaredField("errors");
83+
this.skemaErrorErrorsField.setAccessible(true);
84+
this.skemaErrorItemCodeField = Error.SkemaErrorItem.class.getDeclaredField("code");
85+
this.skemaErrorItemCodeField.setAccessible(true);
86+
this.skemaErrorItemMessageField = Error.SkemaErrorItem.class.getDeclaredField("message");
87+
this.skemaErrorItemMessageField.setAccessible(true);
88+
this.storeApplicationCommandsBuiltInCommandsProviderField = StoreApplicationCommands.class.getDeclaredField("builtInCommandsProvider");
89+
this.storeApplicationCommandsBuiltInCommandsProviderField.setAccessible(true);
7090

7191
var storeApplicationCommands = StoreStream.getApplicationCommands();
7292
var storeChannelsSelected = StoreStream.getChannelsSelected();
@@ -107,7 +127,7 @@ public void loadPatches(Context context) throws Throwable {
107127

108128
var applicationIndexSource = Patches.applicationIndexSourceFromContext(this_.$guildId, storeChannelsSelected);
109129
try {
110-
ReflectUtils.setField(this_.this$0, "query", this_.$query);
130+
storeApplicationCommandsQueryField.set(this_.this$0, this_.$query);
111131
this.passCommandData(this_.this$0, applicationIndexSource, RequestSource.QUERY);
112132
} catch (Exception e) {
113133
throw new RuntimeException(e);
@@ -180,18 +200,12 @@ public void loadPatches(Context context) throws Throwable {
180200
var errorResponse = ((MessageResult.UnknownFailure) result)
181201
.getError()
182202
.getResponse();
183-
var error = ReflectUtils.getField(errorResponse, "skemaError");
184-
var dataErrors = (List<Error.SkemaErrorItem>) ReflectUtils.getField(
185-
((Map<String, Error.SkemaError>) ReflectUtils.getField(
186-
error,
187-
"subErrors"
188-
))
189-
.get("data"),
190-
"errors"
191-
);
203+
var error = this.errorResponseErrorField.get(errorResponse);
204+
var subErrors = ((Map<String, Error.SkemaError>) skemaErrorSubErrorsField.get(error));
205+
var dataErrors = (List<Error.SkemaErrorItem>) skemaErrorErrorsField.get(subErrors.get("data"));
192206

193207
for (var dataError: dataErrors) {
194-
var errorCode = (String) ReflectUtils.getField(dataError, "code");
208+
var errorCode = (String) this.skemaErrorItemCodeField.get(dataError);
195209
if (errorCode.equals("INTERACTION_APPLICATION_COMMAND_INVALID_VERSION")) {
196210
ApplicationIndexSource applicationIndexSource = null;
197211
var guildId = localSendData.component3();
@@ -203,7 +217,7 @@ public void loadPatches(Context context) throws Throwable {
203217
}
204218
this.cleanApplicationIndexCache(applicationIndexSource);
205219

206-
var errorMessage = (String) ReflectUtils.getField(dataError, "message");
220+
var errorMessage = (String) this.skemaErrorItemMessageField.get(dataError);
207221
Utils.showToast(errorMessage);
208222

209223
break;
@@ -237,7 +251,7 @@ private void passCommandData(StoreApplicationCommands storeApplicationCommands,
237251

238252
var applications = new ArrayList<Application>(applicationIndex.applications.values());
239253
Collections.sort(applications, (left, right) -> left.getName().compareTo(right.getName()));
240-
applications.add(((BuiltInCommandsProvider) ReflectUtils.getField(storeApplicationCommands, "builtInCommandsProvider")).getBuiltInApplication());
254+
applications.add(((BuiltInCommandsProvider) this.storeApplicationCommandsBuiltInCommandsProviderField.get(storeApplicationCommands)).getBuiltInApplication());
241255
this.handleGuildApplicationsUpdateMethod.invoke(storeApplicationCommands, applications);
242256

243257
switch (requestSource) {

0 commit comments

Comments
 (0)