package org.gander.core.rScript; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import org.gander.core.util.Debug; /** * Read Script from .dat file using the required headers **/ public class ScriptFileLoader { private final static String[] folderNames = {"ActionButtons","ObjectClick","Item on Item","NPCClick","SingleItemClick"}; private final static String path = "etc/Scripts/"; private int scriptsLoaded = 0; public ScriptFileLoader() { File[] folders = new File[folderNames.length]; Debug.print("Loading scripts (Number of folders: "+folderNames.length+")"); for(int index = 0; index < folderNames.length; index++) { folders[index] = new File(path+folderNames[index]+"/"); if(!folders[index].exists()) throw new ScriptException("Error "+folderNames[index]+" Does not exist in etc/Scripts/"); } for(File folder : folders) for(File script : folder.listFiles()) manageScript(script); Debug.print("Finished loading "+scriptsLoaded+" Scripts"); } private void manageScript(File scriptFile) { try { currentOffset = 0; buffer = new byte[(int)scriptFile.length()]; FileInputStream ff = new FileInputStream(scriptFile); ff.read(buffer); ff.close(); int header = readByte(); if(header != 4) throw new RuntimeException("Header was not 4 got "+header+" Script : "+scriptFile.getName()); String scriptAuthor = readString(); String scriptName = readString(); boolean threading = readByte() == 1; int type = readByte(); int lines = readByte(); String[] caret = new String[lines]; for(int i = 0; i < lines; i++) caret[i] = readString(); parseScript(caret,type,scriptAuthor,scriptName,threading); } catch (FileNotFoundException ex) { Debug.print("File not found"); } catch (IOException ee) { ee.printStackTrace(); } } private void parseScript(String[] lines, int type, String author, String name, boolean threading) { scriptsLoaded++; Script newScript = new Script(lines,type,author,name,threading); ScriptManager.addScript(newScript); } public int readByte() { return (int)buffer[currentOffset++]; } public java.lang.String readString() { int i = currentOffset; while(buffer[currentOffset++] != 10); return new String(buffer, i, currentOffset - i - 1); } private byte[] buffer; private int currentOffset = 0; }