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 4 commits into
base: master
Choose a base branch
from
Open
Changes from all 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
59 changes: 57 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,61 @@ 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.");
}

// 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)
{
AssignNextIndex(blockName);

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

return;
}

// Add to cache
newBlockNames[jsonBlockIndex] = blockName;
newBlockIndices[blockName] = jsonBlockIndex;
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