apieceoffruit

Observable

Jul 6th, 2022 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.67 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using AnomieUtilities;
  4. using Photon.Pun;
  5. using UnityEngine;
  6.  
  7. namespace JasonStorey
  8. {
  9.     public class PhotonSettingsObservable : MonoBehaviour, IPunObservable
  10.     {
  11.         [SerializeField]
  12.         GameSettings _settings;
  13.  
  14.         void SendModifiedSettings(PhotonStream stream, GameSettings settings)
  15.         {
  16.             var modified = settings.DirtyProperties.ToList();
  17.             stream.SendNext(modified.Count);
  18.  
  19.             foreach (var propertyInfo in modified)
  20.                 SendSettingFromPropertyInfo(stream, propertyInfo,settings);
  21.         }
  22.  
  23.         void ReceiveModifiedSettings(PhotonStream stream, GameSettings settings)
  24.         {
  25.             var count = (int)stream.ReceiveNext();
  26.             for (int i = 0; i < count; i++) ReceiveSettingsFromPropertyInfo(stream, settings);
  27.         }
  28.  
  29.         void SendSettingFromPropertyInfo(PhotonStream stream, MonitoredSettings.PropertyInfo prop,GameSettings settings)
  30.         {
  31.             switch (Type.GetTypeCode(prop.Type))
  32.             {
  33.                 case TypeCode.Int32:
  34.                 case TypeCode.Decimal:
  35.                 case TypeCode.Boolean:
  36.                 case TypeCode.Single:
  37.                     stream.SendNext(settings.GetValue(prop));
  38.                     break;
  39.             }
  40.            
  41.             if (prop.Type == typeof(Vector3))
  42.             {
  43.                 var v = (Vector3)settings.GetValue(prop);
  44.                 SendVector(stream,v);
  45.             }
  46.             else if (prop.Type == typeof(Color))
  47.             {
  48.                 var c = (Color)settings.GetValue(prop);
  49.                 SendColor(stream,c);
  50.             }
  51.             else if (prop.Type == typeof(Quaternion))
  52.             {
  53.                 var q = (Quaternion)settings.GetValue(prop);
  54.                 SendQuaternion(stream,q);
  55.             }
  56.            
  57.            
  58.  
  59.         }
  60.        
  61.         void ReceiveSettingsFromPropertyInfo(PhotonStream stream, GameSettings settings)
  62.         {
  63.             var modified = GetPropertyInfo(stream);
  64.             switch (Type.GetTypeCode(modified.Type))
  65.             {
  66.                 case TypeCode.Int32:
  67.                     settings.Set(modified,(int)stream.ReceiveNext());
  68.                     break;
  69.                 case TypeCode.Decimal:
  70.                     settings.Set(modified,(decimal)stream.ReceiveNext());
  71.                     break;
  72.                 case TypeCode.Boolean:
  73.                     settings.Set(modified,(bool)stream.ReceiveNext());
  74.                     break;
  75.                 case TypeCode.Single:
  76.                     settings.Set(modified,(float)stream.ReceiveNext());
  77.                     break;
  78.             }
  79.  
  80.             if (modified.Type == typeof(Vector3))
  81.             {
  82.                 var v3 = new Vector3((float)stream.ReceiveNext(), (float)stream.ReceiveNext(),
  83.                     (float)stream.ReceiveNext());
  84.                 settings.Set(modified,v3);
  85.             }
  86.             else if (modified.Type == typeof(Color))
  87.             {
  88.                 var col = new Color((float)stream.ReceiveNext(), (float)stream.ReceiveNext(),
  89.                     (float)stream.ReceiveNext());
  90.                 settings.Set(modified,col);
  91.             }
  92.             else if (modified.Type == typeof(Quaternion))
  93.             {
  94.                 var col = new Quaternion((float)stream.ReceiveNext(), (float)stream.ReceiveNext(),
  95.                     (float)stream.ReceiveNext(),(float)stream.ReceiveNext());
  96.                 settings.Set(modified,col);
  97.             }
  98.            
  99.            
  100.            
  101.         }
  102.  
  103.         #region Plumbing
  104.  
  105.         void SendColor(PhotonStream stream, Color color)
  106.         {
  107.             stream.SendNext(color.r);
  108.             stream.SendNext(color.g);
  109.             stream.SendNext(color.b);
  110.         }
  111.        
  112.         void SendQuaternion(PhotonStream stream, Quaternion q)
  113.         {
  114.             stream.SendNext(q.x);
  115.             stream.SendNext(q.y);
  116.             stream.SendNext(q.z);
  117.             stream.SendNext(q.w);
  118.         }
  119.        
  120.         void SendVector(PhotonStream stream, Vector3 v)
  121.         {
  122.             stream.SendNext(v.x);
  123.             stream.SendNext(v.y);
  124.             stream.SendNext(v.z);
  125.         }
  126.        
  127.         void OnEnable() => _settings.Changed += OnChanged;
  128.  
  129.         void OnDisable() => _settings.Changed -= OnChanged;
  130.  
  131.         void OnChanged() => HasUnsavedChanges = true;
  132.  
  133.         void SendPropertyInfo(PhotonStream stream,MonitoredSettings.PropertyInfo info,object val)
  134.         {
  135.             stream.SendNext(info.Key);
  136.             stream.SendNext(info.Type.FullName);
  137.             stream.SendNext(val);
  138.         }
  139.  
  140.         MonitoredSettings.PropertyInfo GetPropertyInfo(PhotonStream stream) =>
  141.             new MonitoredSettings.PropertyInfo
  142.             {
  143.                 Key = (string)stream.ReceiveNext(),
  144.                 Type = Type.GetType((string)stream.ReceiveNext())
  145.             };
  146.  
  147.         public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  148.         {
  149.             if (stream.IsWriting && HasUnsavedChanges)
  150.                 SendObjectValues(stream, info);
  151.             else if (stream.IsReading)
  152.                 ReceiveObjectValues(stream, info);
  153.         }
  154.  
  155.         void ReceiveObjectValues(PhotonStream stream, PhotonMessageInfo info) =>
  156.             ReceiveModifiedSettings(stream, _settings);
  157.  
  158.         void SendObjectValues(PhotonStream stream, PhotonMessageInfo info) =>
  159.             SendModifiedSettings(stream, _settings);
  160.  
  161.         void ChangesSaved()
  162.         {
  163.             HasUnsavedChanges = false;
  164.             _settings.Clean();
  165.         }
  166.  
  167.         bool HasUnsavedChanges;
  168.        
  169.  
  170.         #endregion
  171.     }
  172. }
  173.  
Add Comment
Please, Sign In to add comment