Modding:IDE reference

From Stardew Valley Wiki
Revision as of 15:34, 17 July 2020 by Pathoschild (talk | contribs) (→‎Create a mod project: update for Visual Studio 2019, tweak headers)
Jump to navigation Jump to search

Index

This page is a quick reference for how to use Visual Studio 2017 or MonoDevelop/Visual Studio for Mac when creating or editing a SMAPI mod. See Modding:Modder Guide for the main guide.

Before you start

  • You should install Visual Studio Community (Windows), MonoDevelop (Linux), or Visual Studio for Mac (Mac).
  • Reviewing C# Fundamentals for Absolute Beginners is strongly recommended if you're new to programming C#.
  • Here are some basic terms to remember:
    term definition
    IDE The program used to edit, run, and compile your code (short for Integrated Development Environment). The main IDEs are Visual Studio on Windows, and MonoDevelop/Visual Studio for Mac on Linux/Mac.
    DLL The file with a .dll extension which contains your compiled code (short for Dynamic Link Library). This is the file that SMAPI reads when loading your mod.
    project A collection of source code + configuration + resources (like images) you edit in the IDE. Each project is compiled into its own DLL.
    solution A collection of projects with some global settings. The solution itself isn't compiled, but it enables some convenient features (like letting projects easily reference each other).

Create a mod project

Before you can write your mod code, you need to create a solution and project to contain it.

Windows (Visual Studio 2019)

  1. Open Visual Studio 2019.
  2. Click Create a new project from the start screen.
    Modding - IDE reference - create project (Visual Studio 1).png
  3. Filter by "C#" and "Library", then choose Class Library (.NET Framework) and click Next. Make sure you don't choose Class Library (.NET Core) or Class Library (.NET Standard), which won't work.
    Modding - IDE reference - create project (Visual Studio 2).png
  4. In the configure your new project window:
    1. Enter a descriptive mod name. By convention, the name should be one word with mixed caps (like "PineapplesEverywhere").
    2. Make sure "Place solution and project in the same directory" is not checked.
    3. Make sure .NET Framework 4.5.2 is selected.
    4. Click "Create" to to create the project.
    Modding - IDE reference - create project (Visual Studio 3).png

Linux (MonoDevelop)

  1. Open MonoDevelop.
  2. Click File » New Solution from the menu bar:
    Modding - IDE reference - create project (MonoDevelop 1).png
  3. In the 'New Project' window, choose .NET » Library and click Next:
    Modding - IDE reference - create project (MonoDevelop 2).png
  4. Enter a descriptive mod name. By convention, the name should be one word with mixed caps (like "PineapplesEverywhere"):
    Modding - IDE reference - create project (MonoDevelop 3).png
  5. Make sure "create a project directory within the solution directory" is checked, and click Create:
    Modding - IDE reference - create project (MonoDevelop 4).png

MacOS (Visual Studio for Mac)

  1. Open Visual Studio for Mac.
  2. Click File » New Solution from the menu bar:
    Modding - IDE reference - create project (Visual Studio for Mac 1).png
  3. In the 'New Project' window, choose .NET » Library and click Next:
    Modding - IDE reference - create project (Visual Studio for Mac 2).png
  4. Enter a descriptive mod name. By convention, the name should be one word with mixed caps (like "PineapplesEverywhere"):
    Modding - IDE reference - create project (Visual Studio for Mac 3).png
  5. Make sure "create a project directory within the solution directory" is checked, and click Create:
    Modding - IDE reference - create project (Visual Studio for Mac 4).png

Set the target framework

The 'target framework' is the version of .NET Framework your code uses, which affects the version needed to run your mod. The recommended target framework is .NET Framework 4.5, which is the version SMAPI itself targets.

In Visual Studio 2017

  1. Open the Solution Explorer pane. If it's not visible, click View » Solution Explorer from the menu:
    Modding - IDE reference - show solution pane (Visual Studio).png
  2. From the Solution Explorer, right-click on the project and choose Properties:
    Modding - IDE reference - change target framework (Visual Studio 1).png
  3. On the Application tab, change the Target Framework dropdown to .NET Framework 4.5:
    Modding - IDE reference - change target framework (Visual Studio 2).png
  4. A dialogue may appear asking you to confirm the change. Click 'Yes' to confirm:
    Modding - IDE reference - change target framework (Visual Studio 3).png

In MonoDevelop/Visual Studio for Mac

  1. Open the Solution pad. If it's not visible, click View » Pads » Solution from the menu:
    Modding - IDE reference - show solution pane (MonoDevelop).png
  2. From the Solution pad, right-click on the project and choose Options:
    Modding - IDE reference - change target framework (MonoDevelop 1).png
  3. On the Build » General tab, change the Target Framework dropdown to Mono / .NET 4.5:
    Modding - IDE reference - change target framework (MonoDevelop 2).png

Add a file

In Visual Studio 2017

  1. Open the Solution Explorer pane. If it's not visible, click View » Solution Explorer from the menu:
    Modding - IDE reference - show solution pane (Visual Studio).png
  2. From the Solution Explorer pane, right-click on the project and choose Add » New Item:
    Modding - IDE reference - create file (Visual Studio 1).png
  3. From the 'Add New Item' window, choose the file type (usually Visual C# Item » Class):
    Modding - IDE reference - create file (Visual Studio 2).png
  4. Enter a descriptive file name and click Add:
    Modding - IDE reference - create file (Visual Studio 3).png

In MonoDevelop/Visual Studio for Mac

  1. Open the Solution pad. If it's not visible, click View » Pads » Solution from the menu:
    Modding - IDE reference - show solution pane (MonoDevelop).png
  2. From the Solution pad, right-click on the project to delete and choose Add » New File:
    Modding - IDE reference - create file (MonoDevelop 1).png
  3. From the 'New File' window, choose the file type (usually General » Empty Class):
    Modding - IDE reference - create file (MonoDevelop 2).png
  4. Enter a descriptive file name and click New:
    Modding - IDE reference - create file (MonoDevelop 3).png

Delete a file

In Visual Studio 2017

  1. Open the Solution Explorer pane. If it's not visible, click View » Solution Explorer from the menu:
    Modding - IDE reference - show solution pane (Visual Studio).png
  2. From the Solution Explorer pane, right-click on the file to delete and choose Delete:
    Modding - IDE reference - delete file (Visual Studio).png

In MonoDevelop/Visual Studio for Mac

  1. Open the Solution pad. If it's not visible, click View » Pads » Solution from the menu:
    Modding - IDE reference - show solution pane (MonoDevelop).png
  2. From the Solution pad, right-click on the file to delete and choose Remove:
    Modding - IDE reference - delete file (MonoDevelop).png

Add a NuGet package

In Visual Studio 2017

  1. Click Tools » NuGet Package Manager » Manage NuGet Packages for Solution from the menu:
    Modding - IDE reference - add NuGet package (Visual Studio 1).png
  2. On the Browse tab, search for the package and click on the result to display some options:
    Modding - IDE reference - add NuGet package (Visual Studio 2).png
  3. In the options, check the box next to your project and click Install:
    Modding - IDE reference - add NuGet package (Visual Studio 3).png
  4. If a 'Review Changes' dialogue appears, click OK:
    Modding - IDE reference - add NuGet package (Visual Studio 4).png

In MonoDevelop/Visual Studio for Mac

  1. Click Project » Add NuGet Packages from the menu:
    Modding - IDE reference - add NuGet package (MonoDevelop 1).png
  2. Search for the package, click on the result, and click Add Package:
    Modding - IDE reference - add NuGet package (MonoDevelop 2).png

Edit project file (.csproj)

Sometimes you may want to edit the project file directly (mainly to configure build steps). The project is a .csproj file, and can be edited from within the IDE.

In Visual Studio 2017

  1. Open the Solution Explorer pane. If it's not visible, click View » Solution Explorer from the menu:
    Modding - IDE reference - show solution pane (Visual Studio).png
  2. From the Solution Explorer pane, right-click on the project and choose Unload:
    Modding - IDE reference - edit project (Visual Studio 1).png
  3. Right-click on the project again and choose Edit <project name>.csproj:
    Modding - IDE reference - edit project (Visual Studio 2).png
  4. Make your changes in the editor that appears and save.
  5. When you're done, right-click on the project again and choose Reload Project:
    Modding - IDE reference - edit project (Visual Studio 3).png

In MonoDevelop/Visual Studio for Mac

  1. Open the Solution pad. If it's not visible, click View » Pads » Solution from the menu:
    Modding - IDE reference - show solution pane (MonoDevelop).png
  2. From the Solution pad, right-click on the project and choose Tools » Edit File:
    Modding - IDE reference - edit project (MonoDevelop).png
  3. Make your changes in the editor that appears and save.

Find compiled files

In Visual Studio 2017

  1. Open the Solution Explorer pane. If it's not visible, click View » Solution Explorer from the menu:
    Modding - IDE reference - show solution pane (Visual Studio).png
  2. From the Solution Explorer pane, right-click on the project and choose Open Folder in File Explorer:
    Modding - IDE reference - view build output (Visual Studio).png
  3. Navigate to bin\Debug (or bin\Release if you switched to release build configuration).

In MonoDevelop/Visual Studio for Mac

  1. Open the Solution pad. If it's not visible, click View » Pads » Solution from the menu:
    Modding - IDE reference - show solution pane (MonoDevelop).png
  2. From the Solution pad, right-click on the project and choose Open Containing Folder:
    Modding - IDE reference - view build output (MonoDevelop).png
  3. Navigate to bin/Debug (or bin/Release if you switched to release build configuration).

Add a reference to another DLL

In Visual Studio 2017

  1. Open the Solution Explorer pane. If it's not visible, click View » Solution Explorer from the menu:
    Modding - IDE reference - show solution pane (Visual Studio).png
  2. From the Solution Explorer pane, right-click on References and choose Add Reference...:
    Modding - IDE reference - add reference (Visual Studio 1).png
  3. From the 'Reference Manager' window, choose Browse in the left side, then click Browse.. at the bottom:
    Modding - IDE reference - add reference (Visual Studio 2).png
  4. Find the DLL you want to reference, select it, and click Add:
    Modding - IDE reference - add reference (Visual Studio 3).png
  5. From the 'Reference Manager' window, click OK:
    Modding - IDE reference - add reference (Visual Studio 4).png