-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathFileLoader.cs
132 lines (122 loc) · 4.85 KB
/
FileLoader.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
using UnityEngine;
using System.Text.Json;
using PVZ_Hyper_Fusion.AssetStore;
using System.Text.Encodings.Web;
using System.Text.Unicode;
namespace PVZ_Hyper_Fusion
{
internal static class FileLoader
{
internal enum AssetType
{
Textures,
Strings,
Dumps,
}
#if USE_TRANSLATE
internal static void LoadStrings()
{
string textureDir = getAssetDir(AssetType.Strings);
if (!Directory.Exists(textureDir))
{
Directory.CreateDirectory(textureDir);
}
try
{
foreach (string filepath in Directory.EnumerateFiles(textureDir, "*.json", SearchOption.AllDirectories))
{
string fileName = Path.GetFileNameWithoutExtension(filepath);
StringStore.stringsDict[fileName] = filepath;
string jsonString = File.ReadAllText(filepath);
if (fileName.EndsWith("_strings"))
{
Dictionary<string, string> dictionary = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonString);
foreach (var (key, value) in dictionary)
{
StringStore.translationString[key] = value;
}
}
else if (fileName.EndsWith("_regexs"))
{
Dictionary<string, string> dictionary = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonString);
foreach (var (key, value) in dictionary)
{
StringStore.translationStringRegex[key] = value;
}
}
}
SaveStrings();
DumpJson();
}
catch (Exception e)
{
Log.LogError("Error loading String. ");
Log.LogError(e.GetType() + " " + e.Message);
}
Log.LogInfo("Strings loaded successfully.");
}
#endif
#if USE_TEXTURE
internal static void LoadTextures()
{
string jsonDir = getAssetDir(AssetType.Textures);
if (!Directory.Exists(jsonDir))
{
Directory.CreateDirectory(jsonDir);
}
try
{
foreach (string filepath in Directory.EnumerateFiles(jsonDir, "*.png", SearchOption.AllDirectories))
{
Texture2D texture2D = Utils.LoadImage(filepath);
TextureStore.textureDict[Path.GetFileNameWithoutExtension(filepath)] = filepath;
}
}
catch (Exception e)
{
Log.LogError("Error loading Texture. ");
Log.LogError(e.GetType() + " " + e.Message);
}
Log.LogInfo("Textures loaded successfully.");
}
#endif
#if USE_TRANSLATE
internal static void SaveStrings()
{
string stringDir = getAssetDir(AssetType.Strings);
if (!Directory.Exists(stringDir))
{
Directory.CreateDirectory(stringDir);
}
string translationStringRegex = JsonSerializer.Serialize(StringStore.translationStringRegex, new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
File.WriteAllText(Path.Combine(stringDir, "translation_regexs.json"), translationStringRegex);
string translationString = JsonSerializer.Serialize(StringStore.translationString, new JsonSerializerOptions
{
WriteIndented = true,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
File.WriteAllText(Path.Combine(stringDir, "translation_strings.json"), translationString);
}
public static void DumpJson()
{
string stringDir = getAssetDir(AssetType.Dumps);
if (!Directory.Exists(stringDir))
{
Directory.CreateDirectory(stringDir);
}
string LawnStrings = Resources.Load<TextAsset>("LawnStrings").text;
string ZombieStrings = Resources.Load<TextAsset>("ZombieStrings").text;
File.WriteAllText(Path.Combine(stringDir, "LawnStrings.json"), LawnStrings);
File.WriteAllText(Path.Combine(stringDir, "ZombieStrings.json"), ZombieStrings);
}
#endif
private static string getAssetDir(AssetType assetType)
{
return Path.Combine("Mods", "PVZ_Hyper_Fusion", assetType.ToString());
}
}
}