script-assembler: remove duplicate code

This commit is contained in:
Lucwousin
2019-07-24 08:56:35 +02:00
parent 4264aeecaf
commit 4069f8b945
2 changed files with 27 additions and 73 deletions

View File

@@ -52,56 +52,25 @@ public class AssembleMojo extends AbstractMojo
@Parameter(required = true)
private File outputDirectory;
private AssembleMojo(File scriptDirectory, File outputDirectory)
{
this.scriptDirectory = scriptDirectory;
this.outputDirectory = outputDirectory;
}
private final Log log = getLog();
public static void main(String args[])
public static void main(String[] args) throws Exception
{
if (args.length < 2)
{
throw new IllegalArgumentException("Usage: inputfile outputfile");
}
File scriptDirectory = new File(args[0]);
File outputDirectory = new File(args[1]);
RuneLiteInstructions instructions = new RuneLiteInstructions();
instructions.init();
Assembler assembler = new Assembler(instructions);
ScriptSaver saver = new ScriptSaver();
int count = 0;
File scriptOut = new File(outputDirectory, Integer.toString(IndexType.CLIENTSCRIPT.getNumber()));
scriptOut.mkdirs();
for (File scriptFile : scriptDirectory.listFiles((dir, name) -> name.endsWith(".rs2asm")))
{
System.out.println("Assembling " + scriptFile);
try (FileInputStream fin = new FileInputStream(scriptFile))
{
ScriptDefinition script = assembler.assemble(fin);
byte[] packedScript = saver.save(script);
File targetFile = new File(scriptOut, Integer.toString(script.getId()));
Files.write(packedScript, targetFile);
// Copy hash file
File hashFile = new File(scriptDirectory, Files.getNameWithoutExtension(scriptFile.getName()) + ".hash");
if (hashFile.exists())
{
Files.copy(hashFile, new File(scriptOut, Integer.toString(script.getId()) + ".hash"));
}
else if (script.getId() < 10000) // Scripts >=10000 are RuneLite scripts, so they shouldn't have a .hash
{
System.out.println("Unable to find hash file for " + scriptFile);
}
++count;
}
catch (IOException ex)
{
System.out.println("unable to open file " + ex);
}
}
System.out.println("Assembled " + count + " scripts");
new AssembleMojo(scriptDirectory, outputDirectory).execute();
}
@Override

View File

@@ -48,38 +48,23 @@ public class IndexMojo extends AbstractMojo
@Parameter(required = true)
private File indexFile;
public static void main(String args[])
private IndexMojo(File archiveOverlayDirectory, File indexFile)
{
try (DataOutputStream fout = new DataOutputStream(new FileOutputStream(args[1])))
{
for (File indexFolder : new File(args[0]).listFiles())
{
if (indexFolder.isDirectory())
{
int indexId = parseInt(indexFolder.getName());
for (File archiveFile : indexFolder.listFiles())
{
int archiveId;
try
{
archiveId = parseInt(archiveFile.getName());
}
catch (NumberFormatException ex)
{
continue;
}
this.archiveOverlayDirectory = archiveOverlayDirectory;
this.indexFile = indexFile;
}
fout.writeInt(indexId << 16 | archiveId);
}
}
}
fout.writeInt(-1);
}
catch (IOException ex)
public static void main(String[] args) throws Exception
{
if (args.length < 2)
{
ex.printStackTrace();
throw new IllegalArgumentException("Usage: overlaydir indexfile");
}
File archiveOverlayDirectory = new File(args[0]);
File indexFile = new File(args[1]);
new IndexMojo(archiveOverlayDirectory, indexFile).execute();
}
@Override
@@ -113,7 +98,7 @@ public class IndexMojo extends AbstractMojo
}
catch (IOException ex)
{
throw new MojoExecutionException("error build index file", ex);
throw new MojoExecutionException("error building index file", ex);
}
}