/// <summary> /// Request a refresh of the data, intended to be called by event handlers. /// Will often refuse to refresh to save CPU time! /// Note that the callbacks may be called from background jobs, and so they /// should never do any Unity UI manipulation (unless you like hard crashes). /// Setting member variables seems to be safe. /// This is supposed to be the single entry point when any other class needs to /// load data for the plug-in. /// </summary> /// <param name="newOrigin">Body for which to calculate; can override some throttling behaviors if it's different from the last one</param> /// <param name="partialLoaded">Function to call when we have enough data for a simple display, but not quite complete</param> /// <param name="fullyLoaded">Function to call on successful completion of the load</param> /// <param name="aborted">Function to call if we decide not to load</param> /// <returns> /// True if we kicked off an actual refresh, false otherwise. /// </returns> public bool TryStartLoad(ITargetable newOrigin, LoadDoneCallback partialLoaded, LoadDoneCallback fullyLoaded, LoadDoneCallback aborted) { if (newOrigin != null) { // 1. Check whether we should even do anything, if not call aborted() and return if (!AllowStart(newOrigin)) { if (aborted != null) { aborted(); } return(false); } else { // 2. Start background job new Thread(() => ThreadStart(newOrigin, partialLoaded, fullyLoaded, aborted)).Start(); return(true); } } else { DbgFmt("Somebody tried to load with a null origin"); if (aborted != null) { aborted(); } return(false); } }
/// <summary> /// Construct a loader object for the given model /// </summary> /// <param name="m">Model object for us to manage</param> /// <param name="unReqNotif">Function to call if we trigger our own refresh without being asked (usually because a burn expired)</param> public AstrogationLoadBehaviorette(AstrogationModel m, LoadDoneCallback unReqNotif) { model = m; unrequestedLoadNotification = unReqNotif; // Watch for expiring burns StartBurnTimePolling(); }
/// <summary> /// Request a refresh of the data, intended to be called by event handlers. /// Will often refuse to refresh to save CPU time! /// Note that the callbacks may be called from background jobs, and so they /// should never do any Unity UI manipulation (unless you like hard crashes). /// Setting member variables seems to be safe. /// This is supposed to be the single entry point when any other class needs to /// load data for the plug-in. /// </summary> /// <param name="newOrigin">Body for which to calculate; can override some throttling behaviors if it's different from the last one</param> /// <param name="partialLoaded">Function to call when we have enough data for a simple display, but not quite complete</param> /// <param name="fullyLoaded">Function to call on successful completion of the load</param> /// <param name="aborted">Function to call if we decide not to load</param> /// <param name="overrideTimer">True if we absolutely positively must reload now even if it has been less than 5 seconds since the last one</param> /// <returns> /// True if we kicked off an actual refresh, false otherwise. /// </returns> public bool TryStartLoad(ITargetable newOrigin, LoadDoneCallback partialLoaded, LoadDoneCallback fullyLoaded, LoadDoneCallback aborted, bool overrideTimer = false) { if (newOrigin != null) { // 1. Check whether we should even do anything, if not call aborted() and return if (!AllowStart(newOrigin, overrideTimer)) { if (aborted != null) { aborted(); } return(false); } else if (calculator != null && calculator.IsAlive) { if (aborted != null) { aborted(); } return(false); } else { // 2. Start background job calculator = new Thread( () => ThreadStart(newOrigin, partialLoaded, fullyLoaded, aborted) ) { Name = "Astrogator transfer calculator" }; calculator.Start(); return(true); } } else { DbgFmt("Somebody tried to load with a null origin"); if (aborted != null) { aborted(); } return(false); } }
private void ThreadStart(ITargetable newOrigin, LoadDoneCallback partialLoaded, LoadDoneCallback fullyLoaded, LoadDoneCallback aborted) { lock (bgLoadMutex) { loading = true; // 3. In background, load the first pass of stuff model.Reset(newOrigin); if (PlaneChangesEnabled) { // Ejection burns are relatively cheap to calculate and needed for the display to look good RecalculateEjections(); if (partialLoaded != null) { partialLoaded(); // Let's only ever call it once. partialLoaded = null; } // 5. Load everything else RecalculatePlaneChanges(); if (fullyLoaded != null) { fullyLoaded(); } } else { // Ejection burns are all we need RecalculateEjections(); if (fullyLoaded != null) { fullyLoaded(); } } lastUpdateTime = Planetarium.GetUniversalTime(); loading = false; } }
/// <summary> /// Construct a loader object for the given model /// </summary> /// <param name="m">Model object for us to manage</param> /// <param name="unReqNotif">Function to call if we trigger our own refresh without being asked (usually because a burn expired)</param> public AstrogationLoadBehaviorette(AstrogationModel m, LoadDoneCallback unReqNotif) { model = m; unrequestedLoadNotification = unReqNotif; }