Difference between revisions of "Code Based Mod"

From wiki
Jump to: navigation, search
(Update the code sample to use the IModHookV2)
(Minor grammar/spelling fixes. Added Staxel.Items using directive to sample code, as it is necessary for TileAccessFlags. Added "Interface Documentation" section in anticipation of documenting ICommandBuilder.)
Line 1: Line 1:
 
[[Category:Modding]]
 
[[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 ever wanted to add new features to Staxel, this is the way to do it.
+
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.
  
  
Line 7: Line 7:
 
'''Before starting''' be sure to install XNA for your IDE. For Visual Studio 2015, you can head to https://mxa.codeplex.com/releases to download a compatible version.
 
'''Before starting''' be sure to install XNA for your IDE. For Visual Studio 2015, you can head to https://mxa.codeplex.com/releases to download a compatible version.
  
 +
The rest of this page assumes you're running Visual Studio 2015. 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.
  
The rest of this page, assumes your running Visual Studio 2015, 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.
+
To set up Visual Studio, first create a new solution. Name this solution whatever you want, and select "Class Library". If you are having trouble finding it, it is located here:
 
+
 
+
To set up Visual Studio, first create a new solution. Name this solution whatever you want, and select "Class Library". If you are having trouble finding it, it is located here;
+
 
[[File:Class Library Location.png]]
 
[[File:Class Library Location.png]]
  
 
This sets it so that it builds in a manner that Staxel will be able to access. After this, let it create the solution.
 
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 Staxels .dll's. Right-Click on the project (Not the solution) in the Solution explorer then go to "Add" and then "Reference..."  
+
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's may be needed at a later time.
+
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.
 
You are now setup for creating a mod.
Line 25: Line 23:
 
= Helpful Tips =
 
= 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 addons 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/.
+
'''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 =
 
= Contents of a mod =
  
For the most part, contents of code is left up to the user. Yet there is a need to implement interfaces for Staxel to run your program's code.
+
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:
  
A good starting place would be to implement this interface made for modding;
 
 
<pre>
 
<pre>
 
using System;
 
using System;
 
using Plukit.Base;
 
using Plukit.Base;
 +
using Staxel.Items;
 
using Staxel.Logic;
 
using Staxel.Logic;
 
using Staxel.Tiles;
 
using Staxel.Tiles;
Line 41: Line 41:
 
namespace ClassLibrary1
 
namespace ClassLibrary1
 
{
 
{
     public class Class1 : Staxel.Modding.IModHookV2{
+
     public class Class1 : Staxel.Modding.IModHookV2 {
 
         public bool CanPlaceTile(Entity entity, Vector3I location, Tile tile, TileAccessFlags accessFlags) {
 
         public bool CanPlaceTile(Entity entity, Vector3I location, Tile tile, TileAccessFlags accessFlags) {
 
             throw new NotImplementedException();
 
             throw new NotImplementedException();
Line 130: Line 130:
  
  
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.
+
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.'''
+
'''Be aware: Exceptions will cause the game to crash. Try to avoid leaving NotImplementExceptions in unused methods.'''
  
 
= Loading A Mod Into Staxel =
 
= Loading A Mod Into Staxel =
  
After you have written your code, be sure to build your file. You can then navigate to your solutions bin folder. In that folder, will be a bunch of files, select the ones you have made and then copy and paste them into the bin folder in Staxel's directory. (That is, copy and paste those files into "pathToStaxel/bin")
+
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 you .dll file except replace .dll with .mod. Then inside of this .mod file, simply add {} and save. Eventually this may include more information but for now it is empty.
+
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!
 
If all goes well, your mod will now be loaded into Staxel. Have fun coding!
 +
 +
 +
= Interface Documentation =
 +
 +
* [[IAchievementComponentBuilder]]
 +
* [[IBiomeBuilder]]
 +
* [[ICommandBuilder]]
 +
* [[IComponentBuilder]]
 +
* [[IEffectBuilder]]
 +
* [[IEntityAction]]
 +
* [[IEntityLogicBuilder]]
 +
* [[IEntityPainterBuilder]]
 +
* [[IFarmAnimalEntityBehaviourBuilder]]
 +
* [[IItemBuilder]]
 +
* [[IItemComponentBuilder]]
 +
* [[IModHookV2]]
 +
* [[INotificationBuilder]]
 +
* [[IPlayerExtendedCommand]]
 +
* [[IScript]]
 +
* [[ISubProcessHandler]]
 +
* [[ITileComponentBuilder]]
 +
* [[ITileConfigurationBuilder]]
 +
* [[ITileStateBuilder]]
 +
* [[ITileStateBuilder]]
 +
* [[IVillagerEntityCommand]]
 +
* [[IVillagerEntityInstinct]]
 +
* [[IWeatherComponentBuilder]]

Revision as of 11:53, 2 February 2018

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.


Setting Up For Staxel

Before starting be sure to install XNA for your IDE. For Visual Studio 2015, you can head to https://mxa.codeplex.com/releases to download a compatible version.

The rest of this page assumes you're running Visual Studio 2015. 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.

To set up Visual Studio, first create a new solution. Name this solution whatever you want, and select "Class Library". If you are having trouble finding it, it is located here: Class Library Location.png

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