feat: improve template some more

This commit is contained in:
Tobias Berger 2024-04-04 23:05:34 +02:00
parent 035f652046
commit e52d10e49a
Signed by: toby
GPG key ID: 2D05EFAB764D6A88
14 changed files with 301 additions and 165 deletions

2
.vscode/mod.csproj vendored
View file

@ -57,7 +57,7 @@
<PackageReference Include="Krafs.Rimworld.Ref"
Version="1.4.3901" />
<!-- Set IncludeAssets to 'compile' to enable Harmony -->
<PackageReference Include="Lib.Harmony.Thin"
<PackageReference Include="Lib.Harmony"
Version="2.3.3"
IncludeAssets="none" />
<PackageReference Include="roslynator.analyzers"

20
.vscode/settings.json vendored
View file

@ -3,11 +3,11 @@
"files.exclude": {
".direnv": true,
".vscode/obj": true,
"*/Assemblies/": true,
"*/Assemblies/": true
},
"files.readonlyInclude": {
"log/rimworld.log": true,
"About/About.xml": true,
"About/About.xml": true
},
"omnisharp.organizeImportsOnFormat": true,
"dotnet.inlayHints.enableInlayHintsForParameters": true,
@ -25,5 +25,19 @@
"csharp.suppressDotnetInstallWarning": true,
"csharp.suppressDotnetRestoreNotification": true,
"dotnet.server.useOmnisharp": true,
"omnisharp.enableLspDriver": false
"omnisharp.enableLspDriver": false,
"[xml]": {
"editor.quickSuggestions": {
"other": "on",
"comments": "off",
"strings": "off"
}
},
"[jsonc]": {
"editor.quickSuggestions": {
"strings": true
},
"editor.suggest.insertMode": "replace",
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
}

1
.vscode/setup.sh vendored
View file

@ -18,3 +18,4 @@ else
fi
popd

23
.vscode/tasks.json vendored
View file

@ -23,9 +23,7 @@
"isDefault": true
},
"command": ".vscode/build.sh",
"problemMatcher": [
"$msCompile"
],
"problemMatcher": ["$msCompile"],
"dependsOn": []
},
{
@ -36,9 +34,7 @@
"isDefault": true
},
"command": ".vscode/build.sh Release",
"problemMatcher": [
"$msCompile"
],
"problemMatcher": ["$msCompile"],
"dependsOn": []
},
{
@ -51,10 +47,7 @@
"command": ".vscode/launch.sh",
"problemMatcher": [],
"dependsOrder": "sequence",
"dependsOn": [
"build mod (Debug)",
"set up mod directory"
]
"dependsOn": ["build mod (Debug)", "set up mod directory"]
},
{
"label": "run game (Release)",
@ -66,10 +59,7 @@
"command": ".vscode/launch.sh",
"problemMatcher": [],
"dependsOrder": "sequence",
"dependsOn": [
"build mod (Release)",
"set up mod directory"
]
"dependsOn": ["build mod (Release)", "set up mod directory"]
},
{
"label": "run game (Debug + quicktest)",
@ -81,10 +71,7 @@
"command": ".vscode/launch.sh -quicktest",
"problemMatcher": [],
"dependsOrder": "sequence",
"dependsOn": [
"build mod (Debug)",
"set up mod directory"
]
"dependsOn": ["build mod (Debug)", "set up mod directory"]
},
{
"label": "set up mod directory",

Binary file not shown.

View file

@ -1,2 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<LanguageData />
<LanguageData>
<TemplateMod.SettingsCategory>Template Mod</TemplateMod.SettingsCategory>
</LanguageData>

View file

@ -1,19 +0,0 @@
# RimWorld Mod Template
This template is created for creating RimWorld mods in [VSCodium/Visual Studio Code](https://vscodium.com/)
## Setup
1. Ensure the extension `ms-dotnettools.csharp` (a.k.a. [C#](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp)) is installed
1. Run the task `link mod directory`.
- This should symlink the mod directory to RimWorld's mod folder.
- The script won't work if you have installed RimWorld anywhere else than the default steam install.
1. Run the task `build mod` to compile assemblies and About.xml
1. Run the task `run game` to launch the game
- The script won't work if you have installed RimWorld anywhere else than the default steam install.
## Additional notes
- All attributes for `About.xml` are kept in `.vscode/mod.csproj`, `About.xml` will get overriden each build.
- You can run `nix fmt` to format all code in the repository.
- While this template is designed for VSCodium, it works just as well with any other editor, simply launch the scripts manually

92
Source/Mod.cs Normal file
View file

@ -0,0 +1,92 @@
using System.Reflection;
// using HarmonyLib;
using Verse;
namespace Template;
using Settings;
using UnityEngine;
public class TemplateMod : Mod
{
internal static string Translate(string key)
{
const string TranslationKey = nameof(TemplateMod);
return (TranslationKey + "." + key).Translate();
}
public TemplateMod(ModContentPack content)
: base(content)
{
#if DEBUG
const string build = "Debug";
#else
const string build = "Release";
#endif
Log.Message(
$"Running Version {Assembly.GetAssembly(typeof(TemplateMod)).GetName().Version} "
+ build
);
Log.Message(content.ModMetaData.packageIdLowerCase);
// Harmony harmony = new(content.ModMetaData.packageIdLowerCase);
}
#nullable disable // Set in constructor.
public static TemplateSettings Settings { get; private set; }
#nullable enable
public void ResetSettings()
{
Settings = new();
}
public override void DoSettingsWindowContents(Rect inRect) =>
SettingsWindow.DoSettingsWindowContents(inRect);
public override string SettingsCategory() => SettingsWindow.SettingsCategory();
public static class Log
{
const string LogPrefix = "Toby's Template Mod - ";
public static void DebugError(string message)
{
#if DEBUG
Error(message);
#endif
}
public static void Error(string message)
{
Verse.Log.Error(LogPrefix + message);
}
public static void DebugWarn(string message)
{
#if DEBUG
Warn(message);
#endif
}
public static void Warn(string message)
{
Verse.Log.Warning(LogPrefix + message);
}
public static void DebugLog(string message)
{
#if DEBUG
Message(message);
#endif
}
public static void Message(string message)
{
Verse.Log.Message(LogPrefix + message);
}
}
}

View file

@ -0,0 +1,30 @@
using UnityEngine;
using Verse;
namespace Template.Settings;
public static class SettingsWindow
{
private static Vector2 settingsScrollPosition = new();
private static float settingsHeight;
private static TemplateSettings Settings => TemplateMod.Settings;
public static void DoSettingsWindowContents(Rect inRect)
{
Listing_Standard listing = new();
Rect viewRect = new(inRect.x, inRect.y, inRect.width - 16f, settingsHeight);
Widgets.BeginScrollView(inRect, ref settingsScrollPosition, viewRect);
listing.Begin(new Rect(viewRect.x, viewRect.y, viewRect.width, float.PositiveInfinity));
listing.End();
settingsHeight = listing.CurHeight;
Widgets.EndScrollView();
}
public static string SettingsCategory()
{
return TemplateMod.Translate("SettingsCategory");
}
}

View file

@ -0,0 +1,44 @@
using System.Collections.Generic;
using Verse;
namespace Template.Settings;
public class TemplateSettings : ModSettings
{
#region Scribe Helpers
private static void LookField<T>(ref T value, string label, T defaultValue)
where T : struct
{
Scribe_Values.Look(ref value, label, defaultValue);
}
private static void LookHashSet<T>(
ref HashSet<T> valueHashSet,
string label,
HashSet<T> defaultValues
)
where T : notnull
{
if (Scribe.mode == LoadSaveMode.Saving && valueHashSet is null)
{
TemplateMod.Log.Warn(
label + " is null before saving. Reinitializing with default values."
);
valueHashSet = defaultValues;
}
Scribe_Collections.Look(ref valueHashSet, label, lookMode: LookMode.Value);
if (Scribe.mode == LoadSaveMode.LoadingVars && valueHashSet is null)
{
TemplateMod.Log.Warn(
label + " is null after loading. Reinitializing with default values."
);
valueHashSet = defaultValues;
}
}
#endregion
public override void ExposeData()
{
base.ExposeData();
}
}

View file

@ -1,14 +0,0 @@
using Verse;
// using HarmonyLib;
namespace Template;
[StaticConstructorOnStartup]
public static class Startup
{
static Startup()
{
Log.Message("Mod template loaded successfully!");
}
}

View file

@ -6,7 +6,7 @@ using Verse;
namespace Template;
[DefOf]
public static class TemplateDefOf
public static class StinkyTweaksDefOf
{
public static LetterDef success_letter;
}

View file

@ -12,8 +12,8 @@ public class TemplateMapComponent(Map map) : MapComponent(map)
Messages.Message("Success", null, MessageTypeDefOf.PositiveEvent);
Find.LetterStack.ReceiveLetter(
"Success",
TemplateDefOf.success_letter.description,
TemplateDefOf.success_letter,
StinkyTweaksDefOf.success_letter.description,
StinkyTweaksDefOf.success_letter,
null
);
}

View file

@ -28,7 +28,6 @@
programs = {
nixpkgs-fmt.enable = true;
csharpier.enable = true;
prettier.enable = true;
};
};
};