ModScriptBasics: Difference between revisions
Jump to navigation
Jump to search
Codebyfire (talk | contribs) No edit summary |
Stlnegril9 (talk | contribs) No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 3: | Line 3: | ||
=Basics= | =Basics= | ||
For now follow the Rimworld setup but instead | For now follow the Rimworld setup but instead (at least):<br> | ||
# Use .NET Framework 4.8 (vs. .NET Framework 4.7.2) | |||
# In ''C:\Program Files (x86)\Steam\steamapps\common\The Colonists\TheColonists_Data\Managed\'' (or alternate OS equivalent): | |||
## Use '''Game.dll''' vs. '''Rimworld dll''' | |||
## In addition to referencing '''Assembly-CSharp.dll''' and '''UnityEngine.CoreModule.dll''', also reference '''0Harmony.dll''' and '''game.dll'''. | |||
[https://rimworldwiki.com/wiki/Modding_Tutorials/Setting_up_a_solution](https://rimworldwiki.com/wiki/Modding_Tutorials/Setting_up_a_solution) | [https://rimworldwiki.com/wiki/Modding_Tutorials/Setting_up_a_solution](https://rimworldwiki.com/wiki/Modding_Tutorials/Setting_up_a_solution) | ||
==Framework Sample | =Testing= | ||
When running the game with a scripting mod active, the custom_log.txt file should show entries similar to:<br> | |||
#MOD DLL# loading... | |||
DLL LOADED - methods:1 | |||
=Framework Sample= | |||
This is a basic class which sets up Harmony, registers a custom tech and contains a Harmony prefix for when a player completes a research. | This is a basic class which sets up Harmony, registers a custom tech and contains a Harmony prefix for when a player completes a research. | ||
| Line 27: | Line 35: | ||
[StaticConstructorOnStartup] | [StaticConstructorOnStartup] | ||
public static int Init() { | public static int Init() { | ||
Harmony.DEBUG = true; | Harmony.DEBUG = true; | ||
| Line 46: | Line 56: | ||
[HarmonyPatch(typeof(Player), "OnTechResearched")] | [HarmonyPatch(typeof(Player), "OnTechResearched")] | ||
class Patch { | class Patch { | ||
static void Prefix(TechID techId) { | static void Prefix(TechID techId) { | ||
Debug.Log("#TestMod2# OnTechResearched:" + techId); | Debug.Log("#TestMod2# OnTechResearched:" + techId); | ||
if((int) techId == TestMod2.CUSTOM_TECH_1_ID) { | |||
FileLog.Log("custom tech researched - myVar: " + techId.ToInfo<CustomTechInfo>().myVar); | |||
} | |||
} | } | ||
} | |||
public class CustomTechInfo : TechInfo { | |||
public int myVar; | |||
public override int Serialise(byte[] buffer, int offset) { | |||
offset = DataEncoder.Encode(myVar, buffer, offset); | |||
return base.Serialise(buffer, offset); | |||
} | |||
public override int Deserialise(byte[] buffer, int offset) { | |||
offset = DataEncoder.Decode(out myVar, buffer, offset); | |||
return base.Deserialise(buffer, offset); | |||
} | |||
} | } | ||
} | } | ||
Latest revision as of 15:36, 31 January 2023
Basics-Scripting (C#)
This page is in work! - needs code block formatting in particular.
Basics
For now follow the Rimworld setup but instead (at least):
- Use .NET Framework 4.8 (vs. .NET Framework 4.7.2)
- In C:\Program Files (x86)\Steam\steamapps\common\The Colonists\TheColonists_Data\Managed\ (or alternate OS equivalent):
- Use Game.dll vs. Rimworld dll
- In addition to referencing Assembly-CSharp.dll and UnityEngine.CoreModule.dll, also reference 0Harmony.dll and game.dll.
[1](https://rimworldwiki.com/wiki/Modding_Tutorials/Setting_up_a_solution)
Testing
When running the game with a scripting mod active, the custom_log.txt file should show entries similar to:
#MOD DLL# loading... DLL LOADED - methods:1
Framework Sample
This is a basic class which sets up Harmony, registers a custom tech and contains a Harmony prefix for when a player completes a research.
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
namespace TestMod2 {
public class TestMod2 {
static public int CUSTOM_TECH_1_ID = 200;
[StaticConstructorOnStartup]
public static int Init() {
Harmony.DEBUG = true;
var harmony = new Harmony("testmod2");
FileLog.Log("testing harmony logging");
harmony.PatchAll();
SerialiseFactory.AddCustom(typeof(TechInfo), (byte) CUSTOM_TECH_1_ID, typeof(CustomTechInfo));
Debug.Log("#TestMod2# Init Complete!");
return 2;
}
}
[HarmonyPatch(typeof(Player), "OnTechResearched")]
class Patch {
static void Prefix(TechID techId) {
Debug.Log("#TestMod2# OnTechResearched:" + techId);
if((int) techId == TestMod2.CUSTOM_TECH_1_ID) {
FileLog.Log("custom tech researched - myVar: " + techId.ToInfo<CustomTechInfo>().myVar);
}
}
}
public class CustomTechInfo : TechInfo {
public int myVar;
public override int Serialise(byte[] buffer, int offset) {
offset = DataEncoder.Encode(myVar, buffer, offset);
return base.Serialise(buffer, offset);
}
public override int Deserialise(byte[] buffer, int offset) {
offset = DataEncoder.Decode(out myVar, buffer, offset);
return base.Deserialise(buffer, offset);
}
}
}