-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWikiExtractorMod.cs
More file actions
150 lines (128 loc) · 5.35 KB
/
WikiExtractorMod.cs
File metadata and controls
150 lines (128 loc) · 5.35 KB
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
// WikiExtractorMod.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using BepInEx;
using HarmonyLib;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;
using Util.Commands;
using WikiExtractorMod.Commands;
using static Assets.Scripts.Localization;
namespace WikiExtractorMod
{
[BepInPlugin(PluginGuid, PluginName, PluginVersion)]
public class WikiExtractorMod : BaseUnityPlugin
{
private const string PluginGuid = "net.traineratwot.stationeers.Extractor";
private const string PluginName = "Extractor";
private const string PluginVersion = "2.0.10";
#if DEBUG
private const bool Debug = true;
#else
private const bool Debug = false;
#endif
private static readonly Dictionary<int, string> ImageHash = new Dictionary<int, string>();
private void Awake()
{
try
{
var harmony = new Harmony(PluginGuid);
harmony.PatchAll();
Helpers.Log("Patch WikiExtractorMod succeeded");
Helpers.Log("USE COMMAND 'stationpedia_export_all', 'stationpedia_export'");
var lang = CurrentLanguage;
Helpers.Log("Current language: " + lang);
var folderPath = Path.Combine(Application.dataPath, "wiki_data");
if (!Directory.Exists(folderPath))
Directory.CreateDirectory(folderPath);
CommandLine.AddCommand("stationpedia_export_all", new StationpediaExportAllCommand());
CommandLine.AddCommand("stationpedia_export", new StationpediaExportCommand());
}
catch (Exception e)
{
Helpers.Log("Patch WikiExtractorMod Failed");
Helpers.Warn(e.ToString());
}
}
public static Dictionary<int, string> GetImageHash()
{
return ImageHash;
}
public static class Helpers
{
public static void Log(string line)
{
UnityEngine.Debug.Log("[" + PluginName + "]: " + line);
}
public static void info(string line)
{
UnityEngine.Debug.Log("[" + PluginName + "] [INFO]: " + line);
}
public static void Warn(string line)
{
// if (Debug) UnityEngine.Debug.LogWarning("[" + PluginName + "][Warn]: " + line);
}
public static void SaveJson(string name, object obj, string folder = "wiki_data/source/languages")
{
var lang = CurrentLanguage;
if (name == null)
{
Warn("Can't save json, name is null");
return;
}
name += ".json";
var json = SortAndSerialize(obj);
var path = Path.Combine(Application.dataPath, folder, lang.ToString(), name);
var folderPath = Path.GetDirectoryName(path);
if (!Directory.Exists(folderPath))
if (folderPath != null)
Directory.CreateDirectory(folderPath);
try
{
File.WriteAllText(path, json, Encoding.UTF8);
}
catch (Exception e)
{
Warn("Failed to save JSON: " + e);
}
}
private static string SortAndSerialize(object obj)
{
var token = JToken.FromObject(obj);
var sorted = SortToken(token);
return JsonConvert.SerializeObject(sorted, Formatting.Indented);
}
private static JToken SortToken(JToken token)
{
switch (token.Type)
{
case JTokenType.Object:
var obj = (JObject)token;
var sortedObj = new JObject(
obj.Properties()
.OrderBy(p => p.Name, StringComparer.Ordinal) // Сортируем свойства по имени
.Select(p => new JProperty(p.Name, SortToken(p.Value)))
);
return sortedObj;
case JTokenType.Array:
var arr = (JArray)token;
var sortedItems = arr.Select(SortToken).ToList();
// Пытаемся отсортировать массив, если все элементы — объекты
if (sortedItems.All(t => t.Type == JTokenType.Object))
sortedItems = sortedItems
.OrderBy(t =>
t.ToString()) // Сортируем объекты по их строковому представлению (детерминированно)
.ToList();
// Если не все объекты — оставляем как есть (не сортируем примитивы)
return new JArray(sortedItems);
default:
return token; // Примитивы, null и т.д. — не меняем
}
}
}
}
}