/// <summary>
        /// Main mod constructor that initializes Harmony patches.
        /// </summary>
        /// <param name="content"></param>
        public PersistentWorldsMod(ModContentPack content) : base(content)
        {
            var harmony = new Harmony("me.filunderscore.persistentrimworlds");

#if DEBUG
            Harmony.DEBUG = true;

            FileLog.StartLoggingToFile("debug_log.txt");
#endif

            harmony.PatchAll(Assembly.GetExecutingAssembly());
        }
Beispiel #2
0
        static void Postfix(PostLoadIniter __instance, IExposable s)
        {
            if (Scribe.mode != LoadSaveMode.LoadingVars || PersistentWorldManager.GetInstance().PersistentWorldNotNullAndLoadStatusIs(PersistentWorldLoadSaver.PersistentWorldLoadStatus.Converting))
            {
                return;
            }

            if (!PersistentWorldManager.GetInstance().HasPersistentWorld)
            {
                return;
            }

            var persistentWorld = PersistentWorldManager.GetInstance().PersistentWorld;

            if (s == null || !(s is ILoadReferenceable referenceable))
            {
                return;
            }

            // Decreases load times.
            if (referenceable is Thing && !(referenceable is Pawn) && Scribe.loader.curXmlParent.HasChildNodes && Scribe.loader.curXmlParent.ChildNodes[0].Name == "thing")
            {
                return;
            }

            var path = FindParent(Scribe.loader.curXmlParent);

            FileLog.Log("Path: " + path);

            var pathToLoad = "";
            var label      = "";

            if (Scribe.loader.curXmlParent.HasChildNodes)
            {
                label = Scribe.loader.curXmlParent.ChildNodes[0].Name;

                var currentListIndex = Regex.Matches(path + "/", "\\/li\\[(\\d+)\\]\\/").Count;

                if (Scribe.loader.curXmlParent.ChildNodes[0].Name == "li")
                {
                    ++currentListIndex;
                }

                if (Scribe.loader.curXmlParent.ChildNodes[0].Name == "li")
                {
                    if (listIndexes.ContainsKey(currentListIndex))
                    {
                        listIndexes[currentListIndex] += 1;
                    }
                    else
                    {
                        listIndexes.Add(currentListIndex, 0);
                    }

                    pathToLoad = path + "/" +
                                 Scribe.loader.curXmlParent.ChildNodes[0].Name + "[" + listIndexes[currentListIndex] + "]";

                    FileLog.Log("Path LI: " + pathToLoad);
                }
                else if (Scribe.loader.curXmlParent.ChildNodes[0].Name == "thing")
                {
                    pathToLoad = path + "/" +
                                 Scribe.loader.curXmlParent.ChildNodes[0].Name + "[" + ++currentThingIndex + "]";

                    FileLog.Log("Path THING: " + pathToLoad);
                }
                else
                {
                    FileLog.Log("CurPathRelToParent Child Nodes: " + pathToLoad + "/" + Scribe.loader.curXmlParent.ChildNodes[0].Name);
                }
            }
            else
            {
                FileLog.Log("CurPathRelToParent: " + path);
            }

            FileLog.Log("Adding reference: " + pathToLoad);
            FileLog.Log("Ref ID: " + referenceable.GetUniqueLoadID());

            if (!persistentWorld.LoadSaver.ReferenceTable.ContainsReferenceWithLoadId(referenceable.GetUniqueLoadID()))
            {
                persistentWorld.LoadSaver.ReferenceTable.LoadReferenceIntoMemory(referenceable, pathToLoad);
            }
        }
        static bool Prefix(ScribeSaver __instance, string nodeName)
        {
            if (!PersistentWorldManager.GetInstance().PersistentWorldNotNull())
            {
                return(true);
            }

            var writer = WriterField.GetValue(__instance);

            if (writer == null)
            {
                return(true);
            }

            var curPath = (string)CurPathField.GetValue(__instance);

            curPath = curPath + "/" + nodeName;

            var currentListIndex = Regex.Matches(curPath + "/", "\\/li\\[(\\d+)\\]\\/").Count;

            if (nodeName == "li")
            {
                ++currentListIndex;
            }

            FileLog.Log("Current list index before: " + currentListIndex);
            FileLog.Log("Curpath before: " + curPath);

            if (nodeName == "li" || nodeName == "thing")
            {
                if (nodeName == "li")
                {
                    if (ListIndexes.ContainsKey(currentListIndex))
                    {
                        ListIndexes[currentListIndex] += 1;
                    }
                    else
                    {
                        ListIndexes.Add(currentListIndex, 0);
                    }

                    FileLog.Log("CurPath b4 set: " + curPath);
                    curPath = curPath + "[" + ListIndexes[currentListIndex] + "]";
                    FileLog.Log("CurPath after set: " + curPath);
                }
                else
                {
                    curPath = curPath + "[" + currentThingIndex++ + "]";
                }
            }
            else
            {
                if (currentListIndex < ListIndexes.Count)
                {
                    ListIndexes.RemoveAll(set => set.Key > currentListIndex);
                }
            }

            FileLog.Log("Current list index after: " + currentListIndex);
            FileLog.Log("Curpath after: " + curPath);

            CurPathField.SetValue(__instance, curPath);

            return(true);
        }