Skip to content

Commit eaa68a7

Browse files
committed
my player accessors; library path in csharp
1 parent 2233997 commit eaa68a7

File tree

5 files changed

+84
-45
lines changed

5 files changed

+84
-45
lines changed

csharp/bot/main.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Runtime.InteropServices;
5-
using System.IO;
64

75
namespace Unnatural
86
{
@@ -100,25 +98,7 @@ void Run()
10098

10199
static int Main(string[] args)
102100
{
103-
string root = Environment.GetEnvironmentVariable("UNNATURAL_ROOT");
104-
if (root == null)
105-
{
106-
string cwd = Directory.GetCurrentDirectory();
107-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Directory.GetFiles(cwd, Interop.LibName + ".dll").Length > 0)
108-
root = cwd;
109-
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Directory.GetFiles(cwd, "lib" + Interop.LibName + ".so").Length > 0)
110-
root = cwd;
111-
}
112-
if (root == null)
113-
{
114-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
115-
root = "C:/Program Files (x86)/Steam/steamapps/common/Unnatural Worlds/bin";
116-
else
117-
root = Environment.GetEnvironmentVariable("HOME") + "/.steam/steam/steamapps/common/Unnatural Worlds/bin";
118-
}
119-
Console.WriteLine("looking for uw library in: " + root);
120-
Directory.SetCurrentDirectory(root);
121-
101+
LibraryHelpers.SetCurrentDirectory();
122102
Bot bot = new Bot();
123103
bot.Run();
124104
return 0;

csharp/uwapi/events.cs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class ChatMessage
1818

1919
public static class Events
2020
{
21-
2221
public static event EventHandler<ConnectionStateEnum> ConnectionStateChanged;
2322
public static event EventHandler<GameStateEnum> GameStateChanged;
2423
public static event EventHandler<MapStateEnum> MapStateChanged;
@@ -89,7 +88,6 @@ static void ChatCallback(string msg, uint sender, ChatTargetFLags flags)
8988
ChatReceived(null, c);
9089
}
9190

92-
9391
static Events()
9492
{
9593
AppDomain.CurrentDomain.ProcessExit += Destructor;
@@ -118,17 +116,6 @@ static void Destructor(object sender, EventArgs e)
118116
}
119117
}
120118

121-
public static class InteropHelpers
122-
{
123-
public static uint[] Ids(Interop.UwIds ids)
124-
{
125-
uint[] tmp = new uint[ids.count];
126-
if (ids.count > 0)
127-
Marshal.Copy(ids.ids, (int[])(object)tmp, 0, (int)ids.count);
128-
return tmp;
129-
}
130-
}
131-
132119
public static class UwapiTasks
133120
{
134121
public static ulong InsertTask(Action a)

csharp/uwapi/library.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System.IO;
2+
using System;
3+
using System.Runtime.InteropServices;
4+
5+
namespace Unnatural
6+
{
7+
public static class InteropHelpers
8+
{
9+
public static uint[] Ids(Interop.UwIds ids)
10+
{
11+
uint[] tmp = new uint[ids.count];
12+
if (ids.count > 0)
13+
Marshal.Copy(ids.ids, (int[])(object)tmp, 0, (int)ids.count);
14+
return tmp;
15+
}
16+
}
17+
18+
public static class LibraryHelpers
19+
{
20+
public static string LibraryPath()
21+
{
22+
string root = Environment.GetEnvironmentVariable("UNNATURAL_ROOT");
23+
if (root == null)
24+
{
25+
string cwd = Directory.GetCurrentDirectory();
26+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Directory.GetFiles(cwd, Interop.LibName + ".dll").Length > 0)
27+
root = cwd;
28+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) && Directory.GetFiles(cwd, "lib" + Interop.LibName + ".so").Length > 0)
29+
root = cwd;
30+
}
31+
if (root == null)
32+
{
33+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
34+
root = "C:/Program Files (x86)/Steam/steamapps/common/Unnatural Worlds/bin";
35+
else
36+
root = Environment.GetEnvironmentVariable("HOME") + "/.steam/steam/steamapps/common/Unnatural Worlds/bin";
37+
}
38+
return root;
39+
}
40+
41+
public static void SetCurrentDirectory()
42+
{
43+
string root = LibraryPath();
44+
Console.WriteLine("looking for uw library in: " + root);
45+
Directory.SetCurrentDirectory(root);
46+
}
47+
}
48+
}

csharp/uwapi/world.cs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,24 @@ namespace Unnatural
1313

1414
public static class World
1515
{
16+
public static uint MyPlayerId()
17+
{
18+
return myPlayer.playerEntityId;
19+
}
20+
1621
public static uint MyForceId()
1722
{
18-
return myForceId;
23+
return myPlayer.forceEntityId;
24+
}
25+
26+
public static bool IsPrimaryController()
27+
{
28+
return myPlayer.primaryController;
29+
}
30+
31+
public static bool IsAdmin()
32+
{
33+
return myPlayer.admin;
1934
}
2035

2136
public static MyForceStatistics MyForceStatistics()
@@ -104,7 +119,7 @@ public static PolicyEnum Policy(uint force)
104119
return policies.TryGetValue(force, out val) ? val : PolicyEnum.None;
105120
}
106121

107-
static uint myForceId;
122+
static Interop.UwMyPlayer myPlayer;
108123
static MyForceStatistics myForceStatistics = new MyForceStatistics();
109124
static readonly Dictionary<uint, Entity> entities = new Dictionary<uint, Entity>();
110125
static readonly Dictionary<uint, PolicyEnum> policies = new Dictionary<uint, PolicyEnum>();
@@ -169,9 +184,9 @@ static void UpdatePolicies()
169184
if (!e.ForeignPolicy.HasValue)
170185
continue;
171186
ForeignPolicy fp = e.ForeignPolicy.Value;
172-
if (fp.forces[0] == myForceId)
187+
if (fp.forces[0] == myPlayer.forceEntityId)
173188
policies[fp.forces[1]] = fp.policy;
174-
if (fp.forces[1] == myForceId)
189+
if (fp.forces[1] == myPlayer.forceEntityId)
175190
policies[fp.forces[0]] = fp.policy;
176191
}
177192
}
@@ -193,11 +208,7 @@ static void UpdateOverview(bool stepping)
193208

194209
static void Updating(object sender, bool stepping)
195210
{
196-
{
197-
Interop.UwMyPlayer tmp = new Interop.UwMyPlayer();
198-
Interop.uwMyPlayer(ref tmp);
199-
myForceId = tmp.forceEntityId;
200-
}
211+
Interop.uwMyPlayer(ref myPlayer);
201212
Interop.uwMyForceStatistics(ref myForceStatistics);
202213
UpdateRemoved();
203214
UpdateFresh();

python/uwapi/world.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ def _make_empty_UwMyForceStatistics() -> UwMyForceStatistics:
88
return UwMyForceStatistics(0, 0, 0, 0, 0, 0)
99

1010

11+
def _make_empty_UwMyPlayer() -> UwMyPlayer:
12+
return UwMyPlayer(0, 0, False, False)
13+
14+
1115
class World:
1216
_instance = None
13-
_my_force_id: int = 0
17+
_my_player = _make_empty_UwMyPlayer()
1418
_my_force_statistics = _make_empty_UwMyForceStatistics()
1519
_entities: dict[int, Entity] = {}
1620
_policies: dict[int, UwForeignPolicyEnum] = {}
@@ -22,8 +26,17 @@ def __new__(cls):
2226
uw_events.on_update(cls._instance._update)
2327
return cls._instance
2428

29+
def my_player_id(self) -> int:
30+
return self._my_player.playerEntityId
31+
2532
def my_force_id(self) -> int:
26-
return self._my_force_id
33+
return self._my_player.forceEntityId
34+
35+
def is_primary_controller(self) -> bool:
36+
return self._my_player.primaryController
37+
38+
def is_admin(self) -> bool:
39+
return self._my_player.admin
2740

2841
def my_force_statistics(self) -> UwMyForceStatistics:
2942
return self._my_force_statistics

0 commit comments

Comments
 (0)