From 4e3d6dd7975fdc4c661046c71891a0526c5fb0bd Mon Sep 17 00:00:00 2001 From: Xoduz Date: Sun, 3 Sep 2023 14:43:16 +0800 Subject: [PATCH] Recreating PR so can do changes to it Style change update Updated changelog.txt Co-Authored-By: charles <128108973+oldfoolagain@users.noreply.github.com> --- source/Changelog.txt | 4 +++ source/MultiMul.hpp | 2 +- source/regions.cpp | 71 +++++++++++++++++++++++++------------------- source/regions.h | 3 +- 4 files changed, 47 insertions(+), 33 deletions(-) diff --git a/source/Changelog.txt b/source/Changelog.txt index 13b15602c..eb6f2e40e 100644 --- a/source/Changelog.txt +++ b/source/Changelog.txt @@ -1,3 +1,7 @@ +28/08/2023 - punt + Replaced all routine calls with a specific type of stream ifstream/ofstream to the generic istream/ostream for flexibility + Modernized region resource loading/saving code + 24/08/2023 - punt Fixed a memory leak with regions.cpp, related to worldsaves diff --git a/source/MultiMul.hpp b/source/MultiMul.hpp index 39e30379d..db7c23200 100644 --- a/source/MultiMul.hpp +++ b/source/MultiMul.hpp @@ -14,7 +14,7 @@ #include "UOPData.hpp" #include "mapclasses.h" class TileInfo; -class CTile ; +class CTile; //================================================================================================== // MultiItem_st //================================================================================================== diff --git a/source/regions.cpp b/source/regions.cpp index fb24f0218..20ba9d8e2 100644 --- a/source/regions.cpp +++ b/source/regions.cpp @@ -9,7 +9,7 @@ #include "StringUtility.hpp" #include "ObjectFactory.h" -using namespace std::string_literals ; +using namespace std::string_literals; #define DEBUG_REGIONS 0 @@ -329,23 +329,26 @@ MapResource_st& CMapWorld::GetResource( SI16 x, SI16 y ) //o------------------------------------------------------------------------------------------------o void CMapWorld::SaveResources( UI08 worldNum ) { - auto resourceFile = std::filesystem::path(cwmWorldState->ServerData()->Directory( CSDDP_SHARED ) + "resource["s + std::to_string( worldNum ) + "].bin"s); - auto output = std::ofstream(resourceFile.string(), std::ios::binary); - auto buffer = std::vector(3,0) ; + auto resourceFile = std::filesystem::path( cwmWorldState->ServerData()->Directory( CSDDP_SHARED ) + "resource["s + std::to_string( worldNum ) + "].bin"s ); + auto output = std::ofstream( resourceFile.string(), std::ios::binary ); + auto buffer = std::vector( 3, 0 ); - if (output.is_open()) { - for (auto iter = mapResources.begin(); iter != mapResources.end();iter++) { - buffer[0] = iter->oreAmt ; - std::reverse(reinterpret_cast(buffer.data()),reinterpret_cast(buffer.data())+2); // Make it big endian - buffer[1] = iter->logAmt ; - std::reverse(reinterpret_cast(buffer.data())+2,reinterpret_cast(buffer.data())+4); - buffer[2] = iter->fishAmt ; - std::reverse(reinterpret_cast(buffer.data())+4,reinterpret_cast(buffer.data())+6); + if( output.is_open() ) + { + for( auto iter = mapResources.begin(); iter != mapResources.end(); iter++ ) + { + buffer[0] = iter->oreAmt; + std::reverse( reinterpret_cast( buffer.data()), reinterpret_cast( buffer.data()) + 2 ); // Make it big endian + buffer[1] = iter->logAmt; + std::reverse( reinterpret_cast( buffer.data()) + 2, reinterpret_cast( buffer.data()) + 4 ); + buffer[2] = iter->fishAmt; + std::reverse( reinterpret_cast( buffer.data()) + 4, reinterpret_cast( buffer.data()) + 6 ); // Now write it - output.write(reinterpret_cast(buffer.data()),buffer.size()*2); + output.write( reinterpret_cast( buffer.data()), buffer.size() * 2 ); } } - else { + else + { // Can't save resources Console.Error( "Failed to open resource.bin for writing" ); } @@ -359,36 +362,42 @@ void CMapWorld::SaveResources( UI08 worldNum ) //o------------------------------------------------------------------------------------------------o void CMapWorld::LoadResources( UI08 worldNum ) { - mapResources = std::vector(mapResources.size(),MapResource_st(cwmWorldState->ServerData()->ResOre(),cwmWorldState->ServerData()->ResLogs(),cwmWorldState->ServerData()->ResFish(),BuildTimeValue( static_cast( cwmWorldState->ServerData()->ResOreTime() )), BuildTimeValue( static_cast( cwmWorldState->ServerData()->ResLogTime() )),BuildTimeValue( static_cast( cwmWorldState->ServerData()->ResFishTime() )))) ; + mapResources = std::vector( mapResources.size(), + MapResource_st( cwmWorldState->ServerData()->ResOre(), cwmWorldState->ServerData()->ResLogs(), + cwmWorldState->ServerData()->ResFish(), BuildTimeValue( static_cast( cwmWorldState->ServerData()->ResOreTime() )), + BuildTimeValue( static_cast( cwmWorldState->ServerData()->ResLogTime() )), BuildTimeValue( static_cast( cwmWorldState->ServerData()->ResFishTime() )))); - auto resourceFile = std::filesystem::path(cwmWorldState->ServerData()->Directory( CSDDP_SHARED ) + "resource["s + oldstrutil::number( worldNum ) + "].bin"s); + auto resourceFile = std::filesystem::path( cwmWorldState->ServerData()->Directory( CSDDP_SHARED ) + "resource["s + oldstrutil::number( worldNum ) + "].bin"s ); // The data is grouped as three shorts (for each resource), so we read in that format - auto buffer = std::vector(3,0) ; - auto input = std::ifstream(resourceFile.string(),std::ios::binary) ; + auto buffer = std::vector( 3, 0 ); + auto input = std::ifstream( resourceFile.string(), std::ios::binary ); // We want to get the iteratro for the first mapResources ; - auto iter = mapResources.begin() ; - if (input.is_open()){ - while(input.good() && !input.eof() && iter != mapResources.end()){ - input.read(reinterpret_cast(buffer.data()),buffer.size()*2) ; - if (input.gcount() != buffer.size()*2) { + auto iter = mapResources.begin(); + if( input.is_open() ) + { + while( input.good() && !input.eof() && iter != mapResources.end() ) + { + input.read( reinterpret_cast( buffer.data() ), buffer.size() * 2 ); + if( input.gcount() != buffer.size() * 2 ) + { // We had issues reading the full amount, break out of this break; } + // For whatever reason the resources are stored in big endidan, which on int/arm machines , we need little endian, so reverse them - std::for_each(buffer.begin(), buffer.end(), [](std::int16_t &value){ - std::reverse(reinterpret_cast(&value),reinterpret_cast(&value)+2); + std::for_each( buffer.begin(), buffer.end(), []( SI16 &value ) { + std::reverse( reinterpret_cast( &value ), reinterpret_cast( &value ) + 2 ); }); + // Now set the values - (*iter).oreAmt = buffer[0] ; - (*iter).logAmt = buffer[1] ; - (*iter).fishAmt = buffer[2] ; - iter++ ; + ( *iter ).oreAmt = buffer[0]; + ( *iter ).logAmt = buffer[1]; + ( *iter ).fishAmt = buffer[2]; + iter++; } } - - } //o------------------------------------------------------------------------------------------------o diff --git a/source/regions.h b/source/regions.h index 050b149d6..87de396e9 100644 --- a/source/regions.h +++ b/source/regions.h @@ -28,7 +28,8 @@ struct MapResource_st SI16 fishAmt; UI32 fishTime; - MapResource_st(std::int16_t defOre = 0, std::int16_t defLog = 0, std::int16_t defFish = 0, std::uint32_t defOreTime = 0, std::uint32_t defLogTIme = 0, std::uint32_t defFishTIme=0) : oreAmt( defOre ), oreTime( defOreTime ), logAmt( defLog ), logTime( defLogTIme ), fishAmt( defFish ), fishTime( defFishTIme ) + MapResource_st( SI16 defOre = 0, SI16 defLog = 0, SI16 defFish = 0, UI32 defOreTime = 0, UI32 defLogTIme = 0, UI32 defFishTIme = 0 ) : + oreAmt( defOre ), oreTime( defOreTime ), logAmt( defLog ), logTime( defLogTIme ), fishAmt( defFish ), fishTime( defFishTIme ) { } };