Exemplo n.º 1
0
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            if (this.Viewport == null && ViewportTemplate != null)
            {
                this.Viewport = ViewportTemplate?.GetViewport();
            }

            InitializeViewport(null, this.Viewport);
        }
Exemplo n.º 2
0
        private static string EditCode(ViewportTemplate template, Viewport viewport, string source)
        {
            string patched = source;

            if (!ViewportHandling.IsMatch(source))
            {
                ConfigManager.LogManager.LogDebug($"adding viewport handling to '{viewport.RelativeInitFilePath}'");
                EnsureCrLf(ref patched);
                patched += "dofile(LockOn_Options.common_script_path..\"ViewportHandling.lua\")\r\n";
            }

            // if there are multiple viewports defined in the same file, we need to
            // use original viewport name to disambiguate and edit each separately
            MatchCollection matches  = TryFindAssignedViewport.Matches(source);
            Match           assigned = null;

            switch (matches.Count)
            {
            case 0:
                // nothing found, add new entry to end
                ConfigManager.LogManager.LogDebug($"adding viewport selection to '{viewport.RelativeInitFilePath}'");
                EnsureCrLf(ref patched);
                patched +=
                    $"try_find_assigned_viewport(\"{template.ViewportPrefix}_{viewport.ViewportName}\", \"{viewport.ViewportName}\")\r\n";
                return(patched);

            case 1:
                // only a single viewport accessed, ignore name mismatch
                assigned = matches[0];
                break;

            default:
                // ambiguity, match name of viewport or fail
                foreach (Match match in matches)
                {
                    string existingName = match.Groups[match.Groups[2].Success ? 2 : 1].Value;
                    if (!viewport.ViewportName.Equals(existingName))
                    {
                        continue;
                    }

                    // found the matching item we can update
                    assigned = match;
                    break;
                }

                break;
            }

            if (assigned == null)
            {
                throw new Exception(
                          $"viewport '{viewport.ViewportName}' not found in file '{viewport.RelativeInitFilePath}' accessing multiple viewports; ambiguity cannot be resolved");
            }

            ConfigManager.LogManager.LogDebug($"changing viewport selection in '{viewport.RelativeInitFilePath}'");
            string abstractName = assigned.Groups[assigned.Groups[2].Success ? 2 : 1].Value;

            patched = patched.Replace(assigned.Groups[0].Value,
                                      $"try_find_assigned_viewport(\"{template.ViewportPrefix}_{viewport.ViewportName}\", \"{abstractName}\")");
            return(patched);
        }