From ddc17071db7d24b01329a100df5c008b40d6fa85 Mon Sep 17 00:00:00 2001 From: Michael Becker Date: Tue, 27 Aug 2024 08:46:31 -0400 Subject: [PATCH] preliminary implementation of UE DataFormat in Python --- editor-python/src/editor/core/DataFormat.py | 13 ++++++++++++- .../editor/core/InvalidDataFormatException.py | 2 ++ editor-python/src/editor/core/__init__.py | 3 +++ editor-python/src/editor/core/io/Reader.py | 18 ++++++++++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 editor-python/src/editor/core/InvalidDataFormatException.py create mode 100644 editor-python/src/editor/core/__init__.py diff --git a/editor-python/src/editor/core/DataFormat.py b/editor-python/src/editor/core/DataFormat.py index e8aa710..d7c8f6e 100644 --- a/editor-python/src/editor/core/DataFormat.py +++ b/editor-python/src/editor/core/DataFormat.py @@ -15,6 +15,17 @@ # You should have received a copy of the GNU General Public License # along with editor-python. If not, see . +from io import FileIO + class DataFormat: - pass \ No newline at end of file + 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) diff --git a/editor-python/src/editor/core/InvalidDataFormatException.py b/editor-python/src/editor/core/InvalidDataFormatException.py new file mode 100644 index 0000000..3ffad07 --- /dev/null +++ b/editor-python/src/editor/core/InvalidDataFormatException.py @@ -0,0 +1,2 @@ +class InvalidDataFormatException (Exception): + pass \ No newline at end of file diff --git a/editor-python/src/editor/core/__init__.py b/editor-python/src/editor/core/__init__.py new file mode 100644 index 0000000..ec2f14e --- /dev/null +++ b/editor-python/src/editor/core/__init__.py @@ -0,0 +1,3 @@ +from .DataFormat import DataFormat +from .ObjectModel import ObjectModel +from .InvalidDataFormatException import InvalidDataFormatException diff --git a/editor-python/src/editor/core/io/Reader.py b/editor-python/src/editor/core/io/Reader.py index 495c969..d95bbc0 100644 --- a/editor-python/src/editor/core/io/Reader.py +++ b/editor-python/src/editor/core/io/Reader.py @@ -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)