Bone Rush - pyglet resource error

Hello. Project https://pyweek.org/e/ThEdA_P22/


I started out getting errors with pyglet's resource module not allowing backslashes in its path elements. I surmise it's a peculiarity of Python on Windows. This was easily worked around by replacing in main.py and run_game.py all of the os.path.join() with "/".join().

But I hit an error I can't solve. Help would be appreciated. :)

960.0 640.0 Scene
Node
Sprite
b'Texture/singleColor.png'  Type:  1  SMode:  0
Sprite
b'Texture/title.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Node
Sprite
b'Texture/env/flowers.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/env/grass.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/env/flowers.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/env/grass.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/env/flowers.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/env/grass.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/env/grass.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/env/flowers.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Sprite
b'Texture/best.png'  Type:  0  SMode:  1
SpriteOpacity:  255
Label
 b'100'(0.0,0.0) 30.0 OverFlowType: 0
Traceback (most recent call last):
  File "/pyweek26/ThEdA_P22/bone_rush-1.0/run_game.py", line 12, in 
    main.main()
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\main.py", line 77, in main
    scn = CreateScene()
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\main.py", line 61, in CreateScene
    scene = cr.LoadScene('/'.join(["Scene", "main.ccreator"]))
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\ccreader.py", line 305, in LoadScene
    scene = self.createTree(root)
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\ccreader.py", line 265, in createTree
    sub_tree = self.createTree(tree.Children(i))
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\ccreader.py", line 265, in createTree
    sub_tree = self.createTree(tree.Children(i))
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\ccreader.py", line 233, in createTree
    (node, node_name) = createLabel(buff, pos)
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\ccreader.py", line 201, in createLabel
    anchor_x='center', anchor_y='center', multiline=ml, width=lbl.Node().ContentSize().W(), height = lbl.Node().ContentSize().H())
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\cocos\text.py", line 74, in __init__
    self.create_element()
  File "\pyweek26\ThEdA_P22\bone_rush-1.0\gamelib\cocos\text.py", line 77, in create_element
    self.element = self.klass(group=self.group, batch=self.batch, **self.kwargs)
  File "\Python36\lib\site-packages\pyglet\text\__init__.py", line 461, in __init__
    'align': align,
  File "\Python36\lib\site-packages\pyglet\text\document.py", line 603, in set_style
    0, len(self.text), attributes)
  File "\Python36\lib\site-packages\pyglet\text\document.py", line 511, in set_style
    self.dispatch_event('on_style_text', start, end, attributes)
  File "\Python36\lib\site-packages\pyglet\event.py", line 357, in dispatch_event
    if handler(*args):
  File "\Python36\lib\site-packages\pyglet\text\layout.py", line 1047, in on_style_text
    self._init_document()
  File "\Python36\lib\site-packages\pyglet\text\layout.py", line 1020, in _init_document
    self._update()
  File "\Python36\lib\site-packages\pyglet\text\layout.py", line 956, in _update
    lines = self._get_lines()
  File "\Python36\lib\site-packages\pyglet\text\layout.py", line 932, in _get_lines
    glyphs = self._get_glyphs()
  File "\Python36\lib\site-packages\pyglet\text\layout.py", line 1052, in _get_glyphs
    self._document.get_font_runs(dpi=self._dpi),
  File "\Python36\lib\site-packages\pyglet\text\document.py", line 613, in get_font_runs
    ft = self.get_font(dpi=dpi)
  File "\Python36\lib\site-packages\pyglet\text\document.py", line 623, in get_font
    bold=bool(bold), italic=bool(italic), dpi=dpi)
  File "\Python36\lib\site-packages\pyglet\font\__init__.py", line 156, in load
    font = _font_class(name, size, bold=bold, italic=italic, dpi=dpi)
  File "\Python36\lib\site-packages\pyglet\font\win32.py", line 492, in __init__
    super(GDIPlusFont, self).__init__(name, size, bold, italic, dpi)
  File "\Python36\lib\site-packages\pyglet\font\win32.py", line 233, in __init__
    self.logfont = self.get_logfont(name, size, bold, italic, dpi)
  File "\Python36\lib\site-packages\pyglet\font\win32.py", line 265, in get_logfont
    logfont.lfFaceName = asbytes(name)
ValueError: bytes too long (57, maximum length 32)
Exception ignored in: >
Traceback (most recent call last):
  File "\Python36\lib\site-packages\pyglet\font\win32.py", line 532, in __del__
    super(GDIPlusFont, self).__del__()
  File "\Python36\lib\site-packages\pyglet\font\win32.py", line 247, in __del__
    gdi32.DeleteObject(self.hfont)
AttributeError: 'GDIPlusFont' object has no attribute 'hfont'
Process finished with exit code 1

(log in to comment)

Comments

I hope you are using the pyglet version 1.3.2 

the latest version 1.4.* seems to have compatibility issues with cocos library. 

Also it seems strange to me that you had to replace the os.path.join() with "/".join() not sure if its' a windows thing though. 
Ok, turns out pyglet doesn't want to deal with the "\" which is what os.path.join() would throw on windows.. 

@ gummbum thank you for reporting and taking the effort to fix the problem. Please follow the below steps to get it work 


replace line 199 in ccbreader.py to look like following:

       font_name=lbl.FontName().decode('utf-8')[:20],

the pyglet resource thing is a known issue on windows.... last pyweek I patched a pyglet version but forgot to submit it as a pull request or bug report. Maybe this time I will.
Please please .. submit it.  I didn't realise this was an issue. I've tried to get the big re-opened. Posted the sample code which produces the error.

Haven't heard any response back though.

Hi again I dig into my older pyweek files and I found the patched file. Its resource.py and I have pasted the patched file here: https://bpaste.net/show/720420eb5a91 Look for the two additional
        name = os.path.normpath(name)
call that I added at lines 418 and 434.

This should do the trick I think.
Apparently I needed both fixes, the ccreader.py mod and the pyglet mods. Thanks, guys. It runs now.
Yup.. one is a bug in pyglet the other in cocos for windows specifically.
https://bitbucket.org/pyglet/pyglet/pull-requests/171/handle-backslash-in-paths-for-resources/diff
Thank you man. 👍