Skip to content

Commit

Permalink
Add Multi-User Account System
Browse files Browse the repository at this point in the history
  • Loading branch information
CADIndie committed Nov 3, 2024
1 parent 6db6575 commit b0ce764
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/main/assets/lwjgl/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1728465869857
1730624021613
29 changes: 14 additions & 15 deletions src/main/java/pojlib/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;

import androidx.annotation.Nullable;

import com.google.gson.JsonObject;

import pojlib.account.MinecraftAccount;
Expand Down Expand Up @@ -35,6 +37,7 @@ public class API {
public static String currentDownload;
public static String profileImage;
public static String profileName;
public static String profileUUID;
public static String memoryValue = "1800";
public static boolean developerMods;
public static MinecraftAccount currentAcc;
Expand Down Expand Up @@ -130,14 +133,7 @@ public static boolean deleteInstance(MinecraftInstances instances, MinecraftInst
* @throws IOException Throws if download of library or asset fails
*/
public static MinecraftInstances.Instance createNewInstance(Activity activity, MinecraftInstances instances, String instanceName, boolean useDefaultMods, String minecraftVersion, String imageURL) throws IOException {

if(ignoreInstanceName) {
return InstanceHandler.create(activity, instances, instanceName, Constants.USER_HOME, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, imageURL);
} else if (instanceName.contains("/") || instanceName.contains("!")) {
throw new IOException("You cannot use special characters (!, /, ., etc) when creating instances.");
} else {
return InstanceHandler.create(activity, instances, instanceName, Constants.USER_HOME, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, imageURL);
}
return InstanceHandler.create(activity, instances, instanceName, Constants.USER_HOME, useDefaultMods, minecraftVersion, InstanceHandler.ModLoader.Fabric, imageURL);
}

/**
Expand Down Expand Up @@ -187,21 +183,22 @@ public static void launchInstance(Activity activity, MinecraftAccount account, M
}

/**
* Logs the user out
* Removes the user account
*
* @param activity The base directory where minecraft should be setup
* @return True if logout was successful
* @param username The username of the profile to remove
* @return True if removal was successful
*/
public static boolean logout(Activity activity) {
return MinecraftAccount.logout(activity);
public static boolean removeAccount(Activity activity, String username) {
return MinecraftAccount.removeAccount(activity, username);
}

/**
* Start the login process
*
* @param activity Android activity object
*/
public static void login(Activity activity)
public static void login(Activity activity, @Nullable String accountName)
{
ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkCapabilities capabilities = connManager.getNetworkCapabilities(connManager.getActiveNetwork());
Expand All @@ -212,19 +209,21 @@ public static void login(Activity activity)
hasWifi = capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI);
}

MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts");
MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts", accountName);
if(acc != null && (acc.expiresOn >= System.currentTimeMillis() || !hasWifi || acc.isDemoMode)) {
currentAcc = acc;
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
API.profileUUID = API.currentAcc.uuid;
return;
} else if(acc != null && acc.expiresOn < System.currentTimeMillis()) {
currentAcc = LoginHelper.refreshAccount(activity);
currentAcc = LoginHelper.refreshAccount(activity, currentAcc.username);
if(currentAcc == null) {
LoginHelper.login(activity);
} else {
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
API.profileUUID = API.currentAcc.uuid;
return;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/pojlib/account/LoginHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class LoginHelper {
SCOPES.add("XboxLive.offline_access");
}

public static MinecraftAccount refreshAccount(Activity activity) {
public static MinecraftAccount refreshAccount(Activity activity, String uuid) {
Set<IAccount> accountsInCache = pca.getAccounts().join();
IAccount account = accountsInCache.iterator().next();

Expand All @@ -76,7 +76,7 @@ public static MinecraftAccount refreshAccount(Activity activity) {

result = pca.acquireTokenSilently(silentParameters).join();
MinecraftAccount acc = new Msa(activity).performLogin(result.accessToken());
GsonUtils.objectToJsonFile(activity.getFilesDir() + "/accounts/account.json", acc);
GsonUtils.objectToJsonFile(activity.getFilesDir() + "/accounts/" + uuid + ".json", acc);
return acc;
} catch (Exception ex) {
Logger.getInstance().appendToLog("Couldn't refresh token! " + ex);
Expand All @@ -102,6 +102,7 @@ public static void login(Activity activity) {
}
API.profileImage = MinecraftAccount.getSkinFaceUrl(API.currentAcc);
API.profileName = API.currentAcc.username;
API.profileUUID = API.currentAcc.uuid;
} catch (ExecutionException | InterruptedException 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
10 changes: 5 additions & 5 deletions src/main/java/pojlib/account/MinecraftAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ public static MinecraftAccount login(Activity activity, String gameDir, String m
Msa instance = new Msa(activity);
MinecraftAccount account = instance.performLogin(msToken);

GsonUtils.objectToJsonFile(gameDir + "/account.json", account);
GsonUtils.objectToJsonFile(gameDir + "/" + account.uuid + ".json", account);
return account;
}

public static boolean logout(Activity activity) {
File accountFile = new File(activity.getFilesDir() + "/accounts/account.json");
public static boolean removeAccount(Activity activity, String uuid) {
File accountFile = new File(activity.getFilesDir() + "/accounts/" + uuid + ".json");
File accountCache = new File(Constants.USER_HOME + "/cache_data");

return accountFile.delete() && accountCache.delete();
}

//Try this before using login - the account will have been saved to disk if previously logged in
public static MinecraftAccount load(String path) {
return GsonUtils.jsonFileToObject(path + "/account.json", MinecraftAccount.class);
public static MinecraftAccount load(String path, String uuid) {
return GsonUtils.jsonFileToObject(path + "/" + uuid + ".json", MinecraftAccount.class);
}

public static String getSkinFaceUrl(MinecraftAccount account) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/account/Msa.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public MinecraftAccount performLogin(String msToken) throws MSAException {
fetchOwnedItems(mcToken);
checkMcProfile(mcToken);

MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts");
MinecraftAccount acc = MinecraftAccount.load(activity.getFilesDir() + "/accounts", mcName);
if (acc == null) acc = new MinecraftAccount();
if (doesOwnGame) {
acc.accessToken = mcToken;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pojlib/util/DownloadUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ private static void download(URL url, OutputStream os) throws IOException {
public static void downloadFile(String url, File out) throws IOException {
Objects.requireNonNull(out.getParentFile()).mkdirs();
File tempOut = File.createTempFile(out.getName(), ".part", out.getParentFile());
BufferedOutputStream bos = null;
try {
OutputStream bos2 = new BufferedOutputStream(Files.newOutputStream(tempOut.toPath()));
try {
Expand All @@ -76,6 +75,7 @@ public static void downloadFile(String url, File out) throws IOException {
throw th3;
}
}

public static boolean compareSHA1(File f, @Nullable String sourceSHA) {
try {
String sha1_dst;
Expand Down

0 comments on commit b0ce764

Please sign in to comment.