Skip to content

Commit

Permalink
Recreating PR so can do changes to it
Browse files Browse the repository at this point in the history
Style change update
Updated changelog.txt

Co-Authored-By: charles <[email protected]>
  • Loading branch information
Xoduz and OldFoolAgain committed Sep 3, 2023
1 parent fe2ed7d commit 4e3d6dd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
4 changes: 4 additions & 0 deletions source/Changelog.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion source/MultiMul.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "UOPData.hpp"
#include "mapclasses.h"
class TileInfo;
class CTile ;
class CTile;
//==================================================================================================
// MultiItem_st
//==================================================================================================
Expand Down
71 changes: 40 additions & 31 deletions source/regions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "StringUtility.hpp"
#include "ObjectFactory.h"

using namespace std::string_literals ;
using namespace std::string_literals;

#define DEBUG_REGIONS 0

Expand Down Expand Up @@ -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<std::int16_t>(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<SI16>( 3, 0 );

if (output.is_open()) {
for (auto iter = mapResources.begin(); iter != mapResources.end();iter++) {
buffer[0] = iter->oreAmt ;
std::reverse(reinterpret_cast<char*>(buffer.data()),reinterpret_cast<char*>(buffer.data())+2); // Make it big endian
buffer[1] = iter->logAmt ;
std::reverse(reinterpret_cast<char*>(buffer.data())+2,reinterpret_cast<char*>(buffer.data())+4);
buffer[2] = iter->fishAmt ;
std::reverse(reinterpret_cast<char*>(buffer.data())+4,reinterpret_cast<char*>(buffer.data())+6);
if( output.is_open() )
{
for( auto iter = mapResources.begin(); iter != mapResources.end(); iter++ )
{
buffer[0] = iter->oreAmt;
std::reverse( reinterpret_cast<char*>( buffer.data()), reinterpret_cast<char*>( buffer.data()) + 2 ); // Make it big endian
buffer[1] = iter->logAmt;
std::reverse( reinterpret_cast<char*>( buffer.data()) + 2, reinterpret_cast<char*>( buffer.data()) + 4 );
buffer[2] = iter->fishAmt;
std::reverse( reinterpret_cast<char*>( buffer.data()) + 4, reinterpret_cast<char*>( buffer.data()) + 6 );
// Now write it
output.write(reinterpret_cast<char*>(buffer.data()),buffer.size()*2);
output.write( reinterpret_cast<char*>( buffer.data()), buffer.size() * 2 );
}
}
else {
else
{
// Can't save resources
Console.Error( "Failed to open resource.bin for writing" );
}
Expand All @@ -359,36 +362,42 @@ void CMapWorld::SaveResources( UI08 worldNum )
//o------------------------------------------------------------------------------------------------o
void CMapWorld::LoadResources( UI08 worldNum )
{
mapResources = std::vector<MapResource_st>(mapResources.size(),MapResource_st(cwmWorldState->ServerData()->ResOre(),cwmWorldState->ServerData()->ResLogs(),cwmWorldState->ServerData()->ResFish(),BuildTimeValue( static_cast<R32>( cwmWorldState->ServerData()->ResOreTime() )), BuildTimeValue( static_cast<R32>( cwmWorldState->ServerData()->ResLogTime() )),BuildTimeValue( static_cast<R32>( cwmWorldState->ServerData()->ResFishTime() )))) ;
mapResources = std::vector<MapResource_st>( mapResources.size(),
MapResource_st( cwmWorldState->ServerData()->ResOre(), cwmWorldState->ServerData()->ResLogs(),
cwmWorldState->ServerData()->ResFish(), BuildTimeValue( static_cast<R32>( cwmWorldState->ServerData()->ResOreTime() )),
BuildTimeValue( static_cast<R32>( cwmWorldState->ServerData()->ResLogTime() )), BuildTimeValue( static_cast<R32>( 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<std::int16_t>(3,0) ;
auto input = std::ifstream(resourceFile.string(),std::ios::binary) ;
auto buffer = std::vector<SI16>( 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<char*>(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<char*>( 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<char*>(&value),reinterpret_cast<char*>(&value)+2);
std::for_each( buffer.begin(), buffer.end(), []( SI16 &value ) {
std::reverse( reinterpret_cast<char*>( &value ), reinterpret_cast<char*>( &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
Expand Down
3 changes: 2 additions & 1 deletion source/regions.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
}
};
Expand Down

0 comments on commit 4e3d6dd

Please sign in to comment.