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

View File

@ -163,7 +163,7 @@ class CartridgeEnhanced : public Cartridge
@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.
@ -234,7 +234,7 @@ class CartridgeEnhanced : public Cartridge
size_t mySize{0};
// Handle PlusROM functionality, if available
PlusROM myPlusROM;
unique_ptr<PlusROM> myPlusROM;
protected:
// The mask for 6507 address space

View File

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

View File

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

View File

@ -21,6 +21,7 @@
#include "PlusRomsSetupDialog.hxx"
static const int MAX_NICK_LEN = 16;
static const int ID_LEN = 32 - 2; // WE prefix added later
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
PlusRomsSetupDialog::PlusRomsSetupDialog(OSystem& osystem, DialogContainer& parent,
@ -45,25 +46,26 @@ void PlusRomsSetupDialog::loadConfig()
void PlusRomsSetupDialog::saveConfig()
{
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,
int data, int id)
{
bool exit = false;
switch(cmd)
{
case GuiObject::kOKCmd:
case EditableWidget::kAcceptCmd:
saveConfig();
instance().eventHandler().leaveMenuMode();
exit = true;
break;
case kCloseCmd:
instance().eventHandler().leaveMenuMode();
exit = true;
break;
case EditableWidget::kCancelCmd:
@ -73,4 +75,20 @@ void PlusRomsSetupDialog::handleCommand(CommandSender* sender, int cmd,
InputTextDialog::handleCommand(sender, cmd, data, id);
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);
}
}
}