terraria.js v1
// Terraria — native Linux (FNA/SDL2). handlers/tmodloader.js is the same
// handler with tModLoader's launch on top; keep the two in step.
// See NOTES.md "Terraria / tModLoader".
//
// Multiplayer:
// - "Host & Play" starts a SEPARATE server process (TerrariaServer) with
//   "-autoshutdown ... -steam -localsteamid <client> -lobby friends". The
//   server creates the Steam lobby; the host's client connects to it over
//   TCP 127.0.0.1:7777 and learns the lobby ID in the world data.
// - "Invite Friends" (Esc menu while hosting) calls
//   ActivateGameOverlayInviteDialog(lobby); with auto_send_invite.txt gbe_fork
//   sends a lobby invite to every other instance, whose GameLobbyJoinRequested
//   opens the character select and joins over Steam P2P to the server.
//   gbe_fork itself skips every instance here (its "already in my lobby"
//   check compares lobby IDs, and the host client and the other instances are
//   all in none), so the hook replaces that call (JANUS_GOLDBERG_INVITE_FIX).
// - The server inherits the host client's environment, hence its Goldberg
//   settings and Steam ID. The hook gives it its own GseAppPath (= own
//   identity) when its argv has -autoshutdown (JANUS_ARGV_ENV_MATCH).
// - LAN: Host & Play listens on all interfaces, so "Join via IP" 127.0.0.1
//   (port 7777) works from any instance without Steam.
Game.appid = 105600;
Game.name = "Terraria";
// Steam's Linux launch entry (appinfo.vdf config/launch, oslist=linux).
Game.launchCommand = "./Terraria";
Game.maxPlayers = 4;
Game.launchMessage =
  "To play together: one player chooses Multiplayer > Host & Play. Once in " +
  "the world, they open the menu (Esc) and choose \"Invite Friends\": the " +
  "others get the character select and join. Or the others choose " +
  "Multiplayer > Join via IP, address 127.0.0.1.";
Game.runtime = "none";            // Steam runs it bare (no runtime in appinfo)
Game.isolation = "none";          // the handler separates instances itself
Game.profileSaves = true;         // ...per player profile (ctx.saveRoot)
Game.borderless = false;
Game.goldberg = true;
Game.goldbergDisableLan = false;  // instances must see each other for invites
// FNA follows a resized window: when the tiles re-flow (a player joins or
// leaves), Janus resizes the game's window to its new tile (resize.py).
Game.resizeGame = "window";
Game.goldbergInvites = true;
// Pads: Steam Input. FNA's SDL3 reads each instance's Steam virtual pad (the
// hook allows SDL to open it and blocks the physical one), and the session
// forces this game's Steam Input layout (steam_input.force_layout), so Steam's
// per-game remapping applies. Terraria never asks for ISteamInput itself.
Game.steamInputFilter = false;
// FNA3D's default Vulkan path doesn't present under gamescope (GAMES.md).
Game.extraEnv = {FNA3D_FORCE_DRIVER: "OpenGL"};

const APPID = 105600;
const SLUG = "terraria";

Janus.addOption("saves", {
  label: "Saves",
  description: "firstReal: the Steam account's player plays on your real " +
               "characters and worlds (local and Steam Cloud); every other " +
               "profile has its own saves, empty at first. isolated: every " +
               "profile has its own saves, empty at first. copy: every profile " +
               "gets a copy of your real saves the first time it plays, then " +
               "keeps its own (progress stays in Janus; your real saves are " +
               "never written).",
  type: "choice", choices: ["firstReal", "isolated", "copy"], default: "firstReal"});

function realSaves(ctx) {
  return `${ctx.home}/.local/share/Terraria`;
}

// A gbe_fork app dir (GseAppPath): identity, LAN on, own saves folder.
// Local steam_settings beat the global "GSE Saves/settings" (merge_ini keeps
// the first value), and with saves_folder_name set the global dir isn't the
// one in ~/.local/share/GSE Saves anyway.
function writeGseSettings(dir, name, steamid, gseFolder) {
  const s = `${dir}/steam_settings`;
  Janus.writeFile(`${s}/steam_appid.txt`, `${APPID}\n`);
  Janus.writeFile(`${s}/configs.user.ini`,
    "[user::general]\n" +
    `account_name=${name}\n` +
    `account_steamid=${steamid}\n` +
    "language=english\n" +
    "[user::saves]\n" +
    `saves_folder_name=${gseFolder}\n`);
  Janus.writeFile(`${s}/configs.main.ini`,
    "[main::connectivity]\n" +
    "disable_lan_only=0\n");
}

// The Steam Cloud folder of the most recently used Steam account that has one
// for this app (Terraria keeps cloud characters/worlds there).
function steamCloudDir(ctx) {
  const dirs = Janus.glob(`${ctx.home}/.local/share/Steam/userdata/*/${APPID}/remote`);
  if (dirs.length <= 1) return dirs[0] || null;
  const r = Janus.run(["stat", "-c", "%Y %n"].concat(dirs));
  return r.stdout.trim().split("\n").map(l => [Number(l.split(" ")[0]), l.slice(l.indexOf(" ") + 1)])
    .sort((a, b) => b[0] - a[0])[0][1];
}

function isLink(p) {
  return Janus.run(["test", "-L", p]).code === 0;
}

// Returns {dir: local save dir to pass (null = the game's default, i.e. the
// real one), gseFolder: Goldberg saves_folder_name, relative to ~/.local/share}.
// Goldberg's emulated Steam Cloud is <gseFolder>/<appid>/remote. Saves belong
// to the player's profile (ctx.saveRoot), not to the instance number.
function setupSaves(ctx) {
  const data = `${ctx.home}/.local/share/janus/${SLUG}`;
  const mode = ctx.options.saves;
  const cloud = steamCloudDir(ctx);
  const rel = (p) => p.slice(`${ctx.home}/.local/share/`.length);

  // firstReal, the Steam account's player: the real saves.
  if (mode === "firstReal" && ctx.profile.owner) {
    const gse = `${data}/real/gse`;
    const remote = `${gse}/${APPID}/remote`;
    // The real cloud folder, live. Steam syncs the changes the next time the
    // game is started through Steam, as after playing offline.
    if (!Janus.exists(remote) && !isLink(remote)) {
      Janus.mkdir(`${gse}/${APPID}`);
      if (cloud) Janus.symlink(cloud, remote);
      else Janus.mkdir(remote);
    }
    return {dir: null, gseFolder: rel(gse)};
  }

  // Everyone else: own saves, kept across sessions. copy seeds them from the
  // real saves the first time; isolated (and firstReal's other profiles)
  // start empty. Kept apart, so switching modes never mixes them.
  const base = `${ctx.saveRoot}/${mode === "copy" ? "copy" : "isolated"}`;
  const dir = `${base}/saves`;
  const remote = `${base}/gse/${APPID}/remote`;
  if (!Janus.exists(dir)) {
    Janus.mkdir(dir);
    const real = realSaves(ctx);
    if (mode === "copy" && Janus.exists(real)) {
      for (const p of Janus.glob(`${real}/*`)) {
        const name = p.slice(p.lastIndexOf("/") + 1);
        if (name !== "tModLoader") Janus.copy(p, `${dir}/${name}`);  // tModLoader's saves
      }
      Janus.log(`${ctx.profile.name}: copied ${real} -> ${dir}`);
    }
  }
  if (!Janus.exists(remote)) {
    if (mode === "copy" && cloud) {
      Janus.copy(cloud, remote);
      Janus.log(`${ctx.profile.name}: copied Steam Cloud ${cloud} -> ${remote}`);
    } else Janus.mkdir(remote);
  }
  return {dir, gseFolder: rel(`${base}/gse`)};
}

// LastLaunchedVersion written by the game in use (Terraria 1.4.5.8). Only a
// fallback: see lastLaunchedVersion().
const KNOWN_RELEASE = 326;

// The game runs per-version settings upgrades for every release newer than
// config.json's LastLaunchedVersion (Main.LoadSettings), and with the key
// missing (0) one of them resets the volumes to 1.0, so a fresh instance's
// music setting never stuck. A config written by the game has the current
// value: take it from the real config or any profile's, else KNOWN_RELEASE.
function lastLaunchedVersion(ctx) {
  const cfgs = [`${realSaves(ctx)}/config.json`].concat(
    Janus.glob(`${ctx.profilesDir}/*/${APPID}/*/saves/config.json`));
  let v = 0;
  for (const p of cfgs) {
    try { v = Math.max(v, Number(Janus.readJson(p).LastLaunchedVersion) || 0); }
    catch (e) { /* missing or unreadable */ }
  }
  return v || KNOWN_RELEASE;
}

// config.json, every launch: windowed at the tile size, and music off in
// every instance but the first (one soundtrack, not N overlapping copies).
// The real config.json (firstReal, the Steam account's player) is snapshot first and
// restored when the session ends.
function writeConfig(dir, ctx, isReal) {
  const set = {};
  if (ctx.width && ctx.height)
    Object.assign(set, {DisplayWidth: ctx.width, DisplayHeight: ctx.height,
                        Fullscreen: false, WindowMaximized: false, WindowBorderless: true});
  const cfg = `${dir}/config.json`;
  // A profile's saves follow it to any player number: the volume muted as
  // P2+ is put back when it plays as P1 (the real config is restored from
  // its backup anyway).
  const muted = `${dir}/janus-music-volume.txt`;
  if (ctx.instance === 1 && Janus.exists(muted)) {
    const v = Number(Janus.readFile(muted));
    set.VolumeMusic = isNaN(v) ? 1 : v;
    Janus.remove(muted, {force: true});
  }
  if (!Object.keys(set).length && ctx.instance === 1) return;
  if (isReal) Janus.backup(cfg);
  let c = {};
  if (Janus.exists(cfg)) {
    try { c = Janus.readJson(cfg); }
    catch (e) { Janus.log(`config.json unreadable, left alone: ${e.message}`); return; }
  }
  if (ctx.instance > 1) {
    if (!isReal && c.VolumeMusic !== 0 && !Janus.exists(muted))
      Janus.writeFile(muted, String(c.VolumeMusic == null ? 1 : c.VolumeMusic));
    set.VolumeMusic = 0;
  }
  if (!c.LastLaunchedVersion) set.LastLaunchedVersion = lastLaunchedVersion(ctx);
  Janus.writeJson(cfg, Object.assign(c, set));
}

// The profile menus' line for each profile: its characters and world count,
// from the saves setupSaves() gives it (read-only: only globs).
Game.profileInfo = function (ctx) {
  const mode = ctx.options.saves;
  let dirs;
  if (mode === "firstReal" && ctx.profile.owner) {
    const cloud = Janus.glob(`${ctx.home}/.local/share/Steam/userdata/*/${APPID}/remote`);
    dirs = [realSaves(ctx)].concat(cloud);
  } else {
    const base = `${ctx.saveRoot}/${mode === "copy" ? "copy" : "isolated"}`;
    dirs = [`${base}/saves`, `${base}/gse/${APPID}/remote`];
  }
  const names = (kind, ext) => {
    const out = new Set();
    for (const d of dirs)
      for (const sub of [kind, kind.toLowerCase()])
        for (const p of Janus.glob(`${d}/${sub}/*.${ext}`))
          out.add(p.slice(p.lastIndexOf("/") + 1, -ext.length - 1).replace(/_/g, " "));
    return [...out].sort();
  };
  const chars = names("Players", "plr"), worlds = names("Worlds", "wld");
  if (!chars.length && !worlds.length) return "No characters yet";
  const who = chars.length > 3 ? `${chars.slice(0, 3).join(", ")} +${chars.length - 3}`
                               : chars.join(", ") || "no characters";
  return `${who} · ${worlds.length} world${worlds.length === 1 ? "" : "s"}`;
};

Game.play = function (ctx) {
  const n = ctx.instance;
  const saves = setupSaves(ctx);
  const name = ctx.profile.name;
  // The server gets its own Steam ID, derived from the player's. SteamID64s
  // exceed 2^53, hence BigInt.
  const serverId = (BigInt(ctx.profile.steamId) + 100n).toString();

  // Client and Host & Play server identities. gbe_fork reads steam_settings/
  // under GseAppPath; the vendored lib is loaded through the hook's dlopen
  // redirect, so "next to the lib" would be shared by every instance.
  const gse = `${ctx.gameDir}/_janus_gbe_logs`;   // = Janus's debug-build GseAppPath
  const srv = `${ctx.gameDir}/_janus_gbe_server`;
  writeGseSettings(gse, name, ctx.profile.steamId, saves.gseFolder);
  writeGseSettings(srv, `${name} (host)`, serverId, saves.gseFolder);
  for (const f of ["auto_send_invite.txt", "auto_accept_invite.txt"])
    Janus.writeFile(`${gse}/steam_settings/${f}`, "");
  ctx.env.GseAppPath = gse;
  ctx.env.JANUS_ARGV_ENV_MATCH = "-autoshutdown";
  ctx.env.JANUS_ARGV_ENV = "GseAppPath=" + srv;
  // gbe_fork only invites friends whose lobby differs from the inviter's;
  // this client is in NO lobby (the server owns it), and neither are the
  // other instances, so gbe skips them all. The hook sends the invites itself.
  ctx.env.JANUS_GOLDBERG_INVITE_FIX = "1";

  if (saves.dir) ctx.gameArgs.push("-savedirectory", saves.dir);
  writeConfig(saves.dir || realSaves(ctx), ctx, saves.dir == null);
  Janus.log(`instance ${n} (${name}): host with Multiplayer > Host & Play, then Esc > ` +
            `"Invite Friends"; or join with Multiplayer > Join via IP 127.0.0.1`);
};

Comments 0

No comments yet. Tried this handler? Say how it went.

Sign in to comment.