Try load chunk data.
All checks were successful
Build Dusk / build-linux (push) Successful in 53s
Build Dusk / build-psp (push) Successful in 56s

This commit is contained in:
2025-11-16 15:02:18 -06:00
parent 750e8840f0
commit 9a59c22288
6 changed files with 129 additions and 12 deletions

View File

@@ -0,0 +1,36 @@
import json
class ChunkData:
def __init__(self):
self.current = {}
self.original = {}
self.filename = None
def load(self, filename):
try:
with open(filename, 'r') as f:
self.current = json.load(f)
self.filename = filename
self.original = json.loads(json.dumps(self.current)) # Deep copy
except Exception as e:
raise RuntimeError(f"Failed to load chunk file: {e}")
def save(self, filename=None):
fname = filename if filename else self.filename
if not fname:
raise ValueError("No filename specified for saving chunk.")
try:
with open(fname, 'w') as f:
json.dump(self.current, f, indent=2)
self.original = json.loads(json.dumps(self.current)) # Deep copy
self.filename = fname
except Exception as e:
raise RuntimeError(f"Failed to save chunk file: {e}")
def new(self):
self.current = {}
self.original = {}
self.filename = None
def isDirty(self):
return json.dumps(self.current, sort_keys=True) != json.dumps(self.original, sort_keys=True)

View File

@@ -0,0 +1,53 @@
import json
import os
from dusk.event import Event
class ChunkData:
def __init__(self, mapData, x, y, z):
self.mapData = mapData
self.x = x
self.y = y
self.z = z
self.current = {}
self.original = {}
self.onChunkData = Event()
def load(self):
fname = self.getFilename()
if not fname or not os.path.exists(fname):
self.new()
return
try:
with open(fname, 'r') as f:
self.current = json.load(f)
self.original = json.loads(json.dumps(self.current)) # Deep copy
self.onChunkData.invoke(self.current)
except Exception as e:
raise RuntimeError(f"Failed to load chunk file: {e}")
def save(self):
fname = self.getFilename()
if not fname:
raise ValueError("No filename specified for saving chunk.")
try:
with open(fname, 'w') as f:
json.dump(self.current, f, indent=2)
self.original = json.loads(json.dumps(self.current)) # Deep copy
except Exception as e:
raise RuntimeError(f"Failed to save chunk file: {e}")
def new(self):
self.current = {}
self.original = {}
self.onChunkData.invoke(self.current)
def isDirty(self):
return json.dumps(self.current, sort_keys=True) != json.dumps(self.original, sort_keys=True)
def getFilename(self):
if not self.mapData or not hasattr(self.mapData, 'getMapDirectory'):
return None
dir_path = self.mapData.getMapDirectory()
if dir_path is None:
return None
return f"{dir_path}/{self.x}_{self.y}_{self.z}.json"

View File

@@ -1,8 +1,5 @@
from dusk.event import Event
MAP_WIDTH = 5
MAP_HEIGHT = 5
MAP_DEPTH = 3
from editortool.map.mapdata import MAP_WIDTH, MAP_HEIGHT, MAP_DEPTH
class Map:
def __init__(self):

View File

@@ -3,9 +3,14 @@ from dusk.event import Event
from PyQt5.QtWidgets import QFileDialog, QMessageBox
from PyQt5.QtCore import QTimer
import os
from editortool.map.chunkdata import ChunkData
MAP_DEFAULT_PATH = os.path.join(os.path.dirname(__file__), '../../../assets/map/')
EDITOR_CONFIG_PATH = os.path.join(os.path.dirname(__file__), '.editor')
MAP_WIDTH = 5
MAP_HEIGHT = 5
MAP_DEPTH = 3
MAP_CHUNK_COUNT = MAP_WIDTH * MAP_HEIGHT * MAP_DEPTH
class MapData:
def __init__(self):
@@ -13,7 +18,14 @@ class MapData:
self.dataOriginal = {}
self.onMapData = Event()
self.mapFileName = None
self.chunks = {}
index = 0
for x in range(MAP_WIDTH):
for y in range(MAP_HEIGHT):
for z in range(MAP_DEPTH):
self.chunks[index] = ChunkData(self, x, y, z)
index += 1
QTimer.singleShot(16, self.loadLastFile)
def loadLastFile(self):
@@ -29,7 +41,7 @@ class MapData:
def updateEditorConfig(self):
try:
config = {'lastFile': self.mapFileName if self.mapFileName else ""}
config = {'lastFile': self.getMapFilename() if self.getMapFilename() else ""}
with open(EDITOR_CONFIG_PATH, 'w') as f:
json.dump(config, f, indent=2)
except Exception:
@@ -39,23 +51,25 @@ class MapData:
self.data = {}
self.dataOriginal = {}
self.mapFileName = None
for chunk in self.chunks.values():
chunk.new()
self.onMapData.invoke(self.data)
self.updateEditorConfig()
def save(self, fname=None):
if not self.mapFileName and fname is None:
if not self.getMapFilename() and fname is None:
filePath, _ = QFileDialog.getSaveFileName(None, "Save Map File", MAP_DEFAULT_PATH, "Map Files (*.json)")
if not filePath:
return
self.mapFileName = filePath
if fname:
self.mapFileName = fname
if not self.isMapFileDirty():
return
try:
with open(self.mapFileName, 'w') as f:
with open(self.getMapFilename(), 'w') as f:
json.dump(self.data, f, indent=2)
self.dataOriginal = json.loads(json.dumps(self.data)) # Deep copy
for chunk in self.chunks.values():
chunk.save()
self.updateEditorConfig()
except Exception as e:
QMessageBox.critical(None, "Save Error", f"Failed to save map file:\n{e}")
@@ -66,6 +80,8 @@ class MapData:
self.data = json.load(f)
self.mapFileName = fileName
self.dataOriginal = json.loads(json.dumps(self.data)) # Deep copy
for chunk in self.chunks.values():
chunk.load()
self.onMapData.invoke(self.data)
self.updateEditorConfig()
except Exception as e:
@@ -75,4 +91,19 @@ class MapData:
return json.dumps(self.data, sort_keys=True) != json.dumps(self.dataOriginal, sort_keys=True)
def isDirty(self):
return self.isMapFileDirty()
return self.isMapFileDirty() or self.anyChunksDirty()
def getMapFilename(self):
return self.mapFileName if self.mapFileName else None
def getMapDirectory(self):
fname = self.getMapFilename()
if not fname or not fname.endswith('.json'):
return None
return fname[:-5] # Remove '.json' extension
def anyChunksDirty(self):
for chunk in self.chunks.values():
if chunk.isDirty():
return True
return False

View File

@@ -5,7 +5,7 @@ from editortool.map.chunkpanel import ChunkPanel
from editortool.map.mapinfopanel import MapInfoPanel
from editortool.map.mapdata import MapData
from editortool.map.toolbar import MapToolbar
from editortool.statusbar import StatusBar
from editortool.map.statusbar import StatusBar
class MapWindow(QMainWindow):