This repository was archived by the owner on Feb 3, 2022. It is now read-only.
forked from flarfo/Muck-SaveUtility
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorld.cs
More file actions
177 lines (147 loc) · 5.42 KB
/
Copy pathWorld.cs
File metadata and controls
177 lines (147 loc) · 5.42 KB
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
using System;
using System.Collections;
using System.Collections.Generic;
using HarmonyLib;
using UnityEngine;
using System.Reflection;
namespace SaveUtility
{
[HarmonyPatch]
public static class World
{
public static bool doSave = false;
public static int worldSeed;
internal static Dictionary<int, BuildWrapper> builds = new Dictionary<int, BuildWrapper>();
public static int chest;
public static int furnace;
public static int cauldron;
[HarmonyPatch(typeof(ItemManager), "InitAllItems")]
[HarmonyPostfix]
static void GetBuildIds()
{
chest = ItemManager.Instance.GetItemByName("Chest").id;
furnace = ItemManager.Instance.GetItemByName("Furnace").id;
cauldron = ItemManager.Instance.GetItemByName("Cauldron").id;
}
[HarmonyPatch(typeof(SteamLobby), "FindSeed")]
static void Postfix(ref int __result)
{
worldSeed = __result;
}
[HarmonyPatch(typeof(GameLoop), "NewDay")]
static void Postfix(int day)
{
if (UIManager.useAutoSave)
{
Debug.Log("Day " + day);
if (day == 0 || !doSave)
{
Debug.Log("DO NOT SAVE");
return;
}
Save();
}
}
public static void Save()
{
if (LocalClient.serverOwner)
{
if (LoadManager.currentSave != "")
{
ClientSend.SendChatMessage("<color=#ADD8E6>Saving...");
ChatBox.Instance.AppendMessage(-1, "<color=#ADD8E6>Saving...", "");
Debug.Log("SAVING");
//send packets to receive player data
ServerMethods.SendServerSave();
WorldTimer timer = new GameObject("World Timer", new[] { typeof(WorldTimer) }).GetComponent<WorldTimer>();
timer.StartSave(5);
}
}
}
[HarmonyPatch(typeof(GameManager), "LeaveGame")]
[HarmonyPostfix]
static void LeaveGameReset()
{
Debug.Log("RESETTING");
builds.Clear();
doSave = false;
LoadManager.players.Clear();
LoadManager.currentSave = "";
LoadManager.hasSaveLoaded = false;
LoadManager.serverHasSaveLoaded = false;
LoadManager.playersUpdated = false;
LoadManager.loadedSave = null;
CustomSaveData.customSaves = new SerializableDictionary<string, SerializableDictionary<string, object>>();
}
[HarmonyPatch(typeof(BuildManager), "BuildItem")]
static void Postfix(int buildOwner, int itemID, int objectId, Vector3 position, int yRotation)
{
if (LocalClient.serverOwner)
{
if (itemID == chest || itemID == furnace || itemID == cauldron || builds.ContainsKey(objectId))
{
return;
}
builds.Add(objectId, new BuildWrapper(itemID, position, yRotation));
}
}
[HarmonyPatch(typeof(ResourceManager), "RemoveItem")]
[HarmonyPostfix]
static void RemoveBuild(int id)
{
if (LocalClient.serverOwner)
{
if (builds.ContainsKey(id))
{
builds.Remove(id);
}
}
}
}
class WorldTimer : MonoBehaviour
{
private IEnumerator coroutine;
public void StartSave(int _time)
{
coroutine = SaveCoroutine(_time);
StartCoroutine(coroutine);
}
public void StartPlayerDeath(float _time)
{
Debug.Log("Starting Player Death");
coroutine = PlayerDeathCoroutine(_time);
Debug.Log("Player Death Started");
StartCoroutine(coroutine);
}
private IEnumerator PlayerDeathCoroutine(float _time)
{
Debug.Log("Player Death Started Coroutine");
yield return new WaitForSecondsRealtime(_time);
var flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
Debug.Log("PLAYER DYING");
typeof(PlayerStatus).GetMethod("PlayerDied", flags).Invoke(PlayerStatus.Instance, new object[] { 0, -1 });
Debug.Log("Player Died");
UnityEngine.Object.Destroy(this);
}
private IEnumerator SaveCoroutine(float _time)
{
yield return new WaitForSecondsRealtime(_time);
if (LoadManager.currentSave != "")
{
try
{
SaveSystem.Save(LoadManager.currentSave);
ClientSend.SendChatMessage($"<color=#ADD8E6>Save Completed! Seed: {World.worldSeed}");
ChatBox.Instance.AppendMessage(-1, $"<color=#ADD8E6>Save Completed! Seed: {World.worldSeed}", "");
}
catch (Exception ex)
{
Debug.LogError(ex);
ClientSend.SendChatMessage("<color=#FF0000>Save Failed!");
ChatBox.Instance.AppendMessage(-1, "<color=#FF0000>Save Failed", "");
}
}
UnityEngine.Object.Destroy(this);
}
}
}