Add item dumper/ItemID generator
This commit is contained in:
127
src/main/java/net/runelite/cache/ItemDumper.java
vendored
Normal file
127
src/main/java/net/runelite/cache/ItemDumper.java
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
package net.runelite.cache;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import net.runelite.cache.definitions.ItemDefinition;
|
||||
import net.runelite.cache.definitions.loaders.ItemLoader;
|
||||
import net.runelite.cache.fs.Archive;
|
||||
import net.runelite.cache.fs.Index;
|
||||
import net.runelite.cache.fs.Store;
|
||||
import net.runelite.cache.io.InputStream;
|
||||
|
||||
public class ItemDumper
|
||||
{
|
||||
private final File cache, out, java;
|
||||
private final Gson gson;
|
||||
private ItemLoader loader;
|
||||
|
||||
public ItemDumper(File cache, File out, File java)
|
||||
{
|
||||
this.cache = cache;
|
||||
this.out = out;
|
||||
this.java = java;
|
||||
|
||||
GsonBuilder builder = new GsonBuilder()
|
||||
.setPrettyPrinting();
|
||||
gson = builder.create();
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws IOException
|
||||
{
|
||||
if (args.length < 3)
|
||||
System.exit(1);
|
||||
|
||||
File cache = new File(args[0]);
|
||||
File out = new File(args[1]);
|
||||
File java = new File(args[2]);
|
||||
|
||||
ItemDumper dumper = new ItemDumper(cache, out, java);
|
||||
dumper.load();
|
||||
dumper.dump();
|
||||
dumper.java();
|
||||
}
|
||||
|
||||
public void load() throws IOException
|
||||
{
|
||||
loader = new ItemLoader();
|
||||
|
||||
try (Store store = new Store(cache))
|
||||
{
|
||||
store.load();
|
||||
|
||||
Index index = store.getIndex(ItemLoader.INDEX_TYPE);
|
||||
Archive archive = index.getArchive(ItemLoader.ARCHIVE_ID);
|
||||
|
||||
for (net.runelite.cache.fs.File f : archive.getFiles())
|
||||
{
|
||||
loader.load(f.getFileId(), new InputStream(f.getContents()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dump() throws IOException
|
||||
{
|
||||
for (ItemDefinition def : loader.getItems())
|
||||
{
|
||||
out.mkdirs();
|
||||
java.io.File targ = new java.io.File(out, def.id + ".json");
|
||||
try (FileWriter fw = new FileWriter(targ))
|
||||
{
|
||||
fw.write(gson.toJson(def));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void java() throws IOException
|
||||
{
|
||||
java.mkdirs();
|
||||
java.io.File targ = new java.io.File(java, "ItemID.java");
|
||||
try (PrintWriter fw = new PrintWriter(targ))
|
||||
{
|
||||
Set<String> used = new HashSet<>();
|
||||
|
||||
fw.println("/* This file is automatically generated. Do not edit. */");
|
||||
fw.println("package net.runelite.api;");
|
||||
fw.println("");
|
||||
fw.println("public final class ItemID {");
|
||||
for (ItemDefinition def : loader.getItems())
|
||||
{
|
||||
if (def.name.equalsIgnoreCase("NULL"))
|
||||
continue;
|
||||
|
||||
String name = name(def.name);
|
||||
String suffix = "";
|
||||
while (used.contains(name + suffix))
|
||||
{
|
||||
if (suffix.isEmpty())
|
||||
suffix = "_2";
|
||||
else
|
||||
suffix = "_" + (Integer.parseInt(suffix.substring(1)) + 1);
|
||||
}
|
||||
name += suffix;
|
||||
|
||||
used.add(name);
|
||||
|
||||
fw.println(" public static final int " + name + " = " + def.id + ";");
|
||||
}
|
||||
fw.println("}");
|
||||
}
|
||||
}
|
||||
|
||||
private static String name(String in)
|
||||
{
|
||||
String s = in.toUpperCase()
|
||||
.replace(' ', '_')
|
||||
.replaceAll("[^a-zA-Z0-9_]", "");
|
||||
if (Character.isDigit(s.charAt(0)))
|
||||
return "_" + s;
|
||||
else
|
||||
return s;
|
||||
}
|
||||
}
|
||||
22
src/test/java/net/runelite/cache/ItemDumperTest.java
vendored
Normal file
22
src/test/java/net/runelite/cache/ItemDumperTest.java
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
package net.runelite.cache;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ItemDumperTest
|
||||
{
|
||||
@Test
|
||||
public void test() throws IOException
|
||||
{
|
||||
ItemDumper dumper = new ItemDumper(
|
||||
new File("d:/rs/07/cache"),
|
||||
new File("d:/rs/07/cache/items"),
|
||||
new File("d:/rs/07/cache")
|
||||
);
|
||||
dumper.load();
|
||||
dumper.dump();
|
||||
dumper.java();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,8 @@ public class ItemLoaderTest
|
||||
//@Test
|
||||
public void extract() throws IOException
|
||||
{
|
||||
// XXX this can all use ItemDumper
|
||||
|
||||
ItemLoader loader = new ItemLoader();
|
||||
|
||||
java.io.File base = StoreLocation.LOCATION;
|
||||
|
||||
Reference in New Issue
Block a user