-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerWorld.cs
102 lines (86 loc) · 3.12 KB
/
ServerWorld.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#nullable enable
using System;
using System.Collections.Generic;
using System.Linq;
using ProjectM;
using ProjectM.Network;
using ProjectM.UI;
using Unity.Collections;
using Unity.Entities;
using UnityEngine;
namespace VRisingServerApiPlugin;
public static class ServerWorld
{
private static readonly Lazy<World> _server = new(() =>
GetWorld("Server") ??
throw new Exception("There is no Server world (yet). This should not be installed on a client."));
private static readonly World Server = _server.Value;
public static EntityManager EntityManager = Server.EntityManager;
public static GameDataSystem GameDataSystem = Server.GetExistingSystem<GameDataSystem>();
public static bool IsServer => Application.productName == "VRisingServer";
private static World? GetWorld(string name)
{
foreach (var world in World.s_AllWorlds)
{
if (world.Name == name)
{
return world;
}
}
return null;
}
private static Player ConvertEntityToPlayer(Entity userEntity)
{
var user = EntityManager.GetComponentData<User>(userEntity);
PlayerCharacter? playerCharacter = EntityManager.HasComponent<PlayerCharacter>(user.LocalCharacter._Entity)
? EntityManager.GetComponentData<PlayerCharacter>(user.LocalCharacter._Entity)
: null;
return new Player(user,
userEntity,
playerCharacter,
user.LocalCharacter._Entity
);
}
public static IEnumerable<Player> GetAllPlayerCharacters()
{
return ListUtils.Convert(
EntityManager.CreateEntityQuery(ComponentType.ReadOnly<User>())
.ToEntityArray(Allocator.Temp)
)
.Where(userEntity => EntityManager.GetComponentData<User>(userEntity).LocalCharacter._Entity != Entity.Null)
.Select(ConvertEntityToPlayer)
.ToList();
}
public static Player? GetPlayer(int userIndex)
{
Entity? entity = ListUtils
.Convert(EntityManager.CreateEntityQuery(ComponentType.ReadOnly<User>())
.ToEntityArray(Allocator.Temp))
.FirstOrDefault(e => EntityManager.GetComponentData<User>(e).Index == userIndex);
return entity.HasValue
? ConvertEntityToPlayer(entity.Value)
: null;
}
public static IEnumerable<ClanTeam> GetAllClans()
{
return ListUtils.Convert(
EntityManager.CreateEntityQuery(ComponentType.ReadOnly<ClanTeam>())
.ToEntityArray(Allocator.Temp)
)
.Select(clanEntity => EntityManager.GetComponentData<ClanTeam>(clanEntity))
.ToList();
}
public static IEnumerable<Entity> GetAllClanEntities()
{
return ListUtils
.Convert(EntityManager.CreateEntityQuery(ComponentType.ReadOnly<ClanTeam>())
.ToEntityArray(Allocator.Temp))
.ToList();
}
public readonly record struct Player(
User User,
Entity UserEntity,
PlayerCharacter? Character,
Entity CharacterEntity
);
}