Skip to content

Commit

Permalink
fix: some bugs related to map window
Browse files Browse the repository at this point in the history
  • Loading branch information
dudantas committed Jan 31, 2024
1 parent 394c4ba commit b78acf8
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 32 deletions.
7 changes: 3 additions & 4 deletions source/client_assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ enum MapVersionID {
MAP_OTBM_2 = 1,
MAP_OTBM_3 = 2,
MAP_OTBM_4 = 3,
MAP_OTBM_5 = 4,
};

// The composed version of a otbm file (otbm version, client version)
// The composed version of a otbm file (otbm version)
struct MapVersion {
MapVersion() :
otbm(MAP_OTBM_1) { }
MapVersion(MapVersionID m) :
otbm(m) { }
otbm(MAP_OTBM_5) { }
MapVersionID otbm;
};

Expand Down
4 changes: 2 additions & 2 deletions source/common_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ void MapPropertiesWindow::OnClickOK(wxCommandEvent &WXUNUSED(event)) {
map.convert(new_ver, true);

// Load the new version
if (!g_gui.LoadVersion(error, warnings)) {
if (!g_gui.loadMapWindow(error, warnings)) {
g_gui.ListDialog(this, "Warnings", warnings);
g_gui.PopupDialog(this, "Map Loader Error", error, wxOK);
g_gui.PopupDialog(this, "Conversion Error", "Could not convert map. The map will now be closed.", wxOK);
Expand Down Expand Up @@ -317,7 +317,7 @@ void MapPropertiesWindow::OnClickOK(wxCommandEvent &WXUNUSED(event)) {
map.cleanInvalidTiles(true);
} else {
UnnamedRenderingLock();
if (!g_gui.LoadVersion(error, warnings)) {
if (!g_gui.loadMapWindow(error, warnings)) {
g_gui.ListDialog(this, "Warnings", warnings);
g_gui.PopupDialog(this, "Map Loader Error", error, wxOK);
g_gui.PopupDialog(this, "Conversion Error", "Could not convert map. The map will now be closed.", wxOK);
Expand Down
24 changes: 10 additions & 14 deletions source/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,9 @@ Editor::Editor(CopyBuffer &copybuffer) :
replace_brush(nullptr) {
wxString error;
wxArrayString warnings;
bool ok = true;

if (g_gui.CloseAllEditors()) {
ok = g_gui.LoadVersion(error, warnings);
if (!g_gui.loadMapWindow(error, warnings)) {
g_gui.PopupDialog("Error", error, wxOK);
g_gui.ListDialog("Warnings", warnings);

auto clientDirectory = ClientAssets::getPath().ToStdString() + "/";
if (!wxDirExists(wxString(clientDirectory))) {
PreferencesWindow dialog(nullptr);
dialog.getBookCtrl().SetSelection(4);
dialog.ShowModal();
dialog.Destroy();
}
}

MapVersion version;
Expand All @@ -88,6 +77,7 @@ Editor::Editor(CopyBuffer &copybuffer) :
map.doChange();
}

// Used for loading a new map from "open map" menu
Editor::Editor(CopyBuffer &copybuffer, const FileName &fn) :
live_server(nullptr),
live_client(nullptr),
Expand All @@ -97,16 +87,22 @@ Editor::Editor(CopyBuffer &copybuffer, const FileName &fn) :
replace_brush(nullptr) {
MapVersion ver;
if (!IOMapOTBM::getVersionInfo(fn, ver)) {
g_gui.PopupDialog("Error", "Could not open file \"" + fn.GetFullPath() + "\".", wxOK);
spdlog::error("Could not open file {}. This is not a valid OTBM file or it does not exist.", nstr(fn.GetFullPath()));
throw std::runtime_error("Could not open file \"" + nstr(fn.GetFullPath()) + "\".\nThis is not a valid OTBM file or it does not exist.");
}

if (ver.otbm != g_gui.getLoadedMapVersion().otbm) {
auto result = g_gui.PopupDialog("Map error", "The loaded map appears to be a OTBM format that is not supported by the editor. Do you still want to attempt to load the map? Caution: this will close your current map!", wxYES | wxNO);
if (result == wxID_NO) {
throw std::runtime_error("Maps of different versions can't be loaded at same time. Save and close your current map and try again.");
}
}

bool success = true;
wxString error;
wxArrayString warnings;
if (g_gui.CloseAllEditors()) {
success = g_gui.LoadVersion(error, warnings);
success = g_gui.loadMapWindow(error, warnings);
if (!success) {
g_gui.PopupDialog("Error", error, wxOK);
auto clientDirectory = ClientAssets::getPath().ToStdString() + "/";
Expand Down
54 changes: 50 additions & 4 deletions source/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@

#include <appearances.pb.h>

namespace InternalGUI {
void logErrorAndSetMessage(const std::string &message, wxString &error) {
spdlog::error(message);
error = message + ". Error:" + error;
g_gui.DestroyLoadBar();
g_gui.unloadMapWindow();
}
} // namespace (internal use only)

const wxEventType EVT_UPDATE_MENUS = wxNewEventType();
const wxEventType EVT_UPDATE_ACTIONS = wxNewEventType();

Expand Down Expand Up @@ -230,14 +239,20 @@ void GUI::discoverDataDirectory(const wxString &existentFile) {
}
}

bool GUI::LoadVersion(wxString &error, wxArrayString &warnings) {
bool GUI::loadMapWindow(wxString &error, wxArrayString &warnings, bool force /* = false*/) {
if (!force && ClientAssets::isLoaded()) {
return true;
}

// There is another version loaded right now, save window layout
g_gui.SavePerspective();

// Disable all rendering so the data is not accessed while reloading
UnnamedRenderingLock();
DestroyPalettes();
DestroyMinimap();
// Destroy the previous window
unloadMapWindow();

bool ret = LoadDataFiles(error, warnings);
if (ret) {
Expand Down Expand Up @@ -283,9 +298,8 @@ bool GUI::LoadDataFiles(wxString &error, wxArrayString &warnings) {
g_gui.SetLoadDone(20, "Loading client assets...");
spdlog::info("Loading appearances");
if (!ClientAssets::loadAppearanceProtobuf(error, warnings)) {
error = "Couldn't load catalog-content.json, error: " + error;
spdlog::error("[GUI::LoadDataFiles] {}", error.ToStdString());
g_gui.DestroyLoadBar();

InternalGUI::logErrorAndSetMessage("Couldn't load catalog-content.json", error);
return false;
}

Expand Down Expand Up @@ -350,6 +364,38 @@ bool GUI::LoadDataFiles(wxString &error, wxArrayString &warnings) {
return true;
}

void GUI::unloadMapWindow() {
UnnamedRenderingLock();
gfx.clear();
current_brush = nullptr;
previous_brush = nullptr;

house_brush = nullptr;
house_exit_brush = nullptr;
waypoint_brush = nullptr;
optional_brush = nullptr;
eraser = nullptr;
normal_door_brush = nullptr;
locked_door_brush = nullptr;
magic_door_brush = nullptr;
quest_door_brush = nullptr;
hatch_door_brush = nullptr;
window_door_brush = nullptr;

g_materials.clear();
g_brushes.clear();
g_items.clear();
gfx.clear();

FileName cdb = ClientAssets::getLocalPath();
cdb.SetFullName("monsters.xml");
g_monsters.saveToXML(cdb);
cdb.SetFullName("npcs.xml");
g_monsters.saveToXML(cdb);
g_monsters.clear();
g_npcs.clear();
}

void GUI::SaveCurrentMap(FileName filename, bool showdialog) {
MapTab* mapTab = GetCurrentMapTab();
if (mapTab) {
Expand Down
8 changes: 7 additions & 1 deletion source/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,9 @@ class GUI {
return m_dataDirectory;
}

bool LoadVersion(wxString &error, wxArrayString &warnings);
// Load/unload a map tabs (takes care of dialogs aswell)
void unloadMapWindow();
bool loadMapWindow(wxString &error, wxArrayString &warnings, bool force = false);

// Centers current view on position
void SetScreenCenterPosition(const Position &position, bool showIndicator = true);
Expand Down Expand Up @@ -364,6 +366,9 @@ class GUI {
void SaveMap();
void SaveMapAs();
bool LoadMap(const FileName &fileName);
const MapVersion &getLoadedMapVersion() const {
return m_loadedMapVersion;
}

protected:
bool LoadDataFiles(wxString &error, wxArrayString &warnings);
Expand Down Expand Up @@ -451,6 +456,7 @@ class GUI {

wxGLContext* OGLContext;

MapVersion m_loadedMapVersion;
EditorMode mode;
bool pasting;

Expand Down
7 changes: 3 additions & 4 deletions source/iomap_otbm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ bool IOMapOTBM::getVersionInfo(NodeFileReadHandle* f, MapVersion &out_ver) {
root->getU16(u16);
root->getU16(u16);
root->getU32(u32);
root->skip(4); // Skip the otb version (deprecated)

return true;
}
Expand Down Expand Up @@ -665,7 +666,7 @@ bool IOMapOTBM::loadMap(Map &map, NodeFileReadHandle &f) {

version.otbm = (MapVersionID)u32;

if (version.otbm > MAP_OTBM_4) {
if (version.otbm > MAP_OTBM_5) {
// Failed to read version
if (g_gui.PopupDialog("Map error", "The loaded map appears to be a OTBM format that is not supported by the editor."
"Do you still want to attempt to load the map?",
Expand Down Expand Up @@ -1528,11 +1529,9 @@ bool IOMapOTBM::saveMap(Map &map, NodeFileWriteHandle &f) {
const IOMapOTBM &self = *this;

FileName tmpName;
MapVersion mapVersion = map.getVersion();

f.addNode(0);
{
f.addU32(2); // Version (deprecated)
f.addU32(MapVersionID::MAP_OTBM_5); // Map version

f.addU16(map.width);
f.addU16(map.height);
Expand Down
2 changes: 1 addition & 1 deletion source/live_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ void LiveClient::parseChangeClientVersion(NetworkMessage &message) {

wxString error;
wxArrayString warnings;
g_gui.LoadVersion(error, warnings);
g_gui.loadMapWindow(error, warnings);

sendReady();
}
Expand Down
2 changes: 1 addition & 1 deletion source/live_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

LiveSocket::LiveSocket() :
cursors(), mapReader(nullptr, 0), mapWriter(),
mapVersion(MapVersion(MAP_OTBM_4)), log(nullptr),
mapVersion(MapVersion()), log(nullptr),
name("User"), password("") {
//
}
Expand Down
2 changes: 1 addition & 1 deletion source/main_menubar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ void MainMenuBar::OnDebugViewDat(wxCommandEvent &WXUNUSED(event)) {
void MainMenuBar::OnReloadDataFiles(wxCommandEvent &WXUNUSED(event)) {
wxString error;
wxArrayString warnings;
g_gui.LoadVersion(error, warnings);
g_gui.loadMapWindow(error, warnings, true);
g_gui.PopupDialog("Error", error, wxOK);
g_gui.ListDialog("Warnings", warnings);
auto clientDirectory = ClientAssets::getPath().ToStdString() + "/";
Expand Down

0 comments on commit b78acf8

Please sign in to comment.