This repository was archived by the owner on Jun 15, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
137 lines (118 loc) · 3.99 KB
/
Copy pathProgram.cs
File metadata and controls
137 lines (118 loc) · 3.99 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
using Npgsql;
using System;
using System.Xml.Linq;
namespace EZ2ScoreShare
{
internal class Program
{
public static List<ScoreData> LocalScores;
public static List<ScoreData> RemoteScores;
public static string DefaultName;
static void Main(string[] args)
{
Console.WriteLine("EZ2ScoreShare - R U READY 2 SHARE SUM SCORES?");
Console.WriteLine();
if (args.Length < 4)
{
Console.WriteLine("USAGE:\n" +
"\tEZ2ScoreShare.exe {server} {user} {password} {database} {table} {defaultName=\"EZ2AC_FN\"}\n" +
"\tCLI only.");
Console.Read();
return;
}
LocalScores = new List<ScoreData>();
RemoteScores = new List<ScoreData>();
if (args.Length < 6) DefaultName = "EZ2AC_FN";
else DefaultName = args[5];
if(Directory.Exists(AppContext.BaseDirectory + "Sound"))
{
Console.WriteLine(AppContext.BaseDirectory + "Sound");
foreach (string path in Directory.GetFiles(AppContext.BaseDirectory + "Sound", "rank_*.bin"))
{
LocalScores.Add(new ScoreData(path));
}
Console.WriteLine($"Local rankings: {LocalScores.Count}");
}
else
{
Console.WriteLine("Can't find Sound folder! Make sure you are starting EZ2ScoreShare from game's root directory.");
return;
}
string connectionString = $"server={args[0]};uid={args[1]};pwd={args[2]};database={args[3]}";
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connectionString);
var dataSource = dataSourceBuilder.Build();
Console.WriteLine("Fetching rankings from server....");
try
{
var conn = dataSource.OpenConnection();
List<string> remoteFilenames = new List<string>();
using (var cmd = new NpgsqlCommand($"SELECT * FROM \"{args[4]}\";", conn))
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
remoteFilenames.Add(reader.GetString(0));
}
List<string> uniqueFilenames = new List<string>();
List<ScoreData.ScoreEntry> entries = new List<ScoreData.ScoreEntry>();
foreach (string f in remoteFilenames)
if (!uniqueFilenames.Contains(f)) uniqueFilenames.Add(f);
foreach (string f in uniqueFilenames)
{
entries.Clear();
using (var cmd = new NpgsqlCommand($"SELECT * FROM \"{args[4]}\" WHERE filename='{f}';", conn))
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
entries.Add(new ScoreData.ScoreEntry(reader.GetString(1), reader.GetInt32(2)));
}
}
RemoteScores.Add(new ScoreData(f, entries.ToArray()));
}
Console.WriteLine("ScoreShare found these rankings on server:");
foreach(ScoreData data in RemoteScores)
{
Console.WriteLine($"\t> {data.Filename} ({data.Scores.Length} score(-s))");
}
Console.WriteLine("Uploading local scores....");
foreach (ScoreData data in LocalScores)
{
foreach (ScoreData.ScoreEntry e in data.Scores)
{
if (e.Score > 0)
{
using (var cmd = new NpgsqlCommand($"INSERT INTO \"{args[4]}\" (filename, player, score) VALUES(@f, @p, @s) ON CONFLICT DO NOTHING;", conn))
{
cmd.Parameters.AddWithValue("f", data.Filename.ToLower());
cmd.Parameters.AddWithValue("p", e.Player);
cmd.Parameters.AddWithValue("s", e.Score);
cmd.ExecuteNonQuery();
}
}
}
}
Console.WriteLine("Updating local scores....");
List<ScoreData> mergedScores = new List<ScoreData>();
foreach(ScoreData remote in RemoteScores)
{
ScoreData? local = LocalScores.Find((x)=> x.Filename == remote.Filename.ToLower());
if (local != null)
mergedScores.Add(ScoreData.MergeScores(local, remote));
else
mergedScores.Add(remote);
}
foreach(ScoreData data in mergedScores)
{
File.WriteAllBytes(AppContext.BaseDirectory + "Sound\\" + data.Filename + ".bin", data.GetBytes());
}
Console.WriteLine($"Successfully written {mergedScores.Count} rankings!");
}
catch (Exception ex)
{
Console.WriteLine($"An exception has occurred: {ex}");
Console.Read();
return;
}
}
}
}