This function will export a file, and then read through that file using a FileObject, and when it finds something which would cause a syntax error, it encapsulates the rest of the variable in an array.
This is not a good thing to do if you are releasing the add-on, as its much slower than export, but it works fine if you just want to lazily save some variables.
Also worth noting that this allows you to save arrays and stuff when you don't know either the length or the index of each element, as it uses export.
function Safe_Export(%vars, %dir) {
export(%vars, "Config/Client/SafeExport_Temp.cs");
%readFile = new fileObject();
%readFile.openForRead("Config/Client/SafeExport_Temp.cs");
%writeFile = new fileObject();
%writeFile.openForWrite(%dir);
%legalChars = "abcdefghijklmnopqrstuvwxyz_"; //Only these chars can be the first letter of a var name
%extra = "0123456789"; //These chars can be anywhere in a var name
while(!%readFile.isEOF()) {
%line = %readFile.readLine();
%var = getSubStr(%line, 1, (%pos = strPos(%line, " = "))-1);
%value = getSubStr(%line, %pos + 3, strLen(%line) - strLen(%var) - 5);
if(striPos(%legalChars, %firstChar = getSubStr(%var, 0, 1)) == -1) {
error("ERROR: Safe_Export: Var starts with illegal char!" SPC %firstChar);
continue;
}
for(%i=1; %i < strLen(%var); %i++) {
%char = getSubStr(%var, %i, 1);
if(striPos(%legalChars @ %extra, %char) == -1) {
%var = getSubStr(%var, 0, %i) @ "[\"" @ expandEscape(getSubStr(%var, %i, strLen(%var) - %i)) @ "\"]";
break;
}
}
if(getSubStr(%value, 0, 1) !$= "\"" || getSubStr(%value, strLen(%value)-1, 1) !$= "\"")
%value = "\"" @ %value @ "\"";
%writeFile.writeLine("$" @ %var SPC "=" SPC %value @ ";");
}
%readFile.close();
%readFile.delete();
%writeFile.close();
%writeFile.delete();
}
Edit: Also, you'll probably want to check that openForWrite(%dir); returns true, and if it doesn't, close and delete the file objects. I've already spoonfed enough though.
Edit 2: Oh, it's also a lot smarter, faster, and more efficient to simply start the array after the first character in the variable's name. This function is old, idk what I was thinking.