diff --git a/CMakeLists.txt b/CMakeLists.txt index 10294d3e..d79cfffb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -308,6 +308,7 @@ endif() set(ENGINE_SOURCES src/log.cpp +src/baseutils.cpp src/fileutils.cpp src/utfutils.cpp diff --git a/src/baseutils.cpp b/src/baseutils.cpp new file mode 100644 index 00000000..aad61eb4 --- /dev/null +++ b/src/baseutils.cpp @@ -0,0 +1,89 @@ +/** + * Furnace Tracker - multi-system chiptune tracker + * Copyright (C) 2021-2022 tildearrow and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "baseutils.h" +#include + +const char* base64Table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +std::string taEncodeBase64(const std::string& data) { + std::string ret; + + ret.reserve((2+data.size()*4)/3); + + unsigned int groupOfThree=0; + unsigned char pos=0; + for (const char& i: data) { + groupOfThree|=((unsigned char)i)<<((2-pos)<<3); + if (++pos>=3) { + pos=0; + ret+=base64Table[(groupOfThree>>18)&63]; + ret+=base64Table[(groupOfThree>>12)&63]; + ret+=base64Table[(groupOfThree>>6)&63]; + ret+=base64Table[groupOfThree&63]; + groupOfThree=0; + } + } + if (pos==2) { + ret+=base64Table[(groupOfThree>>18)&63]; + ret+=base64Table[(groupOfThree>>12)&63]; + ret+=base64Table[(groupOfThree>>6)&63]; + ret+='='; + } else if (pos==1) { + ret+=base64Table[(groupOfThree>>18)&63]; + ret+=base64Table[(groupOfThree>>12)&63]; + ret+="=="; + } + + return ret; +} + +std::string taDecodeBase64(const char* buf) { + std::string data; + + unsigned int groupOfThree=0; + signed char pos=18; + for (const char* i=buf; *i; i++) { + unsigned char nextVal=0; + if ((*i)=='/') { + nextVal=63; + } else if ((*i)=='+') { + nextVal=62; + } else if ((*i)>='0' && (*i)<='9') { + nextVal=52+((*i)-'0'); + } else if ((*i)>='a' && (*i)<='z') { + nextVal=26+((*i)-'a'); + } else if ((*i)>='A' && (*i)<='Z') { + nextVal=((*i)-'A'); + } else { + nextVal=0; + } + groupOfThree|=nextVal<>16)&0xff) data+=(groupOfThree>>16)&0xff; + if ((groupOfThree>>8)&0xff) data+=(groupOfThree>>8)&0xff; + if (groupOfThree&0xff) data+=groupOfThree&0xff; + groupOfThree=0; + } + } + + return data; +} diff --git a/src/baseutils.h b/src/baseutils.h new file mode 100644 index 00000000..438696aa --- /dev/null +++ b/src/baseutils.h @@ -0,0 +1,28 @@ +/** + * Furnace Tracker - multi-system chiptune tracker + * Copyright (C) 2021-2022 tildearrow and contributors + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _BASEUTILS_H +#define _BASEUTILS_H + +#include + +std::string taEncodeBase64(const std::string& data); +std::string taDecodeBase64(const char* str); + +#endif diff --git a/src/engine/config.cpp b/src/engine/config.cpp index 47ff3997..ce7b1b4a 100644 --- a/src/engine/config.cpp +++ b/src/engine/config.cpp @@ -19,6 +19,7 @@ #include "config.h" #include "../ta-log.h" +#include "../baseutils.h" #include "../fileutils.h" #include @@ -48,41 +49,9 @@ String DivConfig::toString() { return ret; } -const char* base64Table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - String DivConfig::toBase64() { String data=toString(); - String ret; - - ret.reserve((2+data.size()*4)/3); - - unsigned int groupOfThree=0; - unsigned char pos=0; - for (char& i: data) { - groupOfThree|=((unsigned char)i)<<((2-pos)<<3); - if (++pos>=3) { - pos=0; - ret+=base64Table[(groupOfThree>>18)&63]; - ret+=base64Table[(groupOfThree>>12)&63]; - ret+=base64Table[(groupOfThree>>6)&63]; - ret+=base64Table[groupOfThree&63]; - groupOfThree=0; - } - } - if (pos==2) { - ret+=base64Table[(groupOfThree>>18)&63]; - ret+=base64Table[(groupOfThree>>12)&63]; - ret+=base64Table[(groupOfThree>>6)&63]; - ret+='='; - } else if (pos==1) { - ret+=base64Table[(groupOfThree>>18)&63]; - ret+=base64Table[(groupOfThree>>12)&63]; - ret+="=="; - } - - logV("toBase64: %s",ret); - - return ret; + return taEncodeBase64(data); } void DivConfig::parseLine(const char* line) { @@ -143,38 +112,7 @@ bool DivConfig::loadFromMemory(const char* buf) { } bool DivConfig::loadFromBase64(const char* buf) { - String data; - - unsigned int groupOfThree=0; - signed char pos=18; - for (const char* i=buf; *i; i++) { - unsigned char nextVal=0; - if ((*i)=='/') { - nextVal=63; - } else if ((*i)=='+') { - nextVal=62; - } else if ((*i)>='0' && (*i)<='9') { - nextVal=52+((*i)-'0'); - } else if ((*i)>='a' && (*i)<='z') { - nextVal=26+((*i)-'a'); - } else if ((*i)>='A' && (*i)<='Z') { - nextVal=((*i)-'A'); - } else { - nextVal=0; - } - groupOfThree|=nextVal<>16)&0xff) data+=(groupOfThree>>16)&0xff; - if ((groupOfThree>>8)&0xff) data+=(groupOfThree>>8)&0xff; - if (groupOfThree&0xff) data+=groupOfThree&0xff; - groupOfThree=0; - } - } - - logV("fromBase64: %s",data); - + String data=taDecodeBase64(buf); return loadFromMemory(data.c_str()); } diff --git a/src/engine/engine.h b/src/engine/engine.h index bea954fa..50cabead 100644 --- a/src/engine/engine.h +++ b/src/engine/engine.h @@ -440,8 +440,6 @@ class DivEngine { void reset(); void playSub(bool preserveDrift, int goalRow=0); - void convertOldFlags(unsigned int oldFlags, DivConfig& newFlags, DivSystem sys); - bool loadDMF(unsigned char* file, size_t len); bool loadFur(unsigned char* file, size_t len); bool loadMod(unsigned char* file, size_t len); @@ -537,6 +535,10 @@ class DivEngine { static DivSystem systemFromFileDMF(unsigned char val); static unsigned char systemToFileDMF(DivSystem val); + // convert old flags + static void convertOldFlags(unsigned int oldFlags, DivConfig& newFlags, DivSystem sys); + + // benchmark (returns time in seconds) double benchmarkPlayback(); double benchmarkSeek(); diff --git a/src/gui/gui.h b/src/gui/gui.h index b467daac..36de61c5 100644 --- a/src/gui/gui.h +++ b/src/gui/gui.h @@ -913,10 +913,7 @@ struct FurnaceGUISysDefChip { struct FurnaceGUISysDef { const char* name; String definition; - FurnaceGUISysDef(const char* n, std::initializer_list def): - name(n) { - // fuck it - } + FurnaceGUISysDef(const char* n, std::initializer_list def); FurnaceGUISysDef(const char* n, std::initializer_list def); }; diff --git a/src/gui/presets.cpp b/src/gui/presets.cpp index 5d009414..d3080e3c 100644 --- a/src/gui/presets.cpp +++ b/src/gui/presets.cpp @@ -18,6 +18,7 @@ */ #include "gui.h" +#include "../baseutils.h" #include // add system configurations here. @@ -2325,6 +2326,32 @@ void FurnaceGUI::initSystemPresets() { sysCategories.push_back(cat); } +FurnaceGUISysDef::FurnaceGUISysDef(const char* n, std::initializer_list def): + name(n) { + std::vector uncompiled=def; + int index=0; + + for (size_t i=0; i def): name(n) { std::vector uncompiled=def; @@ -2339,9 +2366,8 @@ FurnaceGUISysDef::FurnaceGUISysDef(const char* n, std::initializer_list