Skip to content

Commit

Permalink
Fix exceptions in the Cities log (#311)
Browse files Browse the repository at this point in the history
- Use customized, stripped down version of protobuf-net which has no further assembly dependencies
- Only put correct dlls to output directory so we can use the whole directory for an installation
- Fix exception about our save file by adding an empty package pathpath
- Correctly cancel the rich presence thread
- Shutdown our own Steam integration when Cities shuts down to prevent crashes
- Check for the existence of the simulation manager when shutting down the game as client
- Simplify the call to OpenNAT
  • Loading branch information
kaenganxt committed Apr 11, 2023
1 parent 9f99f66 commit 29272d3
Show file tree
Hide file tree
Showing 13 changed files with 56 additions and 79 deletions.
10 changes: 0 additions & 10 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ jobs:
- name: Build Mod
run: powershell.exe -NoP -NonI -Command "../scripts/build.ps1 -Build"
working-directory: src

# Delete files only used for building
- name: Delete unused artifacts
working-directory: src/csm/bin/Release
run: |
del Assembly-CSharp.dll
del ColossalManaged.dll
del ICities.dll
del UnityEngine.dll
del UnityEngine.UI.dll

# Publish artifacts
- name: Upload mod DLLs
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,5 @@ __pycache__/


.vscode/
assemblies/
assemblies/*
!assemblies/protobuf-net.dll
Binary file added assemblies/protobuf-net.dll
Binary file not shown.
9 changes: 1 addition & 8 deletions scripts/build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,7 @@ If ($Install)

# Copy the required files
Write-Host "[CSM Install Script] Copying required files..."
Copy-Item -Path "$($OutputDirectory)$($Sep)LiteNetLib.dll" -Destination "$($ModDirectory)$($Sep)LiteNetLib.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)protobuf-net.dll" -Destination "$($ModDirectory)$($Sep)protobuf-net.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)CSM.dll" -Destination "$($ModDirectory)$($Sep)CSM.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)CSM.API.dll" -Destination "$($ModDirectory)$($Sep)CSM.API.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)CSM.BaseGame.dll" -Destination "$($ModDirectory)$($Sep)CSM.BaseGame.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)Open.Nat.dll" -Destination "$($ModDirectory)$($Sep)Open.Nat.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)System.Threading.dll" -Destination "$($ModDirectory)$($Sep)System.Threading.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)CitiesHarmony.API.dll" -Destination "$($ModDirectory)$($Sep)CitiesHarmony.API.dll" -Force
Copy-Item -Path "$($OutputDirectory)$($Sep)*.dll" -Destination "$($ModDirectory)" -Force

# Done
Write-Host "[CSM Install Script] Completed Copy"
Expand Down
9 changes: 1 addition & 8 deletions scripts/install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,6 @@ Remove-Item $ModDirectory -Recurse -ErrorAction Ignore
New-Item -ItemType directory -Path $ModDirectory | Out-Null

# Copy the items
Copy-Item -Path "LiteNetLib.dll" -Destination "$($ModDirectory)$($Sep)LiteNetLib.dll" -Force
Copy-Item -Path "protobuf-net.dll" -Destination "$($ModDirectory)$($Sep)protobuf-net.dll" -Force
Copy-Item -Path "CSM.dll" -Destination "$($ModDirectory)$($Sep)CSM.dll" -Force
Copy-Item -Path "CSM.API.dll" -Destination "$($ModDirectory)$($Sep)CSM.API.dll" -Force
Copy-Item -Path "CSM.BaseGame.dll" -Destination "$($ModDirectory)$($Sep)CSM.BaseGame.dll" -Force
Copy-Item -Path "Open.Nat.dll" -Destination "$($ModDirectory)$($Sep)Open.Nat.dll" -Force
Copy-Item -Path "System.Threading.dll" -Destination "$($ModDirectory)$($Sep)System.Threading.dll" -Force
Copy-Item -Path "CitiesHarmony.API.dll" -Destination "$($ModDirectory)$($Sep)CitiesHarmony.API.dll" -Force
Copy-Item -Path "*.dll" -Destination "$($ModDirectory)" -Force

Write-Host "Mod is now installed, open Cities Skylines and enable the mod."
14 changes: 4 additions & 10 deletions src/api/CSM.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Chat.cs" />
Expand All @@ -57,12 +50,13 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="protobuf-net">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\protobuf-net.dll</HintPath>
</Reference>
<PackageReference Include="LiteNetLib">
<Version>0.9.4</Version>
</PackageReference>
<PackageReference Include="protobuf-net">
<Version>2.4.7</Version>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
19 changes: 10 additions & 9 deletions src/basegame/CSM.BaseGame.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,34 @@
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ColossalManaged, Version=0.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\ColossalManaged.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ICities, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\ICities.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\UnityEngine.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="protobuf-net">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\protobuf-net.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="BaseGameConnection.cs" />
Expand Down Expand Up @@ -246,7 +247,7 @@
<Version>0.9.4</Version>
</PackageReference>
<PackageReference Include="CitiesHarmony.API">
<Version>2.1.0</Version>
<Version>2.2.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
25 changes: 11 additions & 14 deletions src/csm/CSM.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,33 @@
<Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ColossalManaged, Version=0.3.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\ColossalManaged.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="ICities, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\ICities.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\UnityEngine.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="UnityEngine.UI, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\UnityEngine.UI.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="protobuf-net">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\assemblies\protobuf-net.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -163,9 +165,7 @@
<Compile Include="Panels\JoinGamePanel.cs" />
<Compile Include="CSM.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util\CSMWebClient.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Util\CSMWebClient.cs" />
<Compile Include="Util\Serializer.cs" />
</ItemGroup>
<ItemGroup>
Expand All @@ -175,11 +175,8 @@
<PackageReference Include="Open.Nat">
<Version>2.1.0</Version>
</PackageReference>
<PackageReference Include="protobuf-net">
<Version>2.4.7</Version>
</PackageReference>
<PackageReference Include="CitiesHarmony.API">
<Version>2.1.0</Version>
<Version>2.2.0</Version>
</PackageReference>
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/csm/Helpers/SaveHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ public static void LoadLevel(byte[] world)

// Load the package
Package package = new Package(SYNC_NAME, world);
object memoryPackage = ReflectionHelper.GetAttr(package, "m_PackageImplementation");
// Set package path to empty string, otherwise Cities throws an exception sporadically
ReflectionHelper.SetAttr(memoryPackage, "m_PackagePath", "");

// Ensure that the LoadingManager is ready.
// Don't know if thats really necessary but doesn't hurt either. - root#0042
Expand Down
21 changes: 13 additions & 8 deletions src/csm/Helpers/SteamHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class SteamHelpers
private IntPtr _friendsPtr;
private Callback<GameRichPresenceJoinRequested_t> _gameRichPresenceJoinRequested;
private readonly Dictionary<string, string> _richPresenceValues = new Dictionary<string, string>();
private Thread _refreshThread;
private static bool _use64Bit = true;

public static bool Init()
Expand All @@ -46,15 +45,17 @@ public static bool Init()

Instance._gameRichPresenceJoinRequested = Callback<GameRichPresenceJoinRequested_t>.Create(OnGameRichPresenceJoinRequested);

Instance._refreshThread = new Thread(() =>
new Thread(() =>
{
while (true)
while (Instance._friendsPtr != IntPtr.Zero)
{
Instance.RefreshRichPresence();
Thread.Sleep(TimeSpan.FromMinutes(2));
}
});
Instance._refreshThread.Start();
}).Start();

// Detect when Cities shuts down the steam API so we don't act on freed memory
PlatformService.eventPlatformServiceShutdown += Instance.Shutdown;

return true;
}
Expand Down Expand Up @@ -135,9 +136,11 @@ private void JoinFromSteam(string token, bool checkLoadingManager)

public void Shutdown()
{
_gameRichPresenceJoinRequested.Unregister();
_refreshThread?.Abort();
// We can't shutdown the Steam API here as Cities might continue using it (also it crashes the game)
if (_friendsPtr != IntPtr.Zero)
{
_gameRichPresenceJoinRequested.Unregister();
_friendsPtr = IntPtr.Zero;
}
}

/// <summary>
Expand Down Expand Up @@ -195,6 +198,8 @@ private void SetRichPresence(string key, string value)
public void ClearRichPresence()
{
_richPresenceValues.Clear();
if (_friendsPtr == IntPtr.Zero) return;

SteamAPI_ISteamFriends_ClearRichPresence(_friendsPtr);
}

Expand Down
12 changes: 8 additions & 4 deletions src/csm/Networking/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -333,11 +333,15 @@ public void Disconnect(bool stopPeer = true)

if (needsUnload)
{
Singleton<SimulationManager>.instance.m_ThreadingWrapper.QueueMainThread(() =>
if (Singleton<SimulationManager>.exists &&
Singleton<SimulationManager>.instance.m_ThreadingWrapper != null)
{
// Go back to the main menu after disconnecting
Singleton<LoadingManager>.instance.UnloadLevel();
});
Singleton<SimulationManager>.instance.m_ThreadingWrapper.QueueMainThread(() =>
{
// Go back to the main menu after disconnecting
Singleton<LoadingManager>.instance.UnloadLevel();
});
}
}

if (downloading)
Expand Down
8 changes: 2 additions & 6 deletions src/csm/Networking/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
using CSM.Networking.Config;
using CSM.Util;
using LiteNetLib;
using Open.Nat;
using ColossalFramework;
using CSM.BaseGame.Injections.Tools;
using Open.Nat;
using CommandReceiver = CSM.Commands.CommandReceiver;

namespace CSM.Networking
Expand Down Expand Up @@ -120,12 +120,8 @@ public bool StartServer(ServerConfig serverConfig)
// Second strategy for NAT traversal: Upnp
try
{
// This async stuff is nasty, but we have to target .net 3.5 (unless cities skylines upgrades to something higher).
NatDiscoverer nat = new NatDiscoverer();
CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(5000);

nat.DiscoverDeviceAsync(PortMapper.Upnp, cts).ContinueWith(task => task.Result.CreatePortMapAsync(new Mapping(Protocol.Udp, Config.Port,
nat.DiscoverDeviceAsync().ContinueWith(task => task.Result.CreatePortMapAsync(new Mapping(Protocol.Udp, Config.Port,
Config.Port, "Cities Skylines Multiplayer (UDP)"))).Wait();
AutomaticSuccess = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/gs/CSM.GS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="protobuf-net" Version="2.4.6" />
<PackageReference Include="protobuf-net" Version="2.4.8" />
<PackageReference Include="LiteNetLib" Version="0.9.4" />
</ItemGroup>
</Project>

0 comments on commit 29272d3

Please sign in to comment.