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

Added a few new features, and fixed a few bugs. #13

Closed
wants to merge 12 commits into from
Closed
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
target/
build/
out/
classes/

# Maven
dependency-reduced-pom.xml
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Also if any other developers would like to open any PRs with fixes and additions
While this program will copy your resource packs before converting them, we still recommend backing them up, just in case!

## Usage
[Download the compiled jar file](https://github.com/HypixelDev/ResourcePackConverter/releases/latest), or compile the source yourself.
[Download the compiled jar file](https://github.com/agentdid127/ResourcePackConverter/releases/latest), or compile the source yourself.
The program will look for any valid resource packs in the current directory and is easily run by doing this.

java -jar ResourcePackConverter.jar
Expand All @@ -20,5 +20,10 @@ You can set the input directory using one of the following parameters.
`-i <path>`, `--input <path>` or `--input-dir <path>`.

java -jar ResourcePackConverter.jar --input input/

You can also convert to 1.15 using this addon.
`--1.15`.

java -jar ResourcePackConverter.jar --1.15

We hope this helps out!
3 changes: 1 addition & 2 deletions src/main/java/net/hypixel/resourcepack/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ public static void main(String[] args) throws IOException {

new PackConverter(optionSet).run();

System.out.println("Done, press any key to exit!");
System.in.read();

}

}
2 changes: 2 additions & 0 deletions src/main/java/net/hypixel/resourcepack/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class Options {
.withValuesConvertedBy(new PathConverter())
.defaultsTo(Paths.get("./"));

public static final OptionSpec<Void> ONEFIFTEEN = PARSER.accepts("1.15", "Updates to 1.15 version");

public static final OptionSpec<Void> MINIFY = PARSER.accepts("minify", "Minify the json files.");

public static class PathConverter implements ValueConverter<Path> {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/hypixel/resourcepack/PackConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public PackConverter(OptionSet optionSet) {

// this needs to be run first, other converters might reference new directory names
this.registerConverter(new NameConverter(this));

this.registerConverter(new PackMetaConverter(this));
if (this.optionSet.has(Options.ONEFIFTEEN)) this.registerConverter(new PackMetaConverter(this, "1.15"));
else this.registerConverter(new PackMetaConverter(this, "1.13"));

this.registerConverter(new ModelConverter(this));
this.registerConverter(new SpacesConverter(this));
Expand All @@ -40,6 +40,8 @@ public PackConverter(OptionSet optionSet) {
this.registerConverter(new BlockStateConverter(this));
this.registerConverter(new AnimationConverter(this));
this.registerConverter(new MapIconConverter(this));

this.registerConverter(new MCPatcherConverter(this));
}

public void registerConverter(Converter converter) {
Expand Down
150 changes: 150 additions & 0 deletions src/main/java/net/hypixel/resourcepack/extra/PropertiesEx.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package net.hypixel.resourcepack.extra;

import java.io.*;
import java.util.*;

public class PropertiesEx extends Properties {
private static final char[] hexDigit = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

private String saveConvert(String theString, boolean escapeSpace, boolean escapeUnicode) {
int len = theString.length();
int bufLen = len * 2;
if (bufLen < 0) {
bufLen = 2147483647;
}

StringBuilder outBuffer = new StringBuilder(bufLen);

for (int x = 0; x < len; ++x) {
char aChar = theString.charAt(x);
if (aChar > '=' && aChar < 127) {
if (aChar == '\\') {
outBuffer.append('\\');
outBuffer.append('\\');
} else {
outBuffer.append(aChar);
}
} else {
switch (aChar) {
case '\t':
outBuffer.append('\\');
outBuffer.append('t');
continue;
case '\n':
outBuffer.append('\\');
outBuffer.append('n');
continue;
case '\f':
outBuffer.append('\\');
outBuffer.append('f');
continue;
case '\r':
outBuffer.append('\\');
outBuffer.append('r');
continue;
case ' ':
if (x == 0 || escapeSpace) {
outBuffer.append('\\');
}

outBuffer.append(' ');
continue;
case '!':
case '#':
case '=':
outBuffer.append('\\');
outBuffer.append(aChar);
continue;
}

if ((aChar < ' ' || aChar > '~') & escapeUnicode) {
outBuffer.append('\\');
outBuffer.append('u');
outBuffer.append(toHex(aChar >> 12 & 15));
outBuffer.append(toHex(aChar >> 8 & 15));
outBuffer.append(toHex(aChar >> 4 & 15));
outBuffer.append(toHex(aChar & 15));
} else {
outBuffer.append(aChar);
}
}
}

return outBuffer.toString();
}
private static void writeComments(BufferedWriter bw, String comments) throws IOException {
bw.write("#");
int len = comments.length();
int current = 0;
int last = 0;

for(char[] uu = new char[]{'\\', 'u', '\u0000', '\u0000', '\u0000', '\u0000'}; current < len; ++current) {
char c = comments.charAt(current);
if (c > 255 || c == '\n' || c == '\r') {
if (last != current) {
bw.write(comments.substring(last, current));
}

if (c > 255) {
uu[2] = toHex(c >> 12 & 15);
uu[3] = toHex(c >> 8 & 15);
uu[4] = toHex(c >> 4 & 15);
uu[5] = toHex(c & 15);
bw.write(new String(uu));
} else {
bw.newLine();
if (c == '\r' && current != len - 1 && comments.charAt(current + 1) == '\n') {
++current;
}

if (current == len - 1 || comments.charAt(current + 1) != '#' && comments.charAt(current + 1) != '!') {
bw.write("#");
}
}

last = current + 1;
}
}

if (last != current) {
bw.write(comments.substring(last, current));
}

bw.newLine();
}
public void store(OutputStream out, String comments) throws IOException {
this.store1(new BufferedWriter(new OutputStreamWriter(out, "8859_1")), comments, true);
}

private void store1(BufferedWriter bw, String comments, boolean escUnicode) throws IOException {
if (comments != null) {
writeComments(bw, comments);
}

bw.write("#" + (new Date()).toString());
bw.newLine();
synchronized (this) {
Iterator var5 = this.entrySet().iterator();

while (true) {
if (!var5.hasNext()) {
break;
}

Map.Entry<Object, Object> e = (Map.Entry) var5.next();
String key = (String) e.getKey();
String val = (String) e.getValue();
//key = this.saveConvert(key, true, escUnicode);
//val = this.saveConvert(val, false, escUnicode);
bw.write(key + "=" + val);
bw.newLine();
}
}

bw.flush();
}

private static char toHex(int nibble) {
return hexDigit[nibble & 15];
}
}
151 changes: 151 additions & 0 deletions src/main/java/net/hypixel/resourcepack/impl/MCPatcherConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package net.hypixel.resourcepack.impl;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.hypixel.resourcepack.Converter;
import net.hypixel.resourcepack.PackConverter;
import net.hypixel.resourcepack.Util;
import net.hypixel.resourcepack.pack.Pack;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import net.hypixel.resourcepack.extra.PropertiesEx;


public class MCPatcherConverter extends Converter {

public MCPatcherConverter(PackConverter packConverter) {
super(packConverter);
}

@Override
public void convert(Pack pack) throws IOException {
Path models = pack.getWorkingPath().resolve("assets" + File.separator + "minecraft" +File.separator + "optifine");
findFiles(models);
//remapModelJson(models.resolve("item"));
//remapModelJson(models.resolve("block"));
}

protected void findFiles(Path path) throws IOException {
File directory = new File(path.toString());
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isDirectory()) {
remapProperties(Paths.get(file.getPath()));
findFiles(Paths.get(file.getPath()));

}
}
}
protected void remapProperties(Path path) throws IOException {

if (!path.toFile().exists()) return;

Files.list(path)
.filter(path1 -> path1.toString().endsWith(".properties"))
.forEach(model -> {
try (InputStream input = new FileInputStream( model.toString())) {
PropertiesEx prop = new PropertiesEx();
prop.load(input);

try(OutputStream output = new FileOutputStream( model.toString())) {
//updates textures
if (prop.containsKey("texture")) {
prop.setProperty("texture", replaceTextures(prop));
}
//Updates Item IDs
if (prop.containsKey("matchItems")) {
prop.setProperty("matchItems", updateID("matchItems", prop).replaceAll("\"", ""));

}
if (prop.containsKey("matchBlocks")) {
prop.setProperty("matchBlocks", updateID("matchBlocks", prop).replaceAll("\"", ""));

}



//Saves File
prop.store(output, "");
}

catch (IOException io) {
io.printStackTrace();
}


} catch (IOException e) {
throw Util.propagate(e);
}
});
}

protected String replaceTextures(PropertiesEx prop) {
NameConverter nameConverter = packConverter.getConverter(NameConverter.class);
String properties = prop.getProperty("texture");

if (properties.startsWith("textures/blocks/")) {
properties = "textures/block/" + nameConverter.getBlockMapping();
} else if (properties.startsWith("textures/items/")) {
properties = "textures/item/" + nameConverter.getItemMapping();
}
else{
return properties;
}
return properties;
}

protected String updateID(String type, PropertiesEx prop) {
JsonObject id = Util.readJsonResource(packConverter.getGson(), "/ids.json");
String properties2 = new String();
for (Map.Entry<String, JsonElement> id2 : id.entrySet()) {
if (prop.getProperty(type).contains(" ")) {
String[] split = prop.getProperty(type).split(" ");
for (int i = 0; i < split.length; i++) {
if (prop.containsKey("metadata")) {
if ((split[i] + ":" + prop.getProperty("metadata")).equals(id2.getKey())) {
properties2 = properties2 + " " + id2.getValue().getAsString();
System.out.println("renamed " + prop.getProperty(type) + " to " + properties2);
}
}

else if ((split[i]).equals(id2.getKey())) {
properties2 = properties2 + " " + id2.getValue().getAsString();
System.out.println("renamed " + prop.getProperty(type) + " to " + properties2);
}

}


} else {

if (prop.getProperty(type).equals(id2.getKey())) {
String value = new String();
if (prop.containsKey("metadata")) {
value = id2.getValue().getAsString() + ":" + prop.getProperty("metadata");

} else {
value = id2.getValue().getAsString();
}

properties2 = value;
System.out.println("renamed " + prop.getProperty(type) + " to " + value);
return properties2;
}
}

}
if(prop.containsKey("metadata")) prop.remove("metadata");
if (properties2 != "") {
return properties2;

} else {;
return prop.getProperty(type);
}


}
}
Loading