Skip to content

Commit

Permalink
Multiplayer support with MPAPI 0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
notfood committed Jun 23, 2019
1 parent 552521f commit 2e5aa8c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
Binary file added Assemblies/0MultiplayerAPI.dll
Binary file not shown.
Binary file modified Assemblies/ResearchTree.dll
Binary file not shown.
57 changes: 57 additions & 0 deletions Source/Multiplayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Collections.Generic;
using System.Linq;
using Multiplayer.API;
using Verse;

namespace ResearchPal
{
// Can be run anywhere really. Multiplayer runs its API init code on Mod()
// and since it runs always after Core, it's certain it will be ready.
[StaticConstructorOnStartup]
public static class Multiplayer
{
static Dictionary<ResearchProjectDef, ResearchNode> _cache;

static Multiplayer()
{
if (!MP.enabled) return;

// Let's sync all Queue operations that involve the GUI
// Don't worry about EnqueueRange calling Enqueue, MP mod handles it
MP.RegisterSyncMethod(typeof(Queue), nameof(Queue.Enqueue));
MP.RegisterSyncMethod(typeof(Queue), nameof(Queue.EnqueueRange));
MP.RegisterSyncMethod(typeof(Queue), nameof(Queue.Dequeue));

MP.RegisterSyncWorker<ResearchNode>(HandleResearchNode);

Log.Message("Initialized compatibility for Multiplayer");
}

static ResearchNode Lookup(ResearchProjectDef projectDef)
{
if (_cache == null)
_cache = Tree.Nodes.OfType<ResearchNode>().ToDictionary(r => r.Research);

return _cache[projectDef];
}

// We only care about the research, no new nodes will be created or moved.
static void HandleResearchNode(SyncWorker sw, ref ResearchNode node)
{
// Bind commands are in the order they are placed
// So if you write a Def first, you must read it first and so on
if (sw.isWriting) {
// We are writing, node is the outgoing object
sw.Bind(ref node.Research);
} else {
ResearchProjectDef research = null;

sw.Bind(ref research);

// We are reading, node is null, we must set it. So we look it up
// research is unique in the Tree
node = Lookup(research);
}
}
}
}
4 changes: 4 additions & 0 deletions Source/ResearchTree.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@
<HintPath>..\Dlls\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="0MultiplayerAPI">
<HintPath>..\Dlls\0MultiplayerAPI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Assets.cs" />
Expand All @@ -76,6 +79,7 @@
<Compile Include="ResearchTree.cs" />
<Compile Include="Settings.cs" />
<Compile Include="ResourceBank.cs" />
<Compile Include="Multiplayer.cs" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
Expand Down

0 comments on commit 2e5aa8c

Please sign in to comment.