示例#1
0
        public void convertStruct(string path, List <string> files)
        {
            StructConverter sc = new StructConverter();
            // Parse the enum OMX_INDEXTYPE to get the references
            CStruct OMX_INDEXTYPE = sc.convert(StructParser.parse(File.ReadAllText(Path.Combine(path, "source", "OMX_Index.h")), "enum").First());

            List <Struct> cstruct = new List <Struct>();

            foreach (string file in files)
            {
                string        sourcestring = File.ReadAllText(Path.Combine(path, "source", file + ".h"));
                List <Struct> structs      = sc.convert(StructParser.parse(sourcestring, "struct"), file);
                cstruct.AddRange(structs);

                Directory.CreateDirectory(Path.Combine(path, @"..\..\..\lib\classes"));
                using (StreamWriter sw = new StreamWriter(Path.GetFullPath(Path.Combine(path, @"..\..\..\lib\classes", file + ".ts"))))
                {
                    writeTs(sw, structs);
                }
            }

            using (StreamWriter sw = new StreamWriter(Path.GetFullPath(Path.Combine(path, @"..\..\..\src", "ParametersGet.cpp"))))
            {
                sw.WriteLine(@"#include ""Parameters.h""");
                sw.WriteLine();
                writeGetterSetter(sw, cstruct, WriteType.get);
                sw.WriteLine(@"v8::Local<v8::Object> Parameters::GetParameter(OMX_HANDLETYPE *handle, int port, OMX_INDEXTYPE nParamIndex) {");
                sw.WriteLine(@"  Nan::EscapableHandleScope scope;");
                sw.WriteLine(@"  v8::Local<v8::Object> ret = Nan::New<v8::Object>();");
                sw.WriteLine(@"");
                writeAllCasesCpp(sw, OMX_INDEXTYPE, cstruct, WriteType.get);
                sw.WriteLine(@"  return scope.Escape(ret);");
                sw.WriteLine(@"}");
            }
            using (StreamWriter sw = new StreamWriter(Path.GetFullPath(Path.Combine(path, @"..\..\..\src", "ParametersSet.cpp"))))
            {
                sw.WriteLine(@"#include ""Parameters.h""");
                sw.WriteLine();
                writeGetterSetter(sw, cstruct, WriteType.set);
                sw.WriteLine(@"void Parameters::SetParameter(OMX_HANDLETYPE *handle, int port, OMX_INDEXTYPE nParamIndex, v8::Local<v8::Object> param) {");
                writeAllCasesCpp(sw, OMX_INDEXTYPE, cstruct, WriteType.set);
                sw.WriteLine(@"}");
            }
        }
示例#2
0
        private Dictionary <string, CStruct> getAllStructs(CStruct OMX_INDEXTYPE, List <CStruct> cstructs)
        {
            Dictionary <string, CStruct> res = new Dictionary <string, CStruct>();

            foreach (CField field in OMX_INDEXTYPE.fields)
            {
                if (field.reference.Length == 0)
                {
                    continue;
                }

                CStruct cstruct = cstructs.Where(a => a.name == field.reference).FirstOrDefault();
                if (cstruct != null)
                {
                    res.Add(field.name, cstruct);
                }
            }
            return(res);
        }
示例#3
0
        public static List <CStruct> parse(string sourcestring, string type)
        {
            List <CStruct> cslist = new List <CStruct>();

            Regex           re = new Regex(@"(typedef\s" + type + @"\s([^;]*?)\{(.*?)\})", RegexOptions.Singleline);
            MatchCollection mc = re.Matches(sourcestring);

            foreach (Match m in mc)
            {
                string name = m.Groups[2].Value.Trim();

                CStruct cs = new CStruct(name);

                string itemsStr = m.Groups[3].Value;

                string itemsOneLine = Regex.Replace(itemsStr, @"\/\*(.*?)\*\/", Callback, RegexOptions.Singleline);

                string[] items = itemsOneLine.Split(new char[] { '\n' });

                foreach (string item in items)
                {
                    string itemTrimmed = item.Trim();
                    if (itemTrimmed.Length == 0)
                    {
                        continue;
                    }

                    CField cf = parseLine(itemTrimmed);
                    if (cf != null)
                    {
                        cs.fields.Add(cf);
                    }
                    else
                    {
                        //Console.WriteLine("{0} is null", itemTrimmed);
                    }
                }

                cslist.Add(cs);
            }
            return(cslist);
        }
示例#4
0
        private void writeCaseGet(StreamWriter sw, string indexName, CStruct cstruct)
        {
            if (indexName == "OMX_IndexParamBrcmRecursionUnsafe" || indexName == "OMX_IndexParamSourceSeed")
            {
                return;
            }

            sw.WriteLine("    case {0}:", indexName);
            sw.WriteLine("    {");

            sw.WriteLine("      {0} format;", cstruct.name);

            bool   hasPort    = cstruct.fields.Any(a => a.name == "nPortIndex");
            string hasPortStr = hasPort ? ", port" : "";

            sw.WriteLine("      GetParameterTemplate(&format{0}, handle, nParamIndex);", hasPortStr);

            sw.WriteLine("      return scope.Escape(GET_{0}(format));", cstruct.name);

            sw.WriteLine("    }");
            sw.WriteLine("      break;");
        }
示例#5
0
        private void writeAllCasesCpp(StreamWriter sw, CStruct OMX_INDEXTYPE, List <CStruct> cstructs, WriteType t)
        {
            Dictionary <string, CStruct> allStructs = getAllStructs(OMX_INDEXTYPE, cstructs);

            sw.WriteLine(@"  switch (nParamIndex) {");
            foreach (var row in allStructs)
            {
                string  index   = row.Key;
                CStruct cstruct = row.Value;

                if (t == WriteType.get)
                {
                    writeCaseGet(sw, index, cstruct);
                }
                else
                {
                    writeCaseSet(sw, index, cstruct);
                }
            }
            sw.WriteLine(@"    default:");
            sw.WriteLine(@"      break;");
            sw.WriteLine(@"  }");
        }
示例#6
0
        private void writeFunctionSetter(StreamWriter sw, CStruct cstruct, bool writeBody, List <CStruct> cstructs)
        {
            if (cstruct.name.Length == 0)
            {
                return;
            }

            sw.Write(@"void SET_" + cstruct.name + "(" + cstruct.name + " &format, v8::Local<v8::Object> param)");
            sw.WriteLine(writeBody ? " {" : ";");
            if (!writeBody)
            {
                return;
            }

            foreach (CField f in cstruct.fields)
            {
                if (f.name == "nSize" || f.name == "nVersion" || f.name == "nPortIndex")
                {
                    continue;
                }

                // Remove the array info
                if (Regex.IsMatch(f.name, @"\[\w*?\]"))
                {
                    continue;
                }
                string nameNoArray = Regex.Replace(f.name, @"\[\w*?\]", "");

                if (
                    f.type == "OMX_STRING" ||
                    f.type == "OMX_PTR" ||
                    f.type == "OMX_BRCM_POOL_T" ||
                    f.type == "struct" ||
                    f.type == "OMX_BRCM_PERFSTATS" ||
                    f.type == "OMX_CONFIG_LENSCALIBRATIONVALUETYPE"
                    )
                {
                    continue;
                }

                // Special code for OMX_IndexParamPortDefinition
                if (cstruct.name == "OMX_PARAM_PORTDEFINITIONTYPE" && new string[] { "audio", "video", "image", "other" }.Contains(nameNoArray))
                {
                    sw.WriteLine(@"  if (format.eDomain == OMX_PortDomain" + Utils.FirstCharToUpper(nameNoArray) + ") {");
                    sw.WriteLine(@"    v8::Local<v8::Object> obj = Nan::To<v8::Object>(Nan::Get(param, Nan::New(""{0}"").ToLocalChecked()).ToLocalChecked()).ToLocalChecked();", nameNoArray);
                    sw.WriteLine(@"    SET_OMX_{1}_PORTDEFINITIONTYPE(format.format.{0}, obj);", nameNoArray, nameNoArray.ToUpper());
                    sw.WriteLine(@"  }");
                    continue;
                }

                bool isObject = cstructs.Where(a => a.name == f.type).Count() > 0;

                if (isObject)
                {
                    sw.WriteLine(@"  if (Nan::Has(param, Nan::New(""{0}"").ToLocalChecked()).FromJust())", nameNoArray);
                    sw.WriteLine(@"  {");
                    sw.WriteLine(@"    SET_{1}(format.{0}, Nan::To<v8::Object>(Nan::Get(param, Nan::New(""{0}"").ToLocalChecked()).ToLocalChecked()).ToLocalChecked());", nameNoArray, f.type);
                    sw.WriteLine(@"  }");
                }
                else
                {
                    sw.WriteLine(@"  format.{1} = ({0}) Nan::To<int>(Nan::Get(param, Nan::New(""{1}"").ToLocalChecked()).ToLocalChecked()).FromJust();{2}", f.type, nameNoArray, f.comment.Length == 0 ? "" : " // " + f.comment);
                }
            }
            sw.WriteLine(@"}");
        }
示例#7
0
        private void writeFunctionGetter(StreamWriter sw, CStruct cstruct, bool writeBody, List <CStruct> cstructs)
        {
            if (cstruct.name.Length == 0)
            {
                return;
            }
            sw.Write(@"v8::Local<v8::Object> GET_" + cstruct.name + "(" + cstruct.name + " &format)");
            sw.WriteLine(writeBody ? " {" : ";");
            if (!writeBody)
            {
                return;
            }
            sw.WriteLine(@"  Nan::EscapableHandleScope scope;");
            sw.WriteLine(@"  v8::Local<v8::Object> ret = Nan::New<v8::Object>();");

            foreach (CField f in cstruct.fields)
            {
                if (f.name == "nSize" || f.name == "nVersion" || f.name == "nPortIndex")
                {
                    continue;
                }

                // Remove the array info
                string nameNoArray = Regex.Replace(f.name, @"\[\w*?\]", "");

                string fname = nameNoArray;

                if (
                    f.type == "OMX_PTR" ||
                    f.type == "OMX_STATICBOX" ||
                    f.type == "OMX_BRCM_POOL_T" ||
                    f.type == "struct" ||
                    f.type == "OMX_BRCM_PERFSTATS" ||
                    f.type == "OMX_CONFIG_LENSCALIBRATIONVALUETYPE" ||
                    f.type == "OMX_FOCUSREGIONXY"
                    )
                {
                    continue;
                }

                // Special code for OMX_IndexParamPortDefinition
                if (cstruct.name == "OMX_PARAM_PORTDEFINITIONTYPE" && new string[] { "audio", "video", "image", "other" }.Contains(nameNoArray))
                {
                    sw.WriteLine(@"  if (format.eDomain == OMX_PortDomain" + Utils.FirstCharToUpper(nameNoArray) + ") {");
                    sw.WriteLine(@"    Nan::Set(ret, Nan::New(""{0}"").ToLocalChecked(), GET_OMX_{1}_PORTDEFINITIONTYPE(format.format.{0}));", nameNoArray, nameNoArray.ToUpper());
                    sw.WriteLine(@"  }");
                    continue;
                }

                bool canBeNull = f.type == "OMX_STRING";

                string castTo = null;
                if (f.type == "OMX_U64")
                {
                    castTo = "double";
                }

                if (canBeNull)
                {
                    sw.WriteLine(@"  if (format.{0} != NULL)", nameNoArray);
                    sw.Write("  ");
                }

                bool isObject = cstructs.Where(a => a.name == f.type).Count() > 0 && !blackList.Contains(f.type);

                if (isObject)
                {
                    sw.WriteLine(@"  Nan::Set(ret, Nan::New(""{0}"").ToLocalChecked(), GET_{1}(format.{0}));", nameNoArray, f.type);
                }
                else
                {
                    sw.WriteLine(@"  Nan::Set(ret, Nan::New(""{0}"").ToLocalChecked(), Nan::New({1}format.{2}){3});{4}", nameNoArray, castTo != null ? "(" + castTo + ")" : "", fname, canBeNull ? ".ToLocalChecked()" : "", f.comment.Length == 0 ? "" : " // " + f.comment);
                }
            }

            sw.WriteLine(@"  return scope.Escape(ret);");
            sw.WriteLine(@"}");
        }