Files
NiemandKG ad2154b38c Release checklist: no cheat console for players, Steam required, Steam's language wins when changed, menu links, head bob toggle
- The cheat console opens only in the editor, a development build, or with -gcdev
- Without steam_appid.txt next to the exe, a failed Steam start says so and quits instead of running offline; -nosteam only works in our own builds
- A language picked in the game's Steam properties overrides the one saved in the game, if it changed since
- Main menu: language button, Steam page (overlay, else browser), feedback (Steam discussions); "early build" and the AppID gone from the footer
- Settings > Game: head bob on/off
- The cursor stays inside the window while it's free
- -resetsettings puts every setting back as it comes out of the box

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 18:45:17 +06:00

276 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using UnityEngine;
namespace GrassCutter
{
/// <summary>
/// Interface languages. Every player-facing string is written in English in the code and looked up here by that
/// English text: <c>Loc.T("PLAY")</c>, or <c>Loc.F("{0} sectors clean", n)</c> for text with values in it. A missing
/// translation shows the English. Tables are <c>Resources/Loc/&lt;code&gt;.txt</c>, one "english TAB translation" per line
/// (Tools/loc/extract.py lists every key the code uses). Text kept in data tables (tool names, perk descriptions…)
/// is marked <c>Loc.N("…")</c> where it is written and translated with <c>Loc.T</c> where it is shown.
/// </summary>
public static class Loc
{
public class Lang
{
public string code, steam, native;
/// <summary>Numbers are written the way this culture writes them.</summary>
public string culture;
/// <summary>Chinese, Japanese, Korean: drawn with the Noto CJK cut, not LilitaOne/Nunito.</summary>
public string cjkFont;
/// <summary>LilitaOne has no Cyrillic, nor Polish or Turkish letters: these draw headings in Nunito Black.</summary>
public bool latinDisplayOnly;
}
/// <summary>The twelve ticked as "Interface" on the Steam page, English first. <c>steam</c> is what Steam's
/// GetCurrentGameLanguage() answers.</summary>
public static readonly Lang[] All =
{
new Lang { code = "en", steam = "english", native = "English", culture = "en-US" },
new Lang { code = "fr", steam = "french", native = "Français", culture = "fr-FR" },
new Lang { code = "it", steam = "italian", native = "Italiano", culture = "it-IT" },
new Lang { code = "de", steam = "german", native = "Deutsch", culture = "de-DE" },
new Lang { code = "es", steam = "spanish", native = "Español", culture = "es-ES" },
new Lang { code = "pt-BR", steam = "brazilian", native = "Português (Brasil)", culture = "pt-BR" },
new Lang { code = "pl", steam = "polish", native = "Polski", culture = "pl-PL", latinDisplayOnly = true },
new Lang { code = "tr", steam = "turkish", native = "Türkçe", culture = "tr-TR", latinDisplayOnly = true },
new Lang { code = "ru", steam = "russian", native = "Русский", culture = "ru-RU", latinDisplayOnly = true },
new Lang { code = "zh-Hans", steam = "schinese", native = "简体中文", culture = "zh-CN", cjkFont = "GcCjkSC" },
new Lang { code = "ja", steam = "japanese", native = "日本語", culture = "ja-JP", cjkFont = "GcCjkJP" },
new Lang { code = "ko", steam = "koreana", native = "한국어", culture = "ko-KR", cjkFont = "GcCjkKR" },
};
public static Lang Current { get; private set; } = All[0];
public static string Code => Current.code;
public static bool IsEnglish => Current == All[0];
/// <summary>For numbers on screen (thousands, decimals). Never for saves or the network: those stay invariant.</summary>
public static CultureInfo Culture { get; private set; } = CultureInfo.GetCultureInfo("en-US");
/// <summary>After the language changes: screens rebuild themselves.</summary>
public static event Action Changed;
static Dictionary<string, string> _table;
static bool _ready;
/// <summary>The English text in the current language, or the English if it has no translation.</summary>
public static string T(string en)
{
if (!_ready) Init();
if (string.IsNullOrEmpty(en) || _table == null) return en;
return _table.TryGetValue(en, out var s) && !string.IsNullOrEmpty(s) ? s : en;
}
/// <summary>T, then string.Format with this language's culture: "{0:N0} sectors" etc.</summary>
public static string F(string en, params object[] args)
{
var fmt = T(en);
try { return string.Format(Culture, fmt, args); }
catch (FormatException)
{
// a broken translation must never take the UI down: fall back to the English pattern
try { return string.Format(Culture, en, args); } catch (FormatException) { return en; }
}
}
/// <summary>Marks English kept in a data table for the extractor; the table hands it to T where it is shown.</summary>
public static string N(string en) => en;
/// <summary>A number the way the language writes it ("1,234.5", "1 234,5", "1.234,5").</summary>
public static string Num(double v, string format = "N0") => v.ToString(format, Culture);
public static string Num(float v, string format) => v.ToString(format, Culture);
// ───────────────────────── text sent over the network ─────────────────────────
const char PackMark = '\u0001', ArgMark = '\u0002', KeyMark = '\u0003';
/// <summary>Server text (announcements) goes out as the English pattern and its values, and every client reads it
/// in its own language. Arguments that are themselves English keys go through <see cref="Key"/>.</summary>
public static string Pack(string en, params string[] args)
{
var sb = new StringBuilder();
sb.Append(PackMark).Append(en);
foreach (var a in args) sb.Append(ArgMark).Append(a ?? "");
return sb.ToString();
}
/// <summary>An argument of <see cref="Pack"/> that the reader translates too (a tool or machine name).</summary>
public static string Key(string en) => KeyMark + en;
/// <summary>The reader's side of <see cref="Pack"/>; plain text goes through T.</summary>
public static string Unpack(string text)
{
if (string.IsNullOrEmpty(text)) return text;
if (text[0] != PackMark) return text[0] == KeyMark ? Arg(text) : T(text);
var parts = text.Substring(1).Split(ArgMark);
var args = new object[parts.Length - 1];
for (int i = 1; i < parts.Length; i++) args[i - 1] = Arg(parts[i]);
return F(parts[0], args);
}
/// <summary>A translated name with something after it, like "Sickle" + " III".</summary>
public static string Key(string en, string suffix) => KeyMark + en + SuffixMark + suffix;
const char SuffixMark = '\u0004';
static string Arg(string a)
{
if (a.Length == 0 || a[0] != KeyMark) return a;
a = a.Substring(1);
int s = a.IndexOf(SuffixMark);
return s < 0 ? T(a) : T(a.Substring(0, s)) + " " + a.Substring(s + 1);
}
// ───────────────────────── choosing ─────────────────────────
const string Pref = "s_lang";
/// <summary>Steam's language when the choice was saved: a new one picked in the game's Steam properties wins over it.</summary>
const string SteamPref = "s_lang_steam";
/// <summary>Saved choice, else Steam's language for this game, else the system's, else English. A language
/// changed in Steam's properties since the choice was saved overrides the choice.</summary>
public static void Init()
{
if (_ready) return;
_ready = true;
string code = CommandLine.Get("-demolang") ?? CommandLine.Get("-lang");
string steam = FromSteam(), steamThen = PlayerPrefs.GetString(SteamPref, "");
if (Find(code) == null && Find(steam) != null && steamThen.Length > 0 && steamThen != steam)
{
code = steam;
PlayerPrefs.SetString(Pref, Find(steam).code);
PlayerPrefs.SetString(SteamPref, steam);
PlayerPrefs.Save();
}
if (Find(code) == null) code = PlayerPrefs.GetString(Pref, "");
if (Find(code) == null) code = steam;
if (Find(code) == null) code = FromSystem();
Load(Find(code) ?? All[0]);
}
public static Lang Find(string code)
{
if (string.IsNullOrEmpty(code)) return null;
foreach (var l in All) if (string.Equals(l.code, code, StringComparison.OrdinalIgnoreCase) || l.steam == code) return l;
return null;
}
/// <summary>The settings screen's choice: remembered, and the screens rebuild. The language shots switch without remembering.</summary>
public static void Set(string code, bool remember = true)
{
var l = Find(code) ?? All[0];
if (remember)
{
PlayerPrefs.SetString(Pref, l.code);
PlayerPrefs.SetString(SteamPref, FromSteam() ?? "");
PlayerPrefs.Save();
}
_ready = true;
if (l == Current && _table != null) return;
Load(l);
Changed?.Invoke();
}
static string FromSteam()
{
try { return SteamManager.Initialized ? Steamworks.SteamApps.GetCurrentGameLanguage() : null; }
catch (Exception) { return null; }
}
static string FromSystem()
{
switch (Application.systemLanguage)
{
case SystemLanguage.French: return "fr";
case SystemLanguage.Italian: return "it";
case SystemLanguage.German: return "de";
case SystemLanguage.Spanish: return "es";
case SystemLanguage.Portuguese: return "pt-BR";
case SystemLanguage.Polish: return "pl";
case SystemLanguage.Turkish: return "tr";
case SystemLanguage.Russian: case SystemLanguage.Ukrainian: case SystemLanguage.Belarusian: return "ru";
case SystemLanguage.Chinese: case SystemLanguage.ChineseSimplified: return "zh-Hans";
case SystemLanguage.Japanese: return "ja";
case SystemLanguage.Korean: return "ko";
default: return "en";
}
}
static void Load(Lang l)
{
Current = l;
try { Culture = CultureInfo.GetCultureInfo(l.culture); }
catch (CultureNotFoundException) { Culture = CultureInfo.InvariantCulture; }
_table = null;
if (l == All[0]) return;
var asset = Resources.Load<TextAsset>("Loc/" + l.code);
if (!asset) { Debug.LogWarning($"[Loc] no table for {l.code}"); return; }
_table = Parse(asset.text);
Resources.UnloadAsset(asset);
Debug.Log($"[Loc] {l.code}: {_table.Count} strings");
}
/// <summary>"english TAB translation" per line; # starts a comment; \n \t \\ are escapes on both sides.</summary>
public static Dictionary<string, string> Parse(string text)
{
var d = new Dictionary<string, string>(4096);
foreach (var raw in text.Split('\n'))
{
var line = raw.TrimEnd('\r');
if (line.Length == 0 || line[0] == '#') continue;
int tab = line.IndexOf('\t');
if (tab <= 0) continue;
var key = Unescape(line.Substring(0, tab));
var val = Unescape(line.Substring(tab + 1));
d[key] = val;
}
return d;
}
static string Unescape(string s)
{
if (s.IndexOf('\\') < 0) return s;
var sb = new StringBuilder(s.Length);
for (int i = 0; i < s.Length; i++)
{
char c = s[i];
if (c == '\\' && i + 1 < s.Length)
{
char n = s[++i];
sb.Append(n == 'n' ? '\n' : n == 't' ? '\t' : n);
}
else sb.Append(c);
}
return sb.ToString();
}
// ───────────────────────── fonts ─────────────────────────
/// <summary>The heading and body fonts for the current language.</summary>
public static void Fonts(AssetBank bank, out Font display, out Font body)
{
var lilita = Named(bank, "LilitaOne");
var nunito = Named(bank, "Nunito-Black") ?? Named(bank, "Nunito");
display = lilita;
body = nunito;
if (Current.cjkFont != null)
{
var cjk = Named(bank, Current.cjkFont);
if (cjk) { display = cjk; body = cjk; }
else Debug.LogWarning($"[Loc] font {Current.cjkFont} missing: {Current.code} will show boxes");
}
else if (Current.latinDisplayOnly && nunito) display = nunito;
if (!display) display = body;
}
static Font Named(AssetBank bank, string name)
{
if (bank == null) return null;
foreach (var f in bank.fonts) if (f && f.name.Contains(name)) return f;
return null;
}
}
}