private static void CreateInitFile(string path, string initClassName) { using (StreamWriter streamWriter = new StreamWriter(path + initClassName + ".cs")) { CodeWriter code = new CodeWriter(streamWriter); GenerateHeader(code, initClassName); code.WriteLine("internal static Func<string, IntPtr> GetProcAddress;"); code.WriteLine(); code.WriteLine("public static void Init(Func<string, IntPtr> @procAddress)"); code.WriteOpenBraceAndIndent(); code.WriteLine("GetProcAddress = @procAddress;"); code.WriteCloseBraceAndDedent(); CloseFile(code); } }
private static void WriteCommands(CodeWriter code, VariableTypes types, List<string> requiredCommands, Dictionary<GLCommandGroup, List<GLCommand>> commands) { code.WriteLine("#region Commands"); int commandIID = requiredCommands.Count; // Get distinct elements and convert into a list again. List<string> distinct = requiredCommands.Distinct().ToList(); foreach (var command in distinct) { GLCommand obj = FindCommand(commands, command); if (obj != null) { var paramCode = ""; var argsInCall = ""; bool firstParam = true; foreach (var parameter in obj.GetParameters()) { if (!firstParam) { paramCode += ", "; argsInCall += ", "; } if (firstParam) { firstParam = false; } paramCode += types.GetCSName(parameter.Value) + " " + parameter.Key.Trim(); argsInCall += (parameter.Value.Contains("ref") ? "ref " : "") + parameter.Key.Trim(); } code.WriteLine("internal delegate {0} {1}Func({2});", types.GetCSName(obj.returnType), obj.name, paramCode); code.WriteLine("internal static {0}Func {0}Ptr;", obj.name); code.WriteLine("internal static void load{0}()", obj.name.Replace("gl", "")); code.WriteOpenBraceAndIndent(); code.WriteLine("try"); code.WriteOpenBraceAndIndent(); code.WriteLine(string.Format("{0}Ptr = ({0}Func)Marshal.GetDelegateForFunctionPointer(OpenGLInit.GetProcAddress(\"{0}\"), typeof({0}Func));", obj.name)); code.WriteCloseBraceAndDedent(); code.WriteLine("catch"); code.WriteOpenBraceAndIndent(); code.WriteLine("Console.WriteLine(\"Failed to get function pointer for '{0}'.\");", obj.name); // throw new InvalidOperationException code.WriteCloseBraceAndDedent(); code.WriteCloseBraceAndDedent(); code.WriteLine("public static {0} {1}({2}) => " + "{1}Ptr({3});", types.GetCSName(obj.returnType), obj.name, paramCode, argsInCall); // => " + (returnType != "void" ? "return {1}Ptr({3});" : "{1}Ptr({3});") if (commandIID > 1) { code.WriteLine(); } commandIID--; } } code.WriteLine("#endregion"); }
private static void CloseFile(CodeWriter code) { code.WriteCloseBraceAndDedent(); code.WriteCloseBraceAndDedent(); }
private static void WriteInterop(CodeWriter code, string className, List<string> requiredCommands, Dictionary<GLCommandGroup, List<GLCommand>> commands) { code.WriteLine("#region Interop"); code.WriteLine("static {0}()", className); code.WriteOpenBraceAndIndent(); List<string> writtenInterop = new List<string>(); code.WriteLine("Console.WriteLine(\"Initalising {0} interop methods!\");", className); code.WriteLine(); code.WriteLine("if (OpenGLInit.GetProcAddress == null)"); code.WriteOpenBraceAndIndent(); code.WriteLine("throw new System.ArgumentException(\"Value OpenGLInit.GetProcAddress cannot be null, call \'OpenGLInit.Init(Func<string, IntPtr> @procAddress)\' before using OpenGl!\", \"OpenGLInit.GetProcAddress\");"); code.WriteCloseBraceAndDedent(); code.WriteLine(); foreach (var command in requiredCommands) { GLCommand obj = FindCommand(commands, command); if (obj != null) { code.WriteLine("load{0}();", obj.name.Replace("gl", "")); } } code.WriteCloseBraceAndDedent(); code.WriteLine("#endregion"); code.WriteLine(); }