Исходный код римворлд

Обновлено: 02.07.2024

This page is about making mods. For info about using mods, see Installing mods.

Contents

  • This tutorial requires you to have set up a solution.

Once you've set up your solution correctly you'll usually be greeted with a class similar like the template below:

Template

Let's go over the template:

Using

The first thing you notice is using System; which indicates this class can call anything from the namespace System without calling it like System.Class.SomeDataType but rather SomeDataType. This is very useful because we don't have to worry about such calls anymore (for now).

The next section shows which namespaces you could put in using.

Namespace

Next up is the namespace MyNameSpace part - in principle everything inside of a namespace knows everything else inside of that namespace. If you were to make a folder inside of your project (MyProjectName not Solution MySolutionName), any classes inside of that folder would have the namespace MyNameSpace.MyFolderName. That's a different namespace and unless you tell other classes that it exists (using MyNameSpace.MyFolderName;) they won't accept calls to parts of that namespace without that namespace as a prefix.

Anything inside of MyNameSpace.MyFolderName does however know of everything inside MyNameSpace, just nothing about other subfolders of MyNameSpace.

By default your project knows nothing. Adding DLL references to the References folder (as done in the previous tutorial) allows you to reference them with using:

You can set your project's namespace by going to visual studio and right-clicking MyProjectName -> Properties -> Application -> Root namespace(Visual Studio 2019 it is Default namespace). Please take some time to make it unique so no compatibility issues will arise with other mods, for your and everyone else's sanity.

Summary

This summary is part of the template but it's not required. It can help other people understand your code better when you provide the source but besides that you really don't need it.

Class and Constructor

This is your class definition. To access anything inside of a public class you will need a reference to an instance of that class:

The brackets at new MyClassName() indicate you're creating this instance without parameters which is possible because your class contains a constructor (anything inside of class MyClassName which has parameters and isn't a method, often abbreviated .ctor) which accepts a call without parameters:

The last implementation of the constructor (MyNameSpace.MyClassName.MyClassName inside of MyNameSpace.MyClassName) is capable of accepting zero to two parameters while the second implementation of the constructor will only accept calls with exactly two parameters.

Some very useful namespaces in general are the following:

And the namespaces for RimWorld in particular are these:

Namespace What does it do?
RimWorld Many RimWorld specific classes can be found in this namespace. This and the Verse namespace will be most used.
Verse Many general use classes can be found in this namespace. This and the RimWorld namespace will be most used.
RimWorld.BaseGen Everything that creates the layout and contents of the Outposts, Bases and Settlements is in here.
RimWorld.Planet Everything (almost everything) having to do with the planet is in here.
Verse.AI Holds the Jobs and general AI. Broadly speaking, what pawns do.
Verse.AI.Group Holds the squad AI. This is useful when you want 2 or more pawns to work together.
Verse.Grammar Contains functionality for the Languages folder and other things having to do with text operations such as the art flavour text generator.
Verse.Noise Something something you'll never need this.
Verse.Sound Much like Verse.Noise.
Verse.Steam Steam integration by Tynan - you won't need it.
UnityEngine Contains many more methods you most likely won't actively use. RimWorld mostly uses it for the GUI, Rect and Color methods.

The difference between RimWorld and Verse is sometimes blurred. "RimWorld" contains things which are reasonably specific to RimWorld functionality. Classes in Verse are more oriented towards general game functionality. Since it can be difficult to guess which namespace holds a class, you might as well add both using statements whenever you want.
If you want to use the exact functionality of a base game class you're best off copying all its using statements, its namespace and the namespace of its parent.

The base game provides a bunch of code snippets in ../Source/, relative to your Rimworld installation. Since this isn't a lot, one might want to take a look at the game's full source code. RimWorld's EULA allows you to decompile the game for personal use. It's recommended to read it.

The following programs are used and recommended by modders:

What you can mod

RimWorld does not limit what you can or can't do. The only limit is skills and motivation, or patience and willpower. You can mod everything, from defs, to code, images, and sounds. Some examples:

Advice

  • The tilde key (` or

Mod file structure

For defining new game content, XML files are used. Here is an example of the format, that applies to most of the definitions:

All rootnodes for Defs start with <Defs>. Remember, that the word Some must be replaced by the name of whatever are you defining. For thing it's <ThingDef>.

The RimWorld mods use the following directory structure. Root folders have to follow the naming pattern (About, Assemblies, Defs, Sounds, Patches, Textures, Languages) for them to be read by RimWorld. Apart from the Languages Folder, subfolders can have any structure. For the sake of organisation, it is recommended to keep things categorised:

Mod info

This is found in MODNAME/About

  • The contents of About.xml are plain text. HTML Markup tags cause NullRef's.
  • You can have an image for your mod. Restrict the image size to 1 MB.

Textures

These are found in MODNAME/Textures

  • You can have any path you want from this point on.
  • When referencing textures in your mod, using <graphicPath>, have the complete path relative to your mod, including the filename (but not the file extension). Example for the RoyalBed Testmod: <graphicPath>Things/Building/RoyalBed</graphicPath>

For textures that have multiple faces, like animals and furniture which can be rotated; You can use the Graphic_Multi class. Immediately following your <graphicPath></graphicPath>. Your Path should point to the folder containing the different textures: Texturename_east (for both east- and west-facing), Texturename_south (front-facing), and Texturename_north (back-facing).

Inside your folder:

For plant textures that have multiple growth stages and/or utilize the Graphic_Random class, the different images for each stage must be in separate folders. These images within must also share the same filenames. For example, the corn plant has one immature form, and two different mature forms that appear at random once the plant is fully grown. Similarly to Graphic_Multi, when using the Graphic_Random class, you will always want to reference the folder containing the images rather than the images themselves. It should be noted that only harvestable plants are able to display an immature image.

An example of how retexturing a plant could work:

Even though there is only one image used for the immature corn plant, we must have two copies of it with the same filenames as the mature plant images in order to prevent broken textures in-game. If you wanted to add a leafless variant to a plant (such as the leafless trees in winter), another folder would be added.

Interactive "Things"

A Thing is anything that exists in the game world. It includes resources, races (humanoid and animal), buildings, furniture, and many others.

These are defined in MODNAME/Defs/ThingDefs If you make a new workbench, you'll need to define a recipe for it. This is a list, so you can have many new recipes listed. Note that new workbenches also need a WorkGiver tied to them, otherwise pawns will ignore your new bench.

The recipes themselves are defined in: MODNAME/Defs/RecipeDefs In here you can define what ingredients/resources are required, what can be used, and what the default recipe is.

Any new resources will need to be defined in: MODNAME/Defs/ThingDefs

Turrets

These are defined in two files in: MODNAME/Defs/ThingDefs/Buildings_Big.xml and Weapons_Guns.xml (Remember, these can be named anything)

Buildings_Big.xml defines the structure of the turret itself. For example:

Weapons_Gun.xml defines the weapon the turret uses. Anything can be used as a weapon for turrets, including grenades.

Resources

These are defined in: MODNAME/Defs/ThingDefs/Resources.xml

Research projects

These are found in: MODNAME/Defs/ResearchProjectDefs

You can have research trees as well, where additional research projects get unlocked as you move through. This is how to add prerequisites:

Because it's a list, you can have multiple prerequisites for a research project.

Contents

I want to make a mod that can.

Or, how do I make my mod do this?

You can find links to tutorials on the Modding Tutorials page. There's also more tutorials about the basics there, so don't miss out on that.

Contents

dnSpy

dnSpy is a great application for decompiling or debugging and has a Visual Studio editor feel to it. It's currently only for Windows.

  1. Download dnSpy and extract it somewhere.
  2. Open dnSpy.exe. Once it's open, click "open" on the top ribbon (or press Ctrl+O).
  3. Navigate to ../Rimworld***_Data/Managed/, relative to your Rimworld installation and with *** being a version number.
  4. Ctrl+Shift+K to open the search bar.
  5. Explore the assembly and look through the source code to your heart's desire.

ILSpy

One method is to use ILSpy. This software is recommended because its settings are correct on a clean install. It provides binaries only for Windows, but you can compile it yourself as a CLI application for Mono framework on OS X and Linux, see its "How to build" instructions on GitHub for more info.

  1. Download ILSpy (Download latest release) and extract it to a directory of your choosing. Optionally create a desktop shortcut;
  2. Either: associate the .dll extension with ILSpy:
    1. Navigate to Assembly-CSharp.dll in ../Rimworld***_Data/Managed/, relative to your Rimworld installation and with *** being a version number (See Note on MacOS below);
    2. Right-click "Open with" and select a standard program. Navigate to your ILSpy installation and double-click ILSpy.exe, tick the checkbox and accept;
    3. Double-click Assembly-CSharp.dll,
    1. Open ILSpy;
    2. Go to File -> Open or press Ctrl+O, navigate to ../Rimworld***_Data/Managed/, relative to your Rimworld installation and with *** being a version number (See Note on MacOS below);
    3. Select Assembly-CSharp.dll and confirm,

    If using CLI application:

    MonoDevelop

    MonoDevelop is capable of decompiling DLLs, albeit using clumsy initial settings. It is Linux only, otherwise you have to download Xamarin Studio which doesn't have a decompiler.

    1. Download MonoDevelop and install it;
    2. Either: associate the .dll extension with MonoDevelop:
      1. Navigate to Assembly-CSharp.dll in ../Rimworld***_Data/Managed/, relative to your Rimworld installation and with *** being a version number (See Note on MacOS below);
      2. Right-click "Open with" and select MonoDevelop as standard program;
      3. Double-click Assembly-CSharp.dll,
      1. Open MonoDevelop;
      2. Go to File -> Open, navigate to ../Rimworld***_Data/Managed/, relative to your Rimworld installation and with *** being a version number (See Note on MacOS below);
      3. Select Assembly-CSharp.dll and confirm,

      Rider

      Rider is a cross-platform IDE with a built-in decompiler. If you're using Rider as your IDE (Setup Instructions), you can view the source of any RimWorld class or method by right-clicking its name and clicking Go To > Definition.

      The basics: Defs

      The most basic kind of RimWorld modding is done by editing Defs.

      Definitions each define some piece of the game. Different kinds of definitions can define:

      When the game runs, it collects all the definitions into a database. It then draws from these pools in when appropriate. It will randomly draw guns of a certain category to arm a new enemy mercenary. Or, it will randomly spawn an animal type on the edge of the map. Modding the game with Defs means adding your definitions to these pools and watching the game use them.

      A mod could be a pack of new weapons, a new animal, or a total conversion of the game. A mod will usually contain several Defs of various types, as well as content like images and sounds.

      The base game is defined in a single mod called Core.

      Players can choose which mods to activate. Most players often have several mods active at once. You could use a medieval mod to replace the core mod, and add on a weapons pack created by someone else. The game will run with the medieval mod content, and also randomly spawn in weapons from the extra weapons pack.

      Mod-making tutorials

      Mod-making tutorials are available at the Modding Tutorials page. If you can't find what you need, you should try looking in the forums help section on modding to check if the information you're looking for has been asked previously, before starting your own thread.

      Mod-making Templates

      Multi-version Mods

      Читайте также: