﻿using System.Text;
using TweetDuck.Plugins.Enums;

namespace TweetDuck.Plugins{
    static class PluginScriptGenerator{
        public static string GenerateConfig(PluginConfig config){
            return config.AnyDisabled ? "window.TD_PLUGINS.disabled = [\""+string.Join("\",\"", config.DisabledPlugins)+"\"];" : string.Empty;
        }

        public static string GeneratePlugin(string pluginIdentifier, string pluginContents, int pluginToken, PluginEnvironment environment){
            StringBuilder build = new StringBuilder(2*pluginIdentifier.Length+pluginContents.Length+165);

            build.Append("(function(").Append(environment.GetScriptVariables()).Append("){");
            
            build.Append("let tmp={");
            build.Append("id:\"").Append(pluginIdentifier).Append("\",");
            build.Append("obj:new class extends PluginBase{").Append(pluginContents).Append("}");
            build.Append("};");
            
            build.Append("tmp.obj.$id=\"").Append(pluginIdentifier).Append("\";");
            build.Append("tmp.obj.$token=").Append(pluginToken).Append(";");
            build.Append("window.TD_PLUGINS.install(tmp);");

            build.Append("})(").Append(environment.GetScriptVariables()).Append(");");

            return build.ToString();
        }
    }
}

﻿using System.Globalization;
using TweetDuck.Plugins.Enums;

namespace TweetDuck.Plugins{
    static class PluginScriptGenerator{
        public static string GenerateConfig(PluginConfig config){
            return config.AnyDisabled ? "window.TD_PLUGINS.disabled = [\""+string.Join("\",\"", config.DisabledPlugins)+"\"];" : string.Empty;
        }

        public static string GeneratePlugin(string pluginIdentifier, string pluginContents, int pluginToken, PluginEnvironment environment){
            return PluginGen
                .Replace("%params", environment.GetScriptVariables())
                .Replace("%id", pluginIdentifier)
                .Replace("%token", pluginToken.ToString(CultureInfo.InvariantCulture))
                .Replace("%contents", pluginContents);
        }

        private const string PluginGen = "(function(%params,$d){let tmp={id:'%id',obj:new class extends PluginBase{%contents}};$d(tmp.obj,'$id',{value:'%id'});$d(tmp.obj,'$token',{value:%token});window.TD_PLUGINS.install(tmp);})(%params,Object.defineProperty);";

/* PluginGen

(function(%params, $i, $d){
  let tmp = {
    id: '%id',
    obj: new class extends PluginBase{%contents}
  };
  
  $d(tmp.obj, '$id', { value: '%id' });
  $d(tmp.obj, '$token', { value: %token });
  
  window.TD_PLUGINS.install(tmp);
})(%params, Object.defineProperty);

*/
    }
}

Make $id and $token properties in plugin objects unmodifiable
Make $id and $token properties in plugin objects unmodifiable
