Skip to content

Commit

Permalink
GPU name selection, default config creation
Browse files Browse the repository at this point in the history
  • Loading branch information
imAETHER committed Nov 15, 2022
1 parent 374a3b7 commit befbf99
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 24 deletions.
21 changes: 8 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
# <p align="center">PC Stats<br><img src="https://aetherclient.com/uploads/imf44/2a8cfc720a.png"></p>

# 📄 Config
You need to place `config.json` in the same directory as the jar.<br>
This is an example config, you can use it if you want:
```
{
"appId": "995501904685715476",
"smallImage": "https://aetherclient.com/images/gato.png",
"largeImage": "https://c.tenor.com/hEwfEcj2R60AAAAd/laptop-smoking.gif",
"showPCActiveTime": false
}
```

# 🟢 Usage
Either build it yourself or go to [releases](https://github.com/imAETHER/PCStats/releases) and download it.

This project uses OSHI to get processor temperatures, OSHI uses [OpenHardwareMonitor](https://github.com/openhardwaremonitor/openhardwaremonitor) to get these, download and run it in the background while you use the RPC to get accurate readings.<br>
This project uses OSHI to get processor temperatures, OSHI uses [OpenHardwareMonitor](https://openhardwaremonitor.org/) to get these, download and run it in the background while you use the RPC to get accurate readings.<br>

- Place the jar in a folder
- Open CMD/Windows Terminal(looks better) and execute the jar with the command:
`java -jar PCStats.jar`
- It will automatically create config.json in the current directory and will ask you which gpu you want to use **(for the display name only)**
- Will auto save the config when you close the program by doing CTRL + C
- You can manually edit the config to change selected gpu, show uptime and change the RPC images/gifs

# 💻 Supported platforms
All platforms since its java & its using OSHI (haven't tested tho), for now GPU usage and temperature only works with NVIDIA gpus that have nvidia-smi.
All platforms since its java & its using OSHI (haven't tested tho), for now GPU usage and temperature **only works with NVIDIA gpus that have nvidia-smi**.
7 changes: 7 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"appId": "995501904685715476",
"smallImage": "https://aetherclient.com/images/gato.png",
"largeImage": "https://c.tenor.com/hEwfEcj2R60AAAAd/laptop-smoking.gif",
"showPCActiveTime": false,
"gpuId": 0
}
9 changes: 4 additions & 5 deletions src/main/java/im/aether/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
*/
public class Config {

public String appId = "";
public String smallImage = "";
public String largeImage = "";

public String appId = "995501904685715476";
public String smallImage = "https://aetherclient.com/images/gato.png";
public String largeImage = "https://c.tenor.com/hEwfEcj2R60AAAAd/laptop-smoking.gif";
public boolean showPCActiveTime;

public int gpuId = -1;
}
48 changes: 42 additions & 6 deletions src/main/java/im/aether/Main.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package im.aether;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import de.jcm.discordgamesdk.Core;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.GraphicsCard;
import oshi.hardware.Sensors;
import oshi.util.FileUtil;
import oshi.util.GlobalConfig;
import oshi.util.tuples.Pair;

import javax.swing.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Logger;

/**
Expand All @@ -27,25 +34,31 @@ public class Main {
public static void main(String[] args) throws Exception {
Logger.getGlobal().info("Checking for config file...");

final Gson gson = new GsonBuilder().setPrettyPrinting().create();

Config config;
if (!CONFIG.exists()) {
if (CONFIG.createNewFile()) Logger.getGlobal().info("Created config file.");
else {
Logger.getGlobal().severe("Failed to create config file. please create it and re-launch.");
return;
}
final String serialized = gson.toJson(config = new Config());
Files.write(CONFIG.toPath(), serialized.getBytes(StandardCharsets.UTF_8), StandardOpenOption.WRITE);
Logger.getGlobal().info("Default config created and loaded.");
} else {
final String cfgData = Files.readAllLines(CONFIG.toPath(), StandardCharsets.UTF_8).stream().reduce("", (a, b) -> a + b);
config = gson.fromJson(cfgData, Config.class);
Logger.getGlobal().info("Existing config loaded from file.");
}
final String cfgData = Files.readAllLines(CONFIG.toPath(), StandardCharsets.UTF_8).stream().reduce("", (a, b) -> a + b);

final Gson gson = new Gson();
final Config config = gson.fromJson(cfgData, Config.class);

if (config == null) {
Logger.getGlobal().severe("Failed to parse config file. Please check it and re-launch.");
Logger.getGlobal().severe("An example config can be found in the README.");
return;
}

Logger.getGlobal().info("Config loaded. Downloading libs...");
Logger.getGlobal().info("Downloading RPC lib...");

// temp(libs) and tempDir(lib directory), this is so Core doesn't create more temp dirs and just uses the given one
final Pair<File, File> discordLib = DiscordRPC.downloadDiscordLibrary();
Expand All @@ -64,6 +77,20 @@ public static void main(String[] args) throws Exception {
Logger.getGlobal().warning("or you may get inaccurate readings. You can also try running me as admin.");
}

final List<GraphicsCard> gpus = SYSTEM_INFO.getHardware().getGraphicsCards();
if (gpus.size() > 0) {
if (config.gpuId == -1) {
Logger.getGlobal().info("<!> Multiple GPUs detected, select one <!>");
for (int i = 0; i < gpus.size(); i++) {
Logger.getGlobal().info(String.format("[%s] %s", i, gpus.get(i).getName()));
}
System.out.print("[GPU]> ");
config.gpuId = new Scanner(System.in).nextInt();

Logger.getGlobal().info("Selected GPU n°" + config.gpuId + ". To change this edit the config.");
}
} else config.gpuId = 0;

DiscordRPC.initActivity(config.appId, config.showPCActiveTime);
DiscordRPC.updateImages(config.largeImage, config.smallImage);

Expand All @@ -83,7 +110,7 @@ public static void main(String[] args) throws Exception {
DiscordRPC.update(
"CPU Usage: " + cpuUsage + " % | Temp: " + cpuTemp + "°C",
"GPU Usage: " + gpuInfoNvidia.getA() + " | Temp: " + gpuInfoNvidia.getB() + "°C",
SYSTEM_INFO.getHardware().getGraphicsCards().get(0).getName(), //gpu name
gpus.get(config.gpuId).getName(), //gpu name
processor.getProcessorIdentifier().getName() //cpu name
);

Expand All @@ -93,6 +120,15 @@ public static void main(String[] args) throws Exception {
s.start();

Runtime.getRuntime().addShutdownHook(new Thread(() -> {
// Save config
final String serialized = gson.toJson(config);
try {
Files.write(CONFIG.toPath(), serialized.getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
Logger.getGlobal().severe("Failed to save the config on exit!");
}

s.stop();
DiscordRPC.dispose();
}));
Expand Down

0 comments on commit befbf99

Please sign in to comment.