From 70a62b7048a756c637457ab3b6d9de845aea978a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 15 Oct 2026 01:54:21 +0300 Subject: [PATCH 1/3] Port EntityMapData SetValue --- Game.Shared/MapEntitiesShared.cs | 60 ++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/Game.Shared/MapEntitiesShared.cs b/Game.Shared/MapEntitiesShared.cs index d8cf2afbd..7d6765d62 100644 --- a/Game.Shared/MapEntitiesShared.cs +++ b/Game.Shared/MapEntitiesShared.cs @@ -1,5 +1,7 @@ using Source.Engine; +using System.Runtime.InteropServices; + namespace Game.Shared; public class EntityMapData @@ -73,14 +75,66 @@ public bool GetNextKey(Span keyName, Span value) { } // find the nth keyName in the entdata and change its value to the specified one. - // TODO: faithful port of CEntityMapData::SetValue (in-place buffer edit via pointer math). - // No callers yet (only used by the not-yet-ported ParseMapData / entity I/O fixup path). public bool SetValue(ReadOnlySpan keyName, ReadOnlySpan newValue, int nKeyInstance = 0) { Assert(EntDataSize != -1); if (EntDataSize == -1) return false; - throw new NotImplementedException(); + Span token = stackalloc byte[MAPKEY_MAXLENGTH]; + Span newValueBuf = stackalloc byte[1024]; + Span entData = MemoryMarshal.AsMemory(EntData).Span; + ReadOnlySpan key = keyName[..MapEntity.StrLen(keyName)]; + ReadOnlySpan inputData = entData; + int currKeyInstance = 0; + + while (!inputData.IsEmpty) { + inputData = MapEntity.ParseToken(inputData, token); + if (token[0] == '}') + break; + + if (token[..MapEntity.StrLen(token)].SequenceEqual(key)) { + ++currKeyInstance; + if (currKeyInstance > nKeyInstance) { + int prevOffset = entData.Length - inputData.Length; + inputData = MapEntity.ParseToken(inputData, token); + int postOffset = entData.Length - inputData.Length; + byte[] postData = entData.Slice(postOffset, MapEntity.StrLen(entData[postOffset..])).ToArray(); + + ReadOnlySpan value = newValue[..MapEntity.StrLen(newValue)]; + int newValueLen; + if (value.IsEmpty || value[0] != '"') { + newValueLen = Math.Min(value.Length, newValueBuf.Length - 3); + newValueBuf[0] = (byte)'"'; + value[..newValueLen].CopyTo(newValueBuf[1..]); + newValueBuf[newValueLen + 1] = (byte)'"'; + newValueLen += 2; + } + else { + newValueLen = Math.Min(value.Length, newValueBuf.Length - 1); + value[..newValueLen].CopyTo(newValueBuf); + } + + int padding = newValueLen - MapEntity.StrLen(token) - 2; + + int writeOffset = prevOffset + 1; + int destEnd = Math.Min(EntDataSize, entData.Length); + int copyLen = Math.Min(newValueLen, destEnd - writeOffset - 1); + newValueBuf[..copyLen].CopyTo(entData[writeOffset..]); + writeOffset += copyLen; + + int postLen = Math.Min(postData.Length, destEnd - writeOffset - 1); + postData.AsSpan(0, postLen).CopyTo(entData[writeOffset..]); + entData[writeOffset + postLen] = 0; + + CurrentKey = EntData[(EntData.Length - CurrentKey.Length + padding)..]; + return true; + } + } + + inputData = MapEntity.ParseToken(inputData, token); + } + + return false; } } From 8885f63fc0a95dbe5cbbbdedad78716c6b0a2232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Thu, 15 Oct 2026 01:58:47 +0300 Subject: [PATCH 2/3] Port point_template and template entity IO fixup --- Game.Server/MapEntities.cs | 22 ++- Game.Server/PointTemplate.cs | 253 ++++++++++++++++++++++++++++++++ Game.Server/TemplateEntities.cs | 163 +++++++++++++++++++- Source.Common/CFormatting.cs | 7 + 4 files changed, 439 insertions(+), 6 deletions(-) create mode 100644 Game.Server/PointTemplate.cs diff --git a/Game.Server/MapEntities.cs b/Game.Server/MapEntities.cs index d2bee2abc..41499da5f 100644 --- a/Game.Server/MapEntities.cs +++ b/Game.Server/MapEntities.cs @@ -32,9 +32,6 @@ public interface IMapEntityFilter BaseEntity? CreateNextEntity(ReadOnlySpan className); } -[LinkEntityToClass("point_template")] -public class PointTemplate : BaseEntity { } // TODO move this - [InlineArray(EntityMapData.MAPKEY_MAXLENGTH)] public struct InlineArrayMapKeyMaxLength { T first; } public static class MapEntities { @@ -183,7 +180,7 @@ public static void MapEntity_ParseAllEntities(ReadOnlyMemory mapData, IMap int numTemplates = pointTemplate.GetNumTemplateEntities(); for (int templateNm = 0; templateNm < numTemplates; templateNm++) { // Find it in the spawn list - BaseEntity entity = pointTemplate.GetTemplateEntity(templateNm); + BaseEntity? entity = pointTemplate.GetTemplateEntity(templateNm); for (int iEntNum = 0; iEntNum < numEntities; iEntNum++) { if (spawnList[iEntNum].Entity == entity) { // Give the point_template the mapdata @@ -209,6 +206,21 @@ public static void MapEntity_ParseAllEntities(ReadOnlyMemory mapData, IMap SpawnHierarchicalList(numEntities, spawnList, activateEntities); } + public static void MapEntity_PrecacheEntity(ReadOnlyMemory entData, int entDataSize) { + EntityMapData mapData = new(entData, entDataSize); + Span className = stackalloc byte[EntityMapData.MAPKEY_MAXLENGTH]; + + if (!mapData.ExtractValue("classname"u8, className)) + Error("classname missing from entity!\n"); + + BaseEntity? entity = CreateEntityByName(TokenString(className)); + if (entity != null) { + entity.ParseMapData(mapData); + entity.Precache(); + Util.RemoveImmediate(entity); + } + } + public static void FreeContainingEntity(Edict? ed) { if (ed != null) { BaseEntity? ent = BaseEntity.GetContainingEntity(ed); @@ -325,7 +337,7 @@ static void SetupParentsForSpawnList(int entities, HierarchicalSpawn_t[] spawnLi } } - static void SpawnHierarchicalList(int entities, HierarchicalSpawn_t[] spawnList, bool activateEntities) { + internal static void SpawnHierarchicalList(int entities, HierarchicalSpawn_t[] spawnList, bool activateEntities) { // Compute the hierarchical depth of all entities hierarchically attached ComputeSpawnHierarchyDepth(entities, spawnList); diff --git a/Game.Server/PointTemplate.cs b/Game.Server/PointTemplate.cs new file mode 100644 index 000000000..0653f581b --- /dev/null +++ b/Game.Server/PointTemplate.cs @@ -0,0 +1,253 @@ +using Game.Shared; + +using Source; +using Source.Common; +using Source.Common.Mathematics; + +using System.Numerics; +using System.Text; + +namespace Game.Server; + +using DEFINE = Source.DEFINE; + +public class PointTemplatePrecacher() : AutoGameSystem("CPointTemplatePrecacher") +{ + public static readonly PointTemplatePrecacher g_PointTemplatePrecacher = new(); + + readonly List> Ents = []; + + public void AddToPrecache(PointTemplate ent) { + Handle handle = new(); + handle.Set(ent); + Ents.Add(handle); + } + + public override void LevelInitPreEntity() => Ents.Clear(); + + public override void Shutdown() => Ents.Clear(); + + public void Precache() { + int c = Ents.Count; + for (int i = 0; i < c; ++i) { + PointTemplate? ent = Ents[i].Get(); + ent?.PerformPrecache(); + } + + Ents.Clear(); + } + + public static void PrecachePointTemplates() => g_PointTemplatePrecacher.Precache(); +} + +[LinkEntityToClass("point_template")] +public class PointTemplate : LogicalEntity +{ + public const int MAX_NUM_TEMPLATES = 16; + + const int SF_POINTTEMPLATE_DONTREMOVETEMPLATEENTITIES = 0x0001; + const int SF_POINTTEMPLATE_PRESERVE_NAMES = 0x0002; + + public struct Template + { + public int TemplateIndex; + public Matrix4x4 EntityToTemplate; + } + + string? TemplateEntityName01, TemplateEntityName02, TemplateEntityName03, TemplateEntityName04, TemplateEntityName05, TemplateEntityName06, TemplateEntityName07, TemplateEntityName08, + TemplateEntityName09, TemplateEntityName10, TemplateEntityName11, TemplateEntityName12, TemplateEntityName13, TemplateEntityName14, TemplateEntityName15, TemplateEntityName16; + + readonly List TemplateEntities = []; + readonly List