Skip to content

Commit

Permalink
Remove useless WaitForClosePortClose code. Fix ded-serv sleep disable…
Browse files Browse the repository at this point in the history
… function on windows.
  • Loading branch information
Chicken-Bones committed Sep 1, 2022
1 parent 0337e46 commit 9bc6484
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 51 deletions.
54 changes: 50 additions & 4 deletions patches/TerrariaNetCore/Terraria/Main.cs.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
--- src/Terraria/Terraria/Main.cs
+++ src/TerrariaNetCore/Terraria/Main.cs
@@ -114,7 +_,7 @@
Ready
}

-#if WINDOWS
+//#if WINDOWS
internal static class NativeMethods
{
public const uint ES_CONTINUOUS = 2147483648u;
@@ -123,7 +_,7 @@
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
}
-#endif
+//#endif

private struct MouseTextCache
{
@@ -501,11 +_,7 @@
public static float gFader;
public static byte gFadeDir = 1;
Expand All @@ -13,6 +31,18 @@
public static IMultiplayerClosePlayersOverlay ActiveClosePlayersTeamOverlay = new NewMultiplayerClosePlayersOverlay();
public static double UpdateTimeAccumulator;
public static bool drawSkip;
@@ -1964,9 +_,9 @@
public static string oldStatusText = "";
public static string autoGenFileLocation = null;
public static bool autoShutdown;
-#if WINDOWS
+//#if WINDOWS
private uint previousExecutionState;
-#endif
+//#endif
public static int sundialCooldown;
public static bool fastForwardTime;
private static Stopwatch splashTimer = new Stopwatch();
@@ -3250,6 +_,7 @@
PendingBorderlessState = screenBorderless;
screenBorderlessPendingResizes = (screenBorderless ? 6 : 0);
Expand All @@ -37,11 +67,27 @@
((Form)Control.FromHandle(base.Window.Handle)).WindowState = FormWindowState.Maximized;
#endif
}
@@ -4037,6 +_,7 @@
if (previousExecutionState != 0)
@@ -4027,16 +_,17 @@
}

public void NeverSleep() {
-#if WINDOWS
- previousExecutionState = NativeMethods.SetThreadExecutionState(2147483649u);
-#endif
+//#if WINDOWS
+ if (OperatingSystem.IsWindows())
+ previousExecutionState = NativeMethods.SetThreadExecutionState(2147483649u);
+//#endif
}

public void YouCanSleepNow() {
-#if WINDOWS
- if (previousExecutionState != 0)
+//#if WINDOWS
+ if (OperatingSystem.IsWindows() && previousExecutionState != 0)
NativeMethods.SetThreadExecutionState(previousExecutionState);
#endif
+ Netplay.WaitForClosePortClose();
-#endif
+//#endif
}

public void DedServ() {
Expand Down
38 changes: 1 addition & 37 deletions patches/TerrariaNetCore/Terraria/Netplay.cs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -70,30 +70,6 @@
}

private static void ServerFullWriteCallBack(object state) {
@@ -261,6 +_,7 @@
}
}

+ private static bool serverNeedsCleanUp = false;
private static void CleanupServer() {
StopListening();
try {
@@ -288,6 +_,7 @@

private static void ServerLoop() {
int num = 0;
+ serverNeedsCleanUp = true;
StartBroadCasting();
while (!Disconnect) {
StartListeningIfNeeded();
@@ -298,6 +_,7 @@

StopBroadCasting();
CleanupServer();
+ serverNeedsCleanUp = false;
}

private static void UpdateConnectedClients() {
@@ -399,6 +_,11 @@
flag = false;
}
Expand Down Expand Up @@ -157,7 +133,7 @@
int num = 0;
for (int i = 0; i < 255; i++) {
if (Main.player[i].active)
@@ -730,19 +_,30 @@
@@ -730,13 +_,18 @@
if (broadcastThread != null)
StopBroadCasting();

Expand All @@ -177,15 +153,3 @@
broadcastThread = null;
}

if (BroadcastClient != null) {
BroadcastClient.Close();
BroadcastClient = null;
+ }
+ }
+
+ public static void WaitForClosePortClose() {
+ while(!serverNeedsCleanUp) {
+ Thread.Sleep(10);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@
+ int num = path.LastIndexOf("\\", position - 1, StringComparison.Ordinal) + 1;
path = path.Remove(num, position - num + removeLength);
return Math.Max(num - 1, 1);
}
}
6 changes: 3 additions & 3 deletions patches/tModLoader/Terraria/Main.cs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -671,14 +671,14 @@
}

public void SetWorldName(string world) {
@@ -4032,6 +_,8 @@
@@ -4033,6 +_,8 @@
}

public void YouCanSleepNow() {
+ if (!dedServ && audioSystem == null) //supress extra exceptions from audio engine failing to load
+ return;
#if WINDOWS
if (previousExecutionState != 0)
//#if WINDOWS
if (OperatingSystem.IsWindows() && previousExecutionState != 0)
NativeMethods.SetThreadExecutionState(previousExecutionState);
@@ -4042,7 +_,7 @@
public void DedServ() {
Expand Down
12 changes: 6 additions & 6 deletions patches/tModLoader/Terraria/Netplay.cs.patch
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
int num = FindNextOpenClientSlot();
if (num != -1) {
Clients[num].Reset();
@@ -283,6 +_,8 @@
@@ -282,6 +_,8 @@
Main.netMode = 0;
Main.menuMode = 10;
WorldFile.SaveWorld();
Expand All @@ -33,7 +33,7 @@
Main.menuMode = 0;
}
else {
@@ -327,7 +_,7 @@
@@ -324,7 +_,7 @@
}

if (Clients[i].IsActive) {
Expand All @@ -42,7 +42,7 @@
Clients[i].PendingTerminationApproved = true;
continue;
}
@@ -421,6 +_,12 @@
@@ -418,6 +_,12 @@
}

private static void ClientLoopSetup(RemoteAddress address) {
Expand All @@ -55,7 +55,7 @@
Connection.ResetSpecialFlags();
ResetNetDiag();
Main.ServerSideCharacter = false;
@@ -437,11 +_,11 @@
@@ -434,11 +_,11 @@
Main.netMode = 1;
Main.menuMode = 14;
if (!Main.autoPass)
Expand All @@ -69,15 +69,15 @@
}

private static void InnerClientLoop() {
@@ -479,6 +_,7 @@
@@ -476,6 +_,7 @@
Main.cloudAlpha = Main.maxRaining;
Main.ToggleGameplayUpdates(state: false);
WorldGen.clearWorld();
+ SystemLoader.OnWorldLoad();
if (Main.mapEnabled)
Main.Map.Load();
}
@@ -530,6 +_,11 @@
@@ -527,6 +_,11 @@
}

if (!Main.gameMenu) {
Expand Down

0 comments on commit 9bc6484

Please sign in to comment.