liverare Posted October 19, 2017 Share Posted October 19, 2017 (edited) I've expanded upon my CLI Support Made Easy to build a fully functional file reader/writer for each script. So assume you're launching a fighting script with the following CLI: -script Fighter:scrub.ini.noob.ini.body.green-d'hide-body.legs.green-d'hide-chaps That'll get converted to: scrub.ini -to- OSBot/data/Fighter/scrub.ini noob.ini -to- OSBot/data/Fighter/noob.ini body:green d'hide body legs:green d'hide chaps Also, there'll be a OSBot/data/Fighter/default.ini created too. The default.ini file is where you specify parameters you want to share for every bot. These files will be created automatically (if they're not already present) and loaded: Anything that you put in the CLI that isn't a .ini file, those will be added (and override) all the key/values plucked from the files. This is similar to when you add inline CSS to HTML elements and how that potentially overrides the CSS loaded in from .css files. Example script code: @ScriptManifest(author = "", info = "", logo = "", name = "Fighter", version = 0) public class CLITest extends Script { NPC npc; ScriptFile scriptFile; Properties cli; @Override public void onStart() throws InterruptedException { String parameters = getParameters(); try { if (parameters != null && !parameters.isEmpty()) { scriptFile = ScriptFile.compileAndCollateScriptIniFiles(getName(), getVersion(), getParameters()); cli = scriptFile.getProperties(); } } catch (IOException e) { e.printStackTrace(); } } @Override public int onLoop() throws InterruptedException { npc = npcs.closest(cli.getProperty("target", "Guard")); return 250; } } So let's say we had noob.ini and we wanted to target a tramp The script will then use that But let's say you didn't specify "target", probably because you misspelled it The CLI parameter "target" isn't specified, so the script will look for a guard instead Source code: import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Properties; public class ScriptFile { // C:\Users\<USER>\osbot\data\ public static final String OSBOT_DATA_DIR = System.getProperty("user.home") + File.separatorChar + "osbot" + File.separatorChar + "data" + File.separatorChar; private final String scriptName; private final double scriptVersion; private final String relativePath; private final File file; private final Properties properties; public ScriptFile(String scriptName, double scriptVersion, String filename, boolean initialise) throws IOException { super(); this.scriptName = scriptName; this.scriptVersion = scriptVersion; this.relativePath = (scriptName + File.separatorChar + filename); this.file = new File(OSBOT_DATA_DIR, relativePath); this.properties = new Properties(); if (initialise) { createFolder(); createFile(); load(); } } @Override public String toString() { return String.format("%s (%s)", scriptName, scriptVersion); } public String getScriptName() { return scriptName; } public double getScriptVersion() { return scriptVersion; } public String getRelativePath() { return relativePath; } public File getFile() { return file; } public Properties getProperties() { return properties; } public String getAbsolutePath() { return file.getAbsolutePath(); } public void createFolder() { File parent = file.getParentFile(); if (!parent.exists() || !parent.isDirectory()) { parent.mkdirs(); } } public void createFile() throws IOException { if (!file.exists() || !file.isFile()) { file.createNewFile(); } } public void load() throws FileNotFoundException, IOException { properties.clear(); try (FileInputStream in = new FileInputStream(file)) { properties.load(in); } } public void save() throws IOException { try (FileOutputStream out = new FileOutputStream(file)) { properties.store(out, toString()); } } public void include(ScriptFile scriptFile) { properties.putAll(scriptFile.properties); } /** * Converts OSBot raw parameter to key/value pairs. * * @param rawParameterString * @return Key/value pairs */ private static Map<String, String> compileParameters(String rawParameterString) { Map<String, String> parameters = null; String[] contents = rawParameterString.split("\\."); String key; String value; if (contents != null && contents.length > 0) { parameters = new HashMap<>(); for (int i = 0; i < contents.length; i += 2) { key = contents[i]; value = (i + 1 < contents.length ? contents[i + 1] : ""); value = value.replaceAll("-", " "); parameters.put(key, value); } } return parameters; } /** * Compile a list of script .ini files * * @param rawParameters * @return List of custom script files and a overriding script file which is not * saved to the system * @throws IOException * - problem reading/writing to file */ private static List<ScriptFile> compileScriptIniFiles(String scriptName, double scriptVersion, String rawParameterString) throws IOException { List<ScriptFile> scriptFiles = null; ScriptFile overridingScriptFile; Map<String, String> parameters = compileParameters(rawParameterString); String k; String v; if (parameters != null) { scriptFiles = new ArrayList<>(); overridingScriptFile = new ScriptFile(scriptName, scriptVersion, "temp", false); for (Entry<String, String> e : parameters.entrySet()) { k = e.getKey(); v = e.getValue(); if (v.equals("ini")) { scriptFiles.add(new ScriptFile(scriptName, scriptVersion, k + "." + v, true)); } else { v = v.replaceAll("-", " "); overridingScriptFile.properties.put(k, v); } } scriptFiles.add(overridingScriptFile); } return scriptFiles; } /** * Load in the default.ini, all custom ini files, and hard-coded overriding * parameters * * @param scriptManifest * - Script to associate this file with * @param rawParameterString * @return Collated script file * @throws IOException - problem reading/writing ini file */ public static ScriptFile compileAndCollateScriptIniFiles(String scriptName, double scriptVersion, String rawParameterString) throws IOException { ScriptFile scriptFile = new ScriptFile(scriptName, scriptVersion, (rawParameterString.hashCode() + ".ini"), false); ScriptFile defaultIni = new ScriptFile(scriptName, scriptVersion, "default.ini", true); List<ScriptFile> customInis = compileScriptIniFiles(scriptName, scriptVersion, rawParameterString); if (defaultIni != null) { scriptFile.include(defaultIni); } if (customInis != null) { customInis.forEach(scriptFile::include); } return scriptFile; } } Edited October 20, 2017 by liverare 6 Quote Link to comment Share on other sites More sharing options...
Deceiver Posted October 19, 2017 Share Posted October 19, 2017 thanks for this! Quote Link to comment Share on other sites More sharing options...
DeathmatchesGG Posted October 19, 2017 Share Posted October 19, 2017 thanks, so much to go through already Quote Link to comment Share on other sites More sharing options...