Last active
May 15, 2018 02:19
-
-
Save boformer/5f8e9269b02d5b8809da to your computer and use it in GitHub Desktop.
Random Tree Rotation Mod for Cities: Skylines (Concerts/Natural Disasters)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ICities; | |
namespace RandomTreeRotation | |
{ | |
public class RandomTreeRotationMod : LoadingExtensionBase, IUserMod | |
{ | |
public string Name | |
{ | |
get { return "Random Tree Rotation"; } | |
} | |
public string Description | |
{ | |
get { return "Applys a random location to all rendered trees."; } | |
} | |
public override void OnCreated(ILoading loading) | |
{ | |
base.OnCreated(loading); | |
TreeInstanceDetour.Deploy(); | |
} | |
public override void OnReleased() | |
{ | |
base.OnReleased(); | |
TreeInstanceDetour.Revert(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 OTHERa | |
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; | |
using System.Runtime.InteropServices; | |
namespace RandomTreeRotation | |
{ | |
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) | |
{ | |
var fptr1 = from.MethodHandle.GetFunctionPointer(); | |
RevertJumpTo(fptr1, state); | |
} | |
/// <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; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using ColossalFramework; | |
using ColossalFramework.Math; | |
using System; | |
using System.Reflection; | |
using UnityEngine; | |
namespace RandomTreeRotation | |
{ | |
public class TreeInstanceDetour : Singleton<TreeInstanceDetour> | |
{ | |
public int x = 0; | |
public long[] a_bits = new long[100]; | |
public float[] a_rots = new float[100]; | |
private static bool deployed = false; | |
private static RedirectCallsState state; | |
private static MethodInfo original; | |
private static MethodInfo detour; | |
public static void Deploy() | |
{ | |
instance.x = 0; | |
if (!deployed) | |
{ | |
original = typeof(TreeInstance).GetMethod("RenderInstance", BindingFlags.Static | BindingFlags.Public); | |
detour = typeof(TreeInstanceDetour).GetMethod("RenderInstance", BindingFlags.Static | BindingFlags.Public); | |
state = RedirectionHelper.RedirectCalls(original, detour); | |
deployed = true; | |
Debug.Log("RandomTreeRotation: TreeInstance Methods detoured!"); | |
} | |
} | |
public static void Revert() | |
{ | |
if (deployed) | |
{ | |
RedirectionHelper.RevertRedirect(original, state); | |
original = null; | |
detour = null; | |
deployed = false; | |
Debug.Log("RandomTreeRotation: TreeInstance Methods restored!"); | |
} | |
} | |
public static void RenderInstance(RenderManager.CameraInfo cameraInfo, TreeInfo info, Vector3 position, float scale, float brightness) | |
{ | |
if (info.m_prefabInitialized) | |
{ | |
if (cameraInfo == null || info.m_lodMesh1 == null || cameraInfo.CheckRenderDistance(position, info.m_lodRenderDistance)) | |
{ | |
TreeManager instance = Singleton<TreeManager>.instance; | |
MaterialPropertyBlock materialBlock = instance.m_materialBlock; | |
Matrix4x4 matrix = default(Matrix4x4); | |
// mod begin | |
long setofBits = BitConverter.DoubleToInt64Bits(position.magnitude); | |
Quaternion rotation = Quaternion.Euler(0, (float)(setofBits % 360), 0); | |
// mod end | |
matrix.SetTRS(position, /* use the new rotation here */ rotation, new Vector3(scale, scale, scale)); | |
Color value = info.m_defaultColor * brightness; | |
value.a = Singleton<WeatherManager>.instance.GetWindSpeed(position); | |
materialBlock.Clear(); | |
materialBlock.SetColor(instance.ID_Color, value); | |
TreeManager expr_97_cp_0 = instance; | |
expr_97_cp_0.m_drawCallData.m_defaultCalls = expr_97_cp_0.m_drawCallData.m_defaultCalls + 1; | |
Graphics.DrawMesh(info.m_mesh, matrix, info.m_material, info.m_prefabDataLayer, null, 0, materialBlock); | |
} | |
else | |
{ | |
position.y += info.m_generatedInfo.m_center.y * (scale - 1f); | |
Color color = info.m_defaultColor * brightness; | |
color.a = Singleton<WeatherManager>.instance.GetWindSpeed(position); | |
info.m_lodLocations[info.m_lodCount] = new Vector4(position.x, position.y, position.z, scale); | |
info.m_lodColors[info.m_lodCount] = color.linear; | |
info.m_lodMin = Vector3.Min(info.m_lodMin, position); | |
info.m_lodMax = Vector3.Max(info.m_lodMax, position); | |
if (++info.m_lodCount == info.m_lodLocations.Length) | |
{ | |
global::TreeInstance.RenderLod(cameraInfo, info); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment