14 changed files with 466 additions and 24 deletions
-
2.gitignore
-
3build.gradle
-
69src/main/java/com/emzi0767/discord/InjectorInstaller.java
-
31src/main/java/com/emzi0767/discord/Main.java
-
57src/main/java/com/emzi0767/discord/Util.java
-
17src/main/java/com/emzi0767/discord/envvars/EnvironmentVariableDriver.java
-
20src/main/java/com/emzi0767/discord/envvars/IEnvironmentVariableDriver.java
-
67src/main/java/com/emzi0767/discord/envvars/UnixEnvironmentVariableDriver.java
-
66src/main/java/com/emzi0767/discord/envvars/WindowsEnvironmentVariableDriver.java
-
24src/main/java/com/emzi0767/discord/injector/IInjectorLocationDriver.java
-
63src/main/java/com/emzi0767/discord/injector/Injector.java
-
13src/main/java/com/emzi0767/discord/injector/InjectorLocationDriver.java
-
27src/main/java/com/emzi0767/discord/injector/UnixInjectorLocationDriver.java
-
31src/main/java/com/emzi0767/discord/injector/WindowsInjectorLocationDriver.java
@ -1,3 +1,3 @@ |
|||
/src/main/resources/injector/index.js |
|||
/src/main/resources/injector/discord-css-injector.js |
|||
/.gradle/ |
|||
/build/ |
@ -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"); |
|||
} |
|||
} |
@ -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
File diff suppressed because it is too large
View File
@ -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(); |
|||
} |
|||
} |
@ -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(); |
|||
} |
@ -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()); |
|||
} |
|||
} |
@ -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(); |
|||
} |
|||
} |
@ -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")); |
|||
} |
|||
} |
@ -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"); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue