public static string Decompile(PSB psb) { if (Consts.JsonArrayCollapse) { return(ArrayCollapseJsonTextWriter.SerializeObject(psb.Objects, new PsbJsonConverter(Consts.JsonUseDoubleOnly, Consts.JsonUseHexNumber))); } return(JsonConvert.SerializeObject(psb.Objects, Formatting.Indented, new PsbJsonConverter(Consts.JsonUseDoubleOnly, Consts.JsonUseHexNumber))); }
public static string SerializeObject(object obj, JsonConverter converter = null) { using StringWriter sw = new StringWriter(); using JsonWriter jw = new ArrayCollapseJsonTextWriter(sw) { Formatting = Formatting.Indented }; var ser = new JsonSerializer(); if (converter != null) { ser.Converters.Add(converter); } ser.Serialize(jw, obj); return(sw.ToString()); }
internal static void OutputResources(PSB psb, FreeMountContext context, string filePath, PsbExtractOption extractOption = PsbExtractOption.Original, PsbImageFormat extractFormat = PsbImageFormat.png, bool useResx = true) { var name = Path.GetFileNameWithoutExtension(filePath); var dirPath = Path.Combine(Path.GetDirectoryName(filePath), name); PsbResourceJson resx = new PsbResourceJson(psb, context.Context); if (File.Exists(dirPath)) { name += "-resources"; dirPath += "-resources"; } var extraDir = Path.Combine(dirPath, Consts.ExtraResourceFolderName); if (!Directory.Exists(dirPath)) //ensure there is no file with same name! { if (psb.Resources.Count != 0) { Directory.CreateDirectory(dirPath); } } if (psb.ExtraResources.Count > 0) { var extraDic = PsbResHelper.OutputExtraResources(psb, context, name, extraDir, out var flattenArrays, extractOption); resx.ExtraResources = extraDic; if (flattenArrays != null && flattenArrays.Count > 0) { resx.ExtraFlattenArrays = flattenArrays; } } var resDictionary = psb.TypeHandler.OutputResources(psb, context, name, dirPath, extractOption); //MARK: We use `.resx.json` to distinguish from psbtools' `.res.json` if (useResx) { resx.Resources = resDictionary; resx.Context = context.Context; string json; if (Consts.JsonArrayCollapse) { json = ArrayCollapseJsonTextWriter.SerializeObject(resx); } else { json = JsonConvert.SerializeObject(resx, Formatting.Indented); } File.WriteAllText(ChangeExtensionForOutputJson(filePath, ".resx.json"), json); } else { if (psb.ExtraResources.Count > 0) { throw new NotSupportedException("PSBv4 cannot use legacy res.json format."); } File.WriteAllText(ChangeExtensionForOutputJson(filePath, ".res.json"), JsonConvert.SerializeObject(resDictionary.Values.ToList(), Formatting.Indented)); } }