Skip to content

Commit

Permalink
Update l2db.py
Browse files Browse the repository at this point in the history
* changed standard DB_INDEX_TYPE to 2
* fully implemented boolean storage (didn't work before because the bools were identified as ints)
  • Loading branch information
Lampe2020 committed Feb 26, 2023
1 parent 0ddc62c commit 7d37863
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions l2db.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, source={}, ign_corrupted_source=False):
self.strict = not ign_corrupted_source
self.db = None
# Default metadata, only kept when L2DB created from dict:
self.metadata = {'VER': self.implementation_version, 'VALTABLE_LEN': 0, 'DB_INDEX_TYPE': 1, 'RAW_VALUES': False}
self.metadata = {'VER': self.implementation_version, 'VALTABLE_LEN': 0, 'DB_INDEX_TYPE': 2, 'RAW_VALUES': False}
self.valtable = {}
self.database = {}
if type(source) == bytes:
Expand Down Expand Up @@ -236,12 +236,10 @@ def eval_db(self, database):
self.db[key] = ''.join([chr(b) for b in self.db[key].split(b'\x00', 1)[1]])
case b'bool':
self.db[key] = not not self.db[key].split(b'\x00', 1)[1][
0] # Invert 2 times to get a boolean
# from the byte's integer value
0] # Invert 2 times to get a boolean from the byte's integer value
case req_type if req_type in (b'', b'bstr', b'bytes'):
self.db[key] = self.db[key].split(b'\x00', 1)[
1] # Just cut away the leading null-byte to not
# destroy the actual value
1] # Just cut away the leading null-byte to not destroy the actual value
except Exception as e:
print(f"Couldn't assign type to entry '{key}' because of a {type(e).__name__}: {e}")

Expand All @@ -257,6 +255,8 @@ def to_bytes(obj):
case '': # str
return helpers['bstr_from_str'](obj) if self.metadata['RAW_VALUES'] \
else b'str\x00' + helpers['bstr_from_str'](obj)
case bool(): # bool
return (b'\x01' if obj else b'\x00') if self.metadata['RAW_VALUES'] else (b'bool\x00\x01' if obj else b'bool\x00\x00')
case 0: # int
try:
return helpers['bstr_from_int'](obj) if self.metadata['RAW_VALUES'] \
Expand Down

0 comments on commit 7d37863

Please sign in to comment.