Skip to content

Commit

Permalink
Replace singletons with dependency injection
Browse files Browse the repository at this point in the history
  • Loading branch information
godarklight committed May 4, 2017
1 parent 341718b commit 04b1548
Show file tree
Hide file tree
Showing 36 changed files with 1,412 additions and 1,337 deletions.
20 changes: 5 additions & 15 deletions Client/AdminSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ namespace DarkMultiPlayer
{
public class AdminSystem
{
private static AdminSystem singleton;
private List<string> serverAdmins = new List<string>();
private object adminLock = new object();
//Services
Settings dmpSettings;

public static AdminSystem fetch
public AdminSystem(Settings dmpSettings)
{
get
{
return singleton;
}
this.dmpSettings = dmpSettings;
}

public void HandleAdminMessage(byte[] messageData)
Expand Down Expand Up @@ -80,7 +78,7 @@ private void UnregisterServerAdmin(string adminName)
/// <returns><c>true</c> if the current player is admin; otherwise, <c>false</c>.</returns>
public bool IsAdmin()
{
return IsAdmin(Settings.fetch.playerName);
return IsAdmin(dmpSettings.playerName);
}

/// <summary>
Expand All @@ -95,14 +93,6 @@ public bool IsAdmin(string playerName)
return serverAdmins.Contains(playerName);
}
}

public static void Reset()
{
lock (Client.eventLock)
{
singleton = new AdminSystem();
}
}
}
}

53 changes: 24 additions & 29 deletions Client/AsteroidWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ namespace DarkMultiPlayer
{
public class AsteroidWorker
{
//singleton
private static AsteroidWorker singleton;
//How many asteroids to spawn into the server
public int maxNumberOfUntrackedAsteroids;
public bool workerEnabled;
Expand All @@ -18,13 +16,21 @@ public class AsteroidWorker
private List<string> serverAsteroids = new List<string>();
private Dictionary<string, string> serverAsteroidTrackStatus = new Dictionary<string, string>();
private object serverAsteroidListLock = new object();
//Services
private DMPGame dmpGame;
private LockSystem lockSystem;
private NetworkWorker networkWorker;
private VesselWorker vesselWorker;

public static AsteroidWorker fetch
public AsteroidWorker(DMPGame dmpGame, LockSystem lockSystem, NetworkWorker networkWorker, VesselWorker vesselWorker)
{
get
{
return singleton;
}
this.dmpGame = dmpGame;
this.lockSystem = lockSystem;
this.networkWorker = networkWorker;
this.vesselWorker = vesselWorker;
this.dmpGame.updateEvent.Add(Update);
GameEvents.onGameSceneLoadRequested.Add(OnGameSceneLoadRequested);
GameEvents.onVesselCreate.Add(OnVesselCreate);
}

private void Update()
Expand Down Expand Up @@ -55,13 +61,13 @@ private void Update()
{
lastAsteroidCheck = Client.realtimeSinceStartup;
//Try to acquire the asteroid-spawning lock if nobody else has it.
if (!LockSystem.fetch.LockExists("asteroid-spawning"))
if (!lockSystem.LockExists("asteroid-spawning"))
{
LockSystem.fetch.AcquireLock("asteroid-spawning", false);
lockSystem.AcquireLock("asteroid-spawning", false);
}

//We have the spawn lock, lets do stuff.
if (LockSystem.fetch.LockIsOurs("asteroid-spawning"))
if (lockSystem.LockIsOurs("asteroid-spawning"))
{
if ((HighLogic.CurrentGame.flightState.protoVessels != null) && (FlightGlobals.fetch.vessels != null))
{
Expand Down Expand Up @@ -94,7 +100,7 @@ private void Update()
ProtoVessel pv = asteroid.BackupVessel();
DarkLog.Debug("Sending changed asteroid, new state: " + asteroid.DiscoveryInfo.trackingStatus.Value + "!");
serverAsteroidTrackStatus[asteroid.id.ToString()] = asteroid.DiscoveryInfo.trackingStatus.Value;
NetworkWorker.fetch.SendVesselProtoMessage(pv, false, false);
networkWorker.SendVesselProtoMessage(pv, false, false);
}
}
}
Expand All @@ -112,16 +118,16 @@ private void OnVesselCreate(Vessel checkVessel)
{
lock (serverAsteroidListLock)
{
if (LockSystem.fetch.LockIsOurs("asteroid-spawning"))
if (lockSystem.LockIsOurs("asteroid-spawning"))
{
if (!serverAsteroids.Contains(checkVessel.id.ToString()))
{
if (GetAsteroidCount() <= maxNumberOfUntrackedAsteroids)
{
DarkLog.Debug("Spawned in new server asteroid!");
serverAsteroids.Add(checkVessel.id.ToString());
VesselWorker.fetch.RegisterServerVessel(checkVessel.id);
NetworkWorker.fetch.SendVesselProtoMessage(checkVessel.protoVessel, false, false);
vesselWorker.RegisterServerVessel(checkVessel.id);
networkWorker.SendVesselProtoMessage(checkVessel.protoVessel, false, false);
}
else
{
Expand Down Expand Up @@ -250,22 +256,11 @@ private void OnGameSceneLoadRequested(GameScenes scene)
scenarioController = null;
}

public static void Reset()
public void Stop()
{
lock (Client.eventLock)
{
if (singleton != null)
{
singleton.workerEnabled = false;
Client.updateEvent.Remove(singleton.Update);
GameEvents.onGameSceneLoadRequested.Remove(singleton.OnGameSceneLoadRequested);
GameEvents.onVesselCreate.Remove(singleton.OnVesselCreate);
}
singleton = new AsteroidWorker();
Client.updateEvent.Add(singleton.Update);
GameEvents.onGameSceneLoadRequested.Add(singleton.OnGameSceneLoadRequested);
GameEvents.onVesselCreate.Add(singleton.OnVesselCreate);
}
dmpGame.updateEvent.Remove(Update);
GameEvents.onGameSceneLoadRequested.Remove(OnGameSceneLoadRequested);
GameEvents.onVesselCreate.Remove(OnVesselCreate);
}
}
}
Expand Down

0 comments on commit 04b1548

Please sign in to comment.