mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-22 16:13:12 +09:00
Extended export data to cover more icon types (#838)
* Enhance export functionality and add icon reference page - Expanded exported assets to include additional spritesheets and metadata for recipes, signals, technologies, planets, qualities, and entities. - added logging metrics to game logs - Introduced a new Icon Reference page to display available icons with filtering capabilities. - Updated ExportManifest documentation to reflect new asset categories. * Add support for exporting static icons and update metadata handling * -added data export snapshot metrics * Fix HTML attribute syntax in IconReferencePage component * Applied PR sugestions: -removed un needed x === "string"s -removed isntancename / modpack from icon reference and export -logging per line on export -fixed variable names -linting * linted * Shows "no export data available" when no export happens * -single sprite sheet -removed static icons with no prototype * Fix formatting * Rename types to allowedTypes for clarity * -removed duplication check (redundant) -fixed bug overwriting metadata with same name from previous categories * Rename asset keys for consistency in export validation test --------- Co-authored-by: Solar <solar@homelab.local>
This commit is contained in:
1 parent
2235294c59
commit
6733aed3fb
8 files changed
+482
-82
No files matched your search
@@ -0,0 +1,248 @@
|
||||
import React, { useMemo, useState } from "react";
|
||||
import { Card, Input, Space, Table, Tag, Typography } from "antd";
|
||||
|
||||
import {
|
||||
useItemMetadata,
|
||||
useRecipeMetadata,
|
||||
useSignalMetadata,
|
||||
useTechnologyMetadata,
|
||||
usePlanetMetadata,
|
||||
useQualityMetadata,
|
||||
useEntityMetadata,
|
||||
} from "../model/item_metadata";
|
||||
import { useExportManifest } from "../model/export_manifest";
|
||||
import PageHeader from "./PageHeader";
|
||||
import PageLayout from "./PageLayout";
|
||||
|
||||
const { Text } = Typography;
|
||||
|
||||
type IconEntry = {
|
||||
key: string;
|
||||
category: string;
|
||||
name: string;
|
||||
cssClass: string;
|
||||
size: number;
|
||||
path?: string;
|
||||
};
|
||||
|
||||
const CATEGORIES = [
|
||||
"item", "recipe", "signal", "technology", "planet", "quality", "entity",
|
||||
] as const;
|
||||
|
||||
type Category = typeof CATEGORIES[number];
|
||||
|
||||
const CATEGORY_LABELS: Record<Category, string> = {
|
||||
item: "Item",
|
||||
recipe: "Recipe",
|
||||
signal: "Signal",
|
||||
technology: "Technology",
|
||||
planet: "Planet",
|
||||
quality: "Quality",
|
||||
entity: "Entity",
|
||||
};
|
||||
|
||||
function useAllIconEntries() {
|
||||
const item = useItemMetadata();
|
||||
const recipe = useRecipeMetadata();
|
||||
const signal = useSignalMetadata();
|
||||
const technology = useTechnologyMetadata();
|
||||
const planet = usePlanetMetadata();
|
||||
const quality = useQualityMetadata();
|
||||
const entity = useEntityMetadata();
|
||||
|
||||
const byCategory: Record<Category, Map<string, { size: number; path?: string }>> = {
|
||||
item, recipe, signal, technology, planet, quality, entity,
|
||||
};
|
||||
|
||||
return useMemo(() => {
|
||||
const entries: IconEntry[] = [];
|
||||
|
||||
for (const category of CATEGORIES) {
|
||||
const names = [...byCategory[category].keys()].sort();
|
||||
for (const name of names) {
|
||||
const meta = byCategory[category].get(name)!;
|
||||
entries.push({
|
||||
key: `${category}:${name}`,
|
||||
category,
|
||||
name,
|
||||
cssClass: `${category}-${CSS.escape(name)}`,
|
||||
size: meta.size,
|
||||
path: meta.path,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return entries;
|
||||
}, [item, recipe, signal, technology, planet, quality, entity]);
|
||||
}
|
||||
|
||||
export default function IconReferencePage() {
|
||||
const [filter, setFilter] = useState("");
|
||||
const [expandedGroups, setExpandedGroups] = useState<Set<string>>(new Set());
|
||||
const allEntries = useAllIconEntries();
|
||||
|
||||
const filtered = useMemo(() => {
|
||||
const q = filter.trim().toLowerCase();
|
||||
if (!q) {
|
||||
return allEntries;
|
||||
}
|
||||
return allEntries.filter(e => e.name.toLowerCase().includes(q) || e.cssClass.toLowerCase().includes(q)
|
||||
);
|
||||
}, [allEntries, filter]);
|
||||
|
||||
const groups = useMemo(() => {
|
||||
const map = new Map<string, IconEntry[]>();
|
||||
for (const cat of CATEGORIES) {
|
||||
map.set(cat, []);
|
||||
}
|
||||
for (const entry of filtered) {
|
||||
map.get(entry.category)!.push(entry);
|
||||
}
|
||||
return CATEGORIES.map(cat => ({ category: cat, entries: map.get(cat)! }))
|
||||
.filter(g => g.entries.length > 0);
|
||||
}, [filtered]);
|
||||
|
||||
const visibleCount = filtered.length;
|
||||
const totalCount = allEntries.length;
|
||||
const autoExpand = visibleCount <= 20;
|
||||
|
||||
const isExpanded = (category: string) => autoExpand || expandedGroups.has(category);
|
||||
|
||||
const toggleGroup = (category: string) => {
|
||||
setExpandedGroups(prev => {
|
||||
const next = new Set(prev);
|
||||
if (next.has(category)) {
|
||||
next.delete(category);
|
||||
} else {
|
||||
next.add(category);
|
||||
}
|
||||
return next;
|
||||
});
|
||||
};
|
||||
|
||||
const columns = [
|
||||
{
|
||||
title: "Icon",
|
||||
dataIndex: "cssClass",
|
||||
key: "icon",
|
||||
width: 48,
|
||||
render: (_: string, record: IconEntry) => (
|
||||
<div className={record.cssClass} style={{ imageRendering: "pixelated" }} />
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "Name",
|
||||
dataIndex: "name",
|
||||
key: "name",
|
||||
render: (name: string) => (
|
||||
<Text style={{ fontSize: 12 }}>{name}</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "Usage",
|
||||
dataIndex: "cssClass",
|
||||
key: "usage",
|
||||
render: (cssClass: string) => (
|
||||
<Text code style={{ fontSize: 11, color: "#90ee90" }}>
|
||||
{`<div class="${cssClass}" />`}
|
||||
</Text>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: "Path",
|
||||
dataIndex: "path",
|
||||
key: "path",
|
||||
render: (p: string | undefined) => (p
|
||||
? <Text type="secondary" style={{ fontSize: 11 }}>{p}</Text>
|
||||
: null),
|
||||
},
|
||||
];
|
||||
|
||||
const manifest = useExportManifest();
|
||||
|
||||
return (
|
||||
<PageLayout nav={[{ name: "Icon Reference" }]}>
|
||||
<PageHeader title="Icon Reference" />
|
||||
<div style={{ width: "80%", margin: "0 auto" }}>
|
||||
{!manifest && (
|
||||
<Card>
|
||||
<Text type="secondary">
|
||||
No export data available. Run export-data on an instance to generate icons.
|
||||
</Text>
|
||||
</Card>
|
||||
)}
|
||||
{manifest && manifest.exportedAt && (
|
||||
<div style={{ marginBottom: 12, fontSize: 12, lineHeight: 1.8 }}>
|
||||
{manifest.exportedAt && (
|
||||
<div>
|
||||
<Text type="secondary">Exported: </Text>
|
||||
<Text>{new Date(manifest.exportedAt).toLocaleString()}</Text>
|
||||
</div>
|
||||
)}
|
||||
<div>
|
||||
<Text type="secondary">Icons: </Text>
|
||||
<Text>{totalCount}</Text>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
<Card>
|
||||
<Space style={{ marginBottom: 16, display: "flex", alignItems: "center" }}>
|
||||
<Input
|
||||
placeholder="Filter by name or class…"
|
||||
value={filter}
|
||||
onChange={e => setFilter(e.target.value)}
|
||||
allowClear
|
||||
style={{ width: 320 }}
|
||||
/>
|
||||
<Text type="secondary" style={{ fontSize: 12, whiteSpace: "nowrap" }}>
|
||||
{visibleCount === totalCount
|
||||
? `${totalCount} icons`
|
||||
: `${visibleCount} / ${totalCount} icons`}
|
||||
</Text>
|
||||
</Space>
|
||||
{groups.map(({ category, entries }) => {
|
||||
const label = CATEGORY_LABELS[category as Category] ?? category;
|
||||
const expanded = isExpanded(category);
|
||||
return (
|
||||
<div key={category} style={{ marginBottom: 8 }}>
|
||||
<div
|
||||
onClick={() => toggleGroup(category)}
|
||||
style={{
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
gap: 8,
|
||||
padding: "6px 12px",
|
||||
background: "#1d1d1d",
|
||||
border: "1px solid #333",
|
||||
borderRadius: expanded ? "4px 4px 0 0" : 4,
|
||||
cursor: "pointer",
|
||||
userSelect: "none",
|
||||
}}
|
||||
>
|
||||
<span style={{ fontSize: 10, color: "#888", width: 12 }}>
|
||||
{expanded ? "▾" : "►"}
|
||||
</span>
|
||||
<Tag style={{ margin: 0 }}>{label}</Tag>
|
||||
<Text type="secondary" style={{ fontSize: 12 }}>
|
||||
{entries.length}
|
||||
</Text>
|
||||
</div>
|
||||
{expanded && (
|
||||
<Table<IconEntry>
|
||||
columns={columns}
|
||||
dataSource={entries}
|
||||
rowKey="key"
|
||||
size="small"
|
||||
showHeader={false}
|
||||
pagination={false}
|
||||
style={{ borderTop: "none" }}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</Card>
|
||||
</div>
|
||||
</PageLayout>
|
||||
);
|
||||
}
|
||||
@@ -4,10 +4,10 @@ import * as lib from "@clusterio/lib";
|
||||
import ControlContext from "../components/ControlContext";
|
||||
|
||||
let exportManifestCache: lib.ExportManifest|null = null;
|
||||
let emptyCache = { assets: {} };
|
||||
let loaded = false;
|
||||
export function useExportManifest(): lib.ExportManifest|null {
|
||||
const control = useContext(ControlContext);
|
||||
let [exportManifest, setExportManifest] = useState<lib.ExportManifest|null>(exportManifestCache || emptyCache);
|
||||
let [exportManifest, setExportManifest] = useState<lib.ExportManifest|null>(exportManifestCache);
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
let modPack = await control.send(new lib.ModPackGetDefaultRequest());
|
||||
@@ -15,9 +15,10 @@ export function useExportManifest(): lib.ExportManifest|null {
|
||||
exportManifestCache = modPack.exportManifest;
|
||||
setExportManifest(exportManifestCache);
|
||||
}
|
||||
loaded = true;
|
||||
}
|
||||
|
||||
if (!exportManifestCache) {
|
||||
if (!loaded) {
|
||||
load();
|
||||
}
|
||||
}, []);
|
||||
|
||||
@@ -7,31 +7,51 @@ type Metadata = {
|
||||
x: number;
|
||||
y: number;
|
||||
size: number;
|
||||
category: string;
|
||||
localised_name?: any[];
|
||||
path?: string;
|
||||
};
|
||||
let itemMetadataCache: Map<string, Metadata> | null = null;
|
||||
export function useItemMetadata() {
|
||||
let exportManifest = useExportManifest();
|
||||
let [itemMetadata, setItemMetadata] = useState<Map<string, Metadata>>(itemMetadataCache || new Map());
|
||||
|
||||
const metadataCaches = new Map<string, Map<string, Metadata>>();
|
||||
let cssInjected = false;
|
||||
|
||||
/**
|
||||
* Load the unified spritesheet metadata and inject CSS rules for every icon.
|
||||
* CSS class name: `.{category}-{CSS.escape(name)}`
|
||||
* e.g. `.item-iron-plate`, `.recipe-iron-plate`, `.signal-signal-red`, `.planet-nauvis`
|
||||
*/
|
||||
function useSpriteMetadata(category: string): Map<string, Metadata> {
|
||||
const exportManifest = useExportManifest();
|
||||
const [metadata, setMetadata] = useState<Map<string, Metadata>>(
|
||||
metadataCaches.get(category) ?? new Map()
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
async function load() {
|
||||
if (
|
||||
!exportManifest
|
||||
|| !exportManifest.assets["item-metadata"]
|
||||
|| !exportManifest.assets["item-spritesheet"]
|
||||
|| !exportManifest.assets["metadata"]
|
||||
|| !exportManifest.assets["spritesheet"]
|
||||
) {
|
||||
return;
|
||||
}
|
||||
let response = await fetch(`${staticRoot}static/${exportManifest.assets["item-metadata"]}`);
|
||||
if (response.ok) {
|
||||
let data = await response.json();
|
||||
itemMetadataCache = new Map(data);
|
||||
let style = document.createElement("style");
|
||||
|
||||
const response = await fetch(`${staticRoot}static/${exportManifest.assets["metadata"]}`);
|
||||
if (!response.ok) {
|
||||
return;
|
||||
}
|
||||
|
||||
const data: [string, Metadata][] = await response.json();
|
||||
|
||||
if (!cssInjected) {
|
||||
const sheetUrl = `${staticRoot}static/${exportManifest.assets["spritesheet"]}`;
|
||||
const style = document.createElement("style");
|
||||
document.head.appendChild(style);
|
||||
for (let [name, meta] of itemMetadataCache) {
|
||||
for (const [key, meta] of data) {
|
||||
const name = key.includes(".") ? key.slice(key.indexOf(".") + 1) : key;
|
||||
style.sheet!.insertRule(`\
|
||||
.item-${CSS.escape(name)} {
|
||||
background-image: url("${staticRoot}static/${exportManifest.assets["item-spritesheet"]}");
|
||||
.${meta.category}-${CSS.escape(name)} {
|
||||
background-image: url("${sheetUrl}");
|
||||
background-repeat: no-repeat;
|
||||
background-position: -${meta.x}px -${meta.y}px;
|
||||
height: ${meta.size}px;
|
||||
@@ -39,14 +59,32 @@ export function useItemMetadata() {
|
||||
}`
|
||||
);
|
||||
}
|
||||
setItemMetadata(itemMetadataCache);
|
||||
cssInjected = true;
|
||||
}
|
||||
|
||||
const cache = new Map<string, Metadata>();
|
||||
for (const [key, meta] of data) {
|
||||
if (meta.category === category) {
|
||||
const name = key.includes(".") ? key.slice(key.indexOf(".") + 1) : key;
|
||||
cache.set(name, meta);
|
||||
}
|
||||
}
|
||||
metadataCaches.set(category, cache);
|
||||
setMetadata(cache);
|
||||
}
|
||||
|
||||
if (!itemMetadataCache) {
|
||||
if (!metadataCaches.has(category)) {
|
||||
load();
|
||||
}
|
||||
}, [exportManifest]);
|
||||
|
||||
return itemMetadata;
|
||||
return metadata;
|
||||
}
|
||||
|
||||
export function useItemMetadata() { return useSpriteMetadata("item"); }
|
||||
export function useRecipeMetadata() { return useSpriteMetadata("recipe"); }
|
||||
export function useSignalMetadata() { return useSpriteMetadata("signal"); }
|
||||
export function useTechnologyMetadata() { return useSpriteMetadata("technology"); }
|
||||
export function usePlanetMetadata() { return useSpriteMetadata("planet"); }
|
||||
export function useQualityMetadata() { return useSpriteMetadata("quality"); }
|
||||
export function useEntityMetadata() { return useSpriteMetadata("entity"); }
|
||||
@@ -16,6 +16,7 @@ import RoleViewPage from "./components/RoleViewPage";
|
||||
import PluginsPage from "./components/PluginsPage";
|
||||
import PluginViewPage from "./components/PluginViewPage";
|
||||
import ErrorPage from "./components/ErrorPage";
|
||||
import IconReferencePage from "./components/IconReferencePage";
|
||||
|
||||
export const pages: PluginPage[] = [
|
||||
{
|
||||
@@ -88,6 +89,10 @@ export const pages: PluginPage[] = [
|
||||
sidebarPath: "/plugins",
|
||||
content: <PluginViewPage />,
|
||||
},
|
||||
{
|
||||
path: "/icon-reference",
|
||||
content: <IconReferencePage />,
|
||||
},
|
||||
{
|
||||
path: "/error",
|
||||
content: <ErrorPage throw error={new Error("Example Error Message")} />,
|
||||
|
||||
Reference in new issue
Block a user