-
Notifications
You must be signed in to change notification settings - Fork 4
/
Mods.cs
157 lines (150 loc) · 6.6 KB
/
Mods.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace modsync
{
class Mods
{
public static void Pull(ref FtpLib ftp)
{
// return if disabled
if (Config.settings.SyncFolders == "")
{
return;
}
// download changes on server to local
foreach (string folder in Config.settings.SyncFolders.Split(','))
{
Console.WriteLine(Strings.Get("SyncDown") + " " + folder + " ... ");
FTPSyncSettings ftpsyncsettings = new FTPSyncSettings();
if (folder == "config")
{
// special case for config folder
ftpsyncsettings.Recurse = false;
ftpsyncsettings.WhenMissingLocal = FTPAction.Copy;
ftpsyncsettings.WhenMissingRemote = FTPAction.Noop;
ftpsyncsettings.WhenNewerLocal = FTPAction.Noop;
ftpsyncsettings.WhenNewerRemote = FTPAction.Copy;
ftpsyncsettings.WhenLargerLocal = FTPAction.Noop;
ftpsyncsettings.WhenLargerRemote = FTPAction.Noop;
}
else if (folder == "cachedImages/skins")
{
// special case for cachedImages folder
ftpsyncsettings.Recurse = true;
ftpsyncsettings.WhenMissingLocal = FTPAction.Copy;
ftpsyncsettings.WhenMissingRemote = FTPAction.Copy;
ftpsyncsettings.WhenNewerLocal = FTPAction.Copy;
ftpsyncsettings.WhenNewerRemote = FTPAction.Copy;
ftpsyncsettings.WhenLargerLocal = FTPAction.Noop;
ftpsyncsettings.WhenLargerRemote = FTPAction.Noop;
// move all small png files from desktop to cachedImages folder
foreach (string src in Directory.GetFiles(Locations.LocalFolderName_Desktop))
{
if (src.EndsWith(".png"))
{
FileInfo src_file = new System.IO.FileInfo(src);
if (src_file.Length < 2048)
{
string dst = Locations.LocalFolderName_Minecraft + "\\cachedImages\\skins\\" + src_file.Name;
if (File.Exists(dst))
{
FileInfo dst_file = new System.IO.FileInfo(dst);
if (dst_file.LastWriteTime >= src_file.LastWriteTime)
{
continue;
}
}
try
{
File.Delete(dst);
src_file.CopyTo(dst);
Console.WriteLine("Uploading skin: " + src_file.Name);
}
catch (Exception)
{
Console.WriteLine("Error uploading skin: " + src_file.Name);
Console.ReadKey();
}
}
}
}
}
else
{
// other folders
ftpsyncsettings.Recurse = false;
ftpsyncsettings.WhenMissingLocal = FTPAction.Copy;
ftpsyncsettings.WhenMissingRemote = FTPAction.Delete;
ftpsyncsettings.WhenNewerLocal = FTPAction.Noop;
ftpsyncsettings.WhenNewerRemote = FTPAction.Noop;
ftpsyncsettings.WhenLargerLocal = FTPAction.Noop;
ftpsyncsettings.WhenLargerRemote = FTPAction.Noop;
}
Sync(ref ftp, folder, ftpsyncsettings);
}
}
public static void Push(ref FtpLib ftp)
{
// return if disabled
if (Config.settings.SyncFolders == "")
{
return;
}
if (Config.settings.SyncAllowUpload != "true")
{
Console.WriteLine(Strings.Get("SyncUpNotAllowed"));
Console.ReadKey();
return;
}
// upload changes in local folders to server
foreach (string folder in Config.settings.SyncFolders.Split(','))
{
Console.WriteLine(Strings.Get("SyncUp") + " " + folder + " ... ");
FTPSyncSettings ftpsyncsettings = new FTPSyncSettings();
if (folder == "config")
{
// skip push for config folder
continue;
}
else
{
// other folders
ftpsyncsettings.Recurse = false;
ftpsyncsettings.WhenMissingLocal = FTPAction.Delete;
ftpsyncsettings.WhenMissingRemote = FTPAction.Copy;
ftpsyncsettings.WhenNewerLocal = FTPAction.Noop;
ftpsyncsettings.WhenNewerRemote = FTPAction.Noop;
ftpsyncsettings.WhenLargerLocal = FTPAction.Noop;
ftpsyncsettings.WhenLargerRemote = FTPAction.Noop;
}
Sync(ref ftp, folder, ftpsyncsettings);
}
}
static bool Sync(ref FtpLib ftp, string folder, FTPSyncSettings ftpsyncsettings)
{
try
{
// make list of changes
string localFolder = Locations.LocalFolderName_Minecraft + "\\" + folder.Replace("/", "\\");
string remoteFolder = Config.ftpsettings.FtpServerFolder + "/" + folder;
List<FTPSyncModification> changes = ftp.CompareDirectory(localFolder, remoteFolder, ftpsyncsettings);
if (changes.Count > 0)
{
Console.WriteLine(Strings.Get("Syncing") + " " + folder + " ... ");
ftp.SynchronizeFiles(changes, false);
}
}
catch (Exception ex)
{
Console.WriteLine(Strings.Get("SyncError") + ex.Message);
Console.WriteLine(Strings.Get("PressKey"));
Console.ReadKey();
return false;
}
return true;
}
}
}