Skip to content

Commit

Permalink
Fix rare save issue with shade position
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Nov 25, 2024
1 parent 1cdd467 commit 941e130
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 49 deletions.
6 changes: 3 additions & 3 deletions HKMP/Game/Client/Save/SaveManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private void OnUpdatePlayerData() {
private static byte[] EncodeValue(object value) {
// Since all strings in the save data are scene names (or map scene names), we can convert them to indices
byte[] EncodeString(string stringValue) {
if (!EncodeUtil.GetSceneIndex(stringValue, out var index)) {
if (!EncodeUtil.GetStringIndex(stringValue, out var index)) {
// Logger.Info($"Could not encode string value: {stringValue}");
// return Array.Empty<byte>();
throw new Exception($"Could not encode string value: {stringValue}");
Expand Down Expand Up @@ -812,7 +812,7 @@ private void UpdateSaveWithData(ushort index, byte[] encodedValue) {
for (var i = 0; i < length; i++) {
var sceneIndex = BitConverter.ToUInt16(encodedValue, 2 + i * 2);

if (!EncodeUtil.GetSceneName(sceneIndex, out var sceneName)) {
if (!EncodeUtil.GetStringName(sceneIndex, out var sceneName)) {
throw new Exception($"Could not decode string in list from save update: {sceneIndex}");
}

Expand Down Expand Up @@ -991,7 +991,7 @@ private void UpdateSaveWithData(ushort index, byte[] encodedValue) {
string DecodeString(byte[] encoded, int startIndex) {
var sceneIndex = BitConverter.ToUInt16(encoded, startIndex);

if (!EncodeUtil.GetSceneName(sceneIndex, out var value)) {
if (!EncodeUtil.GetStringName(sceneIndex, out var value)) {
throw new Exception($"Could not decode string from save update: {encodedValue}");
}

Expand Down
2 changes: 1 addition & 1 deletion HKMP/HKMP.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<EmbeddedResource Include="Resource\entity-registry.json" />
<EmbeddedResource Include="Resource\action-registry.json" />
<EmbeddedResource Include="Resource\save-data.json" />
<EmbeddedResource Include="Resource\scene-data.json" />
<EmbeddedResource Include="Resource\string-data.json" />
<EmbeddedResource Include="Resource\music-data.json" />
</ItemGroup>

Expand Down
78 changes: 53 additions & 25 deletions HKMP/Resource/scene-data.json → HKMP/Resource/string-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -554,34 +554,10 @@
"Intro_Cutscene",
"Dream_NailCollection",
"RestBench",
"TOWN",
"CLIFFS",
"CROSSROADS",
"BoneBench",
"SHAMAN_TEMPLE",
"FINAL_BOSS",
"GREEN_PATH",
"FOG_CANYON",
"QUEENS_STATION",
"WASTES",
"CITY",
"WATERWAYS",
"GODS_GLORY",
"RestBench (1)",
"DEEPNEST",
"RestBench Return",
"BEASTS_DEN",
"ABYSS",
"OUTSKIRTS",
"COLOSSEUM",
"HIVE",
"MINES",
"RESTING_GROUNDS",
"ROYAL_GARDENS",
"WhiteBench",
"WHITE_PALACE",
"TRAM_UPPER",
"TRAM_LOWER",
"Death Respawn Marker",
"Gruz Boss Scene",
"False Knight Boss Scene",
Expand Down Expand Up @@ -620,5 +596,57 @@
"Nosk Hornet Boss Scene",
"Radiance Boss Scene",
"Oblobbles Boss Scene",
"God Tamer Boss Scene"
"God Tamer Boss Scene",
"NONE",
"TEST_AREA",
"KINGS_PASS",
"CLIFFS",
"TOWN",
"CROSSROADS",
"GREEN_PATH",
"ROYAL_GARDENS",
"FOG_CANYON",
"WASTES",
"DEEPNEST",
"HIVE",
"BONE_FOREST",
"PALACE_GROUNDS",
"MINES",
"RESTING_GROUNDS",
"CITY",
"DREAM_WORLD",
"COLOSSEUM",
"ABYSS",
"ROYAL_QUARTER",
"WHITE_PALACE",
"SHAMAN_TEMPLE",
"WATERWAYS",
"QUEENS_STATION",
"OUTSKIRTS",
"KINGS_STATION",
"MAGE_TOWER",
"TRAM_UPPER",
"TRAM_LOWER",
"FINAL_BOSS",
"SOUL_SOCIETY",
"ACID_LAKE",
"NOEYES_TEMPLE",
"MONOMON_ARCHIVE",
"MANTIS_VILLAGE",
"RUINED_TRAMWAY",
"DISTANT_VILLAGE",
"ABYSS_DEEP",
"ISMAS_GROVE",
"WYRMSKIN",
"LURIENS_TOWER",
"LOVE_TOWER",
"GLADE",
"BLUE_LAKE",
"PEAK",
"JONI_GRAVE",
"OVERGROWN_MOUND",
"CRYSTAL_MOUND",
"BEASTS_DEN",
"GODS_GLORY",
"GODSEEKER_WASTE"
]
40 changes: 20 additions & 20 deletions HKMP/Util/EncodeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@ namespace Hkmp.Util;
/// </summary>
public static class EncodeUtil {
/// <summary>
/// The file path of the embedded resource file for scene data.
/// The file path of the embedded resource file for string data.
/// </summary>
private const string SceneDataFilePath = "Hkmp.Resource.scene-data.json";
private const string StringDataFilePath = "Hkmp.Resource.string-data.json";

/// <summary>
/// Bi-directional lookup that maps scene names to their indices.
/// Bi-directional lookup that maps strings (for encoding) to their indices.
/// </summary>
private static readonly BiLookup<string, ushort> SceneIndices;
private static readonly BiLookup<string, ushort> StringIndices;

/// <summary>
/// Static construct to load the scene indices.
/// </summary>
static EncodeUtil() {
SceneIndices = new BiLookup<string, ushort>();
StringIndices = new BiLookup<string, ushort>();

var sceneNames = FileUtil.LoadObjectFromEmbeddedJson<List<string>>(SceneDataFilePath);
var strings = FileUtil.LoadObjectFromEmbeddedJson<List<string>>(StringDataFilePath);
ushort index = 0;
foreach (var sceneName in sceneNames) {
SceneIndices.Add(sceneName, index++);
foreach (var str in strings) {
StringIndices.Add(str, index++);
}
}

Expand Down Expand Up @@ -61,22 +61,22 @@ public static bool[] GetBoolsFromByte(byte b) {
}

/// <summary>
/// Try to get the scene index corresponding to the given scene name for encoding/decoding purposes.
/// Try to get the string index corresponding to the given string for encoding/decoding purposes.
/// </summary>
/// <param name="sceneName">The name of the scene.</param>
/// <param name="index">The index of the scene or default if the scene name could not be found.</param>
/// <returns>true if there is a corresponding index for the given scene name, false otherwise.</returns>
public static bool GetSceneIndex(string sceneName, out ushort index) {
return SceneIndices.TryGetValue(sceneName, out index);
/// <param name="sceneName">The string.</param>
/// <param name="index">The index of the string or default if the string could not be found.</param>
/// <returns>true if there is a corresponding index for the given string, false otherwise.</returns>
public static bool GetStringIndex(string sceneName, out ushort index) {
return StringIndices.TryGetValue(sceneName, out index);
}

/// <summary>
/// Try to get the scene name corresponding to the given scene index for encoding/decoding purposes.
/// Try to get the string corresponding to the given string index for encoding/decoding purposes.
/// </summary>
/// <param name="index">The index of the scene.</param>
/// <param name="sceneName">The name of the scene or default if the scene index could not be found.</param>
/// <returns>true if there is a corresponding name for the given scene index, false otherwise.</returns>
public static bool GetSceneName(ushort index, out string sceneName) {
return SceneIndices.TryGetValue(index, out sceneName);
/// <param name="index">The string.</param>
/// <param name="sceneName">The string or default if the string index could not be found.</param>
/// <returns>true if there is a corresponding string for the given index, false otherwise.</returns>
public static bool GetStringName(ushort index, out string sceneName) {
return StringIndices.TryGetValue(index, out sceneName);
}
}

0 comments on commit 941e130

Please sign in to comment.