Skip to content

Commit e4a8ec3

Browse files
author
John
committed
Massively overhauled importing/exporting/transferring code, should be way more readable.
1 parent 7428c8f commit e4a8ec3

File tree

5 files changed

+185
-198
lines changed

5 files changed

+185
-198
lines changed

DoomEternal.cs

Lines changed: 3 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -23,139 +23,20 @@ public class DoomEternal
2323
public static void EnumerateSaves() {
2424
Saves = new DoomEternalSavePathCollection();
2525
if (Directory.Exists(BnetSavePath)) {
26-
Saves.Add(new DoomEternalSavePath("savegame.unencrypted", BnetSavePathUnencrypted, DoomEternalSavePlatform.BethesdaNet));
26+
Saves.Add(new DoomEternalSavePath("savegame.unencrypted", DoomEternalSavePlatform.BethesdaNet, false));
2727
foreach (var single in Directory.GetDirectories(BnetSavePath, "*.*", SearchOption.TopDirectoryOnly)) {
2828
if (Utilities.CheckUUID(Path.GetFileNameWithoutExtension(single)))
29-
Saves.Add(new DoomEternalSavePath(Path.GetFileNameWithoutExtension(single), BnetSavePath, DoomEternalSavePlatform.BethesdaNet));
29+
Saves.Add(new DoomEternalSavePath(Path.GetFileNameWithoutExtension(single), DoomEternalSavePlatform.BethesdaNet));
3030
}
3131
}
3232
if (Directory.Exists(SteamSavePath)) {
3333
foreach (var steamId3 in Directory.GetDirectories(SteamSavePath, "*.*", SearchOption.TopDirectoryOnly)) {
34-
Console.WriteLine(Path.GetFileNameWithoutExtension(steamId3));
3534
foreach (var single in Directory.GetDirectories(steamId3, "*.*", SearchOption.TopDirectoryOnly)) {
3635
if (Path.GetFileNameWithoutExtension(single) == SteamGameID.ToString())
37-
Saves.Add(new DoomEternalSavePath(Utilities.Id3ToId64(Path.GetFileNameWithoutExtension(steamId3)), SteamSavePath, DoomEternalSavePlatform.Steam));
36+
Saves.Add(new DoomEternalSavePath(Utilities.Id3ToId64(Path.GetFileNameWithoutExtension(steamId3)), DoomEternalSavePlatform.Steam));
3837
}
3938
}
4039
}
4140
}
42-
43-
#region Bethesda.net
44-
public static void BnetFileEncrypt(string fromFile, string toFile, string toUUID) {
45-
if (fromFile.EndsWith("-BACKUP"))
46-
return;
47-
byte[] fromFileData = File.ReadAllBytes(fromFile);
48-
Directory.CreateDirectory(Path.GetDirectoryName(toFile));
49-
File.WriteAllBytes(toFile, Crypto.EncryptAndDigest($"{toUUID}PAINELEMENTAL{Path.GetFileName(toFile)}", fromFileData));
50-
}
51-
52-
public static void BnetFileDecrypt(string fromFile, string fromUUID, string toFile) {
53-
if (fromFile.EndsWith("-BACKUP"))
54-
return;
55-
byte[] fromFileData = Crypto.DecryptAndVerify($"{fromUUID}PAINELEMENTAL{Path.GetFileName(fromFile)}", File.ReadAllBytes(fromFile));
56-
Directory.CreateDirectory(Path.GetDirectoryName(toFile));
57-
File.WriteAllBytes(toFile, fromFileData);
58-
}
59-
60-
public static void BnetFileTransfer(string fromFile, string fromUUID, string toFile, string toUUID) {
61-
if (fromFile.EndsWith("-BACKUP"))
62-
return;
63-
byte[] fromFileData = Crypto.DecryptAndVerify($"{fromUUID}PAINELEMENTAL{Path.GetFileName(fromFile)}", File.ReadAllBytes(fromFile));
64-
Directory.CreateDirectory(Path.GetDirectoryName(toFile));
65-
File.WriteAllBytes(toFile, Crypto.EncryptAndDigest($"{toUUID}PAINELEMENTAL{Path.GetFileName(toFile)}", fromFileData));
66-
}
67-
68-
public static void BnetBulkTransfer(string fromUUID, string toUUID) {
69-
string fromDir = Path.Combine(BnetSavePath, fromUUID);
70-
foreach(var single in Directory.GetFiles(fromDir, "*.*", SearchOption.AllDirectories)) {
71-
BnetFileTransfer(single, fromUUID, single.Replace(fromUUID, toUUID), toUUID);
72-
}
73-
}
74-
75-
public static void BnetBulkEncrypt(string fromDir, string toUUID) {
76-
string toDir = Path.Combine(BnetSavePath, toUUID);
77-
foreach (var single in Directory.GetFiles(fromDir, "*.*", SearchOption.AllDirectories)) {
78-
BnetFileEncrypt(single, Path.Combine(toDir, single.Replace(fromDir, "").Substring(1)), toUUID);
79-
}
80-
}
81-
82-
public static void BnetBulkDecrypt(string fromUUID, string toDir) {
83-
string fromDir = Path.Combine(BnetSavePath, fromUUID);
84-
foreach (var single in Directory.GetFiles(fromDir, "*.*", SearchOption.AllDirectories)) {
85-
BnetFileDecrypt(single, fromUUID, Path.Combine(toDir, single.Replace(Path.Combine(BnetSavePath, fromUUID), "").Substring(1)));
86-
}
87-
}
88-
#endregion
89-
90-
#region Steam
91-
public static void SteamFileEncrypt(string fromFile, string toFile, string toId) {
92-
//toId = Utilities.Id64ToId3(toId);
93-
if (fromFile.EndsWith("-BACKUP"))
94-
return;
95-
byte[] fromFileData = File.ReadAllBytes(fromFile);
96-
Directory.CreateDirectory(Path.GetDirectoryName(toFile));
97-
File.WriteAllBytes(toFile, Crypto.EncryptAndDigest($"{toId}MANCUBUS{Path.GetFileName(toFile)}", fromFileData));
98-
}
99-
100-
public static void SteamFileDecrypt(string fromFile, string fromId, string toFile) {
101-
//fromId = Utilities.Id64ToId3(fromId);
102-
if (fromFile.EndsWith("-BACKUP"))
103-
return;
104-
byte[] fromFileData = Crypto.DecryptAndVerify($"{fromId}MANCUBUS{Path.GetFileName(fromFile)}", File.ReadAllBytes(fromFile));
105-
Directory.CreateDirectory(Path.GetDirectoryName(toFile));
106-
File.WriteAllBytes(toFile, fromFileData);
107-
}
108-
109-
public static void SteamFileTransfer(string fromFile, string fromId, string toFile, string toId) {
110-
fromId = Utilities.Id64ToId3(fromId);
111-
toId = Utilities.Id64ToId3(toId);
112-
if (fromFile.EndsWith("-BACKUP"))
113-
return;
114-
byte[] fromFileData = Crypto.DecryptAndVerify($"{Utilities.Id3ToId64(fromId)}MANCUBUS{Path.GetFileName(fromFile)}", File.ReadAllBytes(fromFile));
115-
Directory.CreateDirectory(Path.GetDirectoryName(toFile));
116-
File.WriteAllBytes(toFile, Crypto.EncryptAndDigest($"{Utilities.Id3ToId64(toId)}MANCUBUS{Path.GetFileName(toFile)}", fromFileData));
117-
}
118-
119-
public static void SteamBulkTransfer(string fromId, string toId) {
120-
fromId = Utilities.Id64ToId3(fromId);
121-
toId = Utilities.Id64ToId3(toId);
122-
string fromDir = Path.Combine(BnetSavePath, fromId);
123-
foreach (var single in Directory.GetFiles(fromDir, "*.*", SearchOption.AllDirectories)) {
124-
SteamFileTransfer(single, fromId, single.Replace(fromId, toId), toId);
125-
}
126-
}
127-
128-
public static void SteamBulkEncrypt(string fromDir, string toId) {
129-
toId = Utilities.Id64ToId3(toId);
130-
string toDir = Path.Combine(SteamSavePath, toId, SteamGameID.ToString(), "remote");
131-
foreach (var single in Directory.GetFiles(fromDir, "*.*", SearchOption.AllDirectories)) {
132-
SteamFileEncrypt(single, Path.Combine(toDir, single.Replace(fromDir, "").Substring(1)), Utilities.Id3ToId64(toId));
133-
}
134-
}
135-
136-
public static void SteamBulkDecrypt(string fromId, string toDir) {
137-
fromId = Utilities.Id64ToId3(fromId);
138-
string fromDir = Path.Combine(SteamSavePath, fromId, SteamGameID.ToString(), "remote");
139-
foreach (var single in Directory.GetFiles(fromDir, "*.*", SearchOption.AllDirectories)) {
140-
SteamFileDecrypt(single, Utilities.Id3ToId64(fromId), Path.Combine(toDir, single.Replace(Path.Combine(SteamSavePath, fromId, SteamGameID.ToString(), "remote"), "").Substring(1)));
141-
}
142-
}
143-
#endregion
144-
145-
#region Both
146-
public static void BnetToSteamTransfer(string fromId, string toId) {
147-
Directory.CreateDirectory("tmp");
148-
BnetBulkDecrypt(fromId, "tmp");
149-
SteamBulkEncrypt("tmp", toId);
150-
Directory.Delete("tmp", true);
151-
}
152-
153-
public static void SteamToBnetTransfer(string fromId, string toId) {
154-
Directory.CreateDirectory("tmp");
155-
SteamBulkDecrypt(fromId, "tmp");
156-
BnetBulkEncrypt("tmp", toId);
157-
Directory.Delete("tmp", true);
158-
}
159-
#endregion
16041
}
16142
}

DoomEternalSavePath.cs

Lines changed: 137 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System;
2-
using System.Collections.Generic;
2+
using System.IO;
33
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
4+
using System.Collections.Generic;
5+
6+
using ICSharpCode.SharpZipLib.Zip;
7+
using ICSharpCode.SharpZipLib.Core;
68

79
namespace DOOMSaveManager
810
{
@@ -15,16 +17,144 @@ public enum DoomEternalSavePlatform
1517
public class DoomEternalSavePath
1618
{
1719
public string Identifier;
18-
public string BasePath;
20+
public string FullPath;
1921
public DoomEternalSavePlatform Platform;
2022
public bool Encrypted = true;
2123

22-
public DoomEternalSavePath(string id, string path, DoomEternalSavePlatform platform) {
24+
public static string BnetSavePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Saved Games", "id Software", "DOOMEternal", "base", "savegame");
25+
public static string SteamSavePath = Path.Combine(Utilities.GetSteamPath(), "userdata");
26+
27+
public DoomEternalSavePath(string id, DoomEternalSavePlatform platform, bool encrypted = true) {
2328
Identifier = id;
24-
BasePath = path;
2529
Platform = platform;
26-
if (id == "savegame.unencrypted")
30+
Encrypted = encrypted;
31+
32+
if (Identifier == "savegame.unencrypted") {
2733
Encrypted = false;
34+
FullPath = Path.Combine(BnetSavePath, "savegame.unencrypted", Environment.UserName);
35+
} else if (platform == DoomEternalSavePlatform.BethesdaNet)
36+
FullPath = Path.Combine(BnetSavePath, Identifier);
37+
else if (platform == DoomEternalSavePlatform.Steam)
38+
FullPath = Path.Combine(SteamSavePath, Utilities.Id64ToId3(Identifier), DoomEternal.SteamGameID.ToString(), "remote");
2839
}
40+
41+
public string[] GetAbsolutePaths() => Directory.GetFiles(FullPath, "*.*", SearchOption.AllDirectories);
42+
43+
public string[] GetRelativePaths() => GetAbsolutePaths().Select(single => single.Replace(FullPath, "").Substring(1)).ToArray();
44+
45+
public void Zip(string filename) {
46+
using (var fsOut = File.Create(filename))
47+
using (var zs = new ZipOutputStream(fsOut)) {
48+
zs.SetLevel(3);
49+
foreach (var single in GetAbsolutePaths()) {
50+
if (single.EndsWith("-BACKUP"))
51+
continue;
52+
53+
byte[] fileData = File.ReadAllBytes(single);
54+
string relPath = single.Replace(FullPath, "").Substring(1);
55+
if (Platform == DoomEternalSavePlatform.BethesdaNet && Encrypted)
56+
fileData = Crypto.DecryptAndVerify($"{Identifier}PAINELEMENTAL{Path.GetFileName(single)}", fileData);
57+
else if (Platform == DoomEternalSavePlatform.Steam && Encrypted)
58+
fileData = Crypto.DecryptAndVerify($"{Identifier}MANCUBUS{Path.GetFileName(single)}", fileData);
59+
60+
var fi = new FileInfo(single);
61+
var entryName = ZipEntry.CleanName(relPath);
62+
var ze = new ZipEntry(entryName);
63+
ze.Size = fileData.Length;
64+
ze.DateTime = fi.LastWriteTime;
65+
zs.PutNextEntry(ze);
66+
var buffer = new byte[4096];
67+
using (var dataIn = new MemoryStream(fileData)) {
68+
StreamUtils.Copy(dataIn, zs, buffer);
69+
}
70+
zs.CloseEntry();
71+
}
72+
}
73+
}
74+
75+
public void Extract(string filename) {
76+
using (Stream fsIn = File.OpenRead(filename))
77+
using (var zf = new ZipFile(fsIn)) {
78+
79+
foreach (ZipEntry ze in zf) {
80+
if (!ze.IsFile)
81+
continue;
82+
83+
string entryFileName = ze.Name;
84+
85+
var fullZipToPath = Path.Combine(FullPath, entryFileName);
86+
var directoryName = Path.GetDirectoryName(fullZipToPath);
87+
if (directoryName.Length > 0) {
88+
Directory.CreateDirectory(directoryName);
89+
}
90+
91+
var buffer = new byte[4096];
92+
using (var zs = zf.GetInputStream(ze))
93+
using (MemoryStream dataOut = new MemoryStream()) {
94+
StreamUtils.Copy(zs, dataOut, buffer);
95+
96+
byte[] fileData = dataOut.ToArray();
97+
if (Platform == DoomEternalSavePlatform.BethesdaNet && Encrypted)
98+
fileData = Crypto.EncryptAndDigest($"{Identifier}PAINELEMENTAL{Path.GetFileName(entryFileName)}", fileData);
99+
else if (Platform == DoomEternalSavePlatform.Steam && Encrypted)
100+
fileData = Crypto.EncryptAndDigest($"{Identifier}MANCUBUS{Path.GetFileName(entryFileName)}", fileData);
101+
102+
File.WriteAllBytes(fullZipToPath, fileData);
103+
}
104+
105+
}
106+
}
107+
}
108+
109+
public void Transfer(DoomEternalSavePath dst) {
110+
List<Tuple<string, byte[]>> srcFiles = new List<Tuple<string, byte[]>>();
111+
112+
// read from source
113+
string srcAAD;
114+
switch(Platform) {
115+
case DoomEternalSavePlatform.BethesdaNet: {
116+
srcAAD = "PAINELEMENTAL";
117+
break;
118+
}
119+
case DoomEternalSavePlatform.Steam: {
120+
srcAAD = "MANCUBUS";
121+
break;
122+
}
123+
default: {
124+
throw new Exception("Unsupported source platform specified!");
125+
}
126+
}
127+
foreach (var single in GetAbsolutePaths()) {
128+
if (Encrypted)
129+
srcFiles.Add(new Tuple<string, byte[]>(single.Replace(FullPath, "").Substring(1), Crypto.DecryptAndVerify($"{Identifier}{srcAAD}{Path.GetFileName(single)}", File.ReadAllBytes(single))));
130+
else
131+
srcFiles.Add(new Tuple<string, byte[]>(single.Replace(FullPath, "").Substring(1), File.ReadAllBytes(single)));
132+
}
133+
134+
// copy to destination
135+
string dstAAD;
136+
switch (dst.Platform) {
137+
case DoomEternalSavePlatform.BethesdaNet: {
138+
dstAAD = "PAINELEMENTAL";
139+
break;
140+
}
141+
case DoomEternalSavePlatform.Steam: {
142+
dstAAD = "MANCUBUS";
143+
break;
144+
}
145+
default: {
146+
throw new Exception("Unsupported destination platform specified!");
147+
}
148+
}
149+
foreach (var single in srcFiles) {
150+
Directory.CreateDirectory(Path.GetDirectoryName(Path.Combine(dst.FullPath, single.Item1)));
151+
if (dst.Encrypted)
152+
File.WriteAllBytes(Path.Combine(dst.FullPath, single.Item1), Crypto.EncryptAndDigest($"{dst.Identifier}{dstAAD}{Path.GetFileName(single.Item1)}", single.Item2));
153+
else
154+
File.WriteAllBytes(Path.Combine(dst.FullPath, single.Item1), single.Item2);
155+
}
156+
}
157+
158+
public static void Transfer(DoomEternalSavePath src, DoomEternalSavePath dst) => src.Transfer(dst);
29159
}
30160
}

DoomEternalSavePathCollection.cs

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,59 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
4+
using System.IO;
35
using System.Linq;
6+
using System.Runtime.CompilerServices;
47
using System.Text;
58
using System.Threading.Tasks;
69

710
namespace DOOMSaveManager
811
{
9-
public class DoomEternalSavePathCollection
12+
public class DoomEternalSavePathCollection : IList<DoomEternalSavePath>
1013
{
11-
private List<DoomEternalSavePath> Saves = new List<DoomEternalSavePath>();
14+
private List<DoomEternalSavePath> Paths = new List<DoomEternalSavePath>();
1215

13-
public DoomEternalSavePathCollection() {
16+
public int Count => Paths.Count;
1417

15-
}
18+
public bool IsReadOnly => false;
1619

17-
public DoomEternalSavePathCollection(IEnumerable<DoomEternalSavePath> saves) {
18-
Saves.AddRange(saves);
19-
}
20+
public DoomEternalSavePathCollection() { }
2021

21-
public void Add(DoomEternalSavePath save) {
22-
Saves.Add(save);
23-
}
22+
public DoomEternalSavePathCollection(IEnumerable<DoomEternalSavePath> paths) => Paths.AddRange(paths);
2423

25-
public void AddRange(IEnumerable<DoomEternalSavePath> saves) {
26-
Saves.AddRange(saves);
24+
public DoomEternalSavePath this[int index] {
25+
get {
26+
return Paths[index];
27+
}
28+
set {
29+
Paths[index] = value;
30+
}
2731
}
2832

33+
public int IndexOf(DoomEternalSavePath path) => Paths.IndexOf(path);
34+
35+
public void Insert(int index, DoomEternalSavePath path) => Paths.Insert(index, path);
36+
37+
public void RemoveAt(int index) => Paths.RemoveAt(index);
38+
39+
public void Add(DoomEternalSavePath path) => Paths.Add(path);
40+
41+
public void Clear() => Paths.Clear();
42+
43+
public bool Contains(DoomEternalSavePath path) => Paths.Contains(path);
44+
45+
public void CopyTo(DoomEternalSavePath[] array, int index) => Paths.CopyTo(array, index);
46+
47+
public bool Remove(DoomEternalSavePath path) => Paths.Remove(path);
48+
49+
public IEnumerator<DoomEternalSavePath> GetEnumerator() => Paths.GetEnumerator();
50+
51+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
52+
53+
public DoomEternalSavePath Get(int index) => Paths[index];
54+
2955
public bool SaveExists(string id, out DoomEternalSavePath save) {
30-
foreach(var single in Saves) {
56+
foreach(var single in Paths) {
3157
if (single.Identifier == id) {
3258
save = single;
3359
return true;
@@ -43,8 +69,6 @@ public DoomEternalSavePath GetSave(string id) {
4369
return save;
4470
}
4571

46-
public string[] GetIdentifiers() {
47-
return Saves.Select(single => single.Identifier).ToArray();
48-
}
72+
public string[] GetIdentifiers() => Paths.Select(single => single.Identifier).ToArray();
4973
}
5074
}

0 commit comments

Comments
 (0)