-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameSaveTimerMod.cs
More file actions
123 lines (108 loc) · 4.06 KB
/
Copy pathGameSaveTimerMod.cs
File metadata and controls
123 lines (108 loc) · 4.06 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
using System;
using UnityEngine;
using MSCLoader;
namespace TommoJProductions.GameSaveTimerMod
{
public class GameSaveTimerMod : Mod
{
// Written, 11.05.2019
public override string ID => "GameSaveTimer";
public override string Name => "Game Save Timer";
public override string Version => "0.1";
public override string Author => "tommojphillips";
/// <summary>
/// Represents the save file name.
/// </summary>
private const string SAVE_FILE_NAME = "gamesavetimermod.txt";
/// <summary>
/// Represents the save data.
/// </summary>
private GameSaveTimerSaveData timerSaveData;
/// <summary>
/// Represents if the game is saving.
/// </summary>
private bool saving = false;
public override void OnLoad()
{
// Written, 11.05.2019
this.timerSaveData = this.LoadData();
if (this.timerSaveData is null)
{
this.timerSaveData = new GameSaveTimerSaveData()
{
startedFromNewGame = false
};
}
GameSaveTimerSaveData.gameStartTime = this.timerSaveData.timePassed;
ModConsole.Print(this.Name + ": Loaded");
}
public override void OnNewGame()
{
// Written, 11.05.2019
this.timerSaveData = new GameSaveTimerSaveData()
{
startedFromNewGame = true
};
SaveLoad.SerializeSaveFile(this, this.timerSaveData, SAVE_FILE_NAME);
}
public override void OnSave()
{
// Written, 11.05.2019
this.saveData();
}
public override void Update()
{
// Written, 11.05.2019
if (!saving)
this.updateTime();
}
public override void OnGUI()
{
// Written, 11.05.2019
string _message;
if (!saving)
{
string _sessionMessage = this.timerSaveData.nextSession == 1 ? "First session" : String.Format("<b>#{0}</b> {1}", this.timerSaveData.nextSession, TimeSpan.FromSeconds(GameSaveTimerSaveData.timePassedSession).ToString());
if (this.timerSaveData.startedFromNewGame)
_sessionMessage += " | <color= green>started from new game</color>";
_message = string.Format("<color=white>Time Passed: {0}\nSession: {1}</color>", TimeSpan.FromSeconds(this.timerSaveData.timePassed).ToString(), _sessionMessage);
}
else
_message = String.Format("Session finished at, {0}\nTotal Time, {1}", TimeSpan.FromSeconds(GameSaveTimerSaveData.timePassedSession).ToString(), TimeSpan.FromSeconds(this.timerSaveData.timePassed).ToString());
GUI.Label(new Rect(5, 5, 100, 25), _message, new GUIStyle() { fontSize = 18 });
}
private void saveData()
{
// Written, 11.05.2019
try
{
this.saving = true;
this.updateTime();
this.timerSaveData.nextSession++;
SaveLoad.SerializeSaveFile(this, this.timerSaveData, SAVE_FILE_NAME);
}
catch
{
ModConsole.Error("<b>[GameSaveTimerMod]</b> - an error occured while attempting to save info..");
}
}
private void updateTime()
{
// Written, 11.05.2019
this.timerSaveData.timePassed = float.Parse(Math.Round(Time.timeSinceLevelLoad + GameSaveTimerSaveData.gameStartTime).ToString());
GameSaveTimerSaveData.timePassedSession = float.Parse(Math.Round(Time.timeSinceLevelLoad).ToString());
}
private GameSaveTimerSaveData LoadData()
{
// Written, 11.05.2019
try
{
return SaveLoad.DeserializeSaveFile<GameSaveTimerSaveData>(this, SAVE_FILE_NAME);
}
catch
{
return null;
}
}
}
}