Skip to content

Commit 694050d

Browse files
dorin-gaGogoshika-ga
authored andcommitted
catch exception instead of error code
check for existing database fix default path collision catch exceptioninstead of error code
1 parent b897cd0 commit 694050d

File tree

4 files changed

+50
-15
lines changed

4 files changed

+50
-15
lines changed

source/gameanalytics/GAStore.cpp

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -207,32 +207,67 @@ namespace gameanalytics
207207
return sqlDatabase;
208208
}
209209

210-
bool GAStore::ensureDatabase(bool dropDatabase, std::string const& key)
210+
bool GAStore::fixOldDatabase()
211+
{
212+
std::filesystem::path oldPath = dbPath;
213+
std::filesystem::path filename = oldPath.filename();
214+
215+
oldPath = oldPath.parent_path() / ".." / filename;
216+
217+
if(std::filesystem::exists(oldPath) && !std::filesystem::exists(dbPath))
218+
{
219+
try
220+
{
221+
std::filesystem::rename(oldPath, dbPath);
222+
}
223+
catch(...)
224+
{
225+
return false;
226+
}
227+
228+
return true;
229+
}
230+
231+
return false;
232+
}
233+
234+
bool GAStore::initDatabaseLocation()
211235
{
212-
// lazy creation of db path
213-
if(getInstance().dbPath.empty())
236+
constexpr const char* DATABASE_NAME = "ga.sqlite3";
237+
238+
std::filesystem::path p = device::GADevice::getWritablePath();
239+
240+
p /= state::GAState::getGameKey();
241+
242+
dbPath = (p / DATABASE_NAME).string();
243+
if(!std::filesystem::exists(p))
214244
{
215-
std::string path = device::GADevice::getWritablePath();
216-
path += "/ga.sqlite3";
245+
if(!std::filesystem::create_directory(p))
246+
return false;
217247

218-
getInstance().dbPath = path;
248+
fixOldDatabase();
219249
}
220250

221-
const std::string dbPath = getInstance().dbPath;
251+
return true;
252+
}
222253

254+
bool GAStore::ensureDatabase(bool dropDatabase, std::string const& key)
255+
{
256+
getInstance().initDatabaseLocation();
257+
223258
// Open database
224-
if (sqlite3_open(dbPath.c_str(), &getInstance().sqlDatabase) != SQLITE_OK)
259+
if (sqlite3_open(getInstance().dbPath.c_str(), &getInstance().sqlDatabase) != SQLITE_OK)
225260
{
226261
getInstance().dbReady = false;
227-
logging::GALogger::w("Could not open database: %s", dbPath.c_str());
262+
logging::GALogger::w("Could not open database: %s", getInstance().dbPath.c_str());
228263
return false;
229264
}
230265
else
231266
{
232267
getInstance().dbReady = true;
233-
logging::GALogger::i("Database opened: %s", dbPath.c_str());
268+
logging::GALogger::i("Database opened: %s", getInstance().dbPath.c_str());
234269
}
235-
270+
236271
if (dropDatabase)
237272
{
238273
logging::GALogger::d("Drop tables");

source/gameanalytics/GAStore.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,10 @@ namespace gameanalytics
5050

5151
static GAStore& getInstance();
5252

53+
bool fixOldDatabase();
5354
bool trimEventTable();
55+
56+
bool initDatabaseLocation();
5457

5558
// set when calling "ensureDatabase"
5659
// using a "writablePath" that needs to be set into the C++ component before

source/gameanalytics/GameAnalytics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,8 +356,7 @@ namespace gameanalytics
356356
{
357357
return;
358358
}
359-
360-
359+
361360
threading::GAThreading::performTaskOnGAThread([gameKey, gameSecret]()
362361
{
363362
if (isSdkReady(true, false))

source/gameanalytics/Platform/GADevicePlatform.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,3 @@ namespace gameanalytics
4949
return std::make_unique<GADevicePlatform>();
5050
}
5151
}
52-
53-

0 commit comments

Comments
 (0)