SexScripts : Reading INI files with variables/strings - https://ss.deviatenow.com:443/viewtopic.php?f=4&t=345 Page 1 of 1

Reading INI files with variables/strings

Banjo_oz [ Mon Oct 21, 2013 1:47 pm ]

Is it possible to create an ini file such as:
Code:
[Hello]
Greeting1="Hello, "+loadString("user.name")+"."
Greeting2="Hi there, , "+loadString("user.name")+"!"

... and have it work correctly when read via:
Code:
def readini = new org.ini4j.Wini(new File("hello.ini"));
show(readini.get("Hello", "Greeting1");

... so that the loadString parts actually work?


I can't get SexScripts to read an ini AND parse the string part of the section properly.

That is, I get:
Code:
"Hello, "+loadString("user.name")+"."

... displayed on-screen, not
Code:
"Hello, John."


Is this a bug or limitation, or something I'm doing wrong?

Re: Reading INI files with variables/strings

doti [ Mon Oct 21, 2013 3:48 pm ]

save()/loadString()/... will work only with the standard ini file. Using org.ini4j.Wini is not recommanded.

Re: Reading INI files with variables/strings

Banjo [ Mon Oct 21, 2013 4:13 pm ]

doti wrote:
save()/loadString()/... will work only with the standard ini file. Using org.ini4j.Wini is not recommanded.

Sorry, I'm a bit confused. Are you saying it's not possible at all (fair enough, if so) or that it's just not possible the way I'm doing it?

I got the above method from your help a while back (might have been a question I asked, or from the tutorial threads, so I'm more than open to other options.

Re: Reading INI files with variables/strings

doti [ Mon Oct 21, 2013 7:36 pm ]

save()/loadString()... only works with the main .properties file.
Edit: I didn't understood at all what you were trying to do. It's to put some Groovy code into a .ini file, then have it executed.
No, it (this mecanism that would be "dynamic code") can not work, sorry for that. You should do :
Code:
[Hello]
Greeting1="Hello, %user.name%."
Greeting2="Hi there, %user.name%!"

Code:
def readini = new org.ini4j.Wini(new File("hello.ini"));
def g1 = readini.get("Hello", "Greeting1"
g1 = g1.replace("%user.name%", loadString("user.name"))

(% is a symbol I chose here, it can be anything)

Re: Reading INI files with variables/strings

Banjo [ Tue Oct 22, 2013 11:42 am ]

doti wrote:
save()/loadString()... only works with the main .properties file.
Edit: I didn't understood at all what you were trying to do. It's to put some Groovy code into a .ini file, then have it executed.
No, it (this mecanism that would be "dynamic code") can not work, sorry for that. You should do :
Code:
[Hello]
Greeting1="Hello, %user.name%."
Greeting2="Hi there, %user.name%!"

Code:
def readini = new org.ini4j.Wini(new File("hello.ini"));
def g1 = readini.get("Hello", "Greeting1"
g1 = g1.replace("%user.name%", loadString("user.name"))

(% is a symbol I chose here, it can be anything)


Thanks! Although I wish there was an easier/less "hacky" way, that does work perfectly (but it also bloats out the script with dozens of 'replace' lines).

Is there a way to use the .replace method on multiple strings at once to reduce code lines? In other words, something besides having to write out:
Code:
def dialog = new org.ini4j.Wini(new File("mistress.ini"));
def hello = dialog.get("Hello", "Greeting1");
def firstmeet1a = dialog.get("FirstMeet", "FirstMeet1A");
def firstmeet1b = dialog.get("FirstMeet", "FirstMeet1B");
   firstmeet1b = firstmeet1b.replace("%user.name%", loadString("user.name"));
def firstmeet1c = dialog.get("FirstMeet", "FirstMeet1C");
   firstmeet1c = firstmeet1c.replace("%mistress.name%", loadString("mistress.name"));
   firstmeet1c = firstmeet1c.replace("%mistress.lastname%", loadString("mistress.lastname"));
   firstmeet1c = firstmeet1c.replace("%mistress.title%", loadString("mistress.title"));
   firstmeet1c = firstmeet1c.replace("%mistress.knownby%", loadString("mistress.knownby"));
def firstmeet2a = dialog.get("FirstMeet", "FirstMeet2A");
def firstmeet2b = dialog.get("FirstMeet", "FirstMeet2A");
   firstmeet2b = firstmeet2b.replace("%user.name%", loadString("user.name"));

Is there a way to check and replace, say, "user.name" in multiple strings in advance, or are we forced to do it one at a time as above? Theoretically, something like:
Code:
firstmeet2a,firstmeet2b = firstmeet2a,firstmeet2b.replace("%user.name%", loadString("user.name"));

Also, is there a way to retrieve a line break? Do I have to make each new line a separate ini key (what I'm doing now) or is there a way to replace "%\n\n%" (for example) with "\n\n" and have it work in SexScripts? Currently I'm using:
Code:
INI FILE:
[FirstMeet]
FirstMeet1A=Hmm, it looks like I have a new plaything.
FirstMeet1B=So, you're %user.name%, hmm? Nice to meet you.
FirstMeet1C=My name is %mistress.name% %mistress.lastname%, but you can call me %mistress.title% %mistress.knownby%.

GROOVY FILE:
show(""+firstmeet1a+"\n\n"+firstmeet1b+"\n\n"+firstmeet1c+"");

... as a workaround.

Re: Reading INI files with variables/strings

doti [ Tue Oct 22, 2013 7:55 pm ]

To apply it again and again, do a closure, but this is advanced Groovy :

Code:
def update = { s -> return s.replace("%mistress.name%", loadString("mistress.name")  )
   .replace("%mistress.lastname%", loadString("mistress.lastname"))
   .replace("%mistress.title%", loadString("mistress.title"))
   .replace("%mistress.knownby%", loadString("mistress.knownby"))
   .replace("%n%", "\n")
}
...
def firstmeet1c = update(dialog.get("FirstMeet", "FirstMeet1C"))

Also, solution above should do the trick for the line returns.

Re: Reading INI files with variables/strings

Banjo [ Tue Oct 22, 2013 8:30 pm ]

doti wrote:
To apply it again and again, do a closure, but this is advanced Groovy :

Code:
def update = { s -> return s.replace("%mistress.name%", loadString("mistress.name")
   .replace("%mistress.lastname%", loadString("mistress.lastname"))
   .replace("%mistress.title%", loadString("mistress.title"))
   .replace("%mistress.knownby%", loadString("mistress.knownby"))
   .replace("%n%", "\n")
}
...
def firstmeet1c = update(dialog.get("FirstMeet", "FirstMeet1C"))

Also, solution above should do the trick for the line returns.

Thanks! Will test this tomorrow... it will be VERY helpful if it works as I hope (right now, my script is massively bloated!).

For integer values, do I put:
Code:
.replace("%spank_swats%", spank_swats.toString ()  )
?

EDIT:

I get an error with the example code that it find "}" but expected ")". Is there a bracket I need to add somewhere to make this work? If so, where would I put it?

When I tried adding one where expected (before the closing "}" or at the end of the first line), both times I got a bunch of "Error groovy.lang.MissingPropertyException: No such property: toString for class: java.lang.String" errors reported.

Re: Reading INI files with variables/strings

doti [ Thu Oct 24, 2013 7:12 pm ]

A ) and () were missing above

Re: Reading INI files with variables/strings

Banjo [ Fri Oct 25, 2013 4:13 pm ]

doti wrote:
A ) and () were missing above

Thanks. Although, with those changes I now get the error:
Code:
Error java.lang.NullPointerException: Cannot invoke method replace() on null object(@ line 441)
(where line 441 is "def update = { s -> return s.replace("%mistress.name%", loadString("mistress.name"))". :(

(changing that first .replace value to something else - for example "user.name"- makes no difference either, just calls the error to the same line 441).

Sorry to post a "wall" of code, but it might be more helpful; THIS works...
Show spoiler
but THIS gives the "cannot replace null object" error mentioned:
Show spoiler

Re: Reading INI files with variables/strings

doti [ Sat Oct 26, 2013 8:58 am ]

It means there is a null value at the left of a dot, so one of the call is done on the null value, because dialog.get() returns null if not found.
Code:
def xxx = update(null)

Re: Reading INI files with variables/strings

Banjo [ Sat Oct 26, 2013 9:22 am ]

doti wrote:
It means there is a null value at the left of a dot, so one of the call is done on the null value, because dialog.get() returns null if not found.
Code:
def xxx = update(null)

Sorry, I'm confused. That makes sense in theory (say, I forgot to set a value before calling it), but then why does
Code:
def update = { s -> return s.replace("%mistress.name%", loadString("mistress.name"))
      .replace("%mistress.lastname%", loadString("mistress.lastname"))
      .replace("%mistress.title%", loadString("mistress.title"))
      .replace("%mistress.knownby%", loadString("mistress.knownby"))
}
def firstmeet1c = update(dialog.get("FirstMeet", "FirstMeet1C"))
fail with that error, yet
Code:
def firstmeet1c = dialog.get("FirstMeet", "FirstMeet1C");
   firstmeet1c = firstmeet1c.replace("%mistress.name%", loadString("mistress.name"));   
   firstmeet1c = firstmeet1c.replace("%mistress.lastname%", loadString("mistress.lastname"));
   firstmeet1c = firstmeet1c.replace("%mistress.title%", loadString("mistress.title"));
   firstmeet1c = firstmeet1c.replace("%mistress.knownby%", loadString("mistress.knownby"));
works fine?

Do you mean that the empty (null) field is in the mistress.ini keys being searched for, or that its an unset value in the .groovy script or data.properties?

Re: Reading INI files with variables/strings

doti [ Sat Oct 26, 2013 9:36 am ]

You must, with you current data, find the bad line. Try commenting must of the .replace() lines, must of the x = x.replace() lines, and find the thing missing.
Trying this bad trick would help to fnd the error.

Code:
def firstmeet1c = update(dialog.get("FirstMeet", "FirstMeet1C")+"")

Re: Reading INI files with variables/strings

Banjo [ Sat Oct 26, 2013 11:46 am ]

FINALLY found it!

When i did, I realized what you were saying: it was an error in mistress.ini not the groovy script itself. One of the keys referenced didn't actually exist due to a single character typo (was "Happy=" instead of "Happy2="). Just wanted to mention that in case anyone else gets stumped by this problem in the future.

Only issue is that checking the script now with SexScripts built-in editor/checker returns "simulation aborted" rather than a clean exit. Presumably this means there's a loop-type error somewhere but no idea where as it doesn't say... I'll just keep testing/running through the script until I hit an issue to fix. :)

Re: Reading INI files with variables/strings

doti [ Sat Oct 26, 2013 3:32 pm ]

It means there's an infinite loop, or, at least, it can't manage to find an exit. This is only the simulation, not the main error checking before, it is possible that it is not a problem at all.

Re: Reading INI files with variables/strings

Banjo [ Sat Oct 26, 2013 5:21 pm ]

doti wrote:
It means there's an infinite loop, or, at least, it can't manage to find an exit. This is only the simulation, not the main error checking before, it is possible that it is not a problem at all.

Yup, I had that error once before and it was indeed an "infinite loop" that caused it.

I can't track it down this time, but I figure running the script over and over and picking different options each time, and/or posting a test version (as I hope to do soon) will mean sooner or later getting stuck in that loop and knowing where the bug is! :)

Fortunately, it seems that this is the "last" bug to be reported, meaning other errors will be detected first (so they can be fixed).

Thanks again for the help!

Page 1 of 1 All times are UTC + 1 hour [ DST ]
https://ss.deviatenow.com:443/
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010, 2011