コード例 #1
0
        public override void Initialize(Verse.CompProperties props)
        {
            base.Initialize(props);
            // Do 4 things
            //   1.  Create Controller for saving/loading in proper order (do once)
            //   2.  most important: give each compRefuelable from the def a unique id for save/load
            //   3.  make linked list
            //   4.  make sure the fuel has a decent label
            CompRefuelable_Multi crm = this.parent.GetComp <CompRefuelable_Multi>();

            if (crm == this) // "first post!"
            {
                id = 0;
                this.controller = (LWM.Multi_Fuel_Requirement.Controller)Activator.
                                  CreateInstance(typeof(LWM.Multi_Fuel_Requirement.Controller));
                controller.parent = parent;
                controller.Initialize(new PlaceHolder());
                parent.AllComps.Add(controller);
                controller.fuels.Add(this);
            }
            else
            {
                this.controller = crm.controller;
                controller.fuels.Add(this);
                // Build a linked list of the CompRefuelable_Multis this parent has:
                //   This will give us a faster way to check all of the than searching the whole
                //   List<> by way of GetComps<CompRefuelable_Multi>
                int count = 1;
                while (crm.next != null)
                {
                    crm = crm.next;
                    count++;
                }
                crm.next = this;
                id       = count; // unique label for save/load
                // Move the controller to the end of the list: (slightly inefficient, but necessary)
                var c = parent.GetComp <LWM.Multi_Fuel_Requirement.Controller>();
                parent.AllComps.Remove(c);
                parent.AllComps.Add(c);
            }

            // It would be nice to tell the fuels apart:
            //   (we do this here because we can override Initialize.
            //    we can't override for CompProperties_Refuelable?)
            if (this.Props.fuelLabel.NullOrEmpty())
            {
                this.Props.fuelLabel = this.Props.fuelFilter.Summary;
            }
        } // initialize
コード例 #2
0
        public override void PostExposeData() // where save/load is handled
        {
            base.PostExposeData();
            // saving data: the controller handles the order:
            CompRefuelable_Multi crm = parent.GetComp <CompRefuelable_Multi>();
            string orderString       = "" + crm.id;

            while (crm.next != null)
            {
                crm          = crm.next;
                orderString += ":" + crm.id;
            }
            //Log.Warning("Saving order- " + orderString);
            Scribe_Values.Look <string>(ref orderString, "LWM_CRM_Order");
            // On load, put things in the right order!
            //   We can get away with this because the controller is
            //   guaranteed to be *after* the CompRefuelable_Multis
            //   and rearranging them is okay
            var allCrms  = parent.GetComps <CompRefuelable_Multi>().ToList();
            var allComps = parent.AllComps;
            //Log.Warning("Loading order- " + orderString);
            var orders = orderString.Split(':').Reverse();// reverse order so can put at beginning easily

            // Go thru what the order should be
            foreach (string s in orders)
            {
                int which = Int32.Parse(s); // let's be honest.  Won't need Int64 :p
                                            // Find the right ones and put them in:
                crm = allCrms.Find(x => x.id == which);
                if (crm == null)
                {
                    Log.Error("LWM.Multi_Fuel_Requirement: Tried to load fuel " + which +
                              " but could not find it in def. Destroying " +
                              parent.ToString() + " and aborting.");
                    parent.Destroy();
                    return;
                }
                allComps.Remove(crm);
                allComps.Insert(0, crm); // put at beginning
            }
            this.first = parent.GetComp <CompRefuelable_Multi>();
            // Each CompRefuelable_Multi handles its own fuel amounts, etc.
        }