Skip to content

Commit

Permalink
Merge pull request #2 from ShoosGun/qsb-and-owo-integration
Browse files Browse the repository at this point in the history
OWO Integration
  • Loading branch information
loco-choco authored Nov 23, 2022
2 parents 3199492 + 6b3423e commit 5221e9b
Show file tree
Hide file tree
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.
405 changes: 399 additions & 6 deletions .gitignore

Large diffs are not rendered by default.

Binary file modified CustomShipLib/AssetBundles/shipspawner
Binary file not shown.
18 changes: 15 additions & 3 deletions CustomShipLib/AssetBundles/shipspawner.manifest
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
ManifestFileVersion: 0
CRC: 784699139
CRC: 2615481730
Hashes:
AssetFileHash:
serializedVersion: 2
Hash: 8dff243e4605d09493afed4c20f560fd
Hash: 3e5d26ee224d6c920ffd2d6bcc53e55d
TypeTreeHash:
serializedVersion: 2
Hash: fc02bf6dedc4609611fd4aebc9b9806d
Hash: 31754debfd46453ed534505f965a4d95
HashAppended: 0
ClassTypes:
- Class: 1
Expand All @@ -29,8 +29,20 @@ ClassTypes:
Script: {instanceID: 0}
- Class: 65
Script: {instanceID: 0}
- Class: 114
Script: {fileID: 432413374, guid: 5695844481fda224a8297a115d141540, type: 3}
- Class: 114
Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3}
- Class: 114
Script: {fileID: 2083899121, guid: 5695844481fda224a8297a115d141540, type: 3}
- Class: 114
Script: {fileID: 412721156, guid: 5695844481fda224a8297a115d141540, type: 3}
- Class: 114
Script: {fileID: 1329524759, guid: 5695844481fda224a8297a115d141540, type: 3}
- Class: 114
Script: {fileID: -840784912, guid: 5695844481fda224a8297a115d141540, type: 3}
- Class: 114
Script: {fileID: 11500000, guid: 6e08224a0c172cf49b787f8bc8d72094, type: 3}
- Class: 115
Script: {instanceID: 0}
- Class: 128
Expand Down
4 changes: 4 additions & 0 deletions CustomShipLib/Modules/Wheels/OWSimpleRaycastWheel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class OWSimpleRaycastWheel : MonoBehaviour
public Rigidbody rb; //!< The Rigidbody the wheel will use to apply the forces it calculates.
public LayerMask collisionMask; //!< The LayerMask it will use on the raycast to be considered as valid ground.

public bool enablePhysics = true;
void Start()
{
minLenght = restLenght - springTravel;
Expand All @@ -49,6 +50,9 @@ void Start()

void FixedUpdate()
{
if (!enablePhysics)
return;

GetGround();
}

Expand Down
22 changes: 22 additions & 0 deletions CustomShipLib/NetworkingInterface/NetworkingInterface.cs
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 CustomShipLib/NetworkingInterface/ObjectNetworkingInterface.cs
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 CustomShipLib/NetworkingInterface/SimpleNetworkingInterface.cs
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;
}
}
Loading

0 comments on commit 5221e9b

Please sign in to comment.