-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSurfaceConverter.cs
151 lines (138 loc) · 5.75 KB
/
SurfaceConverter.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ValveKeyValue;
namespace KVSurfaceUpdater
{
internal class SurfaceConverter : Converter
{
KVObject decals_subrect;
Dictionary<string, string> translationData = new Dictionary<string, string>();
List<KVObject> decals = new List<KVObject>();
Dictionary<string, KV3Decal> newDecals = new Dictionary<string, KV3Decal>();
public override async Task ConvertAsync(string targetPath)
{
LoadDecalsSubrect(targetPath);
string outputFolder = Path.Combine(targetPath, "surface");
if (!Directory.Exists(outputFolder))
{
Directory.CreateDirectory(outputFolder);
}
Console.WriteLine("-= Will process decals...");
string decalOutPath = Path.Combine(outputFolder, "decal");
if (!Directory.Exists(decalOutPath))
{
Directory.CreateDirectory(decalOutPath);
}
var createdDecals = CreateDecalFiles(decals, decalOutPath);
newDecals = createdDecals.ToDictionary(x => x.Name);
await base.ConvertAsync(targetPath);
}
protected override bool ShouldConvertFile(string filename)
{
return filename.Contains("surfaceproperties") && !filename.Contains("manifest");
}
private void LoadDecalsSubrect(string targetFolder)
{
string decalsSubrectPath = Path.Combine(targetFolder, "decals_subrect.txt");
try
{
Console.WriteLine($"-= Will load '{decalsSubrectPath}' for translation data and decals...");
decals_subrect = OpenKVFile(decalsSubrectPath);
//Sort out the translation data from the other decals
foreach (var child in decals_subrect)
{
if (child.Children.Count() > 0)
{
decals.Add(child);
}
else
{
if (!translationData.ContainsKey(child.Name))
{
translationData.Add(child.Name, (string)child.Value);
}
else
{
Console.WriteLine($"! Duplicate translation key '{child.Name}' with value '{(string)child.Value}' will be ignored ({child.Name} will instead translate to '{translationData[child.Name]}').");
}
}
}
}
catch (FileNotFoundException e)
{
ExitFromError(e, decalsSubrectPath);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("!!! Program did not have permission to access this file. Move it elsewhere or run this as admin !!!");
ExitFromError(e, decalsSubrectPath);
}
catch (Exception e)
{
ExitFromError(e);
}
}
protected override async Task<bool> ProcessFile(string filePath, string outputFolder)
{
try
{
KVObject? kvObject = OpenKVFile(filePath);
//Make a list of all the surfaces
List<KVObject> surfaceObjects = ListAllObjects(kvObject);
foreach (var surfaceObject in surfaceObjects)
{
string surfaceName = KV3Surface.Prefix + surfaceObject.Name;
string surfaceFile = Path.Combine(outputFolder, "surface", $"{surfaceName.ToLower()}.surface");
KV3Surface newSurface = new KV3Surface(surfaceObject);
newSurface.Name = KV3Surface.Prefix + newSurface.Name;
KVValue? gamematerial = newSurface.gamematerial;
if (gamematerial != null)
{
if (translationData.TryGetValue((string)gamematerial, out string decalName))
{
newDecals.TryGetValue(decalName, out KV3Decal? decal);
newSurface.gamematerial_decal = decal;
}
else
{
Console.WriteLine($"WARNING: Unable to find decal '{gamematerial}' translation for surface '{surfaceName}'.");
}
}
File.WriteAllText(surfaceFile, newSurface.ToString());
Console.WriteLine($"Made surface file for '{surfaceName}'.");
return true;
}
}
catch (Exception e)
{
ExitFromError(e, filePath);
}
return false;
}
List<KV3Decal> CreateDecalFiles(IEnumerable<KVObject> decals, string outputFolder)
{
try
{
List<KV3Decal> newDecals = new List<KV3Decal>();
foreach (var decal in decals)
{
string decalName = decal.Name;
string decalFile = Path.Combine(outputFolder, $"{decalName.ToLower()}.decal");
KV3Decal newDecal = new KV3Decal(decal);
File.WriteAllText(decalFile, newDecal.ToString());
Console.WriteLine($"Made decal file for '{newDecal.Name}'.");
newDecals.Add(newDecal);
}
return newDecals;
}
catch (Exception e)
{
ExitFromError(e);
return null;
}
}
}
}