From 30da3688293f79cfbbf70bf38b86c94b08e825b2 Mon Sep 17 00:00:00 2001 From: Kirill Date: Sun, 10 Mar 2024 17:28:55 +0300 Subject: [PATCH] add export ic functions and constants --- WikiExtractorMod/BepInEx.cs | 49 +++++- WikiExtractorMod/Extractor.cs | 299 +++++++++++++--------------------- 2 files changed, 157 insertions(+), 191 deletions(-) diff --git a/WikiExtractorMod/BepInEx.cs b/WikiExtractorMod/BepInEx.cs index e30e02f..802cf51 100644 --- a/WikiExtractorMod/BepInEx.cs +++ b/WikiExtractorMod/BepInEx.cs @@ -1,7 +1,6 @@ using System; using System.IO; using Assets.Scripts; -using Assets.Scripts.Objects.Electrical; using BepInEx; using HarmonyLib; using Newtonsoft.Json; @@ -16,7 +15,7 @@ public class ExtractorBepInEx : BaseUnityPlugin { public const string pluginGuid = "net.elmo.stationeers.Extractor"; public const string pluginName = "Extractor"; - public const string pluginVersion = "1.1"; + public const string pluginVersion = "1.2"; private void Awake() { @@ -40,6 +39,52 @@ public static void Log(string line) { Debug.Log("[" + pluginName + "]: " + line); } + + public static void SaveJson(string name, object obj, string folder = "wiki_data") + { + var lang = Localization.CurrentLanguage; + name += ".json"; + var json = JsonConvert.SerializeObject(obj); + var path = Path.Combine( + Application.dataPath, + folder, + lang.ToString(), + name + ); + var folderPath = Path.Combine(Application.dataPath, folder, lang.ToString()); + + if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath); + + using (var writer = new StreamWriter(path)) + { + writer.WriteLine(json); + } + } + + public static string SpriteToBase64(Sprite sprite) + { + var lang = Localization.CurrentLanguage; + if (lang != LanguageCode.EN) + // убираем избыточноть в остальных языках + return null; + + if (sprite == null) return null; + + if (sprite.texture == null) return null; + + var renderTexture = new RenderTexture(sprite.texture.width, sprite.texture.height, 0); + Graphics.Blit(sprite.texture, renderTexture); + + var texture = new Texture2D(sprite.texture.width, sprite.texture.height); + RenderTexture.active = renderTexture; + texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); + texture.Apply(); + + var bytes = texture.EncodeToPNG(); + var base64String = Convert.ToBase64String(bytes); + + return base64String; + } } #endregion diff --git a/WikiExtractorMod/Extractor.cs b/WikiExtractorMod/Extractor.cs index 794e8fd..dd5c81c 100644 --- a/WikiExtractorMod/Extractor.cs +++ b/WikiExtractorMod/Extractor.cs @@ -1,32 +1,23 @@ -using System.IO; -using Assets.Scripts.UI; -using HarmonyLib; -using Newtonsoft.Json; -using UnityEngine; -using System; -using System.Linq; +using System; +using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using Assets.Scripts; using Assets.Scripts.Objects; using Assets.Scripts.Objects.Electrical; using Assets.Scripts.Objects.Pipes; -using Objects.Items; +using Assets.Scripts.UI; +using HarmonyLib; +using UnityEngine; namespace WikiExtractorMod { - using System.Collections.Generic; - using System.Security.Cryptography; - [HarmonyPatch(typeof(Stationpedia), nameof(Stationpedia.Register))] public class Extractor { - public static int idx = 0; - [HarmonyPrefix] - public static void Prefix(StationpediaPage page, bool fallback = false) + public static bool Prefix(StationpediaPage page, bool fallback = false) { - idx++; var obj = new Dictionary(); string prefab = null; var tags = new List(); @@ -35,13 +26,14 @@ public static void Prefix(StationpediaPage page, bool fallback = false) prefab = page.PrefabName; obj.Add("TYPE", "object"); if (prefab.StartsWith("Structure")) - { tags.Add("structure"); - } - else if (prefab.StartsWith("Item")) - { - tags.Add("item"); - } + else if (prefab.StartsWith("Item")) tags.Add("item"); + + if (prefab.EndsWith("Ingot")) tags.Add("ingot"); + + if (prefab.StartsWith("ItemKit")) tags.Add("kit"); + + if (prefab.StartsWith("StructureCable")) tags.Add("cable"); } else if (page.ReagentsType != null) { @@ -56,7 +48,37 @@ public static void Prefix(StationpediaPage page, bool fallback = false) tags.Add("page"); } - LanguageCode lang = Localization.CurrentLanguage; + + if (page.LogicInsert.Count > 0) tags.Add("hasLogic"); + + if (page.SlotInserts.Count > 0) + { + tags.Add("hasSlot"); + foreach (var slotInsert in page.SlotInserts) + if (slotInsert.SlotType == "ProgrammableChip") + { + tags.Add("hasChip"); + break; + } + } + + if (page.BuildStates.Count > 0) tags.Add("buildable"); + + if (page.ModeInsert.Count > 0) tags.Add("hasMode"); + + if (page.FoundInOre.Count > 0) tags.Add("hasOre"); + + if (page.FoundInGas.Count > 0) tags.Add("hasGas"); + + if (page.PaintableText == "Yes") tags.Add("paintable"); + + if (page.GasType > 0) tags.Add("gas"); + + if (page.ProducedThingsInserts.Count > 0) tags.Add("hasReciepe"); + + if (prefab != null) tags.Add("hasPrefab"); + + var lang = Localization.CurrentLanguage; obj.Add("prefab", prefab); //Very unique id obj.Add("Lang", lang.ToString()); //Very unique id obj.Add("Key", page.Key); @@ -102,7 +124,7 @@ public static void Prefix(StationpediaPage page, bool fallback = false) obj.Add("DrillHeadProperties", page.DrillHeadProperties); obj.Add("GasType", page.GasType); obj.Add("DisplayFilter", page.DisplayFilter); - obj.Add("CustomSpriteToUse", SpriteToBase64(page.CustomSpriteToUse)); + obj.Add("CustomSpriteToUse", ExtractorBepInEx.SpriteToBase64(page.CustomSpriteToUse)); obj.Add("PageCustomCategories", page.PageCustomCategories); obj.Add("SlotInserts", ParseSlot(page.SlotInserts)); obj.Add("HowToBuild", ParseBuild(page.HowToBuild)); @@ -121,96 +143,28 @@ public static void Prefix(StationpediaPage page, bool fallback = false) obj.Add("UsedIn", ParseCategory(page.UsedIn)); obj.Add("LifeRequirements", page.LifeRequirements); - if (page.LogicInsert.Count > 0) - { - tags.Add("hasLogic"); - } - - if (page.SlotInserts.Count > 0) - { - tags.Add("hasSlot"); - foreach (var slotInsert in page.SlotInserts) - { - if (slotInsert.SlotType == "ProgrammableChip") - { - tags.Add("hasChip"); - break; - } - } - } - - if (page.BuildStates.Count > 0) - { - tags.Add("buildable"); - } - - if (page.ModeInsert.Count > 0) - { - tags.Add("hasMode"); - } - - if (page.FoundInOre.Count > 0) - { - tags.Add("isOre"); - } - - if (page.FoundInGas.Count > 0) - { - tags.Add("isGas"); - } - - if (page.PaintableText == "Yes") - { - tags.Add("paintable"); - } - - if (page.ProducedThingsInserts.Count > 0) - { - tags.Add("hasReciepe"); - } - - if (prefab != null) - { - tags.Add("hasPrefab"); - } - - int deviceConnectCount = 0; - Sprite mainImage = page.CustomSpriteToUse; + var deviceConnectCount = 0; + var mainImage = page.CustomSpriteToUse; try { - Thing thing = Prefab.Find(page.PrefabHash); + var thing = Prefab.Find(page.PrefabHash); if (thing != null) { - if (mainImage == null) - { - mainImage = thing.GetThumbnail(); - } + if (mainImage == null) mainImage = thing.GetThumbnail(); if (tags.Contains("hasChip")) - { foreach (var slot in thing.Slots) - { if (slot.Type == Slot.Class.ProgrammableChip) { if (thing is CircuitHousing) - { deviceConnectCount = 6; - } - else if (thing is DeviceInputOutputCircuit) - { - deviceConnectCount = 2; - } + else if (thing is DeviceInputOutputCircuit) deviceConnectCount = 2; break; } - } - } } - if (mainImage == null) - { - mainImage = page.BuildStates[page.BuildStates.Count - 1].PrinterImage; - } + if (mainImage == null) mainImage = page.BuildStates[page.BuildStates.Count - 1].PrinterImage; } catch (Exception) { @@ -218,63 +172,35 @@ public static void Prefix(StationpediaPage page, bool fallback = false) } obj.Add("DeviceConnectCount", deviceConnectCount); - obj.Add("MainImage", SpriteToBase64(mainImage)); - if (mainImage != null) - { - tags.Add("hasImage"); - } + obj.Add("MainImage", ExtractorBepInEx.SpriteToBase64(mainImage)); + if (mainImage != null) tags.Add("hasImage"); obj.Add("tags", tags); string fileName; if (prefab != null) - { fileName = prefab; - } else - { fileName = page.Key; - } obj.Add("ID", fileName); - fileName += ".json"; - string json = JsonConvert.SerializeObject(obj); - string path = Path.Combine( - Application.dataPath, - "wiki_data", - lang.ToString(), - fileName - ); - string folderPath = Path.Combine(Application.dataPath, "wiki_data", lang.ToString()); - - if (!Directory.Exists(folderPath)) - { - Directory.CreateDirectory(folderPath); - } - using (StreamWriter writer = new StreamWriter(path)) - { - writer.WriteLine(json); - } - ExtractorBepInEx.Log(path); + ExtractorBepInEx.SaveJson(fileName, obj); + return true; } - private static List> ParseBuild(List buildStates) { - List> newBuildStates = new List>(); + var newBuildStates = new List>(); foreach (var stationBuildCostInsert in buildStates) { - string base64String = SpriteToBase64(stationBuildCostInsert.PrinterImage); - Dictionary buildState = new Dictionary(); + var base64String = ExtractorBepInEx.SpriteToBase64(stationBuildCostInsert.PrinterImage); + var buildState = new Dictionary(); buildState.Add("PrinterName", stationBuildCostInsert.PrinterName); buildState.Add("TierName", stationBuildCostInsert.TierName); buildState.Add("Details", stationBuildCostInsert.Details); buildState.Add("Description", stationBuildCostInsert.Description); buildState.Add("PageLink", stationBuildCostInsert.PageLink); - if (base64String != "") - { - buildState.Add("image", base64String); - } + if (base64String != "") buildState.Add("image", base64String); newBuildStates.Add(buildState); } @@ -284,18 +210,15 @@ private static List> ParseBuild(List> ParseCategory(List buildStates) { - List> newBuildStates = new List>(); + var newBuildStates = new List>(); foreach (var stationCategoryInsert in buildStates) { - string base64String = SpriteToBase64(stationCategoryInsert.InsertImage); - Dictionary buildState = new Dictionary(); + var base64String = ExtractorBepInEx.SpriteToBase64(stationCategoryInsert.InsertImage); + var buildState = new Dictionary(); buildState.Add("NameOfThing", stationCategoryInsert.NameOfThing); buildState.Add("PrefabHash", stationCategoryInsert.PrefabHash); buildState.Add("PageLink", stationCategoryInsert.PageLink); - if (base64String != "") - { - buildState.Add("image", base64String); - } + if (base64String != "") buildState.Add("image", base64String); newBuildStates.Add(buildState); } @@ -305,18 +228,15 @@ private static List> ParseCategory(List> ParseSlot(List buildStates) { - List> newBuildStates = new List>(); + var newBuildStates = new List>(); foreach (var stationCategoryInsert in buildStates) { - string base64String = SpriteToBase64(stationCategoryInsert.SlotIcon); - Dictionary buildState = new Dictionary(); + var base64String = ExtractorBepInEx.SpriteToBase64(stationCategoryInsert.SlotIcon); + var buildState = new Dictionary(); buildState.Add("SlotName", stationCategoryInsert.SlotName); buildState.Add("SlotType", stationCategoryInsert.SlotType); buildState.Add("SlotIndex", stationCategoryInsert.SlotIndex); - if (base64String != "") - { - buildState.Add("image", base64String); - } + if (base64String != "") buildState.Add("image", base64String); newBuildStates.Add(buildState); } @@ -327,20 +247,17 @@ private static List> ParseSlot(List> ParseStructureVersion( List buildStates) { - List> newBuildStates = new List>(); + var newBuildStates = new List>(); foreach (var stationCategoryInsert in buildStates) { - string base64String = SpriteToBase64(stationCategoryInsert.StructureImage); - Dictionary buildState = new Dictionary(); + var base64String = ExtractorBepInEx.SpriteToBase64(stationCategoryInsert.StructureImage); + var buildState = new Dictionary(); buildState.Add("StructureVersion", stationCategoryInsert.StructureVersion); buildState.Add("CreationMultiplier", stationCategoryInsert.CreationMultiplier); buildState.Add("EnergyCostMultiplier", stationCategoryInsert.EnergyCostMultiplier); buildState.Add("MaterialCostMultiplier", stationCategoryInsert.MaterialCostMultiplier); buildState.Add("BuildTimeMultiplier", stationCategoryInsert.BuildTimeMultiplier); - if (base64String != "") - { - buildState.Add("image", base64String); - } + if (base64String != "") buildState.Add("image", base64String); newBuildStates.Add(buildState); } @@ -348,52 +265,56 @@ private static List> ParseStructureVersion( return newBuildStates; } - private static string SpriteToBase64(Sprite sprite) + public static byte[] GetHash(string inputString) { - LanguageCode lang = Localization.CurrentLanguage; - if (lang != LanguageCode.EN) - { - // убираем избыточноть в остальных языках - return null; - } - - if (sprite == null) - { - return null; - } - - if (sprite.texture == null) + using (HashAlgorithm algorithm = MD5.Create()) { - return null; + return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString)); } + } - RenderTexture renderTexture = new RenderTexture(sprite.texture.width, sprite.texture.height, 0); - Graphics.Blit(sprite.texture, renderTexture); - - Texture2D texture = new Texture2D(sprite.texture.width, sprite.texture.height); - RenderTexture.active = renderTexture; - texture.ReadPixels(new Rect(0, 0, renderTexture.width, renderTexture.height), 0, 0); - texture.Apply(); - - byte[] bytes = texture.EncodeToPNG(); - string base64String = Convert.ToBase64String(bytes); - - return base64String; + public static string GetHashString(string inputString) + { + var sb = new StringBuilder(); + foreach (var b in GetHash(inputString)) + sb.Append(b.ToString("X2")); + return sb.ToString(); } + } - public static byte[] GetHash(string inputString) + [HarmonyPatch(typeof(HelpReference))] // Указываем класс, который хотим патчить + public class HelpReferencePatch + { + [HarmonyPatch("Setup", typeof(ScriptCommand), typeof(Sprite))] + [HarmonyPrefix] // Этот метод будет вызван перед оригинальным методом Setup + public static bool PrefixSetupWithScriptCommand(HelpReference __instance, + ScriptCommand command, + Sprite defaultItemImage) { - using (HashAlgorithm algorithm = MD5.Create()) - return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString)); + var obj = new Dictionary(); + obj.Add("name", command.ToString()); + obj.Add("example", ProgrammableChip.StripColorTags(ProgrammableChip.GetCommandExample(command))); + obj.Add("referenceType", "Instruction"); + obj.Add("description", ProgrammableChip.StripColorTags(ProgrammableChip.GetCommandDescription(command))); + ExtractorBepInEx.SaveJson(command.ToString(), obj, "wiki_instruction"); + return true; } - public static string GetHashString(string inputString) + [HarmonyPatch("Setup", typeof(ProgrammableChip.Constant), typeof(Sprite))] + [HarmonyPrefix] // Этот метод будет вызван перед оригинальным методом Setup + public static bool PrefixSetupWithScriptCommand(HelpReference __instance, + ProgrammableChip.Constant constant, + Sprite defaultItemImage) { - StringBuilder sb = new StringBuilder(); - foreach (byte b in GetHash(inputString)) - sb.Append(b.ToString("X2")); - return sb.ToString(); + var obj = new Dictionary(); + obj.Add("literal", constant.Literal); + obj.Add("name", ProgrammableChip.StripColorTags(constant.GetName())); + obj.Add("value", constant.Value); + obj.Add("referenceType", "Constant"); + obj.Add("description", ProgrammableChip.StripColorTags(constant.Description)); + ExtractorBepInEx.SaveJson(constant.Literal, obj, "wiki_constant"); + return true; } } } \ No newline at end of file