Kitty Corp Meow Mix Forums

Help & Tutorials => Programming Tutorials => Topic started by: libertyernie on June 02, 2013, 08:17:41 PM



Title: Using BrawlLib in Python or Ruby
Post by: libertyernie on June 02, 2013, 08:17:41 PM
BrawlLib is the library used by BrawlBox. It's written in C#, but it can be used with any language that runs on the .NET Framework. The most popular of these languages are C# and Visual Basic, but there are also special .NET implementations of the scripting languages Python and Ruby, named IronPython (http://ironpython.net) and IronRuby, (http://www.ironruby.net) respectively. Both projects provide an interpreter for the language that also allows use of .NET classes in your code.
IronPython and IronRuby are open-source, but were originally developed by Microsoft until 2010. IronPython was last updated in August 2012 and targets Python 2.7; IronRuby targets Ruby 1.9.2 and hasn't been updated since March 2011 (but it still works.)

First, you'll need to download and install IronPython (http://ironpython.net) or IronRuby (http://www.ironruby.net) from their web sites. That way, you'll have the interpreter installed. IronPython will be in C:\Program Files (x86)\IronPython 2.7\ipy.exe, and IronRuby will be in C:\Program Files (x86)\IronRuby 1.1\bin\ir.exe. (Remove the "(x86)" part if you're on a 32-bit machine.)
You can make a shortcut to that .exe file and drag-and-drop your .py or .rb script onto it. Or, you can right-click your script, choose "Open with," and find that .exe file. (You might want to uncheck the box that says "Always use the selected program...")

Then, you need a script to run.
If you want to use BrawlLib in your scripts, you need to put BrawlLib.dll in the same folder as the .rb or .py file, and include these lines at the top.

For Python:
Code:
import clr
clr.AddReference("BrawlLib.dll")

For Ruby:
Code:
require "BrawlLib.dll"

Then you can import BrawlLib classes like this. Both of these lines will import all the classes in the BrawlLib.SSBB.ResourceNodes (https://code.google.com/p/brawltools2/source/browse/#svn%2Ftrunk%2FBrawlLib%2FSSBB%2FResourceNodes) namespace, including the NodeFactory and ResourceNode classes. You might need other ones too, depending on what you want to do in your script.
Code:
# For Python
from BrawlLib.SSBB.ResourceNodes import *

# For Ruby
include BrawlLib::SSBB::ResourceNodes

.py and .rb scripts are just plain text files, and so they can be edited with Notepad. Here's a sample program I wrote to test IronRuby and IronPython. It uses a recursive method to find all the TEX0 textures in a file called "input.pac", and exports them all to PNG files in the same folder.

For Python:
Code:
import clr
clr.AddReference("BrawlLib.dll")
from BrawlLib.SSBB.ResourceNodes import *

def tex_search(node): #Recursive function to scan for all TEX0 nodes in the file
if isinstance(node, TEX0Node):
return [node] #If it's a TEX0 node, include it in the list
list = []
for child in node.Children:
list += tex_search(child) #Otherwise, keep looking for children
return list

input_file = NodeFactory.FromFile(None, "input.pac")
for item in tex_search(input_file):
print item.Name
item.Export(item.Name + ".png") #We already know everything in this list is a TEX0, so we can export it to a PNG
print("    Done!")

For Ruby:
Code:
require "BrawlLib.dll"
include BrawlLib::SSBB::ResourceNodes

def tex_search(node) #Recursive function to scan for all TEX0 nodes in the file
if node.kind_of?(TEX0Node)
return [node] #If it's a TEX0 node, include it in the list
else
list = Array.new
node.Children.each { |child|
list += tex_search(child) #Otherwise, keep looking for children
}
return list
end
end

input_file = NodeFactory.FromFile(nil, "input.pac")
tex_search(input_file).each { |item|
puts item.Name
item.Export(item.Name + ".png") #We already know everything in this list is a TEX0, so we can export it to a PNG
}
puts("    Done!")

To figure out how to use BrawlLib, you'll probably need to look at the BrawlLib and BrawlBox source code. (https://code.google.com/p/brawltools2/source/browse/) I'm afraid I won't be much help here; I often need to look at BrawlLib's code to figure out what to do. So you'll still have to read some C#, but you won't need to write it.