Npc dumper

This commit is contained in:
Adam
2016-04-23 13:22:43 -04:00
parent 0cef0e0b3d
commit ad099cbe95
3 changed files with 157 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
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.NpcDefinition;
import net.runelite.cache.definitions.loaders.ItemLoader;
import net.runelite.cache.definitions.loaders.NpcLoader;
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 NpcDumper
{
private final File cache, out, java;
private final Gson gson;
private NpcLoader loader;
public NpcDumper(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 NpcLoader();
try (Store store = new Store(cache))
{
store.load();
Index index = store.getIndex(NpcLoader.INDEX_TYPE);
Archive archive = index.getArchive(NpcLoader.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 (NpcDefinition def : loader.getNpcs())
{
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, "NpcID.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 NpcID {");
for (NpcDefinition def : loader.getNpcs())
{
if (def.name.equalsIgnoreCase("NULL"))
continue;
String name = name(def.name);
if (name == null)
continue;
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 (s.isEmpty())
return null;
if (Character.isDigit(s.charAt(0)))
return "_" + s;
else
return s;
}
}

View File

@@ -0,0 +1,22 @@
package net.runelite.cache;
import java.io.File;
import java.io.IOException;
import org.junit.Test;
public class NpcDumperTest
{
@Test
public void test() throws IOException
{
NpcDumper dumper = new NpcDumper(
new File("d:/rs/07/cache"),
new File("d:/rs/07/cache/npcs"),
new File("d:/rs/07/cache")
);
dumper.load();
dumper.dump();
dumper.java();
}
}

View File

@@ -37,6 +37,7 @@ public class NpcLoaderTest
new java.io.File(base, "npcs").mkdir();
// XXX this can use npc dumper
GsonBuilder builder = new GsonBuilder()
.setPrettyPrinting();
Gson g = builder.create();