1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-12-28 22:30:30 +00:00

Implements a basic ROM installation loop.

Albeit that I need to figure out how layouts work to keep that request view at least centred.
This commit is contained in:
Thomas Harte 2020-06-02 23:35:01 -04:00
parent f215405beb
commit 11c28357a1
3 changed files with 214 additions and 54 deletions

View File

@ -3,11 +3,9 @@
#include <QStandardPaths>
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "timer.h"
#include "../../Analyser/Static/StaticAnalyser.hpp"
#include "../../Machines/Utility/MachineForTarget.hpp"
#include "../../Numeric/CRC.hpp"
/*
General Qt implementation notes:
@ -41,6 +39,11 @@ MainWindow::MainWindow(QWidget *parent)
// TODO: not until there's actually something to display.
timerThread->start();
qTimer->start();
// Hide the missing ROMs box unless or until it's needed; grab the text it
// began with as a prefix for future mutation.
ui->missingROMsBox->setVisible(false);
romRequestBaseText = ui->missingROMsBox->toPlainText();
}
void MainWindow::createActions() {
@ -65,51 +68,12 @@ void MainWindow::createActions() {
void MainWindow::open() {
QString fileName = QFileDialog::getOpenFileName(this);
if(!fileName.isEmpty()) {
const auto targets = Analyser::Static::GetTargets(fileName.toStdString());
targets = Analyser::Static::GetTargets(fileName.toStdString());
if(targets.empty()) {
qDebug() << "Not media:" << fileName;
} else {
qDebug() << "Got media:" << fileName;
const QStringList appDataLocations = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
ROMMachine::ROMFetcher rom_fetcher = [&appDataLocations]
(const std::vector<ROMMachine::ROM> &roms) -> std::vector<std::unique_ptr<std::vector<uint8_t>>> {
std::vector<std::unique_ptr<std::vector<uint8_t>>> results;
for(const auto &rom: roms) {
FILE *file = nullptr;
for(const auto &path: appDataLocations) {
const std::string source = path.toStdString() + "/ROMImages/" + rom.machine_name + "/" + rom.file_name;
const std::string nativeSource = QDir::toNativeSeparators(QString::fromStdString(source)).toStdString();
qDebug() << "Taking a run at " << nativeSource.c_str();
file = fopen(nativeSource.c_str(), "rb");
if(file) break;
}
if(file) {
auto data = std::make_unique<std::vector<uint8_t>>();
fseek(file, 0, SEEK_END);
data->resize(std::ftell(file));
fseek(file, 0, SEEK_SET);
size_t read = fread(data->data(), 1, data->size(), file);
fclose(file);
if(read == data->size()) {
results.push_back(std::move(data));
} else {
results.push_back(nullptr);
}
} else {
results.push_back(nullptr);
}
}
return results;
};
Machine::Error error;
std::unique_ptr<Machine::DynamicMachine> machine(Machine::MachineForTargets(targets, rom_fetcher, error));
launchMachine();
}
}
}
@ -121,3 +85,158 @@ MainWindow::~MainWindow() {
while(timerThread->isRunning());
}
// MARK: Machine launch.
namespace {
std::unique_ptr<std::vector<uint8_t>> fileContentsAndClose(FILE *file) {
auto data = std::make_unique<std::vector<uint8_t>>();
fseek(file, 0, SEEK_END);
data->resize(std::ftell(file));
fseek(file, 0, SEEK_SET);
size_t read = fread(data->data(), 1, data->size(), file);
fclose(file);
if(read == data->size()) {
return data;
}
return nullptr;
}
}
void MainWindow::launchMachine() {
const QStringList appDataLocations = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
missingRoms.clear();
ROMMachine::ROMFetcher rom_fetcher = [&appDataLocations, this]
(const std::vector<ROMMachine::ROM> &roms) -> std::vector<std::unique_ptr<std::vector<uint8_t>>> {
std::vector<std::unique_ptr<std::vector<uint8_t>>> results;
for(const auto &rom: roms) {
FILE *file = nullptr;
for(const auto &path: appDataLocations) {
const std::string source = path.toStdString() + "/ROMImages/" + rom.machine_name + "/" + rom.file_name;
const std::string nativeSource = QDir::toNativeSeparators(QString::fromStdString(source)).toStdString();
qDebug() << "Taking a run at " << nativeSource.c_str();
file = fopen(nativeSource.c_str(), "rb");
if(file) break;
}
if(file) {
auto data = fileContentsAndClose(file);
if(data) {
results.push_back(std::move(data));
continue;
}
}
results.push_back(nullptr);
missingRoms.push_back(rom);
}
return results;
};
Machine::Error error;
std::unique_ptr<Machine::DynamicMachine> machine(Machine::MachineForTargets(targets, rom_fetcher, error));
switch(error) {
default:
ui->missingROMsBox->setVisible(false);
uiPhase = UIPhase::RunningMachine;
// TODO: launch machine.
break;
case Machine::Error::MissingROM: {
ui->missingROMsBox->setVisible(true);
uiPhase = UIPhase::RequestingROMs;
// Populate request text.
QString requestText = romRequestBaseText;
size_t index = 0;
for(const auto rom: missingRoms) {
requestText += "";
requestText += rom.descriptive_name.c_str();
++index;
if(index == missingRoms.size()) {
requestText += ".\n";
continue;
}
if(index == missingRoms.size() - 1) {
requestText += "; and\n";
continue;
}
requestText += ";\n";
}
ui->missingROMsBox->setPlainText(requestText);
} break;
}
}
void MainWindow::dragEnterEvent(QDragEnterEvent* event) {
// Always accept dragged files.
if(event->mimeData()->hasUrls())
event->accept();
}
void MainWindow::dropEvent(QDropEvent* event) {
if(!event->mimeData()->hasUrls()) {
return;
}
event->accept();
switch(uiPhase) {
case UIPhase::NoFileSelected:
// Treat exactly as a File -> Open... .
break;
case UIPhase::RequestingROMs: {
// Attempt to match up the dragged files to the requested ROM list;
// if and when they match, copy to a writeable QStandardPaths::AppDataLocation
// and try launchMachine() again.
bool foundROM = false;
const auto appDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).toStdString();
for(const auto &url: event->mimeData()->urls()) {
const char *const name = url.toLocalFile().toUtf8();
FILE *const file = fopen(name, "rb");
const auto contents = fileContentsAndClose(file);
if(!contents) continue;
CRC::CRC32 generator;
const uint32_t crc = generator.compute_crc(*contents);
for(const auto &rom: missingRoms) {
if(std::find(rom.crc32s.begin(), rom.crc32s.end(), crc) != rom.crc32s.end()) {
foundROM = true;
// Ensure the destination folder exists.
const std::string path = appDataLocation + "/ROMImages/" + rom.machine_name;
QDir dir(QString::fromStdString(path));
if (!dir.exists())
dir.mkpath(".");
// Write into place.
const std::string destination = QDir::toNativeSeparators(QString::fromStdString(path+ "/" + rom.file_name)).toStdString();
FILE *const target = fopen(destination.c_str(), "wb");
fwrite(contents->data(), 1, contents->size(), target);
fclose(target);
}
}
}
if(foundROM) launchMachine();
} break;
case UIPhase::RunningMachine:
// Attempt to insert into the running machine.
qDebug() << "Should start machine";
break;
}
}

View File

@ -4,6 +4,10 @@
#include <QMainWindow>
#include <memory>
#include "timer.h"
#include "ui_mainwindow.h"
#include "../../Analyser/Static/StaticAnalyser.hpp"
#include "../../Machines/Utility/MachineForTarget.hpp"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@ -24,6 +28,19 @@ class MainWindow : public QMainWindow {
std::unique_ptr<QThread> timerThread;
std::unique_ptr<Timer> timer;
// Initial setup stuff.
Analyser::Static::TargetList targets;
enum class UIPhase {
NoFileSelected, RequestingROMs, RunningMachine
} uiPhase = UIPhase::NoFileSelected;
void launchMachine();
QString romRequestBaseText;
std::vector<ROMMachine::ROM> missingRoms;
// File drag and drop is supported.
void dragEnterEvent(QDragEnterEvent* event) override;
void dropEvent(QDropEvent* event) override;
private slots:
void open();

View File

@ -23,18 +23,42 @@
<string>MainWindow</string>
</property>
<widget class="QOpenGLWidget" name="openGLWidget">
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>22</height>
</rect>
</property>
<widget class="QPlainTextEdit" name="missingROMsBox">
<property name="geometry">
<rect>
<x>170</x>
<y>170</y>
<width>421</width>
<height>231</height>
</rect>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Maximum" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="acceptDrops">
<bool>true</bool>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="plainText">
<string>Clock Signal requires you to provide images of the system ROMs for this machine. They will be stored permanently; you need do this only once.
Please drag and drop the following over this window:
</string>
</property>
</widget>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionDo_this">
<property name="text">
<string>Do this</string>
</property>
</action>
</widget>
<resources/>
<connections/>