-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters