Skip to content

Commit 46830c2

Browse files
feat(map): implemented Stellaris language setting, changing which loc files are loaded
1 parent 1665e8f commit 46830c2

9 files changed

Lines changed: 42 additions & 18 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ There is an Electron version available for local development. Some changes to th
5252
This exists mostly for debugging in Chromium. The release builds all use Tauri. Currently, the Electron version lacks the following features:
5353

5454
- loading data (localization, emblems, colors) from Stellaris mods
55+
- loading non-English Stellaris localization
56+
- translator mode
57+
- (temporarily) country emblems
5558
- production builds

src-tauri/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ async fn get_stellaris_colors_cmd(path: String) -> Result<Vec<String>, String> {
5959
}
6060

6161
#[tauri::command]
62-
async fn get_stellaris_loc_cmd(path: String) -> Result<HashMap<String, String>, String> {
63-
return get_stellaris_loc(path).map_err(|err| err.to_string());
62+
async fn get_stellaris_loc_cmd(path: String, language: String) -> Result<HashMap<String, String>, String> {
63+
return get_stellaris_loc(path, language).map_err(|err| err.to_string());
6464
}
6565

6666
#[tauri::command]
@@ -359,7 +359,7 @@ fn get_stellaris_data_paths(
359359
.collect();
360360
}
361361

362-
fn get_stellaris_loc(path: String) -> anyhow::Result<HashMap<String, String>> {
362+
fn get_stellaris_loc(path: String, language: String) -> anyhow::Result<HashMap<String, String>> {
363363
let loc_file_paths = get_stellaris_data_paths(
364364
Path::new(&path).to_path_buf(),
365365
Path::new("localisation").to_path_buf(),
@@ -384,7 +384,7 @@ fn get_stellaris_loc(path: String) -> anyhow::Result<HashMap<String, String>> {
384384
line_number += 1;
385385
let _ = buf_reader.read_line(&mut first_line)?;
386386
}
387-
if first_line.contains("l_english") {
387+
if first_line.contains(language.as_str()) {
388388
let re = Regex::new(r#"(?m)^\s*([\w\.\-]+)\s*:\d*\s*"(.*)".*$"#).unwrap();
389389
let mut raw_content = String::new();
390390
let _ = buf_reader.read_to_string(&mut raw_content)?;

src/renderer/src/lib/loadStellarisData.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { hsv } from 'd3-hsv';
44
import { get, writable } from 'svelte/store';
55
import { jsonify, tokenize } from '../../../shared/parseSave';
66
import { ADDITIONAL_COLORS } from './colors';
7+
import { appSettings } from './settings';
78
import stellarMapsApi from './stellarMapsApi';
89
import { timeItAsync } from './utils';
910

@@ -14,6 +15,14 @@ export const stellarisDataPromiseStore = writable(
1415
}),
1516
);
1617

18+
// reload data if appStellarisLanguage setting changed
19+
let loadedStellarisLanguage = get(appSettings).appStellarisLanguage;
20+
appSettings.subscribe((value) => {
21+
if (value.appStellarisLanguage !== loadedStellarisLanguage) {
22+
loadStellarisData();
23+
}
24+
});
25+
1726
export function loadStellarisData() {
1827
const stellarisDataPromise = loadStellarisDataUnwrapped();
1928
stellarisDataPromiseStore.set(stellarisDataPromise);
@@ -23,12 +32,14 @@ export function loadStellarisData() {
2332
async function loadStellarisDataUnwrapped() {
2433
const path = get(stellarisPathStore) || (await stellarMapsApi.loadStellarisInstallDir());
2534
stellarisPathStore.set(path);
26-
const [colors, loc] = await Promise.all([loadColors(path), loadLoc(path)]);
35+
const language = get(appSettings).appStellarisLanguage;
36+
loadedStellarisLanguage = language;
37+
const [colors, loc] = await Promise.all([loadColors(path), loadLoc(path, language)]);
2738
return { colors, loc };
2839
}
2940

30-
function loadLoc(path: string) {
31-
return timeItAsync('loadLoc', stellarMapsApi.loadLoc, path);
41+
function loadLoc(path: string, language: string) {
42+
return timeItAsync('loadLoc', stellarMapsApi.loadLoc, path, language);
3243
}
3344

3445
async function loadColors(path: string): Promise<Record<string, string>> {

src/renderer/src/lib/map/MapContainer.svelte

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
stellarisDataPromiseStore,
1919
stellarisPathStore,
2020
} from '../loadStellarisData';
21-
import { lastProcessedMapSettings, mapSettings } from '../settings';
21+
import { appStellarisLanguage, lastProcessedMapSettings, mapSettings } from '../settings';
2222
import stellarMapsApi from '../stellarMapsApi';
2323
import { debounce, timeItAsync, toastError } from '../utils';
2424
import Map from './Map.svelte';
@@ -30,8 +30,9 @@
3030
const modalStore = getModalStore();
3131
3232
$: mapDataPromise =
33-
$gameStatePromise?.then((gs) => processMapData(gs, $lastProcessedMapSettings)) ??
34-
new Promise<Awaited<ReturnType<typeof processMapData>>>(() => {});
33+
$gameStatePromise?.then((gs) =>
34+
processMapData(gs, $lastProcessedMapSettings, $appStellarisLanguage),
35+
) ?? new Promise<Awaited<ReturnType<typeof processMapData>>>(() => {});
3536
3637
loadStellarisData();
3738
const toastStore = getToastStore();

src/renderer/src/lib/map/data/processMapData.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as R from 'rambda';
22
import { get } from 'svelte/store';
33
import type { GameState } from '../../GameState';
44
import debug from '../../debug';
5-
import type { MapSettings } from '../../settings';
5+
import { type MapSettings } from '../../settings';
66
import { timeIt, timeItAsync } from '../../utils';
77
import processBorders, { processBordersDeps } from './processBorders';
88
import processBypassLinks from './processBypassLinks';
@@ -24,7 +24,11 @@ import processTerraIncognitaPath, {
2424
import processVoronoi, { processVoronoiDeps } from './processVoronoi';
2525
import { createHyperlanePaths } from './utils';
2626

27-
export default async function processMapData(gameState: GameState, rawSettings: MapSettings) {
27+
export default async function processMapData(
28+
gameState: GameState,
29+
rawSettings: MapSettings,
30+
language: string,
31+
) {
2832
console.time('TOTAL PROCESSING TIME');
2933
const settings = { ...rawSettings };
3034
if (settings.hyperlaneMetroStyle) settings.alignStarsToGrid = true;
@@ -36,7 +40,7 @@ export default async function processMapData(gameState: GameState, rawSettings:
3640
cached(processEmblems),
3741
Object.values(gameState.country),
3842
);
39-
const countryNamesPromise = timeItAsync('names', cached(processNames), gameState);
43+
const countryNamesPromise = timeItAsync('names', cached(processNames), gameState, language);
4044

4145
const getSystemCoordinates = timeIt(
4246
'system coordinates',

src/renderer/src/lib/map/data/processNames.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { Country, GameState } from '../../GameState';
33
import { stellarisDataPromiseStore } from '../../loadStellarisData';
44
import { localizeTextSync } from './locUtils';
55

6-
export default async function processNames(gameState: GameState) {
6+
// _language is just here to control caching
7+
export default async function processNames(gameState: GameState, _language: string) {
78
const countryNames = await localizeCountryNames(gameState.country);
89
return countryNames;
910
}

src/renderer/src/lib/settings/appSettings.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { localStorageStore } from '@skeletonlabs/skeleton';
2-
import { get } from 'svelte/store';
2+
import { derived, get } from 'svelte/store';
33
import { locale } from '../../intl';
44
import stellarMapsApi from '../stellarMapsApi';
55
import { disableTranslatorMode, enableTranslatorMode } from '../translatorMode';
@@ -27,6 +27,7 @@ const defaultAppSettings: AppSettings = {
2727
};
2828

2929
export const appSettings = localStorageStore('appSettings', defaultAppSettings);
30+
export const appStellarisLanguage = derived(appSettings, (value) => value.appStellarisLanguage);
3031

3132
function loadSettings() {
3233
return getAppSettingsPath()
@@ -39,10 +40,13 @@ function loadSettings() {
3940
.then((settings) => appSettings.set(settings))
4041
.then(() => {
4142
appSettings.subscribe((settings) => {
43+
// update locale
4244
locale.set(settings.appLocale as Parameters<(typeof locale)['set']>[0]);
45+
// write to file
4346
createAppConfigDirIfNeeded()
4447
.then(getAppSettingsPath)
4548
.then((path) => stellarMapsApi.fs.writeFile(path, JSON.stringify(settings)));
49+
// toggle translator mode
4650
if (settings.appTranslatorMode) {
4751
enableTranslatorMode();
4852
} else {

src/renderer/src/lib/stellarMapsApi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ if (stellarMapsApi == null) {
2727
loadColors(path) {
2828
return invoke('get_stellaris_colors_cmd', { path });
2929
},
30-
loadLoc(path) {
31-
return invoke('get_stellaris_loc_cmd', { path });
30+
loadLoc(path, language) {
31+
return invoke('get_stellaris_loc_cmd', { path, language });
3232
},
3333
loadStellarisInstallDir() {
3434
return invoke('get_stellaris_install_dir_cmd');

src/shared/StellarMapsApi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface StellarisSaveMetadata {
77

88
export interface StellarMapsAPI {
99
loadSaveMetadata(): Promise<[StellarisSaveMetadata, ...StellarisSaveMetadata[]][]>;
10-
loadLoc(installPath: string): Promise<Record<string, string>>;
10+
loadLoc(installPath: string, language: string): Promise<Record<string, string>>;
1111
loadColors(installPath: string): Promise<string[]>;
1212
loadStellarisInstallDir(): Promise<string>;
1313
loadSave(savePath: string): Promise<unknown>;

0 commit comments

Comments
 (0)