Skip to content

Commit

Permalink
convert: Load nyan API.
Browse files Browse the repository at this point in the history
  • Loading branch information
heinezen committed Sep 4, 2019
1 parent 94e9296 commit 23be413
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 12 deletions.
8 changes: 4 additions & 4 deletions doc/nyan/api_reference/reference_aux.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ Reference documentation of the `engine.aux` module of the openage modding API.

```python
Accuracy(Entity):
accuracy : float
accuracy_dispersion : float
dispersion_dropoff : DropOffType
target_types : set(GameEntityType)
accuracy : float
accuracy_dispersion : float
dispersion_dropoff : DropOffType
target_types : set(GameEntityType)
blacklisted_entities : set(GameEntity)
```

Expand Down
37 changes: 37 additions & 0 deletions openage/convert/nyan/api_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2019-2019 the openage authors. See copying.md for legal info.

"""
Loads the API into the converter.
TODO: Implement a parser instead of hardcoded
object creation.
"""

from ...nyan.nyan_structs import NyanObject, NyanMember


def load_api():
"""
Returns a dict with the API object's fqon as keys
and the API objects as values.
"""
api_objects = dict()

# Object creation

# engine.root
# engine.root.Entity
nyan_object = NyanObject("Entity")
fqon = "engine.root.Entity"
nyan_object.set_fqon(fqon)

api_objects.update({fqon: nyan_object})

# engine.aux
# engine.aux.accuracy.Accuracy
parents = [api_objects["engine.root.Entity"]]
nyan_object = NyanObject("Accuracy", parents)
fqon = "engine.aux.accuracy.Accuracy"
nyan_object.set_fqon(fqon)

api_objects.update({fqon: nyan_object})
5 changes: 4 additions & 1 deletion openage/convert/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def __init__(self, input_data, palette=None, custom_cutter=None):
"from unknown source type: %s" % (type(input_data)))

self.image_data, (self.width, self.height), self.image_metadata\
= merge_frames(frames)
= merge_frames(frames)

def _slp_to_subtextures(self, frame, palette=None, custom_cutter=None):
"""
Expand Down Expand Up @@ -186,6 +186,9 @@ def save(self, targetdir, filename, meta_formats=None):
formatter.export(targetdir, meta_formats)

def dump(self, filename):
"""
Creates a DataDefinition object for the texture metadata.
"""
return [data_definition.DataDefinition(self,
self.image_metadata,
filename)]
Expand Down
1 change: 1 addition & 0 deletions openage/nyan/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
add_py_modules(
nyan_file.py
nyan_structs.py
)
59 changes: 59 additions & 0 deletions openage/nyan/nyan_file.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# Copyright 2019-2019 the openage authors. See copying.md for legal info.

"""
Nyan file struct that stores a bunch of objects and
manages imports.
"""

from .nyan_structs import NyanObject

FILE_VERSION = "NYAN FILE version 0.1.0"


class NyanFile:
"""
Superclass for nyan files.
"""

def __init__(self, targetdir, filename, nyan_objects=None):

self.targetdir = targetdir
if not isinstance(filename, str):
raise ValueError("str expected as filename, not %s" % type(filename))

self.filename = filename

self.nyan_objects = set()
if nyan_objects:
self.nyan_objects = nyan_objects

def add_nyan_object(self, new_object):
"""
Adds a nyan object to the file.
"""
if isinstance(new_object, NyanObject):
self.nyan_objects.add(new_object)

else:
raise Exception("nyan file cannot contain %s",
new_object)

def save(self):
"""
Creates a .nyan file from the data.
"""
with self.targetdir[self.filename].open("wb") as nyan_file:
nyan_file.write(self.dump())

def dump(self):
"""
Returns the string that represents the nyan file.
"""
output_str = "# %s" % (FILE_VERSION)

# TODO: imports

for nyan_object in self.nyan_objects:
output_str += nyan_object.dump()

return output_str
50 changes: 43 additions & 7 deletions openage/nyan/nyan_structs.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,49 @@ def __init__(self, name: str, parents=None, members=None,
nested_object.set_fqon("%s.%s" % (self._fqon,
nested_object.get_name()))

# Set of children
self._children = set()

self._sanity_check()

if len(self._parents) > 0:
self._process_inheritance()

def add_nested_object(self, nested_object):
def add_nested_object(self, new_nested_object):
"""
Adds a nested object to the nyan object.
"""
if not isinstance(nested_object, NyanObject):
if not isinstance(new_nested_object, NyanObject):
raise Exception("nested object must have <NyanObject> type")

if nested_object is self:
if new_nested_object is self:
raise Exception("nyan object must not contain itself as nested object")

self._nested_objects.add(nested_object)
self._nested_objects.add(new_nested_object)

new_nested_object.set_fqon("%s.%s" % (self._fqon,
new_nested_object.get_name()))

def add_member(self, new_member):
"""
Adds a member to the nyan object.
"""
if self.is_inherited():
raise Exception("added member cannot be inherited")

if not isinstance(new_member, NyanMember):
raise Exception("added member must have <NyanMember> type")

nested_object.set_fqon("%s.%s" % (self._fqon,
nested_object.get_name()))
self._members.add(new_member)

def add_child(self, new_child):
"""
Registers another object as a child.
"""
if not isinstance(new_child, NyanObject):
raise Exception("children must have <NyanObject> type")

self._children.add(new_child)

def get_fqon(self) -> str:
"""
Expand Down Expand Up @@ -155,6 +179,18 @@ def set_fqon(self, new_fqon):
nested_object.set_fqon("%s.%s" % (new_fqon,
nested_object.get_name()))

def update_inheritance(self):
"""
Update the set of inherited members.
"""
# Reinitialize set
self._inherited_members = set()
self._process_inheritance()

# Update child objects
for child in self._children:
child.update_inheritance()

def dump(self, indent_depth=0):
"""
Returns the string representation of the object.
Expand Down Expand Up @@ -502,7 +538,7 @@ def is_initialized(self) -> bool:
"""
Returns True if the member has a value.
"""
return self.value != None
return self.value is not None

def is_inherited(self) -> bool:
"""
Expand Down

0 comments on commit 23be413

Please sign in to comment.