Display game name and title.

This commit is contained in:
Jean-Philip Desjardins 2021-09-22 11:26:06 -04:00
parent b3931a22bb
commit 5cf51370e6
4 changed files with 37 additions and 4 deletions

View file

@ -1,6 +1,6 @@
import { configureStore, createAsyncThunk, createReducer } from "@reduxjs/toolkit";
import { TypedUseSelectorHook, useDispatch, useSelector } from "react-redux";
import { PsfPlayerModule, initPsfPlayerModule, getPsfArchiveFileList, loadPsfFromArchive, tickPsf } from "./PsfPlayerModule";
import { PsfPlayerModule, initPsfPlayerModule, getPsfArchiveFileList, loadPsfFromArchive, getCurrentPsfTags, tickPsf } from "./PsfPlayerModule";
import { Mutex } from 'async-mutex';
let archiveFilePath = "archive.zip";
@ -18,11 +18,13 @@ let updateFct = async function() {
export type AudioState = {
value: string,
archiveFileList : string[]
currentPsfTags : any | undefined
};
let initialState : AudioState = {
value: "unknown",
archiveFileList: [],
currentPsfTags: undefined
};
export const init = createAsyncThunk<void>('init',
@ -52,13 +54,15 @@ export const loadArchive = createAsyncThunk<string[] | undefined, string>('loadA
return fileList;
}
);
export const loadPsf = createAsyncThunk<void, string>('loadPsf',
export const loadPsf = createAsyncThunk<any, string>('loadPsf',
async (psfFilePath : string, thunkAPI) => {
let releaseLock = await tickMutex.acquire();
clearTimeout(updateTimer);
await loadPsfFromArchive(archiveFilePath, psfFilePath);
let tags = getCurrentPsfTags();
updateTimer = setTimeout(updateFct, updateDelay);
releaseLock();
return Object.fromEntries(tags);
}
);
export const play = createAsyncThunk<void, void>('play',
@ -98,6 +102,7 @@ const reducer = createReducer(initialState, (builder) => (
})
.addCase(loadPsf.fulfilled, (state, action) => {
state.value = "psf loaded";
state.currentPsfTags = action.payload;
return state;
})
.addCase(loadPsf.rejected, (state, action) => {

View file

@ -54,7 +54,8 @@ export default function App() {
{RenderRow}
</FixedSizeList>
<div>
<textarea value={state.value} />
<br />
<div>{state.currentPsfTags ? `${state.currentPsfTags.game} - ${state.currentPsfTags.title}` : 'PsfPlayer'}</div>
<br />
<button onClick={() => dispatch(play())}>Start</button>
<button onClick={() => dispatch(stop())}>Stop</button>

View file

@ -19,6 +19,19 @@ function convertStringVectorToArray(strVector : any) {
return strArray;
}
function convertStringMapToDictionary(strMap : any) {
let keysList = strMap.keys();
let keys : string[] = [];
for(let i = 0; i < keysList.size(); i++) {
let str = keysList.get(i);
keys.push(str);
}
let strDict : Map<string, string> = new Map();
keys.forEach(key => strDict.set(key, strMap.get(key)));
strMap.delete();
return strDict;
}
export let initPsfPlayerModule = async function() {
PsfPlayerModule = await PsfPlayer(module_overrides);
PsfPlayerModule.FS.mkdir("/work");
@ -34,6 +47,11 @@ export let loadPsfFromArchive = async function(archivePath : string, psfPath : s
await PsfPlayerModule.ccall("loadPsf", "", ['string', 'string'], [archivePath, psfPath], { async: true });
}
export let getCurrentPsfTags = function() {
let tags = convertStringMapToDictionary(PsfPlayerModule.getCurrentPsfTags());
return tags;
}
export let tickPsf = async function() {
await PsfPlayerModule.ccall("step", "", [], [], { async: true });
}

View file

@ -11,6 +11,7 @@
#include <emscripten/bind.h>
CPsfVm* g_virtualMachine = nullptr;
CPsfBase::TagMap g_tags;
int main(int argc, const char** argv)
{
@ -45,8 +46,9 @@ extern "C" void loadPsf(const char* archivePath, const char* psfPath)
try
{
assert(g_virtualMachine);
g_tags.clear();
auto fileToken = CArchivePsfStreamProvider::GetPathTokenFromFilePath(psfPath);
CPsfLoader::LoadPsf(*g_virtualMachine, fileToken, archivePath);
CPsfLoader::LoadPsf(*g_virtualMachine, fileToken, archivePath, &g_tags);
}
catch(const std::exception& ex)
{
@ -86,11 +88,18 @@ std::vector<std::string> getPsfArchiveFileList(std::string rawArchivePath)
return result;
}
CPsfBase::TagMap getCurrentPsfTags()
{
return g_tags;
}
EMSCRIPTEN_BINDINGS(PsfPlayer)
{
using namespace emscripten;
register_vector<std::string>("StringList");
register_map<std::string, std::string>("StringMap");
function("getPsfArchiveFileList", &getPsfArchiveFileList);
function("getCurrentPsfTags", &getCurrentPsfTags);
}