Skip to content

Commit

Permalink
#15 async read/write for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
XProger committed Feb 25, 2018
1 parent e6f2deb commit 60141f8
Show file tree
Hide file tree
Showing 9 changed files with 202 additions and 108 deletions.
8 changes: 1 addition & 7 deletions src/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,6 @@ extern int osGetTime ();
extern bool osJoyReady (int index);
extern void osJoyVibrate (int index, float L, float R);

extern bool osCacheWrite (const char *name, const char *data, int size);
extern Stream* osCacheRead (const char *name);

extern bool osSaveGame (const char *data, int size);
extern Stream* osLoadGame ();


struct Mutex {
void *obj;

Expand Down Expand Up @@ -292,6 +285,7 @@ namespace Core {
} support;

#define SETTINGS_VERSION 1
#define SETTINGS_READING 0xFF

struct Settings {
enum Quality { LOW, MEDIUM, HIGH };
Expand Down
2 changes: 1 addition & 1 deletion src/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ namespace Debug {

fwrite(&header, sizeof(header), 1, f);

Sound::VAG vag(new Stream(&level->soundData[level->soundOffsets[index]], dataSize));
Sound::VAG vag(new Stream(NULL, &level->soundData[level->soundOffsets[index]], dataSize));
Sound::Frame frames[4 * 28];
while (int count = vag.decode(frames, 4 * 28))
for (int i = 0; i < count; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -3674,7 +3674,7 @@ namespace TR {
case VER_TR2_PSX : size = soundSize[index]; break;
default : ASSERT(false);
}
return new Stream(data, size);
return new Stream(NULL, data, size);
}

int getMeshByID(int id) const {
Expand Down
56 changes: 33 additions & 23 deletions src/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,52 +12,55 @@ ShaderCache *shaderCache;
namespace Game {
Level *level;
Stream *nextLevel;
}

void loadAsync(Stream *stream, void *userData) {
if (!stream) {
if (Game::level) Game::level->isEnded = false;
return;
}
Game::nextLevel = stream;
}

namespace Game {
void startLevel(Stream *lvl) {
delete level;
level = new Level(*lvl);
UI::game = level;
delete lvl;
}
}

void stopChannel(Sound::Sample *channel) {
if (level) level->stopChannel(channel);
void loadAsync(Stream *stream, void *userData) {
if (!stream) {
if (Game::level) Game::level->isEnded = false;
return;
}
Game::nextLevel = stream;
}

void readSettings() {
Stream *stream = osCacheRead("settings");
if (!stream) return;
void loadSettings(Stream *stream, void *userData) {
if (stream) {
uint8 version;
stream->read(version);
if (version == SETTINGS_VERSION && stream->size == sizeof(Core::Settings))
stream->raw((char*)&Core::settings + 1, stream->size - 1); // read settings data right after version number
delete stream;
}

Core::settings.version = SETTINGS_VERSION;
shaderCache = new ShaderCache();
Game::startLevel((Stream*)userData);
UI::init(Game::level);
}

void init(Stream *lvl) {
nextLevel = NULL;

Core::init();
readSettings();
namespace Game {

shaderCache = new ShaderCache();
void stopChannel(Sound::Sample *channel) {
if (level) level->stopChannel(channel);
}

UI::init(level);
void init(Stream *lvl) {
nextLevel = NULL;
shaderCache = NULL;
level = NULL;

Sound::callback = stopChannel;

level = NULL;
startLevel(lvl);
Core::init();
Core::settings.version = SETTINGS_READING;
Stream::cacheRead("settings", loadSettings, lvl);
}

void init(const char *lvlName = NULL) {
Expand Down Expand Up @@ -100,6 +103,10 @@ namespace Game {
}

bool update() {
// async load for settings
if (Core::settings.version == SETTINGS_READING)
return true;

PROFILE_MARKER("UPDATE");

if (!Core::update())
Expand Down Expand Up @@ -155,6 +162,9 @@ namespace Game {
}

void render() {
if (Core::settings.version == SETTINGS_READING)
return;

PROFILE_MARKER("RENDER");
PROFILE_TIMING(Core::stats.tFrame);
Core::beginFrame();
Expand Down
6 changes: 3 additions & 3 deletions src/level.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct Level : IGame {
save->size = ptr - data;
save->version = level.version & TR::VER_VERSION;

osSaveGame(data, int(ptr - data));
//osSaveGame(new Stream("savegame", data, int(ptr - data)));
delete[] data;

LOG("Ok\n");
Expand All @@ -139,7 +139,7 @@ struct Level : IGame {
virtual void loadGame(int slot) {
LOG("Load Game... ");

Stream *stream = osLoadGame();
Stream *stream = NULL;//osLoadGame();
if (!stream)
return;

Expand Down Expand Up @@ -237,7 +237,7 @@ struct Level : IGame {

Core::settings = settings;

osCacheWrite("settings", (char*)&settings, sizeof(settings));
Stream::cacheWrite("settings", (char*)&settings, sizeof(settings));

if (rebuildShaders) {
delete shaderCache;
Expand Down
118 changes: 76 additions & 42 deletions src/platform/web/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,83 @@ int osGetTime() {
return (int)emscripten_get_now();
}

bool osSave(const char *name, const void *data, int size) {
// TODO cookie? idb?
FILE *f = fopen(name, "wb");
if (!f) return false;
// cache & data
const char *IDB = "db";

void onError(void *arg) {
Stream *stream = (Stream*)arg;
LOG("! IDB error for %s\n", stream->name);
if (stream->callback)
stream->callback(NULL, stream->userData);
delete stream;
}

void onLoad(void *arg, void *data, int size) {
Stream *stream = (Stream*)arg;

FILE *f = fopen(stream->name, "wb");
fwrite(data, size, 1, f);
fclose(f);
return true;

stream->callback(new Stream(stream->name), stream->userData);
delete stream;
}

void onLoadAndStore(void *arg, void *data, int size) {
emscripten_idb_async_store(IDB, ((Stream*)arg)->name, data, size, NULL, NULL, onError);
onLoad(arg, data, size);
}

void onExists(void *arg, int exists) {
if (exists)
emscripten_idb_async_load(IDB, ((Stream*)arg)->name, arg, onLoad, onError);
else
emscripten_async_wget_data(((Stream*)arg)->name, arg, onLoadAndStore, onError);
}

void osDownload(Stream *stream) {
emscripten_idb_async_exists(IDB, stream->name, stream, onExists, onError);
}

void onCacheStore(void *arg) {
Stream *stream = (Stream*)arg;
LOG("cache stored: %s\n", stream->name);
if (stream->callback)
stream->callback(new Stream(stream->name, NULL, 0), stream->userData);
delete stream;
}

void onCacheLoad(void *arg, void *data, int size) {
Stream *stream = (Stream*)arg;
LOG("cache loaded: %s\n", stream->name);
if (stream->callback)
stream->callback(new Stream(stream->name, data, size), stream->userData);
delete stream;
}

void onCacheError(void *arg) {
Stream *stream = (Stream*)arg;
LOG("! cache error: %s\n", stream->name);
if (stream->callback)
stream->callback(NULL, stream->userData);
delete stream;
}

void osCacheWrite(Stream *stream) {
emscripten_idb_async_store(IDB, stream->name, stream->data, stream->size, stream, onCacheStore, onCacheError);
}

void osCacheRead(Stream *stream) {
emscripten_idb_async_load(IDB, stream->name, stream, onCacheLoad, onCacheError);
}

// memory card
void osSaveGame(Stream *stream) {
return osCacheWrite(stream);
}

void osLoadGame(Stream *stream) {
return osCacheRead(stream);
}

extern "C" {
Expand All @@ -79,7 +149,7 @@ extern "C" {
}

void EMSCRIPTEN_KEEPALIVE game_level_load(char *data, int size) {
Game::startLevel(new Stream(data, size));
Game::startLevel(new Stream(NULL, data, size));
}
}

Expand Down Expand Up @@ -279,42 +349,6 @@ EM_BOOL mouseCallback(int eventType, const EmscriptenMouseEvent *e, void *userDa
return 1;
}

const char *IDB = "db";

void onError(void *arg) {
Stream *stream = (Stream*)arg;
LOG("! IDB error for %s\n", stream->name);
stream->callback(NULL, stream->userData);
delete stream;
}

void onLoad(void *arg, void *data, int size) {
Stream *stream = (Stream*)arg;

FILE *f = fopen(stream->name, "wb");
fwrite(data, size, 1, f);
fclose(f);

stream->callback(new Stream(stream->name), stream->userData);
delete stream;
}

void onLoadAndStore(void *arg, void *data, int size) {
emscripten_idb_async_store(IDB, ((Stream*)arg)->name, data, size, NULL, NULL, onError);
onLoad(arg, data, size);
}

void onExists(void *arg, int exists) {
if (exists)
emscripten_idb_async_load(IDB, ((Stream*)arg)->name, arg, onLoad, onError);
else
emscripten_async_wget_data(((Stream*)arg)->name, arg, onLoadAndStore, onError);
}

void osDownload(Stream *stream) {
emscripten_idb_async_exists(IDB, stream->name, stream, onExists, onError);
}

char Stream::cacheDir[255];
char Stream::contentDir[255];

Expand Down
56 changes: 37 additions & 19 deletions src/platform/win/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,50 @@ int osGetTime() {
#endif
}

bool osCacheWrite(const char *name, const char *data, int size) {
void osCacheWrite(Stream *stream) {
char path[255];
strcpy(path, Stream::cacheDir);
strcat(path, name);
strcat(path, stream->name);
FILE *f = fopen(path, "wb");
if (!f) return false;
fwrite(data, size, 1, f);
fclose(f);
return true;
if (f) {
fwrite(stream->data, 1, stream->size, f);
fclose(f);
if (stream->callback)
stream->callback(new Stream(stream->name, NULL, 0), stream->userData);
} else
if (stream->callback)
stream->callback(NULL, stream->userData);

delete stream;
}

Stream* osCacheRead(const char *name) {
void osCacheRead(Stream *stream) {
char path[255];
strcpy(path, Stream::cacheDir);
strcat(path, name);
if (!Stream::exists(path))
return NULL;
return new Stream(path);
}

bool osSaveGame(const char *data, int size) {
return osCacheWrite("savegame", data, size);
}

Stream* osLoadGame() {
return osCacheRead("savegame");
strcat(path, stream->name);
FILE *f = fopen(path, "rb");
if (f) {
fseek(f, 0, SEEK_END);
int size = ftell(f);
fseek(f, 0, SEEK_SET);
char *data = new char[size];
fread(data, 1, size, f);
fclose(f);
if (stream->callback)
stream->callback(new Stream(stream->name, data, size), stream->userData);
delete[] data;
} else
if (stream->callback)
stream->callback(NULL, stream->userData);
delete stream;
}

void osSaveGame(Stream *stream) {
return osCacheWrite(stream);
}

void osLoadGame(Stream *stream) {
return osCacheRead(stream);
}

// common input functions
Expand Down
Loading

0 comments on commit 60141f8

Please sign in to comment.