-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathServerInstance.cs
109 lines (96 loc) · 3.68 KB
/
ServerInstance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
#if DEBUG
using System.Threading;
#endif
using Uchu.Core;
namespace Uchu.Master
{
public class ServerInstance
{
public Process Process { get; private set; }
public Guid Id { get; }
public ServerType ServerType { get; set; }
public int ServerPort { get; set; }
public int ApiPort { get; set; }
public List<ZoneId> Zones { get; set; } = new List<ZoneId>();
public bool Ready { get; set; }
public ServerInstance(Guid id)
{
Id = id;
}
/// <summary>
/// Starts the server instance.
/// </summary>
/// <param name="location">Executable location to use.</param>
/// <param name="dotnet">Location of the dotnet command.</param>
public void Start(string location, string dotnet)
{
#if DEBUG
if (MasterServer.Config.DebugConfig.StartInstancesAsThreads)
this.StartInstanceAsThread();
else
#endif
this.StartInstanceAsProcess(location, dotnet);
}
#if DEBUG
public void StartInstanceAsThread()
{
new Thread(() =>
{
var program = new Uchu.Instance.Program();
program.Start(new[] { Id.ToString(), MasterServer.ConfigPath });
}).Start();
}
#endif
public void StartInstanceAsProcess(string location, string dotnet)
{
// Determine if the dotnet command should be used.
// If the default (dotnet) is used, the system PATH is checked.
var useDotNet = !string.IsNullOrWhiteSpace(dotnet);
if (useDotNet && dotnet?.ToLower() == "dotnet" && !File.Exists(dotnet))
{
var pathDirectories = (Environment.GetEnvironmentVariable("PATH") ?? "").Split(new[] { ';', ':' });
var dotNetInPath = pathDirectories.Any(pathDirectory => File.Exists(Path.Combine(pathDirectory, dotnet)));
if (!dotNetInPath)
{
useDotNet = false;
}
}
// Adjust the file name.
// If dotnet isn't used, the file name may need to be corrected to have .exe or no extension.
var file = useDotNet ? dotnet : location;
if (!useDotNet && file.ToLower().EndsWith(".dll"))
{
var parentDirectory = Path.GetDirectoryName(file) ?? "";
var baseFileName = Path.GetFileNameWithoutExtension(file);
var baseFileLocation = Path.Combine(parentDirectory, baseFileName);
if (File.Exists(baseFileLocation + ".exe"))
{
file = baseFileLocation + ".exe";
} else if (File.Exists(baseFileLocation))
{
file = baseFileLocation;
}
}
// Create and start the process.
this.Process = new Process
{
StartInfo =
{
FileName = file,
WorkingDirectory = useDotNet ? Path.GetDirectoryName(location) : Directory.GetCurrentDirectory(),
Arguments = (useDotNet ? $"{Path.GetFileName(location)} " : "") + $"{Id} \"{MasterServer.ConfigPath}\"",
RedirectStandardOutput = false,
UseShellExecute = true,
CreateNoWindow = false,
WindowStyle = ProcessWindowStyle.Normal,
}
};
this.Process.Start();
}
}
}