Skip to content

Instantly share code, notes, and snippets.

@0x25CBFC4F
Last active September 16, 2023 07:43
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0x25CBFC4F/93174afefe500a620bcc2f7a3660c75e to your computer and use it in GitHub Desktop.
Save 0x25CBFC4F/93174afefe500a620bcc2f7a3660c75e to your computer and use it in GitHub Desktop.
// 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;
}
@a-sync
Copy link

a-sync commented Sep 20, 2018

:suspect:

@Romejanic
Copy link

It's time to stop

@minghao912
Copy link

If I were to actually want to try this out for myself, how would I go about compiling and running this program?

@0x25CBFC4F
Copy link
Author

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