Skip to content

Commit

Permalink
feat(map): implemented Stellaris language setting, changing which loc…
Browse files Browse the repository at this point in the history
… files are loaded
  • Loading branch information
MichaelMakesGames committed May 1, 2024
1 parent 1665e8f commit 46830c2
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 18 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ There is an Electron version available for local development. Some changes to th
This exists mostly for debugging in Chromium. The release builds all use Tauri. Currently, the Electron version lacks the following features:

- loading data (localization, emblems, colors) from Stellaris mods
- loading non-English Stellaris localization
- translator mode
- (temporarily) country emblems
- production builds
8 changes: 4 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ async fn get_stellaris_colors_cmd(path: String) -> Result<Vec<String>, String> {
}

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

#[tauri::command]
Expand Down Expand Up @@ -359,7 +359,7 @@ fn get_stellaris_data_paths(
.collect();
}

fn get_stellaris_loc(path: String) -> anyhow::Result<HashMap<String, String>> {
fn get_stellaris_loc(path: String, language: String) -> anyhow::Result<HashMap<String, String>> {
let loc_file_paths = get_stellaris_data_paths(
Path::new(&path).to_path_buf(),
Path::new("localisation").to_path_buf(),
Expand All @@ -384,7 +384,7 @@ fn get_stellaris_loc(path: String) -> anyhow::Result<HashMap<String, String>> {
line_number += 1;
let _ = buf_reader.read_line(&mut first_line)?;
}
if first_line.contains("l_english") {
if first_line.contains(language.as_str()) {
let re = Regex::new(r#"(?m)^\s*([\w\.\-]+)\s*:\d*\s*"(.*)".*$"#).unwrap();
let mut raw_content = String::new();
let _ = buf_reader.read_to_string(&mut raw_content)?;
Expand Down
17 changes: 14 additions & 3 deletions src/renderer/src/lib/loadStellarisData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { hsv } from 'd3-hsv';
import { get, writable } from 'svelte/store';
import { jsonify, tokenize } from '../../../shared/parseSave';
import { ADDITIONAL_COLORS } from './colors';
import { appSettings } from './settings';
import stellarMapsApi from './stellarMapsApi';
import { timeItAsync } from './utils';

Expand All @@ -14,6 +15,14 @@ export const stellarisDataPromiseStore = writable(
}),
);

// reload data if appStellarisLanguage setting changed
let loadedStellarisLanguage = get(appSettings).appStellarisLanguage;
appSettings.subscribe((value) => {
if (value.appStellarisLanguage !== loadedStellarisLanguage) {
loadStellarisData();
}
});

export function loadStellarisData() {
const stellarisDataPromise = loadStellarisDataUnwrapped();
stellarisDataPromiseStore.set(stellarisDataPromise);
Expand All @@ -23,12 +32,14 @@ export function loadStellarisData() {
async function loadStellarisDataUnwrapped() {
const path = get(stellarisPathStore) || (await stellarMapsApi.loadStellarisInstallDir());
stellarisPathStore.set(path);
const [colors, loc] = await Promise.all([loadColors(path), loadLoc(path)]);
const language = get(appSettings).appStellarisLanguage;
loadedStellarisLanguage = language;
const [colors, loc] = await Promise.all([loadColors(path), loadLoc(path, language)]);
return { colors, loc };
}

function loadLoc(path: string) {
return timeItAsync('loadLoc', stellarMapsApi.loadLoc, path);
function loadLoc(path: string, language: string) {
return timeItAsync('loadLoc', stellarMapsApi.loadLoc, path, language);
}

async function loadColors(path: string): Promise<Record<string, string>> {
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/src/lib/map/MapContainer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
stellarisDataPromiseStore,
stellarisPathStore,
} from '../loadStellarisData';
import { lastProcessedMapSettings, mapSettings } from '../settings';
import { appStellarisLanguage, lastProcessedMapSettings, mapSettings } from '../settings';
import stellarMapsApi from '../stellarMapsApi';
import { debounce, timeItAsync, toastError } from '../utils';
import Map from './Map.svelte';
Expand All @@ -30,8 +30,9 @@
const modalStore = getModalStore();
$: mapDataPromise =
$gameStatePromise?.then((gs) => processMapData(gs, $lastProcessedMapSettings)) ??
new Promise<Awaited<ReturnType<typeof processMapData>>>(() => {});
$gameStatePromise?.then((gs) =>
processMapData(gs, $lastProcessedMapSettings, $appStellarisLanguage),
) ?? new Promise<Awaited<ReturnType<typeof processMapData>>>(() => {});
loadStellarisData();
const toastStore = getToastStore();
Expand Down
10 changes: 7 additions & 3 deletions src/renderer/src/lib/map/data/processMapData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as R from 'rambda';
import { get } from 'svelte/store';
import type { GameState } from '../../GameState';
import debug from '../../debug';
import type { MapSettings } from '../../settings';
import { type MapSettings } from '../../settings';
import { timeIt, timeItAsync } from '../../utils';
import processBorders, { processBordersDeps } from './processBorders';
import processBypassLinks from './processBypassLinks';
Expand All @@ -24,7 +24,11 @@ import processTerraIncognitaPath, {
import processVoronoi, { processVoronoiDeps } from './processVoronoi';
import { createHyperlanePaths } from './utils';

export default async function processMapData(gameState: GameState, rawSettings: MapSettings) {
export default async function processMapData(
gameState: GameState,
rawSettings: MapSettings,
language: string,
) {
console.time('TOTAL PROCESSING TIME');
const settings = { ...rawSettings };
if (settings.hyperlaneMetroStyle) settings.alignStarsToGrid = true;
Expand All @@ -36,7 +40,7 @@ export default async function processMapData(gameState: GameState, rawSettings:
cached(processEmblems),
Object.values(gameState.country),
);
const countryNamesPromise = timeItAsync('names', cached(processNames), gameState);
const countryNamesPromise = timeItAsync('names', cached(processNames), gameState, language);

const getSystemCoordinates = timeIt(
'system coordinates',
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/src/lib/map/data/processNames.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { Country, GameState } from '../../GameState';
import { stellarisDataPromiseStore } from '../../loadStellarisData';
import { localizeTextSync } from './locUtils';

export default async function processNames(gameState: GameState) {
// _language is just here to control caching
export default async function processNames(gameState: GameState, _language: string) {
const countryNames = await localizeCountryNames(gameState.country);
return countryNames;
}
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/src/lib/settings/appSettings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { localStorageStore } from '@skeletonlabs/skeleton';
import { get } from 'svelte/store';
import { derived, get } from 'svelte/store';
import { locale } from '../../intl';
import stellarMapsApi from '../stellarMapsApi';
import { disableTranslatorMode, enableTranslatorMode } from '../translatorMode';
Expand Down Expand Up @@ -27,6 +27,7 @@ const defaultAppSettings: AppSettings = {
};

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

function loadSettings() {
return getAppSettingsPath()
Expand All @@ -39,10 +40,13 @@ function loadSettings() {
.then((settings) => appSettings.set(settings))
.then(() => {
appSettings.subscribe((settings) => {
// update locale
locale.set(settings.appLocale as Parameters<(typeof locale)['set']>[0]);
// write to file
createAppConfigDirIfNeeded()
.then(getAppSettingsPath)
.then((path) => stellarMapsApi.fs.writeFile(path, JSON.stringify(settings)));
// toggle translator mode
if (settings.appTranslatorMode) {
enableTranslatorMode();
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/lib/stellarMapsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ if (stellarMapsApi == null) {
loadColors(path) {
return invoke('get_stellaris_colors_cmd', { path });
},
loadLoc(path) {
return invoke('get_stellaris_loc_cmd', { path });
loadLoc(path, language) {
return invoke('get_stellaris_loc_cmd', { path, language });
},
loadStellarisInstallDir() {
return invoke('get_stellaris_install_dir_cmd');
Expand Down
2 changes: 1 addition & 1 deletion src/shared/StellarMapsApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ interface StellarisSaveMetadata {

export interface StellarMapsAPI {
loadSaveMetadata(): Promise<[StellarisSaveMetadata, ...StellarisSaveMetadata[]][]>;
loadLoc(installPath: string): Promise<Record<string, string>>;
loadLoc(installPath: string, language: string): Promise<Record<string, string>>;
loadColors(installPath: string): Promise<string[]>;
loadStellarisInstallDir(): Promise<string>;
loadSave(savePath: string): Promise<unknown>;
Expand Down

0 comments on commit 46830c2

Please sign in to comment.