Skip to content

Commit

Permalink
temp: indexed db
Browse files Browse the repository at this point in the history
  • Loading branch information
akorzunin committed Aug 7, 2024
1 parent e18c3d0 commit 2605bc8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 1 deletion.
20 changes: 19 additions & 1 deletion web/src/components/GodotFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Button } from "../shadcn/ui/button";
import { FaPlay, FaExpand, FaVolumeUp, FaVolumeMute } from "react-icons/fa";
import { useQuery } from "@tanstack/react-query";
import { getGameVersionInfo } from "../lib/versions";
import { getGodotFile } from "../foreign/indexedDB";

interface AudioBridge {
state?: boolean;
Expand All @@ -15,6 +16,13 @@ interface WindowBridge extends Window {
godotAudioBridge: GodotBridge;
}

async function aboba() {
const a = {};
let c = getGodotFile(a);
const b = a.blob;
return [c, a];
}

export const GodotFrame = () => {
const [showIframe, setShowIframe] = useState(false);
const [muted, setMuted] = useState(false);
Expand All @@ -23,6 +31,10 @@ export const GodotFrame = () => {
queryKey: ["version-data"],
queryFn: async () => await getGameVersionInfo(),
});
const { data: gameSettingsFile, error } = useQuery({
queryKey: ["game-settings-data"],
queryFn: async () => await aboba(),
});

const handleFullscreen = () => {
if (iframeRef.current) {
Expand Down Expand Up @@ -99,9 +111,15 @@ export const GodotFrame = () => {
</Button>
</>
)}
<p className="absolute bottom-0 right-2 text-primary-foreground">
<p className="absolute bottom-0 right-2 text-primary-foreground outline-1">
build: {gameVersionData?.version} commit: {gameVersionData?.commit}
</p>
<Button
className="absolute bottom-10"
onClick={() => console.log(gameSettingsFile, error)}
>
&nbsp;Aboba
</Button>
</div>
</div>
);
Expand Down
100 changes: 100 additions & 0 deletions web/src/foreign/indexedDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
(function functionToExecute() {
// Open the database connection.
const open = indexedDB.open("/userfs");

open.onupgradeneeded = function () {
// Define the database schema if necessary.
const db = open.result;
const store = db.createObjectStore("/userfs");
console.log("------------> Upgraded userfs?");
};

open.onsuccess = function () {
const db = open.result;
const key = "/userfs/godot/app_userdata/SuperIcosahedron/settings.cfg";

// Write file to DB
// var tx = db.transaction('files', 'readwrite');
// var store = tx.objectStore('files');
// store.put(null, "test");

// Later, read file back out of DB
// let tx2: IDBTransaction | undefined;
let tx2;
try {
tx2 = db.transaction("FILE_DATA", "readonly");
} catch (DOMException) {
console.error("Failed to get user data from indexedDB");
return;
}
if (!tx2) {
return;
}
const store2 = tx2.objectStore("FILE_DATA");
const request = store2.get(key);

request.onsuccess = function (e) {
// Got the file!
const file = request.result;

if (file == null) {
alert("You haven't generated any results yet.");
console.log("File is null");
return;
} else {
console.log("Is int8array: " + (file.contents instanceof Int8Array));
}

const blob = new Blob([file.contents], {
type: "text/plain;charset=utf-8;",
});
return blob;
};
};
})();

export const USER_CONFIG_KEY =
"/userfs/godot/app_userdata/SuperIcosahedron/settings.cfg";

export function getGodotFile(res: unknown, file: string = USER_CONFIG_KEY) {
// Open the database connection.
const open = indexedDB.open("/userfs");

open.onsuccess = function () {
const db = open.result;
const key = file;

// Later, read file back out of DB
let tx: IDBTransaction | undefined;
try {
tx = db.transaction("FILE_DATA", "readonly");
} catch (DOMException) {
console.error("Failed to get user data from indexedDB");
return;
}
if (!tx) {
return;
}
const store2 = tx.objectStore("FILE_DATA");
const request = store2.get(key);

request.onsuccess = function (): Blob {
// Got the file!
const file = request.result;

if (file == null) {
throw new Error("File not found");
} else {
console.log("Is int8array: " + (file.contents instanceof Int8Array));
}

const blob = new Blob([file.contents], {
type: "text/plain;charset=utf-8;",
});
res.blob = blob;
return blob;
};
return request;
};
return open;
}

0 comments on commit 2605bc8

Please sign in to comment.