forked from Peteys93/MCForge-MCLawl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AutoSaver.cs
115 lines (100 loc) · 3.71 KB
/
AutoSaver.cs
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
/*
Copyright 2010 MCSharp team (Modified for use with MCZall/MCLawl/MCForge)
Dual-licensed under the Educational Community License, Version 2.0 and
the GNU General Public License, Version 3 (the "Licenses"); you may
not use this file except in compliance with the Licenses. You may
obtain a copy of the Licenses at
http://www.opensource.org/licenses/ecl2.php
http://www.gnu.org/licenses/gpl-3.0.html
Unless required by applicable law or agreed to in writing,
software distributed under the Licenses are distributed on an "AS IS"
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
or implied. See the Licenses for the specific language governing
permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace MCForge
{
//I
public sealed class AutoSaver
{
static int _interval;
string backupPath = @Server.backupLocation;
static int count = 1;
public AutoSaver(int interval)
{
_interval = interval * 1000;
new Thread(new ThreadStart(delegate
{
while (true)
{
Thread.Sleep(_interval);
Server.ml.Queue(delegate { Run(); });
if (Player.players.Count <= 0) continue;
string allCount = Player.players.Aggregate("", (current, pl) => current + (", " + pl.name));
try { Server.s.Log("!PLAYERS ONLINE: " + allCount.Remove(0, 2), true); }
catch { }
allCount = Server.levels.Aggregate("", (current, l) => current + (", " + l.name));
try { Server.s.Log("!LEVELS ONLINE: " + allCount.Remove(0, 2), true); }
catch { }
}
})).Start();
}
/*
static void Exec()
{
Server.ml.Queue(delegate
{
Run();
});
}*/
public static void Run()
{
try
{
count--;
Server.levels.ForEach(delegate(Level l)
{
try
{
if (!l.changed) return;
if (Server.lava.active && Server.lava.HasMap(l.name)) { l.saveChanges(); return; }
l.Save();
if (count == 0)
{
int backupNumber = l.Backup();
if (backupNumber != -1)
{
l.ChatLevel("Backup " + backupNumber + " saved.");
Server.s.Log("Backup " + backupNumber + " saved for " + l.name);
}
}
}
catch
{
Server.s.Log("Backup for " + l.name + " has caused an error.");
}
});
if (count <= 0)
{
count = 15;
}
}
catch (Exception e) { Server.ErrorLog(e); }
try
{
if (Player.players.Count > 0)
{
List<Player> tempList = new List<Player>();
tempList.AddRange(Player.players);
foreach (Player p in tempList) { p.save(); }
tempList.Clear();
}
}
catch (Exception e) { Server.ErrorLog(e); }
}
}
}