diff --git a/assets/src/data/scripts/bafoundation/util.py b/assets/src/data/scripts/bafoundation/util.py index 95b87d00..eadd647d 100644 --- a/assets/src/data/scripts/bafoundation/util.py +++ b/assets/src/data/scripts/bafoundation/util.py @@ -46,6 +46,30 @@ def utc_now() -> datetime.datetime: return datetime.datetime.now(datetime.timezone.utc) +def data_size_str(bytecount: int) -> str: + """Given a size in bytes, returns a short human readable string. + + This should be 6 or fewer chars for most all sane file sizes. + """ + # pylint: disable=too-many-return-statements + if bytecount <= 999: + return f'{bytecount} B' + kbytecount = bytecount / 1024 + if round(kbytecount, 1) < 10.0: + return f'{kbytecount:.1f} KB' + if round(kbytecount, 0) < 999: + return f'{kbytecount:.0f} KB' + mbytecount = bytecount / (1024 * 1024) + if round(mbytecount, 1) < 10.0: + return f'{mbytecount:.1f} MB' + if round(mbytecount, 0) < 999: + return f'{mbytecount:.0f} MB' + gbytecount = bytecount / (1024 * 1024 * 1024) + if round(gbytecount, 1) < 10.0: + return f'{mbytecount:.1f} GB' + return f'{gbytecount:.0f} GB' + + class DispatchMethodWrapper(Generic[TARG, TRET]): """Type-aware standin for the dispatch func returned by dispatchmethod.""" diff --git a/docs/ba_module.md b/docs/ba_module.md index 9aea2fe1..af51e5b5 100644 --- a/docs/ba_module.md +++ b/docs/ba_module.md @@ -1,6 +1,6 @@ - -
This page documents the Python classes and functions in the 'ba' module, which are the ones most relevant to modding in Ballistica. If you come across something you feel should be included here or could be better explained, please let me know. Happy modding!