### Install WASI Workload
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Installs the experimental WASI workload required for building WebAssembly projects.
```bash
dotnet workload install wasi-experimental
```
--------------------------------
### Pin .NET SDK Version
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Creates a global.json file to pin the .NET SDK version to 8.0.301, ensuring compatibility with the bundler. This specific SDK version may need to be installed on your system.
```json
{
"sdk":
{
"version": "8.0.301",
"rollForward": "latestPatch"
}
}
```
--------------------------------
### Create New WASM Project
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Creates a new console project configured for WebAssembly.
```bash
dotnet new wasiconsole
```
--------------------------------
### C# Project Configuration for Native C
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Configures the .csproj file to include the native C source file and specify custom initialization export names for the WebAssembly runtime.
```xml
<_WasmRuntimePackSrcFile Include="$(MSBuildThisFileDirectory)MyNativeCFile.c" />
```
--------------------------------
### Constructing a Position Object
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Demonstrates how to create a Position object from a tuple of integers.
```csharp
Position myPos = (30, 40);
```
--------------------------------
### ScreepsDotNet World Entrypoint
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
This C# code is the entrypoint for ScreepsDotNet applications targeting the World environment. It utilizes specific attributes for IL trimming and platform compatibility, and initializes the game instance.
```C#
using System;
using System.Diagnostics.CodeAnalysis;
using ScreepsDotNet.API.World;
namespace ScreepsDotNet
{
public static partial class Program
{
private static IGame? game;
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, typeof(Program))]
public static void Main()
{
// Keep the entrypoint platform independent and let Init (which is called from js) create the game instance
// This keeps the door open for unit testing later down the line
}
[System.Runtime.Versioning.SupportedOSPlatform("wasi")]
public static void Init()
{
try
{
game = new Native.World.NativeGame();
// TODO: Add startup logic here!
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
[System.Runtime.Versioning.SupportedOSPlatform("wasi")]
public static void Loop()
{
if (game == null) { return; }
try
{
game.Tick();
// TODO: Add loop logic here!
Console.WriteLine($"Hello world from C#, the current tick is {game.Time}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
```
--------------------------------
### ScreepsDotNet Arena Entrypoint
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
This C# code serves as the entrypoint for ScreepsDotNet applications targeting the Arena environment. It includes necessary attributes for IL trimming and platform support, and initializes the game instance.
```C#
using System;
using System.Diagnostics.CodeAnalysis;
using ScreepsDotNet.API.Arena;
namespace ScreepsDotNet
{
public static partial class Program
{
private static IGame? game;
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicMethods, typeof(Program))]
public static void Main()
{
// Keep the entrypoint platform independent and let Init (which is called from js) create the game instance
// This keeps the door open for unit testing later down the line
}
[System.Runtime.Versioning.SupportedOSPlatform("wasi")]
public static void Init()
{
try
{
game = new Native.Arena.NativeGame();
// TODO: Add startup logic here!
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
[System.Runtime.Versioning.SupportedOSPlatform("wasi")]
public static void Loop()
{
if (game == null) { return; }
try
{
game.Tick();
// TODO: Add loop logic here!
Console.WriteLine($"Hello world from C#, the current tick is {game.Utils.GetTicks()}");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}
```
--------------------------------
### ScreepsDotNet Build Commands
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Commands for building ScreepsDotNet projects. Use publish mode for optimized assembly size, with debug builds recommended for faster iteration.
```bash
dotnet publish -c Debug
-- or --
dotnet publish -c Release
```
--------------------------------
### Abstracted API for Native/Managed Code
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Provides a unified API for 'AddTwo' and 'Sum' that uses native C icalls when running in a WebAssembly environment and falls back to managed implementations otherwise.
```CSharp
using System.Runtime.InteropServices;
public void AddTwo(int a, int b)
{
if (RuntimeInformation.OSArchitecture == Architecture.Wasm)
{
return MyProject_Native.AddTwo(a, b);
}
else
{
return a + b;
}
}
public void Sum(ReadOnlySpan values)
{
if (RuntimeInformation.OSArchitecture == Architecture.Wasm)
{
unsafe
{
fixed (int* valuesPtr = values)
{
return MyProject_Native.Sum(valuesPtr, values.Length);
}
}
}
else
{
int result = 0;
foreach (int value in values)
{
result += value;
}
return result;
}
}
```
--------------------------------
### Calling Native C Functions from C#
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Demonstrates how to call the bound native C functions 'AddTwo' and 'Sum' from C# code, including handling Span for the Sum function.
```CSharp
Console.WriteLine($"1 + 2 = {AddTwo(1, 2)}!");
Span values = [1, 2, 3];
unsafe
{
fixed (int* valuesPtr = values)
{
Console.WriteLine($"1 + 2 + 3 = {Sum(valuesPtr, values.Length)}!");
}
}
```
--------------------------------
### Checking Object Existence
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Illustrates how to verify if a game object reference is still valid using the Exists property.
```csharp
if (gameObject.Exists) {
// ... use the object
}
```
--------------------------------
### Add ScreepsDotNet NuGet Packages
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Adds the necessary ScreepsDotNet API and Bundler packages to the project.
```bash
dotnet add (MyProjectName) package ScreepsDotNet.API
dotnet add (MyProjectName) package ScreepsDotNet.Bundler
```
--------------------------------
### Enable Wasm Compression
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Enable compression for the Wasm bundle to reduce size at the cost of increased CPU usage during startup. This is configured using the 'ScreepsCompressWasm' property.
```XML
true
```
--------------------------------
### Storing User Data on Game Objects
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Shows how to associate custom user data with a game object using SetUserData. Note that only reference types can be stored.
```csharp
gameObject.SetUserData(new MyData());
```
--------------------------------
### ScreepsDotNet Project Configuration
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Configures a .NET project for ScreepsDotNet, including target framework, runtime identifier, and publishing settings. Note that trimming, compression, and encoding settings have implications and may require adjustment.
```xml
net8.0
wasi-wasm
Exe
true
enable
true
full
true
true
true
false
true
true
false
b64
```
--------------------------------
### Including Custom JS Files in Screeps Arena
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Specifies MSBuild properties to include custom JavaScript files in the Screeps Arena distribution. 'ScreepsArenaJsFiles' adds files to the output, while 'ScreepsArenaStartup' and 'ScreepsArenaLoop' control their inclusion in the main script.
```xml
AppBundle/arena
true
true
```
--------------------------------
### Including Custom JS Files in Screeps World
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Specifies MSBuild properties to include custom JavaScript files in the Screeps World distribution. 'ScreepsWorldJsFiles' adds files to the output, while 'ScreepsWorldStartup' and 'ScreepsWorldLoop' control their inclusion in the main script.
```xml
AppBundle/world
true
true
```
--------------------------------
### Configure Bundle Encoding
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Override the default bundle encoding behavior for Screeps Arena. Possible encodings are 'bin', 'b64', and 'b32768'.
```XML
b64
```
--------------------------------
### Native C Functions for Screeps.NET
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Defines C functions for summing two integers or an array of integers, and exports them for use as internal calls in C#.
```C
#include
int AddTwo(int a, int b)
{
return a + b;
}
int Sum(int* values, int n)
{
int result = 0;
for (int i = 0; i < n; i++)
{
result += values[i];
}
return result;
}
__attribute__((export_name("myproject_initnative")))
void myproject_initnative()
{
mono_add_internal_call("MyProject_Native::AddTwo", AddTwo);
mono_add_internal_call("MyProject_Native::Sum", Sum);
}
```
--------------------------------
### C# Binding for Native C Functions
Source: https://github.com/thomasfn/screepsdotnet/blob/main/README.md
Defines a static internal C# class with extern methods that bind to the native C functions exposed via internal calls.
```CSharp
using System.Runtime.CompilerServices;
internal static class MyProject_Native
{
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern int AddTwo(int a, int b);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern unsafe int Sum(int* values, int n);
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.