snake-biscuits / bsp_tool

Python library for analysing .bsp files
GNU General Public License v3.0
104 stars 8 forks source link

Eliminate circular imports in `archives` #200

Open snake-biscuits opened 2 months ago

snake-biscuits commented 2 months ago

Some archives scripts cannot import branches.base valve.source & nexon.cso2 import pkware.Zip & nexon.PakFile Using Struct would be really handy for some binary structures (those less tightly packed)

This might be because we use from ..branches.base import Struct (to avoid collision w/ archives.base) Since that has to go through bsp_tool/branches/__init__.py, which loads all branch scripts

Would from ..branches import base as branches_base work?

If that isn't the case, we'll have to move branches.base classes elsewhere (maybe utils.binary?)

snake-biscuits commented 2 months ago

Would from ..branches import base as branches_base work?

No, it doesn't

Time for Plan B

snake-biscuits commented 2 months ago

moving such a core feature to a new location is a huge job we also need to be sure the new location is a good one

utils.binary seems like a good location from a namespaces perspective but branches.base is a big cluttered file, overlapping w/ function utils could be extremely messy

taking each class (Struct, MappedArray & BitField) into their own script seems smart, but idk branches.base contains some utils universal to all 3, which might be hard to spread

possible new submodule:

lump_classes/
  | __init__.py
  | bitfield.py
  | mapped_array.py
  | struct.py

tbh we could put this under bsp_tool.lumps, since most lumps interact w/ RawBspLump etc. branches.shared.UnsignedInts etc. (BasicLumpClasses) would need to move too grouping them together makes sense to me, feels intuitive to remember branches.shared is kinda an awkward spot anyway

snake-biscuits commented 2 months ago

would also be cool to have a generic SpecialLumpClass baseclass for wiring up a .from_bytes() @classmethod that way implementations only need a .from_stream() __init__

binary.read_struct has been really handy for building .from_stream()s in archives I can see it getting a lot of use in SpecialLumpClass specs as well

Just can't think of where'd be a good place to put that