-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConverter.cs
98 lines (85 loc) · 3.4 KB
/
Converter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using ValveKeyValue;
namespace KVSurfaceUpdater
{
internal abstract class Converter
{
public Converter() { }
public async virtual Task ConvertAsync(string targetPath)
{
string outputFolder = Path.Combine(targetPath, "data");
var filesToProcess = Directory.EnumerateFiles(targetPath, "*", SearchOption.AllDirectories).Where(x => ShouldConvertFile(Path.GetFileName(x)));
List<Task> convertTasks = new List<Task>(filesToProcess.Count());
foreach (var file in filesToProcess)
{
string taskFile = file;
convertTasks.Add(Task.Run(async () =>
{
try
{
await ProcessFile(taskFile, outputFolder);
}
catch(Exception e)
{
Console.WriteLine($"Error processing file '{taskFile}': {e.Message}");
}
}));
}
await Task.WhenAll(convertTasks.ToArray());
}
protected virtual bool ShouldConvertFile(string filename)
{
return true;
}
protected abstract Task<bool> ProcessFile(string filePath, string outputFolder);
protected KVObject OpenKVFile(string filepath)
{
//Read file into text so we can fix the many parser
string fileText = File.ReadAllText(filepath);
//Remove conditionals as this parser just breaks on them?
fileText = Regex.Replace(fileText, @"\[\$.*\]", "");
//Fix broken , seperated parsing
fileText = Regex.Replace(fileText, @"(?<=""\d*),(?=\d*"")", ", ");
//Write back to stream for parser
using (var stream = new MemoryStream())
{
var writer = new StreamWriter(stream);
writer.Write(fileText);
writer.Flush();
stream.Position = 0;
KVSerializer serializer = KVSerializer.Create(KVSerializationFormat.KeyValues1Text);
return serializer.Deserialize(stream);
}
}
protected List<KVObject> ListAllObjects(KVObject kvObject)
{
List<KVObject> listedObjects = new List<KVObject>();
listedObjects.Add(kvObject);
listedObjects.AddRange(kvObject.Children.Where(x => x.Children.Count() > 0));
return listedObjects;
}
public static void ExitFromError(Exception e, string attemptedPath = null)
{
if (!string.IsNullOrEmpty(attemptedPath))
{
Console.WriteLine($"! Unable to open '{Path.GetFileName(attemptedPath)}'. Program will now exit.");
}
else
{
Console.WriteLine("!!! Critical error occurred. Program will now exit.");
}
if (!string.IsNullOrEmpty(attemptedPath))
{
Console.WriteLine($"Attempted path: '{attemptedPath}'");
}
Console.WriteLine($"!!! ERROR: {e.GetType().Name}, {e.Message}.");
Console.WriteLine("Error Stack:\n" + e.StackTrace);
Environment.Exit(1);
}
}
}