Skip to content

Commit

Permalink
Fix db cache MSD
Browse files Browse the repository at this point in the history
  • Loading branch information
nico-abram committed Feb 10, 2018
1 parent b2c1a65 commit 3b6940f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
4 changes: 1 addition & 3 deletions src/NotesLoaderSSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,8 @@ void SetChartKey(SSC::StepsTagInfo& info) {

vector<float> SSC::msdsplit(const RString& s) {
vector<float> o;
for (size_t i = 0; i < s.size(); ++i) {
for (size_t i = 0; i < s.size(); i+=6)
o.emplace_back(StringToFloat(s.substr(i, 5)));
i += 5;
}
return o;
}

Expand Down
4 changes: 2 additions & 2 deletions src/NotesWriterSSC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ static RString JoinLineList( vector<RString> &lines )
RString NotesWriterSSC::MSDToString(MinaSD x) {
RString o = "";
for (size_t i = 0; i < x.size(); i++) {
o.append(NotesWriterSSC::SkillsetDiffsToString(x[i]));
o.append(NotesWriterSSC::MSDsAtRateToString(x[i]));
if (i != x.size() - 1)
o.append(":");
}
return o;
}

RString NotesWriterSSC::SkillsetDiffsToString(SDiffs x) {
RString NotesWriterSSC::MSDsAtRateToString(SDiffs x) {
RString o = "";
for (size_t ii = 0; ii < x.size(); ii++) {
o.append(to_string(x[ii]).substr(0, 5));
Expand Down
2 changes: 1 addition & 1 deletion src/NotesWriterSSC.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace NotesWriterSSC
* @return its success or failure. */
bool WriteEditFileToMachine( const Song *pSong, Steps *pSteps, RString &sErrorOut );
RString MSDToString(MinaSD x);
RString SkillsetDiffsToString(SDiffs x);
RString MSDsAtRateToString(SDiffs x);
}

#endif
Expand Down
76 changes: 45 additions & 31 deletions src/SongCacheIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <SQLiteCpp/VariadicBind.h>
#include "sqlite3.h"


/*
* A quick explanation of song cache hashes: Each song has two hashes; a hash of the
* song path, and a hash of the song directory. The former is Song::GetCacheFilePath;
Expand All @@ -35,7 +36,7 @@
*/
const string CACHE_INDEX = SpecialFiles::CACHE_DIR + "index.cache";
const string CACHE_DB = SpecialFiles::CACHE_DIR + "cache.db";
const string CACHE_DB_VERSION = "1.0";
const unsigned int CACHE_DB_VERSION = 236;


SongCacheIndex *SONGINDEX; // global and accessible from anywhere in our program
Expand Down Expand Up @@ -244,10 +245,9 @@ int SongCacheIndex::InsertSteps(const Steps* pSteps, int songID)
"CONSTRAINT fk_timingdataid FOREIGN KEY (TIMINGDATAID) REFERENCES songs(ID))");
*/
SQLite::Statement insertSteps(*db, "INSERT INTO steps VALUES (NULL, "
"?, ?, ?, ?, ?, "
"?, ?, ?, ?, ?, "
"?, ?, ?, ?, ?, "
"?, ?, ?, "
"?, ?, ?, "
"?, ?, ?, ?, ?)");
vector<RString> lines;
int stepsIndex = 1;
Expand All @@ -259,8 +259,7 @@ int SongCacheIndex::InsertSteps(const Steps* pSteps, int songID)
insertSteps.bind(stepsIndex++, pSteps->GetMeter());

MinaSD o = pSteps->GetAllMSD();
for (size_t i = 0; i < NUM_Skillset; i++)
insertSteps.bind(stepsIndex++, NotesWriterSSC::SkillsetDiffsToString(o[i]));
insertSteps.bind(stepsIndex++, NotesWriterSSC::MSDToString(o));

insertSteps.bind(stepsIndex++, SmEscape(pSteps->GetChartKey()).c_str());//chartkey

Expand Down Expand Up @@ -525,12 +524,15 @@ void SongCacheIndex::CreateDBTables()
{
db->exec("CREATE TABLE IF NOT EXISTS dbinfo (ID INTEGER PRIMARY KEY, "
"VERSION INTEGER)");
db->exec("CREATE TABLE IF NOT EXISTS msdsatrates (ID INTEGER PRIMARY KEY, "
"MSD FLOAT, RATE INTEGER, SONGID INTEGER, "
"CONSTRAINT fk_songid FOREIGN KEY (SONGID) REFERENCES songs(ID))");
db->exec("CREATE TABLE IF NOT EXISTS timingdatas (ID INTEGER PRIMARY KEY, "
"OFFSET TEXT, BPMS TEXT, STOPS TEXT, "
"DELAYS TEXT, WARPS TEXT, TIMESIGNATURESEGMENT TEXT, TICKCOUNTS TEXT, "
"COMBOS TEXT, SPEEDS TEXT, SCROLLS TEXT, FAKES TEXT, LABELS TEXT)");
db->exec("CREATE TABLE IF NOT EXISTS songs (ID INTEGER PRIMARY KEY, "
"VERSION TEXT, TITLE TEXT, SUBTITLE TEXT, ARTIST TEXT, TITLETRANSLIT TEXT, "
"VERSION FLOAT, TITLE TEXT, SUBTITLE TEXT, ARTIST TEXT, TITLETRANSLIT TEXT, "
"SUBTITLETRANSLIT TEXT, ARTISTTRANSLIT TEXT, GENRE TEXT, "
"ORIGIN TEXT, CREDIT TEXT, BANNER TEXT, BACKGROUND TEXT, "
"PREVIEWVID TEXT, JACKET TEXT, CDIMAGE TEXT, DISCIMAGE TEXT, "
Expand All @@ -544,19 +546,20 @@ void SongCacheIndex::CreateDBTables()
"SONGFILENAME TEXT, HASMUSIC INTEGER, HASBANNER INTEGER, MUSICLENGTH FLOAT, DIRHASH INTEGER, DIR TEXT)");
db->exec("CREATE TABLE IF NOT EXISTS steps (id INTEGER PRIMARY KEY, "
"CHARTNAME TEXT, STEPSTYPE TEXT, DESCRIPTION TEXT, CHARTSTYLE TEXT, DIFFICULTY INTEGER, "
"METER INTEGER, OVERALL TEXT, STREAM TEXT, JUMPSTREAM TEXT, HANDSTREAM TEXT, "
"STAMINA TEXT, JACKSPEED TEXT, JACKSTAMINA TEXT, TECHNICAL TEXT, CHARTKEY TEXT, "
"METER INTEGER, MSD TEXT, CHARTKEY TEXT, "
"MUSIC TEXT, RADARVALUES TEXT, CREDIT TEXT, "
"TIMINGDATAID INTEGER, DISPLAYBPMMIN FLOAT, DISPLAYBPMMAX FLOAT, STEPFILENAME TEXT, SONGID INTEGER, "
"CONSTRAINT fk_songid FOREIGN KEY (SONGID) REFERENCES songs(id), "
"CONSTRAINT fk_timingdataid FOREIGN KEY (TIMINGDATAID) REFERENCES songs(ID))");
"CONSTRAINT fk_songid FOREIGN KEY (SONGID) REFERENCES songs(ID), "
"CONSTRAINT fk_timingdataid FOREIGN KEY (TIMINGDATAID) REFERENCES timingdatas(ID))");
db->exec("CREATE INDEX IF NOT EXISTS idx_dirs "
"ON songs(DIR, DIRHASH)");
db->exec("CREATE INDEX IF NOT EXISTS idx_timingdatas "
"ON timingdatas(ID)");
db->exec("CREATE INDEX IF NOT EXISTS idx_steps "
"ON steps(SONGID)");
db->exec("INSERT INTO dbinfo VALUES (NULL, " + CACHE_DB_VERSION + ")");
//db->exec("CREATE INDEX IF NOT EXISTS idx_msds "
// "ON msdsatrates(RATE, SONGID)");
db->exec("INSERT INTO dbinfo VALUES (NULL, " + to_string(CACHE_DB_VERSION) + ")");
}
/* Returns weather or not the db had valid data*/
bool SongCacheIndex::OpenDB()
Expand All @@ -566,12 +569,6 @@ bool SongCacheIndex::OpenDB()
try {
db = new SQLite::Database(FILEMAN->ResolvePath(CACHE_DB), SQLite::OPEN_READWRITE | SQLite::OPEN_CREATE | SQLITE_OPEN_FULLMUTEX);
StartTransaction();
db->exec("CREATE INDEX IF NOT EXISTS idx_dirs "
"ON songs(DIR, DIRHASH)");
db->exec("CREATE INDEX IF NOT EXISTS idx_timingdatas "
"ON timingdatas(ID)");
db->exec("CREATE INDEX IF NOT EXISTS idx_steps "
"ON steps(SONGID)");
if (!ret) {
ResetDB();
FinishTransaction();
Expand All @@ -585,8 +582,8 @@ bool SongCacheIndex::OpenDB()
FinishTransaction();
return false;
}
string cacheVersion = "";
cacheVersion = static_cast<const char *>(qDBInfo.getColumn(1));
unsigned int cacheVersion;
cacheVersion = qDBInfo.getColumn(1);
if (cacheVersion == CACHE_DB_VERSION) {
FinishTransaction();
return true;
Expand Down Expand Up @@ -688,12 +685,17 @@ RString SongCacheIndex::MangleName(const RString &Name)

void SongCacheIndex::StartTransaction()
{
if (db == nullptr)
return;
if (curTransaction != nullptr)
return;
curTransaction = new SQLite::Transaction(*db);
return;
}
void SongCacheIndex::FinishTransaction()
{
if (curTransaction == nullptr || curTransaction != nullptr)
return;
curTransaction->commit();
delete curTransaction;
curTransaction = nullptr;
Expand Down Expand Up @@ -756,9 +758,9 @@ bool SongCacheIndex::LoadSongFromCache(Song* song, string dir)
if (query.isColumnNull(bpmminIndex) || query.isColumnNull(bpmmaxIndex))
{
if (query.isColumnNull(bpmminIndex) && query.isColumnNull(bpmmaxIndex))
song->m_DisplayBPMType = DISPLAY_BPM_RANDOM;
else
song->m_DisplayBPMType = DISPLAY_BPM_ACTUAL;
else
song->m_DisplayBPMType = DISPLAY_BPM_RANDOM;
}
else
{
Expand All @@ -767,17 +769,17 @@ bool SongCacheIndex::LoadSongFromCache(Song* song, string dir)
song->m_fSpecifiedBPMMax = BPMmax;
}

loader.ProcessBPMs(song->m_SongTiming, song->GetMainTitle(), static_cast<const char *>(query.getColumn(index++)));
loader.ProcessStops(song->m_SongTiming, song->GetMainTitle(), static_cast<const char *>(query.getColumn(index++)));
loader.ProcessBPMs(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)), song->GetMainTitle());
loader.ProcessStops(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)), song->GetMainTitle());
loader.ProcessDelays(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)));
loader.ProcessWarps(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)), song->m_fVersion, song->GetMainTitle());
loader.ProcessTimeSignatures(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)));
loader.ProcessTickcounts(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)));
loader.ProcessCombos(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)));
loader.ProcessSpeeds(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)));
loader.ProcessScrolls(song->m_SongTiming, song->GetMainTitle(), static_cast<const char *>(query.getColumn(index++)));
loader.ProcessScrolls(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)), song->GetMainTitle());
loader.ProcessFakes(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)));
loader.ProcessLabels(song->m_SongTiming, song->GetMainTitle(), static_cast<const char *>(query.getColumn(index++)));
loader.ProcessLabels(song->m_SongTiming, static_cast<const char *>(query.getColumn(index++)), song->GetMainTitle());

song->SetSpecifiedLastSecond(static_cast<double>(query.getColumn(index++)));

Expand Down Expand Up @@ -829,21 +831,33 @@ bool SongCacheIndex::LoadSongFromCache(Song* song, string dir)
pNewNotes = song->CreateSteps();
int stepsID = qSteps.getColumn(stepsIndex++);
RString chartName = static_cast<const char*>(qSteps.getColumn(stepsIndex++));
Trim(chartName);
pNewNotes->SetChartName(chartName);
string stepsType = static_cast<const char*>(qSteps.getColumn(stepsIndex++));
pNewNotes->m_StepsType = GAMEMAN->StringToStepsType(stepsType);
pNewNotes->m_StepsTypeStr = stepsType;
RString description = static_cast<const char*>(qSteps.getColumn(stepsIndex++));
Trim(description);
pNewNotes->SetDescription(description);
pNewNotes->SetChartStyle(static_cast<const char*>(qSteps.getColumn(stepsIndex++)));
pNewNotes->SetDifficulty(static_cast<Difficulty>(static_cast<int>(qSteps.getColumn(stepsIndex++))));
pNewNotes->SetMeter(qSteps.getColumn(stepsIndex++));

MinaSD o;
for (size_t i = 0; i < NUM_Skillset; i++)
o.emplace_back(SSC::msdsplit(static_cast<const char*>(qSteps.getColumn(stepsIndex++))));
//for (size_t i = 0; i < NUM_Skillset; i++)
// o.emplace_back(SSC::msdsplit(static_cast<const char*>(qSteps.getColumn(stepsIndex++))));
/*
SQLite::Statement qMsdsForARate(*db, "SELECT * FROM msdsatrates WHERE SONGID=" + to_string(songid)+ " ORDER BY RATE ASC");
while (qMsdsForARate.executeStep()) {
SDiffs temp;
for (size_t i = 1; i <= NUM_Skillset; i++)
temp.emplace_back(qMsdsForARate.getColumn(i));
o.emplace_back(temp);
}
*/
stringstream msds;
msds.str(static_cast<const char*>(qSteps.getColumn(stepsIndex++)));
string msdsatrate;
while (std::getline(msds, msdsatrate, ':'))
o.emplace_back(SSC::msdsplit(msdsatrate));
pNewNotes->SetAllMSD(o);

pNewNotes->SetChartKey(static_cast<const char*>(qSteps.getColumn(stepsIndex++)));
Expand Down Expand Up @@ -902,9 +916,9 @@ bool SongCacheIndex::LoadSongFromCache(Song* song, string dir)
if (qSteps.isColumnNull(bpmminIndex) || qSteps.isColumnNull(bpmmaxIndex))
{
if (qSteps.isColumnNull(bpmminIndex) && qSteps.isColumnNull(bpmmaxIndex))
pNewNotes->SetDisplayBPM(DISPLAY_BPM_RANDOM);
else
pNewNotes->SetDisplayBPM(DISPLAY_BPM_ACTUAL);
else
pNewNotes->SetDisplayBPM(DISPLAY_BPM_RANDOM);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/SongCacheIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class SongCacheIndex
void DeleteDB();
void CreateDBTables();
bool DBEmpty{true};
SQLite::Database *db;
SQLite::Transaction *curTransaction;
SQLite::Database *db{nullptr};
SQLite::Transaction *curTransaction{nullptr};
public:
SongCacheIndex();
~SongCacheIndex();
Expand Down

0 comments on commit 3b6940f

Please sign in to comment.