Exemple #1
0
        public void                         ReIntialize()
        {
            // Call controller Initialize() on game load
            var subControllers = Controller.Data.SubControllers.ToList();

            subControllers.Sort((x, y) => (x.InitializationPriority > y.InitializationPriority) ? -1 : 1);

            foreach (var subsys in subControllers)
            {
                if (subsys.InitializationPriority != SubController.DontProcessThisPhase)
                {
                    if (
                        (subsys.State >= SubControllerState._BaseOk) &&
                        (subsys.ReinitializeOnGameLoad)
                        )
                    {
                        if (!subsys.Initialize())
                        {
                            CCL_Log.Error(subsys.strReturn, subsys.Name + " :: Reinitialization");
                            gameValid = false;
                            enabled   = false;
                            return;
                        }
                        if (subsys.strReturn != string.Empty)
                        {
                            CCL_Log.Message(subsys.strReturn, subsys.Name + " :: Reinitialization");
                        }
                    }
                }
            }
            ticks = 0;
        }
        public static void DrawThing(Texture2D texture, Thing thing, Color color)
        {
#if DEVELOPER
            CCL_Log.Message("Painting cells for " + thing.LabelCap + thing.Position + color);
#endif

            // check if this makes sense
            if (texture == null)
            {
                CCL_Log.Error("Tried to draw to NULL texture");
                return;
            }
            if (thing == null)
            {
                CCL_Log.Error("Tried to get occupied rect for NULL thing");
                return;
            }
            if (
                (thing.OccupiedRect().Cells == null) ||
                (thing.OccupiedRect().Cells.Count() == 0)
                )
            {
                CCL_Log.Error("Tried to get occupier rect for " + thing.LabelCap + " but it is NULL or empty");
                return;
            }

            // paint all cells occupied by thing in 'color'.
            foreach (var cell in thing.OccupiedRect().Cells)
            {
                if (cell.InBounds())
                {
                    texture.SetPixel(cell.x, cell.z, color);
                }
            }
        }
Exemple #3
0
        public void                         UpdateSubControllers()
        {
            if (UpdateControllers == null)
            {
                // Create a list of sub controllers in update order
                UpdateControllers = Controller.Data.SubControllers.ToList();
                UpdateControllers.Sort((x, y) => (x.UpdatePriority > y.UpdatePriority) ? -1 : 1);
            }

            foreach (var subsys in UpdateControllers)
            {
                if (subsys.UpdatePriority != SubController.DontProcessThisPhase)
                {
                    if (
                        (subsys.State == SubControllerState.Ok) &&
                        (subsys.IsHashIntervalTick(ticks))
                        )
                    {
                        if (!subsys.Update())
                        {
                            CCL_Log.Error(subsys.strReturn, subsys.Name + " :: Update");
                            return;
                        }
                        if (subsys.strReturn != string.Empty)
                        {
                            CCL_Log.Message(subsys.strReturn, subsys.Name + " :: Update");
                        }
                    }
                }
            }
        }
Exemple #4
0
        public static void                         CreateMonoBehaviour()
        {
            var gameObject = new GameObject(Controller.Data.UnityObjectName);

            if (gameObject == null)
            {
                CCL_Log.Error("Unable to create GameObject");
                initOk = false;
                return;
            }
            else
            {
                if (gameObject.AddComponent <Controller.MainMonoBehaviour>() == null)
                {
                    CCL_Log.Error("Unable to create MonoBehaviour");
                    initOk = false;
                    return;
                }
                else
                {
                    UnityEngine.Object.DontDestroyOnLoad(gameObject);
                    Controller.Data.UnityObject = gameObject;
                }
            }

            CCL_Log.Message("Queueing Library Initialization");
            LongEventHandler.QueueLongEvent(Initialize, "LibraryStartup", true, null);
        }
        public override bool                Initialize()
        {
            // Don't auto-gen help if "quicktest" or "nohelp" command line switches are used
            if (
                (!GenCommandLine.CommandLineArgPassed("quicktest")) &&
                (!GenCommandLine.CommandLineArgPassed("nohelp"))
                )
            {
                LongEventHandler.SetCurrentEventText("LibraryHelpGen".Translate());

                var stringBuilder = new StringBuilder();
                CCL_Log.CaptureBegin(stringBuilder);

                var startTime = DateTime.Now;

                if (!HelpBuilder.ResolveImpliedDefs())
                {
                    strReturn = "Unexpected error in HelpBuilder.ResolveImpliedDefs()";
                    State     = SubControllerState.InitializationError;
                    return(false);
                }

                var finishTime = DateTime.Now;
                var finalTime  = finishTime - startTime;

                CCL_Log.CaptureEnd(stringBuilder, string.Format("Completed in {0}", finalTime.ToString()));
                CCL_Log.Message(stringBuilder.ToString(), "Help System");

                LongEventHandler.SetCurrentEventText("Initializing".Translate());
            }
            strReturn = "Initialized";
            State     = SubControllerState.Hybernating;
            return(true);
        }
Exemple #6
0
        internal static void                DumpThingsRequestedForGroup(ThingRequest thingRequest, List <Thing> thingsRequested)
        {
            var str = string.Format("ListerThings.ThingsMatching( {0} ) ::\n", thingRequest);

            foreach (var thing in thingsRequested)
            {
                str += string.Format("\t{0} - {1}\n", thing.ThingID, thing.def.defName);
            }
            CCL_Log.Message(str);
        }
Exemple #7
0
        public static void                         Initialize()
        {
            //enabled = false;
            gameValid = false;

            if (!initOk)
            {
                return;
            }

            var subControllers = Controller.Data.SubControllers.ToList();

            if (subControllers.NullOrEmpty())
            {
                CCL_Log.Error("SubControllers array is empty!");
                return;
            }

            LongEventHandler.SetCurrentEventText("LibraryValidation".Translate());

            // Validate all subs-systems
            subControllers.Sort((x, y) => (x.ValidationPriority > y.ValidationPriority) ? -1 : 1);
            foreach (var subsys in subControllers)
            {
                if (subsys.ValidationPriority != SubController.DontProcessThisPhase)
                {
                    if (!subsys.Validate())
                    {
                        CCL_Log.Error(subsys.strReturn, subsys.Name + " :: Validation");
                        return;
                    }
                    if (subsys.strReturn != string.Empty)
                    {
                        CCL_Log.Message(subsys.strReturn, subsys.Name + " :: Validations");
                    }
                }
                else
                {
                    subsys.State = SubControllerState.Validated;
                }
            }

            LongEventHandler.SetCurrentEventText("Initializing".Translate());

            // Initialize all sub-systems
            subControllers.Sort((x, y) => (x.InitializationPriority > y.InitializationPriority) ? -1 : 1);
            foreach (var subsys in subControllers)
            {
                if (subsys.InitializationPriority != SubController.DontProcessThisPhase)
                {
                    if (!subsys.Initialize())
                    {
                        CCL_Log.Error(subsys.strReturn, subsys.Name + " :: Initialization");
                        return;
                    }
                    if (subsys.strReturn != string.Empty)
                    {
                        CCL_Log.Message(subsys.strReturn, subsys.Name + " :: Initialization");
                    }
                }
                else
                {
                    subsys.State = SubControllerState.Ok;
                }
            }

            CCL_Log.Message("Initialized");

            // Yay!
            gameValid = true;
            //enabled = true;
            ticks = 0;
        }
Exemple #8
0
        private static void                 PreLoad()
        {
            // This is a pre-start sequence to hook some deeper level functions.
            // These functions can be hooked later but it would be after the sequence
            // of operations which call them is complete.

            // Log CCL version
            Version.Log();

            bool          InjectionsOk  = true;
            StringBuilder stringBuilder = new StringBuilder();

            CCL_Log.CaptureBegin(stringBuilder);

            // Find all sub-controllers
            var subControllerClasses = typeof(SubController).AllSubclasses();
            var subControllerCount   = subControllerClasses.Count();

            if (subControllerCount == 0)
            {
                InjectionsOk = false;
                CCL_Log.Error(
                    "Unable to find sub-controllers",
                    "PreLoader"
                    );
            }

            // Create sub-controllers
            if (InjectionsOk)
            {
                var subControllers = new SubController[subControllerCount];
                for (int index = 0; index < subControllerCount; ++index)
                {
                    var subControllerType = subControllerClasses.ElementAt(index);
                    var subController     = (SubController)Activator.CreateInstance(subControllerType);
                    if (subController == null)
                    {
                        CCL_Log.Error(
                            string.Format("Unable to create sub-controller {0}", subControllerType.Name),
                            "PreLoader"
                            );
                        InjectionsOk = false;
                        break;
                    }
                    else
                    {
                        subControllers[index] = subController;
                    }
                }
                if (InjectionsOk)
                {
                    Controller.Data.SubControllers = subControllers;
                }
            }

            // Detour Verse.PlayDataLoader.LoadAllPlayData
            if (InjectionsOk)
            {
                MethodInfo Verse_PlayDataLoader_LoadAllPlayData = typeof(PlayDataLoader).GetMethod("LoadAllPlayData", BindingFlags.Static | BindingFlags.Public);
                MethodInfo CCL_PlayDataLoader_LoadAllPlayData   = typeof(Detour._PlayDataLoader).GetMethod("_LoadAllPlayData", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(Verse_PlayDataLoader_LoadAllPlayData, CCL_PlayDataLoader_LoadAllPlayData);
            }

            // Detour Verse.PlayDataLoader.ClearAllPlayData
            if (InjectionsOk)
            {
                MethodInfo Verse_PlayDataLoader_ClearAllPlayData = typeof(PlayDataLoader).GetMethod("ClearAllPlayData", BindingFlags.Static | BindingFlags.Public);
                MethodInfo CCL_PlayDataLoader_ClearAllPlayData   = typeof(Detour._PlayDataLoader).GetMethod("_ClearAllPlayData", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(Verse_PlayDataLoader_ClearAllPlayData, CCL_PlayDataLoader_ClearAllPlayData);
            }

            // Detour Verse.UIRoot_Entry.ShouldShowMainMenuGUI_get
            if (InjectionsOk)
            {
                PropertyInfo Verse_UIRoot_Entry_ShouldShowMainMenuGUI     = typeof(UIRoot_Entry).GetProperty("ShouldShowMainMenuGUI", BindingFlags.Instance | BindingFlags.NonPublic);
                MethodInfo   Verse_UIRoot_Entry_ShouldShowMainMenuGUI_get = Verse_UIRoot_Entry_ShouldShowMainMenuGUI.GetGetMethod(true);
                MethodInfo   CCL_UIRoot_Entry_ShouldShowMainMenuGUI_get   = typeof(Detour._UIRoot_Entry).GetMethod("_ShouldShowMainMenuGUI_get", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(Verse_UIRoot_Entry_ShouldShowMainMenuGUI_get, CCL_UIRoot_Entry_ShouldShowMainMenuGUI_get);
            }

            // Detour RimWorld.MainMenuDrawer.MainMenuOnGUI
            if (InjectionsOk)
            {
                MethodInfo RimWorld_MainMenuDrawer_MainMenuOnGUI = typeof(MainMenuDrawer).GetMethod("MainMenuOnGUI", BindingFlags.Static | BindingFlags.Public);
                MethodInfo CCL_MainMenuDrawer_MainMenuOnGUI      = typeof(Detour._MainMenuDrawer).GetMethod("_MainMenuOnGUI", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(RimWorld_MainMenuDrawer_MainMenuOnGUI, CCL_MainMenuDrawer_MainMenuOnGUI);
            }

            // Detour RimWorld.MainMenuDrawer.DoMainMenuButtons
            if (InjectionsOk)
            {
                MethodInfo RimWorld_MainMenuDrawer_DoMainMenuButtons = typeof(MainMenuDrawer).GetMethod("DoMainMenuButtons", BindingFlags.Static | BindingFlags.Public);
                MethodInfo CCL_MainMenuDrawer_DoMainMenuButtons      = typeof(Detour._MainMenuDrawer).GetMethod("_DoMainMenuButtons", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(RimWorld_MainMenuDrawer_DoMainMenuButtons, CCL_MainMenuDrawer_DoMainMenuButtons);
            }

            // Detour RimWorld.BiomeDef.CommonalityOfAnimal
            if (InjectionsOk)
            {
                MethodInfo RimwWorld_BiomeDef_CommonalityOfAnimal = typeof(BiomeDef).GetMethod("CommonalityOfAnimal", BindingFlags.Instance | BindingFlags.Public);
                MethodInfo CCL_BiomeDef_CommonalityOfAnimal       = typeof(Detour._BiomeDef).GetMethod("_CommonalityOfAnimal", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(RimwWorld_BiomeDef_CommonalityOfAnimal, CCL_BiomeDef_CommonalityOfAnimal);
            }

            // Detour RimWorld.BiomeDef.CommonalityOfPlant
            if (InjectionsOk)
            {
                MethodInfo RimwWorld_BiomeDef_CommonalityOfPlant = typeof(BiomeDef).GetMethod("CommonalityOfPlant", BindingFlags.Instance | BindingFlags.Public);
                MethodInfo CCL_BiomeDef_CommonalityOfPlant       = typeof(Detour._BiomeDef).GetMethod("_CommonalityOfPlant", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(RimwWorld_BiomeDef_CommonalityOfPlant, CCL_BiomeDef_CommonalityOfPlant);
            }
            // Detour RimWorld.BiomeDef.MTBDaysOfDisease
            if (InjectionsOk)
            {
                MethodInfo RimWorld_BiomeDef_MTBDaysOfDisease = typeof(BiomeDef).GetMethod("MTBDaysOfDisease", BindingFlags.Instance | BindingFlags.Public);
                MethodInfo CCL_BiomeDef_MTBDaysOfDisease      = typeof(Detour._BiomeDef).GetMethod("_MTBDaysOfDisease", BindingFlags.Static | BindingFlags.NonPublic);
                InjectionsOk &= Detours.TryDetourFromTo(RimWorld_BiomeDef_MTBDaysOfDisease, CCL_BiomeDef_MTBDaysOfDisease);
            }

            // Detour Verse.PostLoadInitter.DoAllPostLoadInits

            /*
             * if( InjectionsOk )
             * {
             *  MethodInfo Verse_PostLoadInitter_DoAllPostLoadInits = typeof( PostLoadInitter ).GetMethod( "DoAllPostLoadInits", BindingFlags.Static | BindingFlags.Public );
             *  MethodInfo CCL_PostLoadInitter_DoAllPostLoadInits = typeof( Detour._PostLoadInitter ).GetMethod( "_DoAllPostLoadInits", BindingFlags.Static | BindingFlags.NonPublic );
             *  InjectionsOk &= Detours.TryDetourFromTo( Verse_PostLoadInitter_DoAllPostLoadInits, CCL_PostLoadInitter_DoAllPostLoadInits );
             * }
             */

            if (InjectionsOk)
            {
                var gameObject = new GameObject(Controller.Data.UnityObjectName);
                if (gameObject == null)
                {
                    InjectionsOk = false;
                    CCL_Log.Error(
                        "Unable to create GameObject",
                        "PreLoader"
                        );
                }
                else
                {
                    if (gameObject.AddComponent <Controller.MainMonoBehaviour>() == null)
                    {
                        InjectionsOk = false;
                        CCL_Log.Error(
                            "Unable to create MonoBehaviour",
                            "PreLoader"
                            );
                    }
                    else
                    {
                        UnityEngine.Object.DontDestroyOnLoad(gameObject);
                        Controller.Data.UnityObject = gameObject;
                    }
                }
            }

            if (InjectionsOk)
            {
                CCL_Log.Message(
                    "Queueing Library Initialization",
                    "PreLoader"
                    );
                LongEventHandler.QueueLongEvent(Initialize, "LibraryStartup", true, null);
            }

            CCL_Log.CaptureEnd(
                stringBuilder,
                InjectionsOk ? "Initialized" : "Errors during injection"
                );
            CCL_Log.Trace(
                Verbosity.Injections,
                stringBuilder.ToString(),
                "PreLoader");
        }
        // Validate ModHelperDefs, CCL load order, CCL versioning
        public override bool Validate()
        {
            // Hopefully...
            var stringBuilder = new StringBuilder();
            var rVal          = true;

            CCL_Log.CaptureBegin(stringBuilder);

            // Limit one ModHelperDef per mod
            // Create the ordered list by inserting dummies for mods which don't have one
            var allMods = LoadedModManager.RunningMods.ToList();

            // Find Core and CCL in the mod order
            coreModIndex = -1;
            cclModIndex  = -1;
            for (int i = 0; i < allMods.Count; ++i)
            {
                ModContentPack mod = allMods[i];
                if (mod.Identifier == ModContentPack.CoreModIdentifier)
                {
                    coreModIndex = i;
                }
                if (mod.Name == Controller.Data.UnityObjectName)
                {
                    Controller.Data.cclModIdentifier = mod.Identifier;
                    cclModIndex = i;
                }
            }
            if (coreModIndex == -1)
            {
                LongEventHandler.ExecuteWhenFinished(ShowLoadOrderWindow);
                CCL_Log.Error(string.Format("Unable to find '{0}' in mod load order!", ModContentPack.CoreModIdentifier));
                //rVal = false; // Don't throw as an error, will be caught special
            }
            else if (coreModIndex != 0)
            {
                LongEventHandler.ExecuteWhenFinished(ShowLoadOrderWindow);
                CCL_Log.Error(string.Format("'{0}' must be first in mod load order!", ModContentPack.CoreModIdentifier));
                //rVal = false; // Don't throw as an error, will be caught special
            }
            if (cclModIndex == -1)
            {
                LongEventHandler.ExecuteWhenFinished(ShowLoadOrderWindow);
                CCL_Log.Error(string.Format("Unable to find '{0}' in mod load order!", Controller.Data.UnityObjectName));
                //rVal = false; // Don't throw as an error, will be caught special
            }
            else if (cclModIndex != 1)
            {
                LongEventHandler.ExecuteWhenFinished(ShowLoadOrderWindow);
                CCL_Log.Error(string.Format("'{0}' must be second in mod load order, immediately after '{1}'! :: Current position is #{2}", Controller.Data.UnityObjectName, ModContentPack.CoreModIdentifier, (cclModIndex + 1).ToString()));
                //rVal = false; // Don't throw as an error, will be caught special
            }
            else if (Controller.Data.cclModIdentifier == string.Empty)
            {
                LongEventHandler.ExecuteWhenFinished(ShowLoadOrderWindow);
                CCL_Log.Error(string.Format("Unable to identify '{0}' in mod load order!", Controller.Data.UnityObjectName));
                //rVal = false; // Don't throw as an error, will be caught special
            }
            if (rVal)
            {
                for (int i = 0; i < allMods.Count; i++)
                {
                    var modHelperDef  = (ModHelperDef)null;
                    var mod           = allMods[i];
                    var modHelperDefs = Find_Extensions.DefListOfTypeForMod <ModHelperDef>(mod);
                    if (!modHelperDefs.NullOrEmpty())
                    {
                        if (modHelperDefs.Count > 1)
                        {
                            CCL_Log.Error(string.Format("'{0}' has multiple ModHelperDefs!", mod.Name));
                            rVal = false;
                        }
                        else
                        {
                            // Validate the def
                            modHelperDef = modHelperDefs.First();
                            if (!modHelperDef.IsValid)
                            {
                                // Don't do anything special with broken mods
                                CCL_Log.Error(string.Format("ModHelperDef for '{0}' is invalid!", mod.Name));
                                rVal = false;
                            }
                            else if (!modHelperDef.dummy)
                            {
                                // Don't show validation message for dummy defs
                                CCL_Log.Message(string.Format("{0} :: Passed validation, requesting v{1}", mod.Name, modHelperDef.minCCLVersion), "ModHelperDef");
                            }
                        }
                    }
                    else if (rVal == true)
                    {
                        // Doesn't exist, create a dummy for logging but only
                        // create if we're not just checking for remaining errors
                        modHelperDef               = new ModHelperDef();
                        modHelperDef.defName       = mod.Name + "_ModHelperDef";
                        modHelperDef.minCCLVersion = Version.Minimum.ToString();
                        modHelperDef.ModName       = mod.Name;
                        modHelperDef.Verbosity     = Verbosity.NonFatalErrors;
                        modHelperDef.dummy         = true;
                    }
                    if (rVal == true)
                    {
                        // No errors, def is valid or a dummy
                        // Associate the def with the mod (the dictionary is to go the other way)
                        modHelperDef.mod = mod;
                        // Insert it into it's ordered place in the lists
                        Controller.Data.Mods.Insert(i, mod);
                        Controller.Data.ModHelperDefs.Insert(i, modHelperDef);
                        // Add a dictionary entry
                        Controller.Data.DictModHelperDefs.Add(mod, modHelperDef);
                    }
                }
                // Should now be a complete pair of lists in mod load order
                // as well as a dictionary of mods and their defs

#if DEVELOPER
                //Dump ordered list of mods and their defs
                string dump = "Mod load order:\n";
                for (int i = 0; i < Controller.Data.Mods.Count; i++)
                {
                    dump += string.Format("\t[{0} - {1}] - {2} - {3}{4}\n", i, Controller.Data.Mods[i].Identifier, Controller.Data.Mods[i].Name, Controller.Data.ModHelperDefs[i].defName, (Controller.Data.ModHelperDefs[i].dummy ? " - dummy" : ""));
                }
                CCL_Log.Write(dump);
#endif
                if (rVal)
                {
                    ModContentPack CCL_Mod       = Controller.Data.Mods[cclModIndex];
                    ModHelperDef   CCL_HelperDef = Find_Extensions.ModHelperDefForMod(CCL_Mod);

                    // Validate xml version with assembly version
                    var vc = Version.Compare(CCL_HelperDef.minCCLVersion);
                    if (vc != Version.VersionCompare.ExactMatch)
                    {
                        CCL_Log.Error(string.Format("Version mismatch for {0}!", Controller.Data.UnityObjectName), "ModHelperDef");
                        rVal = false;
                    }

                    // CCL rank is #2 in load order and def version matches library
                    Controller.Data.cclMod       = CCL_Mod;
                    Controller.Data.cclHelperDef = CCL_HelperDef;
                }
            }

            // Should be all good or up until the first error encountered
            CCL_Log.CaptureEnd(
                stringBuilder,
                rVal ? "Validated" : "Errors during validation"
                );
            strReturn = stringBuilder.ToString();
            // Return true if all mods OK, false if any failed validation
            State = rVal ? SubControllerState.Validated : SubControllerState.ValidationError;
            return(rVal);
        }