User:FallenAvatar/Code Based Mod

From wiki
Jump to: navigation, search

[[Category:Modding]] Staxel also supports adding in your own code which can attach itself to interfaces within Staxel's code. This attachment is fairly rudimentary, and isn't tested as much as the other features. However, if you've ever wanted to add new features to Staxel, this is the way to do it.


Creating The Project

This page assumes that you are running Visual Studio 2017. If you run other versions of Visual Studio, locations of things may have changed. Other IDE's will definitely have different methods for some actions.

You should not need any external dependencies to develop Mods for Staxel. However, complex changes to the game may need XNA. In that case you can find what you need here: MXA

To create a new Code Based Mod:

  • Open Visual Studio
  • Create a New Project (File > New > Project)
  • Select a Class Library (Installed > Visual C# > Windows Classic Desktop > Class Library (.NET Framework))
    • Note You should be able to use your .Net Language of choice, but all sample code is in C#.
    • You may name it how you like, and use the newest version of the framework.
      • Tested with up to 4.7.1
  • Pick where you want your mod saved
  • Click Ok

New Project Your New Project dialog may not look exactly like this.

Setting Up The Project

Now the project needs to be setup to recognize Staxel's code and directories.

  • Add a Reference

This sets it so that it builds in a manner that Staxel will be able to access. After this, let it create the solution.

After this file is set up, it's time to add references to Staxel's .dll files. Right-Click on the project (not the solution) in the Solution explorer then go to "Add" and then "Reference..."

When the window opens, click on Browse at the bottom of the window, then navigate to where Staxel stores its files. When there, head to the "bin" folder and select both "Staxel.dll" and "Plukit.Base.dll", as both of these are needed for the code below. Other .dll files may be needed at a later time.

You are now setup for creating a mod.


Helpful Tips

Note: If possible, use a refactor service to implement shell versions of a given interface. Visual Studio 2015 has this by default. Third-party add-ons, like JetBrains ReSharper, can also do this alongside other features that make development considerably easier. It can be obtained at https://www.jetbrains.com/resharper/.


Contents of a mod

For the most part, contents of code is left up to the user. However, you must implement certain interfaces in order for Staxel to run your program's code.

A good starting place would be to implement this interface made for modding:

using System;
using Plukit.Base;
using Staxel.Items;
using Staxel.Logic;
using Staxel.Tiles;

namespace ClassLibrary1
{
    public class Class1 : Staxel.Modding.IModHookV2 {
        public bool CanPlaceTile(Entity entity, Vector3I location, Tile tile, TileAccessFlags accessFlags) {
            throw new NotImplementedException();
        }

        public bool CanRemoveTile(Entity entity, Vector3I location, TileAccessFlags accessFlags) {
            throw new NotImplementedException();
        }

        public bool CanReplaceTile(Entity entity, Vector3I location, Tile tile, TileAccessFlags accessFlags) {
            throw new NotImplementedException();
        }

        public void Dispose() {
            throw new NotImplementedException();
        }

        public void GameContextDeinitialize() {
            throw new NotImplementedException();
        }

        public void GameContextInitializeAfter() {
            throw new NotImplementedException();
        }

        public void GameContextInitializeBefore() {
            throw new NotImplementedException();
        }

        public void GameContextInitializeInit() {
            throw new NotImplementedException();
        }

        public void GameContextReloadAfter() {
            throw new NotImplementedException();
        }

        public void GameContextReloadBefore() {
            throw new NotImplementedException();
        }

        public void UniverseUpdateAfter() {
            throw new NotImplementedException();
        }

        public void UniverseUpdateBefore(Universe universe, Timestep step) {
            throw new NotImplementedException();
        }

        public void ClientContextInitializeInit() {
            throw new NotImplementedException();
        }

	public void ClientContextInitializeBefore() {
            throw new NotImplementedException();
        }

	public void ClientContextInitializeAfter() {
            throw new NotImplementedException();
        }

	public void ClientContextDeinitialize() {
            throw new NotImplementedException();
        }

	public void ClientContextReloadBefore() {
            throw new NotImplementedException();
        }

	public void ClientContextReloadAfter() {
            throw new NotImplementedException();
        }

	public void CleanupOldSession() {
            throw new NotImplementedException();
        }
    }
}

You can look around for other types of interfaces within Staxel's code/dll and as an added bonus, Visual Studio will offer to help you implement these interfaces when you find them, with the appropriate methods and "usings".

A list of some of these Interfaces you can implement are:

IAchievementComponentBuilder, IBiomeBuilder, ICommandBuilder, IComponentBuilder, IEffectBuilder, IEntityAction, IEntityLogicBuilder, IEntityPainterBuilder, IFarmAnimalEntityBehaviourBuilder, IItemBuilder, IItemComponentBuilder, IModHook, INotificationBuilder, IPlayerExtendedCommand, IScript, ISubProcessHandler, ITileComponentBuilder, ITileConfigurationBuilder, ITileStateBuilder ITileStateBuilder, IVillagerEntityCommand, IVillagerEntityInstinct, IWeatherComponentBuilder


Past this point it's up to you to look around and find out what you can. One recommendation is to find out how to decompile Staxel.dll, as it is not obfuscated, and have a look at the actual code. Tools with which to do so include ILSpy, and JetBrains dotPeek.

Be aware: Exceptions will cause the game to crash. Try to avoid leaving NotImplementExceptions in unused methods.

Loading A Mod Into Staxel

After you have written your code, be sure to build your file. You can then navigate to your solution's bin folder. In that folder will be a bunch of files - select the ones you have made, then copy and paste them into the bin folder in Staxel's directory. (That is, copy and paste those files into "pathToStaxel/bin")

Once this copy is done, make a new text file and name it the same as your .dll file, except replace .dll with .mod. Inside of this .mod file, simply add {} and save. This may eventually include more information, but for now it is empty.

If all goes well, your mod will now be loaded into Staxel. Have fun coding!


Interface Documentation