Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using AnomieUtilities;
- using Photon.Pun;
- using UnityEngine;
- namespace JasonStorey
- {
- public class PhotonSettingsObservable : MonoBehaviour, IPunObservable
- {
- [SerializeField]
- GameSettings _settings;
- void SendModifiedSettings(PhotonStream stream, GameSettings settings)
- {
- var modified = settings.DirtyProperties.ToList();
- stream.SendNext(modified.Count);
- foreach (var propertyInfo in modified)
- SendSettingFromPropertyInfo(stream, propertyInfo,settings);
- }
- void ReceiveModifiedSettings(PhotonStream stream, GameSettings settings)
- {
- var count = (int)stream.ReceiveNext();
- for (int i = 0; i < count; i++) ReceiveSettingsFromPropertyInfo(stream, settings);
- }
- void SendSettingFromPropertyInfo(PhotonStream stream, MonitoredSettings.PropertyInfo prop,GameSettings settings)
- {
- switch (Type.GetTypeCode(prop.Type))
- {
- case TypeCode.Int32:
- case TypeCode.Decimal:
- case TypeCode.Boolean:
- case TypeCode.Single:
- stream.SendNext(settings.GetValue(prop));
- break;
- }
- if (prop.Type == typeof(Vector3))
- {
- var v = (Vector3)settings.GetValue(prop);
- SendVector(stream,v);
- }
- else if (prop.Type == typeof(Color))
- {
- var c = (Color)settings.GetValue(prop);
- SendColor(stream,c);
- }
- else if (prop.Type == typeof(Quaternion))
- {
- var q = (Quaternion)settings.GetValue(prop);
- SendQuaternion(stream,q);
- }
- }
- void ReceiveSettingsFromPropertyInfo(PhotonStream stream, GameSettings settings)
- {
- var modified = GetPropertyInfo(stream);
- switch (Type.GetTypeCode(modified.Type))
- {
- case TypeCode.Int32:
- settings.Set(modified,(int)stream.ReceiveNext());
- break;
- case TypeCode.Decimal:
- settings.Set(modified,(decimal)stream.ReceiveNext());
- break;
- case TypeCode.Boolean:
- settings.Set(modified,(bool)stream.ReceiveNext());
- break;
- case TypeCode.Single:
- settings.Set(modified,(float)stream.ReceiveNext());
- break;
- }
- if (modified.Type == typeof(Vector3))
- {
- var v3 = new Vector3((float)stream.ReceiveNext(), (float)stream.ReceiveNext(),
- (float)stream.ReceiveNext());
- settings.Set(modified,v3);
- }
- else if (modified.Type == typeof(Color))
- {
- var col = new Color((float)stream.ReceiveNext(), (float)stream.ReceiveNext(),
- (float)stream.ReceiveNext());
- settings.Set(modified,col);
- }
- else if (modified.Type == typeof(Quaternion))
- {
- var col = new Quaternion((float)stream.ReceiveNext(), (float)stream.ReceiveNext(),
- (float)stream.ReceiveNext(),(float)stream.ReceiveNext());
- settings.Set(modified,col);
- }
- }
- #region Plumbing
- void SendColor(PhotonStream stream, Color color)
- {
- stream.SendNext(color.r);
- stream.SendNext(color.g);
- stream.SendNext(color.b);
- }
- void SendQuaternion(PhotonStream stream, Quaternion q)
- {
- stream.SendNext(q.x);
- stream.SendNext(q.y);
- stream.SendNext(q.z);
- stream.SendNext(q.w);
- }
- void SendVector(PhotonStream stream, Vector3 v)
- {
- stream.SendNext(v.x);
- stream.SendNext(v.y);
- stream.SendNext(v.z);
- }
- void OnEnable() => _settings.Changed += OnChanged;
- void OnDisable() => _settings.Changed -= OnChanged;
- void OnChanged() => HasUnsavedChanges = true;
- void SendPropertyInfo(PhotonStream stream,MonitoredSettings.PropertyInfo info,object val)
- {
- stream.SendNext(info.Key);
- stream.SendNext(info.Type.FullName);
- stream.SendNext(val);
- }
- MonitoredSettings.PropertyInfo GetPropertyInfo(PhotonStream stream) =>
- new MonitoredSettings.PropertyInfo
- {
- Key = (string)stream.ReceiveNext(),
- Type = Type.GetType((string)stream.ReceiveNext())
- };
- public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
- {
- if (stream.IsWriting && HasUnsavedChanges)
- SendObjectValues(stream, info);
- else if (stream.IsReading)
- ReceiveObjectValues(stream, info);
- }
- void ReceiveObjectValues(PhotonStream stream, PhotonMessageInfo info) =>
- ReceiveModifiedSettings(stream, _settings);
- void SendObjectValues(PhotonStream stream, PhotonMessageInfo info) =>
- SendModifiedSettings(stream, _settings);
- void ChangesSaved()
- {
- HasUnsavedChanges = false;
- _settings.Clean();
- }
- bool HasUnsavedChanges;
- #endregion
- }
- }
Add Comment
Please, Sign In to add comment