Skip to content

Commit

Permalink
Fix crash with maps edited by Nro's Galaxy Editor
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMakesGames committed Sep 26, 2023
1 parent b41cfe7 commit 685c51c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stellarmaps",
"version": "0.6.1",
"version": "0.6.2",
"private": true,
"scripts": {
"dev": "vite dev",
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "stellarmaps"
version = "0.6.1"
version = "0.6.2"
description = "Stellaris map renderer"
authors = ["Michael Moore"]
license = ""
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
},
"package": {
"productName": "StellarMaps",
"version": "0.6.1"
"version": "0.6.2"
},
"tauri": {
"allowlist": {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/GameState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export interface GameState {
export interface GalacticObject {
coordinate: { x: number; y: number; origin: number; randomized: boolean };
starbases: number[];
hyperlane?: { to: number; length: number }[];
hyperlane?: { to: number; length: number }[] | Record<string, never>;
megastructures?: number[];
colonies?: number[];
}
Expand Down
15 changes: 9 additions & 6 deletions src/lib/processMapData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ export default async function processMapData(gameState: GameState, settings: Map
const hyperlanes = new Set<string>();
const relayHyperlanes = new Set<string>();
Object.entries(gameState.galactic_object).forEach(([goId, go]) => {
for (const hyperlane of go.hyperlane ?? []) {
for (const hyperlane of getGameStateValueAsArray(go.hyperlane)) {
const isRelay =
go.megastructures?.some((id) => relayMegastructures.has(id)) &&
gameState.galactic_object[hyperlane.to].megastructures?.some((id) =>
Expand Down Expand Up @@ -1091,10 +1091,13 @@ function smoothPositionArrayIteration(
return smoothed;
}

function getGameStateValueAsArray<T>(value: null | undefined | T | T[]): T[] {
function getGameStateValueAsArray<T>(
value: null | undefined | Record<string, never> | T | T[],
): T[] {
if (value == null) return [];
if (Array.isArray(value)) return value;
return [value];
if (typeof value === 'object' && Object.keys(value).length === 0) return [];
return [value as T];
}

const CIRCLE_OUTER_PADDING = 20;
Expand Down Expand Up @@ -1141,8 +1144,8 @@ function processCircularGalaxyBorders(gameState: GameState, settings: MapSetting
const next = gameState.galactic_object[nextId];
if (next && !cluster.systems.has(nextId)) {
cluster.systems.add(nextId);
const isOutlier =
next.hyperlane?.length === 1 && next.hyperlane[0].length > OUTLIER_DISTANCE;
const nextHyperlanes = getGameStateValueAsArray(next.hyperlane);
const isOutlier = nextHyperlanes.length === 1 && nextHyperlanes.length > OUTLIER_DISTANCE;
if (isOutlier) {
cluster.outliers.add(nextId);
} else {
Expand All @@ -1151,7 +1154,7 @@ function processCircularGalaxyBorders(gameState: GameState, settings: MapSetting
if (next.coordinate.y < cluster.bBox.yMin) cluster.bBox.yMin = next.coordinate.y;
if (next.coordinate.y > cluster.bBox.yMax) cluster.bBox.yMax = next.coordinate.y;
}
for (const hyperlane of next.hyperlane ?? []) {
for (const hyperlane of nextHyperlanes) {
if (!cluster.systems.has(hyperlane.to) && !edgeSet.has(hyperlane.to)) {
edge.push(hyperlane.to);
edgeSet.add(hyperlane.to);
Expand Down

0 comments on commit 685c51c

Please sign in to comment.