-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ShoosGun/qsb-and-owo-integration
OWO Integration
- Loading branch information
Showing
7,595 changed files
with
58,336 additions
and
169,706 deletions.
The diff you're trying to view is too large. We only load the first 3000 changed files.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using SlateShipyard.ShipSpawner; | ||
using UnityEngine; | ||
|
||
namespace SlateShipyard.NetworkingInterface | ||
{ | ||
//TODO add docs to NetworkingInterface | ||
public abstract class NetworkingInterface | ||
{ | ||
public abstract void InvokeMethod(ObjectNetworkingInterface sender, string methodName, params object[] parameters); | ||
public abstract void SpawnRemoteShip(ShipData shipData, GameObject shipObject); | ||
} | ||
|
||
public class EmptyNetworkingInterface : NetworkingInterface | ||
{ | ||
public override void InvokeMethod(ObjectNetworkingInterface sender, string methodName, params object[] parameters) | ||
{ | ||
} | ||
public override void SpawnRemoteShip(ShipData shipData, GameObject shipObject) | ||
{ | ||
} | ||
} | ||
} |
252 changes: 252 additions & 0 deletions
252
CustomShipLib/NetworkingInterface/ObjectNetworkingInterface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,252 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Linq; | ||
|
||
using UnityEngine; | ||
using SlateShipyard.ShipSpawner; | ||
|
||
namespace SlateShipyard.NetworkingInterface | ||
{ | ||
//TODO add docs to ObjectNetworkingInterface | ||
public abstract class ObjectNetworkingInterface : MonoBehaviour | ||
{ | ||
protected Dictionary<string, SyncableField> FieldsToSync = new(); | ||
protected Dictionary<string, SyncableProperty> PropertiesToSync = new(); | ||
protected Dictionary<string, NetworkableMethod> NetworkableMethods = new(); | ||
|
||
public abstract bool IsPuppet { get; set; } | ||
public abstract string UniqueScriptID { get; } | ||
|
||
public ShipData shipData; | ||
|
||
public virtual void Awake() | ||
{ | ||
var fields = GetType().GetFields(); | ||
|
||
for(int i = 0; i < fields.Length; i++) | ||
{ | ||
FieldInfo field = fields[i]; | ||
var attrs = field.GetCustomAttributes(typeof(SyncableField), true); | ||
if (attrs.Length>0) | ||
{ | ||
SyncableField syncableField = (SyncableField)attrs[0]; | ||
if (string.IsNullOrEmpty(syncableField.SyncName)) | ||
{ | ||
syncableField.SyncName = field.Name; | ||
} | ||
syncableField.Field = field; | ||
syncableField.Type = field.FieldType; | ||
|
||
syncableField.Object = this; | ||
|
||
FieldsToSync.Add(syncableField.SyncName, syncableField); | ||
} | ||
} | ||
|
||
var properties = GetType().GetProperties(); | ||
|
||
for (int i = 0; i < properties.Length; i++) | ||
{ | ||
PropertyInfo property = properties[i]; | ||
var attrs = property.GetCustomAttributes(typeof(SyncableProperty), true); | ||
if (attrs.Length > 0) | ||
{ | ||
SyncableProperty syncableProperty = (SyncableProperty)attrs[0]; | ||
if (string.IsNullOrEmpty(syncableProperty.SyncName)) | ||
{ | ||
syncableProperty.SyncName = property.Name; | ||
} | ||
syncableProperty.Property = property; | ||
syncableProperty.Type = property.PropertyType; | ||
|
||
syncableProperty.Object = this; | ||
|
||
PropertiesToSync.Add(syncableProperty.SyncName, syncableProperty); | ||
} | ||
} | ||
|
||
var methods = GetType().GetMethods(); | ||
|
||
for (int i = 0; i < methods.Length; i++) | ||
{ | ||
MethodInfo method = methods[i]; | ||
var attrs = method.GetCustomAttributes(typeof(NetworkableMethod), true); | ||
if (attrs.Length > 0) | ||
{ | ||
NetworkableMethod networkableMethod = (NetworkableMethod)attrs[0]; | ||
if (string.IsNullOrEmpty(networkableMethod.NetworkName)) | ||
{ | ||
networkableMethod.NetworkName = method.Name; | ||
} | ||
networkableMethod.SetMethod(method); | ||
networkableMethod.Object = this; | ||
|
||
NetworkableMethods.Add(networkableMethod.NetworkName, networkableMethod); | ||
} | ||
} | ||
} | ||
|
||
public void SetValue(string memberName, object value) | ||
{ | ||
if (FieldsToSync.ContainsKey(memberName)) | ||
{ | ||
SyncableField field = FieldsToSync[memberName]; | ||
|
||
if (field.Type != value.GetType()) | ||
throw new Exception($"The field {memberName} is of type {field.Type}, not {value.GetType()}"); | ||
|
||
field.SetValue(value); | ||
} | ||
else if (PropertiesToSync.ContainsKey(memberName)) | ||
{ | ||
SyncableProperty property = PropertiesToSync[memberName]; | ||
|
||
if (property.Type != value.GetType()) | ||
throw new Exception($"The property {memberName} is of type {property.Type}, not {value.GetType()}"); | ||
|
||
property.SetValue(value); | ||
} | ||
} | ||
|
||
public object GetValue(string memberName) | ||
{ | ||
if (FieldsToSync.ContainsKey(memberName)) | ||
{ | ||
return FieldsToSync[memberName].GetValue(); | ||
} | ||
else if (PropertiesToSync.ContainsKey(memberName)) | ||
{ | ||
return PropertiesToSync[memberName].GetValue(); | ||
} | ||
|
||
throw new Exception($"This class doesn't have a syncable field called {memberName}"); | ||
} | ||
|
||
public void InvokeMethod(string methodName, object[] parameters, out bool hasReturnValue, out object returnValue) | ||
{ | ||
if (!FieldsToSync.ContainsKey(methodName)) | ||
throw new Exception($"This class doesn't have a networkable method called {methodName}"); | ||
|
||
NetworkableMethod method = NetworkableMethods[methodName]; | ||
|
||
if (!method.AreParametersAcceptable(parameters)) | ||
throw new Exception($"The types on the passed parameters don't match the method ({methodName}) parameter types"); | ||
|
||
method.InvokeMethod(parameters, out hasReturnValue, out returnValue); | ||
} | ||
|
||
public SyncableMember[] GetValues() | ||
{ | ||
//TODO make this better | ||
return ((SyncableMember[])FieldsToSync.Values.ToArray()).Concat(PropertiesToSync.Values.ToArray()).ToArray(); | ||
} | ||
} | ||
|
||
//TODO add docs to SyncableField | ||
public abstract class SyncableMember : Attribute | ||
{ | ||
public string SyncName; | ||
public Type Type; | ||
public object Object; | ||
public SyncableMember() | ||
{ | ||
} | ||
public SyncableMember(string syncName) | ||
{ | ||
SyncName = syncName; | ||
} | ||
public abstract object GetValue(); | ||
public abstract void SetValue(object value); | ||
} | ||
|
||
//TODO add docs to SyncableField | ||
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] | ||
public class SyncableField : SyncableMember | ||
{ | ||
public FieldInfo Field; | ||
public SyncableField() | ||
{ | ||
} | ||
public SyncableField(string syncName) | ||
{ | ||
SyncName = syncName; | ||
} | ||
public override object GetValue() | ||
{ | ||
return Field.GetValue(Object); | ||
} | ||
public override void SetValue(object value) | ||
{ | ||
Field.SetValue(Object, value); | ||
} | ||
} | ||
//TODO add docs to SyncableProperty | ||
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)] | ||
public class SyncableProperty : SyncableMember | ||
{ | ||
public PropertyInfo Property; | ||
public SyncableProperty() | ||
{ | ||
} | ||
public SyncableProperty(string syncName) | ||
{ | ||
SyncName = syncName; | ||
} | ||
public override object GetValue() | ||
{ | ||
return Property.GetValue(Object); | ||
} | ||
public override void SetValue(object value) | ||
{ | ||
Property.SetValue(Object, value); | ||
} | ||
} | ||
//TODO add docs to NetworkableMethod | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public class NetworkableMethod : Attribute | ||
{ | ||
public string NetworkName; | ||
public ParameterInfo[] ParameterTypes; | ||
public MethodInfo Method; | ||
public object Object; | ||
public NetworkableMethod() | ||
{ | ||
} | ||
public NetworkableMethod(string networkName) | ||
{ | ||
NetworkName = networkName; | ||
} | ||
public void SetMethod(MethodInfo method) | ||
{ | ||
Method = method; | ||
ParameterTypes = method.GetParameters(); | ||
} | ||
public bool AreParametersAcceptable(object[] parameters) | ||
{ | ||
bool isAcceptable = true; | ||
|
||
int i; | ||
for (i = 0; i < ParameterTypes.Length && i < parameters.Length; i++) | ||
{ | ||
if (ParameterTypes[i].ParameterType.IsEquivalentTo(parameters[i].GetType())) | ||
{ | ||
isAcceptable = false; | ||
break; | ||
} | ||
} | ||
if(i + 1 < ParameterTypes.Length && !ParameterTypes[i + 1].IsOptional) | ||
{ | ||
isAcceptable = false; | ||
} | ||
|
||
return isAcceptable; | ||
} | ||
public void InvokeMethod(object[] parameters, out bool hasReturnValue, out object returnValue) | ||
{ | ||
returnValue = Method.Invoke(Object, parameters); | ||
hasReturnValue = returnValue != null; | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
CustomShipLib/NetworkingInterface/SimpleNetworkingInterface.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using UnityEngine; | ||
|
||
namespace SlateShipyard.NetworkingInterface | ||
{ | ||
public class SimpleNetworkingInterface : ObjectNetworkingInterface | ||
{ | ||
protected bool isPuppet = false; | ||
public override bool IsPuppet | ||
{ | ||
get => isPuppet; | ||
set => OnIsPuppetChange(value); | ||
} | ||
public override string UniqueScriptID | ||
{ | ||
get => transform.name; | ||
} | ||
|
||
public virtual void OnIsPuppetChange(bool isPuppet) | ||
{ | ||
if(isPuppet != this.isPuppet) | ||
{ | ||
for (int i = 0; i < scriptsToDisableWhenPuppet.Length; i++) | ||
{ | ||
scriptsToDisableWhenPuppet[i].enabled = !isPuppet; | ||
} | ||
for (int i = 0; i < gameObjectsToDisableWhenPuppet.Length; i++) | ||
{ | ||
gameObjectsToDisableWhenPuppet[i].SetActive(!isPuppet); | ||
} | ||
Rigidbody r = GetComponent<Rigidbody>(); | ||
if (r != null && RigidbodyToKinematicWhenPuppet) | ||
{ | ||
r.isKinematic = isPuppet; | ||
} | ||
} | ||
|
||
this.isPuppet = isPuppet; | ||
} | ||
|
||
public GameObject[] gameObjectsToDisableWhenPuppet; | ||
public MonoBehaviour[] scriptsToDisableWhenPuppet; | ||
public bool RigidbodyToKinematicWhenPuppet = true; | ||
} | ||
} |
Oops, something went wrong.