Skip to content

Instantly share code, notes, and snippets.

@gamefreak
Created February 7, 2010 00:35
Show Gist options
  • Save gamefreak/297087 to your computer and use it in GitHub Desktop.
Save gamefreak/297087 to your computer and use it in GitHub Desktop.
From fd2a0e252c4676b093bbd2ea9e40ffa944e1f0a6 Mon Sep 17 00:00:00 2001
From: Scott McClaugherty <gamefreak4321@gmail.com>
Date: Sat, 6 Feb 2010 19:35:36 -0500
Subject: [PATCH] Created lua format exporter.
Signed-off-by: Scott McClaugherty <gamefreak4321@gmail.com>
---
bin/extract-ares-data.py | 8 +++-
lib/ares/lua.py | 91 ++++++++++++++++++++++++++++++++++++++++++++++
lib/ares/sncd.py | 4 +-
3 files changed, 99 insertions(+), 4 deletions(-)
create mode 100644 lib/ares/lua.py
diff --git a/bin/extract-ares-data.py b/bin/extract-ares-data.py
index 8245077..db51c23 100755
--- a/bin/extract-ares-data.py
+++ b/bin/extract-ares-data.py
@@ -28,6 +28,7 @@ def usage():
print ' -d|--dir read from an expanded resource directory (default).'
print ' -r|--rsrc read from an flat, encoded resource file.'
print ' -x|--xml output each object in XML format.'
+ print ' -l|--lua data in lua format'
print ''
print 'valid types:'
print ' Object (BaseObject, bsob)'
@@ -45,8 +46,8 @@ def main():
"""
try:
opts, args = getopt(sys.argv[1:],
- "drx",
- [ "dir", "rsrc", "xml" ])
+ "drxl",
+ [ "dir", "rsrc", "xml", "lua" ])
except GetoptError, e:
print "%s: %s" % (script_name, str(e))
usage()
@@ -62,6 +63,9 @@ def main():
elif opt in ('-x', '--xml'):
from ares.util import XML
output = XML()
+ elif opt in ('-l', '--lua'):
+ from ares.lua import Lua
+ output = Lua()
else:
assert False, "unhandled option"
diff --git a/lib/ares/lua.py b/lib/ares/lua.py
new file mode 100644
index 0000000..ad765ea
--- /dev/null
+++ b/lib/ares/lua.py
@@ -0,0 +1,91 @@
+# This file is released under the terms of the GNU Lesser General Public
+# License (LGPL). For more information, see:
+# http://www.gnu.org/copyleft/lesser.html
+
+"""
+Lua Exporter.
+"""
+
+__authors__ = [
+ "Scott McClaugherty <gamefreak4321@gmail.com>",
+]
+
+import re
+import json
+
+
+def munge_name(name):
+ return name
+
+def serialize_property(builder, object, property):
+ name = property.replace('_', '-').strip('-')
+ value = getattr(object,property)
+ if value is not None:
+ if type(value) in [str, int, long, bool, float]:
+ if type(value) is str:
+ builder.start(name, {'string': value})
+ elif type(value) is bool:
+ if value:
+ builder.start(name, {'bool': 'true'})
+ else:
+ builder.start(name, {'bool': 'false'})
+ elif type(value) is float:
+ builder.start(name, {'real': str(value)})
+ else:
+ print("INT")
+ builder.start(name, {'integer': str(value)})
+ builder.end(name)
+ else:
+ value.serialize(builder)
+
+
+
+
+class LuaBuilder:
+ def __init__(self):
+ self.object = {}
+ self.stack = []
+ self.stack.append(self.object)
+ def start(self, name, value):
+ pptr = self.stack.pop()
+ ptr = {}
+# pptr[name] = ptr
+ for k, v in value.iteritems():
+# print(k,v)
+ if k in ["bool", "integer", "real","string"]:
+ ptr = v
+ break
+# elif k == "string":
+# ptr = '"' + v + '"'
+# break
+ else:
+ ptr[k] = v
+ pptr[name] = ptr
+ # print(pptr[name])
+ self.stack.append(pptr)
+ self.stack.append(ptr)
+ def end(self, name):
+ self.stack.pop()
+ def tostring(self):
+ pass
+ def close(self):
+ pass
+
+
+class Lua(object):
+
+ def destination(seld, index, object):
+ if hasattr(object, 'id'):
+ id = object.id
+ else:
+ id = index
+ return "%03d.lua" % id
+
+
+ def content(self, object):
+ builder = LuaBuilder()
+ object.serialize(builder)
+ elt = builder.close()
+ return json.dumps(builder.object)
+
+
diff --git a/lib/ares/sncd.py b/lib/ares/sncd.py
index 1bbd378..9dac028 100644
--- a/lib/ares/sncd.py
+++ b/lib/ares/sncd.py
@@ -15,8 +15,8 @@ from ares import ReadUnpack
from ares.exceptions import Underflow
from ares.flags import FlagClass
from ares.types import *
-from ares.util import munge_name, serialize_property
-
+#from ares.util import munge_name, serialize_property
+from ares.lua import munge_name, serialize_property
def range_string(minimum, range):
if range > 0:
--
1.6.6.1+GitX
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment