-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathwaysManager.cs
More file actions
318 lines (270 loc) · 11.4 KB
/
PathwaysManager.cs
File metadata and controls
318 lines (270 loc) · 11.4 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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
namespace Pathways
{
[DefaultExecutionOrder(-1000)]
public class PathwaysManager : MonoBehaviour
{
private static PathwaysManager instance;
public static PathwaysManager Instance
{
get
{
if (instance == null)
{
instance = FindFirstObjectByType<PathwaysManager>();
if (instance == null)
{
var go = new GameObject(nameof(PathwaysManager) + " (Auto Created)");
instance = go.AddComponent<PathwaysManager>();
}
}
return instance;
}
}
/// <summary>
/// Event triggered when an auto-save path is requested. Provides the path for the auto-save file.
/// </summary>
public event Action<string> OnAutoSavePathRequested;
/// <summary>
/// Event triggered when the current pathway changes. Provides the new pathway.
/// </summary>
public event Action<Pathway> OnCurrentPathwayChanged;
public string StorageLocation
{
get => PathwaysGlobalConfigs.StorageLocation;
set
{
if (string.IsNullOrEmpty(value))
throw new ArgumentException("Storage location cannot be null or empty.");
PathwaysGlobalConfigs.StorageLocation = value;
Refresh();
}
}
public bool IsAutoSaveEnabled { get; private set; }
public int AutoSaveSlots
{
get => PathwaysGlobalConfigs.AutoSaveSlots;
private set => PathwaysGlobalConfigs.AutoSaveSlots = value;
}
public float AutoSaveInterval { get; private set; }
public bool UseUnscaledTime { get; private set; }
public Pathway CurrentPathway { get; private set; }
private readonly Dictionary<string, Pathway> loadedPathways = new();
private float autoSaveTimer;
private void Awake()
{
if (instance != null && instance != this)
{
Destroy(gameObject);
return;
}
instance = this;
DontDestroyOnLoad(gameObject);
Refresh();
}
private void Update()
{
if (CurrentPathway == null)
return;
if (CanAutoSave())
{
autoSaveTimer += UseUnscaledTime ? Time.unscaledDeltaTime : Time.deltaTime;
if (autoSaveTimer >= AutoSaveInterval)
{
RequestAutoSavePath();
autoSaveTimer = 0f;
}
}
}
/// <summary>
/// Sets the storage location for all the pathways. Will trigger a refresh of the pathways.
/// </summary>
/// <param name="location">New storage location path.</param>
public void SetStorageLocation(string location) => StorageLocation = location;
/// <summary>
/// Creates or loads a pathway with the given ID. By default, the loaded/created pathway becomes the current pathway.
/// </summary>
/// <param name="pathwayId">The pathway identifier (directory name).</param>
/// <param name="setCurrent">If true, sets this pathway as the current pathway.</param>
/// <returns>The Pathway instance.</returns>
public Pathway CreateOrLoadPathway(string pathwayId, bool setCurrent = true)
{
if (loadedPathways.TryGetValue(pathwayId, out Pathway existingPathway))
{
if (setCurrent)
SetCurrentPathway(existingPathway);
return existingPathway;
}
var pathway = new Pathway(pathwayId);
loadedPathways[pathwayId] = pathway;
if (setCurrent)
SetCurrentPathway(pathway);
return pathway;
}
/// <summary>
/// Checks if auto-saving is enabled and if the current pathway is valid for auto-saving.
/// Checks if auto-save slots and interval are set correctly (greater than 0).
/// </summary>
/// <returns>True if auto-saving can proceed, false otherwise.</returns>
public bool CanAutoSave() =>
IsAutoSaveEnabled && CurrentPathway != null && AutoSaveSlots > 0 && AutoSaveInterval > 0f;
/// <summary>
/// Toggles auto-saving for the current pathway.
/// </summary>
/// <param name="enable">To enable or disable auto-saving.</param>
public void ToggleAutoSave(bool enable) => IsAutoSaveEnabled = enable;
/// <summary>
/// Sets whether auto-saving is enabled for the current pathway.
/// </summary>
/// <param name="enable">To enable or disable auto-saving.</param>
/// <param name="slots">Number of auto-save slots to use. Defaults to 3.</param>
/// <param name="interval">Auto-save interval in seconds. Defaults to 300 seconds (5 minutes).</param>
public void ToggleAutoSave(bool enable, int slots = 3, float interval = 300f)
{
IsAutoSaveEnabled = enable;
AutoSaveSlots = slots;
AutoSaveInterval = interval;
}
/// <summary>
/// Restarts the auto-save timer.
/// </summary>
public void RestartAutoSaveTimer() => autoSaveTimer = 0f;
/// <summary>
/// Sets whether to use unscaled time for auto-saving.
/// </summary>
/// <param name="useUnscaled">If true, uses unscaled time for auto-saving; otherwise, uses scaled time.</param>
public void SetTimescale(bool useUnscaled) => UseUnscaledTime = useUnscaled;
/// <summary>
/// Selects and sets the most recent (last saved to) pathway as the current pathway.
/// If no recent pathway exists, returns null.
/// </summary>
/// <returns>The most recent Pathway instance, or null if none found.</returns>
public Pathway SelectRecentPathway()
{
Pathway recentPathway = loadedPathways
.OrderByDescending(kvp => kvp.Value.RecentFile?.LastWriteTime)
.FirstOrDefault()
.Value;
if (recentPathway == null)
return null;
return SetCurrentPathway(recentPathway);
}
/// <summary>
/// Sets the current pathway, creating a new one if not found within already loaded pathways.
/// </summary>
/// <param name="pathwayId">The ID of the pathway to make current.</param>
public Pathway SetCurrentPathway(string pathwayId)
{
Pathway pathway = CreateOrLoadPathway(pathwayId);
SetCurrentPathway(pathway);
return pathway;
}
/// <summary>
/// Sets the current pathway.
/// </summary>
/// <param name="pathway">The pathway to make current.</param>
private Pathway SetCurrentPathway(Pathway pathway)
{
CurrentPathway = pathway;
autoSaveTimer = 0f;
OnCurrentPathwayChanged?.Invoke(CurrentPathway);
return CurrentPathway;
}
/// <summary>
/// Gets all available pathway ids.
/// </summary>
/// <returns>Array of pathway ids (directory names).</returns>
public string[] GetAllPathwayIds()
{
var storageDir = new DirectoryInfo(StorageLocation);
if (!storageDir.Exists)
return new string[0];
return storageDir.GetDirectories().Select(d => d.Name).ToArray();
}
/// <summary>
/// Gets the path of the most recent save file in the current pathway. If no save file exists, it generates a new manual save path.
/// Ideal for a "Save" button that overwrites the last save or creates a new one.
/// </summary>
/// <returns>A valid save path, or null if no pathway is current.</returns>
public string GetOrCreateRecentSavePath(string fileName = null)
{
if (CurrentPathway == null)
return null;
if (!string.IsNullOrEmpty(fileName))
return CurrentPathway.GetSavePath(fileName);
return CurrentPathway.GetRecentSavePath() ?? CurrentPathway.GetSavePath();
}
/// <summary>
/// Gets the path for a manual save in the current pathway.
/// </summary>
/// <param name="fileName">Optional filename, defaults to timestamp-based name.</param>
/// <returns>Full path for the save file, or null if no pathway is current.</returns>
public string GetManualSavePath(string fileName) => CurrentPathway?.GetSavePath(fileName);
/// <summary>
/// Gets the path for an auto-save in the current pathway.
/// </summary>
/// <returns>Full path for the auto-save file, or null if no pathway is current.</returns>
public string GetAutoSavePath() => CurrentPathway?.GetAutoSavePath();
/// <summary>
/// Requests the path for an auto-save and notifies subscribers.
/// </summary>
/// <returns>The auto-save path, or null if no pathway is current.</returns>
public string RequestAutoSavePath()
{
if (CurrentPathway == null)
return null;
string path = CurrentPathway.GetAutoSavePath();
OnAutoSavePathRequested?.Invoke(path);
RefreshCurrentPathway();
return path;
}
/// <summary>
/// Deletes the current pathway. If successful, refreshes the pathways list.
/// </summary>
/// <returns>Whether the pathway was deleted.</returns>
public bool DeleteCurrentPathway()
{
bool success = CurrentPathway?.Delete() ?? false;
if (success)
Refresh();
return success;
}
/// <summary>
/// Deletes a specific file within the current pathway. If successful, refreshes the current pathway.
/// </summary>
/// <param name="fileName"></param>
/// <returns>Whether the file within the current pathway was deleted.</returns>
public bool DeleteFile(string fileName)
{
bool success = CurrentPathway?.DeleteFile(fileName) ?? false;
if (success)
RefreshCurrentPathway();
return success;
}
/// <summary>
/// Refreshes the file list for the current pathway.
/// </summary>
public void RefreshCurrentPathway() => CurrentPathway?.Refresh();
/// <summary>
/// Searches through <see cref="StorageLocation"/> to get all pathways.
/// </summary>
public void Refresh()
{
loadedPathways.Clear();
CurrentPathway = null;
string[] pathwayIds = GetAllPathwayIds();
foreach (var pathwayId in pathwayIds)
CreateOrLoadPathway(pathwayId);
}
/// <summary>
/// Gets all loaded pathways. Use <see cref="Refresh"/> to update the list.
/// </summary>
/// <returns>Array of Pathway instances.</returns>
public Pathway[] GetAllPathways() => loadedPathways.Values.ToArray();
private void OnApplicationQuit() => instance = null;
}
}