-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
225 lines (191 loc) · 6.8 KB
/
Program.cs
File metadata and controls
225 lines (191 loc) · 6.8 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
namespace ResourceManagement
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("=== Тест 1: Корректное использование ===");
using (var session = new GameSession())
{
session.Run();
}
Console.WriteLine("\n=== Тест 2: Проверка автосохранения ===");
if (File.Exists("saves/autosave.sav"))
Console.WriteLine("Файл сохранения создан");
if (File.Exists("session_log.txt"))
Console.WriteLine("Файл лога создан");
Console.WriteLine("\n=== Тест 3: Обработка ошибок ===");
try
{
var badSaver = new GameSaver(new GameState());
badSaver.SaveToFile("Z:\\несуществующая_папка\\save.sav");
}
catch (Exception ex)
{
Console.WriteLine($"Ошибка перехвачена: {ex.Message}");
}
Console.WriteLine("\n=== Тест 4: Забытый логгер (сработает финализатор) ===");
CreateLoggerWithoutDispose();
GC.Collect();
GC.WaitForPendingFinalizers();
Console.WriteLine("Программа завершена. Проверь файлы лога.");
}
static void CreateLoggerWithoutDispose()
{
var logger = new GameLogger();
logger.Log("Это сообщение может не сохраниться!");
}
}
public class GameLogger : IDisposable
{
private StreamWriter _writer;
private bool _disposed = false;
public GameLogger()
{
_writer = new StreamWriter("session_log.txt", true);
_writer.AutoFlush = true;
}
public void Log(string message)
{
if (_disposed) throw new ObjectDisposedException(nameof(GameLogger));
_writer?.WriteLine($"[{DateTime.Now}] {message}");
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
_writer?.Dispose();
}
_disposed = true;
}
}
~GameLogger()
{
Dispose(false);
Console.WriteLine("[!] Логгер не был явно закрыт! Данные могли потеряться.");
}
}
public class GameSaver
{
private GameState _gameState;
private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions
{
WriteIndented = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
IncludeFields = true,
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
public GameSaver(GameState gameState)
{
_gameState = gameState;
}
public void SaveToFile(string filePath)
{
if (string.IsNullOrWhiteSpace(filePath))
throw new ArgumentException("Путь к файлу не может быть пустым", nameof(filePath));
string tempFile = filePath + ".tmp";
string directory = Path.GetDirectoryName(filePath);
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
Directory.CreateDirectory(directory);
try
{
using (var writer = new StreamWriter(tempFile))
{
string json = JsonSerializer.Serialize(_gameState, _jsonOptions);
writer.Write(json);
}
if (File.Exists(filePath))
File.Delete(filePath);
File.Move(tempFile, filePath);
Console.WriteLine($"[Сохранение] Игра сохранена в {filePath}");
}
catch (Exception ex)
{
if (File.Exists(tempFile))
File.Delete(tempFile);
Console.WriteLine($"[Ошибка сохранения] {ex.Message}");
throw;
}
}
}
public class GameState
{
public int Level;
public int Health;
public float PositionX;
public float PositionY;
public List<string> Inventory;
public GameState()
{
Inventory = new List<string>();
}
public void Update()
{
Health += 10;
Level++;
PositionX += 1.5f;
if (Inventory.Count == 0)
Inventory.Add("Меч");
else
Inventory.Add($"Зелье #{Inventory.Count}");
}
}
public class GameSession : IDisposable
{
private GameLogger _logger;
private GameState _state;
private GameSaver _saver;
private bool _disposed = false;
public GameSession()
{
_logger = new GameLogger();
_state = new GameState();
_saver = new GameSaver(_state);
_logger.Log("Сессия начата");
}
public void Run()
{
try
{
for (int i = 1; i <= 10; i++)
{
_state.Update();
_logger.Log($"Шаг {i}: Здоровье {_state.Health}, Позиция ({_state.PositionX}, {_state.PositionY})");
if (i % 3 == 0)
{
_saver.SaveToFile("saves/autosave.sav");
}
Thread.Sleep(500);
}
}
catch (Exception ex)
{
_logger.Log($"Ошибка в сессии: {ex.Message}");
throw;
}
}
public void Dispose()
{
if (!_disposed)
{
_logger?.Log("Сессия завершена");
_logger?.Dispose();
_disposed = true;
}
}
}
}