Skip to content

Commit

Permalink
Only allocate CodeDomProvider when actually needed, simplify example …
Browse files Browse the repository at this point in the history
…plugin skeleton source code
  • Loading branch information
UnknownShadow200 committed Mar 26, 2021
1 parent fe6b838 commit 52d2c03
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
17 changes: 3 additions & 14 deletions MCGalaxy/Scripting/Backends.cs
Expand Up @@ -49,7 +49,7 @@ namespace MCGalaxy
\t\t// The command's name (what you put after a slash to use this command)
\t\tpublic override string name {{ get {{ return ""{0}""; }} }}
\t\t// Command's shortcut, can be left blank (e.g. ""/copy"" has a shortcut of ""c"")
\t\t// Command's shortcut, can be left blank (e.g. ""/Copy"" has a shortcut of ""c"")
\t\tpublic override string shortcut {{ get {{ return """"; }} }}
\t\t// Which submenu this command displays in under /Help
Expand Down Expand Up @@ -91,10 +91,8 @@ namespace MCGalaxy
\t{{
\t\tpublic override string name {{ get {{ return ""{0}""; }} }}
\t\tpublic override string MCGalaxy_Version {{ get {{ return ""{2}""; }} }}
\t\tpublic override int build {{ get {{ return 100; }} }}
\t\tpublic override string welcome {{ get {{ return ""Loaded Message!""; }} }}
\t\tpublic override string creator {{ get {{ return ""{1}""; }} }}
\t\tpublic override bool LoadAtStartup {{ get {{ return true; }} }}
\t\tpublic override void Load(bool startup)
\t\t{{
Expand Down Expand Up @@ -146,7 +144,7 @@ Namespace MCGalaxy
\t\t\tEnd Get
\t\tEnd Property
\t\t' Command's shortcut, can be left blank (e.g. ""/copy"" has a shortcut of ""c"")
\t\t' Command's shortcut, can be left blank (e.g. ""/Copy"" has a shortcut of ""c"")
\t\tPublic Overrides ReadOnly Property shortcut() As String
\t\t\tGet
\t\t\t\tReturn """"
Expand Down Expand Up @@ -200,6 +198,7 @@ Imports System
Namespace MCGalaxy
\tPublic Class {0}
\t\tInherits Plugin
\t\tPublic Overrides ReadOnly Property name() As String
\t\t\tGet
\t\t\t\tReturn ""{0}""
Expand All @@ -210,11 +209,6 @@ Namespace MCGalaxy
\t\t\t\tReturn ""{2}""
\t\t\tEnd Get
\t\t End Property
\t\tPublic Overrides ReadOnly Property build() As Integer
\t\t\tGet
\t\t\t\tReturn 100
\t\t\tEnd Get
\t\t End Property
\t\tPublic Overrides ReadOnly Property welcome() As String
\t\t\tGet
\t\t\t\tReturn ""Loaded Message!""
Expand All @@ -225,11 +219,6 @@ Namespace MCGalaxy
\t\t\t\tReturn ""{1}""
\t\t\tEnd Get
\t\t End Property
\t\tPublic Overrides ReadOnly Property LoadAtStartup() As Boolean
\t\t\tGet
\t\t\t\tReturn true
\t\t\tEnd Get
\t\t End Property
\t\tPublic Overrides Sub Load(startup As Boolean)
\t\t\t' LOAD YOUR PLUGIN WITH EVENTS OR OTHER THINGS!
Expand Down
21 changes: 14 additions & 7 deletions MCGalaxy/Scripting/Scripting.cs
Expand Up @@ -237,15 +237,20 @@ public abstract class ICompiler {

/// <summary> Compiles source code files from a particular language into a .dll file, using a CodeDomProvider for the compiler. </summary>
public abstract class ICodeDomCompiler : ICompiler {
protected CodeDomProvider compiler;
public abstract string ProviderName { get; }
readonly object compilerLock = new object();
CodeDomProvider compiler;

public abstract string ProviderName { get; }
/// <summary> Adds language-specific default arguments to list of arguments. </summary>
protected abstract void PrepareArgs(CompilerParameters args);

public ICodeDomCompiler() {
compiler = CodeDomProvider.CreateProvider(ProviderName);
if (compiler == null) {
// Lazy init compiler when it's actually needed
void InitCompiler() {
lock (compilerLock) {
if (compiler != null) return;
compiler = CodeDomProvider.CreateProvider(ProviderName);
if (compiler != null) return;

Logger.Log(LogType.Warning,
"WARNING: {0} compiler is missing, you will be unable to compile {1} files.",
ProviderName, Ext);
Expand Down Expand Up @@ -274,11 +279,13 @@ public abstract class ICodeDomCompiler : ICompiler {
if (dstPath != null) args.OutputAssembly = dstPath;
if (dstPath == null) args.GenerateInMemory = true;

AddReferences(lines, args);
PrepareArgs(args);
string source = lines.Join(Environment.NewLine)
.Replace("MCLawl", "MCGalaxy")
.Replace("MCForge", "MCGalaxy");

AddReferences(lines, args);
PrepareArgs(args);
InitCompiler();
return compiler.CompileAssemblyFromSource(args, source);
}
}
Expand Down

0 comments on commit 52d2c03

Please sign in to comment.