Skip to content

Commit

Permalink
Fix hypothetical bug where bootstrapper loaded while loading loader.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed May 16, 2020
1 parent ad32175 commit 6999b8c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Source/Reloaded.Mod.Launcher/Reloaded.Mod.Launcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<UseWPF>true</UseWPF>
<AssemblyName>Reloaded-II</AssemblyName>
<RootNamespace>Reloaded.Mod.Launcher</RootNamespace>
<Version>1.5.0</Version>
<Version>1.5.1</Version>
<Copyright>Sewer56 ~ $([System.DateTime]::UtcNow.Year) | Build: $([System.DateTime]::UtcNow.ToString("s"))</Copyright>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<ApplicationIcon>appicon.ico</ApplicationIcon>
Expand Down
6 changes: 5 additions & 1 deletion Source/Reloaded.Mod.Launcher/Utility/ApplicationInjector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ bool WhileCondition()
if (_process.HasExited)
return 0;

return Client.GetPort((int)_process.Id);
var port = Client.GetPort((int)_process.Id);
if (port == 0)
throw new Exception("Reloaded is still loading.");

return port;
}, WhileCondition, _xamlModLoaderSetupTimeout.Get(), _xamlModLoaderSetupSleepTime.Get());
}

Expand Down
1 change: 1 addition & 0 deletions Source/Reloaded.Mod.Loader.Server/Client.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ private void ReceiveException(ref NetMessage<GenericExceptionResponse> message)
/// Attempts to acquire the port to connect to a remote process with a specific id.
/// </summary>
/// <exception cref="FileNotFoundException"><see cref="MemoryMappedFile"/> was not created by the mod loader, in other words, mod loader is not loaded.</exception>
/// <returns>0 if Reloaded is still initializing, exception if not initialized, else a valid port.</returns>
public static int GetPort(int pid)
{
var mappedFile = MemoryMappedFile.OpenExisting(ServerUtility.GetMappedFileNameForPid(pid));
Expand Down
13 changes: 9 additions & 4 deletions Source/Reloaded.Mod.Loader/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,21 @@ private static void SetupLoader()
/// </summary>
public static int Initialize(IntPtr unusedPtr, int unusedSize)
{
// Setup Loader
SetupLoader();

// Write port as a Memory Mapped File, to allow Mod Loader's Launcher to discover the mod port.
// (And to stop bootstrapper from loading loader again).
int pid = Process.GetCurrentProcess().Id;
_memoryMappedFile = MemoryMappedFile.CreateOrOpen(ServerUtility.GetMappedFileNameForPid(pid), sizeof(int));
var view = _memoryMappedFile.CreateViewStream();
var binaryWriter = new BinaryWriter(view);
binaryWriter.Write(_server.Port);
binaryWriter.Write((int)0);

// Setup Loader
SetupLoader();

// Only write port on completed initialization.
// If port is 0, assume in loading state
binaryWriter.Seek(-sizeof(int), SeekOrigin.Current);
binaryWriter.Write(_server.Port);
return _server?.Port ?? 0;
}

Expand Down

0 comments on commit 6999b8c

Please sign in to comment.