<commit_before>﻿using FlatRedBall.Glue.CodeGeneration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlatRedBall.Glue.Plugins.Interfaces;
using FlatRedBall.Glue.CodeGeneration.CodeBuilder;
using FlatRedBall.Glue.SaveClasses;

namespace LiveUpdater.CodeGeneration
{
    public class ObjectRegistrationCodeGenerator : ElementComponentCodeGenerator
    {
        public override CodeLocation CodeLocation
        {
            get
            {
                return CodeLocation.AfterStandardGenerated; 
            }
        }

        public override ICodeBlock GenerateFields(ICodeBlock codeBlock, IElement element)
        {
            if (element is ScreenSave)
            {
                codeBlock.Line("OcularPlane.Networking.WcfTcp.Host.OcularPlaneHost host;");
            }
            return codeBlock;
        }

        public override ICodeBlock GenerateAddToManagers(ICodeBlock codeBlock, IElement element)
        {
            if(element is ScreenSave)
            {
                codeBlock.Line(
                    "var containerManager = new OcularPlane.ContainerManager();");

                string screenName = element.ClassName;

                foreach (var namedObject in element.NamedObjects)
                {
                    bool shouldAddAsObject = namedObject.IsList == false;
                    if (shouldAddAsObject)
                    {
                        string line =
                            $"containerManager.AddObjectToContainer(nameof({screenName}), " +
                            $"{namedObject.InstanceName}, nameof({namedObject.InstanceName}));";

                        codeBlock.Line(line);
                    }
                }
                codeBlock.Line(
                    "host = new OcularPlane.Networking.WcfTcp.Host.OcularPlaneHost(containerManager, \"localhost\", 9999);");
            }


            return codeBlock;
        }

        public override void GenerateRemoveFromManagers(ICodeBlock codeBlock, IElement element)
        {
            if (element is ScreenSave)
            {
                codeBlock.Line("host.Dispose();");
            }
        }
    }
}
<commit_msg>Moved ContainerManager declaration to class scope so it can be used in custom code too.<commit_after>﻿using FlatRedBall.Glue.CodeGeneration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FlatRedBall.Glue.Plugins.Interfaces;
using FlatRedBall.Glue.CodeGeneration.CodeBuilder;
using FlatRedBall.Glue.SaveClasses;

namespace LiveUpdater.CodeGeneration
{
    public class ObjectRegistrationCodeGenerator : ElementComponentCodeGenerator
    {
        public override CodeLocation CodeLocation
        {
            get
            {
                return CodeLocation.AfterStandardGenerated; 
            }
        }

        public override ICodeBlock GenerateFields(ICodeBlock codeBlock, IElement element)
        {
            if (element is ScreenSave)
            {
                codeBlock.Line("OcularPlane.Networking.WcfTcp.Host.OcularPlaneHost host;");
                codeBlock.Line(
                    "OcularPlane.ContainerManager containerManager = new OcularPlane.ContainerManager();");

            }
            return codeBlock;
        }

        public override ICodeBlock GenerateAddToManagers(ICodeBlock codeBlock, IElement element)
        {
            if(element is ScreenSave)
            {
                string screenName = element.ClassName;

                foreach (var namedObject in element.NamedObjects)
                {
                    bool shouldAddAsObject = namedObject.IsList == false;
                    if (shouldAddAsObject)
                    {
                        string line =
                            $"containerManager.AddObjectToContainer(nameof({screenName}), " +
                            $"{namedObject.InstanceName}, nameof({namedObject.InstanceName}));";

                        codeBlock.Line(line);
                    }
                }
                codeBlock.Line(
                    "host = new OcularPlane.Networking.WcfTcp.Host.OcularPlaneHost(containerManager, \"localhost\", 9999);");
            }


            return codeBlock;
        }

        public override void GenerateRemoveFromManagers(ICodeBlock codeBlock, IElement element)
        {
            if (element is ScreenSave)
            {
                codeBlock.Line("host.Dispose();");
            }
        }
    }
}

{'fraction_non_alphanumeric': 0.07459105779716467, 'fraction_numerical': 0.0017448200654307524, 'mean_word_length': 1.737910447761194, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5998768', 'n_tokens_mistral': 1153, 'n_tokens_neox': 1084, 'n_words': 216}