Skip to main content

Writing C# in Visual Studio Code with linting for Streamer.bot

Description

This tutorial provides a general step-by-step guide on setting up Visual Studio Code (VS Code) for writing C# code for Streamer.bot. By following these instructions, you'll be able to write code with linting, which will help you catch errors early and ensure your code compiles. You still need to copy and paste your code into Streamer.bot's C# compiler.

What is linting?

A lint, or linter is a tool that analyzes your source code to automatically flag programming errors, bugs, stylistic errors, and suspicious constructs. (source)

Pre-requisites


Installation

Installation

  1. Install Visual Studio Code
  2. Launch Visual Studio Code
  3. Open Extensions
    • Keyboard Shortcut: Ctrl+Shift+X
    • Menu Bar: View > Extensions
  4. Install the C# extension image
  5. This will also install the following Extension dependencies:
    • .NET Install Tool

Configure new project once VS Code is configured

  1. Open the Explorer
    • Keyboard Shortcut: Ctrl+Shift+E
    • Menu Bar: View > Explorer
  2. Create a new file with a .csproj filename extension
    • If you used the IntelliSense instructions, the .csproj file may already exist, so open that instead
  3. Replace with this template in the code block below
  4. Save and close the .csproj file
.csproj file for Windows
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net481</TargetFramework>
<LangVersion>13.0</LangVersion>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<DefineConstants>DEBUG;NOT_STREAMERBOT</DefineConstants>
<NoWarn>CS0114</NoWarn>
<!-- Fill in the path to your Streamer.bot folder here -->
<StreamerBotPath>D:\overlays\streamerbot\</StreamerBotPath>
<DotNetPath>C:\Windows\Microsoft.NET\Framework64\v4.0.30319\</DotNetPath>
</PropertyGroup>
<ItemGroup>
<!-- Include all DLLs in a specific directory in the build output -->
<Reference Include="$(DotNetPath)mscorlib.dll" />
<Reference Include="$(DotNetPath)netstandard.dll" />
<Reference Include="$(DotNetPath)System.dll" />
<Reference Include="$(DotNetPath)System.Core.dll" />
<Reference Include="$(DotNetPath)System.Net.dll" />
<Reference Include="$(DotNetPath)System.Net.Http.dll" />
<Reference Include="$(DotNetPath)System.Drawing.dll" />
<Reference Include="$(DotNetPath)System.Windows.Forms.dll" />
<!-- Use forward slashes for cross-platform compatibility -->
<Reference Include="$(StreamerBotPath)Streamer.bot.Plugin.Interface.dll" />
<Reference Include="$(StreamerBotPath)Streamer.bot.Common.dll" />
<Reference Include="$(StreamerBotPath)Twitch.Common.dll" />
<Reference Include="$(StreamerBotPath)NAudio*.dll" />
<Reference Include="$(StreamerBotPath)Wpf*.dll" />
<Reference Include="$(StreamerBotPath)Newtonsoft.Json.dll" />
<!-- Uncomment to reference all dlls in the streamerbot directory -->
<!-- <Reference Include="$(StreamerBotPath)**/*.dll" /> -->
</ItemGroup>
</Project>
.csproj notes
  • The .csproj file configures the project as a .NET Framework 4.8.1 project
    • Streamer.bot v0.2.8 and earlier targetted .NET Framework 4.7.2
  • I've added some basic assembly references that are used frequently
  • You will need to add more per your requirements
  • To point to your own Streamer.bot dlls, replace the directory with your own Streamer.bot directory and Streamer.bot dlls directory

Template for the .cs file

  1. Open the Explorer
    1. Keyboard Shortcut: Ctrl+Shift+E
    2. Menu Bar: View > Explorer
  2. Create a new .cs file and use the following as a template
Template.cs
// Place your namespaces here
using System;

// Do not edit anything other than UniqueClassName //
// which should match your file name //
#if NOT_STREAMERBOT //
using Streamer.bot.Plugin.Interface; //
using Streamer.bot.Plugin.Interface.Model; //
using Streamer.bot.Plugin.Interface.Enums; //
using Streamer.bot.Common.Events; //
public class UniqueClassName : CPHInlineBase
#else //
public class CPHInline //
#endif //
//-------------------------------------------------//
{
public bool Execute()
{
// your main code goes here
return true;
}
}
.cs notes
  • This is similar to the default code found in the Execute C# Code subaction
  • If you have multiple .cs files in the same project, just make sure that you change UniqueClassName to any unique string, otherwise you will see compile warnings
  • The DefineConstants in the .csproj file sets NOT_STREAMERBOT, which how the compiler can detect whether to use the UniqueClassName with the CPHInlineBase inheritor. If pasted into Streamer.bot, then Streamer.bot doesn't have that set, so it uses CPHInline

CPH methods will now auto-fill

  • You can type CPH. and it will automatically give you the available CPH methods

    • This uses the Streamer.bot.Plugin.Interface.dll of your Streamer.bot folder
    • The advantage of this is that you will have a current list of methods available based on which version of Streamer.bot you're running. image
  • Open Problems

    • Keyboard Shortcut: Ctrl+Shift+M
    • Menu bar: View > Problems
  • The Problems view will show you if there will be any compile errors

    • Look for any red error icons

    image

info
  • Compiler errors will show with a red ⭕ and need to be remedied
  • Warnings will be shown with a yellow triangle, however, code will still compile in Streamer.bot

Copying code to Streamer.bot

  1. All you need to do now is copy and paste the code into an Execute C# sub-action in Streamer.bot. You do not need to edit any of the code after pasting.
  2. Make sure the code compiles successfully by clicking Compile
  3. Finally, click Save and Compile