-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArmALauncher.cs
156 lines (130 loc) · 5.36 KB
/
ArmALauncher.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using BSU.Sync.FileTypes.BI;
using Newtonsoft.Json;
using NLog;
using Formatting = Newtonsoft.Json.Formatting;
namespace BSU.Sync
{
public static class ArmALauncher
{
internal static Logger Logger = LogManager.GetCurrentClassLogger();
/// <summary>
///
/// </summary>
public static string LauncherDirectory => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "ArmA 3 Launcher");
/// <summary>
///
/// </summary>
public static string PresetDirectory => Path.Combine(LauncherDirectory, "Presets");
/// <summary>
///
/// </summary>
/// <param name="baseDirectory"></param>
/// <param name="modFolders"></param>
/// <returns></returns>
public static string GeneratePreset(string baseDirectory, List<ModFolder> modFolders, List<string> dlcs)
{
Local local = ReadLocal() ?? new Local
{
DateCreated = DateTime.Now,
AutodetectionDirectories = new List<string>(),
KnownLocalMods = new List<string>(),
UserDirectories = new List<string>()
};
var preset = new Preset2
{
LastUpdated = DateTime.UtcNow,
PublishedId = new List<string>(),
DlcIds = dlcs
};
foreach (ModFolder mf in modFolders)
{
preset.PublishedId.Add($"local:{Path.Combine(baseDirectory, mf.ModName).ToUpper()}");
}
var xmlSerializer = new XmlSerializer(typeof(Preset2));
using (var sw = new StringWriterWithEncoding(Encoding.UTF8))
{
xmlSerializer.Serialize(sw,preset);
return sw.ToString();
}
}
// Borrowed from https://stackoverflow.com/a/32259619
private sealed class StringWriterWithEncoding : System.IO.StringWriter
{
public StringWriterWithEncoding(System.Text.StringBuilder sb) : base(sb)
{
Encoding = System.Text.Encoding.Unicode;
}
public StringWriterWithEncoding(System.Text.Encoding encoding)
{
Encoding = encoding;
}
public StringWriterWithEncoding(System.Text.StringBuilder sb, System.Text.Encoding encoding) : base(sb)
{
Encoding = encoding;
}
public override System.Text.Encoding Encoding { get; }
}
public static void WritePreset(string preset, string name)
{
Logger.Info($"Writing launcher preset to {Path.Combine(PresetDirectory, $"{name}.preset2")}");
File.WriteAllText(Path.Combine(PresetDirectory,$"{name}.preset2"),preset);
}
public static FileTypes.BI.Local ReadLocal()
{
Logger.Info($"Reading local.json from {Path.Combine(LauncherDirectory, "Local.json")}");
if (!File.Exists(Path.Combine(LauncherDirectory, "Local.json")))
{
Logger.Warn("Local.json does not exist");
return null;
}
string json = File.ReadAllText(Path.Combine(LauncherDirectory, "Local.json"));
return JsonConvert.DeserializeObject<Local>(json);
}
public static Local UpdateLocal(List<ModFolder> modFolders, Local local, string baseDirectory)
{
Logger.Info("Updating local.json");
if (local == null)
{
local = new Local
{
DateCreated = DateTime.Now,
AutodetectionDirectories = new List<string>(),
KnownLocalMods = new List<string>(),
UserDirectories = new List<string>()
};
}
foreach (ModFolder mf in modFolders)
{
if (!local.KnownLocalMods.Exists(x => x.ToLower() == Path.Combine(baseDirectory, mf.ModName)))
{
local.KnownLocalMods.Add(Path.Combine(baseDirectory, mf.ModName));
}
if (!local.UserDirectories.Exists(x => x.ToLower() == Path.Combine(baseDirectory, mf.ModName)))
{
local.UserDirectories.Add(Path.Combine(baseDirectory, mf.ModName));
}
}
local.KnownLocalMods = local.KnownLocalMods.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
local.UserDirectories = local.UserDirectories.Distinct(StringComparer.InvariantCultureIgnoreCase).ToList();
return local;
}
public static void WriteLocal(FileTypes.BI.Local local)
{
JsonSerializerSettings settings = new JsonSerializerSettings()
{
DateFormatHandling = DateFormatHandling.MicrosoftDateFormat
};
string json = JsonConvert.SerializeObject(local, Formatting.None, settings);
File.WriteAllText(Path.Combine(LauncherDirectory,"Local.json"),json);
Logger.Info($"Writing local.json to {Path.Combine(LauncherDirectory, "Local.json")}");
}
}
}