Skip to content

Commit

Permalink
Changed expression evaluation on cond/vercond to be in line with issu…
Browse files Browse the repository at this point in the history
…e 73 of nifxml spec: cond is for local variables, vercond for global variables. Updated source xmls accordingly, should not result in a change of generated code (except for nif.xml).
  • Loading branch information
Candoran2 committed Sep 28, 2021
1 parent 6ccb132 commit e5acbcf
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 59 deletions.
2 changes: 1 addition & 1 deletion codegen/Union.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def get_conditions(field):
elif ver2:
conditionals.append(f"{VER} <= {ver2}")
if vercond:
vercond = Expression(vercond)
vercond = Expression(vercond, g_vars=True)
conditionals.append(f"{vercond}")
if versions:
conditionals.append(f"({' or '.join([f'is_{version}(self.context)' for version in versions])})")
Expand Down
21 changes: 10 additions & 11 deletions codegen/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ class Expression(object):

operators = {'==', '!=', '>=', '<=', '&&', '||', '&', '|', '-', '!', '<', '>', '/', '*', '+', '%'}

def __init__(self, expr_str, name_filter=None):
def __init__(self, expr_str, name_filter=None, g_vars=False):
try:
left, self._op, right = self._partition(expr_str)
self._left = self._parse(left, name_filter)
self._right = self._parse(right, name_filter)
self._left = self._parse(left, name_filter, g_vars)
self._right = self._parse(right, name_filter, g_vars)
except:
print("error while parsing expression '%s'" % expr_str)
raise
Expand Down Expand Up @@ -163,18 +163,18 @@ def __str__(self):
return f"{left} {op} {right}".strip()

@classmethod
def _parse(cls, expr_str, name_filter=None):
def _parse(cls, expr_str, name_filter=None, g_vars=False):
"""Returns an Expression, string, or int, depending on the
contents of <expr_str>."""
if not expr_str:
# empty string
return None
# brackets or operators => expression
if ("(" in expr_str) or (")" in expr_str):
return Expression(expr_str, name_filter)
return Expression(expr_str, name_filter, g_vars)
for op in cls.operators:
if expr_str.find(op) != -1:
return Expression(expr_str, name_filter)
return Expression(expr_str, name_filter, g_vars)
# try to convert it to one of the following classes
for create_cls in (int, Version):
try:
Expand All @@ -188,12 +188,11 @@ def _parse(cls, expr_str, name_filter=None):
if name_filter is None:
def name_filter(x):
return convention.name_attribute(x)
prefix = "self."
# globals are stored on the stream
# it is only a global if the leftmost member has version in it
# ie. general_info.ms2_version is not a global
if "version" in expr_str.split(".")[0].lower():
if g_vars:
# globals are stored on the context
prefix = "self.context."
else:
prefix = "self."
return prefix + ('.'.join(name_filter(comp) for comp in expr_str.split(".")))

@classmethod
Expand Down
4 changes: 2 additions & 2 deletions source/formats/fgm/fgm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version id="PZ16" version="20" user_version="8340 8724">Planet Zoo 1.6</version>
<version id="JWE" version="19" user_version="24724 25108">Jurassic World Evolution</version>

<token name="verexpr" attrs="vercond cond">
<token name="verexpr" attrs="vercond">
Commonly used version expressions.
<verexpr token="#DLA#" string="(#VER# #EQ# 15)">Disneyland Adventure</verexpr>
<verexpr token="#ZTUAC#" string="(#VER# #EQ# 17)">ZTUAC</verexpr>
Expand All @@ -19,7 +19,7 @@
<verexpr token="#JWE#" string="(((#USER# #EQ# 24724) #OR# (#USER# #EQ# 25108)) #AND# (#VER# #EQ# 19))">JWE, 25108 is JWE on switch</verexpr>
</token>

<token name="global" attrs="vercond cond">
<token name="global" attrs="vercond">
Global Tokens.
NOTE: These must be listed after the above tokens so that they replace last. For example, `verexpr` uses these tokens.
<global token="#USER#" string="user_version" />
Expand Down
28 changes: 14 additions & 14 deletions source/formats/manis/manis.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@



<token name="verexpr" attrs="vercond cond">
<token name="verexpr" attrs="vercond">
Commonly used version expressions.
<verexpr token="#PZ#" string="(#USER# #EQ# 8340)">PZ</verexpr>
<verexpr token="#PZ16#" string="(#VER# #EQ# 20)">PZ</verexpr>
<verexpr token="#JWE#" string="(#USER# #EQ# 24724)">JWE</verexpr>
<verexpr token="#PC#" string="(#VER# #EQ# 18)">PC</verexpr>
</token>

<token name="global" attrs="vercond cond">
<token name="global" attrs="vercond">
Global Tokens.
NOTE: These must be listed after the above tokens so that they replace last. For example, `verexpr` uses these tokens.
<global token="#USER#" string="user_version" />
Expand Down Expand Up @@ -224,10 +224,10 @@
<add name="ff1" type="ubyte" >always FF FF</add>
<add name="ff2" type="ubyte" >always FF FF</add>
<add name="e" type="ushort" > </add>
<add name="extra pc" type="ushort" arr1="5" cond="#PC#"> </add>
<add name="extra pc" type="ushort" arr1="5" vercond="#PC#"> </add>
<add name="g" type="ushort" > </add>
<add name="zeros 2" type="uint" arr1="57" > rest 228 bytes </add>
<add name="extra zeros pc" type="ushort" arr1="7" cond="#PC#" > rest 14 bytes </add>
<add name="extra zeros pc" type="ushort" arr1="7" vercond="#PC#" > rest 14 bytes </add>
<add name="i" type="ushort" > </add>
<add name="j" type="ushort" > </add>
<add name="ff" type="ushort" > always FF </add>
Expand All @@ -241,28 +241,28 @@
<!-- JWE uses uint-->

<add name="ref" type="Empty" > </add>
<add name="indices c 2" type="ushort" arr1="#ARG#\c2" cond="#PC#" > </add>
<add name="indices c 2" type="uint" arr1="#ARG#\c2" cond="!#PC#" > </add>
<add name="indices c 2" type="ushort" arr1="#ARG#\c2" vercond="#PC#" > </add>
<add name="indices c 2" type="uint" arr1="#ARG#\c2" vercond="!#PC#" > </add>

<add name="indices 0" type="ushort" arr1="#ARG#\c" cond="#PC#" > </add>
<add name="indices 0" type="uint" arr1="#ARG#\c" cond="!#PC#" > </add>
<add name="indices 0" type="ushort" arr1="#ARG#\c" vercond="#PC#" > </add>
<add name="indices 0" type="uint" arr1="#ARG#\c" vercond="!#PC#" > </add>

<add name="indices 1" type="ushort" arr1="#ARG#\name count" cond="#PC#" > </add>
<add name="indices 1" type="uint" arr1="#ARG#\name count" cond="!#PC#" > </add>
<add name="indices 1" type="ushort" arr1="#ARG#\name count" vercond="#PC#" > </add>
<add name="indices 1" type="uint" arr1="#ARG#\name count" vercond="!#PC#" > </add>

<add name="indices e2" type="ushort" arr1="#ARG#\e2" cond="#PC#" > </add>
<add name="indices e2" type="uint" arr1="#ARG#\e2" cond="!#PC#" > </add>
<add name="indices e2" type="ushort" arr1="#ARG#\e2" vercond="#PC#" > </add>
<add name="indices e2" type="uint" arr1="#ARG#\e2" vercond="!#PC#" > </add>

<!-- <add name="indices 2" type="ubyte" arr1="#ARG#\e" cond="#PC#" > </add>-->
<!-- <add name="indices 2" type="ubyte" arr1="#ARG#\e" cond="!#PC#" > </add>-->
<!-- <add name="indices 2" type="ushort" arr1="#ARG#\e" cond="#PC#" > </add>-->
<!-- <add name="indices 2" type="uint" arr1="#ARG#\e" cond="!#PC#" > </add>-->

<add name="p indices 00" type="ubyte" arr1="#ARG#\c2" > </add>
<add name="p indices 00" type="ubyte" arr1="#ARG#\c2" cond="#PC#"> </add>
<add name="p indices 00" type="ubyte" arr1="#ARG#\c2" vercond="#PC#"> </add>

<add name="p indices 0" type="ubyte" arr1="#ARG#\c" > </add>
<add name="p indices 0" type="ubyte" arr1="#ARG#\c" cond="#PC#"> </add>
<add name="p indices 0" type="ubyte" arr1="#ARG#\c" vercond="#PC#"> </add>

<add name="p indices 0b" type="ubyte" arr1="#ARG#\e" > </add>

Expand Down
4 changes: 2 additions & 2 deletions source/formats/matcol/matcol.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<!DOCTYPE niftoolsxml>
<niftoolsxml version="0.7.1.0">

<token name="verexpr" attrs="vercond cond">
<token name="verexpr" attrs="vercond">
Commonly used version expressions.
<verexpr token="#PZ#" string="(#USER# #EQ# 8340)">PZ</verexpr>
<verexpr token="#JWE#" string="(#USER# #EQ# 24724)">JWE</verexpr>
</token>

<token name="global" attrs="vercond cond">
<token name="global" attrs="vercond">
Global Tokens.
NOTE: These must be listed after the above tokens so that they replace last. For example, `verexpr` uses these tokens.
<global token="#USER#" string="user_version" />
Expand Down
46 changes: 23 additions & 23 deletions source/formats/ms2/ms2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<version id="old" version="17 18" >Old</version>

<token name="verexpr" attrs="vercond cond">
<token name="verexpr" attrs="vercond">
Commonly used version expressions.
<verexpr token="#DLA#" string="(#VER# #EQ# 15)">Disneyland Adventure</verexpr>
<verexpr token="#ZTUAC#" string="(#VER# #EQ# 17)">ZTUAC</verexpr>
Expand All @@ -15,7 +15,7 @@
<verexpr token="#JWE#" string="(((#USER# #EQ# 24724) #OR# (#USER# #EQ# 25108)) #AND# (#VER# #EQ# 19))">JWE, 25108 is JWE on switch</verexpr>
</token>

<token name="global" attrs="vercond cond">
<token name="global" attrs="vercond">
Global Tokens.
NOTE: These must be listed after the above tokens so that they replace last. For example, `verexpr` uses these tokens.
<global token="#USER#" string="user_version" />
Expand Down Expand Up @@ -323,18 +323,18 @@
<add name="bone info index" type="uint" >used to find bone info</add>
<add name="ms2 name" type="string" >name of ms2</add>

<add name="model info" type="CoreModelInfo" cond="!#OLD#" >gives relevant info on the mdl, including counts and pack base</add>
<add name="materials" type="MaterialName" arr1="model info\num_materials" cond="!#OLD#" >name pointers for each material</add>
<add name="lods" type="LodInfo" arr1="model info\num_lods" cond="!#OLD#" >lod info for each level</add>
<add name="objects" type="MeshLink" arr1="model info\num_objects" cond="!#OLD#" >material links for each model</add>
<add name="models" type="ModelData" arr1="(model info\num_models)" cond="!#OLD#" >model data blocks for this mdl2</add>
<add name="model info" type="CoreModelInfo" vercond="!#OLD#" >gives relevant info on the mdl, including counts and pack base</add>
<add name="materials" type="MaterialName" arr1="model info\num_materials" vercond="!#OLD#" >name pointers for each material</add>
<add name="lods" type="LodInfo" arr1="model info\num_lods" vercond="!#OLD#" >lod info for each level</add>
<add name="objects" type="MeshLink" arr1="model info\num_objects" vercond="!#OLD#" >material links for each model</add>
<add name="models" type="ModelData" arr1="(model info\num_models)" vercond="!#OLD#" >model data blocks for this mdl2</add>
</compound>

<compound name="MaterialName">
<add name="name index" type="uint" cond="!#OLD#">index into ms2 names array</add>
<add name="name index" type="ushort" cond="#OLD#">index into ms2 names array</add>
<add name="some index" type="uint" cond="!#OLD#">unknown, nonzero in PZ flamingo juvenile, might be junk (padding)</add>
<add name="some index" type="ushort" cond="#OLD#">unknown, nonzero in PZ flamingo juvenile, might be junk (padding)</add>
<add name="name index" type="uint" vercond="!#OLD#">index into ms2 names array</add>
<add name="name index" type="ushort" vercond="#OLD#">index into ms2 names array</add>
<add name="some index" type="uint" vercond="!#OLD#">unknown, nonzero in PZ flamingo juvenile, might be junk (padding)</add>
<add name="some index" type="ushort" vercond="#OLD#">unknown, nonzero in PZ flamingo juvenile, might be junk (padding)</add>
</compound>

<compound name="MeshLink">
Expand Down Expand Up @@ -522,7 +522,7 @@
<add name="count7" type="uint64" >index count 7</add>
<add name="unknownextra" type="uint64" vercond="#OLD#" >zero</add>
<add name="joint count" type="uint64" >joint count</add>
<add name="unk78count" type="uint64" vercond="! #OLD#" >unnk 78 count</add>
<add name="unk78count" type="uint64" vercond="!#OLD#" >unnk 78 count</add>
<add name="unknown88" type="uint64" vercond="#JWE# #OR# #OLD#" >jwe only, everything is shifted a bit due to extra uint 0</add>

<add name="name indices" type="uint" arr1="name count" vercond="!#OLD#">index into ms2 string table for bones used here</add>
Expand Down Expand Up @@ -597,28 +597,28 @@
<add name="count_0" type="uint" >small number</add>
<add name="count_1" type="uint" >small number</add>
<add name="count_2" type="uint" >small number</add>
<add name="zeros extra" type="uint" arr1="2" cond="#PC#">0s, might be related to count 7 in PC</add>
<add name="zeros extra" type="uint" arr1="2" vercond="#PC#">0s, might be related to count 7 in PC</add>
<add name="namespace length" type="uint" >size of the name buffer below, including trailing zeros</add>
<add name="zeros 0" type="uint" arr1="5" >0s</add>
<add name="pc count" type="uint" >0 or 1</add>
<add name="zeros 1" type="uint" arr1="7">0s</add>
<add name="extra zeros pc" type="uint" arr1="4" cond="#PC#">0s</add>
<add name="extra zeros pc" type="uint" arr1="4" vercond="#PC#">0s</add>
<add name="ones" type="uint64" arr1="2" >1, 1</add>
<add name="bone count" type="uint" >matches bone count from bone info</add>
<add name="joint entry count" type="uint" >0</add>
<add name="zeros 2" type="uint" arr1="4">usually 0s</add>
<add name="joint_transforms" type="JointEntry" arr1="joint_count" >corresponds to bone transforms</add>

<add name="zeros 3" type="uint64" arr1="joint_count" cond="! #PC#">might be pointers</add>
<add name="UnknownListc" type="ListCEntry" arr1="joint_count" cond="! #PC#" ></add>
<add name="first list" type="ListFirst" arr1="count_0" cond="! #PC#" >used by ptero, 16 bytes per entry</add>
<add name="short list" type="ListShort" arr1="count_1" cond="! #PC#" ></add>
<add name="long list" type="ListLong" arr1="count_2" cond="! #PC#" ></add>
<add name="zeros 3" type="uint64" arr1="joint_count" vercond="!#PC#">might be pointers</add>
<add name="UnknownListc" type="ListCEntry" arr1="joint_count" vercond="!#PC#" ></add>
<add name="first list" type="ListFirst" arr1="count_0" vercond="!#PC#" >used by ptero, 16 bytes per entry</add>
<add name="short list" type="ListShort" arr1="count_1" vercond="!#PC#" ></add>
<add name="long list" type="ListLong" arr1="count_2" vercond="!#PC#" ></add>

<add name="pc ffs" type="PcFFCounter" cond="#PC#" >?</add>
<add name="pc bytes" type="byte" arr1="9" cond="#PC#" >1FAA FFAAFF00 000000</add>
<add name="pc hitcheck count" type="uint64" cond="#PC#">counts hitchecks for pz</add>
<add name="pc zero 0" type="uint64" cond="#PC#">0</add>
<add name="pc ffs" type="PcFFCounter" vercond="#PC#" >?</add>
<add name="pc bytes" type="byte" arr1="9" vercond="#PC#" >1FAA FFAAFF00 000000</add>
<add name="pc hitcheck count" type="uint64" vercond="#PC#">counts hitchecks for pz</add>
<add name="pc zero 0" type="uint64" vercond="#PC#">0</add>
<add name="pc floats" type="float" arr1="pc count" arr2="10" vercond="#PC#">sometimes an array of floats</add>

<add name="joint indices" type="int" arr1="joint_count" >index into bone info bones for each joint; bone that the joint is attached to</add>
Expand Down
4 changes: 2 additions & 2 deletions source/formats/ovl/ovl.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version id="PZ16" version="20" user_version="8340 8724">Planet Zoo 1.6+</version>
<version id="JWE" version="19" user_version="24724 25108">Jurassic World Evolution</version>

<token name="verexpr" attrs="vercond cond">
<token name="verexpr" attrs="vercond">
Commonly used version expressions.
<verexpr token="#DLA#" string="(#VER# #EQ# 15)">Disneyland Adventure</verexpr>
<verexpr token="#ZTUAC#" string="(#VER# #EQ# 17)">ZTUAC</verexpr>
Expand All @@ -19,7 +19,7 @@
<verexpr token="#JWE#" string="(((#USER# #EQ# 24724) #OR# (#USER# #EQ# 25108)) #AND# (#VER# #EQ# 19))">JWE, 25108 is JWE on switch</verexpr>
</token>

<token name="global" attrs="vercond cond">
<token name="global" attrs="vercond">
Global Tokens.
NOTE: These must be listed after the above tokens so that they replace last. For example, `verexpr` uses these tokens.
<global token="#USER#" string="user_version" />
Expand Down
4 changes: 2 additions & 2 deletions source/formats/tex/tex.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version id="PZ16" version="20" user_version="8340 8724">Planet Zoo 1.6</version>
<version id="JWE" version="19" user_version="24724 25108">Jurassic World Evolution</version>

<token name="verexpr" attrs="vercond cond">
<token name="verexpr" attrs="vercond">
Commonly used version expressions.
<verexpr token="#DLA#" string="(#VER# #EQ# 15)">Disneyland Adventure</verexpr>
<verexpr token="#ZTUAC#" string="(#VER# #EQ# 17)">ZTUAC</verexpr>
Expand All @@ -19,7 +19,7 @@
<verexpr token="#JWE#" string="(((#USER# #EQ# 24724) #OR# (#USER# #EQ# 25108)) #AND# (#VER# #EQ# 19))">JWE, 25108 is JWE on switch</verexpr>
</token>

<token name="global" attrs="vercond cond">
<token name="global" attrs="vercond">
Global Tokens.
NOTE: These must be listed after the above tokens so that they replace last. For example, `verexpr` uses these tokens.
<global token="#USER#" string="user_version" />
Expand Down
4 changes: 2 additions & 2 deletions source/formats/voxelskirt/voxelskirt.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
<version id="PZ" version="19" user_version="8340 8724">Planet Zoo</version>
<version id="JWE" version_flag="1" version="19" user_version="24724 25108">Jurassic World Evolution</version>

<token name="verexpr" attrs="vercond cond">
<token name="verexpr" attrs="vercond">
Commonly used version expressions.
<verexpr token="#ZTUAC#" string="(#VER# #EQ# 17)">ZTUAC</verexpr>
<verexpr token="#PC#" string="(#VER# #EQ# 18)">PC</verexpr>
<verexpr token="#PZ#" string="(((#USER# #EQ# 8340) #OR# (#USER# #EQ# 8724)) #AND# (#VER# #EQ# 19))">PZ</verexpr>
<verexpr token="#JWE#" string="(((#USER# #EQ# 24724) #OR# (#USER# #EQ# 25108)) #AND# (#VER# #EQ# 19) #AND# (#FLAG# #EQ# 1))">JWE, 25108 is JWE on switch</verexpr>
</token>

<token name="global" attrs="vercond cond">
<token name="global" attrs="vercond">
Global Tokens.
NOTE: These must be listed after the above tokens so that they replace last. For example, `verexpr` uses these tokens.
<global token="#USER#" string="user_version" />
Expand Down

0 comments on commit e5acbcf

Please sign in to comment.