Skip to content

Commit

Permalink
fileperms: fix login when running elevated with jagex launcher
Browse files Browse the repository at this point in the history
  • Loading branch information
YvesW committed Mar 11, 2024
1 parent a4934fb commit 63db530
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/main/java/net/runelite/launcher/FilePermissionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2024, YvesW <https://github.com/YvesW>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.launcher;

import java.awt.Color;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import lombok.extern.slf4j.Slf4j;
import static net.runelite.launcher.Launcher.nativesLoaded;

@Slf4j
public class FilePermissionManager
{
private static final Color DARKER_GRAY_COLOR = new Color(30, 30, 30);

static void fixJagexLauncherLogin()
{
if (!nativesLoaded)
{
log.debug("Launcher natives were not loaded. Skipping Jagex Launcher login check.");
return;
}

if (isNotJagexLauncherChild() || !isRunningElevated() || isJagexLauncherElevated())
{
// not problematic if not running without the Jagex Launcher, if not running elevated,
// or if both the Jagex Launcher and RL Launcher are running with elevated permissions
return;
}

log.info("RuneLite is running with elevated permissions, while the Jagex Launcher is not.");
log.info("Deleting compatibility registry keys.");
deleteRegCompatKey("HKLM"); // all users
deleteRegCompatKey("HKCU"); // current user

okOptionPaneCompat();
}

private static boolean isNotJagexLauncherChild()
{
// alternatively get the children or descendants of JagexLauncher.exe
ProcessHandle parent = ProcessHandle.current().parent().orElse(null);
if (parent != null)
{
return !parent.info().command().orElse("-").contains("JagexLauncher.exe");
}
return true;
}

private static boolean isRunningElevated()
{
return isRunningElevated(ProcessHandle.current().pid());
}

private static native boolean isRunningElevated(long pid);

private static boolean isJagexLauncherElevated()
{
if (isNotJagexLauncherChild())
{
return false;
}

ProcessHandle parent = ProcessHandle.current().parent().orElse(null);
if (parent != null)
{
boolean result = isRunningElevated(parent.pid());
log.info("Jagex Launcher is running with elevated permissions: " + result);
return result;
}
return false;
}

private static void deleteRegCompatKey(String rootKey)
{
String subKey = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers";
ProcessHandle.current().info().command().ifPresent(value -> regDeleteValue(rootKey, subKey, value));
}

// Requires elevated permissions. Current valid inputs for key are: "HKCU" and "HKLM"
private static native void regDeleteValue(String key, String subKey, String value);

private static void okOptionPaneCompat()
{
try (var in = FilePermissionManager.class.getResourceAsStream("runelite_128.png"))
{
assert in != null;
ImageIcon icon = new ImageIcon(ImageIO.read(in));
icon = new ImageIcon(icon.getImage().getScaledInstance(64, 64, java.awt.Image.SCALE_SMOOTH));
UIManager.put("OptionPane.background", DARKER_GRAY_COLOR);
UIManager.put("Panel.background", DARKER_GRAY_COLOR);
UIManager.put("OptionPane.messageForeground", Color.WHITE);
String title = "RuneLite - Compatibility settings problems detected";
String message = "Running RuneLite with elevated permissions causes login problems when using the Jagex Launcher.\n"
+ "RuneLite has attempted to remedy the problem. Please relaunch RuneLite via the Jagex Launcher.";

String[] options = {"Ok"};
JOptionPane.showOptionDialog(null,
message,
title,
JOptionPane.DEFAULT_OPTION,
JOptionPane.INFORMATION_MESSAGE,
icon,
options,
null);
}
catch (Exception ex)
{
log.debug("Jagex Launcher suggestion: error showing JOptionPane.", ex);
}
System.exit(0);
}
}
4 changes: 4 additions & 0 deletions src/main/java/net/runelite/launcher/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public class Launcher
private static final String USER_AGENT = "RuneLite/" + LauncherProperties.getVersion();
static final String LAUNCHER_EXECUTABLE_NAME_WIN = "RuneLite.exe";
static final String LAUNCHER_EXECUTABLE_NAME_OSX = "RuneLite";
static boolean nativesLoaded;

public static void main(String[] args)
{
Expand Down Expand Up @@ -200,6 +201,8 @@ public static void main(String[] args)
return;
}

FilePermissionManager.fixJagexLauncherLogin();

final Map<String, String> jvmProps = new LinkedHashMap<>();
if (settings.scale != null)
{
Expand Down Expand Up @@ -904,6 +907,7 @@ private static void initDll()
{
System.loadLibrary("launcher_" + arch);
log.debug("Loaded launcher native launcher_{}", arch);
nativesLoaded = true;
}
catch (Error ex)
{
Expand Down

0 comments on commit 63db530

Please sign in to comment.