-
Notifications
You must be signed in to change notification settings - Fork 379
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
base: master
Are you sure you want to change the base?
Changes from 4 commits
57578e1
16a8a69
c72f281
27a0ee3
b4cfb00
6b98dbd
2af7e31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
|
@@ -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; | ||
|
|
@@ -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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably want to add a fallback to |
||
| 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; | ||
|
|
||





There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.