[Account loader] A dirty fix, might resolves #534

This commit is contained in:
khanhduytran0 2020-12-24 17:13:20 +07:00
parent 9f7f2fb62d
commit 3231ddce64
3 changed files with 41 additions and 48 deletions

View file

@ -7,6 +7,13 @@ import android.view.*;
import java.io.*; import java.io.*;
import net.kdt.pojavlaunch.authenticator.mojang.*; import net.kdt.pojavlaunch.authenticator.mojang.*;
/**
* This account data format is deprecated.
* The current account data format is JSON on net.kdt.pojavlaunch.value.MinecraftAccount.
* This class remain for account data migrator only.
* Methods for saving/exporting on this format are no longer available.
*/
@Deprecated
public class MCProfile public class MCProfile
{ {
private static String[] emptyBuilder = new String[]{ private static String[] emptyBuilder = new String[]{
@ -17,30 +24,6 @@ public class MCProfile
"Steve" "Steve"
}; };
public static void launch(Activity ctx, Object o) {
PojavProfile.setCurrentProfile(ctx, o);
Intent intent = new Intent(ctx, PojavV2ActivityManager.getLauncherRemakeVer(ctx)); //MCLauncherActivity.class);
ctx.startActivity(intent);
}
public static void updateTokens(final Activity ctx, final String name, RefreshListener listen) throws Exception {
new RefreshTokenTask(ctx, listen).execute(Tools.DIR_ACCOUNT_NEW + "/" + name + ".json");
}
public static String build(MCProfile.Builder builder) {
//System.out.println("build THE VER = " + builder.getVersion());
try {
byte[] bFull = toString(builder).getBytes("UTF-8");
Tools.write(Tools.DIR_ACCOUNT_OLD + "/" + builder.getUsername(), bFull);
} catch (IOException e) {
e.printStackTrace();
}
return Tools.DIR_ACCOUNT_OLD + "/" + builder.getUsername();
}
public static MCProfile.Builder load(String pofFilePath) { public static MCProfile.Builder load(String pofFilePath) {
try { try {
//String th = new String(new byte[]{-128}); //String th = new String(new byte[]{-128});
@ -90,20 +73,6 @@ public class MCProfile
} }
} }
public static String toString(String pofFilePath) {
return toString(load(pofFilePath));
}
public static String toString(MCProfile.Builder builder) {
return
builder.getClientID() + ":" +
builder.getProfileID() + ":" +
builder.getAccessToken() + ":" +
builder.getUsername() + ":" +
builder.getVersion() + ":" +
Boolean.toString(builder.isMojangAccount());
}
public static class Builder implements Serializable public static class Builder implements Serializable
{ {
private String[] fullArgs = new String[6]; private String[] fullArgs = new String[6];

View file

@ -717,10 +717,10 @@ public class PojavLoginActivity extends BaseActivity
new MicrosoftAuthTask(PojavLoginActivity.this, authListener) new MicrosoftAuthTask(PojavLoginActivity.this, authListener)
.execute("true", acc.msaRefreshToken); .execute("true", acc.msaRefreshToken);
} else if (acc.accessToken.length() >= 5) { } else if (acc.accessToken.length() >= 5) {
MCProfile.updateTokens(PojavLoginActivity.this, selectedAccName, authListener); PojavProfile.updateTokens(PojavLoginActivity.this, selectedAccName, authListener);
} else { } else {
di.dismiss(); di.dismiss();
MCProfile.launch(PojavLoginActivity.this, selectedAccName); PojavProfile.launch(PojavLoginActivity.this, selectedAccName);
} }
} catch (Exception e) { } catch (Exception e) {
Tools.showError(PojavLoginActivity.this, e); Tools.showError(PojavLoginActivity.this, e);
@ -826,7 +826,7 @@ public class PojavLoginActivity extends BaseActivity
profileName = mProfile.save(); profileName = mProfile.save();
} }
MCProfile.launch(PojavLoginActivity.this, profileName == null ? mProfile : profileName); PojavProfile.launch(PojavLoginActivity.this, profileName == null ? mProfile : profileName);
} catch (IOException e) { } catch (IOException e) {
Tools.showError(this, e); Tools.showError(this, e);
} }

View file

@ -1,8 +1,14 @@
package net.kdt.pojavlaunch; package net.kdt.pojavlaunch;
import java.io.*; import android.app.Activity;
import android.content.*; import android.content.Context;
import net.kdt.pojavlaunch.value.*; import android.content.Intent;
import com.google.gson.*; import android.content.SharedPreferences;
import com.google.gson.JsonSyntaxException;
import java.io.File;
import java.io.IOException;
import net.kdt.pojavlaunch.authenticator.mojang.RefreshListener;
import net.kdt.pojavlaunch.authenticator.mojang.RefreshTokenTask;
import net.kdt.pojavlaunch.value.MinecraftAccount;
public class PojavProfile public class PojavProfile
{ {
@ -27,7 +33,13 @@ public class PojavProfile
} }
public static String getCurrentProfileName(Context ctx) { public static String getCurrentProfileName(Context ctx) {
return getPrefs(ctx).getString(PROFILE_PREF_FILE, ""); String name = getPrefs(ctx).getString(PROFILE_PREF_FILE, "");
// A dirty fix
if (!name.isEmpty() && name.startsWith(Tools.DIR_ACCOUNT_NEW) && name.endsWith(".json")) {
name = name.substring(0, name.length() - 5).replace(Tools.DIR_ACCOUNT_NEW, "");
setCurrentProfile(ctx, name);
}
return name;
} }
public static boolean setCurrentProfile(Context ctx, Object obj) { public static boolean setCurrentProfile(Context ctx, Object obj) {
@ -57,4 +69,16 @@ public class PojavProfile
public static boolean isFileType(Context ctx) { public static boolean isFileType(Context ctx) {
return new File(Tools.DIR_ACCOUNT_NEW + "/" + PojavProfile.getCurrentProfileName(ctx) + ".json").exists(); return new File(Tools.DIR_ACCOUNT_NEW + "/" + PojavProfile.getCurrentProfileName(ctx) + ".json").exists();
} }
public static void launch(Activity ctx, Object o) {
PojavProfile.setCurrentProfile(ctx, o);
Intent intent = new Intent(ctx, PojavV2ActivityManager.getLauncherRemakeVer(ctx)); //MCLauncherActivity.class);
ctx.startActivity(intent);
}
public static void updateTokens(final Activity ctx, final String name, RefreshListener listen) throws Exception {
new RefreshTokenTask(ctx, listen).execute(Tools.DIR_ACCOUNT_NEW + "/" + name + ".json");
}
} }