예제 #1
0
        private void LoadOthers <T>(string json, ContentIndexPackData parent = null) where T : BasePackData
        {
            if (!this.pack.smapiPack.HasFile(json))
            {
                if (parent != null)
                {
                    Log.Warn("Missing json file: " + json);
                }
                return;
            }
            if (parent == null)
            {
                parent = new ContentIndexPackData()
                {
                    pack        = this.pack,
                    parent      = null,
                    ContentType = typeof(T).Name.Substring(0, typeof(T).Name.Length - "PackData".Length),
                    FilePath    = json,
                };
                parent.original          = (ContentIndexPackData)parent.Clone();
                parent.original.original = parent.original;
            }

            try
            {
                var data = this.pack.smapiPack.LoadAsset <List <T> >(json) ?? new List <T>();
                int i    = 0;
                foreach (var d in data)
                {
                    Log.Trace("Loading data<" + typeof(T) + ">...");
                    try
                    {
                        this.pack.others.Add(d);
                        if (!this.pack.enableIndex.ContainsKey(parent))
                        {
                            this.pack.enableIndex.Add(parent, new());
                        }
                        this.pack.enableIndex[parent].Add(d);
                        d.pack              = this.pack;
                        d.parent            = parent;
                        d.original          = (T)d.Clone();
                        d.original.original = d.original;
                        d.PostLoad();
                    }
                    catch (Exception e)
                    {
                        Log.Debug($"Exception loading item entry {i} from {json}: " + e);
                    }
                    ++i;
                }
            }
            catch (Exception e)
            {
                Log.Error("Exception loading data of type " + typeof(T) + ": " + e);
            }
        }
        private void LoadIndex(string json, ContentIndexPackData parent = null)
        {
            if (!this.pack.smapiPack.HasFile(json))
            {
                if (parent != null)
                {
                    Log.Warn("Missing json file: " + json);
                }
                return;
            }
            if (parent == null)
            {
                parent = new ContentIndexPackData()
                {
                    pack        = this.pack,
                    parent      = null,
                    ContentType = "ContentIndex",
                    FilePath    = json,
                };
                parent.original          = (ContentIndexPackData)parent.Clone();
                parent.original.original = parent.original;
            }

            try
            {
                var colorConverter = (JsonConverter)AccessTools.Constructor(AccessTools.TypeByName("StardewModdingAPI.Framework.Serialization.ColorConverter")).Invoke(Array.Empty <object>());
                var vec2Converter  = (JsonConverter)AccessTools.Constructor(AccessTools.TypeByName("StardewModdingAPI.Framework.Serialization.Vector2Converter")).Invoke(Array.Empty <object>());
                var rectConverter  = (JsonConverter)AccessTools.Constructor(AccessTools.TypeByName("StardewModdingAPI.Framework.Serialization.RectangleConverter")).Invoke(Array.Empty <object>());
                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    Converters = new[] { new BasePackDataListConverter(), colorConverter, vec2Converter, rectConverter }
                };
                var data = JsonConvert.DeserializeObject <List <BasePackData> >(File.ReadAllText(Path.Combine(this.pack.smapiPack.DirectoryPath, json)), settings);
                foreach (var d in data)
                {
                    if (d is CommonPackData cd && this.pack.items.ContainsKey(cd.ID))
                    {
                        Log.Error("Duplicate found! " + cd.ID);
                        continue;
                    }
                    Log.Trace("Loading data< " + d.GetType() + " >...");

                    if (!this.pack.enableIndex.ContainsKey(parent))
                    {
                        this.pack.enableIndex.Add(parent, new());
                    }
                    this.pack.enableIndex[parent].Add(d);
                    d.pack              = this.pack;
                    d.parent            = parent;
                    d.original          = (BasePackData)d.Clone();
                    d.original.original = d.original;

                    if (d is CommonPackData cdata)
                    {
                        this.pack.items.Add(cdata.ID, cdata);
                        Mod.itemLookup.Add($"{this.pack.smapiPack.Manifest.UniqueID}/{cdata.ID}".GetDeterministicHashCode(), $"{this.pack.smapiPack.Manifest.UniqueID}/{cdata.ID}");
                    }
                    else
                    {
                        this.pack.others.Add(d);
                    }
                    d.PostLoad();

                    if (d is ContentIndexPackData cidata)
                    {
                        this.LoadIndex(cidata.FilePath, cidata);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Exception loading content index: \"" + json + "\": " + e);
            }
        }
예제 #3
0
        private void LoadIndex(string json, ContentIndexPackData parent = null)
        {
            if (!this.pack.smapiPack.HasFile(json))
            {
                if (parent != null)
                {
                    Log.Warn("Missing json file: " + json);
                }
                return;
            }
            if (parent == null)
            {
                parent = new ContentIndexPackData()
                {
                    pack        = this.pack,
                    parent      = null,
                    ContentType = "ContentIndex",
                    FilePath    = json,
                };
                parent.original          = (ContentIndexPackData)parent.Clone();
                parent.original.original = parent.original;
            }

            try
            {
                var data = this.pack.smapiPack.LoadAsset <List <ContentIndexPackData> >(json) ?? new List <ContentIndexPackData>();
                foreach (var d in data)
                {
                    Log.Trace("Loading data<" + typeof(ContentIndexPackData) + "> " + d.ContentType + " " + d.FilePath + "...");
                    this.pack.others.Add(d);
                    if (!this.pack.enableIndex.ContainsKey(parent))
                    {
                        this.pack.enableIndex.Add(parent, new());
                    }
                    this.pack.enableIndex[parent].Add(d);
                    d.pack              = this.pack;
                    d.parent            = parent;
                    d.original          = (ContentIndexPackData)d.Clone();
                    d.original.original = d.original;
                    d.PostLoad();

                    var packDataType = Type.GetType("DynamicGameAssets.PackData." + d.ContentType + "PackData");
                    if (packDataType == null)
                    {
                        Log.Error("Invalid ContentType: " + d.ContentType);
                        continue;
                    }

                    MethodInfo baseMethod = null;
                    if (packDataType == typeof(ContentIndexPackData))
                    {
                        baseMethod = typeof(ContentPack).GetMethod(nameof(ContentPackLoaderV1.LoadIndex), BindingFlags.NonPublic | BindingFlags.Instance);
                    }
                    else if (packDataType.BaseType == typeof(CommonPackData))
                    {
                        baseMethod = typeof(ContentPack).GetMethod(nameof(ContentPackLoaderV1.LoadAndValidateItems), BindingFlags.NonPublic | BindingFlags.Instance);
                    }
                    else if (packDataType.BaseType == typeof(BasePackData))
                    {
                        baseMethod = typeof(ContentPack).GetMethod(nameof(ContentPackLoaderV1.LoadOthers), BindingFlags.NonPublic | BindingFlags.Instance);
                    }
                    else
                    {
                        throw new Exception("this should never happen");
                    }

                    MethodInfo genMethod = baseMethod.IsGenericMethod ? baseMethod.MakeGenericMethod(packDataType) : baseMethod;
                    genMethod.Invoke(this, new object[] { d.FilePath, d });
                }
            }
            catch (Exception e)
            {
                Log.Error("Exception loading content index: \"" + json + "\": " + e);
            }
        }
예제 #4
0
        private void LoadAndValidateItems <T>(string json, ContentIndexPackData parent = null) where T : CommonPackData
        {
            if (!this.pack.smapiPack.HasFile(json))
            {
                if (parent != null)
                {
                    Log.Warn("Missing json file: " + json);
                }
                return;
            }
            if (parent == null)
            {
                parent = new ContentIndexPackData()
                {
                    pack        = this.pack,
                    parent      = null,
                    ContentType = typeof(T).Name.Substring(0, typeof(T).Name.Length - "PackData".Length),
                    FilePath    = json,
                };
                parent.original          = (ContentIndexPackData)parent.Clone();
                parent.original.original = parent.original;
            }

            try
            {
                var data = this.pack.smapiPack.LoadAsset <List <T> >(json) ?? new List <T>();
                foreach (var d in data)
                {
                    if (this.pack.items.ContainsKey(d.ID))
                    {
                        Log.Error("Duplicate found! " + d.ID);
                        continue;
                    }
                    try
                    {
                        Log.Trace("Loading data<" + typeof(T) + ">: " + d.ID);
                        this.pack.items.Add(d.ID, d);
                        if (!this.pack.enableIndex.ContainsKey(parent))
                        {
                            this.pack.enableIndex.Add(parent, new());
                        }
                        this.pack.enableIndex[parent].Add(d);
                        Mod.itemLookup.Add($"{this.pack.smapiPack.Manifest.UniqueID}/{d.ID}".GetDeterministicHashCode(), $"{this.pack.smapiPack.Manifest.UniqueID}/{d.ID}");

                        /*if ( d is ShirtPackData )
                         *  Mod.itemLookup.Add( $"{smapiPack.Manifest.UniqueID}/{d.ID}".GetDeterministicHashCode() + 1, $"{smapiPack.Manifest.UniqueID}/{d.ID}" );
                         */
                        d.pack              = this.pack;
                        d.parent            = parent;
                        d.original          = (T)d.Clone();
                        d.original.original = d.original;
                        d.PostLoad();
                    }
                    catch (Exception e)
                    {
                        Log.Error("Exception loading item \"" + d.ID + "\": " + e);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error("Exception loading data of type " + typeof(T) + ": " + e);
            }
        }