preliminary implementation of UE DataFormat in Python

This commit is contained in:
Michael Becker 2024-08-27 08:46:31 -04:00
parent f79118ac31
commit ddc17071db
4 changed files with 35 additions and 1 deletions

View File

@ -15,6 +15,17 @@
# You should have received a copy of the GNU General Public License
# along with editor-python. If not, see <https://www.gnu.org/licenses/>.
from io import FileIO
class DataFormat:
pass
def load(self, stream : FileIO):
self.load_internal(stream)
def load_internal (self, stream : FileIO):
pass
def save_internal (self, stream : FileIO):
pass
def save (self, stream : FileIO):
self.save_internal(stream)

View File

@ -0,0 +1,2 @@
class InvalidDataFormatException (Exception):
pass

View File

@ -0,0 +1,3 @@
from .DataFormat import DataFormat
from .ObjectModel import ObjectModel
from .InvalidDataFormatException import InvalidDataFormatException

View File

@ -34,10 +34,28 @@ class Reader (ReaderWriterBase):
return self._stream
def read_fixedstring(self, length : int, encoding : Encoding = None) -> str:
"""
Reads a fixed-length string from the underlying stream.
"""
data = self._stream.read(length)
enc = 'utf-8' if encoding is None else encoding.get_value()
return bytes.decode(data, enc)
def read_terminatedstring(self, encoding : Encoding = None, terminator : str = '\0') -> str:
"""
Reads a terminated string (e.g. zero/null terminated) from the underlying stream.
"""
v = ''
enc = 'utf-8' if encoding is None else encoding.get_value()
while True:
c = self._stream.read(1)
if bytes.decode(c, enc) == terminator:
break
v += bytes.decode(c, enc)
return v
def read_single(self) -> float:
data = self._stream.read(4)