Skip to content

Commit e6c209d

Browse files
committed
noggit: mpq: refacror local file save-load system
1 parent 1aa452a commit e6c209d

File tree

5 files changed

+32
-10
lines changed

5 files changed

+32
-10
lines changed

src/noggit/MPQ.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ MPQFile::MPQFile(std::string const& filename)
180180
: eof(true)
181181
, pointer(0)
182182
, External(false)
183-
, _filename(noggit::mpq::normalized_filename(filename))
183+
, _filename(filename)
184184
, _disk_path (getDiskPath (_filename))
185185
{
186186
if (filename.empty())
@@ -358,5 +358,26 @@ namespace noggit
358358
);
359359
return filename;
360360
}
361+
std::string uni_path (std::string filename)
362+
{
363+
try
364+
{
365+
// Check without normalization
366+
std::filesystem::path local_path(getDiskPath(filename));
367+
368+
if (std::filesystem::exists(local_path) || existsInMPQ (filename))
369+
{
370+
return filename;
371+
}
372+
else
373+
{
374+
return normalized_filename(filename);
375+
}
376+
}
377+
catch (const std::exception& e)
378+
{
379+
throw std::runtime_error ("Error! File " + filename + " does not exists");
380+
}
381+
}
361382
}
362383
}

src/noggit/MPQ.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,6 @@ namespace noggit
107107
{
108108
std::string normalized_filename (std::string filename);
109109
std::string normalized_filename_insane (std::string filename);
110+
std::string uni_path (std::string filename);
110111
}
111112
}

src/noggit/World.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ bool World::IsEditableWorld(int pMapId)
5858
}
5959

6060
std::stringstream ssfilename;
61-
ssfilename << "World\\Maps\\" << lMapName << "\\" << lMapName << ".wdt";
61+
ssfilename << noggit::mpq::uni_path("World/Maps/" + lMapName + "/" + lMapName + ".wdt");
6262

6363
if (!MPQFile::exists(ssfilename.str()))
6464
{

src/noggit/map_horizon.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace noggit
9292
map_horizon::map_horizon(const std::string& basename, const MapIndex * const index)
9393
{
9494
std::stringstream filename;
95-
filename << "World\\Maps\\" << basename << "\\" << basename << ".wdl";
95+
filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + ".wdl");
9696
_filename = filename.str();
9797

9898
if (!MPQFile::exists(_filename))

src/noggit/map_index.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ MapIndex::MapIndex (const std::string &pBasename, int map_id, World* world)
3232
_loading_radius = NoggitSettings.value("loading_radius", 1).toInt();
3333

3434
std::stringstream filename;
35-
filename << "World\\Maps\\" << basename << "\\" << basename << ".wdt";
35+
filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + ".wdt");
3636

3737
MPQFile theFile(filename.str());
3838

@@ -87,7 +87,7 @@ MapIndex::MapIndex (const std::string &pBasename, int map_id, World* world)
8787
theFile.seekRelative(4);
8888

8989
std::stringstream adt_filename;
90-
adt_filename << "World\\Maps\\" << basename << "\\" << basename << "_" << i << "_" << j << ".adt";
90+
adt_filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + "_" + std::to_string(i) + "_" + std::to_string(j) + ".adt");
9191

9292
mTiles[j][i].tile = nullptr;
9393
mTiles[j][i].onDisc = MPQFile::existsOnDisk(adt_filename.str());
@@ -149,7 +149,7 @@ void MapIndex::saveall (World* world)
149149
void MapIndex::save()
150150
{
151151
std::stringstream filename;
152-
filename << "World\\Maps\\" << basename << "\\" << basename << ".wdt";
152+
filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + ".wdt");
153153

154154
//NOGGIT_LOG << "Saving WDT \"" << filename << "\"." << std::endl;
155155

@@ -317,7 +317,7 @@ MapTile* MapIndex::loadTile(const tile_index& tile, bool reloading)
317317
}
318318

319319
std::stringstream filename;
320-
filename << "World\\Maps\\" << basename << "\\" << basename << "_" << tile.x << "_" << tile.z << ".adt";
320+
filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + "_" + std::to_string(tile.x) + "_" + std::to_string(tile.z) + ".adt");
321321

322322
if (!MPQFile::exists(filename.str()))
323323
{
@@ -610,7 +610,7 @@ uid_fix_status MapIndex::fixUIDs (World* world, bool cancel_on_model_loading_err
610610
}
611611

612612
std::stringstream filename;
613-
filename << "World\\Maps\\" << basename << "\\" << basename << "_" << x << "_" << z << ".adt";
613+
filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + "_" + std::to_string(x) + "_" + std::to_string(z) + ".adt");
614614

615615
if (!MPQFile::exists(filename.str()))
616616
{
@@ -860,7 +860,7 @@ uid_fix_status MapIndex::fixUIDs (World* world, bool cancel_on_model_loading_err
860860
// that shouldn't be there to avoid creating new duplicates
861861

862862
std::stringstream filename;
863-
filename << "World\\Maps\\" << basename << "\\" << basename << "_" << x << "_" << z << ".adt";
863+
filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + "_" + std::to_string(x) + "_" + std::to_string(z) + ".adt");
864864

865865
// load the tile without the models
866866
MapTile tile(x, z, filename.str(), mBigAlpha, false, use_mclq_green_lava(), false, world, tile_mode::uid_fix_all);
@@ -900,7 +900,7 @@ void MapIndex::searchMaxUID()
900900
}
901901

902902
std::stringstream filename;
903-
filename << "World\\Maps\\" << basename << "\\" << basename << "_" << x << "_" << z << ".adt";
903+
filename << noggit::mpq::uni_path("World/Maps/" + basename + "/" + basename + "_" + std::to_string(x) + "_" + std::to_string(z) + ".adt");
904904
highestGUID = std::max(highestGUID, getHighestGUIDFromFile(filename.str()));
905905
}
906906
}

0 commit comments

Comments
 (0)