enhanced PlusROM, now reads id and nick from Settings

added PlusROM id generation to PlusRomsSetupDialog
This commit is contained in:
Thomas Jentzsch 2021-10-01 12:24:53 +02:00
parent 7c8f426a1b
commit 793b554f53
5 changed files with 62 additions and 27 deletions

View File

@ -56,9 +56,11 @@ CartridgeEnhanced::CartridgeEnhanced(const ByteBuffer& image, size_t size,
// space will be filled with 0's from above // space will be filled with 0's from above
std::copy_n(image.get(), std::min(mySize, size), myImage.get()); std::copy_n(image.get(), std::min(mySize, size), myImage.get());
myPlusROM = make_unique<PlusROM>(mySettings);
// Determine whether we have a PlusROM cart // Determine whether we have a PlusROM cart
// PlusROM needs to call peek() method, so disable direct peeks // PlusROM needs to call peek() method, so disable direct peeks
if(myPlusROM.initialize(myImage, mySize)) if(myPlusROM->initialize(myImage, mySize))
myDirectPeek = false; myDirectPeek = false;
} }
@ -142,7 +144,7 @@ void CartridgeEnhanced::reset()
// Upon reset we switch to the reset bank // Upon reset we switch to the reset bank
bank(startBank()); bank(startBank());
if (myPlusROM.isValid()) myPlusROM.reset(); if (myPlusROM->isValid()) myPlusROM->reset();
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -151,10 +153,10 @@ uInt8 CartridgeEnhanced::peek(uInt16 address)
const uInt16 peekAddress = address; const uInt16 peekAddress = address;
// Is this a PlusROM? // Is this a PlusROM?
if(myPlusROM.isValid()) if(myPlusROM->isValid())
{ {
uInt8 value = 0; uInt8 value = 0;
if(myPlusROM.peekHotspot(address, value)) if(myPlusROM->peekHotspot(address, value))
return value; return value;
} }
@ -188,7 +190,7 @@ uInt8 CartridgeEnhanced::peek(uInt16 address)
bool CartridgeEnhanced::poke(uInt16 address, uInt8 value) bool CartridgeEnhanced::poke(uInt16 address, uInt8 value)
{ {
// Is this a PlusROM? // Is this a PlusROM?
if(myPlusROM.isValid() && myPlusROM.pokeHotspot(address, value)) if(myPlusROM->isValid() && myPlusROM->pokeHotspot(address, value))
return true; return true;
// Switch banks if necessary // Switch banks if necessary
@ -384,7 +386,7 @@ bool CartridgeEnhanced::save(Serializer& out) const
if(myRamSize > 0) if(myRamSize > 0)
out.putByteArray(myRAM.get(), myRamSize); out.putByteArray(myRAM.get(), myRamSize);
if(myPlusROM.isValid() && !myPlusROM.save(out)) if(myPlusROM->isValid() && !myPlusROM->save(out))
return false; return false;
} }
@ -406,7 +408,7 @@ bool CartridgeEnhanced::load(Serializer& in)
if(myRamSize > 0) if(myRamSize > 0)
in.getByteArray(myRAM.get(), myRamSize); in.getByteArray(myRAM.get(), myRamSize);
if(myPlusROM.isValid() && !myPlusROM.load(in)) if(myPlusROM->isValid() && !myPlusROM->load(in))
return false; return false;
} }
catch(...) catch(...)

View File

@ -163,7 +163,7 @@ class CartridgeEnhanced : public Cartridge
@return Whether this is actually a PlusROM cart @return Whether this is actually a PlusROM cart
*/ */
bool isPlusROM() const override { return myPlusROM.isValid(); } bool isPlusROM() const override { return myPlusROM->isValid(); }
/** /**
Get the hotspot in ROM address space. Get the hotspot in ROM address space.
@ -234,7 +234,7 @@ class CartridgeEnhanced : public Cartridge
size_t mySize{0}; size_t mySize{0};
// Handle PlusROM functionality, if available // Handle PlusROM functionality, if available
PlusROM myPlusROM; unique_ptr<PlusROM> myPlusROM;
protected: protected:
// The mask for 6507 address space // The mask for 6507 address space

View File

@ -24,6 +24,7 @@
#include "PlusROM.hxx" #include "PlusROM.hxx"
#include "Logger.hxx" #include "Logger.hxx"
#include "Version.hxx" #include "Version.hxx"
#include "Settings.hxx"
#if defined(HTTP_LIB_SUPPORT) #if defined(HTTP_LIB_SUPPORT)
#include "http_lib.hxx" #include "http_lib.hxx"
@ -157,6 +158,12 @@ class PlusROMRequest {
PlusROMRequest& operator=(PlusROMRequest&&) = delete; PlusROMRequest& operator=(PlusROMRequest&&) = delete;
}; };
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PlusROM::PlusROM(const Settings& settings)
: mySettings(settings)
{
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool PlusROM::initialize(const ByteBuffer& image, size_t size) bool PlusROM::initialize(const ByteBuffer& image, size_t size)
{ {
@ -337,24 +344,29 @@ void PlusROM::send()
return; return;
} }
auto request = make_shared<PlusROMRequest>( const string id = mySettings.getString("plusroms.id");
PlusROMRequest::Destination(myHost, myPath), if(id != EmptyString)
PlusROMRequest::PlusStoreId("DirtyHairy", "0123456789012345678912"), {
myTxBuffer.data(), const string nick = mySettings.getString("plusroms.nick");
myTxPos auto request = make_shared<PlusROMRequest>(
); PlusROMRequest::Destination(myHost, myPath),
PlusROMRequest::PlusStoreId(nick, id),
myTxBuffer.data(),
myTxPos
);
myTxPos = 0; myTxPos = 0;
// We push to the back in order to avoid reverse_iterator in receive() // We push to the back in order to avoid reverse_iterator in receive()
myPendingRequests.push_back(request); myPendingRequests.push_back(request);
// The lambda will retain a copy of the shared_ptr that is alive as long as the // The lambda will retain a copy of the shared_ptr that is alive as long as the
// thread is running. Thus, the request can only be destructed once the thread has // thread is running. Thus, the request can only be destructed once the thread has
// finished, and we can safely evict it from the deque at any time. // finished, and we can safely evict it from the deque at any time.
std::thread thread([=](){ request->execute(); }); std::thread thread([=]() { request->execute(); });
thread.detach(); thread.detach();
}
#endif #endif
} }

View File

@ -18,6 +18,8 @@
#ifndef PLUSROM_HXX #ifndef PLUSROM_HXX
#define PLUSROM_HXX #define PLUSROM_HXX
class Settings;
#include <deque> #include <deque>
#include "bspf.hxx" #include "bspf.hxx"
@ -50,7 +52,7 @@ class PlusROMRequest;
class PlusROM : public Serializable class PlusROM : public Serializable
{ {
public: public:
PlusROM() = default; PlusROM(const Settings& settings);
~PlusROM() override = default; ~PlusROM() override = default;
public: public:
@ -133,6 +135,7 @@ class PlusROM : public Serializable
void send(); void send();
private: private:
const Settings& mySettings;
bool myIsPlusROM{false}; bool myIsPlusROM{false};
string myHost; string myHost;
string myPath; string myPath;

View File

@ -21,6 +21,7 @@
#include "PlusRomsSetupDialog.hxx" #include "PlusRomsSetupDialog.hxx"
static const int MAX_NICK_LEN = 16; static const int MAX_NICK_LEN = 16;
static const int ID_LEN = 32 - 2; // WE prefix added later
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PlusRomsSetupDialog::PlusRomsSetupDialog(OSystem& osystem, DialogContainer& parent, PlusRomsSetupDialog::PlusRomsSetupDialog(OSystem& osystem, DialogContainer& parent,
@ -45,25 +46,26 @@ void PlusRomsSetupDialog::loadConfig()
void PlusRomsSetupDialog::saveConfig() void PlusRomsSetupDialog::saveConfig()
{ {
instance().settings().setValue("plusroms.nick", getResult(0)); instance().settings().setValue("plusroms.nick", getResult(0));
if(instance().settings().getString("plusroms.id") == EmptyString)
instance().settings().setValue("plusroms.id", "12345678901234567890123456789012"); // TODO: generate in PlusROM class
// Note: The user can cancel, so the existance of an ID must be checked (and generated if not existing) when transmitting scores
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void PlusRomsSetupDialog::handleCommand(CommandSender* sender, int cmd, void PlusRomsSetupDialog::handleCommand(CommandSender* sender, int cmd,
int data, int id) int data, int id)
{ {
bool exit = false;
switch(cmd) switch(cmd)
{ {
case GuiObject::kOKCmd: case GuiObject::kOKCmd:
case EditableWidget::kAcceptCmd: case EditableWidget::kAcceptCmd:
saveConfig(); saveConfig();
instance().eventHandler().leaveMenuMode(); instance().eventHandler().leaveMenuMode();
exit = true;
break; break;
case kCloseCmd: case kCloseCmd:
instance().eventHandler().leaveMenuMode(); instance().eventHandler().leaveMenuMode();
exit = true;
break; break;
case EditableWidget::kCancelCmd: case EditableWidget::kCancelCmd:
@ -73,4 +75,20 @@ void PlusRomsSetupDialog::handleCommand(CommandSender* sender, int cmd,
InputTextDialog::handleCommand(sender, cmd, data, id); InputTextDialog::handleCommand(sender, cmd, data, id);
break; break;
} }
// Make sure there always is an id
if(exit)
{
if(instance().settings().getString("plusroms.id") == EmptyString)
{
const char* HEX_DIGITS = "0123456789ABCDEF";
char id_chr[ID_LEN];
srand(time(NULL));
for(int i = 0; i < ID_LEN; i++)
id_chr[i] = HEX_DIGITS[(rand() % 16)];
std::string id_str(id_chr, ID_LEN);
instance().settings().setValue("plusroms.id", id_str);
}
}
} }