Skip to content
Open
Changes from 2 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
47 changes: 45 additions & 2 deletions Assets/Scripts/Utility/AssetInjection/WorldDataReplacement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,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 +495,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 +504,49 @@ 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));
dfBlock = (DFBlock)SaveLoadManager.Deserialize(typeof(DFBlock), blockReplacementJson);
}
// Seek from mods
else if (ModManager.Instance != null && ModManager.Instance.TryGetAsset(fileName, false, out blockReplacementJsonAsset))
{
dfBlock = (DFBlock)SaveLoadManager.Deserialize(typeof(DFBlock), blockReplacementJsonAsset.text);
}

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

Choose a reason for hiding this comment

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

If this is gonna throw it should probably have its own exception so it can be caught by the caller. Lines 519 and 524 could also possibly throw invalid cast exceptions, whether or not that can actually happen in the FS library idk.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added WorldDataReplacementException and wrapped the (DFBlock)SaveLoadManager.Deserialize(...) calls in a try / catch (InvalidCastException) block.

}

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

// If jsonBlockIndex is invalid (less than or equal to blocksFile.BsaFile.Count), use fallback method
if (jsonBlockIndex <= blocksFile.BsaFile.Count)

Choose a reason for hiding this comment

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

blocksFile doesn't exist in the scope and should probably be:

// If jsonBlockIndex is invalid (less than or equal to nextBlockIndex), use fallback method
if (jsonBlockIndex <= nextBlockIndex)
{
    AssignNextIndex(blockName);
    return;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch and a good solution. Thanks, Z-Machine!

{
AssignNextIndex(blockName);
return;
}

// Add to cache
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.

Probably want to add a fallback to AssignNextIndex here too in case there are already entries for the keys jsonBlockIndex and blockName. This way two different modded RMBs with the same custom index (by accident) can still get along.

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

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