예제 #1
0
        /// <summary>
        /// To the best-effort to restore script state
        /// </summary>
        /// <remarks>
        /// This method does not refresh the Script instance, it is the responsibility of a callee
        /// </remarks>
        private static void RestoreScriptState(Script sc, ScriptStateBackup backup)
        {
            List <string> ifaceSectionNames = sc.GetInterfaceSectionNames(false);

            // Restore selected state
            IniReadWriter.WriteKey(sc.RealPath, ScriptSection.Names.Main, Script.Const.Selected, backup.Selected.ToString());

            // Restore interfaces
            List <UIControl> newCtrls = new List <UIControl>();

            foreach (var kv in backup.IfaceSectionDict)
            {
                string           ifaceSectionName = kv.Key;
                List <UIControl> bakCtrls         = kv.Value;

                if (!ifaceSectionNames.Contains(ifaceSectionName))
                {
                    continue;
                }

                (List <UIControl> uiCtrls, _) = sc.GetInterfaceControls(ifaceSectionName);
                foreach (UIControl uiCtrl in uiCtrls)
                {
                    // Get old uiCtrl, equality identified by Type and Key.
                    UIControl bakCtrl = bakCtrls.FirstOrDefault(bak => bak.Type == uiCtrl.Type && bak.Key.Equals(uiCtrl.Key, StringComparison.OrdinalIgnoreCase));
                    if (bakCtrl == null)
                    {
                        continue;
                    }

                    // Get old value
                    string bakValue = bakCtrl.GetValue(false);
                    Debug.Assert(bakValue != null, "Internal Logic Error at FileUpdater.RestoreInterface");

                    // Add to newCtrls only if apply was successful
                    if (uiCtrl.SetValue(bakValue, false, out _))
                    {
                        newCtrls.Add(uiCtrl);
                    }
                }
            }

            // Write to file
            UIControl.Update(newCtrls);
        }
예제 #2
0
        private static bool RestoreInterface(ref Script sc, InterfaceSectionBackup backup)
        {
            (string ifaceSectionName, List <UIControl> uiCtrls, _) = sc.GetInterfaceControls();

            if (!ifaceSectionName.Equals(backup.SectionName, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            List <UIControl> bakCtrls = backup.ValueCtrls;
            List <UIControl> newCtrls = new List <UIControl>(uiCtrls.Count);

            foreach (UIControl uiCtrl in uiCtrls)
            {
                // Get old uiCtrl, equaility identified by Key and Type.
                UIControl bakCtrl = bakCtrls.FirstOrDefault(bak =>
                                                            bak.Key.Equals(uiCtrl.Key, StringComparison.OrdinalIgnoreCase) && bak.Type == uiCtrl.Type);
                if (bakCtrl == null)
                {
                    continue;
                }

                // Get old value
                string bakValue = bakCtrl.GetValue(false);
                Debug.Assert(bakValue != null, "Internal Logic Error at FileUpdater.RestoreInterface");

                // Add to newCtrls only if apply was successful
                if (uiCtrl.SetValue(bakValue, false, out _))
                {
                    newCtrls.Add(uiCtrl);
                }
            }

            // Write to file
            UIControl.Update(newCtrls);
            sc = sc.Project.RefreshScript(sc);
            return(true);
        }