zz
This commit is contained in:
85
GlobalShared/Patches/EntityNamePatch.cs
Normal file
85
GlobalShared/Patches/EntityNamePatch.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using HarmonyLib;
|
||||
using Sandbox.Game.Entities;
|
||||
using VRage.Game.Entity;
|
||||
|
||||
namespace Global.Shared.Patches
|
||||
{
|
||||
[HarmonyPatch(typeof(MyEntities))]
|
||||
public class EntityNamePatch
|
||||
{
|
||||
public static ConcurrentDictionary<long, string> EntityNameReverseLookup =
|
||||
new ConcurrentDictionary<long, string>();
|
||||
// reverse dictionary
|
||||
|
||||
[HarmonyPatch("SetEntityName")]
|
||||
[HarmonyPrefix]
|
||||
private static bool SetEntityNamePrefix(MyEntity myEntity, bool possibleRename)
|
||||
{
|
||||
if (string.IsNullOrEmpty(myEntity.Name))
|
||||
return false;
|
||||
string previousName = null;
|
||||
if (possibleRename)
|
||||
if (EntityNameReverseLookup.ContainsKey(myEntity.EntityId))
|
||||
{
|
||||
previousName = EntityNameReverseLookup[myEntity.EntityId];
|
||||
if (previousName != myEntity.Name) MyEntities.m_entityNameDictionary.Remove(previousName);
|
||||
}
|
||||
|
||||
if (MyEntities.m_entityNameDictionary.TryGetValue(myEntity.Name, out var myEntity1))
|
||||
{
|
||||
if (myEntity1 == myEntity)
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
MyEntities.m_entityNameDictionary[myEntity.Name] = myEntity;
|
||||
EntityNameReverseLookup[myEntity.EntityId] = myEntity.Name;
|
||||
}
|
||||
|
||||
// MyEntitiesButBetter.CallRename(myEntity, previousName, myEntity.Name);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[HarmonyPatch("RemoveName")]
|
||||
[HarmonyPrefix]
|
||||
private static bool RemoveNamePrefix(MyEntity entity)
|
||||
{
|
||||
if (string.IsNullOrEmpty(entity.Name))
|
||||
return false;
|
||||
MyEntities.m_entityNameDictionary.Remove(entity.Name);
|
||||
EntityNameReverseLookup.Remove(entity.EntityId);
|
||||
return false;
|
||||
}
|
||||
|
||||
[HarmonyPatch("IsNameExists")]
|
||||
[HarmonyPrefix]
|
||||
private static bool IsNameExistsPrefix(ref bool __result, MyEntity entity, string name)
|
||||
{
|
||||
if (string.IsNullOrEmpty(entity.Name))
|
||||
{
|
||||
__result = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (MyEntities.m_entityNameDictionary.ContainsKey(name))
|
||||
{
|
||||
var ent = MyEntities.m_entityNameDictionary[entity.Name];
|
||||
__result = ent != entity;
|
||||
return false;
|
||||
}
|
||||
|
||||
__result = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
[HarmonyPatch("UnloadData")]
|
||||
[HarmonyPostfix]
|
||||
private static void UnloadDataPostfix()
|
||||
{
|
||||
EntityNameReverseLookup.Clear();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user