Skip to content

Commit

Permalink
Synchronise geo rocks and persistent items
Browse files Browse the repository at this point in the history
  • Loading branch information
Extremelyd1 committed Nov 24, 2023
1 parent 88ad3e1 commit 6894cd0
Show file tree
Hide file tree
Showing 6 changed files with 19,682 additions and 1,333 deletions.
8 changes: 8 additions & 0 deletions HKMP/Collection/BiLookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ public bool ContainsSecond(TSecond index) {
return _inverse.ContainsKey(index);
}

/// <summary>
/// Removes all values from the BiLookup.
/// </summary>
public void Clear() {
_normal.Clear();
_inverse.Clear();
}

/// <inheritdoc/>
public IEnumerator<KeyValuePair<TFirst, TSecond>> GetEnumerator() {
return _normal.GetEnumerator();
Expand Down
36 changes: 36 additions & 0 deletions HKMP/Game/Client/Save/PersistentFsmData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using HutongGames.PlayMaker;

namespace Hkmp.Game.Client.Save;

/// <summary>
/// Data class for a persistent item in the scene with the corresponding FsmInt/FsmBool.
/// </summary>
internal class PersistentFsmData {
/// <summary>
/// The persistent item data with the ID and scene name.
/// </summary>
public PersistentItemData PersistentItemData { get; set; }

/// <summary>
/// The FSM variable for an integer. Could be null if a boolean is used instead.
/// </summary>
public FsmInt FsmInt { get; set; }
/// <summary>
/// The FSM variable for a boolean. Could be null if an integer is used instead.
/// </summary>
public FsmBool FsmBool { get; set; }

/// <summary>
/// The last value for the integer if used.
/// </summary>
public int LastIntValue { get; set; }
/// <summary>
/// The last value for the boolean if used.
/// </summary>
public bool LastBoolValue { get; set; }

/// <summary>
/// Whether an int is stored for this data.
/// </summary>
public bool IsInt => FsmInt != null;
}
67 changes: 67 additions & 0 deletions HKMP/Game/Client/Save/PersistentItemData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;

namespace Hkmp.Game.Client.Save;

/// <summary>
/// Data class to identify a persistent item.
/// </summary>
internal class PersistentItemData : IEquatable<PersistentItemData> {
/// <summary>
/// The ID of the item.
/// </summary>
public string Id { get; init; }
/// <summary>
/// The name of the scene of the item.
/// </summary>
public string SceneName { get; init; }

/// <inheritdoc />
public bool Equals(PersistentItemData other) {
if (ReferenceEquals(null, other)) {
return false;
}

if (ReferenceEquals(this, other)) {
return true;
}

return Id == other.Id && SceneName == other.SceneName;
}

/// <inheritdoc />
public override bool Equals(object obj) {
if (ReferenceEquals(null, obj)) {
return false;
}

if (ReferenceEquals(this, obj)) {
return true;
}

if (obj.GetType() != GetType()) {
return false;
}

return Equals((PersistentItemData) obj);
}

/// <inheritdoc />
public override int GetHashCode() {
unchecked {
return ((Id != null ? Id.GetHashCode() : 0) * 397) ^ (SceneName != null ? SceneName.GetHashCode() : 0);
}
}

public static bool operator ==(PersistentItemData left, PersistentItemData right) {
return Equals(left, right);
}

public static bool operator !=(PersistentItemData left, PersistentItemData right) {
return !Equals(left, right);
}

/// <inheritdoc />
public override string ToString() {
return $"({Id}, {SceneName})";
}
}
133 changes: 133 additions & 0 deletions HKMP/Game/Client/Save/SaveData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System.Collections.Generic;
using System.Linq;
using Hkmp.Collection;
using Hkmp.Logging;
using Newtonsoft.Json;

namespace Hkmp.Game.Client.Save;

/// <summary>
/// Serializable data class that stores mappings for what scene data should be synchronised and their indices used for networking.
/// </summary>
internal class SaveData {
/// <summary>
/// Dictionary mapping player data values to booleans indicating whether they should be synchronised.
/// </summary>
[JsonProperty("playerData")]
public Dictionary<string, bool> PlayerDataBools { get; private set; }

/// <summary>
/// Bi-directional lookup that maps save data names and their indices.
/// </summary>
[JsonIgnore]
public BiLookup<string, ushort> PlayerDataIndices { get; private set; }

/// <summary>
/// Deserialized key-value pairs for the geo rock data in the JSON.
/// </summary>
#pragma warning disable 0649
[JsonProperty("geoRocks")]
private readonly List<KeyValuePair<PersistentItemData, bool>> _geoRockDataValues;
#pragma warning restore 0649

/// <summary>
/// Dictionary mapping geo rock data values to booleans indicating whether they should be synchronised.
/// </summary>
[JsonIgnore]
public Dictionary<PersistentItemData, bool> GeoRockDataBools { get; private set; }

/// <summary>
/// Bi-directional lookup that maps geo rock names and their indices.
/// </summary>
[JsonIgnore]
public BiLookup<PersistentItemData, ushort> GeoRockDataIndices { get; private set; }

/// <summary>
/// Deserialized key-value pairs for the persistent bool data in the JSON.
/// </summary>
#pragma warning disable 0649
[JsonProperty("persistentBoolItems")]
private readonly List<KeyValuePair<PersistentItemData, bool>> _persistentBoolsDataValues;
#pragma warning restore 0649

/// <summary>
/// Dictionary mapping persistent bool data values to booleans indicating whether they should be synchronised.
/// </summary>
[JsonIgnore]
public Dictionary<PersistentItemData, bool> PersistentBoolDataBools { get; private set; }

/// <summary>
/// Bi-directional lookup that maps persistent bool names and their indices.
/// </summary>
[JsonIgnore]
public BiLookup<PersistentItemData, ushort> PersistentBoolDataIndices { get; private set; }

/// <summary>
/// Deserialized key-value pairs for the persistent int data in the JSON.
/// </summary>
#pragma warning disable 0649
[JsonProperty("persistentIntItems")]
private readonly List<KeyValuePair<PersistentItemData, bool>> _persistentIntDataValues;
#pragma warning restore 0649

/// <summary>
/// Dictionary mapping persistent int data values to booleans indicating whether they should be synchronised.
/// </summary>
[JsonIgnore]
public Dictionary<PersistentItemData, bool> PersistentIntDataBools { get; private set; }

/// <summary>
/// Bi-directional lookup that maps persistent int names and their indices.
/// </summary>
[JsonIgnore]
public BiLookup<PersistentItemData, ushort> PersistentIntDataIndices { get; private set; }

/// <summary>
/// Initializes the class by converting the deserialized data fields into the various dictionaries and lookups.
/// </summary>
public void Initialize() {
if (PlayerDataBools == null) {
Logger.Warn("Player data bools for save data is null");
return;
}

if (_geoRockDataValues == null) {
Logger.Warn("Geo rock data values for save data is null");
return;
}

if (_persistentBoolsDataValues == null) {
Logger.Warn("Persistent bools data values for save data is null");
return;
}

if (_persistentIntDataValues == null) {
Logger.Warn("Persistent int data values for save data is null");
return;
}

PlayerDataIndices = new BiLookup<string, ushort>();
ushort index = 0;
foreach (var playerDataBool in PlayerDataBools.Keys) {
PlayerDataIndices.Add(playerDataBool, index++);
}

GeoRockDataBools = _geoRockDataValues.ToDictionary(kv => kv.Key, kv => kv.Value);
GeoRockDataIndices = new BiLookup<PersistentItemData, ushort>();
foreach (var geoRockData in GeoRockDataBools.Keys) {
GeoRockDataIndices.Add(geoRockData, index++);
}

PersistentBoolDataBools = _persistentBoolsDataValues.ToDictionary(kv => kv.Key, kv => kv.Value);
PersistentBoolDataIndices = new BiLookup<PersistentItemData, ushort>();
foreach (var persistentBoolData in PersistentBoolDataBools.Keys) {
PersistentBoolDataIndices.Add(persistentBoolData, index++);
}

PersistentIntDataBools = _persistentIntDataValues.ToDictionary(kv => kv.Key, kv => kv.Value);
PersistentIntDataIndices = new BiLookup<PersistentItemData, ushort>();
foreach (var persistentIntData in PersistentIntDataBools.Keys) {
PersistentIntDataIndices.Add(persistentIntData, index++);
}
}
}
Loading

0 comments on commit 6894cd0

Please sign in to comment.