Skip to content

Commit 5735a04

Browse files
authored
Cleaned up build scripts and used AssemblyResolve event instead of copying all dlls (SubnauticaNitrox#1098)
* Cleaned up NitroxModel and NitroxServer to use as little of UnityEngine as possible for now * Used Nitrox.Bootstrapper to launch Nitrox at runtime through reflection * Fixed the surrogates again after moving them to NitroxModel-Subnautica Added shared project for copying the resources to the launcher output * Allowed game to start when server is running (again) by loading assemblies as byte arrays * Fixed wrong output copy for launcher * Small cleanup of server related code * Fixed assembly load from bytes loading the same dll multiple times * Fixed log format exception * Reverted LiteNetLib to upstream 0.8 * Removed .csproj framework version property from Nitrox.Bootloader because it's in Directory.Build.props * Removed unused imports in NitroxPatcher.Main * Fixed build for test project by copying all references over from game on build * Removed all UnityEngine references from NitroxModel * Removed all references to UnityEngine from NitroxServer * Removed default value for prop in NitroxModel.csproj * Reverted changes to NitroxModel namespaces because it's now UnityEngine reference free * Reverted namespace usages in NitroxServer since it's now UnityEngine type free * Added protomember attributes to NitroxColor members * Small unrelated performance fix in BaseDebugger * Used an environment variable to set the Nitrox launcher path instead of relying on appdata file * Disabled loading of Nitrox if QModManager's assembly is loaded too * Forgot the use the variable after refactor to Bootloader.Main * Added AliveOrNull before Vehicles.GetColors calls * Removed dead code * Removed blank line in TextureScaler.cs * Added seamoth colors for VehicleHelper * Removed newline in RandomColorGenerator * Reverted autoformat change in SubnauticaAutoFacRegistrar * Removed template comment in Nitrox.Bootloader.csproj * Removed whitespace * Removed x86 build support * Changed Nitrox.Bootloader error check to method and fixed return type * Cleaned up RetryWait code * Added error log to exception for launcherpath.txt delete call * Removed newline in BatchEntitySpawner * Fixed RetryWait * Formatted QuaternionSurrogate * Removed using rename for TechType in PDAStateData * Renamed NitroxModel's TechType to NitroxTechType
1 parent 92c53a3 commit 5735a04

183 files changed

Lines changed: 1266 additions & 2173 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Directory.Build.props

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<!-- Set default properties for all projects (can be overridden per project) -->
4+
<PropertyGroup>
5+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
6+
<TestLibrary>false</TestLibrary>
7+
</PropertyGroup>
8+
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
9+
<DefineConstants>TRACE;DEBUG</DefineConstants>
10+
<OutputPath>bin\Debug\</OutputPath>
11+
</PropertyGroup>
12+
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
13+
<DefineConstants>TRACE;RELEASE</DefineConstants>
14+
<OutputPath>bin\Release\</OutputPath>
15+
</PropertyGroup>
16+
17+
<PropertyGroup Condition="$([System.Text.RegularExpressions.Regex]::IsMatch($(MSBuildProjectName), '^Nitrox.*$'))">
18+
<NitroxLibrary>true</NitroxLibrary>
19+
</PropertyGroup>
20+
<PropertyGroup Condition="'$(NitroxLibrary)' == 'true' and '$(MSBuildProjectName)' != 'NitroxModel' and '$(MSBuildProjectName)' != 'NitroxServer' and '$(MSBuildProjectName)' != 'Nitrox.Bootloader'">
21+
<UnityModLibrary>true</UnityModLibrary>
22+
</PropertyGroup>
23+
<PropertyGroup Condition="'$(MSBuildProjectName)' == 'NitroxTest'">
24+
<TestLibrary>true</TestLibrary>
25+
</PropertyGroup>
26+
27+
<ItemGroup>
28+
<!-- Include common assembly info if project name starts with Nitrox -->
29+
<Compile Condition="'$(NitroxLibrary)' == 'true'" Include="$(SolutionDir)AssemblyInfoCommon.cs">
30+
<Link>Properties\AssemblyInfoCommon.cs</Link>
31+
</Compile>
32+
33+
<ProjectReference Condition="'$(UnityModLibrary)' == 'true'" Include="$(SolutionDir)NitroxModel\NitroxModel.csproj">
34+
<Name>NitroxModel</Name>
35+
</ProjectReference>
36+
</ItemGroup>
37+
</Project>

Directory.Build.targets

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(SolutionDir)SharedConfig.targets"/>
4+
5+
<!-- UnityEngine libraries to include -->
6+
<ItemGroup Condition="'$(UnityModLibrary)' == 'true' or '$(TestLibrary)' == 'true'">
7+
<Reference Include="$(SubnauticaManaged)\UnityEngine*.dll">
8+
<Private>$(TestLibrary)</Private>
9+
</Reference>
10+
11+
<Reference Include="$(SubnauticaManaged)\Assembly-CSharp.dll">
12+
<Private>$(TestLibrary)</Private>
13+
</Reference>
14+
15+
<Reference Include="$(SubnauticaManaged)\Assembly-CSharp-firstpass.dll">
16+
<Private>$(TestLibrary)</Private>
17+
</Reference>
18+
</ItemGroup>
19+
</Project>

Nitrox.Bootloader/Main.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Reflection;
5+
using System.Threading.Tasks;
6+
7+
namespace Nitrox.Bootloader
8+
{
9+
public static class Main
10+
{
11+
private static readonly Lazy<string> nitroxLauncherDir = new Lazy<string>(() =>
12+
{
13+
string nitroxAppData = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Nitrox");
14+
if (!Directory.Exists(nitroxAppData))
15+
{
16+
return null;
17+
}
18+
string nitroxLauncherPathFile = Path.Combine(nitroxAppData, "launcherpath.txt");
19+
if (!File.Exists(nitroxLauncherPathFile))
20+
{
21+
return null;
22+
}
23+
24+
try
25+
{
26+
string valueInFile = File.ReadAllText(nitroxLauncherPathFile).Trim();
27+
Task.Factory.StartNew(() =>
28+
{
29+
try
30+
{
31+
// Delete the path so that the launcher should be used to launch Nitrox
32+
File.Delete(nitroxLauncherPathFile);
33+
}
34+
catch (Exception ex)
35+
{
36+
Console.WriteLine($"Unable to delete the launcherpath.txt file. Nitrox will launch again without launcher. Error:{Environment.NewLine}{ex}");
37+
}
38+
});
39+
return Directory.Exists(valueInFile) ? valueInFile : null;
40+
}
41+
catch
42+
{
43+
// ignored
44+
}
45+
return null;
46+
});
47+
48+
public static void Execute()
49+
{
50+
string error = ValidateNitroxSetup();
51+
if (error != null)
52+
{
53+
Console.WriteLine(error);
54+
return;
55+
}
56+
57+
Environment.SetEnvironmentVariable("NITROX_LAUNCHER_PATH", nitroxLauncherDir.Value);
58+
59+
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomainOnAssemblyResolve;
60+
AppDomain.CurrentDomain.ReflectionOnlyAssemblyResolve += CurrentDomainOnAssemblyResolve;
61+
62+
BootstrapNitrox();
63+
}
64+
65+
private static void BootstrapNitrox()
66+
{
67+
Assembly core = Assembly.Load(new AssemblyName("NitroxPatcher"));
68+
Type mainType = core.GetType("NitroxPatcher.Main");
69+
mainType.InvokeMember("Execute", BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod, null, null, null);
70+
}
71+
72+
73+
private static string ValidateNitroxSetup()
74+
{
75+
if (nitroxLauncherDir.Value == null)
76+
{
77+
return "Nitrox launcher path not set in AppData. Nitrox will not start.";
78+
}
79+
if (AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(a => a.FullName.StartsWith("QMod", StringComparison.Ordinal)) != null)
80+
{
81+
return "Nitrox will not start because QModManager is active.";
82+
}
83+
84+
return null;
85+
}
86+
87+
private static Assembly CurrentDomainOnAssemblyResolve(object sender, ResolveEventArgs args)
88+
{
89+
string dllFileName = args.Name.Split(',')[0];
90+
if (!dllFileName.EndsWith(".dll"))
91+
{
92+
dllFileName += ".dll";
93+
}
94+
95+
// Load DLLs where Nitrox launcher is first, if not found, use Subnautica's DLLs.
96+
string dllPath = Path.Combine(nitroxLauncherDir.Value, dllFileName);
97+
if (!File.Exists(dllPath))
98+
{
99+
dllPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), dllFileName);
100+
}
101+
102+
if (!File.Exists(dllPath))
103+
{
104+
Console.WriteLine($"Nitrox dll missing: {dllPath}");
105+
}
106+
return Assembly.LoadFile(dllPath);
107+
}
108+
}
109+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{E4226522-9189-410B-93B2-792942FBD588}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Nitrox.Bootloader</RootNamespace>
11+
<AssemblyName>Nitrox.Bootloader</AssemblyName>
12+
<FileAlignment>512</FileAlignment>
13+
<NitroxLibrary>false</NitroxLibrary>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<ErrorReport>prompt</ErrorReport>
21+
<WarningLevel>4</WarningLevel>
22+
</PropertyGroup>
23+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
24+
<PlatformTarget>AnyCPU</PlatformTarget>
25+
<DebugType>pdbonly</DebugType>
26+
<Optimize>true</Optimize>
27+
<ErrorReport>prompt</ErrorReport>
28+
<WarningLevel>4</WarningLevel>
29+
</PropertyGroup>
30+
<ItemGroup>
31+
<Compile Include="Main.cs" />
32+
</ItemGroup>
33+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
34+
</Project>

AssetBundles/chatkeyhint renamed to Nitrox.Subnautica.Assets/AssetBundles/chatkeyhint

File renamed without changes.

AssetBundles/chatlog renamed to Nitrox.Subnautica.Assets/AssetBundles/chatlog

File renamed without changes.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<MSBuildAllProjects>$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
<HasSharedItems>true</HasSharedItems>
6+
<SharedGUID>81F1F8FF-E349-45AF-9830-C17C2D7CE7C2</SharedGUID>
7+
</PropertyGroup>
8+
<PropertyGroup Label="Configuration">
9+
<Import_RootNamespace>Nitrox.Subnautica.Assets</Import_RootNamespace>
10+
</PropertyGroup>
11+
</Project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" InitialTargets="TestBla">
3+
<PropertyGroup Label="Globals">
4+
<ProjectGuid>{0763D551-B1A8-4960-B88A-98833F957936}</ProjectGuid>
5+
</PropertyGroup>
6+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
7+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
8+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
9+
<Import Project="Nitrox.Subnautica.Assets.projitems" Label="Shared" />
10+
<!-- Include all files in project directory automatically -->
11+
<ItemGroup>
12+
<Content Include="$(MSBuildThisFileDirectory)**" Exclude="**\*.projitems" CopyToOutputDirectory="Always" />
13+
</ItemGroup>
14+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
15+
</Project>

0 commit comments

Comments
 (0)