Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic implementation of XDG base directory for alt folder #876

Merged
merged 16 commits into from
Sep 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/main/java/net/wurstclient/WurstClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand All @@ -23,6 +22,7 @@
import net.minecraft.client.resource.language.I18n;
import net.minecraft.client.util.InputUtil;
import net.wurstclient.altmanager.AltManager;
import net.wurstclient.altmanager.Encryption;
import net.wurstclient.analytics.WurstAnalytics;
import net.wurstclient.clickgui.ClickGui;
import net.wurstclient.command.CmdList;
Expand Down Expand Up @@ -145,9 +145,7 @@ public void initialize()
problematicPackDetector.start();

Path altsFile = wurstFolder.resolve("alts.encrypted_json");
Path encFolder =
Paths.get(System.getProperty("user.home"), ".Wurst encryption")
.normalize();
Path encFolder = Encryption.chooseEncryptionFolder();
altManager = new AltManager(altsFile, encFolder);

zoomKey = new KeyBinding("key.wurst.zoom", InputUtil.Type.KEYSYM,
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/net/wurstclient/altmanager/Encryption.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
package net.wurstclient.altmanager;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigInteger;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
Expand All @@ -39,6 +41,7 @@
import net.minecraft.util.Util;
import net.minecraft.util.crash.CrashException;
import net.minecraft.util.crash.CrashReport;
import net.minecraft.util.crash.CrashReportSection;
import net.wurstclient.util.json.JsonException;
import net.wurstclient.util.json.JsonUtils;
import net.wurstclient.util.json.WsonArray;
Expand Down Expand Up @@ -99,6 +102,57 @@ private Path createEncryptionFolder(Path encFolder) throws IOException
return encFolder;
}

public static Path chooseEncryptionFolder()
{
String userHome = System.getProperty("user.home");
String xdgDataHome = System.getenv("XDG_DATA_HOME");
String encFolderName = ".Wurst encryption";

Path homeEncFolder = Paths.get(userHome, encFolderName).normalize();
Path encFolder = homeEncFolder;
if(xdgDataHome != null && !xdgDataHome.isEmpty())
{
encFolder = Paths.get(xdgDataHome, encFolderName).normalize();

if(!Files.exists(encFolder) && Files.isDirectory(homeEncFolder))
migrateEncryptionFolder(homeEncFolder, encFolder);
}

return encFolder;
}
Alexander01998 marked this conversation as resolved.
Show resolved Hide resolved

public static void migrateEncryptionFolder(Path oldFolder, Path newFolder)
{
System.out.println("Migrating encryption folder from " + oldFolder
+ " to " + newFolder);

try
{
Files.createDirectories(newFolder);

File[] oldFiles = oldFolder.toFile().listFiles();
for(File oldFile : oldFiles)
{
Path fileDestination = newFolder.resolve(oldFile.getName());
Files.copy(oldFile.toPath(), fileDestination);
}

for(File oldFile : oldFiles)
oldFile.delete();

Files.deleteIfExists(oldFolder);

}catch(IOException e)
{
CrashReport report =
CrashReport.create(e, "Migrating Wurst encryption folder");
CrashReportSection section = report.addElement("Migration");
section.add("Old path", oldFolder);
section.add("New path", newFolder);
throw new CrashException(report);
}
Alexander01998 marked this conversation as resolved.
Show resolved Hide resolved
}

public byte[] decrypt(byte[] bytes)
{
try
Expand Down