zz
This commit is contained in:
34
GlobalTorch/Util/MTObservableCollection.cs
Normal file
34
GlobalTorch/Util/MTObservableCollection.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Collections.Specialized;
|
||||
using System.Windows.Threading;
|
||||
|
||||
namespace Global.Util
|
||||
{
|
||||
public class MtObservableCollection<T> : ObservableCollection<T>
|
||||
{
|
||||
public override event NotifyCollectionChangedEventHandler CollectionChanged;
|
||||
|
||||
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
var collectionChanged = CollectionChanged;
|
||||
if (collectionChanged == null) return;
|
||||
foreach (var @delegate in collectionChanged.GetInvocationList())
|
||||
{
|
||||
var nh = (NotifyCollectionChangedEventHandler)@delegate;
|
||||
var dispObj = nh.Target as DispatcherObject;
|
||||
var dispatcher = dispObj?.Dispatcher;
|
||||
if (dispatcher != null && !dispatcher.CheckAccess())
|
||||
{
|
||||
dispatcher.BeginInvoke(
|
||||
(Action)(() => nh.Invoke(this,
|
||||
new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))),
|
||||
DispatcherPriority.DataBind);
|
||||
continue;
|
||||
}
|
||||
|
||||
nh.Invoke(this, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
60
GlobalTorch/Util/SEUtils.cs
Normal file
60
GlobalTorch/Util/SEUtils.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
using Sandbox.Game.World;
|
||||
|
||||
namespace Global.Util
|
||||
{
|
||||
public static class SEUtils
|
||||
{
|
||||
public static MyIdentity TryGetIdentity(string playerNameOrSteamId)
|
||||
{
|
||||
var player = MySession.Static.Players.GetPlayerByName(playerNameOrSteamId);
|
||||
if (player != null) return player.Identity;
|
||||
player = MySession.Static.Players.GetPlayerById(new MyPlayer.PlayerId(ulong.Parse(playerNameOrSteamId)));
|
||||
if (player != null) return player.Identity;
|
||||
if (MySession.Static.Players.TryGetPlayerBySteamId(ulong.Parse(playerNameOrSteamId), out player))
|
||||
return player.Identity;
|
||||
|
||||
foreach (var identity in MySession.Static.Players.GetAllIdentities())
|
||||
{
|
||||
if (identity.DisplayName == playerNameOrSteamId) return identity;
|
||||
|
||||
if (!ulong.TryParse(playerNameOrSteamId, out var steamId)) continue;
|
||||
|
||||
var id = MySession.Static.Players.TryGetSteamId(identity.IdentityId);
|
||||
if (id == steamId) return identity;
|
||||
if (identity.IdentityId == (long)steamId) return identity;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static MyIdentity GetIdentityByNameOrId(string playerNameOrSteamId)
|
||||
{
|
||||
var player = MySession.Static.Players.GetPlayerByName(playerNameOrSteamId);
|
||||
if (player != null) return player.Identity;
|
||||
player = MySession.Static.Players.GetPlayerById(new MyPlayer.PlayerId(ulong.Parse(playerNameOrSteamId)));
|
||||
if (player != null) return player.Identity;
|
||||
|
||||
foreach (var identity in MySession.Static.Players.GetAllIdentities())
|
||||
{
|
||||
if (identity.DisplayName == playerNameOrSteamId) return identity;
|
||||
|
||||
if (!ulong.TryParse(playerNameOrSteamId, out var steamId)) continue;
|
||||
|
||||
var id = MySession.Static.Players.TryGetSteamId(identity.IdentityId);
|
||||
|
||||
if (id == steamId) return identity;
|
||||
if (identity.IdentityId == (long)steamId) return identity;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetPlayerName(ulong steamId)
|
||||
{
|
||||
var id = GetIdentityByNameOrId(steamId.ToString());
|
||||
if (id != null && id.DisplayName != null) return id.DisplayName;
|
||||
|
||||
return steamId.ToString();
|
||||
}
|
||||
}
|
||||
}
|
36
GlobalTorch/Util/SendBuffer.cs
Normal file
36
GlobalTorch/Util/SendBuffer.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using VRage.Library.Collections;
|
||||
|
||||
namespace Global.Util
|
||||
{
|
||||
internal class SendBuffer : IBitSerializable
|
||||
{
|
||||
public int NumElements;
|
||||
public long SenderUserId;
|
||||
public byte[] VoiceDataBuffer;
|
||||
|
||||
public bool Serialize(BitStream stream, bool validate, bool acceptAndSetValue = true)
|
||||
{
|
||||
if (stream.Reading)
|
||||
{
|
||||
SenderUserId = stream.ReadInt64();
|
||||
NumElements = stream.ReadInt32();
|
||||
ArrayExtensions.EnsureCapacity(ref VoiceDataBuffer, NumElements);
|
||||
stream.ReadBytes(VoiceDataBuffer, 0, NumElements);
|
||||
}
|
||||
else
|
||||
{
|
||||
stream.WriteInt64(SenderUserId);
|
||||
stream.WriteInt32(NumElements);
|
||||
stream.WriteBytes(VoiceDataBuffer, 0, NumElements);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static implicit operator BitReaderWriter(SendBuffer buffer)
|
||||
{
|
||||
return new BitReaderWriter(buffer);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user