-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDebug.cs
328 lines (312 loc) · 14.2 KB
/
Debug.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;
using MelonLoader;
using HarmonyLib;
using static KarlsonLevels.Main;
using KarlsonLevels.Workshop_API;
using System.Runtime.InteropServices;
namespace KarlsonLevels
{
class Editor
{
const string magic = "MLL2\r\n";
const byte fileVersion = 0;
public static float step = 0.05f;
public static void NewSave(string name = "level") {
File.WriteAllBytes(Path.Combine(Directory.GetCurrentDirectory(), "Levels", name + ".mll"), SaveLevelBytes());
}
public static byte[] SaveLevelBytes()
{
MelonLogger.Msg(Level.Count);
LevelData = new LevelObjectData[Level.Count];
for (int i = 0; i < LevelData.Length; i++)
{
MelonLogger.Msg(Level[i].Object);
LevelData[i].position = Level[i].Object.transform.position;
LevelData[i].scale = Level[i].Object.transform.localScale;
LevelData[i].rotation = Level[i].Object.transform.eulerAngles;
LevelData[i].Id = Level[i].Id;
LevelData[i].prefab = Level[i].prefab;
}
List<byte> save = new List<byte>();
foreach (char c in magic)
{
save.Add(Convert.ToByte(c));
}
save.Add(fileVersion);
ListAdd(ref save, (ushort)LevelData.Length);
foreach (LevelObjectData lod in LevelData)
{
ListAdd(ref save, (ushort)lod.Id);
ListAdd(ref save, (ushort)lod.prefab);
ListAdd(ref save, lod.position);
ListAdd(ref save, lod.scale);
ListAdd(ref save, lod.rotation);
}
ListAdd(ref save, Hash(save.ToArray()));
return save.ToArray();
}
public static byte[] MakeScreenshot()
{
GameObject GOcam = new GameObject("Screenshot Camera");
Camera cam = GOcam.AddComponent<Camera>();
cam.fieldOfView = Camera.main.fieldOfView;
GOcam.transform.position = Camera.main.transform.position;
GOcam.transform.rotation = Camera.main.transform.rotation;
RenderTexture rt = new RenderTexture(177, 100, 24);
cam.targetTexture = rt;
Texture2D screenShot = new Texture2D(177, 100, TextureFormat.RGB24, false);
cam.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, 177, 100), 0, 0);
cam.targetTexture = null;
RenderTexture.active = null;
UnityEngine.Object.Destroy(rt);
UnityEngine.Object.Destroy(GOcam);
return screenShot.EncodeToPNG();
}
static void Save() {
LevelData = new LevelObjectData[Level.Count];
for (int i = 0; i < LevelData.Length; i++)
{
MelonLogger.Msg(Level[i].Object);
LevelData[i].position = Level[i].Object.transform.position;
LevelData[i].scale = Level[i].Object.transform.localScale;
LevelData[i].rotation = Level[i].Object.transform.eulerAngles;
LevelData[i].Id = Level[i].Id;
LevelData[i].prefab = Level[i].prefab;
}
string[] lines = new string[LevelData.Length + 1];
lines[0] = "MLL1";
for (int i = 1; i < lines.Length; i++)
{
int j = i - 1;
lines[i] = $"{LevelData[j].Id};{LevelData[j].prefab};{LevelData[j].position.ToString()};{LevelData[j].scale};{LevelData[j].rotation.ToString()};";
}
File.WriteAllLines(Path.Combine(Directory.GetCurrentDirectory(), "Levels", "level.mll"), lines, Encoding.ASCII);
}
public static LevelObject Spawn(string obj) {
int index = Convert.ToInt32(obj);
if (Prefabs[index].name == "Player" || Prefabs[index].name == "DetectWeapons") throw new FormatException();
Prefabs[index].SetActive(true);
GameObject foo = Object.Instantiate(Prefabs[index], PlayerMovement.Instance.gameObject.transform.position, Quaternion.identity);
LevelObject bar;
bar.Object = foo;
bar.Id = GetId();
bar.prefab = index;
Level.Add(bar);
if (index == 129) // Removes hinge joint because it glitches movement mode in editing
{
Object.Destroy(foo.GetComponent<HingeJoint>());
Object.Destroy(foo.GetComponent<Rigidbody>());
}
Prefabs[index].SetActive(false);
return bar;
}
static int GetId() {
// don't use labels =]]]]]]
bool found;
int foo;
do
{
found = false;
foo = UnityEngine.Random.Range(1, 50000); // bigger range yolo
foreach (LevelObject l in Level) if (l.Id == foo) { found = true; break; }
} while(found);
return foo;
// note from devilexe: id's should've been incrementary, but we are stuck with this now :(
}
public static IEnumerator NewLoad(string path)
{
byte[] data = File.ReadAllBytes(path);
return NewLoad(data, Path.GetFileName(path));
}
public static IEnumerator NewLoad(byte[] data, string levelName) {
try
{
if (data != null && data.Length > 0)
{
currentLevel = data;
currentLevelName = levelName;
// check magic number
if (!(data[0] == 77 && data[1] == 76 && data[2] == 76 && data[3] == 50 && data[4] == 13 && data[5] == 10))
{ // not a good solution but it works and im lazy
MelonLogger.Error("Load error: bad magic number; this might not be a level file");
yield break;
}
else if (data[6] > fileVersion)
{
MelonLogger.Error($"Load error: file version is newer than {fileVersion:00}, please update MLL to load this level!");
yield break;
}
else if (!CheckHash(data))
{
MelonLogger.Error("Load error: level data is corrupted");
yield break;
}
ushort objsLength = BitConverter.ToUInt16(data, 7);
LevelData = new LevelObjectData[objsLength];
for (int j = 0, i = 9; j < objsLength; i += 40, j++)
{
LevelObjectData lod;
lod.Id = BitConverter.ToUInt16(data, i);
lod.prefab = BitConverter.ToUInt16(data, i + 2) & 0x1FFF;
float x = BitConverter.ToSingle(data, i + 4);
float y = BitConverter.ToSingle(data, i + 8);
float z = BitConverter.ToSingle(data, i + 12);
lod.position = new Vector3(x, y, z);
MelonLogger.Msg(lod.position);
x = BitConverter.ToSingle(data, i + 16);
y = BitConverter.ToSingle(data, i + 20);
z = BitConverter.ToSingle(data, i + 24);
lod.scale = new Vector3(x, y, z);
x = BitConverter.ToSingle(data, i + 28);
y = BitConverter.ToSingle(data, i + 32);
z = BitConverter.ToSingle(data, i + 36);
lod.rotation = new Vector3(x, y, z);
LevelData[j] = lod;
}
}
}
catch (Exception e)
{
MelonLogger.Error(e.StackTrace);
MelonLogger.Error(e.Message);
}
yield return null;
SceneManager.LoadScene(6);
yield return null;
foreach (Collider c in Object.FindObjectsOfType<Collider>())
{
if (c.gameObject != PlayerMovement.Instance.gameObject & c.gameObject.GetComponent<DetectWeapons>() == null) DestroyObject.Destroy(c.gameObject);
}
Level = new List<LevelObject>();
for (int i = 0; i < LevelData.Length; i++)
{
Quaternion q = new Quaternion();
q.eulerAngles = LevelData[i].rotation;
GameObject g = Object.Instantiate(Prefabs[LevelData[i].prefab], LevelData[i].position, q);
if ((LevelData[i].prefab ^ 129) == 0) {
g.GetComponent<HingeJoint>().connectedBody = null;
}
g.transform.localScale = LevelData[i].scale;
g.SetActive(true);
LevelObject lo;
lo.Object = g;
lo.prefab = LevelData[i].prefab;
lo.Id = LevelData[i].Id;
Level.Add(lo);
}
if (!editMode)
{
Time.timeScale = 1f;
Game.Instance.StartGame();
}
}
static IEnumerator LoadLevel(string path) {
if (path != null)
{
string[] lines = File.ReadAllLines(path, Encoding.ASCII);
if (lines[0] != "MLL1") yield break;
LevelData = new LevelObjectData[lines.Length - 1];
for (int i = 0; i < lines.Length; i++)
{
string[] line = lines[i + 1].Split(';');
LevelData[i].Id = Convert.ToInt32(line[0]);
LevelData[i].prefab = Convert.ToInt32(line[1]);
LevelData[i].position = StringToVector3(line[2]);
LevelData[i].scale = StringToVector3(line[3]);
LevelData[i].rotation = StringToVector3(line[4]);
}
yield return null;
}
SceneManager.LoadScene(6);
yield return null;
foreach (Collider c in Object.FindObjectsOfType<Collider>())
{
if (c.gameObject != PlayerMovement.Instance.gameObject) DestroyObject.Destroy(c.gameObject);
}
for (int i = 0; i < LevelData.Length; i++)
{
Quaternion q = new Quaternion();
q.eulerAngles = LevelData[i].rotation;
GameObject g = Object.Instantiate(Prefabs[LevelData[i].prefab], LevelData[i].position, q);
g.SetActive(true);
LevelObject lo;
lo.Object = g;
lo.prefab = LevelData[i].prefab;
lo.Id = LevelData[i].Id;
Level.Add(lo);
}
}
public static IEnumerator StartEdit(string path) {
if (path == null)
{
Level = new List<LevelObject>();
SceneManager.LoadScene(6);
}
yield return null;
yield return null;
yield return null;
try
{
editMode = true;
Main.movableObj = -1;
PlayerMovement.Instance.gameObject.GetComponent<Rigidbody>().isKinematic = false;
PlayerMovement.Instance.gameObject.GetComponent<Rigidbody>().useGravity = false; //eh
PlayerMovement.Instance.gameObject.GetComponent<Collider>().enabled = false;
if (path == null)
foreach (Collider c in Object.FindObjectsOfType<Collider>())
if (c.gameObject != PlayerMovement.Instance.gameObject) DestroyObject.Destroy(c.gameObject);
// create gizmo
GameObject go1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
go1.GetComponent<Renderer>().material.SetColor("_Color", new Color(0, 0, 0, 1));
go1.transform.position = new Vector3(5000, 5000, 5000);
go1.transform.transform.localScale = new Vector3(1f, 1f, 1f);
GameObject go2 = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
go2.GetComponent<Renderer>().material.SetColor("_Color", new Color(0, 0, 1, 1));
go2.transform.position = new Vector3(5000, 5000, 5001);
go2.transform.rotation = Quaternion.Euler(90, 0, 0);
go2.transform.transform.localScale = new Vector3(0.3f, 1f, 0.3f);
GameObject go3 = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
go3.transform.position = new Vector3(5000, 5001, 5000);
go3.GetComponent<Renderer>().material.SetColor("_Color", new Color(0, 1, 0, 1));
go3.transform.rotation = Quaternion.Euler(0, 0, 0);
go3.transform.transform.localScale = new Vector3(0.3f, 1f, 0.3f);
GameObject go4 = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
go4.GetComponent<Renderer>().material.SetColor("_Color", new Color(1, 0, 0, 1));
go4.transform.position = new Vector3(5001, 5000, 5000);
go4.transform.rotation = Quaternion.Euler(0, 0, 90);
go4.transform.transform.localScale = new Vector3(0.3f, 1f, 0.3f);
// create backplane
GameObject GObg = GameObject.CreatePrimitive(PrimitiveType.Plane);
GObg.GetComponent<Renderer>().GetComponent<Renderer>().material.SetColor("_Color", new Color(0.5f, 0.5f, 0.5f, 1));
GObg.transform.localScale = new Vector3(1.6f, 1.6f, 1.6f);
GObg.name = "Gizmo Backplane";
}
catch (Exception e)
{
MelonLogger.Error("Player instance error! " + e.StackTrace);
}
}
}
public static class Extensions
{
public static byte[] SubArray(this byte[] array, int offset, int length) {
return array.Skip(offset)
.Take(length)
.ToArray();
}
}
[HarmonyPatch(typeof(Debug), "OpenConsole")]
public class Debug_OpenConsole
{
public static bool Prefix() => !editMode;
}
}