This is a packaging utility for class libraries targeting .NET Framework 4.8 for deployment in the Unity environment
This tool ensures...
-
🔥 The build output is clean (single DLL)
-
📦 All referenced assemblies are included as embedded resources
- This includes implicit references that are not typically copied to the output directory
-
📥 Embedded assemblies are compressed
-
đź“„ MetaData for each assembly is included
-
đźš« Manually referenced DLL's are not included
<Reference>- Things like
UnityEngine.dllwill not be added as an embedded resource
- Things like
-
đźš« Project references are not included
<ProjectReference>
Note
Common libraries like System.Memory are not copied to the output directory resulting in AssemblyLoad errors
These libraries will be included and packaged with your project
Caution
The embedded libraries are not automatically or magically loaded for you!
You must implement your own AppDomain.CurrentDomain.AssemblyResolve += handler;
For automatic dependency resolution, see here
This is a tool meant to run after assets.project.json is generated
On first build/restore, it will update the *.csproj file and changes may not take effect until the second build
Tip
It is recommended to add the following to your *.csproj
Be sure to include InitialTargets="GenerateNewTargets"
UnityModPackager/example.targets
Lines 1 to 17 in fe9b472
An example of main.targets:
UnityModPackager/example.main.targets
Lines 1 to 16 in 86d9c63
Important
Make sure that UnityModPackager can be accessed at $USER_HOME$/.dotnet/tools/
UnityModPackager/UnityModPackager.csproj
Lines 42 to 44 in 86d9c63
Once all of the above is complete, you are ready to start modding!
If you want, you can take a peak at the Workflow below to see how this tool works
Lines 29 to 32 in b231e56
-
Ensure Tag:
CopyLocalLockFileAssemblies = false -
Ensure Tag:
AutoGenerateBindingRedirects = true- Ensures properly linking to referenced libraries to prevent versioning errors
Line 50 in 0ab66d5
- Ensures properly linking to referenced libraries to prevent versioning errors
-
Ensure Tag:
GenerateBindingRedirectsOutputType = true- Ensures the binding redirects are generated based on the project output type ( library )
Line 51 in 0ab66d5
- Ensures the binding redirects are generated based on the project output type ( library )
-
Ensure Attribute:
Private = false- Ensures that
ProjectReferenceandReferenceare not copied to the output directory PackageReferenceis not required because it is handled by the tags aboveLines 52 to 54 in 0ab66d5
- Ensures that
-
Ensure Tag:
<Import Project="obj/GeneratedResources.targets"/>- Ensures the auto-generated resource file is imported into the project
Line 55 in 0ab66d5
- Ensures the auto-generated resource file is imported into the project
Lines 58 to 61 in 0ab66d5
- Locate all dependencies, including implicit (default behavior of
assets.project.json)Line 70 in 3a6a691
Warning
- If our target is invalid
_._or is underref, we must look for alternativesLine 71 in 921c87e
- Strongly prefer files found under
liband DO NOT include.NET Framework 4.5as it has a history of causing fatal crashes in UnityLines 73 to 78 in 921c87e
- If no candidates are found, fallback to files under
refLines 89 to 94 in b231e56
- Allow for multiple versions of the same Assembly, so that if
Assembly.Loadfails, there are fallback assemblies to tryLines 96 to 101 in 3a6a691
-
Generate library metadata in place
Lines 118 to 127 in b231e56
-
Compress library in place
Lines 136 to 140 in b231e56
-
Generate
.targetsfileLines 156 to 158 in 3a6a691
- Ensure the LogicalName (the name of the resource in the manifest) is unique under the current assembly
- This allows for multiple versions of the same assembly for fallback purposes
-
Write file to
obj/GeneratedResources.targets
Note
This tool only generates the workflow for automatically embedding assemblies into your project
Repo.Shared Includes the necessary components for automatically loading, decompressing, and resolving these internal assemblies
Check out REPO.Shared.AssemblyResolver.cs to see how the library metadata is used