first commit

This commit is contained in:
2025-07-06 00:23:46 +02:00
commit 38f50c8819
1788 changed files with 112878 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
namespace NitroxClient.GameLogic.PlayerLogic.PlayerPreferences
{
public interface IPreferenceStateProvider
{
PlayerPreferenceState GetPreferenceState();
void SavePreferenceState(PlayerPreferenceState preferenceState);
}
}

View File

@@ -0,0 +1,100 @@
using System;
using UnityEngine;
namespace NitroxClient.GameLogic.PlayerLogic.PlayerPreferences
{
[Serializable]
public class PlayerPreference : IEquatable<PlayerPreference>
{
public string PlayerName { get; private set; }
public float RedAdditive { get; private set; }
public float GreenAdditive { get; private set; }
public float BlueAdditive { get; private set; }
public PlayerPreference()
{
}
public PlayerPreference(Color playerColor)
{
RedAdditive = playerColor.r;
GreenAdditive = playerColor.g;
BlueAdditive = playerColor.b;
}
public PlayerPreference(string playerName, Color playerColor)
{
PlayerName = playerName;
RedAdditive = playerColor.r;
GreenAdditive = playerColor.g;
BlueAdditive = playerColor.b;
}
public bool Equals(PlayerPreference other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return string.Equals(PlayerName, other.PlayerName) && RedAdditive.Equals(other.RedAdditive) && GreenAdditive.Equals(other.GreenAdditive) && BlueAdditive.Equals(other.BlueAdditive);
}
public PlayerPreference Clone()
{
return new PlayerPreference
{
PlayerName = PlayerName,
RedAdditive = RedAdditive,
GreenAdditive = GreenAdditive,
BlueAdditive = BlueAdditive
};
}
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((PlayerPreference)obj);
}
public override int GetHashCode()
{
unchecked
{
int hashCode = PlayerName != null ? PlayerName.GetHashCode() : 0;
hashCode = (hashCode * 397) ^ RedAdditive.GetHashCode();
hashCode = (hashCode * 397) ^ GreenAdditive.GetHashCode();
hashCode = (hashCode * 397) ^ BlueAdditive.GetHashCode();
return hashCode;
}
}
}
//LitJson does not seem to be capable of ignoring certain properties.
public static class PlayerPreferenceExtensions
{
public static Color PreferredColor(this PlayerPreference playerPreference)
{
return new Color(playerPreference.RedAdditive, playerPreference.GreenAdditive, playerPreference.BlueAdditive);
}
}
}

View File

@@ -0,0 +1,72 @@
using NitroxModel.Helper;
using NitroxModel.MultiplayerSession;
using NitroxModel_Subnautica.DataStructures;
using UnityEngine;
namespace NitroxClient.GameLogic.PlayerLogic.PlayerPreferences
{
public class PlayerPreferenceManager
{
private readonly PlayerPreferenceState state;
private readonly IPreferenceStateProvider stateProvider;
public PlayerPreferenceManager(IPreferenceStateProvider stateProvider)
{
this.stateProvider = stateProvider;
state = stateProvider.GetPreferenceState();
}
public void SetPreference(string ipAddress, PlayerPreference playerPreference)
{
Validate.NotNull(ipAddress);
Validate.NotNull(playerPreference);
if (state.Preferences.ContainsKey(ipAddress))
{
PlayerPreference currentPreference = state.Preferences[ipAddress];
if (currentPreference.Equals(playerPreference))
{
return;
}
state.Preferences[ipAddress] = playerPreference;
state.LastSetPlayerPreference = playerPreference;
return;
}
state.Preferences.Add(ipAddress, playerPreference);
state.LastSetPlayerPreference = playerPreference;
}
public PlayerPreference GetPreference(string ipAddress)
{
Validate.NotNull(ipAddress);
if (state.Preferences.TryGetValue(ipAddress, out PlayerPreference preference))
{
return preference.Clone();
}
if (state.LastSetPlayerPreference != null)
{
return state.LastSetPlayerPreference.Clone();
}
Color playerColor = RandomColorGenerator.GenerateColor().ToUnity();
PlayerPreference defaultPlayerPreference = new PlayerPreference(playerColor);
state.LastSetPlayerPreference = defaultPlayerPreference;
return defaultPlayerPreference;
}
public void Save()
{
stateProvider.SavePreferenceState(state);
}
}
}

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
namespace NitroxClient.GameLogic.PlayerLogic.PlayerPreferences
{
[Serializable]
public class PlayerPreferenceState
{
public PlayerPreference LastSetPlayerPreference;
public Dictionary<string, PlayerPreference> Preferences;
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using LitJson;
using UnityEngine;
namespace NitroxClient.GameLogic.PlayerLogic.PlayerPreferences
{
/// <summary>
/// This abstraction allows us to write tests against the preferences manager. Otherwise - we are unduly tied to Unity.
/// </summary>
public class UnityPreferenceStateProvider : IPreferenceStateProvider
{
private const string UNITY_PREF_KEY_NAME = "NITROX_PLAYER_PREFS";
public PlayerPreferenceState GetPreferenceState()
{
JsonMapper.RegisterImporter((double value) => Convert.ToSingle(value));
JsonMapper.RegisterExporter<float>((value, writer) => writer.Write(Convert.ToDouble(value)));
string playerPreferencesJson = PlayerPrefs.GetString(UNITY_PREF_KEY_NAME);
if (string.IsNullOrEmpty(playerPreferencesJson) || playerPreferencesJson == "{}")
{
return new PlayerPreferenceState
{
Preferences = new Dictionary<string, PlayerPreference>()
};
}
return JsonMapper.ToObject<PlayerPreferenceState>(playerPreferencesJson);
}
public void SavePreferenceState(PlayerPreferenceState preferenceState)
{
JsonMapper.RegisterImporter((double value) => Convert.ToSingle(value));
JsonMapper.RegisterExporter<float>((value, writer) => writer.Write(Convert.ToDouble(value)));
string playerPreferencesJson = JsonMapper.ToJson(preferenceState);
PlayerPrefs.SetString(UNITY_PREF_KEY_NAME, playerPreferencesJson);
}
}
}