Tie data exports to mod packs

Store the export manifest in ModPack to allow mulitple mod packs to have
different data exported. This is necessary as the export contains the
settings prototypes needed to create a proper UI for mod settings.

For now the useLocale and useItemMetadata interfaces uses the manifest
for a default mod pack set in the master config.
This commit is contained in:
Hornwitser committed 2023-02-02 00:28:58 +01:00
1 parent 7c6aaeb05d
commit 13e12f7335
16 files changed
+146 -34

No files matched your search

@@ -1,15 +1,20 @@
import { useEffect, useState } from "react";
import { useEffect, useContext, useState } from "react";
import { libData, libLink } from "@clusterio/lib";
import ControlContext from "../components/ControlContext";
let exportManifestCache = null;
let emptyCache = {};
export function useExportManifest() {
const control = useContext(ControlContext);
let [exportManifest, setExportManifest] = useState(exportManifestCache || emptyCache);
useEffect(() => {
async function load() {
let response = await fetch(`${webRoot}api/export-manifest`);
if (response.ok) {
exportManifestCache = await response.json();
let result = await libLink.messages.getDefaultModPack.send(control);
let modPack = new libData.ModPack(result.mod_pack);
if (modPack.exportManifest) {
exportManifestCache = modPack.exportManifest;
setExportManifest(exportManifestCache);
}
}
+7 -3
View File
@@ -9,10 +9,14 @@ export function useItemMetadata() {
let [itemMetadata, setItemMetadata] = useState(itemMetadataCache || new Map());
useEffect(() => {
async function load() {
if (!exportManifest["item-metadata"] || !exportManifest["item-spritesheet"]) {
if (
!exportManifest
|| !exportManifest.assets["item-metadata"]
|| !exportManifest.assets["item-spritesheet"]
) {
return;
}
let response = await fetch(`${staticRoot}${exportManifest["item-metadata"]}`);
let response = await fetch(`${staticRoot}static/${exportManifest.assets["item-metadata"]}`);
if (response.ok) {
let data = await response.json();
itemMetadataCache = new Map(data);
@@ -22,7 +26,7 @@ export function useItemMetadata() {
for (let [name, meta] of itemMetadataCache) {
style.sheet.insertRule(
`.item-${CSS.escape(name)} {
background-image: url("${staticRoot}${exportManifest["item-spritesheet"]}");
background-image: url("${staticRoot}static/${exportManifest.assets["item-spritesheet"]}");
background-repeat: no-repeat;
background-position: -${meta.x}px -${meta.y}px;
height: ${meta.size}px;
+2 -2
View File
@@ -9,10 +9,10 @@ export function useLocale() {
let [locale, setLocale] = useState(localeCache || new Map());
useEffect(() => {
async function load() {
if (!exportManifest["locale"]) {
if (!exportManifest || !exportManifest.assets["locale"]) {
return;
}
let response = await fetch(`${staticRoot}${exportManifest["locale"]}`);
let response = await fetch(`${staticRoot}static/${exportManifest.assets["locale"]}`);
if (response.ok) {
let data = await response.json();
localeCache = new Map(data);