Skip to content

Commit

Permalink
Merge branch 'QuestCraftPlusPlus:Testing' into Testing
Browse files Browse the repository at this point in the history
  • Loading branch information
thewiilover authored Sep 17, 2024
2 parents 5bcfbe8 + 92a37fb commit 921eae3
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 59 deletions.
6 changes: 1 addition & 5 deletions jre_lwjgl3glfw/src/main/java/org/lwjgl/glfw/GLFW.java
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,7 @@ private Functions() {}
SetWindowHint = apiGetFunctionAddress(GLFW, "pojavSetWindowHint"),
SwapBuffers = apiGetFunctionAddress(GLFW, "pojavSwapBuffers"),
SwapInterval = apiGetFunctionAddress(GLFW, "pojavSwapInterval"),
PumpEvents = apiGetFunctionAddress(GLFW, "pojavPumpEvents"),
StopPumping = apiGetFunctionAddress(GLFW, "pojavStopPumping"),
StartPumping = apiGetFunctionAddress(GLFW, "pojavStartPumping");
PumpEvents = apiGetFunctionAddress(GLFW, "pojavPumpEvents");
}

public static SharedLibrary getLibrary() {
Expand Down Expand Up @@ -1083,9 +1081,7 @@ public static void glfwPollEvents() {
// Prevent these with this code.
if(mGLFWInputPumping) return;
mGLFWInputPumping = true;
callV(Functions.StartPumping);
for (Long ptr : mGLFWWindowMap.keySet()) callJV(ptr, Functions.PumpEvents);
callV(Functions.StopPumping);
mGLFWInputPumping = false;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public static void login(Activity activity)
ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities capabilities = connManager.getNetworkCapabilities(connManager.getActiveNetwork());

boolean hasWifi = false;
boolean hasWifi = true;

if(capabilities != null) {
hasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/InstanceHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public static MinecraftInstances.Instance create(Activity activity, MinecraftIns
String clientClasspath = Installer.installClient(minecraftVersionInfo, gameDir);
String minecraftClasspath = Installer.installLibraries(minecraftVersionInfo, gameDir);
String modLoaderClasspath = Installer.installLibraries(finalModLoaderVersionInfo, gameDir);
String lwjgl = Installer.installLwjgl(activity);
String lwjgl = UnityPlayerActivity.installLWJGL(activity);

instance.classpath = clientClasspath + File.pathSeparator + minecraftClasspath + File.pathSeparator + modLoaderClasspath + File.pathSeparator + lwjgl;

Expand Down
27 changes: 23 additions & 4 deletions src/main/java/pojlib/UnityPlayerActivity.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pojlib;

import static android.os.Build.VERSION.SDK_INT;

import static org.lwjgl.glfw.CallbackBridge.sendKeyPress;
import static org.lwjgl.glfw.CallbackBridge.sendMouseButton;

Expand All @@ -19,13 +18,16 @@
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;

import com.unity3d.player.IUnityPlayerLifecycleEvents;
import com.unity3d.player.UnityPlayer;

import org.lwjgl.glfw.CallbackBridge;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

import fr.spse.gamepad_remapper.RemapperManager;
import fr.spse.gamepad_remapper.RemapperView;
Expand All @@ -35,8 +37,8 @@
import pojlib.input.LwjglGlfwKeycode;
import pojlib.input.gamepad.DefaultDataProvider;
import pojlib.input.gamepad.Gamepad;
import pojlib.util.Constants;
import pojlib.util.FileUtil;
import pojlib.util.Logger;

public class UnityPlayerActivity extends ActivityGroup implements IUnityPlayerLifecycleEvents, GrabListener
{
Expand Down Expand Up @@ -79,9 +81,11 @@ protected String updateUnityCommandLineArguments(String cmdLine)

mUnityPlayer = new UnityPlayer(this, this);
setContentView(mUnityPlayer);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
mUnityPlayer.requestFocus();
File zip = new File(this.getFilesDir() + "/runtimes/JRE-22");
if (!zip.exists()) {

File jre = new File(this.getFilesDir() + "/runtimes/JRE-22");
if (!jre.exists()) {
FileUtil.unzipArchiveFromAsset(this, "JRE-22.zip", this.getFilesDir() + "/runtimes/JRE-22");
}

Expand All @@ -107,6 +111,21 @@ protected String updateUnityCommandLineArguments(String cmdLine)
CallbackBridge.nativeSetUseInputStackQueue(true);
}

public static String installLWJGL(Activity activity) throws IOException {
File lwjgl = new File(Constants.USER_HOME + "/lwjgl3/lwjgl-glfw-classes.jar");
byte[] lwjglAsset = FileUtil.loadFromAssetToByte(activity, "lwjgl/lwjgl-glfw-classes.jar");

if (!lwjgl.exists()) {
Objects.requireNonNull(lwjgl.getParentFile()).mkdirs();
FileUtil.write(lwjgl.getAbsolutePath(), lwjglAsset);
} else if (!FileUtil.matchingAssetFile(lwjgl, lwjglAsset)) {
Objects.requireNonNull(lwjgl.getParentFile()).mkdirs();
FileUtil.write(lwjgl.getAbsolutePath(), lwjglAsset);
}

return lwjgl.getAbsolutePath();
}

public static DisplayMetrics getDisplayMetrics(Activity activity) {
DisplayMetrics displayMetrics = new DisplayMetrics();

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/pojlib/account/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,14 @@ public static void beginLogin(Activity activity) {
while(res.account() == null);
try {
API.currentAcc = MinecraftAccount.login(activity.getFilesDir() + "/accounts", new String[]{res.accessToken(), String.valueOf(res.expiresOnDate().getTime())});
} catch (IOException | JSONException e) {
e.printStackTrace();
} catch (IOException | JSONException | MSAException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
}
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
} catch (ExecutionException | InterruptedException e) {
throw new MSAException("MicrosoftLogin | Something went wrong! Couldn't reach the Microsoft Auth servers.", e);
Logger.getInstance().appendToLog("MicrosoftLogin | Something went wrong! Couldn't reach the Microsoft Auth servers.");
API.msaMessage = "MicrosoftLogin | Something went wrong! Couldn't reach the Microsoft Auth servers.";
}
});

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import pojlib.util.Constants;
import pojlib.util.GsonUtils;
import pojlib.util.Logger;
import pojlib.util.MSAException;

public class MinecraftAccount {
public String accessToken;
Expand All @@ -24,9 +25,9 @@ public class MinecraftAccount {
public final String userType = "msa";


public static MinecraftAccount login(String gameDir, String[] response) throws IOException, JSONException {
public static MinecraftAccount login(String gameDir, String[] response) throws MSAException, IOException, JSONException {
String mcToken = Msa.acquireXBLToken(response[0]);
Msa instance = new Msa(false, mcToken);
Msa instance = new Msa();
MinecraftAccount account = instance.performLogin(mcToken);
account.expiresIn = Long.parseLong(response[1]);

Expand Down Expand Up @@ -54,7 +55,7 @@ public static MinecraftAccount load(String path, @Nullable String newToken, @Nul
}
GsonUtils.objectToJsonFile(path + "/account.json", acc);
return acc;
} catch (IOException | JSONException e) {
} catch (IOException | JSONException | MSAException e) {
Logger.getInstance().appendToLog("Unable to load account! | " + e);
return null;
}
Expand Down
42 changes: 16 additions & 26 deletions src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@


public class Msa {

private final boolean mIsRefresh;
private final String mAuthCode;
private static final Map<Long, String> XSTS_ERRORS;
static {
XSTS_ERRORS = new ArrayMap<>();
Expand All @@ -46,26 +43,20 @@ public class Msa {
}

/* Fields used to fill the account */
public String msRefreshToken;
public static String mcName;
public String mcToken;
public static String mcUuid;
public static boolean doesOwnGame;
public long expiresAt;

public Msa(boolean isRefresh, String authCode){
mIsRefresh = isRefresh;
mAuthCode = authCode;
}

/** Performs a full login, calling back listeners appropriately */
public MinecraftAccount performLogin(String xblToken) {
public MinecraftAccount performLogin(String xblToken) throws MSAException {
try {
String[] xsts = acquireXsts(xblToken);
if(xsts == null) {
return null;
}
String mcToken = acquireMinecraftToken(xsts[0], xsts[1]);
acquireMinecraftToken(xsts[0], xsts[1]);
fetchOwnedItems(mcToken);
checkMcProfile(mcToken);

Expand All @@ -78,17 +69,17 @@ public MinecraftAccount performLogin(String xblToken) {
acc.expiresIn = expiresAt;
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | Unknown Error occurred.");
throw new MSAException("MicrosoftLogin | Unknown Error occurred.", null);
throw new MSAException("MicrosoftLogin | Unknown Error occurred.");
}

return acc;
} catch (Exception e) {
Logger.getInstance().appendToLog("MicrosoftLogin | Exception thrown during authentication " + e);
throw new MSAException("MicrosoftLogin | Exception thrown during authentication ", e);
throw new MSAException("MicrosoftLogin | Exception thrown during authentication ");
}
}

static String acquireXBLToken(String accessToken) throws IOException, JSONException {
static String acquireXBLToken(String accessToken) throws IOException, MSAException, JSONException {
URL url = new URL(Constants.XBL_AUTH_URL);

JSONObject data = new JSONObject();
Expand Down Expand Up @@ -118,7 +109,7 @@ static String acquireXBLToken(String accessToken) throws IOException, JSONExcept
}

/** @return [uhs, token]*/
private String[] acquireXsts(String xblToken) throws IOException, JSONException {
private String[] acquireXsts(String xblToken) throws IOException, JSONException, MSAException {
URL url = new URL(Constants.XSTS_AUTH_URL);

JSONObject data = new JSONObject();
Expand Down Expand Up @@ -153,16 +144,16 @@ private String[] acquireXsts(String xblToken) throws IOException, JSONException
String locale_id = XSTS_ERRORS.get(xerr);
if(locale_id != null) {
Logger.getInstance().appendToLog(responseContents);
throw new MSAException(responseContents, null);
throw new MSAException(responseContents);
}
// Logger.getInstance().appendToLog("Unknown error returned from Xbox Live\n" + responseContents);
throw new MSAException("Unknown error returned from Xbox Live", null);
throw new MSAException("Unknown error returned from Xbox Live");
} else{
throw getResponseThrowable(conn);
}
}

private String acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, JSONException {
private void acquireMinecraftToken(String xblUhs, String xblXsts) throws IOException, MSAException, JSONException {
URL url = new URL(Constants.MC_LOGIN_URL);

JSONObject data = new JSONObject();
Expand All @@ -178,17 +169,16 @@ private String acquireMinecraftToken(String xblUhs, String xblXsts) throws IOExc
}

if(conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) {
expiresAt = System.currentTimeMillis() + 86400000;
JSONObject jo = new JSONObject(FileUtil.read(conn.getInputStream()));
conn.disconnect();
expiresAt = System.currentTimeMillis() + (jo.getInt("expires_in") * 1000L);
mcToken = jo.getString("access_token");
return jo.getString("access_token");
}else{
throw getResponseThrowable(conn);
}
}

private void fetchOwnedItems(String mcAccessToken) throws IOException {
private void fetchOwnedItems(String mcAccessToken) throws MSAException, IOException {
URL url = new URL(Constants.MC_STORE_URL);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Expand All @@ -204,7 +194,7 @@ private void fetchOwnedItems(String mcAccessToken) throws IOException {
}

// Returns false for failure //
public static boolean checkMcProfile(String mcAccessToken) throws IOException, JSONException {
public static boolean checkMcProfile(String mcAccessToken) throws IOException, MSAException, JSONException {
URL url = new URL(Constants.MC_PROFILE_URL);

HttpURLConnection conn = (HttpURLConnection)url.openConnection();
Expand All @@ -231,7 +221,7 @@ public static boolean checkMcProfile(String mcAccessToken) throws IOException, J
} else {
Logger.getInstance().appendToLog("MicrosoftLogin | It seems that this Microsoft Account does not own the game.");
doesOwnGame = false;
throw new MSAException("It seems like this account does not have a Minecraft profile. If you have Xbox Game Pass, please log in on https://minecraft.net/ and set it up.", null);
throw new MSAException("It seems like this account does not have a Minecraft profile. If you have Xbox Game Pass, please log in on https://minecraft.net/ and set it up.");
}
}

Expand Down Expand Up @@ -266,11 +256,11 @@ private static String convertToFormData(String... data) throws UnsupportedEncodi
return builder.toString();
}

private static RuntimeException getResponseThrowable(HttpURLConnection conn) throws IOException {
private static MSAException getResponseThrowable(HttpURLConnection conn) throws IOException, MSAException {
Logger.getInstance().appendToLog("MicrosoftLogin | Error code: " + conn.getResponseCode() + ": " + conn.getResponseMessage());
if(conn.getResponseCode() == 429) {
throw new MSAException("Too many requests, please try again later.", null);
throw new MSAException("Too many requests, please try again later.");
}
throw new MSAException(conn.getResponseMessage(), null);
throw new MSAException(conn.getResponseMessage());
}
}
9 changes: 0 additions & 9 deletions src/main/java/pojlib/install/Installer.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,6 @@ public AsyncDownload( Map.Entry<String, JsonElement> entry, VersionInfo versionI
}
}

public static String installLwjgl(Activity activity) throws IOException {
File lwjgl = new File(Constants.USER_HOME + "/lwjgl3/lwjgl-glfw-classes.jar");
if (!lwjgl.exists()) {
Objects.requireNonNull(lwjgl.getParentFile()).mkdirs();
FileUtil.write(lwjgl.getAbsolutePath(), FileUtil.loadFromAssetToByte(activity, "lwjgl/lwjgl-glfw-classes.jar"));
}
return lwjgl.getAbsolutePath();
}

//Used for mod libraries, vanilla is handled a different (tbh better) way
private static String parseLibraryNameToPath(String libraryName) {
String[] parts = libraryName.split(":");
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/pojlib/util/FileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public static byte[] loadFromAssetToByte(Context ctx, String inFile) {
return buffer;
}

public static boolean matchingAssetFile(File sourceFile, byte[] assetFile) throws IOException {
byte[] sf = Files.readAllBytes(sourceFile.toPath());
return sf == assetFile;
}

public static String read(String path) throws IOException {
return read(Files.newInputStream(Paths.get(path)));
}
Expand All @@ -55,6 +60,9 @@ public static void write(String path, byte[] content) throws IOException
{
File outPath = new File(path);
Objects.requireNonNull(outPath.getParentFile()).mkdirs();
if(!outPath.exists()) {
outPath.createNewFile();
}

BufferedOutputStream fos = new BufferedOutputStream(Files.newOutputStream(outPath.toPath()));
fos.write(content, 0, content.length);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/pojlib/util/MSAException.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package pojlib.util;

import androidx.annotation.Nullable;
import pojlib.API;

public class MSAException extends RuntimeException {
public MSAException(String msaMessage, @Nullable Throwable cause) {
super(msaMessage, cause);
public class MSAException extends Exception {
public MSAException(String msaMessage) {
API.msaMessage = msaMessage;
}
}
2 changes: 1 addition & 1 deletion src/main/java/pojlib/util/json/MinecraftInstances.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static class Instance {

public List<String> generateLaunchArgs(MinecraftAccount account) {
String[] mcArgs = {"--username", account.username, "--version", versionName, "--gameDir", gameDir,
"--assetsDir", assetsDir, "--assetIndex", assetIndex, "--uuid", account.uuid.replaceAll("-", ""),
"--assetsDir", assetsDir, "--assetIndex", assetIndex, "--uuid", account.uuid,
"--accessToken", account.accessToken, "--userType", account.userType, "--versionType", "release"};

List<String> allArgs = new ArrayList<>(Arrays.asList("-cp", classpath));
Expand Down
4 changes: 2 additions & 2 deletions src/main/jniLibs/arm64-v8a/libtinywrapper.so
Git LFS file not shown

0 comments on commit 921eae3

Please sign in to comment.