# Conflicts: # client/RailEnterprise.Client/assets/i18n/strings.csv # client/RailEnterprise.Client/scripts/Main.cs # client/RailEnterprise.Client/scripts/Screens/MenuScreen.cs # docs/ARCHITECTURE.md # docs/PROTOCOL.md # docs/STATE.md # src/RailEnterprise.Protocol/Client/ClientWorld.cs
149 lines
6.0 KiB
C#
149 lines
6.0 KiB
C#
using Godot;
|
|
using RailEnterprise.Client.App;
|
|
using RailEnterprise.Client.Audio;
|
|
using RailEnterprise.Client.Net;
|
|
using RailEnterprise.Client.Screens;
|
|
using RailEnterprise.Core.Data;
|
|
using static RailEnterprise.Client.App.Localization;
|
|
|
|
namespace RailEnterprise.Client;
|
|
|
|
/// <summary>
|
|
/// Nodo radice: tema e traduzioni, dati del gioco, passaggio tra schermata di connessione e partita;
|
|
/// alla chiusura termina la sessione e l'eventuale server locale.
|
|
/// </summary>
|
|
public partial class Main : Node
|
|
{
|
|
private const string SettingsPath = "user://settings.cfg";
|
|
private GameData? _data;
|
|
private Node? _screen;
|
|
private GameSession? _session;
|
|
private string? _autopilot;
|
|
|
|
public override void _Ready()
|
|
{
|
|
var cfg = new ConfigFile();
|
|
cfg.Load(SettingsPath);
|
|
string locale = (string)cfg.GetValue("ui", "locale", "it");
|
|
Localization.Load(locale);
|
|
GetWindow().Theme = UiTheme.Build();
|
|
GetWindow().Title = "Rail Enterprise";
|
|
var args = OS.GetCmdlineUserArgs();
|
|
int i = Array.IndexOf(args, "--autopilot");
|
|
// audio: niente nel pilota automatico e senza schermo (non rallenta il collaudo)
|
|
var audio = new AudioManager(enabled: i < 0 && DisplayServer.GetName() != "headless");
|
|
AddChild(audio);
|
|
try
|
|
{
|
|
_data = GodotDataSource.LoadGameData();
|
|
audio.SetEras(_data.Eras.Eras);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
GD.PrintErr($"Dati del gioco non validi: {e.Message}");
|
|
}
|
|
if (i >= 0 && _data is not null)
|
|
_autopilot = i + 1 < args.Length ? args[i + 1] : ProjectSettings.GlobalizePath("user://autopilot");
|
|
// nel collaudo la schermata di connessione non avvia un suo server: lo avvia StartLocal qui sotto
|
|
ShowMenu(_data is null ? T("Dati del gioco non trovati o non validi.") : "", autoStart: _autopilot is null);
|
|
if (_autopilot is not null)
|
|
{
|
|
// collaudo: mappa piccola e seme fisso per il server locale
|
|
System.Environment.SetEnvironmentVariable("RE_MAP_SIZE", "64");
|
|
System.Environment.SetEnvironmentVariable("RE_SEED", "3");
|
|
System.Environment.SetEnvironmentVariable("RE_AUTOSAVE_MIN", "0");
|
|
StartLocal();
|
|
}
|
|
}
|
|
|
|
private void Swap(Node screen)
|
|
{
|
|
_screen?.QueueFree();
|
|
_screen = screen;
|
|
AddChild(screen);
|
|
}
|
|
|
|
/// <summary>Schermata iniziale e di connessione (immagine 09): server locale o in rete, stato, salvataggi.</summary>
|
|
/// <param name="autoStart">Avvia subito la strada scelta l'ultima volta (server locale o in rete).</param>
|
|
/// <param name="handover">Sessione di prova della schermata precedente, ripresa così com'è (cambio di lingua).</param>
|
|
private void ShowMenu(string status = "", bool autoStart = true, GameSession? handover = null)
|
|
{
|
|
if (_data is null)
|
|
{
|
|
// senza dati non si può aprire nessuna sessione: resta solo il messaggio
|
|
var label = UiTheme.Text(status, 16, Palette.Danger);
|
|
label.SetAnchorsAndOffsetsPreset(Control.LayoutPreset.Center);
|
|
Swap(label);
|
|
return;
|
|
}
|
|
var screen = new ConnectScreen(_data, autoStart: autoStart, handover: handover);
|
|
screen.PlayRequested += ShowGame;
|
|
screen.AudioRequested += () => AudioManager.Instance?.ShowSettings();
|
|
screen.LanguageChanged += code =>
|
|
{
|
|
var cfg = new ConfigFile();
|
|
cfg.Load(SettingsPath);
|
|
cfg.SetValue("ui", "locale", code);
|
|
cfg.Save(SettingsPath);
|
|
TranslationServer.SetLocale(code);
|
|
// la schermata si ricrea con i testi nuovi: la sessione di prova (e il server locale) passa a lei
|
|
ShowMenu(handover: screen.TakeSession());
|
|
};
|
|
Swap(screen);
|
|
if (status.Length > 0) screen.SetStatus(status, error: true);
|
|
}
|
|
|
|
/// <summary>Collaudo: server locale da zero e subito in partita, senza la schermata di connessione.</summary>
|
|
private async void StartLocal()
|
|
{
|
|
if (_data is null) return;
|
|
string? server = LocalServerLauncher.FindServer();
|
|
if (server is null)
|
|
{
|
|
ShowMenu(T("Server locale non trovato: compila src/RailEnterprise.Server o imposta RE_SERVER_PATH."), autoStart: false);
|
|
return;
|
|
}
|
|
var launcher = new LocalServerLauncher();
|
|
try
|
|
{
|
|
string dataDir = ProjectSettings.GlobalizePath("user://autopilot-server");
|
|
if (Directory.Exists(dataDir)) Directory.Delete(dataDir, true);
|
|
int port = await launcher.StartAsync(server, dataDir, TimeSpan.FromSeconds(60));
|
|
var session = await GameSession.ConnectAsync(_data, new Uri($"ws://127.0.0.1:{port}/ws"), launcher.Token, T("Partita locale"));
|
|
session.LocalServer = launcher;
|
|
ShowGame(session);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
launcher.Dispose();
|
|
ShowMenu($"{T("Partita locale non avviata")}: {e.Message}", autoStart: false);
|
|
}
|
|
}
|
|
|
|
private void ShowGame(GameSession session)
|
|
{
|
|
_session = session;
|
|
var game = new GameScreen(session);
|
|
game.Exit += async () =>
|
|
{
|
|
// prima si chiude la partita (e il server locale), poi la schermata di connessione ne
|
|
// avvia uno nuovo: due server sulla stessa cartella dei salvataggi non convivono
|
|
var s = _session;
|
|
_session = null;
|
|
if (s is not null) await s.DisposeAsync();
|
|
ShowMenu();
|
|
};
|
|
Swap(game);
|
|
if (_autopilot is not null) game.AddChild(new AutoPilot(game, _autopilot));
|
|
}
|
|
|
|
public override void _Notification(int what)
|
|
{
|
|
if (what != NotificationWMCloseRequest) return;
|
|
// anche la sessione di prova della schermata di connessione chiude il suo server locale
|
|
var session = _session ?? (_screen as ConnectScreen)?.TakeSession();
|
|
_session = null;
|
|
session?.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(3));
|
|
}
|
|
}
|