Skip to content

[upcoming] 5.1 Using OTAPI Server

Luke edited this page Jul 25, 2021 · 2 revisions
Contents
1. Simple starter template
2. Try a hook

OTAPI itself is mostly* a normal class library - you can selectively call methods in a custom host exe, or you can call the entry point and run the modded server itself.

Using OTAPI itself means you also need to know how to navigate the vanilla game code, mostly using ILSpy/dnSpy and Visual Studios decompile tools.

Simple starter template

  1. Create and navigate to a new folder for your test/example project to live

mkdir example && cd example

  1. Seed a new console project

dotnet new console

  1. Add OTAPI's NuGet package (vanilla)

dotnet add package OTAPI.Upcoming --prerelease

  1. Call entry point (Terraria.WindowLaunch.Main - can also be done dynamically, OTAPI.dll was an exe in a previous life so it contains the metadata for it)
using System;

namespace example
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Terraria.WindowsLaunch.Main(args);
        }
    }
}

Try a hook

using System;

namespace example
{
    class Program
    {
        static void Main(string[] args)
        {
            On.Terraria.Main.DedServ += Main_DedServ;
            Terraria.WindowsLaunch.Main(args);
        }

        private static void Main_DedServ(On.Terraria.Main.orig_DedServ orig, Terraria.Main self)
        {
            Console.WriteLine("Hello Server!");
            orig(self);
        }
    }
}