Stationpedia docs

This commit is contained in:
2025-12-02 17:59:40 -07:00
parent bf1daf12cc
commit 75f1c5c44a
11 changed files with 211 additions and 24 deletions

View File

@@ -3,6 +3,7 @@ namespace Slang;
using System;
using System.Collections.Generic;
using System.Text;
using Assets.Scripts.UI;
using StationeersIC10Editor;
public static unsafe class SlangExtensions
@@ -133,4 +134,34 @@ public static unsafe class SlangExtensions
return SlangFormatter.ColorDefault;
}
}
public static unsafe List<StationpediaPage> ToList(this Vec_FfiDocumentedItem_t vec)
{
var toReturn = new List<StationpediaPage>((int)vec.len);
var currentPtr = vec.ptr;
for (int i = 0; i < (int)vec.len; i++)
{
var doc = currentPtr[i];
var docItemName = doc.item_name.AsString();
var formattedText = TextMeshProFormatter.FromMarkdown(doc.docs.AsString());
var pediaPage = new StationpediaPage(
$"slang-item-{docItemName}",
docItemName,
formattedText
);
pediaPage.Text = formattedText;
pediaPage.Description = formattedText;
pediaPage.ParsePage();
toReturn.Add(pediaPage);
}
Ffi.free_docs_vec(vec);
return toReturn;
}
}

View File

@@ -121,6 +121,31 @@ public unsafe partial class Ffi {
slice_ref_uint16_t input);
}
[StructLayout(LayoutKind.Sequential, Size = 48)]
public unsafe struct FfiDocumentedItem_t {
public Vec_uint8_t item_name;
public Vec_uint8_t docs;
}
/// <summary>
/// Same as [<c>Vec<T></c>][<c>rust::Vec</c>], but with guaranteed <c>#[repr(C)]</c> layout
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 24)]
public unsafe struct Vec_FfiDocumentedItem_t {
public FfiDocumentedItem_t * ptr;
public UIntPtr len;
public UIntPtr cap;
}
public unsafe partial class Ffi {
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
void free_docs_vec (
Vec_FfiDocumentedItem_t v);
}
public unsafe partial class Ffi {
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
void free_ffi_diagnostic_vec (
@@ -164,6 +189,11 @@ public unsafe partial class Ffi {
Vec_uint8_t s);
}
public unsafe partial class Ffi {
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
Vec_FfiDocumentedItem_t get_docs ();
}
public unsafe partial class Ffi {
[DllImport(RustLib, ExactSpelling = true)] public static unsafe extern
Vec_FfiToken_t tokenize_line (

View File

@@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using Assets.Scripts.UI;
using StationeersIC10Editor;
public struct Range
@@ -151,6 +152,14 @@ public static class Marshal
}
}
/// <summary>
/// Gets the currently documented items from the Slang compiler and returns new StationpediaPages with correct formatting.
/// </summary>
public static unsafe List<StationpediaPage> GetSlangDocs()
{
return Ffi.get_docs().ToList();
}
private static string ExtractNativeLibrary(string libName)
{
string destinationPath = Path.Combine(Path.GetTempPath(), libName);

View File

@@ -224,4 +224,14 @@ public static class SlangPatches
_currentlyEditingMotherboard = null;
_motherboardCachedCode = null;
}
[HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Regenerate))]
[HarmonyPostfix]
public static void Stationpedia_Regenerate()
{
foreach (var page in Marshal.GetSlangDocs())
{
Stationpedia.Register(page);
}
}
}

View File

@@ -1,22 +0,0 @@
using Assets.Scripts.UI;
namespace Slang
{
public static class SlangDocs
{
public static StationpediaPage[] Pages
{
get
{
return
[
new StationpediaPage(
"slang-init",
"Slang",
"Slang is a new high level language built specifically for Stationeers"
),
];
}
}
}
}

View File

@@ -0,0 +1,54 @@
using System.Text.RegularExpressions;
namespace Slang;
public static class TextMeshProFormatter
{
private const string CODE_COLOR = "#FFD700";
public static string FromMarkdown(string markdown)
{
if (string.IsNullOrEmpty(markdown))
return "";
// 1. Normalize Line Endings
string text = markdown.Replace("\r\n", "\n");
// 2. Handle Code Blocks (```)
text = Regex.Replace(
text,
@"```\s*(.*?)\s*```",
match =>
{
var codeContent = match.Groups[1].Value;
return $"<color={CODE_COLOR}>{codeContent}</color>"; // Gold color for code
},
RegexOptions.Singleline
);
// 3. Handle Headers (## Header)
// Convert ## Header to large bold text
text = Regex.Replace(
text,
@"^##(\s+)?(.+)$",
"<size=120%><b>$1</b></size>",
RegexOptions.Multiline
);
// 4. Handle Inline Code (`code`)
text = Regex.Replace(text, @"`([^`]+)`", $"<color={CODE_COLOR}>$1</color>");
// 5. Handle Bold (**text**)
text = Regex.Replace(text, @"\*\*(.+?)\*\*", "<b>$1</b>");
// 6. Handle Italics (*text*)
text = Regex.Replace(text, @"\*(.+?)\*", "<i>$1</i>");
// 7. Convert Newlines to TMP Line Breaks
// Stationpedia needs <br> or explicit newlines.
// Often just ensuring \n is preserved is enough, but <br> is safer for HTML-like parsers.
text = text.Replace("\n", "<br>");
return text;
}
}