From a1ce953149898f327ecb82976d34e9870dc24b03 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 14 Jul 2018 12:07:37 +1000 Subject: [PATCH] Simplify InteropPatcher a bit --- ClassicalSharp/ClassicalSharp.csproj | 1 - ClassicalSharp/app.config | 3 - InteropPatcher/InteropPatcher.csproj | 136 ++++++++++++------------ InteropPatcher/Patcher.cs | 153 --------------------------- InteropPatcher/Program.cs | 151 ++++++++++++++++++++++---- InteropPatcher/app.config | 6 -- src/Client/Camera.c | 3 + 7 files changed, 197 insertions(+), 256 deletions(-) delete mode 100644 ClassicalSharp/app.config delete mode 100644 InteropPatcher/Patcher.cs delete mode 100644 InteropPatcher/app.config diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index 94ee64497..0dc44be2c 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -403,7 +403,6 @@ credits.txt PreserveNewest - PreserveNewest diff --git a/ClassicalSharp/app.config b/ClassicalSharp/app.config deleted file mode 100644 index 4a71293db..000000000 --- a/ClassicalSharp/app.config +++ /dev/null @@ -1,3 +0,0 @@ - - - diff --git a/InteropPatcher/InteropPatcher.csproj b/InteropPatcher/InteropPatcher.csproj index 79d7bb66a..78e8475f6 100644 --- a/InteropPatcher/InteropPatcher.csproj +++ b/InteropPatcher/InteropPatcher.csproj @@ -1,71 +1,67 @@ - - - - {4A4110EE-21CA-4715-AF67-0C8B7CE0642F} - Debug - AnyCPU - Exe - InteropPatcher - InteropPatcher - v2.0 - - - Properties - False - False - False - False - obj\$(Configuration)\ - 4 - False - False - OnBuildSuccess - - - AnyCPU - 4194304 - False - Auto - 4096 - - - ..\output\debug\ - True - Full - False - False - DEBUG;TRACE - obj\ - Project - - - ..\output\release\ - False - None - True - False - TRACE - obj\ - - - - Mono.Cecil.dll - - - Mono.Cecil.Mdb.dll - - - Mono.Cecil.Pdb.dll - - - - - - - - - - - - + + + + {4A4110EE-21CA-4715-AF67-0C8B7CE0642F} + Debug + AnyCPU + Exe + InteropPatcher + InteropPatcher + v2.0 + + + Properties + False + False + False + False + obj\$(Configuration)\ + 4 + False + False + OnBuildSuccess + + + AnyCPU + 4194304 + False + Auto + 4096 + + + ..\output\debug\ + True + Full + False + False + DEBUG;TRACE + obj\ + Project + + + ..\output\release\ + False + None + True + False + TRACE + obj\ + + + + Mono.Cecil.dll + + + Mono.Cecil.Mdb.dll + + + Mono.Cecil.Pdb.dll + + + + + + + + \ No newline at end of file diff --git a/InteropPatcher/Patcher.cs b/InteropPatcher/Patcher.cs deleted file mode 100644 index 24d65c569..000000000 --- a/InteropPatcher/Patcher.cs +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. -using System; -using System.IO; -using Mono.Cecil; -using Mono.Cecil.Cil; -using Mono.Cecil.Pdb; -using Mono.Collections.Generic; - -namespace InteropPatcher { - - public static class Patcher { - - static void ReplaceFixedStatement(MethodDefinition method, ILProcessor ilProcessor, Instruction fixedtoPatch) { - TypeReference paramT = ((GenericInstanceMethod)fixedtoPatch.Operand).GenericArguments[0]; - // Preparing locals - // local(0) T* - method.Body.Variables.Add(new VariableDefinition("pin", new PinnedType(new ByReferenceType(paramT)))); - - int index = method.Body.Variables.Count - 1; - Instruction ldlocFixed, stlocFixed; - switch (index) { - case 0: - stlocFixed = ilProcessor.Create(OpCodes.Stloc_0); - ldlocFixed = ilProcessor.Create(OpCodes.Ldloc_0); - break; - case 1: - stlocFixed = ilProcessor.Create(OpCodes.Stloc_1); - ldlocFixed = ilProcessor.Create(OpCodes.Ldloc_1); - break; - case 2: - stlocFixed = ilProcessor.Create(OpCodes.Stloc_2); - ldlocFixed = ilProcessor.Create(OpCodes.Ldloc_2); - break; - case 3: - stlocFixed = ilProcessor.Create(OpCodes.Stloc_3); - ldlocFixed = ilProcessor.Create(OpCodes.Ldloc_3); - break; - default: - stlocFixed = ilProcessor.Create(OpCodes.Stloc, index); - ldlocFixed = ilProcessor.Create(OpCodes.Ldloc, index); - break; - } - - ilProcessor.InsertBefore(fixedtoPatch, stlocFixed); - ilProcessor.Replace(fixedtoPatch, ldlocFixed); - } - - static void PatchMethod(MethodDefinition method) { - if( !method.HasBody ) return; - ILProcessor ilProcessor = method.Body.GetILProcessor(); - - Collection instructions = method.Body.Instructions; - for (int i = 0; i < instructions.Count; i++) - { - Instruction instruction = instructions[i]; - if (instruction.OpCode == OpCodes.Call && instruction.Operand is MethodReference) - { - MethodReference desc = (MethodReference)instruction.Operand; - if( desc.DeclaringType.Name != "Interop" ) continue; - - if( desc.Name.StartsWith( "Calli" ) ) { - CallSite callSite = new CallSite(desc.ReturnType); - callSite.CallingConvention = MethodCallingConvention.StdCall; - // Last parameter is the function ptr, so we don't add it as a parameter for calli - // as it is already an implicit parameter for calli - for (int j = 0; j < desc.Parameters.Count - 1; j++) { - callSite.Parameters.Add(desc.Parameters[j]); - } - - Instruction callIInstruction = ilProcessor.Create(OpCodes.Calli, callSite); - ilProcessor.Replace(instruction, callIInstruction); - } else if( desc.Name.StartsWith( "Fixed" ) ) { - ReplaceFixedStatement(method, ilProcessor, instruction); - } - } - } - } - - static void PatchType( TypeDefinition type ) { - if( type.Name == "Interop" ) return; - - if( NeedsToBePatched( type ) ) { - Console.WriteLine( "Patching type: " + type ); - foreach( MethodDefinition method in type.Methods ) - PatchMethod( method ); - foreach( TypeDefinition nestedType in type.NestedTypes ) - PatchType( nestedType ); - } - } - - static bool NeedsToBePatched( TypeDefinition type ) { - foreach( CustomAttribute attrib in type.CustomAttributes ) { - if( attrib.AttributeType.Name == "InteropPatchAttribute" ) - return true; - } - return false; - } - - public static void PatchFile( string file ) { - // Copy PDB from input assembly to output assembly if any - ReaderParameters rParams = new ReaderParameters(); - rParams.AssemblyResolver = new DefaultAssemblyResolver(); - WriterParameters wParams = new WriterParameters(); - string pdbName = Path.ChangeExtension( file, "pdb" ); - - if( File.Exists( pdbName ) ) { - rParams.SymbolReaderProvider = new PdbReaderProvider(); - rParams.ReadSymbols = true; - wParams.WriteSymbols = true; - } - AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(file, rParams); - TypeDefinition interopTypeDefinition = null; - - Console.WriteLine( "SharpDX interop patch for: " + file ); - foreach( TypeDefinition type in assembly.MainModule.Types ) { - if( type.Name == "Interop" ) { - interopTypeDefinition = type; - break; - } - } - if( interopTypeDefinition == null ) { - Console.WriteLine( "Nothing to do, already patched." ); - return; - } - - foreach( TypeDefinition type in assembly.MainModule.Types ) { - PatchType( type ); - } - - assembly.MainModule.Types.Remove( interopTypeDefinition ); - assembly.Write( file, wParams ); - Console.WriteLine( "Done patching." ); - } - } -} \ No newline at end of file diff --git a/InteropPatcher/Program.cs b/InteropPatcher/Program.cs index fcac745d9..0fcc507e7 100644 --- a/InteropPatcher/Program.cs +++ b/InteropPatcher/Program.cs @@ -1,24 +1,129 @@ -using System; - -namespace InteropPatcher { - - public static class Program { - - public static int Main( string[] args ) { - try { - if( args.Length == 0 ) { - Console.WriteLine( "Expecting single argument specifying the file to patch" ); - return 2; - } - // Some older IDEs seem to like splitting the path when it has spaces. - // So we undo this and treat the arguments as a single path. - string path = String.Join( " ", args ); - Patcher.PatchFile( path ); - return 0; - } catch( Exception ex ) { - Console.WriteLine( ex ); - return 1; - } - } - } +// Copyright (c) 2010-2014 SharpDX - Alexandre Mutel +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using System.IO; +using Mono.Cecil; +using Mono.Cecil.Cil; +using Mono.Cecil.Pdb; +using Mono.Collections.Generic; + +namespace InteropPatcher { + + public static class Patcher { + + public static int Main( string[] args ) { + try { + if( args.Length == 0 ) { + Console.WriteLine( "Expecting single argument specifying the file to patch" ); + return 2; + } + // Some older IDEs seem to like splitting the path when it has spaces. + // So we undo this and treat the arguments as a single path. + string path = String.Join( " ", args ); + PatchFile( path ); + return 0; + } catch( Exception ex ) { + Console.WriteLine( ex ); + return 1; + } + } + + static void PatchMethod(MethodDefinition method) { + if (!method.HasBody) return; + ILProcessor ilProcessor = method.Body.GetILProcessor(); + + Collection instructions = method.Body.Instructions; + for (int i = 0; i < instructions.Count; i++) { + Instruction instruction = instructions[i]; + if (instruction.OpCode == OpCodes.Call && instruction.Operand is MethodReference) { + + MethodReference desc = (MethodReference)instruction.Operand; + if (desc.DeclaringType.Name != "Interop") continue; + if (!desc.Name.StartsWith("Calli")) continue; + + CallSite callSite = new CallSite(desc.ReturnType); + callSite.CallingConvention = MethodCallingConvention.StdCall; + // Last parameter is the function ptr, so we don't add it as a parameter for calli + // as it is already an implicit parameter for calli + for (int j = 0; j < desc.Parameters.Count - 1; j++) { + callSite.Parameters.Add(desc.Parameters[j]); + } + + Instruction calli_ins = ilProcessor.Create(OpCodes.Calli, callSite); + ilProcessor.Replace(instruction, calli_ins); + } + } + } + + static void PatchType(TypeDefinition type) { + if (type.Name == "Interop") return; + if (!NeedsToBePatched(type)) return; + + Console.WriteLine("Patching type: " + type); + foreach (MethodDefinition method in type.Methods) + PatchMethod(method); + foreach (TypeDefinition nestedType in type.NestedTypes) + PatchType(nestedType); + } + + static bool NeedsToBePatched(TypeDefinition type) { + foreach (CustomAttribute attrib in type.CustomAttributes) { + if (attrib.AttributeType.Name == "InteropPatchAttribute") + return true; + } + return false; + } + + public static void PatchFile(string file) { + ReaderParameters rParams = new ReaderParameters(); + rParams.AssemblyResolver = new DefaultAssemblyResolver(); + WriterParameters wParams = new WriterParameters(); + + // Copy PDB from input assembly to output assembly if any + if (File.Exists(Path.ChangeExtension(file, "pdb"))) { + rParams.SymbolReaderProvider = new PdbReaderProvider(); + rParams.ReadSymbols = true; + wParams.WriteSymbols = true; + } + + AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(file, rParams); + TypeDefinition interopTypeDefinition = null; + Console.WriteLine("Interop patching: " + file); + + foreach (TypeDefinition type in assembly.MainModule.Types) { + if (type.Name == "Interop") { + interopTypeDefinition = type; break; + } + } + if (interopTypeDefinition == null) { + Console.WriteLine("Nothing to do, already patched."); + return; + } + + foreach (TypeDefinition type in assembly.MainModule.Types) { + PatchType(type); + } + + assembly.MainModule.Types.Remove(interopTypeDefinition); + assembly.Write(file, wParams); + Console.WriteLine("Done patching."); + } + } } \ No newline at end of file diff --git a/InteropPatcher/app.config b/InteropPatcher/app.config deleted file mode 100644 index 1946b8a9f..000000000 --- a/InteropPatcher/app.config +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/Client/Camera.c b/src/Client/Camera.c index fa4a45cc4..a8fce37fd 100644 --- a/src/Client/Camera.c +++ b/src/Client/Camera.c @@ -253,5 +253,8 @@ void Camera_CycleActive(void) { Camera_Active = &Camera_Cameras[i]; Camera_ActiveIndex = i; + /* reset rotation offset when changing cameras */ + cam_rotOffset.X = 0.0f; cam_rotOffset.Y = 0.0f; + Game_UpdateProjection(); } \ No newline at end of file