Skip to content

Assign constant rather than dynamic blockIndices to modded RMBs #2718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 102 additions & 2 deletions Assets/Scripts/Utility/AssetInjection/WorldDataReplacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@

namespace DaggerfallWorkshop.Utility.AssetInjection
{
/// <summary>
/// Thrown when WorldDataReplacement can’t load or parse the expected JSON for a block.
/// </summary>
public class WorldDataReplacementException : System.Exception
{
public WorldDataReplacementException(string message) : base(message) { }
public WorldDataReplacementException(string message, System.Exception inner) : base(message, inner) { }
}

public struct BlockRecordKey
{
public int blockIndex;
Expand Down Expand Up @@ -486,7 +495,7 @@ private static bool AssignBlockIndices(ref DFLocation dfLocation)
// RMB blocks
foreach (string blockName in dfLocation.Exterior.ExteriorData.BlockNames)
if (blocksFile.GetBlockIndex(blockName) == -1)
AssignNextIndex(blockName);
AssignNewIndex(blockName);

// RDB blocks
if (dfLocation.Dungeon.Blocks != null)
Expand All @@ -495,7 +504,7 @@ private static bool AssignBlockIndices(ref DFLocation dfLocation)
{
string blockName = dungeonBlock.BlockName;
if (blocksFile.GetBlockIndex(blockName) == -1)
AssignNextIndex(blockName);
AssignNewIndex(blockName);
}
}
return true;
Expand All @@ -504,6 +513,97 @@ private static bool AssignBlockIndices(ref DFLocation dfLocation)
return false;
}

public static void AssignNewIndex(string blockName)
{
string fileName = GetDFBlockReplacementFilename(blockName, WorldDataVariants.NoVariant);

int jsonBlockIndex;
DFBlock? dfBlock = null; // Make blockData nullable
TextAsset blockReplacementJsonAsset;

// Seek from loose files
if (File.Exists(Path.Combine(worldDataPath, fileName)))
{
string blockReplacementJson = File.ReadAllText(Path.Combine(worldDataPath, fileName));
try
{
dfBlock = (DFBlock)SaveLoadManager.Deserialize(typeof(DFBlock), blockReplacementJson);
}
catch (System.InvalidCastException e)
{
Debug.LogError($"Invalid JSON format for block '{blockName}': {e.Message}");
throw new WorldDataReplacementException(
$"Block '{blockName}' JSON could not be cast to DFBlock.", e
);
}
}
// Seek from mods
else if (ModManager.Instance != null && ModManager.Instance.TryGetAsset(fileName, false, out blockReplacementJsonAsset))
{
try
{
dfBlock = (DFBlock)SaveLoadManager.Deserialize(typeof(DFBlock), blockReplacementJsonAsset.text);
}
catch (System.InvalidCastException e)
{
Debug.LogError($"Invalid JSON format for block '{blockName}': {e.Message}");
throw new WorldDataReplacementException(
$"Block '{blockName}' JSON could not be cast to DFBlock.", e
);
}
}

// Ensure blockData was successfully assigned
if (!dfBlock.HasValue)
{
Debug.LogError($"Failed to load block data for blockName: {blockName}");
throw new WorldDataReplacementException(
$"Block '{blockName}' does not have a valid Index in its JSON file."
);
}

// Grab the variant key
string blockKey = blockName + WorldDataVariants.NoVariant;

// Check for the "Index" field and assign its value
jsonBlockIndex = dfBlock.Value.Index;

// If jsonBlockIndex is invalid (less than or equal to nextBlockIndex), use fallback method
if (jsonBlockIndex <= nextBlockIndex)
{
int newIndex = nextBlockIndex;

AssignNextIndex(blockName);

if (!blocks.ContainsKey(blockKey))
{
// Update DFBlock index
DFBlock block = dfBlock.Value;
block.Index = newIndex;
blocks[blockKey] = block;
}

return;
}

// Add to cache, but if that jsonBlockIndex or blockName is already taken, fallback again
if (newBlockNames.ContainsKey(jsonBlockIndex) || newBlockIndices.ContainsKey(blockName))
{
AssignNextIndex(blockName);
}
else
{
newBlockNames[jsonBlockIndex] = blockName;
newBlockIndices[blockName] = jsonBlockIndex;
}

Copy link
Contributor

@TwelveEyes TwelveEyes Jun 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably better to handle this up above, since in the fallback the new index still has to be updated in the cached DFBlock:

diff --git a/Assets/Scripts/Utility/AssetInjection/WorldDataReplacement.cs b/Assets/Scripts/Utility/AssetInjection/WorldDataReplacement.cs
index 73875e6e4..18e033bcc 100644
--- a/Assets/Scripts/Utility/AssetInjection/WorldDataReplacement.cs
+++ b/Assets/Scripts/Utility/AssetInjection/WorldDataReplacement.cs
@@ -624,8 +624,8 @@ namespace DaggerfallWorkshop.Utility.AssetInjection
             // Check for the "Index" field and assign its value
             jsonBlockIndex = dfBlock.Value.Index;
 
-            // If jsonBlockIndex is invalid (less than or equal to nextBlockIndex), use fallback method
-            if (jsonBlockIndex <= nextBlockIndex)
+            // If jsonBlockIndex is invalid (less than or equal to nextBlockIndex) or index is already taken, use fallback method
+            if (jsonBlockIndex <= nextBlockIndex || newBlockNames.ContainsKey(jsonBlockIndex) || newBlockIndices.ContainsKey(blockName))
             {
                 int newIndex = nextBlockIndex;

Other than that, looks good!

Debug.LogFormat("Found a new DFBlock: {0}, (assigned index: {1})", blockName, jsonBlockIndex);

// Cache the full DFBlock
if (!blocks.ContainsKey(blockKey))
blocks[blockKey] = dfBlock.Value;
}

private static void AssignNextIndex(string blockName)
{
newBlockNames[nextBlockIndex] = blockName;
Expand Down