Beispiel #1
0
        public void AttachAndApply <TEntity>(
            IPatch <TEntity> patch, long id
            ) where TEntity : Entity
        {
            var entity = Activator.CreateInstance <TEntity>();

            entity.Id = id;

            patch.Apply(_context.Attach(entity).Entity, this);
        }
Beispiel #2
0
        public static CodeInstructionDifference CodeInstructionDifferencesBetweenOriginalAndTranspiledMethod(
            IPatch patch,
            MethodInfo patchedMethod,
            MethodInfo transpilerMethod)
        {
            List <CodeInstruction> originalInstructions = PatchProcessor.GetOriginalInstructions(patchedMethod);
            List <CodeInstruction> codeInstructionList;

            patch.Apply();
            codeInstructionList = (List <CodeInstruction>)transpilerMethod.Invoke(null, new object[] { originalInstructions });

            return(new CodeInstructionDifference(originalInstructions, codeInstructionList));
        }
Beispiel #3
0
        public T NewInstance <T>(IPatch <T> patch)
            where T : Entity
        {
            var instance = Activator.CreateInstance <T>();

            patch.Apply(instance, this);

            if (!instance.IsNew())
            {
                //don't allow that
                instance.Id = 0;
            }

            return(instance);
        }
        private void SetPatchToGrid(IPatch patch)
        {
            this.ClearPatchGrid();

            this.spPatchDisplay.Children.Add(new Label {
                Content = $"Name: {patch.Name}"
            });
            var statusLabel = new Label {
                Content = $"Status: {PatchStatus.None}"
            };

            this.spPatchDisplay.Children.Add(statusLabel);
            if (!string.IsNullOrWhiteSpace(patch.Description))
            {
                var desc = new TextBlock
                {
                    Text         = $"Description: {Environment.NewLine}{patch.Description}",
                    Margin       = new Thickness(5, 0, 0, 0),
                    TextWrapping = TextWrapping.Wrap
                };
                this.spPatchDisplay.Children.Add(desc);
            }

            const int maxButtonWidth = 250;
            var       btnApply       = new Button {
                Content = "Apply"
            };

            btnApply.Click += (sender, args) =>
            {
                var path = this._fileSelector.Path;
                if (!File.Exists(path))
                {
                    MessageBox.Show("No/Invalid path selected.");
                    return;
                }

                try
                {
                    if (!File.Exists(path + ".bk"))
                    {
                        File.Copy(path, path + ".bk");
                    }

                    using (var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
                    {
                        patch.Apply(stream);
                    }

                    this.RefreshStatus(path);

                    MessageBox.Show("Successfully patched!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            };
            //this.spPatchDisplay.Children.Add(btnApply);
            this.patchDisplayBtnApply.Children.Add(btnApply);

            var btnRemove = new Button {
                Content = "Remove"
            };

            btnRemove.Click += (sender, args) =>
            {
                var path = this._fileSelector.Path;
                if (!File.Exists(path))
                {
                    MessageBox.Show("No/Invalid path selected.");
                    return;
                }

                try
                {
                    using (var stream = new FileStream(path, FileMode.Open, FileAccess.ReadWrite))
                    {
                        patch.Remove(stream);
                    }

                    this.RefreshStatus(path);

                    MessageBox.Show("Successfully restored original bytes!");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            };

            //this.spPatchDisplay.Children.Add(btnRemove);
            this.patchDisplayBtnRemove.Children.Add(btnRemove);
            if (this._fileSelector.IsPathValid())
            {
                this.RefreshStatus(this._fileSelector.Path);
            }
        }
Beispiel #5
0
        public static async void ApplyPatches()
        {
            Assembly assembly = Assembly.Load(new AssemblyName("Patches"));

            IEnumerable <Type> types = assembly.GetTypes().Where(t => t.GetInterfaces().Contains(typeof(IPatch)));
            int  currentVersion      = 0;
            bool failed = false;
            int  index  = 0;

            KeyValuePair <string, object> settingValue = ApplicationData.Current.LocalSettings.Values.FirstOrDefault(v => v.Key == VERSION_KEY);

            if (settingValue.Key != null)
            {
                int.TryParse(settingValue.Value.ToString(), out currentVersion);
            }
            else
            {
                ApplicationData.Current.LocalSettings.Values[VERSION_KEY] = currentVersion;
            }

            Type lastPatch = types.OrderByDescending(t => t.Name).FirstOrDefault();

            if (lastPatch != null)
            {
                string[] parts       = lastPatch.Name.Split('_');
                int      lastVersion = int.Parse(parts[parts.Length - 2]);

                if (lastVersion > currentVersion)
                {
                    while (!failed && index < types.Count())
                    {
                        Type type = types.ElementAt(index);

                        parts = type.Name.Split('_');
                        int version = int.Parse(parts[parts.Length - 2]);

                        if (version > currentVersion)
                        {
                            IPatch patch = (IPatch)Activator.CreateInstance(type);

                            failed = await patch.Apply();

                            failed = !failed;

                            if (!failed)
                            {
                                currentVersion = version;

                                ApplicationData.Current.LocalSettings.Values[VERSION_KEY] = currentVersion;
                            }
                        }

                        index++;
                    }

                    if (failed)
                    {
                        // TODO: Add logging and notification to user.
                    }
                }
            }
        }