Jump to content

Script File (ini)


liverare

Recommended Posts

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:

JJ4jxeQ.png

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

qh8vX6W.png

The script will then use that

F6jbUPT.png

But let's say you didn't specify "target", probably because you misspelled it

doOR8QW.png

The CLI parameter "target" isn't specified, so the script will look for a guard instead

hl1dCTn.png

 

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 by liverare
  • Like 6
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...