-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameDataManager.cs
More file actions
80 lines (69 loc) · 2.22 KB
/
Copy pathGameDataManager.cs
File metadata and controls
80 lines (69 loc) · 2.22 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
using System;
using System.IO;
using System.Text.Json;
namespace GoingPostal;
public static class GameDataManager
{
public static GameData GameData {get; set;}
private static string GetSavePath()
{
// Shranimo v mapo za lokalne aplikacijske podatke
string folder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string gameFolder = Path.Combine(folder, "GoingPostal");
// Ustvari mapo, če še ne obstaja
if (!Directory.Exists(gameFolder)) Directory.CreateDirectory(gameFolder);
return Path.Combine(gameFolder, "data.json");
}
public static void Save()
{
try
{
string path = GetSavePath();
// Nastavitve za lepši izpis (Indented)
var options = new JsonSerializerOptions { WriteIndented = true };
string jsonString = JsonSerializer.Serialize(GameData, options);
File.WriteAllText(path, jsonString);
}
catch (Exception ex)
{
Console.WriteLine($"Napaka pri shranjevanju: {ex.Message}");
}
}
public static void DeleteSaveFile()
{
string path = GetSavePath();
try
{
// Preverimo, če datoteka sploh obstaja, preden jo brišemo
if (File.Exists(path))
{
File.Delete(path);
Console.WriteLine("Datoteka uspešno izbrisana.");
}
else
{
Console.WriteLine("Datoteke ni bilo mogoče najti, zato brisanje ni potrebno.");
}
}
catch (Exception ex)
{
// Do napake lahko pride, če je datoteka zaklenjena s strani drugega procesa
Console.WriteLine($"Napaka pri brisanju: {ex.Message}");
}
}
public static GameData Load()
{
string path = GetSavePath();
if (!File.Exists(path)) return new();
try
{
string jsonString = File.ReadAllText(path);
// Deserializiramo nazaj v slovar
return JsonSerializer.Deserialize<GameData>(jsonString);
}
catch
{
return new();
}
}
}