Author Topic: Elven's nwn-lib  (Read 1850 times)

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« on: December 19, 2012, 08:05:12 pm »


               I've just started learning about Elven's nwn-lib.  To say that it is the best thing ever and a trillion times better than LetoScript would be an understatement. (No offense to the creator of LetoScript, which is a very nice tool).

I've never seen anyone talking much about it tho, never seen but the barest sample code, so I figured why not start a thread where people can share a bit about what they've done with it.

Here is my first crack: mod_to_dir.  This script reads a module and dumps its contents into a directory with subdirectories for each file extension.
I.e:
directory\\
+- \\nss
+- \\ncs
etc...

Not super practical or valuable, but pretty simple.
               
               

               


                     Modifié par pope_leo, 19 décembre 2012 - 08:07 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Elven's nwn-lib
« Reply #1 on: December 19, 2012, 08:42:19 pm »


               well ive looked into this previously, but see no way how to run this on windows, or am I wrong there?
               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« Reply #2 on: December 19, 2012, 08:49:43 pm »


               Got to http://rubyinstaller.org/  Download the latest installer: RubyInstaller version 1.9.3-p327

When you install it make sure to check the option for including Ruby binaries in your PATH.  That makes everything much simpler.

Once it's installed you can open a command prompt and do gem install nwn-lib  That will install nwn-lib.

When you run ruby scripts that require nwn-lib like the one i posted it will likely give you a message about xml and not supporting modpacker formats.  I would suggest just ignoring that, because getting libxml to work on my system was a massive exercise in frustration.  I don't even think after hours of trying I got it to work right.

Edit:  I'm running it on Windows 7.
               
               

               


                     Modifié par pope_leo, 19 décembre 2012 - 08:53 .
                     
                  


            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« Reply #3 on: December 19, 2012, 09:20:24 pm »


               Here is another script... it's not a nwn-lib one, but it's an example of how one might muck about with NWN mdl files in Ruby. It assumes nwnmdlcomp.exe is in the same directory as the script. If the model is in binary format it creates a temporary ascii model.

This reads through the ascii mdl line by line and yanks out the bitmap lines and returns a dictionary of their names... (I am assuming that those are the textures)

Github Gist here
               
               

               


                     Modifié par pope_leo, 19 décembre 2012 - 09:21 .
                     
                  


            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« Reply #4 on: December 21, 2012, 07:47:37 pm »


               So... I've been working with nwn-lib bit more and it seems any attempt read a GFF with characters outside the ASCII range produces an error...  I somehow have thousands of character encoding errors in a ~50mb module... Seems to be the same both on Windows and Linux.  Anyone know a workaround for this?

EDIT: All these strings are encoded in windows-1252...

I'm starting to see that there are  some issues with it's and Ruby's ease of use on windows.
               
               

               


                     Modifié par pope_leo, 22 décembre 2012 - 09:15 .
                     
                  


            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
Elven's nwn-lib
« Reply #5 on: December 22, 2012, 03:05:34 pm »


               <not quite...>

I'm thinking it's ruby thinking the strings are UTF-8 when they're not...
I have three links but I haven't done more than read them ;-/

Ruby String.encode still gives “invalid byte sequence in UTF-8”
Ruby 1.9's String
Working with Encodings in Ruby 1.9

The main point being that ruby 1.8+ stored the raw bytes and the encoding separately, with the assumtion that the raw is UTF-8 if no encoding included.

I think.

So it has to be preprocessed --> UTF-8 before nwn-lib works with it.

<...there yet>
               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« Reply #6 on: December 22, 2012, 04:53:57 pm »


                Ah... makes more sense now.  nwn-lib has a environment variable that lets you set the encoding your stuff is in.  When Ruby attempts to encode a byte string in your encoding, it has to encode it in UTF-8 first and so if you have invalid utf-8 characters encodings it quits with an error.  These aren't even off the wall characters either, but things like minuscule a-circumflex, things that it should be able to convert.

I modified things to work enough that it could read the GFFs without erroring out in nwn-lib-0.5.0\\lib\\nwn\\gff\\reader.rb:

162c162
<               @field_data[data_or_offset + 4, len].encode(NWN.setting :in_encoding)
---
>               @field_data[data_or_offset + 4, len].force_encoding(NWN.setting :in_encoding)
166c166
<               @field_data[data_or_offset + 1, len].encode(NWN.setting :in_encoding)
---
>               @field_data[data_or_offset + 1, len].force_encoding(NWN.setting :in_encoding)
179c179
<         str = all[8, len].unpack("a*")[0].encode(NWN.setting :in_encoding)
---
>         str = all[8, len].unpack("a*")[0].force_encoding(NWN.setting :in_encoding)

No clue if that' would be a good idea to a Rubyist, I'll send it on to Elven and see what he thinks.  For querying the GFFs for information the following probably wouldn't really matter, but there is still something fishy going on with the text exports to json and yaml.  Windows aware text editors like Notepad++ seem to be able to figure out the (mixture?) of encodings in the file and render the characters properly... others like Emacs and gVim don't.  I've not tried anything furter than that.  

Is NWN utf-8 aware?
               
               

               


                     Modifié par pope_leo, 22 décembre 2012 - 05:00 .
                     
                  


            

Legacy_niv

  • Jr. Member
  • **
  • Posts: 62
  • Karma: +0/-0
Elven's nwn-lib
« Reply #7 on: December 26, 2012, 11:06:30 am »


                Hi,
regarding the charset issue, I've pushed a commit to address that. Please test it against your data files, and if any still fail (or swallow characters), please mail those to me if possible (and the charset specified). It's being built by CI at: http://ci.swordcoast.net/job/nwn-lib/ and is not yet live on the gem mirrors.

http://ci.swordcoast...n-lib-0.5.1.gem (which is the "next" branch on gh)

Regarding the lack of examples, there's actually a few in the yardocs (and on the internet). I'll list what I can think of.
For general API examples, see: http://ci.swordcoast...file.HOWTO.html

From my own PW, there's some nwn-gff import/export filter scripts used to normalise data: https://github.com/s...ter/mod/filters (they're passed into nwn-gff -r when converting gff files to yml)

And a script to generate alphabetised resref palettes: https://github.com/s...aster/mod/mkpal

Hope that helps a bit ..
               
               

               


                     Modifié par nivviv, 26 décembre 2012 - 11:15 .
                     
                  


            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« Reply #8 on: December 26, 2012, 08:20:38 pm »


               Thank you for the fix and for the links to the scripts/examples!  

I tested it and it works for me.

There is still one issue with the handling of dumping of void fields.  I don't suspect there are many of these so I might just be unlucky.  In my module.ifo the Mod_id void field has a \\xD6 byte that can't be converted.  I'm guessing the field itself has no real meaning, at least for normal modules.  But that can be worked around.

Thanks again.
               
               

               


                     Modifié par pope_leo, 26 décembre 2012 - 08:21 .
                     
                  


            

Legacy_niv

  • Jr. Member
  • **
  • Posts: 62
  • Karma: +0/-0
Elven's nwn-lib
« Reply #9 on: December 26, 2012, 08:27:20 pm »


               Handling that issue on GH as well.

If you have any questions regarding usage/scripting/nwnx_ruby/etc, feel free to ask. I'd be happy to help.
               
               

               
            

Legacy_niv

  • Jr. Member
  • **
  • Posts: 62
  • Karma: +0/-0
Elven's nwn-lib
« Reply #10 on: December 28, 2012, 11:24:18 am »


               Fixed the json issue as well and merged everything into master. Latest release on the gem mirrors is 0.5.1.

Note: json now encodes :void in base64
               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« Reply #11 on: December 28, 2012, 02:06:18 pm »


               Excellent, thank you.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Elven's nwn-lib
« Reply #12 on: August 21, 2013, 03:16:52 am »


               So i finally had the time for this so I installed the ruby and then nwn-lib per pope_leo's suggestion. Problem is that I dont know what to do next. I dont even know if it worked and installed properly.

How do I run a script ?
               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Elven's nwn-lib
« Reply #13 on: August 23, 2013, 01:04:26 am »


               

ShaDoOoW wrote...

So i finally had the time for this so I installed the ruby and then nwn-lib per pope_leo's suggestion. Problem is that I dont know what to do next. I dont even know if it worked and installed properly.

How do I run a script ?


Are you on windows or linux?

There's a nice tool which will explode gff files into various text formats and back. Here it will turn
the aw.uti item into a mostly human editable yaml file (on Linux at least):

       nwn-gff -i aw.uti -o aw.uti.yml -l gff -k yaml

It won't harm the original. To reverse it:

       nwn-gff -o aw_new.uti -i aw.uti.yml -k gff -l yaml

This will produce aw_new.uti.

This is just a bit of what it can do of course. Once you have it in text a lot of other useful tools are already available,
like grep and awk and sed...

You can also add a "-r filter_script" argument to the first command and have it do whatever you need during the
conversion process.  The one I use all the time is the truncate_floats.rb which comes with.

meaglyn
               
               

               
            

Legacy_leo_x

  • Sr. Member
  • ****
  • Posts: 403
  • Karma: +0/-0
Elven's nwn-lib
« Reply #14 on: August 23, 2013, 07:32:45 pm »


               Depends.  If you're using the library directly, same as any other ruby script: ruby my_script.rb (assuming a ruby interpreter is in your PATH).  If you're wanting to use the cool nwn-dsl/nwn-erf/nwn-gff tools like meaglyn mentioned and are on windows and aren't using cygwin or mingw, then it could get a bit messy -- and might regardless since they use #!/usr/bin/ruby

If you're going to use all of it on windows I highly suggest getting mingw/msys and conemu (the latter is nicer terminal emulator than cmd.exe, but you don't need it.) and making your workflow as unix like as possible.

To test if it's all installed right you can try at a command prompt where the bold text is what you type:
irb (the ruby interactive interpret)
irb(main):001:0> require 'nwn/all'
(maybe a message about xml...)
=> true

To use it to its full potential you're really going to need to learn at least the basics Ruby.
               
               

               


                     Modifié par pope_leo, 23 août 2013 - 07:06 .