initial commit
This commit is contained in:
parent
6715289efe
commit
788c3389af
37645 changed files with 2526849 additions and 80 deletions
|
|
@ -0,0 +1,18 @@
|
|||
This adds a simple ILSupport extension that adds `ref T AsRef<T>(in T thing)` to
|
||||
convert a read-only (in) ref to a regular ref, to avoid defensive copies when calling
|
||||
(non-mutating) methods on it. This is wrapped in UnsafeUtilityEx, as only Unity.Collections
|
||||
has InternalsVisibleTo into this library.
|
||||
|
||||
Collections.LowLevel.ILSupport can and should be generated by codegen & ilpostprocessors,
|
||||
however the ilpostproc infrastructure is unstable. Given that the result of this is
|
||||
constant, we just check in the final DLL. The source for everything is in the source~
|
||||
dir.
|
||||
|
||||
To rebuild or to add any functionality:
|
||||
- delete the prebuilt DLLs in this directory
|
||||
- and rename the source~ dir to source (without the ~)
|
||||
- fire up Unity and let it build. Everything should be successful, you're just now using the runtime-generated DLL.
|
||||
- make whatever changes you need to
|
||||
- make sure Unity's builds are up to date
|
||||
- copy Library/ScriptAssemblies/Unity.Collections.LowLevel.ILSupport.dll into this dir
|
||||
- rename source back to source~
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 11246cfa909f81542b920bd109592bf5
|
||||
TextScriptImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Binary file not shown.
|
|
@ -0,0 +1,33 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c1cfdf44d5b6d1e9aa671a48032fee86
|
||||
PluginImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
iconMap: {}
|
||||
executionOrder: {}
|
||||
defineConstraints: []
|
||||
isPreloaded: 0
|
||||
isOverridable: 1
|
||||
isExplicitlyReferenced: 0
|
||||
validateReferences: 1
|
||||
platformData:
|
||||
- first:
|
||||
Any:
|
||||
second:
|
||||
enabled: 1
|
||||
settings: {}
|
||||
- first:
|
||||
Editor: Editor
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
DefaultValueInitialized: true
|
||||
- first:
|
||||
Windows Store Apps: WindowsStoreApps
|
||||
second:
|
||||
enabled: 0
|
||||
settings:
|
||||
CPU: AnyCPU
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 01c1358bf558f42a9aa5cba53bcd9331
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,113 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using Mono.Cecil;
|
||||
using Mono.Cecil.Cil;
|
||||
using Mono.Cecil.Rocks;
|
||||
using Unity.CompilationPipeline.Common.Diagnostics;
|
||||
using Unity.CompilationPipeline.Common.ILPostProcessing;
|
||||
|
||||
namespace Unity.Collections.UnsafeUtility.CodeGen
|
||||
{
|
||||
internal class CollectionsUnsafeUtilityPostProcessor : ILPostProcessor
|
||||
{
|
||||
private static CollectionsUnsafeUtilityPostProcessor s_Instance;
|
||||
|
||||
public override ILPostProcessor GetInstance()
|
||||
{
|
||||
if (s_Instance == null)
|
||||
s_Instance = new CollectionsUnsafeUtilityPostProcessor();
|
||||
return s_Instance;
|
||||
}
|
||||
|
||||
public override bool WillProcess(ICompiledAssembly compiledAssembly)
|
||||
{
|
||||
if (compiledAssembly.Name == "Unity.Collections.LowLevel.ILSupport")
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
|
||||
{
|
||||
if (!WillProcess(compiledAssembly))
|
||||
return null;
|
||||
|
||||
var assemblyDefinition = AssemblyDefinitionFor(compiledAssembly);
|
||||
|
||||
var ilSupportType = assemblyDefinition.MainModule.Types.FirstOrDefault(t => t.FullName == "Unity.Collections.LowLevel.Unsafe.ILSupport");
|
||||
if (ilSupportType == null)
|
||||
throw new InvalidOperationException();
|
||||
|
||||
InjectUtilityAddressOfIn(ilSupportType);
|
||||
InjectUtilityAsRefIn(ilSupportType);
|
||||
|
||||
var pe = new MemoryStream();
|
||||
var pdb = new MemoryStream();
|
||||
var writerParameters = new WriterParameters
|
||||
{
|
||||
SymbolWriterProvider = new PortablePdbWriterProvider(), SymbolStream = pdb, WriteSymbols = true
|
||||
};
|
||||
|
||||
assemblyDefinition.Write(pe, writerParameters);
|
||||
return new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()));
|
||||
}
|
||||
|
||||
private void InjectUtilityAddressOfIn(TypeDefinition ctx)
|
||||
{
|
||||
var method = ctx.Methods.Single(m => m.Name.Equals("AddressOf") && m.Parameters[0].IsIn);
|
||||
var il = GetILProcessorForMethod(method);
|
||||
|
||||
il.Append(il.Create(OpCodes.Ldarg_0));
|
||||
il.Append(il.Create(OpCodes.Ret));
|
||||
}
|
||||
|
||||
private void InjectUtilityAsRefIn(TypeDefinition ctx)
|
||||
{
|
||||
var method = ctx.Methods.Single(m => m.Name.Equals("AsRef") && m.Parameters[0].IsIn);
|
||||
var il = GetILProcessorForMethod(method);
|
||||
|
||||
il.Append(il.Create(OpCodes.Ldarg_0));
|
||||
il.Append(il.Create(OpCodes.Ret));
|
||||
}
|
||||
|
||||
internal static AssemblyDefinition AssemblyDefinitionFor(ICompiledAssembly compiledAssembly)
|
||||
{
|
||||
var readerParameters = new ReaderParameters
|
||||
{
|
||||
SymbolStream = new MemoryStream(compiledAssembly.InMemoryAssembly.PdbData.ToArray()),
|
||||
SymbolReaderProvider = new PortablePdbReaderProvider(),
|
||||
ReadingMode = ReadingMode.Immediate
|
||||
};
|
||||
|
||||
var peStream = new MemoryStream(compiledAssembly.InMemoryAssembly.PeData.ToArray());
|
||||
var assemblyDefinition = AssemblyDefinition.ReadAssembly(peStream, readerParameters);
|
||||
|
||||
return assemblyDefinition;
|
||||
}
|
||||
|
||||
private static ILProcessor GetILProcessorForMethod(TypeDefinition ctx, string methodName, bool clear = true)
|
||||
{
|
||||
var method = ctx.Methods.Single(m => m.Name.Equals(methodName) && m.HasGenericParameters);
|
||||
return GetILProcessorForMethod(method, clear);
|
||||
}
|
||||
|
||||
private static ILProcessor GetILProcessorForMethod(MethodDefinition method, bool clear = true)
|
||||
{
|
||||
var ilProcessor = method.Body.GetILProcessor();
|
||||
|
||||
if (clear)
|
||||
{
|
||||
ilProcessor.Body.Instructions.Clear();
|
||||
ilProcessor.Body.Variables.Clear();
|
||||
ilProcessor.Body.ExceptionHandlers.Clear();
|
||||
}
|
||||
|
||||
return ilProcessor;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 2c40712b5124e4c33b9040e81f566b22
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"name": "Unity.Collections.LowLevel.ILSupport.CodeGen",
|
||||
"references": [
|
||||
],
|
||||
"includePlatforms": [
|
||||
"Editor"
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": true,
|
||||
"overrideReferences": true,
|
||||
"precompiledReferences": [
|
||||
"Mono.Cecil.dll",
|
||||
"Mono.Cecil.Rocks.dll",
|
||||
"Mono.Cecil.Pdb.dll"
|
||||
],
|
||||
"autoReferenced": false,
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": false
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 851faa33f0fe11de69f86c5599d17c88
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 38f5d8a8dc0e00d1fb83ec98cc122c85
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
[assembly: InternalsVisibleTo("Unity.Collections")]
|
||||
|
||||
namespace Unity.Collections.LowLevel.Unsafe
|
||||
{
|
||||
internal unsafe static class ILSupport
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* AddressOf<T>(in T thing)
|
||||
where T : struct
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ref T AsRef<T>(in T thing)
|
||||
where T : struct
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 8ed138ce0eef88e8898615fee7b8acf5
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "Unity.Collections.LowLevel.ILSupport",
|
||||
"references": [
|
||||
],
|
||||
"includePlatforms": [
|
||||
],
|
||||
"excludePlatforms": [],
|
||||
"allowUnsafeCode": true,
|
||||
"overrideReferences": true,
|
||||
"autoReferenced": false,
|
||||
"versionDefines": [],
|
||||
"noEngineReferences": true
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 231b25ba64524e26aa697960dbc7d0d0
|
||||
AssemblyDefinitionImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Loading…
Add table
Add a link
Reference in a new issue