Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using ProtoBuf;
using VRage;
using VRageMath;

namespace CameraInfoApi.Data.Scripts.CameraInfoApi
{
[ProtoContract]
internal class CameraDataPacket
{
[ProtoMember(1)] public MatrixD Matrix;
[ProtoMember(2)] public float FieldOfView;
[ProtoMember(3)] public long GridId;

public MyTuple<MatrixD, float> Tuple => new MyTuple<MatrixD, float>(Matrix, FieldOfView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using CameraInfoApi.Data.Scripts.CameraInfoApi;
using Sandbox.ModAPI;
using System;
using VRage.Game.Components;
using VRage.Game.ModAPI;
using VRageMath;

namespace CameraInfoApi
{
[MySessionComponentDescriptor(MyUpdateOrder.AfterSimulation)]
internal class ClientMain : MySessionComponentBase
{
private IMyCubeGrid prevGrid = null;
private int _ticks = 0;
public override void UpdateAfterSimulation()
{
if (MyAPIGateway.Utilities.IsDedicated)
return;

if (_ticks++ % 10 != 0)
return;

var clientGrid = (MyAPIGateway.Session.Player?.Controller?.ControlledEntity as IMyShipController)?.CubeGrid;
if (clientGrid == null)
{
if (prevGrid != null)
MyAPIGateway.Multiplayer.SendMessageToServer(3621, MyAPIGateway.Utilities.SerializeToBinary(new CameraDataPacket()
{
Matrix = MatrixD.Identity,
FieldOfView = -1,
GridId = prevGrid.EntityId,
}));
prevGrid = null;
return;
}

prevGrid = clientGrid;
MyAPIGateway.Multiplayer.SendMessageToServer(3621, MyAPIGateway.Utilities.SerializeToBinary(new CameraDataPacket()
{
Matrix = MyAPIGateway.Session.Camera.ViewMatrix,
FieldOfView = MyAPIGateway.Session.Camera.FieldOfViewAngle,
GridId = prevGrid.EntityId,
}));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using CameraInfoApi.Data.Scripts.CameraInfoApi;
using Sandbox.Game.Entities;
using Sandbox.ModAPI;
using System;
using System.Collections.Generic;
using VRage;
using VRage.Game.Components;
using VRage.Game.ModAPI;
using VRageMath;

namespace CameraInfoApi
{
internal class ServerMain : MySessionComponentBase
{
public Dictionary<MyCubeGrid, MyTuple<MatrixD, float>> CameraInfos = new Dictionary<MyCubeGrid, MyTuple<MatrixD, float>>();

public override void LoadData()
{
if (!MyAPIGateway.Session.IsServer)
return;

MyAPIGateway.Multiplayer.RegisterSecureMessageHandler(3621, HandleMessage);
InitPbApi();
}

protected override void UnloadData()
{
if (!MyAPIGateway.Session.IsServer)
return;

MyAPIGateway.Multiplayer.UnregisterSecureMessageHandler(3621, HandleMessage);
}

private void HandleMessage(ushort handlerId, byte[] package, ulong senderSteamId, bool fromServer)
{
if (package == null || package.Length == 0)
return;
var message = MyAPIGateway.Utilities.SerializeFromBinary<CameraDataPacket>(package);
if (message == null)
return;

var grid = MyAPIGateway.Entities.GetEntityById(message.GridId) as MyCubeGrid;
if (grid == null)
return;

if (message.FieldOfView == -1)
{
CameraInfos.Remove(grid);
return;
}

CameraInfos[grid] = message.Tuple;
}

private void InitPbApi()
{
var property = MyAPIGateway.TerminalControls.CreateProperty<Func<MyTuple<MatrixD, float>?>, IMyProgrammableBlock>("CameraInfoApi");
property.Getter = b => () =>
{
MyTuple<MatrixD, float> info;
if (!CameraInfos.TryGetValue((MyCubeGrid) b.CubeGrid, out info))
return null;
return new MyTuple<MatrixD, float>?(info);
};
MyAPIGateway.TerminalControls.AddControl<IMyProgrammableBlock>(property);

MyAPIGateway.Entities.GetEntities(null, ent =>
{
var grid = ent as IMyCubeGrid;
if (grid == null)
return false;

// Workaround for scripts crashing when loading before the API is ready (i.e. on world load)
foreach (var pb in grid.GetFatBlocks<IMyProgrammableBlock>())
if (!pb.IsRunning && pb.ProgramData.Contains("CameraInfoApi"))
pb.Recompile();
return false;
});
}
}
}
36 changes: 36 additions & 0 deletions Utility Mods/CameraInfoApi/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CameraInfoApi")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("CameraInfoApi")]
[assembly: AssemblyCopyright("Copyright © 2023")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4b783ffe-e887-4220-8412-25caa84a7869")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]