Jump to content

Trying to set up VS Code for mod development on a Mac


GrinningHermit

Recommended Posts

While trying to create a proper setup for mod development in VS Code, I found that Assembly-CSharp.dll and UnityEngine.dll need to be referenced. As far as I can discover, the references need to be in the .csproj file like this: 

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="Assembly-CSharp">
      <HintPath>../KerbalSpaceProgram/KSP.app/Contents/Resources/Data/Managed/Assembly-CSharp.dll</HintPath>
      <SpecificVersion>False</SpecificVersion>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Reference Include="UnityEngine">
      <HintPath>../KerbalSpaceProgram/KSP.app/Contents/Resources/Data/Managed/UnityEngine.dll</HintPath>
      <SpecificVersion>False</SpecificVersion>
    </Reference>
  </ItemGroup>
</Project>

I'm on Mac, so it seems the dlls need to be targeted inside the .app file. The problem is, that despite these lines I keep getting the error "The type 'MonoBehaviour' is defined in an assembly that is not referenced. You must add a reference to assembly ..." when building or restoring (dotnet build / dotnet restore commands)

Does anyone have a clue what I'm missing?

Link to comment
Share on other sites

  • 2 weeks later...

Not sure if that's the issue here, but on mac you should be using the full .net framework provided by Mono, not .Net standard : https://www.mono-project.com/download/stable/#download-mac
For the target framework. It's either "v3.5" for KSP 1.7 and below or "v4.0" (or latter "v4.x" versions) for 1.8

Also, on Mac (or all platforms on KSP 1.8+), there should be a bunch of additional "UnityEngineXXX.dll" that you need to reference.
See that post :

 

Link to comment
Share on other sites

Thanks for the reply. I installed the Mono .net framework and found the additional references and am able to build without errors in Visual Studio Community and Visual Studio Code...

The resulting dll does not seem to work though. Can't get a 'Hello Kerbin' in the console log. I'm unsure what to do with the target framework reference.

.cs file:

using UnityEngine;

namespace MyMod
{
    public class MyMod : PartModule
    {
        public override void OnStart(StartState state)
        {
            print("Hello Kerbin!");
        }
    }
}

.csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net35</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Reference Include="Assembly-CSharp">
      <HintPath>..\..\KerbalSpaceProgram\KSP.app\Contents\Resources\Data\Managed\Assembly-CSharp.dll</HintPath>
      <Private>False</Private>
    </Reference>
    <Reference Include="UnityEngine.CoreModule">
      <HintPath>..\..\KerbalSpaceProgram\KSP.app\Contents\Resources\Data\Managed\UnityEngine.CoreModule.dll</HintPath>
      <Private>False</Private>
    </Reference>
    <Reference Include="UnityEngine">
      <HintPath>..\..\KerbalSpaceProgram\KSP.app\Contents\Resources\Data\Managed\UnityEngine.dll</HintPath>
      <Private>False</Private>
    </Reference>
  </ItemGroup>
</Project>

The VS Code build fails with target framework net35, but runs when targeting netcoreapp3.0. VS Community builds with the .csproj as seen above

Edited by GrinningHermit
Link to comment
Share on other sites

As far as I know the syntax is

9 minutes ago, GrinningHermit said:

<TargetFramework>v3.5</TargetFramework>

To print something in the unity console you need to use the "Debug.Log()" method.

Also, I really suggest using the wildcard syntax I posted to load all unity dlls, otherwise you will struggle finding the right ones for the various unity methods you will need.

Edited by Gotmachine
Link to comment
Share on other sites

34 minutes ago, Gotmachine said:

To print something in the unity console you need to use the "Debug.Log()" method.

We probably are talking about different things... I meant the KSP console. According to an old tutorial the .cs file I posted should produce result in the KSP console log.

Your target framework v3.5 reference gives an error unfortunately: Invalid target framework 'unsupported'.

Link to comment
Share on other sites

I don't think you should be using

Sdk="Microsoft.NET.Sdk"

That's for .NET standard projects.
How did you create that *.csproj ?
I suggest creating a new project using VS for mac, with the "multiplatform > library" template.
For reference, this is what I get in VS for windows using the default .net framework (not .net core / .net standard) library template :

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>fe8f3ab9-cb75-43eb-b1a3-5a5e93a7fc66</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>ClassLibrary1</RootNamespace>
    <AssemblyName>ClassLibrary1</AssemblyName>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System"/>
    <Reference Include="System.Core"/>
    <Reference Include="System.Xml.Linq"/>
    <Reference Include="System.Data.DataSetExtensions"/>
    <Reference Include="System.Data"/>
    <Reference Include="System.Xml"/>
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Class1.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
 </Project>

And this is the *.csproj for my mod (targeting KSP 1.8 /.NET 4.x)
 

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{98D72929-051F-4028-9BA6-293E2379A2B2}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>MandatoryRCS</RootNamespace>
    <AssemblyName>MandatoryRCS</AssemblyName>
    <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <KSPDllPath>K:\Projets\KSP\Kerbal Space Program 1.8.1 PLAYTEST</KSPDllPath>
    <TargetFrameworkProfile />
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>portable</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <Prefer32Bit>false</Prefer32Bit>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
    <Prefer32Bit>false</Prefer32Bit>
  </PropertyGroup>
  <ItemGroup>
    <Compile Include="ComponentRWTorqueControl.cs" />
    <Compile Include="ComponentSASAttitude.cs" />
    <Compile Include="ComponentBase.cs" />
    <Compile Include="ComponentSASAutopilot.cs" />
    <Compile Include="GameEventsMandatoryRCS.cs" />
    <Compile Include="Lib\VesselPhysics.cs" />
    <Compile Include="UI\NavBallUI.cs" />
    <Compile Include="Settings.cs" />
    <Compile Include="Lib\MathExtensions.cs" />
    <Compile Include="Lib\Vector6.cs" />
    <Compile Include="ModuleTorqueController.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="ComponentPersistantRotation.cs" />
    <Compile Include="UI\UILib.cs" />
    <Compile Include="UI\UIObjects.cs" />
    <Compile Include="UI\UISprites.cs" />
    <Compile Include="VesselModuleMandatoryRCS.cs" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL" />
    <Reference Include="System.Xml">
      <Private>False</Private>
    </Reference>
    <Reference Include="System.Xml.Linq">
      <Private>False</Private>
    </Reference>
    <!-- KSP Dlls -->
    <Reference Include="$(KSPDllPath)\KSP_x64_Data\Managed\Assembly-CSharp.dll">
      <Private>False</Private>
    </Reference>
    <Reference Include="$(KSPDllPath)\KSP_x64_Data\Managed\Assembly-CSharp-firstpass.dll">
      <Private>False</Private>
    </Reference>
    <!-- load all UnityEngine Dlls (since 1.8 we only need those) -->
    <Reference Include="$(KSPDllPath)\KSP_x64_Data\Managed\UnityEngine*.dll">
      <Private>False</Private>
    </Reference>
  </ItemGroup>
  <ItemGroup>
    <Content Include="TODO.txt" />
  </ItemGroup>
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <PropertyGroup>
    <PostBuildEvent>
    </PostBuildEvent>
  </PropertyGroup>
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

 

Note that in the debug configuration, I'm using <DebugType>portable</DebugType>.
That will allow using the generated *.pdb debug symbols file with the unity debugger for VS (just put the *.pdb file next to your *.dll file in your mod folder)

Also, the KSP console is the unity console. Maybe "print" is redirected there, never tried, I've never seen it used anywhere.

Edited by Gotmachine
Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...