Last active
September 16, 2023 07:43
-
-
Save 0x25CBFC4F/93174afefe500a620bcc2f7a3660c75e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thanks Reddit. <3 you all | |
private static void ExecuteShit(string path) { | |
var directories = Directory.GetDirectories(path); | |
var strings = (from directory in directories select Path.GetFileName(directory) into d where d.StartsWith("#") select d.Substring(1)).OrderBy(f => f).ToList(); | |
var sb = new StringBuilder(); | |
foreach(var s in strings) { | |
sb.Append($"{s.Substring(1)}\r\n"); | |
} | |
CompileAndReturnMainMethod(sb.ToString())?.Invoke(null, null); | |
} | |
private static MethodInfo CompileAndReturnMainMethod(string shittyCode) { | |
var provider = new CSharpCodeProvider(); | |
var parameters = new CompilerParameters(); | |
parameters.ReferencedAssemblies.Add("System.dll"); // do you really need anything else? | |
parameters.ReferencedAssemblies.Add("System.Windows.Forms.dll"); | |
parameters.GenerateInMemory = true; | |
parameters.GenerateExecutable = false; | |
var results = provider.CompileAssemblyFromSource(parameters, shittyCode); | |
if (results.Errors.HasErrors) { | |
var sb = new StringBuilder(); | |
foreach(CompilerError error in results.Errors) { | |
sb.AppendLine($ "Error ({error.ErrorNumber}): {error.ErrorText}"); | |
} | |
MessageBox.Show(sb.ToString()); | |
return null; | |
} | |
var allTypes = results.CompiledAssembly.GetTypes(); | |
MethodInfo foundMethod = null; | |
foreach(var type in allTypes) { | |
foreach(var methodInfo in type.GetMethods().Where(methodInfo = >methodInfo.Name.Equals("Main"))) { | |
foundMethod = methodInfo; | |
break; | |
} | |
} | |
return foundMethod; | |
} |
If I were to actually want to try this out for myself, how would I go about compiling and running this program?
Just call ExecuteShit(string)
with string containing path to folder with folders.
For now, only 9 lines of code is supported because of .OrderBy()
behaviour. It compares only first character in the string.
So lines of code like:
#1 ....
#2 ....
<...>
#11
Will be ordered like:
#1
#10
#11
#2
Fix that issue and you're ready to go.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I were to actually want to try this out for myself, how would I go about compiling and running this program?