Decompiled source of RockDrawing v1.0.0

Mods/RockDrawing.dll

Decompiled 2 months ago
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using Il2CppInterop.Runtime.Injection;
using MelonLoader;
using Newtonsoft.Json;
using RockDrawing;
using RumbleModdingAPI;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.Default | DebuggableAttribute.DebuggingModes.DisableOptimizations | DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints | DebuggableAttribute.DebuggingModes.EnableEditAndContinue)]
[assembly: MelonInfo(typeof(Class1), "Rock Drawing", "1.0.0", "SpooderCode", null)]
[assembly: MelonGame("", "")]
[assembly: TargetFramework(".NETCoreApp,Version=v6.0", FrameworkDisplayName = ".NET 6.0")]
[assembly: AssemblyCompany("RockDrawing")]
[assembly: AssemblyConfiguration("Debug")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("1.0.0")]
[assembly: AssemblyProduct("RockDrawing")]
[assembly: AssemblyTitle("RockDrawing")]
[assembly: AssemblyVersion("1.0.0.0")]
namespace RockDrawing;

public class Class1 : MelonMod
{
	public GameObject Brush;

	public static GameObject Pen;

	public GameObject SaveBtn;

	public GameObject LoadBtn;

	public override void OnInitializeMelon()
	{
		ClassInjector.RegisterTypeInIl2Cpp<DecalPainter>();
	}

	public override void OnSceneWasLoaded(int buildIndex, string sceneName)
	{
		//IL_0081: Unknown result type (might be due to invalid IL or missing references)
		//IL_0095: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00cf: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e3: Unknown result type (might be due to invalid IL or missing references)
		//IL_00e8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0108: Unknown result type (might be due to invalid IL or missing references)
		//IL_0128: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0148: Unknown result type (might be due to invalid IL or missing references)
		//IL_015c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0161: Unknown result type (might be due to invalid IL or missing references)
		//IL_0186: Unknown result type (might be due to invalid IL or missing references)
		//IL_019a: Unknown result type (might be due to invalid IL or missing references)
		//IL_01ae: Unknown result type (might be due to invalid IL or missing references)
		//IL_01b3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01d3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f3: Unknown result type (might be due to invalid IL or missing references)
		//IL_01f8: Unknown result type (might be due to invalid IL or missing references)
		//IL_0213: Unknown result type (might be due to invalid IL or missing references)
		//IL_0227: Unknown result type (might be due to invalid IL or missing references)
		//IL_022c: Unknown result type (might be due to invalid IL or missing references)
		//IL_0251: Unknown result type (might be due to invalid IL or missing references)
		//IL_0265: Unknown result type (might be due to invalid IL or missing references)
		//IL_0279: Unknown result type (might be due to invalid IL or missing references)
		//IL_027e: Unknown result type (might be due to invalid IL or missing references)
		//IL_029e: Unknown result type (might be due to invalid IL or missing references)
		//IL_02be: Unknown result type (might be due to invalid IL or missing references)
		//IL_02c3: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		if (sceneName == "Park")
		{
			if ((Object)(object)Brush == (Object)null)
			{
				Brush = Object.Instantiate<GameObject>(Calls.LoadAssetFromStream<GameObject>((MelonMod)(object)this, "RockDrawing.Resources.rumbledrawing", "Brush"));
				Brush.transform.position = new Vector3(0f, -1000f, 0f);
				Object.DontDestroyOnLoad((Object)(object)Brush);
			}
			GameObject val = Create.NewButton(new Vector3(-29.85f, -1.56f, -6.347f), Quaternion.Euler(new Vector3(90f, 90f, 0f)), (Action)CreatePen);
			GameObject val2 = Create.NewText("Rock Pen", 4f, Color.black, new Vector3(-29.85f, -1.26f, -6.347f), Quaternion.Euler(new Vector3(0f, 270f, 0f)));
			val2.transform.position = new Vector3(-29.85f, -1.26f, -6.347f);
			val2.transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f));
			SaveBtn = Create.NewButton(new Vector3(-29.85f, -1.86f, -6.747f), Quaternion.Euler(new Vector3(90f, 90f, 0f)), (Action)Save);
			val2 = Create.NewText("Save", 2f, Color.black, new Vector3(-29.85f, -1.66f, -6.747f), Quaternion.Euler(new Vector3(0f, 270f, 0f)));
			val2.transform.position = new Vector3(-29.85f, -1.66f, -6.747f);
			val2.transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f));
			LoadBtn = Create.NewButton(new Vector3(-29.7f, -1.86f, -5.947f), Quaternion.Euler(new Vector3(90f, 90f, 0f)), (Action)Load);
			val2 = Create.NewText("Load", 2f, Color.black, new Vector3(-29.85f, -1.66f, -5.947f), Quaternion.Euler(new Vector3(0f, 270f, 0f)));
			val2.transform.position = new Vector3(-29.75f, -1.66f, -5.947f);
			val2.transform.rotation = Quaternion.Euler(new Vector3(0f, 270f, 0f));
		}
	}

	public void CreatePen()
	{
		//IL_0072: Unknown result type (might be due to invalid IL or missing references)
		//IL_0084: Unknown result type (might be due to invalid IL or missing references)
		//IL_008e: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		if (!Object.op_Implicit((Object)(object)Pen))
		{
			GameObject val = Object.Instantiate<GameObject>(Calls.LoadAssetFromStream<GameObject>((MelonMod)(object)this, "RockDrawing.Resources.rumbledrawing", "Pen"));
			val.transform.SetParent(((Component)Players.GetPlayerController()).transform.Find("VR/Right Controller"));
			val.AddComponent<DecalPainter>().decalPrefab = Brush;
			val.transform.localPosition = new Vector3(0f, 0f, 0.4f);
			Transform transform = val.transform;
			transform.localScale *= 0.5f;
			val.transform.localRotation = Quaternion.identity;
			Pen = val;
		}
		else
		{
			Pen.GetComponent<DecalPainter>().ClearAllStrokes();
			Object.Destroy((Object)(object)Pen);
			Pen = null;
		}
	}

	public void Save()
	{
		if ((Object)(object)Pen != (Object)null)
		{
			Pen.GetComponent<DecalPainter>().SaveDecals();
		}
	}

	public void Load()
	{
		if ((Object)(object)Pen != (Object)null)
		{
			Pen.GetComponent<DecalPainter>().LoadDecals();
		}
	}
}
public class DecalPainter : MonoBehaviour
{
	[Serializable]
	public class DecalData
	{
		public float px;

		public float py;

		public float pz;

		public float rx;

		public float ry;

		public float rz;

		public float rw;

		public float sx;

		public float sy;

		public float sz;

		public string parentName;

		public DecalData()
		{
		}

		public DecalData(Transform t, string parent)
		{
			//IL_000a: Unknown result type (might be due to invalid IL or missing references)
			//IL_001b: Unknown result type (might be due to invalid IL or missing references)
			//IL_002c: Unknown result type (might be due to invalid IL or missing references)
			//IL_003d: Unknown result type (might be due to invalid IL or missing references)
			//IL_004e: Unknown result type (might be due to invalid IL or missing references)
			//IL_005f: Unknown result type (might be due to invalid IL or missing references)
			//IL_0070: Unknown result type (might be due to invalid IL or missing references)
			//IL_0081: Unknown result type (might be due to invalid IL or missing references)
			//IL_0092: Unknown result type (might be due to invalid IL or missing references)
			//IL_00a3: Unknown result type (might be due to invalid IL or missing references)
			px = t.position.x;
			py = t.position.y;
			pz = t.position.z;
			rx = t.rotation.x;
			ry = t.rotation.y;
			rz = t.rotation.z;
			rw = t.rotation.w;
			sx = t.localScale.x;
			sy = t.localScale.y;
			sz = t.localScale.z;
			parentName = parent;
		}

		public Vector3 GetPosition()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(px, py, pz);
		}

		public Quaternion GetRotation()
		{
			//IL_0018: Unknown result type (might be due to invalid IL or missing references)
			return new Quaternion(rx, ry, rz, rw);
		}

		public Vector3 GetScale()
		{
			//IL_0012: Unknown result type (might be due to invalid IL or missing references)
			return new Vector3(sx, sy, sz);
		}
	}

	public float rayDistance = 10f;

	public LayerMask paintableLayers = LayerMask.op_Implicit(-1);

	public GameObject decalPrefab;

	public float decalSize = 0.2f;

	public float spacing = 0.06f;

	public float shrinkStartDistance = 3f;

	public float minScaleMultiplier = 0.1f;

	public float maxScaleMultiplier = 1f;

	public float eraseRadius = 0.2f;

	private readonly List<GameObject> spawnedDecals = new List<GameObject>();

	private Vector3 lastHitPos;

	private readonly string saveFolder = Path.Combine("UserData", "RockDrawing");

	private readonly string saveFile = Path.Combine("UserData", "RockDrawing", "saved_drawing.json");

	public DecalPainter(IntPtr ptr)
		: base(ptr)
	{
	}//IL_000d: Unknown result type (might be due to invalid IL or missing references)
	//IL_0012: Unknown result type (might be due to invalid IL or missing references)


	public void Update()
	{
		//IL_001b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0026: Unknown result type (might be due to invalid IL or missing references)
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_004a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0050: Unknown result type (might be due to invalid IL or missing references)
		//IL_00ee: Unknown result type (might be due to invalid IL or missing references)
		//IL_00f9: Unknown result type (might be due to invalid IL or missing references)
		//IL_0107: Unknown result type (might be due to invalid IL or missing references)
		//IL_0091: Unknown result type (might be due to invalid IL or missing references)
		//IL_009b: Unknown result type (might be due to invalid IL or missing references)
		//IL_00a0: Unknown result type (might be due to invalid IL or missing references)
		//IL_013c: Unknown result type (might be due to invalid IL or missing references)
		if (RightController.GetTrigger() > 0.9f)
		{
			RaycastHit hit = default(RaycastHit);
			if (Physics.Raycast(((Component)this).transform.position, ((Component)this).transform.forward, ref hit, rayDistance, LayerMask.op_Implicit(paintableLayers)) && Vector3.Distance(((RaycastHit)(ref hit)).point, lastHitPos) > spacing)
			{
				((Component)((Component)this).transform.Find("Line")).gameObject.SetActive(true);
				((Component)this).GetComponent<AudioSource>().mute = false;
				PlaceDecal(hit);
				lastHitPos = ((RaycastHit)(ref hit)).point;
			}
		}
		else
		{
			((Component)this).GetComponent<AudioSource>().mute = true;
			((Component)((Component)this).transform.Find("Line")).gameObject.SetActive(false);
		}
		if (RightController.GetGrip() > 0.9f)
		{
			RaycastHit val = default(RaycastHit);
			if (Physics.Raycast(((Component)this).transform.position, ((Component)this).transform.forward, ref val, rayDistance, LayerMask.op_Implicit(paintableLayers)))
			{
				((Component)((Component)this).transform.Find("EraseLine")).gameObject.SetActive(true);
				EraseAtPoint(((RaycastHit)(ref val)).point);
			}
		}
		else
		{
			((Component)((Component)this).transform.Find("EraseLine")).gameObject.SetActive(false);
		}
		if (RightController.GetSecondary() > 0f)
		{
			ClearAllStrokes();
		}
	}

	private void PlaceDecal(RaycastHit hit)
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_000e: Unknown result type (might be due to invalid IL or missing references)
		//IL_005a: Unknown result type (might be due to invalid IL or missing references)
		//IL_0061: Unknown result type (might be due to invalid IL or missing references)
		//IL_006b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0070: Unknown result type (might be due to invalid IL or missing references)
		//IL_0075: Unknown result type (might be due to invalid IL or missing references)
		//IL_0093: Unknown result type (might be due to invalid IL or missing references)
		//IL_009a: Unknown result type (might be due to invalid IL or missing references)
		//IL_009f: Unknown result type (might be due to invalid IL or missing references)
		//IL_00b0: Unknown result type (might be due to invalid IL or missing references)
		//IL_00bb: Unknown result type (might be due to invalid IL or missing references)
		//IL_00c1: Unknown result type (might be due to invalid IL or missing references)
		//IL_017b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0112: Unknown result type (might be due to invalid IL or missing references)
		//IL_0117: Unknown result type (might be due to invalid IL or missing references)
		//IL_0119: Unknown result type (might be due to invalid IL or missing references)
		//IL_0121: Unknown result type (might be due to invalid IL or missing references)
		//IL_0126: Unknown result type (might be due to invalid IL or missing references)
		//IL_012b: Unknown result type (might be due to invalid IL or missing references)
		//IL_012d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0131: Unknown result type (might be due to invalid IL or missing references)
		//IL_0136: Unknown result type (might be due to invalid IL or missing references)
		//IL_013b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0152: Unknown result type (might be due to invalid IL or missing references)
		//IL_0156: Unknown result type (might be due to invalid IL or missing references)
		//IL_015b: Unknown result type (might be due to invalid IL or missing references)
		//IL_0160: Unknown result type (might be due to invalid IL or missing references)
		//IL_0168: Unknown result type (might be due to invalid IL or missing references)
		float num = Vector3.Distance(((Component)this).transform.position, ((RaycastHit)(ref hit)).point);
		float num2 = maxScaleMultiplier;
		if (num < shrinkStartDistance)
		{
			float num3 = Mathf.Clamp01(num / shrinkStartDistance);
			num2 = Mathf.Lerp(minScaleMultiplier, maxScaleMultiplier, num3);
		}
		GameObject val = Object.Instantiate<GameObject>(decalPrefab, ((RaycastHit)(ref hit)).point + ((RaycastHit)(ref hit)).normal * 0.001f, Quaternion.identity);
		spawnedDecals.Add(val);
		val.transform.rotation = Quaternion.FromToRotation(Vector3.up, ((RaycastHit)(ref hit)).normal);
		val.transform.localScale = Vector3.one * decalSize * num2;
		spacing = 0.06f * num2;
		if (spawnedDecals.Count > 1)
		{
			GameObject val2 = spawnedDecals[spawnedDecals.Count - 2];
			Vector3 position = val2.transform.position;
			Vector3 val3 = position - val.transform.position;
			Vector3 val4 = Vector3.ProjectOnPlane(val3, ((RaycastHit)(ref hit)).normal);
			if (((Vector3)(ref val4)).sqrMagnitude > 0.0001f)
			{
				Quaternion rotation = Quaternion.LookRotation(val4, ((RaycastHit)(ref hit)).normal);
				val.transform.rotation = rotation;
			}
		}
		else
		{
			val.transform.Rotate(Vector3.up, Random.Range(0f, 360f), (Space)1);
		}
		val.transform.SetParent(((Component)((RaycastHit)(ref hit)).collider).transform);
	}

	private void EraseAtPoint(Vector3 point)
	{
		//IL_0034: Unknown result type (might be due to invalid IL or missing references)
		//IL_0039: Unknown result type (might be due to invalid IL or missing references)
		List<GameObject> list = new List<GameObject>();
		foreach (GameObject spawnedDecal in spawnedDecals)
		{
			if (!((Object)(object)spawnedDecal == (Object)null))
			{
				float num = Vector3.Distance(spawnedDecal.transform.position, point);
				if (num < eraseRadius)
				{
					list.Add(spawnedDecal);
				}
			}
		}
		foreach (GameObject item in list)
		{
			spawnedDecals.Remove(item);
			Object.Destroy((Object)(object)item);
		}
	}

	public void ClearAllStrokes()
	{
		foreach (GameObject spawnedDecal in spawnedDecals)
		{
			if ((Object)(object)spawnedDecal != (Object)null)
			{
				Object.Destroy((Object)(object)spawnedDecal);
			}
		}
		spawnedDecals.Clear();
	}

	public void SaveDecals()
	{
		if (!Directory.Exists(saveFolder))
		{
			Directory.CreateDirectory(saveFolder);
		}
		List<DecalData> list = new List<DecalData>();
		foreach (GameObject spawnedDecal in spawnedDecals)
		{
			if (!((Object)(object)spawnedDecal == (Object)null))
			{
				string parent = (((Object)(object)spawnedDecal.transform.parent != (Object)null) ? ((Object)spawnedDecal.transform.parent).name : "World");
				list.Add(new DecalData(spawnedDecal.transform, parent));
			}
		}
		string contents;
		try
		{
			contents = JsonConvert.SerializeObject((object)list, (Formatting)1);
		}
		catch
		{
			contents = JsonConvert.SerializeObject((object)list);
		}
		File.WriteAllText(saveFile, contents);
		MelonLogger.Msg($"Saved {list.Count} decals to {saveFile}");
	}

	public void LoadDecals()
	{
		//IL_005d: Unknown result type (might be due to invalid IL or missing references)
		//IL_0064: Unknown result type (might be due to invalid IL or missing references)
		//IL_0079: Unknown result type (might be due to invalid IL or missing references)
		if (!File.Exists(saveFile))
		{
			MelonLogger.Warning("No save file found!");
			return;
		}
		ClearAllStrokes();
		string text = File.ReadAllText(saveFile);
		List<DecalData> list = JsonConvert.DeserializeObject<List<DecalData>>(text);
		foreach (DecalData item in list)
		{
			GameObject val = Object.Instantiate<GameObject>(decalPrefab, item.GetPosition(), item.GetRotation());
			val.transform.localScale = item.GetScale();
			spawnedDecals.Add(val);
			GameObject val2 = FindObjectByName(item.parentName);
			if ((Object)(object)val2 != (Object)null)
			{
				val.transform.SetParent(val2.transform);
			}
			else if (item.parentName != "World")
			{
				Object.Destroy((Object)(object)val);
			}
		}
		MelonLogger.Msg($"Loaded {list.Count} decals from {saveFile}");
	}

	private GameObject FindObjectByName(string name)
	{
		if (name == "World")
		{
			return null;
		}
		return GameObject.Find(name);
	}
}