mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 10:47:04 +02:00
implement buffer proxy caching
This commit is contained in:
1 parent
9418526897
commit
cf31ad8f7a
9 files changed
+43
-20
No files matched your search
@@ -240,14 +240,16 @@ public partial class GameInterface
|
||||
|
||||
if (dataTable == null)
|
||||
{
|
||||
throw new ArgumentException($"Unknown tank part: {part}");
|
||||
Console.WriteLine($"Unknown tank part (no matching data table): {part}");
|
||||
return [];
|
||||
}
|
||||
|
||||
dataTable.TryGetDataTableRow(part, StringComparison.Ordinal, out var row);
|
||||
|
||||
if (row == null)
|
||||
{
|
||||
throw new ArgumentException($"Unknown tank part: {part}");
|
||||
Console.WriteLine($"Unknown tank part (no matching row): {part}");
|
||||
return [];
|
||||
}
|
||||
|
||||
var visualData = row.Get<UObject>("VisualData");
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
import { notFoundResponse } from "./responses";
|
||||
|
||||
const cache = new Map<string, Uint8Array<ArrayBuffer>>();
|
||||
|
||||
export function bufferProxy(cook: () => number[], key: string) {
|
||||
let array = cache.get(key);
|
||||
|
||||
if (array === undefined) {
|
||||
const bytes = cook();
|
||||
array = new Uint8Array(bytes);
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
cache.set(key, array);
|
||||
}
|
||||
}
|
||||
|
||||
if (array.length === 0) return notFoundResponse;
|
||||
|
||||
return new Response(array);
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import sharp from "sharp";
|
||||
import { bufferProxy } from "./bufferProxy";
|
||||
|
||||
export async function imageProxy(url: string) {
|
||||
if (import.meta.env.DEV) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import { mixStaticPaths } from "../../../astro/mixStaticPaths";
|
||||
import { game } from "../../../game/game";
|
||||
import { getStaticPaths as _getStaticPaths } from "../_index";
|
||||
import { assertDiscoveryTag } from "../../../game/assertDiscoveryTag";
|
||||
import { bufferProxy } from "../../../api/bufferProxy";
|
||||
|
||||
export const getStaticPaths = mixStaticPaths(_getStaticPaths, () => {
|
||||
return api.metadata
|
||||
@@ -11,13 +12,10 @@ export const getStaticPaths = mixStaticPaths(_getStaticPaths, () => {
|
||||
.map(({ id }) => ({ params: { id } }));
|
||||
});
|
||||
|
||||
|
||||
export function GET({ params }: APIContext<never, { id: string }>) {
|
||||
const item = api.metadata.item(params.id);
|
||||
const assetsDiscovery = item.BlitzStaticAssetsDiscovery();
|
||||
const tag = assertDiscoveryTag(assetsDiscovery);
|
||||
const icon = game!.consumableIcon(tag);
|
||||
const array = new Uint8Array(icon);
|
||||
|
||||
return new Response(array);
|
||||
return bufferProxy(() => game!.consumableIcon(tag), `consumable-icon-${tag}`);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { mixStaticPaths } from "../../../astro/mixStaticPaths";
|
||||
import { game } from "../../../game/game";
|
||||
import { getStaticPaths as _getStaticPaths } from "../_index";
|
||||
import { assertDiscoveryTag } from "../../../game/assertDiscoveryTag";
|
||||
import { bufferProxy } from "../../../api/bufferProxy";
|
||||
|
||||
export const getStaticPaths = mixStaticPaths(_getStaticPaths, () => {
|
||||
return api.metadata
|
||||
@@ -20,8 +21,6 @@ export function GET({ params }: APIContext<never, { id: string }>) {
|
||||
const item = api.metadata.item(params.id);
|
||||
const assetsDiscovery = item.BlitzStaticAssetsDiscovery();
|
||||
const tag = assertDiscoveryTag(assetsDiscovery);
|
||||
const icon = game!.equipmentIcon(tag);
|
||||
const array = new Uint8Array(icon);
|
||||
|
||||
return new Response(array);
|
||||
return bufferProxy(() => game!.equipmentIcon(tag), `equipment-icon-${tag}`);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { mixStaticPaths } from "../../../../astro/mixStaticPaths";
|
||||
import { game } from "../../../../game/game";
|
||||
import { clientUnmounted } from "../../../../game/unmounted";
|
||||
import { getStaticPaths as _getStaticPaths } from "../../_index";
|
||||
import { bufferProxy } from "../../../../api/bufferProxy";
|
||||
|
||||
export const getStaticPaths = mixStaticPaths(_getStaticPaths, async () => {
|
||||
return Object.values(Nation)
|
||||
@@ -20,7 +21,6 @@ export async function GET({ params }: APIContext<never, { id: string }>) {
|
||||
|
||||
const enumName = Nation[Number(params.id)];
|
||||
const nationName = enumName.replace("NATION_", "").toLowerCase();
|
||||
const flag = game!.flag(nationName);
|
||||
|
||||
return new Response(new Uint8Array(flag));
|
||||
return bufferProxy(() => game!.flag(nationName), `flag-card-${nationName}`);
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import { assertDiscoveryTag } from "../../../../game/assertDiscoveryTag";
|
||||
import { game } from "../../../../game/game";
|
||||
import { clientUnmounted } from "../../../../game/unmounted";
|
||||
import { getStaticPaths as _getStaticPaths } from "../../_index";
|
||||
import { bufferProxy } from "../../../../api/bufferProxy";
|
||||
|
||||
export const getStaticPaths = mixStaticPaths(_getStaticPaths, async () => {
|
||||
const { list } = await api.tankList();
|
||||
@@ -18,8 +19,6 @@ export async function GET({ params }: APIContext<never, { id: string }>) {
|
||||
const item = api.metadata.item(params.id);
|
||||
const assetDiscovery = item.BlitzStaticAssetsDiscovery();
|
||||
const tag = assertDiscoveryTag(assetDiscovery);
|
||||
const icon = game!.tankBigIcon(tag);
|
||||
const array = new Uint8Array(icon);
|
||||
|
||||
return new Response(array);
|
||||
return bufferProxy(() => game!.tankBigIcon(tag), `tank-icon-big-${tag}`);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { mixStaticPaths } from "../../../../astro/mixStaticPaths";
|
||||
import { getStaticPaths as _getStaticPaths } from "../../_index";
|
||||
import { assertDiscoveryTag } from "../../../../game/assertDiscoveryTag";
|
||||
import { game } from "../../../../game/game";
|
||||
import { bufferProxy } from "../../../../api/bufferProxy";
|
||||
|
||||
export const getStaticPaths = mixStaticPaths(_getStaticPaths, async () => {
|
||||
const tanks = await api.tanks();
|
||||
@@ -36,8 +37,9 @@ export async function GET({
|
||||
const item = api.metadata.item(params.id);
|
||||
const assetDiscovery = item.BlitzStaticAssetsDiscovery();
|
||||
const tag = assertDiscoveryTag(assetDiscovery);
|
||||
const bytes = game!.tankPart(tag, params.part);
|
||||
const array = new Uint8Array(bytes);
|
||||
|
||||
return new Response(array);
|
||||
return bufferProxy(
|
||||
() => game!.tankPart(tag, params.part),
|
||||
`tank-part-${tag}-${params.part}`,
|
||||
);
|
||||
}
|
||||
@@ -2,13 +2,15 @@ import type { APIContext } from "astro";
|
||||
import { mixStaticPaths } from "../../astro/mixStaticPaths";
|
||||
import { game } from "../../game/game";
|
||||
import { getStaticPaths as _getStaticPaths } from "./_index";
|
||||
import { bufferProxy } from "../../api/bufferProxy";
|
||||
|
||||
export const getStaticPaths = mixStaticPaths(_getStaticPaths, () => {
|
||||
return game!.textures.map((name) => ({ params: { name } }));
|
||||
});
|
||||
|
||||
export function GET({ params }: APIContext<never, { name: string }>) {
|
||||
const image = game!.texture(params.name);
|
||||
const array = new Uint8Array(image);
|
||||
return new Response(array);
|
||||
return bufferProxy(
|
||||
() => game!.texture(params.name),
|
||||
`texture-${params.name}`,
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user