Skip to content

Commit 2fb7fe6

Browse files
committed
fix: adapt c860bf7 from MAO for the base engine #2598
1 parent 8610675 commit 2fb7fe6

File tree

5 files changed

+130
-16
lines changed

5 files changed

+130
-16
lines changed

Framework/Intersect.Framework.Core/GameObjects/Maps/MapBase.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ public MapBase(MapBase mapBase) : base(mapBase?.Id ?? Guid.Empty)
149149

150150
foreach (var record in mapBase.LocalEvents)
151151
{
152-
var evt = new EventBase(record.Key, record.Value?.JsonData);
152+
var evt = new EventBase(record.Key, record.Value?.JsonData)
153+
{
154+
MapId = Id,
155+
};
153156
LocalEvents?.Add(record.Key, evt);
154157
}
155158

Intersect.Editor/Forms/DockingElements/frmMapEditor.cs

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,22 +1907,29 @@ public void ProcessSelectionMovement(MapInstance tmpMap, bool ignoreMouse = fals
19071907
if (Globals.SelectionType != (int)SelectionTypes.CurrentLayer ||
19081908
Globals.CurrentLayer == LayerOptions.Events)
19091909
{
1910-
if (Globals.SelectionSource.FindEventAt(x0 - dragxoffset, y0 - dragyoffset) != null)
1910+
var eventAtPosition = Globals.SelectionSource.FindEventAt(x0 - dragxoffset, y0 - dragyoffset);
1911+
if (eventAtPosition == null)
19111912
{
1912-
if (tmpMap.FindEventAt(x0, y0) != null)
1913-
{
1914-
tmpMap.LocalEvents.Remove(tmpMap.FindEventAt(x0, y0).Id);
1915-
}
1916-
1917-
eventCopy = new EventBase(Guid.NewGuid(), Globals.SelectionSource.FindEventAt(x0 - dragxoffset, y0 - dragyoffset)
1918-
)
1919-
{
1920-
SpawnX = x0,
1921-
SpawnY = y0
1922-
};
1913+
continue;
1914+
}
19231915

1924-
tmpMap.LocalEvents.Add(eventCopy.Id, eventCopy);
1916+
var eventOnTemporaryMap = tmpMap.FindEventAt(x0, y0);
1917+
if (eventOnTemporaryMap != null)
1918+
{
1919+
tmpMap.LocalEvents.Remove(eventOnTemporaryMap.Id);
19251920
}
1921+
1922+
eventCopy = new EventBase(
1923+
Guid.NewGuid(),
1924+
eventAtPosition
1925+
)
1926+
{
1927+
MapId = tmpMap.Id,
1928+
SpawnX = x0,
1929+
SpawnY = y0,
1930+
};
1931+
1932+
tmpMap.LocalEvents.Add(eventCopy.Id, eventCopy);
19261933
}
19271934
}
19281935
}

Intersect.Editor/Maps/MapInstance.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public void UpdateAdjacentAutotiles()
299299
}
300300
}
301301

302-
public EventBase FindEventAt(int x, int y)
302+
public EventBase? FindEventAt(int x, int y)
303303
{
304304
if (LocalEvents.Count <= 0)
305305
{

Intersect.Server.Core/Database/DbInterface.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ internal static bool InitDatabase(IServerContext serverContext)
406406
Console.WriteLine("Loading game data...");
407407

408408
LoadAllGameObjects();
409+
410+
ValidateMapEvents();
411+
409412
LoadTime();
410413
OnClassesLoaded();
411414
OnMapsLoaded();
@@ -872,6 +875,107 @@ private static void LoadGameObjects(GameObjectType gameObjectType)
872875
throw;
873876
}
874877
}
878+
879+
private static void ValidateMapEvents()
880+
{
881+
var missingEvents = 0;
882+
var correctedEvents = 0;
883+
884+
foreach (var (mapId, databaseObject) in MapController.Lookup)
885+
{
886+
if (databaseObject is not MapBase mapDescriptor)
887+
{
888+
ApplicationContext.CurrentContext.Logger.LogError(
889+
"Found an invalid database object in the MapDescriptor lookup ({InvalidObjectType}, {InvalidObjectId}, '{InvalidObjectName}'",
890+
databaseObject.GetType().GetName(qualified: true),
891+
databaseObject.Id,
892+
databaseObject.Name
893+
);
894+
continue;
895+
}
896+
897+
var actualMapId = mapDescriptor.Id;
898+
if (mapId != actualMapId)
899+
{
900+
ApplicationContext.CurrentContext.Logger.LogError(
901+
"Map with ID {ActualMapId} was recorded in the lookup under the ID {ExpectedMapId}, this needs to be investigated",
902+
actualMapId,
903+
mapId
904+
);
905+
}
906+
907+
foreach (var eventId in mapDescriptor.EventIds)
908+
{
909+
if (!EventBase.TryGet(eventId, out var eventDescriptor))
910+
{
911+
ApplicationContext.CurrentContext.Logger.LogWarning(
912+
"Map {MapId} references missing event {EventId}, unexpected behavior may occur",
913+
mapId,
914+
eventId
915+
);
916+
++missingEvents;
917+
continue;
918+
}
919+
920+
// Ignore common events
921+
if (eventDescriptor is not { CommonEvent: false })
922+
{
923+
continue;
924+
}
925+
926+
// The map ID is correct, no validation necessary
927+
var referencedMapId = eventDescriptor.MapId;
928+
if (referencedMapId == mapId)
929+
{
930+
continue;
931+
}
932+
933+
// If the event is 1) not common and 2) is not using this map's ID, fix it. It was copied wrong in the editor
934+
string referencedMapName = $"deleted map {referencedMapId}";
935+
if (MapController.TryGet(referencedMapId, out var referencedMapDescriptor))
936+
{
937+
referencedMapName = referencedMapDescriptor.Name;
938+
}
939+
940+
if (string.IsNullOrWhiteSpace(referencedMapName))
941+
{
942+
referencedMapName = "(unnamed map)";
943+
}
944+
945+
eventDescriptor.MapId = mapId;
946+
++correctedEvents;
947+
948+
var eventName = eventDescriptor.Name?.Trim();
949+
if (string.IsNullOrWhiteSpace(eventName))
950+
{
951+
eventName = "(unnamed event)";
952+
}
953+
954+
var mapName = mapDescriptor.Name;
955+
if (string.IsNullOrWhiteSpace(mapName))
956+
{
957+
mapName = "(unnamed map)";
958+
}
959+
960+
961+
ApplicationContext.CurrentContext.Logger.LogWarning(
962+
"Event '{EventName}' ({EventId}) on the map '{MapName}'({MapId}) was pointing to '{ReferencedMapName}' ({ReferencedMapId}) and while this has been corrected, the correction will not be saved until the event is resaved",
963+
eventName,
964+
eventId,
965+
mapName,
966+
mapId,
967+
referencedMapName,
968+
referencedMapId
969+
);
970+
}
971+
}
972+
973+
ApplicationContext.CurrentContext.Logger.LogWarning(
974+
"Finished validating map events on all maps, there were {MissingEvents} missing events and {CorrectedEvents} corrected events",
975+
missingEvents,
976+
correctedEvents
977+
);
978+
}
875979

876980
public static IDatabaseObject AddGameObject(GameObjectType gameObjectType)
877981
{

Intersect.Server.Core/Maps/MapController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,7 @@ public void ClearAllInstances()
626626
mInstances.Clear();
627627
}
628628
#endregion
629-
629+
630630
/// <summary>
631631
/// Gets rid of any orphaned tile layers
632632
/// </summary>

0 commit comments

Comments
 (0)