Skip to content

Commit

Permalink
fix(ingame options): don't load from the static folder at runtime
Browse files Browse the repository at this point in the history
I throughly hate this code and what I had to do. This is a band-aid fix until we figure out something better.

I cannot wait for this to be the permanent fix.
  • Loading branch information
AnthonyFuller committed Oct 31, 2024
1 parent 2094867 commit 1e42371
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions components/menus/settings.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { existsSync, readFileSync } from "fs"
import path from "path"
import { IIniObjectSection, IniValue } from "js-ini"
import { menuSystemDatabase } from "./menuSystem"
import { Flag, FlagSection, GameVersion } from "../types/types"
import { defaultFlags, getAllFlags, saveFlags, setFlag } from "../flags"
import { CommandFunction, commandService } from "../commandService"
// @ts-expect-error This is fine.
import PeacockMenuIndex from "../../static/peacock-menu/index.json"
// @ts-expect-error This is fine.
import PeacockMenuOptions from "../../static/peacock-menu/options.json"
// @ts-expect-error This is fine.
import PeacockMenuFlagIndex from "../../static/peacock-menu/flags/index.json"
// @ts-expect-error This is fine.
import PeacockMenuFlagCategory from "../../static/peacock-menu/flags/category.json"
// @ts-expect-error This is fine.
import PeacockMenuFlag from "../../static/peacock-menu/flags/flag.json"

interface GetAllFlagsResponse {
key: string
Expand All @@ -27,7 +35,18 @@ const commandMap = new Map<string, CommandFunction>([
["setFlagEnum", commandSetFlagEnum as CommandFunction],
])

const pluginPrefix = "/pages/peacock-menu/"
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const builtInPages: Record<string, any> = {
"/pages/peacock-menu/index.json": JSON.parse(PeacockMenuIndex),
"/pages/peacock-menu/options.json": JSON.parse(PeacockMenuOptions),
"/pages/peacock-menu/flags/index.json": JSON.parse(PeacockMenuFlagIndex),
"/pages/peacock-menu/flags/category.json": JSON.parse(
PeacockMenuFlagCategory,
),
"/pages/peacock-menu/flags/flag.json": JSON.parse(PeacockMenuFlag),
}

const pagePrefix = "/pages/peacock-menu/"
const jsonExtension = ".json"

function getDatabaseDiff(configs: string[], gameVersion: GameVersion) {
Expand All @@ -44,29 +63,17 @@ function getConfig(name: string, _gameVersion: GameVersion) {
name = "/pages/peacock-menu/index.json"
}

if (!name.startsWith(pluginPrefix)) {
if (!name.startsWith(pagePrefix)) {
return
}

const fileName = name.substring(pluginPrefix.length)
const cacheBusterIndex = fileName.indexOf(jsonExtension)
const fileNameWithCacheBuster =
const cacheBusterIndex = name.indexOf(jsonExtension)
const fileNameNoCacheBuster =
cacheBusterIndex < 0
? fileName
: fileName.substring(0, cacheBusterIndex + jsonExtension.length)

const filePath = path.join(
process.cwd(),
"static",
"peacock-menu",
fileNameWithCacheBuster,
)

if (existsSync(filePath)) {
return JSON.parse(readFileSync(filePath).toString())
}
? name
: name.substring(0, cacheBusterIndex + jsonExtension.length)

return undefined
return builtInPages[fileNameNoCacheBuster] ?? undefined
}

function getFlagType(
Expand Down

0 comments on commit 1e42371

Please sign in to comment.