Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

Print PlayerConnection port in SpatialOS console #1128

Merged
merged 4 commits into from
Aug 21, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Added

- Workers will now log their `PlayerConnection` ports to SpatialOS after connecting. This port can be used for connecting the Unity profiler or a debugger to workers running in the cloud. [#1128](https://github.com/spatialos/gdk-for-unity/pull/1128)
- Note that this will only happen if the worker was built as a "Development Build".

## `0.2.7` - 2019-08-19

### Breaking Changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private static Dictionary<string, string> ParseCommandLineArgs(IList<string> arg
for (var i = 0; i < args.Count; i++)
{
var flag = args[i];
if (flag.StartsWith("+"))
if (flag.StartsWith("+") || flag.StartsWith("-"))
{
if (i + 1 >= args.Count)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Improbable.Worker.CInterop;
using Unity.Entities;
using UnityEditor;
using UnityEngine;

namespace Improbable.Gdk.Core
Expand Down Expand Up @@ -128,6 +131,18 @@ protected async Task Connect(IConnectionHandlerBuilder builder, ILogDispatcher l
WorkerConnectionSemaphore.Release();
}

#if !UNITY_EDITOR && DEVELOPMENT_BUILD && !UNITY_ANDROID && !UNITY_IPHONE
try
{
var port = GetPlayerConnectionPort();
Worker.SendLogMessage(LogLevel.Info, $"Unity player connection port: {port}.", Worker.WorkerId, null);
}
catch (Exception e)
{
logger.HandleLog(LogType.Exception, new LogEvent("Could not find player connection port.").WithException(e));
}
#endif

foreach (var callback in workerConnectedCallbacks)
{
callback(Worker);
Expand Down Expand Up @@ -237,5 +252,54 @@ private void RemoveFromPlayerLoop()
PlayerLoopUtils.RemoveFromPlayerLoop(Worker.World);
}
}

private ushort GetPlayerConnectionPort()
{
var companyName = Application.companyName;
var productName = Application.productName;

string logPath;

switch (Application.platform)
{
case RuntimePlatform.WindowsPlayer:
logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "AppData", "LocalLow", companyName, productName,
"Player.log");
break;
case RuntimePlatform.OSXPlayer:
// On MacOS it always goes in the Unity folder regardless of what companyName and productName are set to.
logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Library", "Logs", "Unity", "Player.log");
break;
case RuntimePlatform.LinuxPlayer:
logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".config", "unity3d", companyName, productName, "Player.log");
break;
default:
throw new InvalidOperationException($"Cannot find log file on platform: {Application.platform}");
}

CommandLineArgs.FromCommandLine().TryGetCommandLineValue("logfile", ref logPath);

logPath = Path.GetFullPath(logPath);

// We need to open the File as ReadWrite since this process _already_ has it open as ReadWrite.
// Attempting to open it as Read only results in IO exceptions due to permissions. Go figure.
using (var stream = new FileStream(logPath, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
using (var readStream = new StreamReader(stream))
{
var logContents = readStream.ReadToEnd();

const string portRegex =
"PlayerConnection initialized network socket : [0-9]\\.[0-9]\\.[0-9]\\.[0-9] ([0-9]*)";

var regex = new Regex(portRegex, RegexOptions.Compiled);

if (!regex.IsMatch(logContents))
{
throw new Exception("Could not find PlayerConnection port in logfile");
}

return ushort.Parse(regex.Match(logContents).Groups[1].Value);
}
}
}
}