Skip to content

Commit

Permalink
TMPE 1.9.0-rc1
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorPhilipp committed May 15, 2017
1 parent ae7be3f commit cdb0fbe
Show file tree
Hide file tree
Showing 101 changed files with 1,378 additions and 508 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
<ProjectGuid>{D3ADE06E-F493-4819-865A-3BB44FEEDF01}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Util</RootNamespace>
<AssemblyName>TMPE.Util</AssemblyName>
<RootNamespace>CSUtil.Commons</RootNamespace>
<AssemblyName>CSUtil.Commons</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
Expand Down Expand Up @@ -43,6 +43,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="EnumUtil.cs" />
<Compile Include="Log.cs" />
<Compile Include="LogicUtil.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ToStringExt.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Text;

namespace Util {
namespace CSUtil.Commons {
public static class EnumUtil {
public static IEnumerable<T> GetValues<T>() {
return Enum.GetValues(typeof(T)).Cast<T>();
Expand Down
21 changes: 2 additions & 19 deletions TLM/TLM/Log.cs → TLM/CSUtil.Commons/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Threading;
using UnityEngine;

namespace TrafficManager {
namespace CSUtil.Commons {

public static class Log {
private enum LogLevel {
Expand All @@ -17,11 +17,8 @@ private enum LogLevel {

private static object logLock = new object();

const string Prefix = "TrafficLightManager: ";

private static string logFilename = Path.Combine(Application.dataPath, "TMPE.log");
private static string logFilename = Path.Combine(Application.dataPath, "TMPE.log"); // TODO refactor log filename to configuration
private static Stopwatch sw = Stopwatch.StartNew();
private static bool logToConsole = false;
private static bool logFileAccessible = true;


Expand All @@ -42,8 +39,6 @@ public static void _Debug(string s) {

try {
Monitor.Enter(logLock);
if (logToConsole)
UnityEngine.Debug.Log(Prefix + s);
LogToFile(s, LogLevel.Debug);
} catch (Exception) {

Expand All @@ -58,10 +53,6 @@ public static void Info(string s) {
}

try {
#if DEBUG
if (logToConsole)
UnityEngine.Debug.Log(Prefix + s);
#endif
Monitor.Enter(logLock);
LogToFile(s, LogLevel.Info);
} catch (Exception) {
Expand All @@ -77,10 +68,6 @@ public static void Error(string s) {
}

try {
#if DEBUG
if (logToConsole)
UnityEngine.Debug.LogError(Prefix + s + " " + (new System.Diagnostics.StackTrace()).ToString());
#endif
Monitor.Enter(logLock);
LogToFile(s, LogLevel.Error);
} catch (Exception) {
Expand All @@ -96,10 +83,6 @@ public static void Warning(string s) {
}

try {
#if DEBUG
if (logToConsole)
UnityEngine.Debug.LogWarning(Prefix + s + ": " + (new System.Diagnostics.StackTrace()).ToString());
#endif
Monitor.Enter(logLock);
LogToFile(s, LogLevel.Warning);
} catch (Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Linq;
using System.Text;

namespace Util {
namespace CSUtil.Commons {
public static class LogicUtil {
public static bool CheckFlags(uint flags, uint flagMask, uint? expectedResult=null) {
uint res = flags & flagMask;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Linq;
using System.Text;

namespace Util {
namespace CSUtil.Commons {
public static class ToStringExt {
public static string DictionaryToString<K, V>(this IDictionary<K, V> element) {
return string.Join(", ", element.Keys.Select(x => $"{ToString(x)}={ToString(element[x])}").ToArray());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System.Text;
using UnityEngine;

namespace Util {
namespace CSUtil.Commons {
public static class VectorUtil {
public static void ClampPosToScreen(ref Vector3 pos) {
if (pos.x < 0)
Expand Down
64 changes: 64 additions & 0 deletions TLM/CSUtil.Redirection/CSUtil.Redirection.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{7DCC08EF-DC85-47A4-BD6F-79FC52C7EF13}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CSUtil.Redirection</RootNamespace>
<AssemblyName>CSUtil.Redirection</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RedirectionHelper.cs" />
<Compile Include="Redirector.cs" />
<Compile Include="MethodInfoExt.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CSUtil.Commons\CSUtil.Commons.csproj">
<Project>{D3ADE06E-F493-4819-865A-3BB44FEEDF01}</Project>
<Name>CSUtil.Commons</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
34 changes: 34 additions & 0 deletions TLM/CSUtil.Redirection/MethodInfoExt.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Reflection;

namespace CSUtil.Redirection {
public static class MethodInfoExt
{
internal static Redirector.MethodRedirection RedirectTo(this MethodInfo originalMethod, MethodInfo newMethod, Assembly redirectionSource)
{
return new Redirector.MethodRedirection(originalMethod, newMethod, redirectionSource);
}

public static bool IsCompatibleWith(this MethodInfo thisMethod, MethodInfo otherMethod)
{
if (thisMethod.ReturnType != otherMethod.ReturnType)
return false;

ParameterInfo[] thisParameters = thisMethod.GetParameters();
ParameterInfo[] otherParameters = otherMethod.GetParameters();

if (thisParameters.Length != otherParameters.Length)
return false;

for (int i = 0; i < thisParameters.Length; i++)
{
if (!otherParameters[i].ParameterType.IsAssignableFrom(thisParameters[i].ParameterType))
{
return false;
}
}

return true;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=_005FExtensions/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
36 changes: 36 additions & 0 deletions TLM/CSUtil.Redirection/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Transit.Framework.Redirection")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("Transit.Framework.Redirection")]
[assembly: AssemblyCopyright("Copyright © 2015")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("4250c75b-3c80-494f-a5c9-7e49c18ef1bc")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
119 changes: 119 additions & 0 deletions TLM/CSUtil.Redirection/RedirectionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
The MIT License (MIT)
Copyright (c) 2015 Sebastian Schöner
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.Reflection;

namespace CSUtil.Redirection {

public struct RedirectCallsState
{
public byte a, b, c, d, e;
public ulong f;
}

/// <summary>
/// Helper class to deal with detours. This version is for Unity 5 x64 on Windows.
/// We provide three different methods of detouring.
/// </summary>
public static class RedirectionHelper
{
/// <summary>
/// Redirects all calls from method 'from' to method 'to'.
/// </summary>
/// <param name="from"></param>
/// <param name="to"></param>
public static RedirectCallsState RedirectCalls(MethodInfo from, MethodInfo to)
{
// GetFunctionPointer enforces compilation of the method.
var fptr1 = from.MethodHandle.GetFunctionPointer();
var fptr2 = to.MethodHandle.GetFunctionPointer();
return PatchJumpTo(fptr1, fptr2);
}

public static RedirectCallsState RedirectCalls(RuntimeMethodHandle from, RuntimeMethodHandle to)
{
// GetFunctionPointer enforces compilation of the method.
var fptr1 = from.GetFunctionPointer();
var fptr2 = to.GetFunctionPointer();
return PatchJumpTo(fptr1, fptr2);
}

public static void RevertRedirect(MethodInfo from, RedirectCallsState state)
{
try
{
var fptr1 = from.MethodHandle.GetFunctionPointer();
RevertJumpTo(fptr1, state);
}
catch
{
// ignored
}
}

/// <summary>
/// Primitive patching. Inserts a jump to 'target' at 'site'. Works even if both methods'
/// callers have already been compiled.
/// </summary>
/// <param name="site"></param>
/// <param name="target"></param>
public static RedirectCallsState PatchJumpTo(IntPtr site, IntPtr target)
{
RedirectCallsState state = new RedirectCallsState();

// R11 is volatile.
unsafe
{
byte* sitePtr = (byte*)site.ToPointer();
state.a = *sitePtr;
state.b = *(sitePtr + 1);
state.c = *(sitePtr + 10);
state.d = *(sitePtr + 11);
state.e = *(sitePtr + 12);
state.f = *((ulong*)(sitePtr + 2));

*sitePtr = 0x49; // mov r11, target
*(sitePtr + 1) = 0xBB;
*((ulong*)(sitePtr + 2)) = (ulong)target.ToInt64();
*(sitePtr + 10) = 0x41; // jmp r11
*(sitePtr + 11) = 0xFF;
*(sitePtr + 12) = 0xE3;
}

return state;
}

public static void RevertJumpTo(IntPtr site, RedirectCallsState state)
{
unsafe
{
byte* sitePtr = (byte*)site.ToPointer();
*sitePtr = state.a; // mov r11, target
*(sitePtr + 1) = state.b;
*((ulong*)(sitePtr + 2)) = state.f;
*(sitePtr + 10) = state.c; // jmp r11
*(sitePtr + 11) = state.d;
*(sitePtr + 12) = state.e;
}
}

}
}
Loading

0 comments on commit cdb0fbe

Please sign in to comment.