Attempt detecting BOM before parsing parameters
This commit is contained in:
@@ -17,6 +17,7 @@ extern "C"
|
|||||||
#include <86box/config.h>
|
#include <86box/config.h>
|
||||||
#include <86box/qt-glslp-parser.h>
|
#include <86box/qt-glslp-parser.h>
|
||||||
#include <86box/path.h>
|
#include <86box/path.h>
|
||||||
|
#include <86box/plat.h>
|
||||||
|
|
||||||
extern void startblit();
|
extern void startblit();
|
||||||
extern void endblit();
|
extern void endblit();
|
||||||
@@ -81,14 +82,38 @@ static int endswith(const char *str, const char *ext) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
glsl_detect_bom(const char *fn)
|
||||||
|
{
|
||||||
|
FILE *fp;
|
||||||
|
unsigned char bom[4] = { 0, 0, 0, 0 };
|
||||||
|
|
||||||
|
fp = plat_fopen(fn, "rb");
|
||||||
|
if (fp == NULL)
|
||||||
|
return 0;
|
||||||
|
(void) !fread(bom, 1, 3, fp);
|
||||||
|
if (bom[0] == 0xEF && bom[1] == 0xBB && bom[2] == 0xBF) {
|
||||||
|
fclose(fp);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
fclose(fp);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static char *load_file(const char *fn) {
|
static char *load_file(const char *fn) {
|
||||||
FILE *f = fopen(fn, "rb");
|
int bom = glsl_detect_bom(fn);
|
||||||
|
FILE *f = plat_fopen(fn, "rb");
|
||||||
if (!f)
|
if (!f)
|
||||||
return 0;
|
return 0;
|
||||||
fseek(f, 0, SEEK_END);
|
fseek(f, 0, SEEK_END);
|
||||||
long fsize = ftell(f);
|
long fsize = ftell(f);
|
||||||
fseek(f, 0, SEEK_SET);
|
fseek(f, 0, SEEK_SET);
|
||||||
|
|
||||||
|
if (bom) {
|
||||||
|
fsize -= 3;
|
||||||
|
fseek(f, 3, SEEK_SET);
|
||||||
|
}
|
||||||
|
|
||||||
char *data = (char*)malloc(fsize + 1);
|
char *data = (char*)malloc(fsize + 1);
|
||||||
|
|
||||||
fread(data, fsize, 1, f);
|
fread(data, fsize, 1, f);
|
||||||
@@ -131,12 +156,15 @@ static int get_parameters(glslp_t *glsl) {
|
|||||||
int i;
|
int i;
|
||||||
struct parameter p;
|
struct parameter p;
|
||||||
for (i = 0; i < glsl->num_shaders; ++i) {
|
for (i = 0; i < glsl->num_shaders; ++i) {
|
||||||
|
char line[1024];
|
||||||
struct shader *shader = &glsl->shaders[i];
|
struct shader *shader = &glsl->shaders[i];
|
||||||
FILE *f = fopen(shader->shader_fn, "rb");
|
int bom = glsl_detect_bom(shader->shader_fn);
|
||||||
|
FILE *f = plat_fopen(shader->shader_fn, "rb");
|
||||||
if (!f)
|
if (!f)
|
||||||
return 0;
|
return 0;
|
||||||
|
if (bom) {
|
||||||
char line[1024];
|
fseek(f, 3, SEEK_SET);
|
||||||
|
}
|
||||||
while (fgets(line, sizeof(line) - 1, f) && glsl->num_parameters < MAX_PARAMETERS) {
|
while (fgets(line, sizeof(line) - 1, f) && glsl->num_parameters < MAX_PARAMETERS) {
|
||||||
int num = sscanf(line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f", p.id, p.description,
|
int num = sscanf(line, "#pragma parameter %63s \"%63[^\"]\" %f %f %f %f", p.id, p.description,
|
||||||
&p.default_value, &p.min, &p.max, &p.step);
|
&p.default_value, &p.min, &p.max, &p.step);
|
||||||
|
|||||||
@@ -63,6 +63,7 @@ OpenGLShaderManagerDialog::OpenGLShaderManagerDialog(QWidget *parent)
|
|||||||
} else {
|
} else {
|
||||||
ui->buttonConfigure->setEnabled(false);
|
ui->buttonConfigure->setEnabled(false);
|
||||||
}
|
}
|
||||||
|
ui->buttonAdd->setDisabled(ui->shaderListWidget->count() >= MAX_USER_SHADERS);
|
||||||
} else {
|
} else {
|
||||||
ui->buttonRemove->setDisabled(true);
|
ui->buttonRemove->setDisabled(true);
|
||||||
ui->buttonMoveUp->setDisabled(true);
|
ui->buttonMoveUp->setDisabled(true);
|
||||||
@@ -177,6 +178,7 @@ void OpenGLShaderManagerDialog::on_buttonAdd_clicked()
|
|||||||
item->setData(Qt::UserRole + 2, (qulonglong)(uintptr_t)shaderfile);
|
item->setData(Qt::UserRole + 2, (qulonglong)(uintptr_t)shaderfile);
|
||||||
if (ui->shaderListWidget->count()) {
|
if (ui->shaderListWidget->count()) {
|
||||||
ui->shaderListWidget->setCurrentRow(ui->shaderListWidget->count() - 1);
|
ui->shaderListWidget->setCurrentRow(ui->shaderListWidget->count() - 1);
|
||||||
|
ui->buttonAdd->setDisabled(ui->shaderListWidget->count() >= MAX_USER_SHADERS);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
QMessageBox::critical(this, tr("GLSL error"), tr("Could not load filename %1").arg(res));
|
QMessageBox::critical(this, tr("GLSL error"), tr("Could not load filename %1").arg(res));
|
||||||
@@ -197,6 +199,7 @@ void OpenGLShaderManagerDialog::on_buttonRemove_clicked()
|
|||||||
|
|
||||||
on_shaderListWidget_currentRowChanged(ui->shaderListWidget->currentRow());
|
on_shaderListWidget_currentRowChanged(ui->shaderListWidget->currentRow());
|
||||||
}
|
}
|
||||||
|
ui->buttonAdd->setDisabled(ui->shaderListWidget->count() >= MAX_USER_SHADERS);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLShaderManagerDialog::on_OpenGLShaderManagerDialog_accepted()
|
void OpenGLShaderManagerDialog::on_OpenGLShaderManagerDialog_accepted()
|
||||||
|
|||||||
Reference in New Issue
Block a user