From 7934754482ec32299a66c25fb3cf2dfde5a4359c Mon Sep 17 00:00:00 2001
From: Athanasius <github@miggy.org>
Date: Thu, 10 Jun 2021 12:12:02 +0100
Subject: [PATCH] ShipLocker: New event that replaces ShipLockerMaterials

On startup, embark, disembark this is a full event, otherwise it signals
writing of new ShipLocker.json file.
---
 monitor.py | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/monitor.py b/monitor.py
index 4265496e..22893aff 100644
--- a/monitor.py
+++ b/monitor.py
@@ -843,7 +843,21 @@ class EDLogs(FileSystemEventHandler):  # type: ignore # See below
                         # So it's *from* the ship
                         self.state['Cargo'][name] -= c['Count']
 
-            elif event_type == 'ShipLockerMaterials':
+            elif event_type == 'ShipLocker':
+                # As of 4.0.0.400 (2021-06-10)
+                # "ShipLocker" will be a full list written to the journal at startup/boarding/disembarking, and also
+                # written to a separate shiplocker.json file - other updates will just update that file and mention it
+                # has changed with an empty shiplocker event in the main journal.
+
+                if not all([entry.get(t, False) for t in ('Components', 'Consumables', 'Data', 'Items')]):
+                    logger.debug('ShipLocker event is an empty one (at least missing one data type')
+                    # So attempt to load data from the most recent file instead
+                    currentdir_path = pathlib.Path(str(self.currentdir))
+                    # Confirmed filename for 4.0.0.400
+                    with open(currentdir_path / 'ShipLocker.json', 'rb') as h:  # type: ignore
+                        entry = json.load(h, object_pairs_hook=OrderedDict)
+                        self.state['ShipLockerJSON'] = entry
+
                 # This event has the current totals, so drop any current data
                 self.state['Component'] = defaultdict(int)
                 self.state['Consumable'] = defaultdict(int)