Browse Source

Initial working version.

master v1.0.0
Mateusz Brawański 4 years ago
parent
commit
ef099d9ee4
Signed by: Emzi0767 GPG Key ID: 246AB92A3C22030A
  1. 2
      .gitignore
  2. 3
      build.gradle
  3. 69
      src/main/java/com/emzi0767/discord/InjectorInstaller.java
  4. 31
      src/main/java/com/emzi0767/discord/Main.java
  5. 57
      src/main/java/com/emzi0767/discord/Util.java
  6. 17
      src/main/java/com/emzi0767/discord/envvars/EnvironmentVariableDriver.java
  7. 20
      src/main/java/com/emzi0767/discord/envvars/IEnvironmentVariableDriver.java
  8. 67
      src/main/java/com/emzi0767/discord/envvars/UnixEnvironmentVariableDriver.java
  9. 66
      src/main/java/com/emzi0767/discord/envvars/WindowsEnvironmentVariableDriver.java
  10. 24
      src/main/java/com/emzi0767/discord/injector/IInjectorLocationDriver.java
  11. 63
      src/main/java/com/emzi0767/discord/injector/Injector.java
  12. 13
      src/main/java/com/emzi0767/discord/injector/InjectorLocationDriver.java
  13. 27
      src/main/java/com/emzi0767/discord/injector/UnixInjectorLocationDriver.java
  14. 31
      src/main/java/com/emzi0767/discord/injector/WindowsInjectorLocationDriver.java

2
.gitignore

@ -1,3 +1,3 @@
/src/main/resources/injector/index.js
/src/main/resources/injector/discord-css-injector.js
/.gradle/
/build/

3
build.gradle

@ -52,6 +52,9 @@ task copyInjector(type: Copy, dependsOn: buildInjector) {
from "ref/css-injector/dist/index.js"
into "src/main/resources/injector"
rename { filename ->
filename.replace "index", "discord-css-injector"
}
}
buildInjector.finalizedBy copyInjector

69
src/main/java/com/emzi0767/discord/InjectorInstaller.java

@ -1,19 +1,74 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord;
import com.sun.istack.internal.NotNull;
import com.emzi0767.discord.envvars.EnvironmentVariableDriver;
import com.emzi0767.discord.envvars.IEnvironmentVariableDriver;
import com.emzi0767.discord.injector.Injector;
import java.io.File;
import java.io.*;
import java.util.HashMap;
public class InjectorInstaller {
public InjectorInstaller() {
class InjectorInstaller {
}
void install(File cssFile) throws IOException, InterruptedException {
System.out.println("Setting up CSS file");
if (!cssFile.exists()) {
System.out.println("CSS file did not exist, creating");
cssFile.createNewFile();
try (FileWriter fs = new FileWriter(cssFile)) {
try (BufferedWriter bw = new BufferedWriter(fs)) {
try (PrintWriter pw = new PrintWriter(bw)) {
pw.println("/* Put your CSS rules here */");
pw.println();
}
}
}
}
System.out.println("Installing injector");
Injector injector = new Injector();
injector.install();
System.out.println("Setting up environment variables");
HashMap<String, String> envvarKvps = new HashMap<>();
envvarKvps.put(Util.EnvironmentDiscordCssFile, cssFile.getAbsolutePath());
envvarKvps.put(Util.EnvironmentNodeOptions, "--require \"".concat(injector.getLocation().getAbsolutePath()).concat("\""));
public void install(@NotNull File cssFile) {
System.out.println("Setting environment variables");
IEnvironmentVariableDriver envvars = EnvironmentVariableDriver.getDriver();
envvars.configureVariables(envvarKvps);
System.out.println("Restarting Discord");
System.out.println("All done");
}
public void uninstall() {
void uninstall() throws IOException, InterruptedException {
System.out.println("Removing injector");
Injector injector = new Injector();
injector.uninstall();
System.out.println("Tearing down environment variables");
IEnvironmentVariableDriver envvars = EnvironmentVariableDriver.getDriver();
envvars.teardownVariables();
System.out.println("Restarting Discord");
System.out.println("All done");
}
}

31
src/main/java/com/emzi0767/discord/Main.java

@ -1,17 +1,36 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord;
import java.io.File;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Discord CSS Injector Installer by Emzi0767");
if (args.length < 1 || args.length > 2 || args.length == 1 && args[0].toLowerCase().equals("help")) {
printUsage();
}
if (args[0].toLowerCase().equals("install") && args.length <= 2) {
if (args.length == 0) {
InjectorInstaller installer = new InjectorInstaller();
if (!Util.isInstalled())
installer.install(new File(Util.getDefaultDirectory(), "discord.css"));
else
installer.uninstall();
} else if (args[0].toLowerCase().equals("install") && args.length <= 2) {
File css;
if (args.length == 2) {
css = new File(args[1]);
@ -21,7 +40,7 @@ public class Main {
InjectorInstaller installer = new InjectorInstaller();
installer.install(css);
} else if (args[0].toLowerCase().equals("uninstall") && args.length == 1) {
} else if (args.length == 1 && args[0].toLowerCase().equals("uninstall")) {
InjectorInstaller installer = new InjectorInstaller();
installer.uninstall();
} else {

57
src/main/java/com/emzi0767/discord/Util.java

@ -1,12 +1,36 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
public class Util {
public static final String ShellFoldersKey = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
public static final String ShellFoldersValue = "personal";
public static final String EnvironmentKey = "HKCU\\Environment";
public static final String EnvironmentDiscordCssFile = "DISCORD_CSS_FILE";
public static final String EnvironmentNodeOptions = "NODE_OPTIONS";
public static final String EnvironmentDiscordCssNodeOptions = "DISCORD_CSS_NODE_OPTIONS";
private static final String ShellFoldersKey = "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders";
private static final String ShellFoldersValue = "personal";
public static boolean isWindows() {
return System.getProperty("os.name").toLowerCase().contains("win");
@ -46,6 +70,33 @@ public class Util {
in.read(b);
in.close();
return new String(b).split("\\s\\s+")[4];
String sval = new String(b);
if (sval.equals("\r\n\r\n"))
return null;
return sval.split("\\s\\s+")[4];
}
public static void rmDir(File directory) throws IOException {
if (!directory.isDirectory())
return;
Files.walkFileTree(Paths.get(directory.getPath()), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes basicFileAttributes) throws IOException {
Files.delete(path);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path path, IOException e) throws IOException {
Files.delete(path);
return FileVisitResult.CONTINUE;
}
});
}
public static boolean isInstalled() {
return System.getenv(Util.EnvironmentDiscordCssFile) != null && !System.getenv(Util.EnvironmentDiscordCssFile).isEmpty();
}
}

17
src/main/java/com/emzi0767/discord/envvars/EnvironmentVariableDriver.java

@ -1,8 +1,25 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord.envvars;
import com.emzi0767.discord.Util;
public class EnvironmentVariableDriver {
public static IEnvironmentVariableDriver getDriver() {
if (Util.isWindows())
return new WindowsEnvironmentVariableDriver();

20
src/main/java/com/emzi0767/discord/envvars/IEnvironmentVariableDriver.java

@ -1,9 +1,27 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord.envvars;
import java.io.IOException;
import java.util.Map;
public interface IEnvironmentVariableDriver {
void configureVariables(Map<String, String> kvs) throws IOException, InterruptedException;
Map<String, String> prepareVariables(Map<String, String> kvs);
void teardownVariables() throws IOException, InterruptedException;
Map<String, String> prepareVariables(Map<String, String> kvs) throws IOException, InterruptedException;
}

67
src/main/java/com/emzi0767/discord/envvars/UnixEnvironmentVariableDriver.java
File diff suppressed because it is too large
View File

66
src/main/java/com/emzi0767/discord/envvars/WindowsEnvironmentVariableDriver.java

@ -1,22 +1,82 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord.envvars;
import com.emzi0767.discord.Util;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class WindowsEnvironmentVariableDriver implements IEnvironmentVariableDriver {
@Override
public void configureVariables(Map<String, String> kvs) throws IOException, InterruptedException {
kvs = this.prepareVariables(kvs);
for (Map.Entry<String, String> kv : kvs.entrySet()) {
this.configureVariable(kv.getKey(), kv.getValue());
}
}
@Override
public Map<String, String> prepareVariables(Map<String, String> kvs) {
return kvs;
public void teardownVariables() throws IOException, InterruptedException {
this.removeVariable(Util.EnvironmentDiscordCssFile);
this.removeVariable(Util.EnvironmentDiscordCssNodeOptions);
String nodeOptions = Util.regQuery(Util.EnvironmentKey, Util.EnvironmentNodeOptions);
if (nodeOptions == null)
return;
nodeOptions = nodeOptions.replace("%".concat(Util.EnvironmentDiscordCssNodeOptions).concat("%"), "").trim();
if (nodeOptions.isEmpty())
this.removeVariable(Util.EnvironmentNodeOptions);
else
this.configureVariable(Util.EnvironmentNodeOptions, nodeOptions.replace("\"", "\"\""));
}
@Override
public Map<String, String> prepareVariables(Map<String, String> kvs) throws IOException, InterruptedException {
HashMap<String, String> kvsNew = new HashMap<>();
for (Map.Entry<String, String> kv : kvs.entrySet()) {
if (kv.getKey().equals(Util.EnvironmentNodeOptions)) {
String existing = Util.regQuery(Util.EnvironmentKey, Util.EnvironmentNodeOptions);
if (existing != null)
existing = "%".concat(Util.EnvironmentDiscordCssNodeOptions).concat("% ").concat(existing);
else
existing = "%".concat(Util.EnvironmentDiscordCssNodeOptions).concat("%");
kvsNew.put(kv.getKey(), existing.replace("\"", "\"\""));
kvsNew.put(Util.EnvironmentDiscordCssNodeOptions, kv.getValue());
} else {
kvsNew.put(kv.getKey(), kv.getValue());
}
}
return kvsNew;
}
private void configureVariable(String name, String value) throws IOException, InterruptedException {
Runtime.getRuntime().exec(String.format("reg add \"HKCU\\Environment\" /v \"%s\" /t \"REG_SZ\" /d \"%s\" /f", name, value)).waitFor();
Runtime.getRuntime().exec(String.format("reg add \"%s\" /v \"%s\" /t \"%s\" /d \"%s\" /f",
Util.EnvironmentKey,
name,
value.contains("%") ? "REG_EXPAND_SZ" : "REG_SZ",
value)).waitFor();
}
private void removeVariable(String name) throws IOException, InterruptedException {
Runtime.getRuntime().exec(String.format("reg delete \"%s\" /v \"%s\" /f", Util.EnvironmentKey, name)).waitFor();
}
}

24
src/main/java/com/emzi0767/discord/injector/IInjectorLocationDriver.java

@ -0,0 +1,24 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord.injector;
import java.io.File;
public interface IInjectorLocationDriver {
File getInstallDirectory();
}

63
src/main/java/com/emzi0767/discord/injector/Injector.java

@ -0,0 +1,63 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord.injector;
import com.emzi0767.discord.Util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Injector {
private static final String InjectorFileName = "discord-css-injector.js";
private IInjectorLocationDriver location;
public Injector() {
this.location = InjectorLocationDriver.getDriver();
}
public File getLocation() {
return new File(this.location.getInstallDirectory(), InjectorFileName);
}
public void install() throws IOException {
File instDir = this.location.getInstallDirectory();
if (!instDir.isDirectory())
instDir.mkdirs();
File injectorJs = this.getLocation();
if (injectorJs.exists())
injectorJs.delete();
try (InputStream is = this.getClass().getResourceAsStream("/injector/".concat(InjectorFileName))) {
try (FileOutputStream fs = new FileOutputStream(injectorJs)) {
byte[] buff = new byte[32768];
int br = 0;
while ((br = is.read(buff, 0, buff.length)) != -1) {
fs.write(buff, 0, br);
}
}
}
}
public void uninstall() throws IOException {
Util.rmDir(this.location.getInstallDirectory());
}
}

13
src/main/java/com/emzi0767/discord/injector/InjectorLocationDriver.java

@ -0,0 +1,13 @@
package com.emzi0767.discord.injector;
import com.emzi0767.discord.Util;
public class InjectorLocationDriver {
public static IInjectorLocationDriver getDriver() {
if (Util.isWindows())
return new WindowsInjectorLocationDriver();
else
return new UnixInjectorLocationDriver();
}
}

27
src/main/java/com/emzi0767/discord/injector/UnixInjectorLocationDriver.java

@ -0,0 +1,27 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord.injector;
import java.io.File;
public class UnixInjectorLocationDriver implements IInjectorLocationDriver {
@Override
public File getInstallDirectory() {
return new File(System.getProperty("user.home", ".discordcss"));
}
}

31
src/main/java/com/emzi0767/discord/injector/WindowsInjectorLocationDriver.java

@ -0,0 +1,31 @@
// This file is a part of Discord CSS Injector Installer project.
//
// Copyright 2019 Emzi0767
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.emzi0767.discord.injector;
import java.io.File;
public class WindowsInjectorLocationDriver implements IInjectorLocationDriver {
@Override
public File getInstallDirectory() {
String location = System.getenv("APPDATA");
if (location == null)
location = System.getProperty("user.home");
return new File(location, "DiscordCSS");
}
}
Loading…
Cancel
Save