tModLoader NetEasy - For mod devs who despise networking with a passion

What do you think?

  • Cool, this should be useful

  • Eh, there's others like it

  • Ugh, another library

  • I'm not a modder, but I'm ok with downloading this as a dependency

  • I'm not a modder, but I'm not ok with downloading this as a dependency


Results are only viewable after voting.

Dual Iron

Plantera
### Summary

NetEasy lets you combine the neatness of object-oriented programming, the safety of data contracts, and the toolset of networking. In other words, it lets you make multiplayer-compatible mods without ripping your hair out and aging 20 years.

Of course, this mod is meant to be a dependency, not a standalone mod; as such, you'll need a strong reference to utilize it. Refer to this link for a guide on referencing other mods.

The Module class is where most of the magic happens. It represents a packet of information that tells NetEasy what data to send and what to do when receiving the data. The rest is handled automatically. Try it out!

It can be found on the mod browser, the tModLoader discord, and is open-source on GitHub.

### Instructions

Here are some basic instructions to get started.

First, you'll want to register your mod. This lets NetEasy crawl your assembly and ensure all your modules are valid. It also eagerly loads said modules. It's very simple, so don't fret.
  1. Put NetEasy.NetEasy.Register(this) into your Mod.PostSetupContent() method.
  2. Put NetEasy.NetEasy.HandleModule(reader, whoAmI) into your Mod.HandlePacket(BinaryReader reader, int whoAmI) method.
  3. Here's an example.
Then, you'll need to create a module by deriving from the Module class. Here's a guide:
  1. Derive from NetEasy.Module in a new class.
  2. Put [Serializable] above the class.
  3. Populate the class with fields. This tells NetEasy what to send as a net message. (You can ignore sending a field by adding the NonSerializedAttribute to it.)
  4. Override the Receive() method. This tells NetEasy what to do when receiving the net message.
  5. Here's an example.
Lastly, to send the module as a ModPacket, create an instance of the class, set the fields to whatever you want, and call Send(). This is the easiest part! An example, if you need it.

This should give you an idea of where to start. However, there are a few more rules under the hood. If you want to see those rules in-depth, you can see them here.
All modules:
  1. Must be serializable (AKA, they must have [Serializable] above their type). This lets NetEasy serialize the type and lets Visual Studio help you.
  2. Must implement the abstract Receive() method. This defines the module's behavior when received across the net.
  3. Must ignore members that aren't serializable.
  4. Must ignore members whose types aren't sealed. (This is because their types must be known at load-time.)
  5. Should not have complex behavior. They merely represent a data transaction between clients.
  6. Should ignore any members that can be inferred in the Receive() method.
  7. May send serializable, non-read-only fields and auto-properties across the net. All other members will be ignored.
In case you don't know, "serialize" means "turn into basic bytes". Not all data can be serialized, but only serialized data can be sent across the net. That's why there are limitations on what you can send across the net. Hope this helped.

Like magic! You'll get the hang of it once you try it a few times, like your first ModItem.

### Conclusion

NetEasy aims to reduce the cluttered, confusing, and downright atrocious imperative-style networking code that comes from handling bits and bytes directly. That'll rot your brain, trust me. Hope it helps! :)

Also, this:
There are a few extra services provided that aren't covered here but can be found by IntelliSense. Luckily, the extensive XML documentation by yours truly explains what everything does—down to each parameter in each method. If you're confused as to what something does, tell me so I can improve its doc!
  • AggregateModules - Lets you send several modules as one net message. Useful for avoiding net spams.

Thanks @Moonburn IT for the icon btw
 
Last edited:
Damn, this mod will certainly save my life, even more since I have real troubles doing netplay sync.
I wonder if now I can take my companions to the moon :).
 
Back
Top Bottom