Beispiel #1
0
        public static void ExtractStoryData(FileInfo source, FileInfo?json = null, FileInfo?yaml = null,
                                            FileInfo?dest    = null,
                                            FileInfo?lipsync = null)
        {
            var file = Unity3dResource.LoadAssetFile(source);

            try
            {
                var textFile   = file.Objects.OfType <TextAsset>().First();
                var textBuffer = textFile.m_Script;
                var textName   = textFile.m_Name;

                List <Command>?commands = null;

                Console.WriteLine(textName);

                if (dest != null)
                {
                    using var f = dest.Create();
                    f.Write(textBuffer);
                }

                if (json != null)
                {
                    commands ??= Deserializer.Deserialize(textBuffer);
                    using var fj = json.Create();
                    fj.Write(commands.ToUtf8Json());
                }

                if (yaml != null)
                {
                    commands ??= Deserializer.Deserialize(textBuffer);
                    using var fy = yaml.CreateText();
                    fy.Write(commands.ToReadableYaml());
                }
            }
            catch (InvalidOperationException)
            {
                Console.Error.WriteLine($"::warning file={source}::No TextAsset found");
            }

            if (lipsync == null)
            {
                return;
            }

            try
            {
                var ls = file.Objects.OfType <MonoBehaviour>().First().ToType();
                using var fs = lipsync.Create();
                JsonSerializer.SerializeAsync(fs, ls).Wait();
            }
            catch (InvalidOperationException)
            {
                Console.Error.WriteLine($"No MonoBehaviour in {source}");
            }
        }
        /// <summary>
        /// Extract text resource from const text file.
        /// </summary>
        /// <param name="source">The Unity asset file.</param>
        /// <param name="json">Export json to given file. If this is null, no export is done.</param>
        /// <param name="yaml">Export yaml to given file. If this is null, no export is done.</param>
        public static void ExtractConstText(FileInfo source, FileInfo?json = null, FileInfo?yaml = null)
        {
            var file = Unity3dResource.LoadAssetFile(source);
            var ls   = file.Objects.OfType <MonoBehaviour>().First().ToType();
            var data = ls["dataArray"];

            if (data is not List <object> list)
            {
                Console.Error.WriteLine($"Invalid dataArray type {data?.GetType()}.");
                return;
            }

            if (json != null)
            {
                using var fs = json.Create();
                JsonSerializer.SerializeAsync(fs, list, Json.Options).Wait();
            }

            if (yaml != null)
            {
                var dict = new Dictionary <int, ConstText>();
                list.ForEach(x =>
                {
                    if (x is OrderedDictionary od && od["TextId"] is int id && od["TextString"] is string str)
                    {
                        str        = str.Replace("\\n", "\n");
                        var textId = (TextId)id;
                        dict.Add(id,
                                 new ConstText
                        {
                            Name = Enum.IsDefined(textId) ? textId.ToString() : null, Value = str
                        });
                    }
                });

                using var fy = yaml.CreateText();
                var serializer = new SerializerBuilder()
                                 .WithEventEmitter(next => new LiteralMultilineEmitter(next))
                                 .Build();
                serializer.Serialize(fy, dict);
            }
        }