socialpoint-labs / unity-yaml-parser

Python3 library to manipulate Unity serialized files from outside the Unity Editor.
https://pypi.org/project/unityparser/
MIT License
137 stars 24 forks source link

UnityYAML does not seem to have comments #64

Open rafzi opened 11 months ago

rafzi commented 11 months ago

See: https://docs.unity3d.com/Manual/UnityYAML.html "UnityYAML does not support comments."

For example, GameObjects' names can contain #. In a scene file, it may look like this:

GameObject:
  ...
  m_Name: Hello # World

The Unity editor shows this in the UI as Hello # World, but unityparser will just extract Hello.

But I actually found this issue because of the following parsing error. An example scene file:

GameObject:
  ...
  m_Name: Really # really # really # really # really # really # really # long # long#
name#

Note how the name is split across two lines. The trailing # seems to cause the error:

  File "yaml/composer.py", line 127, in compose_mapping_node
    while not self.check_event(MappingEndEvent):
  File "yaml/parser.py", line 98, in check_event
    self.current_event = self.state()
  File "unityparser/parser.py", line 65, in parse_block_mapping_key
    raise ParserError("while parsing a block mapping", self.marks[-1],
yaml.parser.ParserError: while parsing a block mapping
  in "Example.unity", line 997, column 3
expected <block end>, but found '<scalar>'
  in "Example.unity", line 1015, column 5

Happy to submit a PR, but would appreciate some pointers.

sp-ricard-valverde commented 11 months ago

Hey, thanks for reporting !

Frankly, the scalar parsing logic comes from Pyyaml library(which does not preserve comments, but it does parse them out apparently) so it does not surprise me that the comment gets stripped out.

In order to try support this we'll probably need to overwrite the scanner logic that Pyyaml does, by allowing the # character for plain scalars: https://github.com/yaml/pyyaml/blob/a98fd6088e81d7aca571220c966bbfe2ac43c335/lib/yaml/scanner.py#L735

Probably needs overriding the emitter too: https://github.com/yaml/pyyaml/blob/a98fd6088e81d7aca571220c966bbfe2ac43c335/lib/yaml/emitter.py#L692